-
-
Notifications
You must be signed in to change notification settings - Fork 44
/
Patcher.java
323 lines (294 loc) · 11.4 KB
/
Patcher.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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
package nallar.tickthreading.patcher;
import javassist.CannotCompileException;
import javassist.ClassLoaderPool;
import javassist.ClassPool;
import javassist.CtBehavior;
import javassist.CtClass;
import javassist.CtConstructor;
import javassist.CtMethod;
import javassist.NotFoundException;
import nallar.tickthreading.Log;
import nallar.tickthreading.mappings.MethodDescription;
import nallar.tickthreading.util.CollectionsUtil;
import nallar.tickthreading.util.DomUtil;
import nallar.unsafe.UnsafeUtil;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import java.io.*;
import java.lang.annotation.*;
import java.lang.reflect.*;
import java.util.*;
public class Patcher {
private final ClassPool classPool;
private final ClassPool preSrgClassPool;
private Object patchClassInstance;
private Object preSrgPatchClassInstance;
private Map<String, PatchMethodDescriptor> patchMethods = new HashMap<String, PatchMethodDescriptor>();
private Map<String, PatchGroup> classToPatchGroup = new HashMap<String, PatchGroup>();
public Patcher(InputStream config, Class<?> patchesClass) {
for (Method method : patchesClass.getDeclaredMethods()) {
for (Annotation annotation : method.getDeclaredAnnotations()) {
if (annotation instanceof Patch) {
PatchMethodDescriptor patchMethodDescriptor = new PatchMethodDescriptor(method, (Patch) annotation);
patchMethods.put(patchMethodDescriptor.name, patchMethodDescriptor);
}
}
}
classPool = new ClassLoaderPool(false);
preSrgClassPool = new ClassLoaderPool(true);
try {
patchClassInstance = patchesClass.getDeclaredConstructors()[0].newInstance(classPool);
preSrgPatchClassInstance = patchesClass.getDeclaredConstructors()[0].newInstance(preSrgClassPool);
} catch (Exception e) {
Log.severe("Failed to instantiate patch class", e);
}
try {
readPatchesFromXmlDocument(DomUtil.readDocumentFromInputStream(config));
} catch (Throwable t) {
throw UnsafeUtil.throwIgnoreChecked(t);
}
}
private void readPatchesFromXmlDocument(Document document) {
List<Element> patchGroupElements = DomUtil.elementList(document.getDocumentElement().getChildNodes());
for (Element patchGroupElement : patchGroupElements) {
new PatchGroup(patchGroupElement);
}
}
public byte[] preSrgTransformation(String name, String transformedName, byte[] originalBytes) {
PatchGroup patchGroup = getPatchGroup(name);
if (patchGroup != null && patchGroup.preSrg) {
return patchGroup.getClassBytes(name);
}
return originalBytes;
}
private PatchGroup getPatchGroup(String name) {
return classToPatchGroup.get(name);
}
public byte[] postSrgTransformation(String name, String transformedName, byte[] originalBytes) {
PatchGroup patchGroup = getPatchGroup(transformedName);
if (patchGroup != null && !patchGroup.preSrg) {
return patchGroup.getClassBytes(transformedName);
}
return originalBytes;
}
private class PatchGroup {
public final String name;
public final boolean preSrg;
public final boolean onDemand;
public final ClassPool classPool;
private final Map<String, ClassPatchDescriptor> patches;
private final Map<String, byte[]> patchedBytes = new HashMap<String, byte[]>();
private final List<ClassPatchDescriptor> classPatchDescriptors = new ArrayList<ClassPatchDescriptor>();
private boolean ranPatches = false;
private PatchGroup(Element element) {
Map<String, String> attributes = DomUtil.getAttributes(element);
name = element.getTagName();
preSrg = attributes.containsKey("preSrg");
if (preSrg) {
classPool = preSrgClassPool;
} else {
classPool = Patcher.this.classPool;
}
onDemand = attributes.containsKey("onDemand");
patches = onDemand ? new HashMap<String, ClassPatchDescriptor>() : null;
for (Element classElement : DomUtil.elementList(element.getChildNodes())) {
ClassPatchDescriptor classPatchDescriptor = new ClassPatchDescriptor(classElement);
classPatchDescriptors.add(classPatchDescriptor);
classToPatchGroup.put(classPatchDescriptor.name, this);
if (onDemand) {
if (patches.put(classPatchDescriptor.name, classPatchDescriptor) != null) {
throw new Error("Duplicate class patch for " + classPatchDescriptor.name + ", but onDemand is set.");
}
}
}
}
public byte[] getClassBytes(String name) {
if (onDemand) {
try {
return patches.get(name).runPatches().toBytecode();
} catch (Throwable t) {
Log.severe("Failed to patch " + name + " in patch group " + name + '.', t);
}
} else {
runPatchesIfNeeded();
}
byte[] bytes = patchedBytes.remove(name);
if (bytes == null) {
throw new RuntimeException("Class " + name + " not in this patch group.");
}
return bytes;
}
private void runPatchesIfNeeded() {
if (ranPatches) {
return;
}
ranPatches = true;
for (ClassPatchDescriptor classPatchDescriptor : classPatchDescriptors) {
try {
patchedBytes.put(classPatchDescriptor.name, classPatchDescriptor.runPatches().toBytecode());
} catch (Throwable t) {
Log.severe("Failed to patch " + name + " in patch group " + name + '.', t);
}
}
}
private class ClassPatchDescriptor {
private final Map<String, String> attributes;
public final String name;
public final List<PatchDescriptor> patches = new ArrayList<PatchDescriptor>();
private ClassPatchDescriptor(Element element) {
attributes = DomUtil.getAttributes(element);
name = attributes.get("id");
for (Element patchElement : DomUtil.elementList(element.getChildNodes())) {
patches.add(new PatchDescriptor(patchElement));
}
}
public CtClass runPatches() throws NotFoundException {
CtClass ctClass = classPool.get(name);
for (PatchDescriptor patchDescriptor : patches) {
PatchMethodDescriptor patchMethodDescriptor = patchMethods.get(patchDescriptor.getPatch());
patchMethodDescriptor.run(patchDescriptor, ctClass, preSrg ? preSrgPatchClassInstance : patchClassInstance);
}
return ctClass;
}
}
}
private static class PatchDescriptor {
private final Map<String, String> attributes;
private final String methods;
private final String patch;
public PatchDescriptor(Element element) {
attributes = DomUtil.getAttributes(element);
methods = element.getTextContent().trim();
patch = element.getTagName();
}
public String set(String name, String value) {
return attributes.put(name, value);
}
public String get(String name) {
return attributes.get(name);
}
public Map<String, String> getAttributes() {
return attributes;
}
public String getMethods() {
return methods;
}
public String getPatch() {
return patch;
}
}
public class PatchMethodDescriptor {
public final String name;
public final List<String> requiredAttributes;
public final Method patchMethod;
public final boolean isClassPatch;
public final boolean emptyConstructor;
public PatchMethodDescriptor(Method method, Patch patch) {
String name = patch.name();
if (Arrays.asList(method.getParameterTypes()).contains(Map.class)) {
this.requiredAttributes = CollectionsUtil.split(patch.requiredAttributes());
} else {
this.requiredAttributes = null;
}
if (name == null || name.isEmpty()) {
name = method.getName();
}
this.name = name;
emptyConstructor = patch.emptyConstructor();
isClassPatch = method.getParameterTypes()[0].equals(CtClass.class);
patchMethod = method;
}
public Object run(PatchDescriptor patchDescriptor, CtClass ctClass, Object patchClassInstance) {
String methods = patchDescriptor.getMethods();
Map<String, String> attributes = patchDescriptor.getAttributes();
Map<String, String> attributesClean = new HashMap<String, String>(attributes);
attributesClean.remove("code");
Log.fine("Patching " + ctClass.getName() + " with " + this.name + '(' + CollectionsUtil.joinMap(attributesClean) + ')' + (methods.isEmpty() ? "" : " {" + methods + '}'));
if (requiredAttributes != null && !attributes.keySet().containsAll(requiredAttributes)) {
Log.severe("Missing required attributes " + requiredAttributes.toString() + " when patching " + ctClass.getName());
return null;
}
if ("^all^".equals(methods)) {
patchDescriptor.set("silent", "true");
List<CtBehavior> ctBehaviors = new ArrayList<CtBehavior>();
Collections.addAll(ctBehaviors, ctClass.getDeclaredMethods());
Collections.addAll(ctBehaviors, ctClass.getDeclaredConstructors());
CtBehavior initializer = ctClass.getClassInitializer();
if (initializer != null) {
ctBehaviors.add(initializer);
}
for (CtBehavior ctBehavior : ctBehaviors) {
run(ctBehavior, attributes, patchClassInstance);
}
} else if (isClassPatch || (!emptyConstructor && methods.isEmpty())) {
return run(ctClass, attributes, patchClassInstance);
} else if (methods.isEmpty()) {
for (CtConstructor ctConstructor : ctClass.getDeclaredConstructors()) {
run(ctConstructor, attributes, patchClassInstance);
}
} else if ("^static^".equals(methods)) {
CtConstructor ctBehavior = ctClass.getClassInitializer();
if (ctBehavior == null) {
Log.severe("No static initializer found patching " + ctClass.getName() + " with " + toString());
} else {
run(ctBehavior, attributes, patchClassInstance);
}
} else {
List<MethodDescription> methodDescriptions = MethodDescription.fromListString(ctClass.getName(), methods);
for (MethodDescription methodDescription : methodDescriptions) {
CtMethod ctMethod;
try {
ctMethod = methodDescription.inClass(ctClass);
} catch (Throwable t) {
if (!attributes.containsKey("allowMissing")) {
Log.warning("", t);
}
continue;
}
run(ctMethod, attributes, patchClassInstance);
}
}
return null;
}
private Object run(CtClass ctClass, Map<String, String> attributes, Object patchClassInstance) {
try {
if (requiredAttributes == null) {
return patchMethod.invoke(patchClassInstance, ctClass);
} else {
return patchMethod.invoke(patchClassInstance, ctClass, attributes);
}
} catch (Throwable t) {
if (t instanceof InvocationTargetException) {
t = t.getCause();
}
if (t instanceof CannotCompileException && attributes.containsKey("code")) {
Log.severe("Code: " + attributes.get("code"));
}
Log.severe("Error patching " + ctClass.getName() + " with " + toString(), t);
return null;
}
}
private Object run(CtBehavior ctBehavior, Map<String, String> attributes, Object patchClassInstance) {
try {
if (requiredAttributes == null) {
return patchMethod.invoke(patchClassInstance, ctBehavior);
} else {
return patchMethod.invoke(patchClassInstance, ctBehavior, attributes);
}
} catch (Throwable t) {
if (t instanceof InvocationTargetException) {
t = t.getCause();
}
if (t instanceof CannotCompileException && attributes.containsKey("code")) {
Log.severe("Code: " + attributes.get("code"));
}
Log.severe("Error patching " + ctBehavior.getName() + " in " + ctBehavior.getDeclaringClass().getName() + " with " + toString(), t);
return null;
}
}
@Override
public String toString() {
return name;
}
}
}