-
-
Notifications
You must be signed in to change notification settings - Fork 44
/
Main.java
executable file
·90 lines (79 loc) · 2.42 KB
/
Main.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
package nallar.nmsprepatcher;
import com.google.common.base.Charsets;
import com.google.common.base.Throwables;
import com.google.common.collect.Maps;
import com.google.common.io.ByteStreams;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.HashMap;
import java.util.Map.Entry;
import java.util.jar.JarEntry;
import java.util.jar.JarInputStream;
import java.util.jar.JarOutputStream;
public class Main {
/**
* Called to load patches from a given directory
*
* @param patchDirectory patch directory
*/
public static void loadPatches(File patchDirectory) {
try {
PrePatcher.loadPatches(patchDirectory);
} catch (Throwable t) {
t.printStackTrace();
Throwables.propagate(t);
}
}
/**
* @param path name of the class, seperated by '/'
* @param bytes bytes of the class
* @return
*/
private static byte[] manipulateBinary(String path, byte[] bytes) {
return PrePatcher.patchCode(bytes, path);
}
/**
* @param path name of the class, seperated by '/'
* @param source of the class
* @return
*/
private static String manipulateSource(String path, String source) {
return PrePatcher.patchSource(source, path);
}
/**
* @param jar File
* @param source if TRUE source, if FALSE binary
*/
public static void editJar(File jar, boolean source) throws Exception {
HashMap<String, byte[]> stuff = Maps.newHashMap();
// READING
JarInputStream istream = new JarInputStream(new FileInputStream(jar));
JarEntry entry;
while ((entry = istream.getNextJarEntry()) != null) {
byte[] classBytes = ByteStreams.toByteArray(istream);
if (entry.getName().endsWith(source ? ".java" : ".class")) {
// PARSING
String name = entry.getName().replace('\\', '/');
if (source) {
String str = new String(classBytes, Charsets.UTF_8);
str = manipulateSource(name, str);
classBytes = str.getBytes(Charsets.UTF_8);
} else {
classBytes = manipulateBinary(name, classBytes);
}
}
stuff.put(entry.getName(), classBytes);
istream.closeEntry();
}
istream.close();
// WRITING
JarOutputStream ostream = new JarOutputStream(new FileOutputStream(jar));
for (Entry<String, byte[]> e : stuff.entrySet()) {
ostream.putNextEntry(new JarEntry(e.getKey()));
ostream.write(e.getValue());
ostream.closeEntry();
}
ostream.close();
}
}