Skip to content

Commit

Permalink
Revert "Fix/modifyarg(s) after redirect (#128)"
Browse files Browse the repository at this point in the history
This reverts commit 3eb5281
  • Loading branch information
LlamaLad7 committed Jul 9, 2024
1 parent b20b9b3 commit 96b758d
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 86 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
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;
Expand Down Expand Up @@ -110,16 +109,15 @@ protected void inject(Target target, InjectionNode node) {
@Override
protected void injectAtInvoke(Target target, InjectionNode node) {
MethodInsnNode methodNode = (MethodInsnNode)node.getCurrentTarget();
Type[] originalArgs = InvokeUtil.getOriginalArgs(node);
Type[] currentArgs = InvokeUtil.getCurrentArgs(node);
int argIndex = this.findArgIndex(target, originalArgs);
Type[] args = Type.getArgumentTypes(methodNode.desc);
int argIndex = this.findArgIndex(target, args);
InsnList insns = new InsnList();
Extension extraLocals = target.extendLocals();

if (this.singleArgMode) {
this.injectSingleArgHandler(target, extraLocals, currentArgs, argIndex, insns);
this.injectSingleArgHandler(target, extraLocals, args, argIndex, insns);
} else {
this.injectMultiArgHandler(target, extraLocals, originalArgs, currentArgs, argIndex, insns);
this.injectMultiArgHandler(target, extraLocals, args, argIndex, insns);
}

target.insns.insertBefore(methodNode, insns);
Expand All @@ -145,17 +143,17 @@ private void injectSingleArgHandler(Target target, Extension extraLocals, Type[]
/**
* Inject handler opcodes for a multi arg handler
*/
private void injectMultiArgHandler(Target target, Extension extraLocals, Type[] originalArgs, Type[] currentArgs, int argIndex, InsnList insns) {
if (!Arrays.equals(originalArgs, this.methodArgs)) {
private void injectMultiArgHandler(Target target, Extension extraLocals, Type[] args, int argIndex, InsnList insns) {
if (!Arrays.equals(args, this.methodArgs)) {
throw new InvalidInjectionException(this.info, "@ModifyArg method " + this + " targets a method with an invalid signature "
+ Bytecode.getDescriptor(originalArgs) + ", expected " + Bytecode.getDescriptor(this.methodArgs));
+ Bytecode.getDescriptor(args) + ", expected " + Bytecode.getDescriptor(this.methodArgs));
}

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());
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());
}

protected int findArgIndex(Target target, Type[] args) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,13 @@
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;
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}.
Expand Down Expand Up @@ -81,33 +78,28 @@ protected void inject(Target target, InjectionNode node) {
@Override
protected void injectAtInvoke(Target target, InjectionNode node) {
MethodInsnNode targetMethod = (MethodInsnNode)node.getCurrentTarget();

Type[] originalArgs = InvokeUtil.getOriginalArgs(node);
Type[] currentArgs = InvokeUtil.getCurrentArgs(node);
if (originalArgs.length == 0) {

Type[] args = Type.getArgumentTypes(targetMethod.desc);
if (args.length == 0) {
throw new InvalidInjectionException(this.info, "@ModifyArgs injector " + this + " targets a method invocation "
+ targetMethod.name + targetMethod.desc + " with no arguments!");
}

String originalDesc = Type.getMethodDescriptor(Type.VOID_TYPE, originalArgs);
String clArgs = this.argsClassGenerator.getArgsClass(originalDesc, this.info.getMixin().getMixin()).getName();

String clArgs = this.argsClassGenerator.getArgsClass(targetMethod.desc, this.info.getMixin().getMixin()).getName();
boolean withArgs = this.verifyTarget(target);

InsnList insns = new InsnList();
Extension extraStack = target.extendStack().add(1);

Type[] extraArgs = Arrays.copyOfRange(currentArgs, originalArgs.length, currentArgs.length);
int[] extraArgMap = this.storeArgs(target, extraArgs, insns, 0);
this.packArgs(insns, clArgs, originalDesc);


this.packArgs(insns, clArgs, targetMethod);

if (withArgs) {
extraStack.add(target.arguments);
Bytecode.loadArgs(target.arguments, insns, target.isStatic ? 0 : 1);
}

this.invokeHandler(insns);
this.unpackArgs(insns, clArgs, originalArgs);
this.pushArgs(extraArgs, insns, extraArgMap, 0, extraArgs.length);
this.unpackArgs(insns, clArgs, args);

extraStack.apply();
target.insns.insertBefore(targetMethod, insns);
Expand All @@ -129,8 +121,8 @@ private boolean verifyTarget(Target target) {
return false;
}

private void packArgs(InsnList insns, String clArgs, String targetDesc) {
String factoryDesc = Bytecode.changeDescriptorReturnType(targetDesc, "L" + clArgs + ";");
private void packArgs(InsnList insns, String clArgs, MethodInsnNode targetMethod) {
String factoryDesc = Bytecode.changeDescriptorReturnType(targetMethod.desc, "L" + clArgs + ";");
insns.add(new MethodInsnNode(Opcodes.INVOKESTATIC, clArgs, "of", factoryDesc, false));
insns.add(new InsnNode(Opcodes.DUP));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ public class RedirectInjector extends InvokeInjector {
/**
* Meta decoration object for redirector target nodes
*/
public class Meta {
class Meta {

public static final String KEY = "redirector";

Expand Down

This file was deleted.

0 comments on commit 96b758d

Please sign in to comment.