mirror of
https://github.com/LegacyModdingMC/UniMix.git
synced 2026-11-22 13:09:57 +00:00
Support redirecting ARRAYLENGTH, closes #173
This commit is contained in:
1 parent
3763912db0
commit
3fb0e53fed
3 files changed
+68
-16
No files matched your search
@@ -145,7 +145,7 @@ import org.spongepowered.asm.util.ConstraintParser.Constraint;
|
||||
* <tr>
|
||||
* <td>Write element</td>
|
||||
* <td><code>private void setElement(<b>ElementType</b>[] array, int index,
|
||||
* ElementType value)</code></td>
|
||||
* <b>ElementType</b> value)</code></td>
|
||||
* </tr>
|
||||
* </table>
|
||||
*
|
||||
@@ -154,6 +154,25 @@ import org.spongepowered.asm.util.ConstraintParser.Constraint;
|
||||
* {@link BeforeFieldAccess BeforeFieldAccess args} for details
|
||||
* on matching array accesses using the <tt>FIELD</tt> injection point.</p>
|
||||
*
|
||||
* <h4>Array Length Redirect Mode</h4>
|
||||
*
|
||||
* <p>For fields of an array type, it is possible to redirect the call to the
|
||||
* builtin pseudo-property <tt>length</tt>. To do so, specify the argument
|
||||
* <tt>array=length</tt> and configure your handler signature to return <tt>int
|
||||
* </tt> and consume the array as an argument:</p>
|
||||
*
|
||||
* <blockquote><code>private int getLength(<b>ElementType</b>[] array)
|
||||
* </code></blockquote>
|
||||
*
|
||||
* <p>For multi-dimensional arrays, provide one <tt>int</tt> argument for each
|
||||
* additional dimension of the array:</p>
|
||||
*
|
||||
* <blockquote><code>// Multidimensional array<br />
|
||||
* private <b>ElementType</b>[][] array;<br /><br />
|
||||
* // Handler signature:<br />
|
||||
* private <b>int</b> getLength(<b>ElementType</b>[][] array, int baseDim)
|
||||
* </code></blockquote>
|
||||
*
|
||||
* <h4>Constructor Redirect Mode</h4>
|
||||
*
|
||||
* <p>The handler method signature must match the constructor being redirected
|
||||
|
||||
@@ -96,6 +96,7 @@ public class RedirectInjector extends InvokeInjector {
|
||||
private static final String KEY_NOMINATORS = "nominators";
|
||||
private static final String KEY_WILD = "wildcard";
|
||||
private static final String KEY_FUZZ = "fuzz";
|
||||
private static final String KEY_OPCODE = "opcode";
|
||||
|
||||
/**
|
||||
* Meta decoration object for redirector target nodes
|
||||
@@ -169,6 +170,7 @@ public class RedirectInjector extends InvokeInjector {
|
||||
InjectionNode node = target.injectionNodes.get(insn);
|
||||
ConstructorRedirectData ctorData = null;
|
||||
int fuzz = BeforeFieldAccess.ARRAY_SEARCH_FUZZ_DEFAULT;
|
||||
int opcode = 0;
|
||||
|
||||
if (node != null ) {
|
||||
Meta other = node.getDecoration(Meta.KEY);
|
||||
@@ -189,7 +191,9 @@ public class RedirectInjector extends InvokeInjector {
|
||||
if (ip instanceof BeforeNew && !((BeforeNew)ip).hasDescriptor()) {
|
||||
ctorData = this.getCtorRedirect((BeforeNew)ip);
|
||||
} else if (ip instanceof BeforeFieldAccess) {
|
||||
fuzz = ((BeforeFieldAccess)ip).getFuzzFactor();
|
||||
BeforeFieldAccess bfa = (BeforeFieldAccess)ip;
|
||||
fuzz = bfa.getFuzzFactor();
|
||||
opcode = bfa.getArrayOpcode();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -201,6 +205,7 @@ public class RedirectInjector extends InvokeInjector {
|
||||
targetNode.decorate(ConstructorRedirectData.KEY, ctorData);
|
||||
} else {
|
||||
targetNode.decorate(RedirectInjector.KEY_FUZZ, Integer.valueOf(fuzz));
|
||||
targetNode.decorate(RedirectInjector.KEY_OPCODE, Integer.valueOf(opcode));
|
||||
}
|
||||
myNodes.add(targetNode);
|
||||
}
|
||||
@@ -328,7 +333,8 @@ public class RedirectInjector extends InvokeInjector {
|
||||
throw new InvalidInjectionException(this.info, "Dimensionality of handler method is greater than target array on " + this);
|
||||
} else if (handlerDimensions == 0 && targetDimensions > 0) {
|
||||
int fuzz = node.<Integer>getDecoration(RedirectInjector.KEY_FUZZ).intValue();
|
||||
this.injectAtArrayField(target, fieldNode, opCode, ownerType, fieldType, fuzz);
|
||||
int opcode = node.<Integer>getDecoration(RedirectInjector.KEY_OPCODE).intValue();
|
||||
this.injectAtArrayField(target, fieldNode, opCode, ownerType, fieldType, fuzz, opcode);
|
||||
} else {
|
||||
this.injectAtScalarField(target, fieldNode, opCode, ownerType, fieldType);
|
||||
}
|
||||
@@ -337,12 +343,15 @@ public class RedirectInjector extends InvokeInjector {
|
||||
/**
|
||||
* Redirect an array element access
|
||||
*/
|
||||
private void injectAtArrayField(Target target, FieldInsnNode fieldNode, int opCode, Type ownerType, Type fieldType, int fuzz) {
|
||||
private void injectAtArrayField(Target target, FieldInsnNode fieldNode, int opCode, Type ownerType, Type fieldType, int fuzz, int opcode) {
|
||||
Type elementType = fieldType.getElementType();
|
||||
if (opCode != Opcodes.GETSTATIC && opCode != Opcodes.GETFIELD) {
|
||||
throw new InvalidInjectionException(this.info, "Unspported opcode " + Bytecode.getOpcodeName(opCode) + " for array access " + this.info);
|
||||
} else if (this.returnType.getSort() != Type.VOID) {
|
||||
AbstractInsnNode varNode = BeforeFieldAccess.findArrayNode(target.insns, fieldNode, elementType.getOpcode(Opcodes.IALOAD), fuzz);
|
||||
if (opcode != Opcodes.ARRAYLENGTH) {
|
||||
opcode = elementType.getOpcode(Opcodes.IALOAD);
|
||||
}
|
||||
AbstractInsnNode varNode = BeforeFieldAccess.findArrayNode(target.insns, fieldNode, opcode, fuzz);
|
||||
this.injectAtGetArray(target, fieldNode, varNode, ownerType, fieldType);
|
||||
} else {
|
||||
AbstractInsnNode varNode = BeforeFieldAccess.findArrayNode(target.insns, fieldNode, elementType.getOpcode(Opcodes.IASTORE), fuzz);
|
||||
@@ -351,10 +360,10 @@ public class RedirectInjector extends InvokeInjector {
|
||||
}
|
||||
|
||||
/**
|
||||
* Array element read (xALOAD)
|
||||
* Array element read (xALOAD) or array.length (ARRAYLENGTH)
|
||||
*/
|
||||
private void injectAtGetArray(Target target, FieldInsnNode fieldNode, AbstractInsnNode varNode, Type ownerType, Type fieldType) {
|
||||
String handlerDesc = Bytecode.generateDescriptor(this.returnType, (Object[])RedirectInjector.getArrayArgs(fieldType));
|
||||
String handlerDesc = RedirectInjector.getGetArrayHandlerDescriptor(varNode, this.returnType, fieldType);
|
||||
boolean withArgs = this.checkDescriptor(handlerDesc, target, "array getter");
|
||||
this.injectArrayRedirect(target, fieldNode, varNode, withArgs, "array getter");
|
||||
}
|
||||
@@ -363,7 +372,7 @@ public class RedirectInjector extends InvokeInjector {
|
||||
* Array element write (xASTORE)
|
||||
*/
|
||||
private void injectAtSetArray(Target target, FieldInsnNode fieldNode, AbstractInsnNode varNode, Type ownerType, Type fieldType) {
|
||||
String handlerDesc = Bytecode.generateDescriptor(null, (Object[])RedirectInjector.getArrayArgs(fieldType, fieldType.getElementType()));
|
||||
String handlerDesc = Bytecode.generateDescriptor(null, (Object[])RedirectInjector.getArrayArgs(fieldType, 1, fieldType.getElementType()));
|
||||
boolean withArgs = this.checkDescriptor(handlerDesc, target, "array setter");
|
||||
this.injectArrayRedirect(target, fieldNode, varNode, withArgs, "array setter");
|
||||
}
|
||||
@@ -572,9 +581,16 @@ public class RedirectInjector extends InvokeInjector {
|
||||
target.replaceNode(initNode, insns);
|
||||
meta.injected++;
|
||||
}
|
||||
|
||||
private static Type[] getArrayArgs(Type fieldType, Type... extra) {
|
||||
int dimensions = fieldType.getDimensions() + 1;
|
||||
|
||||
private static String getGetArrayHandlerDescriptor(AbstractInsnNode varNode, Type returnType, Type fieldType) {
|
||||
if (varNode != null && varNode.getOpcode() == Opcodes.ARRAYLENGTH) {
|
||||
return Bytecode.generateDescriptor(Type.INT_TYPE, (Object[])RedirectInjector.getArrayArgs(fieldType, 0));
|
||||
}
|
||||
return Bytecode.generateDescriptor(returnType, (Object[])RedirectInjector.getArrayArgs(fieldType, 1));
|
||||
}
|
||||
|
||||
private static Type[] getArrayArgs(Type fieldType, int extraDimensions, Type... extra) {
|
||||
int dimensions = fieldType.getDimensions() + extraDimensions;
|
||||
Type[] args = new Type[dimensions + extra.length];
|
||||
for (int i = 0; i < args.length; i++) {
|
||||
args[i] = i == 0 ? fieldType : i < dimensions ? Type.INT_TYPE : extra[dimensions - i];
|
||||
|
||||
@@ -67,9 +67,9 @@ import org.spongepowered.asm.util.Bytecode;
|
||||
*
|
||||
* <p>Matching array access:</p>
|
||||
* <p>For array fields, it is possible to match field accesses followed by a
|
||||
* corresponding array element <em>get</em> or <em>set</em> operation. To enable
|
||||
* this behaviour specify the <tt>array</tt> named-argument with the desired
|
||||
* operation:</p>
|
||||
* corresponding array element <em>get</em>, <em>set</em> or <em>length</em>
|
||||
* operation. To enable this behaviour specify the <tt>array</tt> named-argument
|
||||
* with the desired operation:</p>
|
||||
*
|
||||
* <blockquote><pre>
|
||||
* @At(value = "FIELD", target="myIntArray:[I", args = "array=get")
|
||||
@@ -86,6 +86,10 @@ import org.spongepowered.asm.util.Bytecode;
|
||||
@AtCode("FIELD")
|
||||
public class BeforeFieldAccess extends BeforeInvoke {
|
||||
|
||||
private static final String ARRAY_GET = "get";
|
||||
private static final String ARRAY_SET = "set";
|
||||
private static final String ARRAY_LENGTH = "length";
|
||||
|
||||
/**
|
||||
* Default fuzz factor for searching for array access opcodes
|
||||
*/
|
||||
@@ -113,13 +117,26 @@ public class BeforeFieldAccess extends BeforeInvoke {
|
||||
this.opcode = data.getOpcode(-1, Opcodes.GETFIELD, Opcodes.PUTFIELD, Opcodes.GETSTATIC, Opcodes.PUTSTATIC, -1);
|
||||
|
||||
String array = data.get("array", "");
|
||||
this.arrOpcode = "get".equalsIgnoreCase(array) ? Opcodes.IALOAD : "set".equalsIgnoreCase(array) ? Opcodes.IASTORE : 0;
|
||||
this.arrOpcode = BeforeFieldAccess.ARRAY_GET.equalsIgnoreCase(array) ? Opcodes.IALOAD
|
||||
: BeforeFieldAccess.ARRAY_SET.equalsIgnoreCase(array) ? Opcodes.IASTORE
|
||||
: BeforeFieldAccess.ARRAY_LENGTH.equalsIgnoreCase(array) ? Opcodes.ARRAYLENGTH : 0;
|
||||
this.fuzzFactor = Math.min(Math.max(data.get("fuzz", BeforeFieldAccess.ARRAY_SEARCH_FUZZ_DEFAULT), 1), 32);
|
||||
}
|
||||
|
||||
public int getFuzzFactor() {
|
||||
return this.fuzzFactor;
|
||||
}
|
||||
|
||||
public int getArrayOpcode() {
|
||||
return this.arrOpcode;
|
||||
}
|
||||
|
||||
private int getArrayOpcode(String desc) {
|
||||
if (this.arrOpcode != Opcodes.ARRAYLENGTH) {
|
||||
return Type.getType(desc).getElementType().getOpcode(this.arrOpcode);
|
||||
}
|
||||
return this.arrOpcode;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected boolean matchesInsn(AbstractInsnNode insn) {
|
||||
@@ -142,7 +159,7 @@ public class BeforeFieldAccess extends BeforeInvoke {
|
||||
protected boolean addInsn(InsnList insns, Collection<AbstractInsnNode> nodes, AbstractInsnNode insn) {
|
||||
if (this.arrOpcode > 0) {
|
||||
FieldInsnNode fieldInsn = (FieldInsnNode)insn;
|
||||
int accOpcode = Type.getType(fieldInsn.desc).getElementType().getOpcode(this.arrOpcode);
|
||||
int accOpcode = this.getArrayOpcode(fieldInsn.desc);
|
||||
this.log("{} > > > > searching for array access opcode {} fuzz={}", this.className, Bytecode.getOpcodeName(accOpcode), this.fuzzFactor);
|
||||
|
||||
if (BeforeFieldAccess.findArrayNode(insns, fieldInsn, accOpcode, this.fuzzFactor) == null) {
|
||||
|
||||
Reference in new issue
Block a user