Commit c939f39ca1cce8f4efc7f85a1ba547a5262d3a7c

Authored by Mumfrey
1 parent b29e8bbb

adding BeforeStringInvoke injection point class

java/common/com/mumfrey/liteloader/transformers/event/inject/BeforeInvoke.java
... ... @@ -24,30 +24,30 @@ public class BeforeInvoke extends InjectionPoint
24 24 /**
25 25 * Method name(s) to search for, usually this will contain the different names of the method for different obfuscations (mcp, srg, notch)
26 26 */
27   - private final String[] methodNames;
  27 + protected final String[] methodNames;
28 28  
29 29 /**
30 30 * Method owner(s) to search for, the values in this array MUST much the equivalent indices in methodNames, if the array is NULL then
31 31 * all owners are valid
32 32 */
33   - private final String[] methodOwners;
  33 + protected final String[] methodOwners;
34 34  
35 35 /**
36 36 * Method signature(s) to search for, the values in this array MUST much the equivalent indices in methodNames, if the array is NULL
37 37 * then all signatures are valid
38 38 */
39   - private final String[] methodSignatures;
  39 + protected final String[] methodSignatures;
40 40  
41 41 /**
42 42 * This strategy can be used to identify a particular invokation if the same method is invoked at multiple points, if this value is -1
43 43 * then the strategy returns ALL invokations of the method.
44 44 */
45   - private final int ordinal;
  45 + protected final int ordinal;
46 46  
47 47 /**
48 48 * True to turn on strategy debugging to the console
49 49 */
50   - private boolean logging = false;
  50 + protected boolean logging = false;
51 51  
52 52 /**
53 53 * Match all occurrences of the specified method or methods
... ... @@ -220,27 +220,36 @@ public class BeforeInvoke extends InjectionPoint
220 220 if (index > -1 && ownerIndex == index && descIndex == index)
221 221 {
222 222 if (this.logging) LiteLoaderLogger.info("BeforeInvoke found a matching invoke, checking ordinal...");
223   - if (this.ordinal == -1)
  223 + if (this.matchesInsn(node, ordinal))
224 224 {
225 225 if (this.logging) LiteLoaderLogger.info("BeforeInvoke found a matching invoke at ordinal %d", ordinal);
226 226 nodes.add(node);
227 227 found = true;
228   - }
229   - else if (this.ordinal == ordinal)
230   - {
231   - if (this.logging) LiteLoaderLogger.info("BeforeInvoke found a matching invoke at ordinal %d", ordinal);
232   - nodes.add(node);
233   - return true;
  228 +
  229 + if (this.ordinal == ordinal)
  230 + break;
234 231 }
235 232  
236 233 ordinal++;
237 234 }
238 235 }
  236 +
  237 + this.inspectInsn(desc, insns, insn);
239 238 }
240 239  
241 240 return found;
242 241 }
243 242  
  243 + protected void inspectInsn(String desc, InsnList insns, AbstractInsnNode insn)
  244 + {
  245 + // stub for subclasses
  246 + }
  247 +
  248 + protected boolean matchesInsn(MethodInsnNode node, int ordinal)
  249 + {
  250 + return this.ordinal == -1 || this.ordinal == ordinal;
  251 + }
  252 +
244 253 /**
245 254 * Special version of contains which returns TRUE if the haystack array is null, which is an odd behaviour we actually
246 255 * want here because null indicates that the value is not important
... ...
java/common/com/mumfrey/liteloader/transformers/event/inject/BeforeStringInvoke.java 0 → 100644
  1 +package com.mumfrey.liteloader.transformers.event.inject;
  2 +
  3 +import java.util.Collection;
  4 +
  5 +import org.objectweb.asm.tree.AbstractInsnNode;
  6 +import org.objectweb.asm.tree.InsnList;
  7 +import org.objectweb.asm.tree.LdcInsnNode;
  8 +import org.objectweb.asm.tree.MethodInsnNode;
  9 +
  10 +import com.mumfrey.liteloader.transformers.event.Event;
  11 +import com.mumfrey.liteloader.transformers.event.MethodInfo;
  12 +import com.mumfrey.liteloader.util.log.LiteLoaderLogger;
  13 +
  14 +/**
  15 + * An injection point which searches for a matching String LDC insn immediately prior to a qualifying invoke
  16 + *
  17 + * @author Adam Mummery-Smith
  18 + */
  19 +public class BeforeStringInvoke extends BeforeInvoke
  20 +{
  21 + private static final String STRING_VOID_SIG = "(Ljava/lang/String;)V";
  22 +
  23 + private final String ldcValue;
  24 +
  25 + private boolean foundLdc;
  26 +
  27 + public BeforeStringInvoke(String ldcValue, MethodInfo method)
  28 + {
  29 + this(ldcValue, method, -1);
  30 + }
  31 +
  32 + public BeforeStringInvoke(String ldcValue, MethodInfo method, int ordinal)
  33 + {
  34 + super(method, ordinal);
  35 + this.ldcValue = ldcValue;
  36 +
  37 + for (int i = 0; i < this.methodSignatures.length; i++)
  38 + if (!STRING_VOID_SIG.equals(this.methodSignatures[i]))
  39 + throw new IllegalArgumentException("BeforeStringInvoke requires method with with signature " + STRING_VOID_SIG);
  40 + }
  41 +
  42 + @Override
  43 + public boolean find(String desc, InsnList insns, Collection<AbstractInsnNode> nodes, Event event)
  44 + {
  45 + this.foundLdc = false;
  46 +
  47 + return super.find(desc, insns, nodes, event);
  48 + }
  49 +
  50 + @Override
  51 + protected void inspectInsn(String desc, InsnList insns, AbstractInsnNode insn)
  52 + {
  53 + if (insn instanceof LdcInsnNode)
  54 + {
  55 + LdcInsnNode node = (LdcInsnNode)insn;
  56 + if (node.cst instanceof String && this.ldcValue.equals(node.cst))
  57 + {
  58 + if (this.logging) LiteLoaderLogger.info("BeforeInvoke found a matching LDC with value %s", node.cst);
  59 + this.foundLdc = true;
  60 + return;
  61 + }
  62 + }
  63 +
  64 + this.foundLdc = false;
  65 + }
  66 +
  67 + @Override
  68 + protected boolean matchesInsn(MethodInsnNode node, int ordinal)
  69 + {
  70 + return this.foundLdc && super.matchesInsn(node, ordinal);
  71 + }
  72 +}
... ...