mirror of
https://github.com/LegacyModdingMC/UniMix.git
synced 2026-11-20 01:49:34 +00:00
Fix/modifyarg(s) after redirect (#128)
* Fix: Resolve `@Redirect`s breaking `@ModifyArg`. * Fix: Resolve `@Redirect`s breaking `@ModifyArgs`.
This commit is contained in:
1 parent
a57c4fbb43
commit
3eb5281d66
4 files changed
+83
-24
No files matched your search
@@ -33,6 +33,7 @@ import org.objectweb.asm.tree.MethodInsnNode;
|
||||
import org.spongepowered.asm.mixin.injection.InjectionPoint;
|
||||
import org.spongepowered.asm.mixin.injection.InjectionPoint.RestrictTargetLevel;
|
||||
import org.spongepowered.asm.mixin.injection.ModifyArg;
|
||||
import org.spongepowered.asm.mixin.injection.invoke.util.InvokeUtil;
|
||||
import org.spongepowered.asm.mixin.injection.struct.InjectionInfo;
|
||||
import org.spongepowered.asm.mixin.injection.struct.InjectionNodes.InjectionNode;
|
||||
import org.spongepowered.asm.mixin.injection.struct.Target;
|
||||
@@ -109,15 +110,16 @@ public class ModifyArgInjector extends InvokeInjector {
|
||||
@Override
|
||||
protected void injectAtInvoke(Target target, InjectionNode node) {
|
||||
MethodInsnNode methodNode = (MethodInsnNode)node.getCurrentTarget();
|
||||
Type[] args = Type.getArgumentTypes(methodNode.desc);
|
||||
int argIndex = this.findArgIndex(target, args);
|
||||
Type[] originalArgs = InvokeUtil.getOriginalArgs(node);
|
||||
Type[] currentArgs = InvokeUtil.getCurrentArgs(node);
|
||||
int argIndex = this.findArgIndex(target, originalArgs);
|
||||
InsnList insns = new InsnList();
|
||||
Extension extraLocals = target.extendLocals();
|
||||
|
||||
if (this.singleArgMode) {
|
||||
this.injectSingleArgHandler(target, extraLocals, args, argIndex, insns);
|
||||
this.injectSingleArgHandler(target, extraLocals, currentArgs, argIndex, insns);
|
||||
} else {
|
||||
this.injectMultiArgHandler(target, extraLocals, args, argIndex, insns);
|
||||
this.injectMultiArgHandler(target, extraLocals, originalArgs, currentArgs, argIndex, insns);
|
||||
}
|
||||
|
||||
target.insns.insertBefore(methodNode, insns);
|
||||
@@ -138,17 +140,17 @@ public class ModifyArgInjector extends InvokeInjector {
|
||||
/**
|
||||
* Inject handler opcodes for a multi arg handler
|
||||
*/
|
||||
private void injectMultiArgHandler(Target target, Extension extraLocals, Type[] args, int argIndex, InsnList insns) {
|
||||
if (!Arrays.equals(args, this.methodArgs)) {
|
||||
private void injectMultiArgHandler(Target target, Extension extraLocals, Type[] originalArgs, Type[] currentArgs, int argIndex, InsnList insns) {
|
||||
if (!Arrays.equals(originalArgs, this.methodArgs)) {
|
||||
throw new InvalidInjectionException(this.info, "@ModifyArg method " + this + " targets a method with an invalid signature "
|
||||
+ Bytecode.getDescriptor(args) + ", expected " + Bytecode.getDescriptor(this.methodArgs));
|
||||
+ Bytecode.getDescriptor(originalArgs) + ", expected " + Bytecode.getDescriptor(this.methodArgs));
|
||||
}
|
||||
|
||||
int[] argMap = this.storeArgs(target, args, insns, 0);
|
||||
this.pushArgs(args, insns, argMap, 0, argIndex);
|
||||
this.invokeHandlerWithArgs(args, insns, argMap, 0, args.length);
|
||||
this.pushArgs(args, insns, argMap, argIndex + 1, args.length);
|
||||
extraLocals.add((argMap[argMap.length - 1] - target.getMaxLocals()) + args[args.length - 1].getSize());
|
||||
int[] argMap = this.storeArgs(target, currentArgs, insns, 0);
|
||||
this.pushArgs(currentArgs, insns, argMap, 0, argIndex);
|
||||
this.invokeHandlerWithArgs(originalArgs, insns, argMap, 0, originalArgs.length);
|
||||
this.pushArgs(currentArgs, insns, argMap, argIndex + 1, currentArgs.length);
|
||||
extraLocals.add((argMap[argMap.length - 1] - target.getMaxLocals()) + currentArgs[currentArgs.length - 1].getSize());
|
||||
}
|
||||
|
||||
protected int findArgIndex(Target target, Type[] args) {
|
||||
|
||||
+19
-11
@@ -33,6 +33,7 @@ import org.objectweb.asm.tree.VarInsnNode;
|
||||
import org.spongepowered.asm.mixin.injection.InjectionPoint.RestrictTargetLevel;
|
||||
import org.spongepowered.asm.mixin.injection.ModifyArgs;
|
||||
import org.spongepowered.asm.mixin.injection.invoke.arg.ArgsClassGenerator;
|
||||
import org.spongepowered.asm.mixin.injection.invoke.util.InvokeUtil;
|
||||
import org.spongepowered.asm.mixin.injection.struct.InjectionInfo;
|
||||
import org.spongepowered.asm.mixin.injection.struct.InjectionNodes.InjectionNode;
|
||||
import org.spongepowered.asm.mixin.injection.struct.Target;
|
||||
@@ -40,6 +41,8 @@ import org.spongepowered.asm.mixin.injection.struct.Target.Extension;
|
||||
import org.spongepowered.asm.mixin.injection.throwables.InvalidInjectionException;
|
||||
import org.spongepowered.asm.util.Bytecode;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
/**
|
||||
* A bytecode injector which allows a single argument of a chosen method call to
|
||||
* be altered. For details see javadoc for {@link ModifyArgs @ModifyArgs}.
|
||||
@@ -78,28 +81,33 @@ public class ModifyArgsInjector extends InvokeInjector {
|
||||
@Override
|
||||
protected void injectAtInvoke(Target target, InjectionNode node) {
|
||||
MethodInsnNode targetMethod = (MethodInsnNode)node.getCurrentTarget();
|
||||
|
||||
Type[] args = Type.getArgumentTypes(targetMethod.desc);
|
||||
if (args.length == 0) {
|
||||
|
||||
Type[] originalArgs = InvokeUtil.getOriginalArgs(node);
|
||||
Type[] currentArgs = InvokeUtil.getCurrentArgs(node);
|
||||
if (originalArgs.length == 0) {
|
||||
throw new InvalidInjectionException(this.info, "@ModifyArgs injector " + this + " targets a method invocation "
|
||||
+ targetMethod.name + targetMethod.desc + " with no arguments!");
|
||||
}
|
||||
|
||||
String clArgs = this.argsClassGenerator.getArgsClass(targetMethod.desc, this.info.getMixin().getMixin()).getName();
|
||||
|
||||
String originalDesc = Type.getMethodDescriptor(Type.VOID_TYPE, originalArgs);
|
||||
String clArgs = this.argsClassGenerator.getArgsClass(originalDesc, this.info.getMixin().getMixin()).getName();
|
||||
boolean withArgs = this.verifyTarget(target);
|
||||
|
||||
InsnList insns = new InsnList();
|
||||
Extension extraStack = target.extendStack().add(1);
|
||||
|
||||
this.packArgs(insns, clArgs, targetMethod);
|
||||
|
||||
|
||||
Type[] extraArgs = Arrays.copyOfRange(currentArgs, originalArgs.length, currentArgs.length);
|
||||
int[] extraArgMap = this.storeArgs(target, extraArgs, insns, 0);
|
||||
this.packArgs(insns, clArgs, originalDesc);
|
||||
|
||||
if (withArgs) {
|
||||
extraStack.add(target.arguments);
|
||||
Bytecode.loadArgs(target.arguments, insns, target.isStatic ? 0 : 1);
|
||||
}
|
||||
|
||||
this.invokeHandler(insns);
|
||||
this.unpackArgs(insns, clArgs, args);
|
||||
this.unpackArgs(insns, clArgs, originalArgs);
|
||||
this.pushArgs(extraArgs, insns, extraArgMap, 0, extraArgs.length);
|
||||
|
||||
extraStack.apply();
|
||||
target.insns.insertBefore(targetMethod, insns);
|
||||
@@ -121,8 +129,8 @@ public class ModifyArgsInjector extends InvokeInjector {
|
||||
return false;
|
||||
}
|
||||
|
||||
private void packArgs(InsnList insns, String clArgs, MethodInsnNode targetMethod) {
|
||||
String factoryDesc = Bytecode.changeDescriptorReturnType(targetMethod.desc, "L" + clArgs + ";");
|
||||
private void packArgs(InsnList insns, String clArgs, String targetDesc) {
|
||||
String factoryDesc = Bytecode.changeDescriptorReturnType(targetDesc, "L" + clArgs + ";");
|
||||
insns.add(new MethodInsnNode(Opcodes.INVOKESTATIC, clArgs, "of", factoryDesc, false));
|
||||
insns.add(new InsnNode(Opcodes.DUP));
|
||||
|
||||
|
||||
@@ -103,7 +103,7 @@ public class RedirectInjector extends InvokeInjector {
|
||||
/**
|
||||
* Meta decoration object for redirector target nodes
|
||||
*/
|
||||
class Meta {
|
||||
public class Meta {
|
||||
|
||||
public static final String KEY = "redirector";
|
||||
|
||||
|
||||
@@ -0,0 +1,49 @@
|
||||
/*
|
||||
* 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.injection.invoke.util;
|
||||
|
||||
import org.objectweb.asm.Opcodes;
|
||||
import org.objectweb.asm.Type;
|
||||
import org.objectweb.asm.tree.MethodInsnNode;
|
||||
import org.spongepowered.asm.mixin.injection.invoke.RedirectInjector;
|
||||
import org.spongepowered.asm.mixin.injection.struct.InjectionNodes.InjectionNode;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
public class InvokeUtil {
|
||||
public static Type[] getOriginalArgs(InjectionNode node) {
|
||||
return Type.getArgumentTypes(((MethodInsnNode) node.getOriginalTarget()).desc);
|
||||
}
|
||||
|
||||
public static Type[] getCurrentArgs(InjectionNode node) {
|
||||
MethodInsnNode methodNode = (MethodInsnNode) node.getCurrentTarget();
|
||||
Type[] currentArgs = Type.getArgumentTypes(methodNode.desc);
|
||||
if (node.isReplaced() && node.hasDecoration(RedirectInjector.Meta.KEY) && methodNode.getOpcode() != Opcodes.INVOKESTATIC) {
|
||||
// A non-static redirect handler method will have an extra arg at the start that we don't care about.
|
||||
return Arrays.copyOfRange(currentArgs, 1, currentArgs.length);
|
||||
}
|
||||
return currentArgs;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user