-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f2dc31c
commit 469839c
Showing
7 changed files
with
158 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
...in/java/dev/su5ed/sinytra/adapter/patch/transformer/param/RemoveParameterTransformer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package dev.su5ed.sinytra.adapter.patch.transformer.param; | ||
|
||
import com.mojang.serialization.Codec; | ||
import dev.su5ed.sinytra.adapter.patch.api.MethodContext; | ||
import dev.su5ed.sinytra.adapter.patch.api.Patch; | ||
import dev.su5ed.sinytra.adapter.patch.api.PatchContext; | ||
import dev.su5ed.sinytra.adapter.patch.util.AdapterUtil; | ||
import org.objectweb.asm.Type; | ||
import org.objectweb.asm.tree.ClassNode; | ||
import org.objectweb.asm.tree.LocalVariableNode; | ||
import org.objectweb.asm.tree.MethodNode; | ||
|
||
import java.util.List; | ||
|
||
public record RemoveParameterTransformer(int index) implements ParameterTransformer { | ||
public static final Codec<RemoveParameterTransformer> CODEC = Codec.intRange(0, 255) | ||
.fieldOf("index").xmap(RemoveParameterTransformer::new, RemoveParameterTransformer::index) | ||
.codec(); | ||
|
||
@Override | ||
public Patch.Result apply(ClassNode classNode, MethodNode methodNode, MethodContext methodContext, PatchContext context, List<Type> parameters, int offset) { | ||
final int target = this.index() + offset; | ||
final int lvtIndex = ParameterTransformer.calculateLVTIndex(parameters, !methodContext.isStatic(methodNode), target); | ||
|
||
// Remove the use of the param in a wrapop first to avoid the new LVT messing with the outcome of that | ||
extractWrapOperation(methodContext, methodNode, parameters, op -> op.removeParameter(target)); | ||
|
||
withLVTSnapshot(methodNode, () -> { | ||
LocalVariableNode lvn = methodNode.localVariables.stream() | ||
.filter(v -> v.index == lvtIndex) | ||
.findFirst() | ||
.orElse(null); | ||
if (lvn != null) { | ||
methodNode.localVariables.remove(lvn); | ||
AdapterUtil.replaceLVT(methodNode, idx -> idx == lvtIndex ? -1 : idx); | ||
} | ||
}); | ||
|
||
methodNode.parameters.remove(target); | ||
parameters.remove(target); | ||
|
||
return Patch.Result.COMPUTE_FRAMES; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
definition/src/test/java/dev/su5ed/sinytra/adapter/patch/test/mixin/ParameterRemoveTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package dev.su5ed.sinytra.adapter.patch.test.mixin; | ||
|
||
import dev.su5ed.sinytra.adapter.patch.api.MixinConstants; | ||
import dev.su5ed.sinytra.adapter.patch.api.Patch; | ||
import org.junit.jupiter.api.Test; | ||
|
||
public class ParameterRemoveTest extends MixinPatchTest { | ||
@Test | ||
void testSimpleRemove() throws Exception { | ||
assertSameCode( | ||
"org/sinytra/adapter/test/mixins/ParameterRemoveMixin", | ||
"testSimpleRemove", | ||
Patch.builder() | ||
.targetInjectionPoint("") | ||
.targetMethod("testSimple") | ||
.targetMixinType(MixinConstants.INJECT) | ||
.transformParams(params -> params.remove(2)) | ||
); | ||
} | ||
|
||
@Test | ||
void testWrapOpRemove() throws Exception { | ||
assertSameCode( | ||
"org/sinytra/adapter/test/mixins/ParameterRemoveMixin", | ||
"testWrapOpRemove", | ||
Patch.builder() | ||
.targetMethod("testWrapOp") | ||
.transformParams(params -> params.remove(2)) | ||
); | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
definition/src/testClasses/java/org/sinytra/adapter/test/classes/ParameterRemove.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package org.sinytra.adapter.test.classes; | ||
|
||
import java.util.concurrent.atomic.AtomicBoolean; | ||
|
||
public class ParameterRemove { | ||
public static void testSimple(int p1, char p2) { | ||
|
||
} | ||
|
||
public static void testWrapOp(SObj sObj, AtomicBoolean p1) { | ||
|
||
} | ||
|
||
public record SObj() { | ||
public void call(AtomicBoolean value) { | ||
|
||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
definition/src/testClasses/java/org/sinytra/adapter/test/mixins/ParameterRemoveMixin.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package org.sinytra.adapter.test.mixins; | ||
|
||
import com.llamalad7.mixinextras.injector.wrapoperation.Operation; | ||
import com.llamalad7.mixinextras.injector.wrapoperation.WrapOperation; | ||
import org.sinytra.adapter.test.classes.ParameterRemove; | ||
import org.spongepowered.asm.mixin.Mixin; | ||
import org.spongepowered.asm.mixin.injection.At; | ||
import org.spongepowered.asm.mixin.injection.Inject; | ||
|
||
import java.util.concurrent.atomic.AtomicBoolean; | ||
|
||
@Mixin(ParameterRemove.class) | ||
public class ParameterRemoveMixin { | ||
@Inject(method = "testSimple(IC)V", at = @At("HEAD")) | ||
private static void testSimpleRemove(int p1, char p2, boolean p3) { | ||
System.out.println("Hi: " + p2 / p1); | ||
} | ||
|
||
private static void testSimpleRemoveExpected(int p1, char p2) { | ||
System.out.println("Hi: " + p2 / p1); | ||
} | ||
|
||
@WrapOperation(method = "testWrapOp(Lorg/sinytra/adapter/test/classes/ParameterRemove$SObj;Ljava/util/concurrent/atomic/AtomicBoolean;)V", at = @At(value = "INVOKE", target = "Lorg/sinytra/adapter/test/classes/ParameterRemove$SObj;call(Ljava/util/concurrent/atomic/AtomicBoolean;)V")) | ||
private static void testWrapOpRemove(ParameterRemove.SObj obj, AtomicBoolean p1, int p2, Operation<Void> operation) { | ||
System.out.println(operation.call(obj, p1, p2)); | ||
} | ||
|
||
private static void testWrapOpRemoveExpected(ParameterRemove.SObj obj, AtomicBoolean p1, Operation<Void> operation) { | ||
System.out.println(operation.call(obj, p1)); | ||
} | ||
} |