BeforeInvoke.java
13.9 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
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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
/*
* This file is part of LiteLoader.
* Copyright (C) 2012-16 Adam Mummery-Smith
* All Rights Reserved.
*/
package com.mumfrey.liteloader.transformers.event.inject;
import java.util.Collection;
import java.util.ListIterator;
import org.spongepowered.asm.lib.tree.AbstractInsnNode;
import org.spongepowered.asm.lib.tree.FieldInsnNode;
import org.spongepowered.asm.lib.tree.InsnList;
import org.spongepowered.asm.lib.tree.MethodInsnNode;
import org.spongepowered.asm.mixin.injection.InjectionPoint;
import com.mumfrey.liteloader.transformers.ClassTransformer;
import com.mumfrey.liteloader.transformers.event.MethodInfo;
import com.mumfrey.liteloader.util.log.LiteLoaderLogger;
/**
* An injection point which searches for method invocations matching its
* arguments and returns a list of insns immediately prior to matching
* invocations. Only the method name is required, owners and signatures are
* optional and can be used to disambiguate between methods of the same name but
* with different args, or belonging to different classes.
*
* @author Adam Mummery-Smith
*/
public class BeforeInvoke extends InjectionPoint
{
protected class InsnInfo
{
public final String owner;
public final String name;
public final String desc;
public InsnInfo(AbstractInsnNode insn)
{
if (insn instanceof MethodInsnNode)
{
MethodInsnNode methodNode = (MethodInsnNode)insn;
this.owner = methodNode.owner;
this.name = methodNode.name;
this.desc = methodNode.desc;
}
else if (insn instanceof FieldInsnNode)
{
FieldInsnNode fieldNode = (FieldInsnNode)insn;
this.owner = fieldNode.owner;
this.name = fieldNode.name;
this.desc = fieldNode.desc;
}
else
{
throw new IllegalArgumentException("insn must be an instance of MethodInsnNode or FieldInsnNode");
}
}
}
/**
* Method name(s) to search for, usually this will contain the different
* names of the method for different obfuscations (mcp, srg, notch)
*/
protected final String[] methodNames;
/**
* Method owner(s) to search for, the values in this array MUST much the
* equivalent indices in methodNames, if the array is NULL then all owners
* are valid.
*/
protected final String[] methodOwners;
/**
* Method signature(s) to search for, the values in this array MUST much the
* equivalent indices in methodNames, if the array is NULL then all
* signatures are valid.
*/
protected final String[] methodSignatures;
/**
* This strategy can be used to identify a particular invocation if the same
* method is invoked at multiple points, if this value is -1 then the
* strategy returns ALL invocations of the method.
*/
protected final int ordinal;
/**
* True to turn on strategy debugging to the console
*/
protected boolean logging = false;
protected final String className;
/**
* Match all occurrences of the specified method or methods
*
* @param methodNames Method name(s) to search for
*/
public BeforeInvoke(String... methodNames)
{
this(methodNames, null, -1);
}
/**
* Match the specified invocation of the specified method
*
* @param methodName Method name to search for
* @param ordinal ID of the invocation to hook, or -1 to hook all
* invocations
*/
public BeforeInvoke(String methodName, int ordinal)
{
this(new String[] { methodName }, null, null, ordinal);
}
/**
* Match the specified invocation of the specified method(s)
*
* @param methodNames Method names to search for
* @param ordinal ID of the invocation to hook, or -1 to hook all
* invocations
*/
public BeforeInvoke(String[] methodNames, int ordinal)
{
this(methodNames, null, null, ordinal);
}
/**
* Match all occurrences of the specified method or methods with the
* specified owners.
*
* @param methodNames Method names to search for
* @param methodOwners Owners to search for, indices in this array MUST
* match the indices in methodNames, eg. if methodNames contains
* { "mcpName", "func_12345_a", "a" } then methodOwners should contain
* { "net/minecraft/pkg/ClsName", "net/minecraft/pkg/ClsName", "abc" }
* in order that the appropriate owner name obfuscation matches the
* corresponding index in the methodNames array
*/
public BeforeInvoke(String[] methodNames, String[] methodOwners)
{
this(methodNames, methodOwners, null, -1);
}
/**
* Match the specified invocation of the specified method or methods with
* the specified owners.
*
* @param methodNames Method names to search for
* @param methodOwners Owners to search for, indices in this array MUST
* match the indices in methodNames, eg. if methodNames contains
* { "mcpName", "func_12345_a", "a" } then methodOwners should contain
* { "net/minecraft/pkg/ClsName", "net/minecraft/pkg/ClsName", "abc" }
* in order that the appropriate owner name obfuscation matches the
* corresponding index in the methodNames array
* @param ordinal ID of the invocation to hook or -1 to hook all invocations
*/
public BeforeInvoke(String[] methodNames, String[] methodOwners, int ordinal)
{
this(methodNames, methodOwners, null, ordinal);
}
/**
* Match all occurrences of the specified method or methods with the
* specified owners or signatures, pass null to the owners array if you only
* want to match signatures.
*
* @param methodNames Method names to search for
* @param methodOwners Owners to search for, indices in this array MUST
* match the indices in methodNames, eg. if methodNames contains
* { "mcpName", "func_12345_a", "a" } then methodOwners should contain
* { "net/minecraft/pkg/ClsName", "net/minecraft/pkg/ClsName", "abc" }
* in order that the appropriate owner name obfuscation matches the
* corresponding index in the methodNames array
* @param methodSignatures Signatures to search for, indices in this array
* MUST match the indices in methodNames, eg. if methodNames contains
* { "mcpName", "func_12345_a", "a" } then methodSignatures should
* contain
* { "(Lnet/minecraft/pkg/ClsName;)V",
* "(Lnet/minecraft/pkg/ClsName;)V", "(Labc;)V" }
* in order that the appropriate signature obfuscation matches the
* corresponding index in the methodNames array (and ownerNames array
* if present)
*/
public BeforeInvoke(String[] methodNames, String[] methodOwners, String[] methodSignatures)
{
this(methodNames, methodOwners, methodSignatures, -1);
}
/**
* Match the specified invocation of the specified method or methods with
* the specified owners or signatures, pass null to the owners array if you
* only want to match signatures.
*
* @param methodNames Method names to search for
* @param methodOwners Owners to search for, indices in this array MUST
* match the indices in methodNames, eg. if methodNames contains
* { "mcpName", "func_12345_a", "a" } then methodOwners should contain
* { "net/minecraft/pkg/ClsName", "net/minecraft/pkg/ClsName", "abc" }
* in order that the appropriate owner name obfuscation matches the
* corresponding index in the methodNames array
* @param methodSignatures Signatures to search for, indices in this array
* MUST match the indices in methodNames, eg. if methodNames contains
* { "mcpName", "func_12345_a", "a" } then methodSignatures should
* contain { "(Lnet/minecraft/pkg/ClassName;)V",
* "(Lnet/minecraft/pkg/ClassName;)V", "(Labc;)V" }
* in order that the appropriate signature obfuscation matches the
* corresponding index in the methodNames array (and ownerNames array
* if present)
* @param ordinal ID of the invocation to hook or -1 to hook all invocations
*/
public BeforeInvoke(String[] methodNames, String[] methodOwners, String[] methodSignatures, int ordinal)
{
if (methodNames == null || methodNames.length == 0)
{
throw new IllegalArgumentException("Method name selector must not be null");
}
if (methodSignatures != null && methodSignatures.length == 0) methodSignatures = null;
if (methodOwners != null && methodOwners.length == 0) methodOwners = null;
if (ordinal < 0) ordinal = -1;
this.methodNames = methodNames;
this.methodOwners = methodOwners;
this.methodSignatures = methodSignatures;
this.ordinal = ordinal;
this.className = this.getClass().getSimpleName();
this.convertClassRefs();
}
/**
* Match the invocation described by the supplied MethodInfo
*
* @param method
*/
public BeforeInvoke(MethodInfo method)
{
this(method, -1);
}
/**
* Match the invocation described by the supplied MethodInfo at the
* specified ordinal.
*
* @param method
* @param ordinal
*/
public BeforeInvoke(MethodInfo method, int ordinal)
{
this.methodNames = method.getNames();
this.methodOwners = method.getOwners();
this.methodSignatures = method.getDescriptors();
this.ordinal = ordinal;
this.className = this.getClass().getSimpleName();
this.convertClassRefs();
}
private void convertClassRefs()
{
for (int i = 0; i < this.methodOwners.length; i++)
{
if (this.methodOwners[i] != null) this.methodOwners[i] = this.methodOwners[i].replace('.', '/');
}
if (this.methodSignatures != null)
{
for (int i = 0; i < this.methodSignatures.length; i++)
{
if (this.methodSignatures[i] != null) this.methodSignatures[i] = this.methodSignatures[i].replace('.', '/');
}
}
}
public BeforeInvoke setLogging(boolean logging)
{
this.logging = logging;
return this;
}
@Override
public boolean find(String desc, InsnList insns, Collection<AbstractInsnNode> nodes)
{
int ordinal = 0;
boolean found = false;
if (this.logging)
{
LiteLoaderLogger.debug(ClassTransformer.HORIZONTAL_RULE);
LiteLoaderLogger.debug(this.className + " is searching for an injection point in method with descriptor %s", desc);
}
ListIterator<AbstractInsnNode> iter = insns.iterator();
while (iter.hasNext())
{
AbstractInsnNode insn = iter.next();
if (this.matchesInsn(insn))
{
InsnInfo nodeInfo = new InsnInfo(insn);
if (this.logging)
{
LiteLoaderLogger.debug(this.className + " is considering insn NAME=%s DESC=%s OWNER=%s",
nodeInfo.name, nodeInfo.desc, nodeInfo.owner);
}
int index = BeforeInvoke.arrayIndexOf(this.methodNames, nodeInfo.name, -1);
if (index > -1 && this.logging) LiteLoaderLogger.debug(this.className + " found a matching insn, checking owner/signature...");
int ownerIndex = BeforeInvoke.arrayIndexOf(this.methodOwners, nodeInfo.owner, index);
int descIndex = BeforeInvoke.arrayIndexOf(this.methodSignatures, nodeInfo.desc, index);
if (index > -1 && ownerIndex == index && descIndex == index)
{
if (this.logging) LiteLoaderLogger.debug(this.className + " found a matching insn, checking preconditions...");
if (this.matchesInsn(nodeInfo, ordinal))
{
if (this.logging) LiteLoaderLogger.debug(this.className + " found a matching insn at ordinal %d", ordinal);
nodes.add(insn);
found = true;
if (this.ordinal == ordinal)
{
break;
}
}
ordinal++;
}
}
this.inspectInsn(desc, insns, insn);
}
if (this.logging) LiteLoaderLogger.debug(ClassTransformer.HORIZONTAL_RULE);
return found;
}
protected boolean matchesInsn(AbstractInsnNode insn)
{
return insn instanceof MethodInsnNode;
}
protected void inspectInsn(String desc, InsnList insns, AbstractInsnNode insn)
{
// stub for subclasses
}
protected boolean matchesInsn(InsnInfo nodeInfo, int ordinal)
{
if (this.logging)
{
LiteLoaderLogger.debug(this.className + " comparing target ordinal %d with current ordinal %d", this.ordinal, ordinal);
}
return this.ordinal == -1 || this.ordinal == ordinal;
}
/**
* Special version of contains which returns TRUE if the haystack array is
* null, which is an odd behaviour we actually want here because null
* indicates that the value is not important.
*
* @param haystack
* @param needle
*/
private static int arrayIndexOf(String[] haystack, String needle, int pos)
{
if (haystack == null) return pos;
if (pos > -1 && pos < haystack.length && needle.equals(haystack[pos])) return pos;
for (int index = 0; index < haystack.length; index++)
{
if (needle.equals(haystack[index]))
{
return index;
}
}
return -1;
}
}