Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
lDEVinux
GitHub Repository: lDEVinux/eaglercraft
Path: blob/main/epkcompiler/src/CompilePackage.java
8641 views
1
import java.io.ByteArrayOutputStream;
2
import java.io.File;
3
import java.io.FileInputStream;
4
import java.io.FileOutputStream;
5
import java.io.IOException;
6
import java.io.InputStream;
7
import java.io.OutputStream;
8
import java.nio.charset.Charset;
9
import java.nio.charset.StandardCharsets;
10
import java.text.SimpleDateFormat;
11
import java.util.ArrayList;
12
import java.util.Date;
13
14
import com.jcraft.jzlib.CRC32;
15
import com.jcraft.jzlib.Deflater;
16
import com.jcraft.jzlib.DeflaterOutputStream;
17
import com.jcraft.jzlib.GZIPOutputStream;
18
19
public class CompilePackage {
20
21
private static ArrayList<File> files = new ArrayList();
22
23
public static void main(String[] args) throws IOException {
24
if(args.length < 2 || args.length > 4) {
25
System.out.println("Usage: java -jar CompilePackage.jar <input directory> <output file> [gzip|zlib|none] [file-type]");
26
return;
27
}
28
29
File root = new File(args[0]);
30
File output = new File(args[1]);
31
char compressionType;
32
33
if(args.length > 2) {
34
if(args[2].equalsIgnoreCase("gzip")) {
35
compressionType = 'G';
36
}else if(args[2].equalsIgnoreCase("zlib")) {
37
compressionType = 'Z';
38
}else if(args[2].equalsIgnoreCase("none")) {
39
compressionType = '0';
40
}else {
41
throw new IllegalArgumentException("Unknown compression method: " + args[2]);
42
}
43
}else {
44
compressionType = 'G';
45
}
46
47
listDirectory(root);
48
ByteArrayOutputStream osb = new ByteArrayOutputStream();
49
String start = root.getAbsolutePath();
50
51
osb.write("EAGPKG$$".getBytes(Charset.forName("UTF-8")));
52
53
String chars = "ver2.0";
54
osb.write(chars.length());
55
osb.write(chars.getBytes(StandardCharsets.US_ASCII));
56
57
Date d = new Date();
58
59
String comment = "\n\n # Eagler EPK v2.0 (c) " + (new SimpleDateFormat("yyyy")).format(d) + " Calder Young\n" +
60
" # update: on " + (new SimpleDateFormat("MM/dd/yyyy")).format(d) + " at " +
61
(new SimpleDateFormat("hh:mm:ss aa")).format(d) + "\n\n";
62
63
String nm = output.getName();
64
osb.write(nm.length());
65
osb.write(nm.getBytes(StandardCharsets.US_ASCII));
66
67
writeShort(comment.length(), osb);
68
osb.write(comment.getBytes(StandardCharsets.US_ASCII));
69
70
writeLong(d.getTime(), osb);
71
writeInt(files.size() + 1, osb);
72
73
osb.write(compressionType);
74
75
OutputStream os;
76
77
if(compressionType == 'G') {
78
os = new GZIPOutputStream(osb, new Deflater(9, 15+16), 16384, true);
79
}else if(compressionType == 'Z') {
80
os = new DeflaterOutputStream(osb, new Deflater(9), 16384, true);
81
}else {
82
os = osb;
83
}
84
85
os.write("HEAD".getBytes(StandardCharsets.US_ASCII));
86
String key = "file-type";
87
os.write(key.length());
88
os.write(key.getBytes(StandardCharsets.US_ASCII));
89
String value;
90
if(args.length > 3) {
91
value = args[3];
92
}else {
93
value = "epk/resources";
94
}
95
writeInt(value.length(), os);
96
os.write(value.getBytes(StandardCharsets.US_ASCII));
97
os.write('>');
98
99
CRC32 checkSum = new CRC32();
100
for(File f : files) {
101
InputStream stream = new FileInputStream(f);
102
byte[] targetArray = new byte[(int)f.length()];
103
stream.read(targetArray);
104
stream.close();
105
106
checkSum.reset();
107
checkSum.update(targetArray, 0, targetArray.length);
108
int ch = (int)checkSum.getValue();
109
110
os.write("FILE".getBytes(StandardCharsets.US_ASCII));
111
112
String p = f.getAbsolutePath().replace(start, "").replace('\\', '/');
113
if(p.startsWith("/")) {
114
p = p.substring(1);
115
}
116
os.write(p.length());
117
os.write(p.getBytes(StandardCharsets.US_ASCII));
118
writeInt(targetArray.length + 5, os);
119
writeInt(ch, os);
120
121
os.write(targetArray);
122
os.write(':');
123
os.write('>');
124
}
125
126
os.write("END$".getBytes(StandardCharsets.US_ASCII));
127
os.close();
128
129
osb.write(":::YEE:>".getBytes(StandardCharsets.US_ASCII));
130
131
FileOutputStream out = new FileOutputStream(output);
132
out.write(osb.toByteArray());
133
out.close();
134
}
135
136
public static void writeShort(int i, OutputStream os) throws IOException {
137
os.write((i >> 8) & 0xFF);
138
os.write(i & 0xFF);
139
}
140
141
public static void writeInt(int i, OutputStream os) throws IOException {
142
os.write((i >> 24) & 0xFF);
143
os.write((i >> 16) & 0xFF);
144
os.write((i >> 8) & 0xFF);
145
os.write(i & 0xFF);
146
}
147
148
public static void writeLong(long i, OutputStream os) throws IOException {
149
os.write((int)((i >> 56) & 0xFF));
150
os.write((int)((i >> 48) & 0xFF));
151
os.write((int)((i >> 40) & 0xFF));
152
os.write((int)((i >> 32) & 0xFF));
153
os.write((int)((i >> 24) & 0xFF));
154
os.write((int)((i >> 16) & 0xFF));
155
os.write((int)((i >> 8) & 0xFF));
156
os.write((int)(i & 0xFF));
157
}
158
159
public static void listDirectory(File dir) {
160
for(File f : dir.listFiles()) {
161
if(f.isDirectory()) {
162
listDirectory(f);
163
}else {
164
files.add(f);
165
}
166
}
167
}
168
169
}
170
171