-
-
Notifications
You must be signed in to change notification settings - Fork 44
/
Patcher.java
192 lines (176 loc) · 6.6 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
package nallar.tickthreading.patcher;
import javassist.CannotCompileException;
import javassist.ClassPool;
import javassist.CtBehavior;
import javassist.CtClass;
import javassist.CtConstructor;
import javassist.CtMethod;
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 java.io.*;
import java.lang.annotation.*;
import java.lang.reflect.*;
import java.util.*;
public class Patcher {
ClassPool classPool;
Patches patchClassInstance;
Map<String, PatchMethodDescriptor> patchMethods = new HashMap<String, PatchMethodDescriptor>();
Map<String, PatchDescriptor> patches = new HashMap<String, PatchDescriptor>();
public void loadPatches(InputStream config, Class<? extends Patches> patchesClass, ClassPool classPool) {
try {
patchClassInstance = (Patches) patchesClass.getDeclaredConstructors()[0].newInstance(this, classPool);
} catch (Exception e) {
Log.severe("Failed to instantiate patch class", e);
}
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);
}
}
}
try {
Document document = DomUtil.readDocumentFromInputStream(config);
} catch (Throwable t) {
throw UnsafeUtil.throwIgnoreChecked(t);
}
}
private class PatchDescriptor {
private Map<String, String> attributes = new HashMap<String, String>();
private String methods;
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 void setMethods(String methods) {
this.methods = methods.trim();
}
}
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) {
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);
}
} else if (isClassPatch || (!emptyConstructor && methods.isEmpty())) {
return run(ctClass, attributes);
} else if (methods.isEmpty()) {
for (CtConstructor ctConstructor : ctClass.getDeclaredConstructors()) {
run(ctConstructor, attributes);
}
} 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);
}
} 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);
}
}
return null;
}
private Object run(CtClass ctClass, Map<String, String> attributes) {
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) {
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;
}
}
}