mirror of
https://github.com/LegacyModdingMC/UniMix.git
synced 2026-11-21 08:02:13 +00:00
Allow Redirect annotations to work on fields as well
This commit is contained in:
1 parent
19272e1170
commit
5a79cacc14
7 files changed
+295
-15
No files matched your search
@@ -0,0 +1,46 @@
|
||||
/*
|
||||
* This file is part of Mixin, licensed under the MIT License (MIT).
|
||||
*
|
||||
* Copyright (c) SpongePowered <https://www.spongepowered.org>
|
||||
* Copyright (c) contributors
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
package org.spongepowered.asm.mixin;
|
||||
|
||||
public class MixinException extends RuntimeException {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
public MixinException() {
|
||||
}
|
||||
|
||||
public MixinException(String message) {
|
||||
super(message);
|
||||
}
|
||||
|
||||
public MixinException(Throwable cause) {
|
||||
super(cause);
|
||||
}
|
||||
|
||||
public MixinException(String message, Throwable cause) {
|
||||
super(message, cause);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -33,7 +33,9 @@ import org.spongepowered.asm.mixin.MixinEnvironment.Option;
|
||||
|
||||
/**
|
||||
* <p>Specifies that this mixin method should redirect the specified
|
||||
* {@link #method} call to the method decorated with this annotation.</p>
|
||||
* method call or field access to the method decorated with this annotation.</p>
|
||||
*
|
||||
* <h4>Method Redirect Mode</h4>
|
||||
*
|
||||
* <p>The handler method signature must match the hooked method precisely
|
||||
* <b>but</b> prepended with an arg of the owning object's type to accept the
|
||||
@@ -70,6 +72,40 @@ import org.spongepowered.asm.mixin.MixinEnvironment.Option;
|
||||
* <pre>public boolean barProxy(Foo someObject, int abc, int def,
|
||||
* int someInt, String someString)</pre>
|
||||
* </blockquote>
|
||||
*
|
||||
* <h4>Field Access Redirect Mode</h4>
|
||||
*
|
||||
* <p>The handler method signature varies depending on whether the redirector is
|
||||
* handling a field <b>write</b> (<tt>PUTFIELD</tt>, <tt>PUTSTATIC</tt>) or a
|
||||
* field <b>read</b> (<tt>GETFIELD</tt>, <tt>GETSTATIC</tt>).</p>
|
||||
*
|
||||
* <table width="100%">
|
||||
* <tr>
|
||||
* <th>Operation (OPCODE)</th>
|
||||
* <th>Handler signature</th>
|
||||
* </tr>
|
||||
* <tr>
|
||||
* <td>Read static field (<tt>GETSTATIC</tt>)</td>
|
||||
* <td><code>private <b>FieldType</b> getFieldValue()</code></td>
|
||||
* </tr>
|
||||
* <tr>
|
||||
* <td>Read instance field (<tt>GETFIELD</tt>)</td>
|
||||
* <td><code>private <b>FieldType</b> getFieldValue(<b>OwnerType</b>
|
||||
* owner)</code></td>
|
||||
* </tr>
|
||||
* <tr>
|
||||
* <td>Write static field (<tt>PUTSTATIC</tt>)</td>
|
||||
* <td><code>private void setFieldValue(<b>FieldType</b> value)</code></td>
|
||||
* </tr>
|
||||
* <tr>
|
||||
* <td>Write instance field (<tt>PUTFIELD</tt>)</td>
|
||||
* <td><code>private void getFieldValue(<b>OwnerType</b>
|
||||
* owner, <b>FieldType</b> value)</code></td>
|
||||
* </tr>
|
||||
* </table>
|
||||
*
|
||||
* <p>Field redirectors do not support capture of enclosing method arguments.
|
||||
* </p>
|
||||
*/
|
||||
@Target({ ElementType.METHOD })
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
|
||||
@@ -80,7 +80,7 @@ public abstract class InvokeInjector extends Injector {
|
||||
+ " in " + this);
|
||||
}
|
||||
|
||||
this.inject(target, (MethodInsnNode)node);
|
||||
this.injectAtInvoke(target, (MethodInsnNode)node);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -89,7 +89,7 @@ public abstract class InvokeInjector extends Injector {
|
||||
* @param target Target to inject into
|
||||
* @param node Discovered instruction node
|
||||
*/
|
||||
protected abstract void inject(Target target, MethodInsnNode node);
|
||||
protected abstract void injectAtInvoke(Target target, MethodInsnNode node);
|
||||
|
||||
/**
|
||||
* @param args handler arguments
|
||||
|
||||
@@ -86,7 +86,7 @@ public class ModifyArgInjector extends InvokeInjector {
|
||||
* Do the injection
|
||||
*/
|
||||
@Override
|
||||
protected void inject(Target target, MethodInsnNode node) {
|
||||
protected void injectAtInvoke(Target target, MethodInsnNode node) {
|
||||
Type[] args = Type.getArgumentTypes(node.desc);
|
||||
int argIndex = this.findArgIndex(target, args);
|
||||
InsnList insns = new InsnList();
|
||||
|
||||
@@ -26,8 +26,12 @@ package org.spongepowered.asm.mixin.injection.invoke;
|
||||
|
||||
import org.spongepowered.asm.lib.Opcodes;
|
||||
import org.spongepowered.asm.lib.Type;
|
||||
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.InsnNode;
|
||||
import org.spongepowered.asm.lib.tree.MethodInsnNode;
|
||||
import org.spongepowered.asm.lib.tree.VarInsnNode;
|
||||
import org.spongepowered.asm.mixin.injection.InvalidInjectionException;
|
||||
import org.spongepowered.asm.mixin.injection.code.Injector;
|
||||
import org.spongepowered.asm.mixin.injection.struct.InjectionInfo;
|
||||
@@ -38,11 +42,12 @@ import com.google.common.collect.ObjectArrays;
|
||||
import com.google.common.primitives.Ints;
|
||||
|
||||
/**
|
||||
* <p>A bytecode injector which allows a method call to be redirected to the
|
||||
* annotated handler method. The handler method signature must match the hooked
|
||||
* method precisely <b>but</b> prepended with an arg of the owning object's type
|
||||
* to accept the object instance the method was going to be invoked on. For
|
||||
* example when hooking the following call:</p>
|
||||
* <p>A bytecode injector which allows a method call or field access to be
|
||||
* redirected to the annotated handler method. For method redirects, the handler
|
||||
* method signature must match the hooked method precisely <b>but</b> prepended
|
||||
* with an arg of the owning object's type to accept the object instance the
|
||||
* method was going to be invoked on. For example when hooking the following
|
||||
* call:</p>
|
||||
*
|
||||
* <blockquote><pre>
|
||||
* int abc = 0;
|
||||
@@ -62,6 +67,9 @@ import com.google.common.primitives.Ints;
|
||||
* <p>For obvious reasons this does not apply for static methods, for static
|
||||
* methods it is sufficient that the signature simply match the hooked method.
|
||||
* </p>
|
||||
*
|
||||
* <p>For field redirections, see the details in {@link Redirect} for the
|
||||
* required signature of the handler method.</p>
|
||||
*/
|
||||
public class RedirectInjector extends InvokeInjector {
|
||||
|
||||
@@ -71,12 +79,33 @@ public class RedirectInjector extends InvokeInjector {
|
||||
public RedirectInjector(InjectionInfo info) {
|
||||
super(info, "@Redirect");
|
||||
}
|
||||
|
||||
/**
|
||||
* Do the injection
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see org.spongepowered.asm.mixin.injection.callback.BytecodeInjector
|
||||
* #inject(org.spongepowered.asm.mixin.injection.callback.Target,
|
||||
* org.objectweb.asm.tree.AbstractInsnNode)
|
||||
*/
|
||||
@Override
|
||||
protected void inject(Target target, MethodInsnNode node) {
|
||||
protected void inject(Target target, AbstractInsnNode node) {
|
||||
if (node instanceof MethodInsnNode) {
|
||||
this.injectAtInvoke(target, (MethodInsnNode)node);
|
||||
return;
|
||||
}
|
||||
|
||||
if (node instanceof FieldInsnNode) {
|
||||
this.injectAtFieldAccess(target, (FieldInsnNode)node);
|
||||
return;
|
||||
}
|
||||
|
||||
throw new InvalidInjectionException(this.info, this.annotationType + " annotation on is targetting an invalid insn in "
|
||||
+ target + " in " + this);
|
||||
}
|
||||
|
||||
/**
|
||||
* Redirect a method invocation
|
||||
*/
|
||||
@Override
|
||||
protected void injectAtInvoke(Target target, MethodInsnNode node) {
|
||||
boolean injectTargetParams = false;
|
||||
boolean targetIsStatic = node.getOpcode() == Opcodes.INVOKESTATIC;
|
||||
Type ownerType = Type.getType("L" + node.owner + ";");
|
||||
@@ -90,8 +119,8 @@ public class RedirectInjector extends InvokeInjector {
|
||||
if (alternateDesc.equals(this.methodNode.desc)) {
|
||||
injectTargetParams = true;
|
||||
} else {
|
||||
throw new InvalidInjectionException(this.info, "@Redirect handler method " + this.methodNode.name + " has an invalid signature "
|
||||
+ ", expected " + desc + " found " + this.methodNode.desc);
|
||||
throw new InvalidInjectionException(this.info, this.annotationType + " handler method " + this
|
||||
+ " has an invalid signature, expected " + desc + " found " + this.methodNode.desc);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -112,4 +141,87 @@ public class RedirectInjector extends InvokeInjector {
|
||||
target.addToLocals(extraLocals);
|
||||
target.addToStack(extraStack);
|
||||
}
|
||||
|
||||
/**
|
||||
* Redirect a field get or set operation
|
||||
*/
|
||||
private void injectAtFieldAccess(Target target, FieldInsnNode node) {
|
||||
boolean staticField = node.getOpcode() == Opcodes.GETSTATIC || node.getOpcode() == Opcodes.PUTSTATIC;
|
||||
Type ownerType = Type.getType("L" + node.owner + ";");
|
||||
Type fieldType = Type.getType(node.desc);
|
||||
|
||||
InsnList insns = new InsnList();
|
||||
int extraStack = 0;
|
||||
if (node.getOpcode() == Opcodes.GETSTATIC || node.getOpcode() == Opcodes.GETFIELD) {
|
||||
extraStack = this.injectAtGetField(insns, target, node, staticField, ownerType, fieldType);
|
||||
} else if (node.getOpcode() == Opcodes.PUTSTATIC || node.getOpcode() == Opcodes.PUTFIELD) {
|
||||
extraStack = this.injectAtPutField(insns, target, node, staticField, ownerType, fieldType);
|
||||
} else {
|
||||
throw new InvalidInjectionException(this.info, "Unspported opcode " + node.getOpcode() + " on FieldInsnNode for " + this.info);
|
||||
}
|
||||
|
||||
target.insns.insertBefore(node, insns);
|
||||
target.insns.remove(node);
|
||||
target.addToStack(extraStack);
|
||||
}
|
||||
|
||||
/**
|
||||
* Inject opcodes to redirect a field getter. The injection will vary based
|
||||
* on the staticness of the field and the handler thus there are four
|
||||
* possible scenarios based on the possible combinations of static on the
|
||||
* handler and the field itself.
|
||||
*/
|
||||
private int injectAtGetField(InsnList insns, Target target, FieldInsnNode node, boolean staticField, Type owner, Type fieldType) {
|
||||
String handlerDesc = staticField ? ASMHelper.generateDescriptor(fieldType) : ASMHelper.generateDescriptor(fieldType, owner);
|
||||
this.checkDescriptor(handlerDesc, "getter");
|
||||
|
||||
if (!this.isStatic) {
|
||||
insns.add(new VarInsnNode(Opcodes.ALOAD, 0));
|
||||
if (!staticField) {
|
||||
insns.add(new InsnNode(Opcodes.SWAP));
|
||||
}
|
||||
}
|
||||
|
||||
this.invokeHandler(insns);
|
||||
return this.isStatic ? 0 : 1;
|
||||
}
|
||||
|
||||
/**
|
||||
* Inject opcodes to redirect a field setter. The injection will vary based
|
||||
* on the staticness of the field and the handler thus there are four
|
||||
* possible scenarios based on the possible combinations of static on the
|
||||
* handler and the field itself.
|
||||
*/
|
||||
private int injectAtPutField(InsnList insns, Target target, FieldInsnNode node, boolean staticField, Type owner, Type fieldType) {
|
||||
String handlerDesc = staticField ? ASMHelper.generateDescriptor(null, fieldType) : ASMHelper.generateDescriptor(null, owner, fieldType);
|
||||
this.checkDescriptor(handlerDesc, "setter");
|
||||
|
||||
if (!this.isStatic) {
|
||||
if (staticField) {
|
||||
insns.add(new VarInsnNode(Opcodes.ALOAD, 0));
|
||||
insns.add(new InsnNode(Opcodes.SWAP));
|
||||
} else {
|
||||
int marshallVar = target.allocateLocals(fieldType.getSize());
|
||||
insns.add(new VarInsnNode(fieldType.getOpcode(Opcodes.ISTORE), marshallVar));
|
||||
insns.add(new VarInsnNode(Opcodes.ALOAD, 0));
|
||||
insns.add(new InsnNode(Opcodes.SWAP));
|
||||
insns.add(new VarInsnNode(fieldType.getOpcode(Opcodes.ILOAD), marshallVar));
|
||||
}
|
||||
}
|
||||
|
||||
this.invokeHandler(insns);
|
||||
return !this.isStatic && !staticField ? 1 : 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check that the handler descriptor matches the calculated descriptor for
|
||||
* the field access being redirected.
|
||||
*/
|
||||
private void checkDescriptor(String handlerDesc, String type) {
|
||||
if (!this.methodNode.desc.equals(handlerDesc)) {
|
||||
throw new InvalidInjectionException(this.info, this.annotationType + " field " + type + " " + this
|
||||
+ " has an invalid signature. Expected " + handlerDesc + " but found " + this.methodNode.desc);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -898,4 +898,49 @@ public class ASMHelper {
|
||||
|
||||
return labels;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param obfType
|
||||
* @param returnType
|
||||
* @param args
|
||||
*/
|
||||
public static String generateDescriptor(/*int obfType, */Object returnType, Object... args)
|
||||
{
|
||||
StringBuilder sb = new StringBuilder().append('(');
|
||||
|
||||
for (Object arg : args)
|
||||
{
|
||||
sb.append(ASMHelper.toDescriptor(/*obfType, */arg));
|
||||
}
|
||||
|
||||
return sb.append(')').append(returnType != null ? ASMHelper.toDescriptor(/*obfType, */returnType) : "V").toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param obfType
|
||||
* @param arg
|
||||
*/
|
||||
private static String toDescriptor(/*int obfType, */Object arg)
|
||||
{
|
||||
// if (arg instanceof Obf)
|
||||
// {
|
||||
// return ((Obf)arg).getDescriptor(obfType);
|
||||
// }
|
||||
// else
|
||||
if (arg instanceof String)
|
||||
{
|
||||
return (String)arg;
|
||||
}
|
||||
else if (arg instanceof Type)
|
||||
{
|
||||
return arg.toString();
|
||||
}
|
||||
else if (arg instanceof Class)
|
||||
{
|
||||
return Type.getDescriptor((Class<?>)arg).toString();
|
||||
}
|
||||
|
||||
return arg == null ? "" : arg.toString();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
/*
|
||||
* This file is part of Mixin, licensed under the MIT License (MIT).
|
||||
*
|
||||
* Copyright (c) SpongePowered <https://www.spongepowered.org>
|
||||
* Copyright (c) contributors
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
package org.spongepowered.asm.util;
|
||||
|
||||
import org.spongepowered.asm.mixin.MixinException;
|
||||
|
||||
public class LVTGeneratorException extends MixinException {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
public LVTGeneratorException(String message) {
|
||||
super(message);
|
||||
}
|
||||
|
||||
public LVTGeneratorException(String message, Throwable cause) {
|
||||
super(message, cause);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in new issue
Block a user