PacketTransformer.java
9.77 KB
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
package com.mumfrey.liteloader.transformers;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import org.objectweb.asm.Opcodes;
import org.objectweb.asm.tree.ClassNode;
import org.objectweb.asm.tree.InsnList;
import org.objectweb.asm.tree.InsnNode;
import org.objectweb.asm.tree.LabelNode;
import org.objectweb.asm.tree.MethodInsnNode;
import org.objectweb.asm.tree.MethodNode;
import org.objectweb.asm.tree.TryCatchBlockNode;
import org.objectweb.asm.tree.VarInsnNode;
import com.mumfrey.liteloader.core.runtime.Obf;
import com.mumfrey.liteloader.util.SortableValue;
import com.mumfrey.liteloader.util.log.LiteLoaderLogger;
/**
* Class transformer which transforms a Packet class and alters the "processPacket" function to call the specified
* callback method in the specified callback class instead of the usual behaviour of calling the relevant synthetic bridge
* method in the packet class itself.
*
* @author Adam Mummery-Smith
*/
public abstract class PacketTransformer extends ClassTransformer
{
private static final Set<String> transformedPackets = new HashSet<String>();
private static int transformerOrder = 0;
private final String packetClass;
private final String packetClassObf;
private final String handlerClassName;
private final String handlerMethodName;
private final boolean prepend;
private final int priority;
private final int order;
/**
* Create a PacketTransformer with default priority
*
* @param packetClass Packet class name we want to override (FQ)
* @param handlerClassName Name of the class which will handle the callback
* @param handlerMethodName Method name to map to in handlerClass (must have signature (INetHandler, PacketClass)Void)
*/
protected PacketTransformer(Obf packetClass, String handlerClassName, String handlerMethodName)
{
this(packetClass.name, packetClass.obf, handlerClassName, handlerMethodName, 0);
}
/**
* Create a PacketTransformer with default priority
*
* @param packetClass Packet class name we want to override (FQ)
* @param packetClassObf Obfuscated packet class name
* @param handlerClassName Name of the class which will handle the callback
* @param handlerMethodName Method name to map to in handlerClass (must have signature (INetHandler, PacketClass)Void)
*/
protected PacketTransformer(String packetClass, String packetClassObf, String handlerClassName, String handlerMethodName)
{
this(packetClass, packetClassObf, handlerClassName, handlerMethodName, 0);
}
/**
* Create a PacketTransformer with default priority
*
* @param packetClass Packet class name we want to override (FQ)
* @param handlerClassName Name of the class which will handle the callback
* @param handlerMethodName Method name to map to in handlerClass (must have signature (INetHandler, PacketClass)Void)
* @param priority transformer priority, if there are multiple transformers registered for this packet then higher priority handlers are called before lower priority ones, default priority is 0
*/
protected PacketTransformer(Obf packetClass, String handlerClassName, String handlerMethodName, int priority)
{
this(packetClass.name, packetClass.obf, handlerClassName, handlerMethodName, priority);
}
/**
* Create a PacketTransformer with default priority
*
* @param packetClass Packet class name we want to override (FQ)
* @param packetClassObf Obfuscated packet class name
* @param handlerClassName Name of the class which will handle the callback
* @param handlerMethodName Method name to map to in handlerClass (must have signature (INetHandler, PacketClass)Void)
* @param priority transformer priority, if there are multiple transformers registered for this packet then higher priority handlers are called before lower priority ones, default priority is 0
*/
protected PacketTransformer(String packetClass, String packetClassObf, String handlerClassName, String handlerMethodName, int priority)
{
this.packetClass = packetClass;
this.packetClassObf = packetClassObf;
this.handlerClassName = handlerClassName.replace('.', '/');
this.handlerMethodName = handlerMethodName;
this.prepend = PacketTransformer.transformedPackets.contains(packetClass);
PacketTransformer.transformedPackets.add(packetClass);
this.priority = priority;
this.order = PacketTransformer.transformerOrder++;
}
/* (non-Javadoc)
* @see net.minecraft.launchwrapper.IClassTransformer#transform(java.lang.String, java.lang.String, byte[])
*/
@Override
public final byte[] transform(String name, String transformedName, byte[] basicClass)
{
if (this.packetClass.equals(transformedName) || this.packetClassObf.equals(transformedName))
{
LiteLoaderLogger.info("PacketTransformer: Running transformer %s for %s", this.getClass().getName(), name);
try
{
byte[] transformedClass = this.transformClass(transformedName, basicClass);
this.notifyInjected();
return transformedClass;
}
catch (Exception ex) { ex.printStackTrace(); }
}
return basicClass;
}
/**
* Found the packet class we want to transform, attempt to transform it
*
* @param className
* @param basicClass
* @return
*/
private byte[] transformClass(String className, byte[] basicClass)
{
ClassNode classNode = this.readClass(basicClass, true);
// Try and transform obfuscated first
if (!this.tryTransformMethod(className, classNode, Obf.processPacket.obf, Obf.INetHandler.obf))
{
// Try to transform srg for use with fml
if (!this.tryTransformMethod(className, classNode, Obf.processPacket.srg, Obf.INetHandler.ref))
{
// Try to transform non-obf for use in dev env
if (!this.tryTransformMethod(className, classNode, Obf.processPacket.name, Obf.INetHandler.ref))
{
LiteLoaderLogger.warning("PacketTransformer: failed transforming class '%s' (%s)", this.packetClass, this.packetClassObf);
}
}
}
return this.writeClass(classNode);
}
/**
* @param className
* @param classNode
* @param functionName
* @param netHandlerClassName
*/
private boolean tryTransformMethod(String className, ClassNode classNode, String functionName, String netHandlerClassName)
{
MethodNode method = this.findMethodByNameAndSignature(classNode.methods, functionName, "(L" + netHandlerClassName + ";)V");
if (method != null)
{
String targetMethodSig = "(L" + netHandlerClassName + ";L" + className.replace('.', '/') + ";)V";
this.transformMethod(className, method, targetMethodSig);
return true;
}
return false;
}
/**
* Clear the old method contents and replace with the call to our handler function
*
* @param method
* @param targetMethodSig
*/
private void transformMethod(String className, MethodNode method, String targetMethodSig)
{
if (this.prepend)
{
InsnList insns = new InsnList();
// Push method argument 1 (INetHandler instance) onto the stack
insns.add(new VarInsnNode(Opcodes.ALOAD, 1));
// Push "this" onto the stack
insns.add(new VarInsnNode(Opcodes.ALOAD, 0));
// Invoke the handler function with the args we just pushed onto the stack
insns.add(new MethodInsnNode(Opcodes.INVOKESTATIC, this.handlerClassName, this.handlerMethodName, targetMethodSig, false));
method.instructions.insert(method.instructions.getFirst(), insns);
}
else
{
// Labels for try/catch
LabelNode tryLabel = new LabelNode();
LabelNode catchLabel = new LabelNode();
// Add try/catch
method.tryCatchBlocks.clear();
method.tryCatchBlocks.add(new TryCatchBlockNode(tryLabel, catchLabel, catchLabel, PacketHandlerException.class.getName().replace('.', '/')));
// Dump the old method content, we don't want it any more
method.instructions.clear();
// Try
method.instructions.add(tryLabel);
// Push method argument 1 (INetHandler instance) onto the stack
method.instructions.add(new VarInsnNode(Opcodes.ALOAD, 1));
// Push "this" onto the stack
method.instructions.add(new VarInsnNode(Opcodes.ALOAD, 0));
// Invoke the handler function with the args we just pushed onto the stack
method.instructions.add(new MethodInsnNode(Opcodes.INVOKESTATIC, this.handlerClassName, this.handlerMethodName, targetMethodSig, false));
// Return if no exception
method.instructions.add(new InsnNode(Opcodes.RETURN));
// Catch
method.instructions.add(catchLabel);
// Return if exception caught
method.instructions.add(new InsnNode(Opcodes.RETURN));
}
}
/**
* For ordering transformers, returns the class name this transformer wants to transform
*/
public final String getPacketClass()
{
return this.packetClass;
}
/**
* Get the priority of this transformer, higher priority transformers run earlier
*/
public final int getPriority()
{
return this.priority;
}
/**
* For subclasses, to set a local flag indicating the code was successfully injected
*/
protected abstract void notifyInjected();
/**
* For subclasses, indicates the transformer ran but was not successful
*/
protected abstract void notifyInjectionFailed();
/**
* @param classNode
* @param funcName
* @param funcNameObf
* @param funcSig
* @param funcSigObf
* @return
*/
private MethodNode findMethodByNameAndSignature(List<MethodNode> methods, String funcName, String funcSig)
{
for (MethodNode method : methods)
{
if (funcName.equals(method.name) && funcSig.equals(method.desc))
return method;
}
return null;
}
/**
* @param className
* @return
*/
public final SortableValue<String> getInfo(String className)
{
return new SortableValue<String>(this.priority, this.order, className);
}
}