Injector: multiplier support added

This commit is contained in:
Clisprail
2014-04-12 00:09:31 +02:00
parent fa0f3cf958
commit 15d10fec4f
4 changed files with 27 additions and 7 deletions
@@ -25,6 +25,7 @@ public class AddGetterAdapter implements Opcodes, Injectable {
private String returnDesc; private String returnDesc;
private boolean staticField; private boolean staticField;
private boolean staticMethod; private boolean staticMethod;
private long multiplier;
/** /**
* *
@@ -40,11 +41,12 @@ public class AddGetterAdapter implements Opcodes, Injectable {
* - return type of method, can be null for default return * - return type of method, can be null for default return
* @param staticMethod * @param staticMethod
* - pass true if you want the method to be static * - pass true if you want the method to be static
* @param multiplier - if this field requires a multiplier
*/ */
public AddGetterAdapter(final ClassNode into, public AddGetterAdapter(final ClassNode into,
final ClassNode fieldLocation, final FieldNode fieldNode, final ClassNode fieldLocation, final FieldNode fieldNode,
final String methodName, final String returnDesc, final String methodName, final String returnDesc,
final boolean staticMethod) { final boolean staticMethod, final long multiplier) {
this.into = into; this.into = into;
this.fieldLocation = fieldLocation; this.fieldLocation = fieldLocation;
this.fieldNode = fieldNode; this.fieldNode = fieldNode;
@@ -52,6 +54,7 @@ public class AddGetterAdapter implements Opcodes, Injectable {
this.returnDesc = returnDesc == null ? fieldNode.desc : returnDesc; this.returnDesc = returnDesc == null ? fieldNode.desc : returnDesc;
this.staticField = Modifier.isStatic(fieldNode.access); this.staticField = Modifier.isStatic(fieldNode.access);
this.staticMethod = staticMethod; this.staticMethod = staticMethod;
this.multiplier = multiplier;
} }
/** /**
@@ -138,8 +141,21 @@ public class AddGetterAdapter implements Opcodes, Injectable {
} }
} }
if (fieldNode.desc.equals("J") && returnDesc.equals("I")) if(multiplier != 0) {
if(fieldNode.desc.equals("I")) {
method.visitInsn(I2L);
}
method.visitLdcInsn(new Long(multiplier));
method.visitInsn(LMUL);
if(returnDesc.equals("I")) {
method.visitInsn(L2I);
}
} else if (fieldNode.desc.equals("J") && returnDesc.equals("I")) {
method.visitInsn(L2I); method.visitInsn(L2I);
} else if(fieldNode.desc.equals("I") && returnDesc.equals("J")) {
method.visitInsn(I2L);
}
method.visitInsn(ASMUtils.getReturnOpcode(returnDesc)); method.visitInsn(ASMUtils.getReturnOpcode(returnDesc));
method.visitMaxs(1, 1); method.visitMaxs(1, 1);
@@ -21,6 +21,7 @@ public class Getter implements Injectable {
private String methodName; private String methodName;
private String returnDesc; private String returnDesc;
private boolean staticMethod; private boolean staticMethod;
private long multiplier;
/** /**
* *
@@ -30,9 +31,10 @@ public class Getter implements Injectable {
* @param methodName - method name of getter * @param methodName - method name of getter
* @param returnDesc - return type of method, can be null for default return * @param returnDesc - return type of method, can be null for default return
* @param staticMethod - pass true if you want the method to be static * @param staticMethod - pass true if you want the method to be static
* @param multiplier - if there is one, otherwise 0L
*/ */
public Getter(final String into, final String fieldLocation, final String fieldNode, public Getter(final String into, final String fieldLocation, final String fieldNode,
final String methodName, final String returnDesc, final boolean staticMethod) { final String methodName, final String returnDesc, final boolean staticMethod, final long multiplier) {
Core.verbose(methodName + "[" + fieldLocation + "." + fieldNode + "]"); Core.verbose(methodName + "[" + fieldLocation + "." + fieldNode + "]");
this.into = ASMUtils.getClass(into); this.into = ASMUtils.getClass(into);
this.fieldLocation = ASMUtils.getClass(fieldLocation); this.fieldLocation = ASMUtils.getClass(fieldLocation);
@@ -40,6 +42,7 @@ public class Getter implements Injectable {
this.methodName = methodName; this.methodName = methodName;
this.returnDesc = returnDesc == null ? this.fieldNode.desc : returnDesc; this.returnDesc = returnDesc == null ? this.fieldNode.desc : returnDesc;
this.staticMethod = staticMethod; this.staticMethod = staticMethod;
this.multiplier = multiplier;
} }
/** /**
@@ -71,7 +74,7 @@ public class Getter implements Injectable {
* @return AddGetterAdapter * @return AddGetterAdapter
*/ */
public AddGetterAdapter getAdapter() { public AddGetterAdapter getAdapter() {
return new AddGetterAdapter(into, fieldLocation, fieldNode, methodName, returnDesc, staticMethod); return new AddGetterAdapter(into, fieldLocation, fieldNode, methodName, returnDesc, staticMethod, multiplier);
} }
} }
@@ -119,7 +119,7 @@ public class JSONHookParser extends HookParser {
String clazz = o.containsKey("class") ? this.get(o, "class") : interfaces.get(this.get(o, "accessor")); String clazz = o.containsKey("class") ? this.get(o, "class") : interfaces.get(this.get(o, "accessor"));
String into = o.containsKey("into") ? this.get(o, "into") : clazz; String into = o.containsKey("into") ? this.get(o, "into") : clazz;
g[i] = new Getter(into, clazz, this.get(o, "field"), this.get(o, "method"), desc, o.containsKey("static") ? (boolean) o.get("static") : false); g[i] = new Getter(into, clazz, this.get(o, "field"), this.get(o, "method"), desc, o.containsKey("static") ? (boolean) o.get("static") : false, 0);
} }
return g; return g;
} }
@@ -159,6 +159,7 @@ public class XMLHookParser extends HookParser {
"accessor", addGetter)); "accessor", addGetter));
final String into = isSet("into", addGetter) ? getValue("into", final String into = isSet("into", addGetter) ? getValue("into",
addGetter) : className; addGetter) : className;
final long multiplier = isSet("multiplier", addGetter) ? Long.parseLong(getValue("multiplier", addGetter)) : 0L;
final String fieldName = getValue("field", addGetter); final String fieldName = getValue("field", addGetter);
final String methodName = getValue("methodname", addGetter); final String methodName = getValue("methodname", addGetter);
boolean staticMethod = isSet("methstatic", addGetter) ? (getValue( boolean staticMethod = isSet("methstatic", addGetter) ? (getValue(
@@ -184,7 +185,7 @@ public class XMLHookParser extends HookParser {
returnDesc = str.toString(); returnDesc = str.toString();
} }
final Getter get = new Getter(into, className, fieldName, final Getter get = new Getter(into, className, fieldName,
methodName, returnDesc, staticMethod); methodName, returnDesc, staticMethod, multiplier);
getterList.add(get); getterList.add(get);
} }
return getterList.toArray(new Getter[getterList.size()]); return getterList.toArray(new Getter[getterList.size()]);