-
-
Notifications
You must be signed in to change notification settings - Fork 44
/
Main.java
executable file
·145 lines (127 loc) · 4 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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
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 com.google.common.io.Files;
import java.io.*;
import java.nio.file.*;
import java.nio.file.attribute.*;
import java.util.*;
import java.util.Map.*;
import java.util.jar.*;
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 {
try {
editJar_(jar, source);
} catch (Throwable t) {
t.printStackTrace();
Throwables.propagate(t);
}
}
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();
File generatedSrcDirectory = new File("./generated/");
generatedSrcDirectory = generatedSrcDirectory.getCanonicalFile();
if (source) {
if (generatedSrcDirectory.exists()) {
deleteDirectory(generatedSrcDirectory.toPath());
}
generatedSrcDirectory.mkdir();
}
// 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();
if (source && e.getValue().length > 0) {
File f = new File(generatedSrcDirectory, e.getKey());
f.getParentFile().mkdirs();
Files.write(e.getValue(), f);
}
}
ostream.close();
}
public static void deleteDirectory(Path path) throws IOException {
java.nio.file.Files.walkFileTree(path, new SimpleFileVisitor<Path>() {
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs)
throws IOException {
java.nio.file.Files.delete(file);
return FileVisitResult.CONTINUE;
}
@Override
public FileVisitResult visitFileFailed(Path file, IOException exc) throws IOException {
// try to delete the file anyway, even if its attributes
// could not be read, since delete-only access is
// theoretically possible
java.nio.file.Files.delete(file);
return FileVisitResult.CONTINUE;
}
@Override
public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
if (exc == null) {
java.nio.file.Files.delete(dir);
return FileVisitResult.CONTINUE;
} else {
// directory iteration failed; propagate exception
throw exc;
}
}
});
}
}