-
-
Notifications
You must be signed in to change notification settings - Fork 44
/
ClassRegistry.java
189 lines (175 loc) · 6.46 KB
/
ClassRegistry.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
package me.nallar.tickthreading.patcher;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
import java.util.jar.JarInputStream;
import java.util.jar.JarOutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipException;
import java.util.zip.ZipFile;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;
import javassist.ClassPath;
import javassist.ClassPool;
import javassist.CtClass;
import javassist.NotFoundException;
import me.nallar.tickthreading.Log;
import me.nallar.tickthreading.util.EnumerableWrapper;
public class ClassRegistry {
private static final byte[] BUFFER = new byte[1024 * 1024];
private Map<String, File> classNameToLocation = new HashMap<String, File>();
private Map<File, Integer> locationToPatchHash = new HashMap<File, Integer>();
private Map<File, Integer> expectedPatchHashes = new HashMap<File, Integer>();
private Set<File> updatedFiles = new HashSet<File>();
private Set<String> unsafeClassNames = new HashSet<String>();
private Set<ClassPath> classPathSet = new HashSet<ClassPath>();
private Map<String, byte[]> replacementFiles = new HashMap<String, byte[]>();
private ClassPool classes = new ClassPool(false);
public void loadJars(File folder) throws IOException {
if (!folder.isDirectory()) {
throw new IllegalArgumentException(folder + " isn't a directory");
}
for (File file : folder.listFiles()) {
if (file.getName().toLowerCase().endsWith(".jar")) {
loadJar(new JarFile(file));
}
}
}
public void loadJar(JarFile jar) throws IOException {
loadZip(jar);
// TODO: Remove code signing?
}
public void loadZip(ZipFile zip) throws IOException {
File file = new File(zip.getName());
try {
classPathSet.add(classes.appendClassPath(file.getAbsolutePath()));
} catch (Exception e) {
Log.severe("Javassist could not load " + file, e);
}
for (ZipEntry zipEntry : new EnumerableWrapper<ZipEntry>((Enumeration<ZipEntry>) zip.entries())) {
String name = zipEntry.getName();
if (name.endsWith(".class")) {
String className = name.replace('/', '.').substring(0, name.lastIndexOf('.'));
if (classNameToLocation.containsKey(className)) {
unsafeClassNames.add(className);
}
classNameToLocation.put(className, file);
} else if (name.equals("TickThreading.hash")) {
ByteArrayOutputStream output = new ByteArrayOutputStream();
copy(zip.getInputStream(zipEntry), output);
int hash = Integer.valueOf(new String(output.toByteArray(), "UTF-8"));
locationToPatchHash.put(file, hash);
}
}
zip.close();
}
public void update(String className, byte[] replacement) {
Log.severe(className + " is in multiple jars. Patching may not work correctly.");
updatedFiles.add(classNameToLocation.get(className));
className = className.replace('.', '/') + ".class";
replacementFiles.put(className, replacement);
}
public static void copy(InputStream input, OutputStream output) throws IOException {
for (int read = input.read(BUFFER); read > -1; read = input.read(BUFFER)) {
output.write(BUFFER, 0, read);
}
}
public void finishModifications() {
for (ClassPath classPath : classPathSet) {
classes.removeClassPath(classPath);
}
}
public void save() throws IOException {
finishModifications();
File tempFile = null, renameFile = null;
ZipInputStream zin = null;
ZipOutputStream zout = null;
try {
for (File zipFile : updatedFiles) {
tempFile = File.createTempFile(zipFile.getName(), null);
tempFile.delete();
if (zipFile.renameTo(tempFile)) {
renameFile = zipFile;
tempFile.deleteOnExit();
} else {
throw new IOException("Couldn't rename " + zipFile + " -> " + tempFile);
}
if (isJar(zipFile)) {
zin = new JarInputStream(new FileInputStream(tempFile));
zout = new JarOutputStream(new FileOutputStream(zipFile));
} else {
zin = new ZipInputStream(new FileInputStream(tempFile));
zout = new ZipOutputStream(new FileOutputStream(zipFile));
}
Set<String> replacements = new HashSet<String>();
ZipEntry zipEntry;
while ((zipEntry = zin.getNextEntry()) != null) {
if (replacementFiles.containsKey(zipEntry.getName())) {
replacements.add(zipEntry.getName());
} else {
// TODO: Ignore meta-inf?
zout.putNextEntry(isJar(zipFile) ? new JarEntry(zipEntry.getName()) : new ZipEntry(zipEntry.getName()));
copy(zin, zout);
}
}
for (String name : replacements) {
zout.putNextEntry(isJar(zipFile) ? new JarEntry(name) : new ZipEntry(name));
zout.write(replacementFiles.get(name));
zout.closeEntry();
}
zout.putNextEntry(isJar(zipFile) ? new JarEntry("TickThreading.hash") : new ZipEntry("TickThreading.hash"));
String patchHash = String.valueOf(expectedPatchHashes.get(zipFile));
zout.write(patchHash.getBytes("UTF-8"));
Log.info("Patched " + replacements.size() + " classes in " + zipFile.getName() + ", patchHash: " + patchHash);
zin.close();
zout.close();
tempFile.delete();
renameFile = null;
}
} catch (ZipException e) {
zin.close();
zout.close();
if (renameFile != null && tempFile != null) {
tempFile.renameTo(renameFile);
}
throw e;
}
}
public CtClass getClass(String className) throws NotFoundException {
return classes.get(className);
}
public void loadPatchHashes(PatchManager patchManager) {
Map<String, Integer> patchHashes = patchManager.getHashes();
for (String clazz : patchHashes.keySet()) {
File location = classNameToLocation.get(clazz);
if (location == null) {
continue;
}
int hash = patchHashes.get(clazz);
Integer currentHash = expectedPatchHashes.get(location);
expectedPatchHashes.put(location, (currentHash == null) ? hash : currentHash * 31 + hash);
}
}
public boolean shouldPatch() {
for (File file : expectedPatchHashes.keySet()) {
if (!file.equals(locationToPatchHash.get(file))) {
return true;
}
}
return false;
}
private static boolean isJar(File file) {
return file.getName().toLowerCase().endsWith(".jar");
}
}