-
-
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 status checks…
Another wrap operation case
Showing
15 changed files
with
331 additions
and
54 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
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
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
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
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
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
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
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
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
181 changes: 181 additions & 0 deletions
181
...tion/src/main/java/org/sinytra/adapter/patch/transformer/dynfix/WrapOperationSurgeon.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,181 @@ | ||
package org.sinytra.adapter.patch.transformer.dynfix; | ||
|
||
import com.google.common.collect.HashMultimap; | ||
import com.google.common.collect.Multimap; | ||
import com.mojang.datafixers.util.Pair; | ||
import org.jetbrains.annotations.Nullable; | ||
import org.objectweb.asm.Opcodes; | ||
import org.objectweb.asm.Type; | ||
import org.objectweb.asm.tree.*; | ||
import org.sinytra.adapter.patch.analysis.MethodCallAnalyzer; | ||
import org.sinytra.adapter.patch.analysis.locals.LocalVariableLookup; | ||
import org.sinytra.adapter.patch.api.MethodContext; | ||
import org.sinytra.adapter.patch.api.Patch; | ||
import org.sinytra.adapter.patch.fixes.BytecodeFixerUpper; | ||
import org.sinytra.adapter.patch.fixes.MethodUpgrader; | ||
import org.sinytra.adapter.patch.fixes.TypeAdapter; | ||
import org.sinytra.adapter.patch.transformer.operation.param.ParamTransformationUtil; | ||
import org.sinytra.adapter.patch.transformer.pipeline.MethodTransformationPipeline; | ||
import org.sinytra.adapter.patch.util.AdapterUtil; | ||
import org.sinytra.adapter.patch.util.MethodQualifier; | ||
import org.sinytra.adapter.patch.util.OpcodeUtil; | ||
|
||
import java.util.*; | ||
import java.util.function.Consumer; | ||
|
||
public class WrapOperationSurgeon { | ||
|
||
public static Patch.Result tryUpgrade(MethodContext methodContext, MethodInsnNode cleanInsn, MethodInsnNode dirtyInsn) { | ||
MethodNode methodNode = methodContext.getMixinMethod(); | ||
LocalVariableLookup mixinLocals = new LocalVariableLookup(methodNode); | ||
|
||
Multimap<Integer, VarInsnNode> usedVars = getUsedVars(mixinLocals, methodContext); | ||
Map<Integer, Pair<TypeAdapter, @Nullable Consumer<InsnList>>> adapters = new HashMap<>(); | ||
|
||
for (Integer key : usedVars.keys()) { | ||
if (key == 0) { | ||
Pair<TypeAdapter, @Nullable Consumer<InsnList>> pair = findReplacementForInstance(cleanInsn, dirtyInsn, methodContext); | ||
if (pair != null) { | ||
adapters.put(key, pair); | ||
continue; | ||
} | ||
} | ||
return Patch.Result.PASS; | ||
} | ||
|
||
MethodQualifier oldQualifier = methodContext.getInjectionPointMethodQualifier(); | ||
String newQualifier = MethodCallAnalyzer.getCallQualifier(dirtyInsn); | ||
return MethodTransformationPipeline.builder(b -> b.modifyInjectionPoint("INVOKE", newQualifier, false, true)) | ||
.onSuccess(() -> (c, m, mtx, ctx) -> { | ||
MethodUpgrader.upgradeWrapOperationLayered(methodContext, oldQualifier, MethodQualifier.create(newQualifier).orElseThrow()); | ||
|
||
usedVars.forEach((i, insn) -> { | ||
Pair<TypeAdapter, @Nullable Consumer<InsnList>> adapter = adapters.get(i); | ||
adapter.getFirst().apply(methodNode.instructions, insn); | ||
}); | ||
|
||
for (Pair<TypeAdapter, @Nullable Consumer<InsnList>> pair : adapters.values()) { | ||
if (pair.getSecond() != null) { | ||
pair.getSecond().accept(methodNode.instructions); | ||
} | ||
} | ||
|
||
return Patch.Result.APPLY; | ||
}) | ||
.apply(methodContext); | ||
} | ||
|
||
@Nullable | ||
private static Pair<TypeAdapter, @Nullable Consumer<InsnList>> findReplacementForInstance(MethodInsnNode cleanInsn, MethodInsnNode dirtyInsn, MethodContext methodContext) { | ||
MethodNode cleanTargetMethod = methodContext.findCleanInjectionTarget().methodNode(); | ||
List<AbstractInsnNode> receiverInsns = getMethodInvocationsInsns(cleanTargetMethod, cleanInsn, 0); | ||
if (!receiverInsns.isEmpty() && receiverInsns.getFirst() instanceof VarInsnNode varInsn) { | ||
BytecodeFixerUpper bfu = methodContext.patchContext().environment().bytecodeFixerUpper(); | ||
if (bfu == null) { | ||
return null; | ||
} | ||
|
||
LocalVariableLookup cleanLookup = methodContext.cleanLocalsTable(); | ||
LocalVariableNode lvn = cleanLookup.getByIndex(varInsn.var); | ||
|
||
TypeAdapter typeAdapter = bfu.getTypeAdapter(Type.getObjectType(dirtyInsn.owner), Type.getType(lvn.desc)); | ||
if (typeAdapter == null) { | ||
return null; | ||
} | ||
|
||
List<AbstractInsnNode> subList = receiverInsns.subList(1, receiverInsns.size()).stream().map(i -> i.clone(Map.of())).toList(); | ||
|
||
TypeAdapter adapter = typeAdapter.andThen((list, insn) -> { | ||
list.insert(insn, AdapterUtil.insnList(subList)); | ||
}); | ||
Consumer<InsnList> castCheck = subList.getFirst() instanceof TypeInsnNode typeInsn && typeInsn.getOpcode() == Opcodes.CHECKCAST ? | ||
list -> { | ||
List<AbstractInsnNode> originalWOCall = new ArrayList<>(ParamTransformationUtil.findWrapOperationOriginalCallArgs(methodContext.getMixinMethod(), methodContext)); | ||
// Include final method call and cast | ||
originalWOCall.add(originalWOCall.getLast().getNext()); | ||
originalWOCall.add(originalWOCall.getLast().getNext()); | ||
originalWOCall.add(originalWOCall.getLast().getNext()); | ||
List<AbstractInsnNode> cloned = originalWOCall.stream().map(i -> i.clone(Map.of())).toList(); | ||
|
||
boolean hasLabel = list.getFirst() instanceof LabelNode; | ||
LabelNode label = hasLabel ? (LabelNode) list.getFirst() : new LabelNode(); | ||
InsnList check = AdapterUtil.insnList( | ||
new LabelNode(), | ||
new VarInsnNode(Opcodes.ALOAD, 0), | ||
new TypeInsnNode(Opcodes.INSTANCEOF, typeInsn.desc), | ||
new JumpInsnNode(Opcodes.IFNE, label), | ||
new LabelNode() | ||
); | ||
typeAdapter.apply(check, check.get(1)); | ||
check.add(AdapterUtil.insnList(cloned)); | ||
check.add(new InsnNode(OpcodeUtil.getReturnOpcode(methodContext.getMixinMethod()))); | ||
if (!hasLabel) { | ||
check.add(label); | ||
} | ||
|
||
list.insert(check); | ||
} | ||
: null; | ||
|
||
return Pair.of(adapter, castCheck); | ||
} | ||
return null; | ||
} | ||
|
||
@Nullable | ||
private static List<AbstractInsnNode> getMethodInvocationsInsns(MethodNode method, MethodInsnNode minsn, int param) { | ||
List<AbstractInsnNode> insns = new ArrayList<>(); | ||
if (getMethodInvocationsInsns(method, minsn, param, insns)) { | ||
return insns; | ||
} | ||
return null; | ||
} | ||
|
||
@Nullable | ||
private static boolean getMethodInvocationsInsns(MethodNode method, MethodInsnNode minsn, int param, List<AbstractInsnNode> insns) { | ||
List<AbstractInsnNode> invocationInsns = MethodCallAnalyzer.findMethodCallParamInsns(method, minsn); | ||
if (invocationInsns.isEmpty()) { | ||
return false; | ||
} | ||
AbstractInsnNode receiver = invocationInsns.getFirst(); | ||
if (receiver instanceof VarInsnNode) { | ||
insns.addAll(0, invocationInsns); | ||
return true; | ||
} else if (receiver instanceof TypeInsnNode typeInsn && receiver.getPrevious() instanceof VarInsnNode varInsn) { | ||
insns.addFirst(typeInsn); | ||
insns.addFirst(varInsn); | ||
return true; | ||
} else if (receiver instanceof MethodInsnNode invocation) { | ||
List<AbstractInsnNode> methodInsns = new ArrayList<>(); | ||
if (getMethodInvocationsInsns(method, invocation, param, methodInsns)) { | ||
insns.add(invocationInsns.get(param)); | ||
insns.addAll(0, methodInsns); | ||
return true; | ||
} | ||
} | ||
return false; | ||
} | ||
|
||
private static Multimap<Integer, VarInsnNode> getUsedVars(LocalVariableLookup mixinLocals, MethodContext methodContext) { | ||
MethodNode methodNode = methodContext.getMixinMethod(); | ||
Type[] argsTypes = Type.getArgumentTypes(methodNode.desc); | ||
Set<Integer> paramVars = new HashSet<>(); | ||
for (int i = 0; i < argsTypes.length; i++) { | ||
if (argsTypes[i].equals(AdapterUtil.OPERATION_TYPE)) { | ||
break; | ||
} | ||
LocalVariableNode lvn = mixinLocals.getByParameterOrdinal(i); | ||
paramVars.add(lvn.index); | ||
} | ||
|
||
List<AbstractInsnNode> originalOpCall = ParamTransformationUtil.findWrapOperationOriginalCallArgs(methodNode, methodContext); | ||
Multimap<Integer, VarInsnNode> usedVars = HashMultimap.create(); | ||
for (AbstractInsnNode insn : methodNode.instructions) { | ||
if (insn instanceof VarInsnNode varInsn && !originalOpCall.contains(insn) && paramVars.contains(varInsn.var)) { | ||
usedVars.put(varInsn.var, varInsn); | ||
} | ||
} | ||
|
||
return usedVars; | ||
} | ||
} |
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
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
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
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
30 changes: 30 additions & 0 deletions
30
test/src/testClasses/java/org/sinytra/adapter/test/mixin/PiglinAiMixin.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,30 @@ | ||
package org.sinytra.adapter.test.mixin; | ||
|
||
import com.llamalad7.mixinextras.injector.wrapoperation.Operation; | ||
import com.llamalad7.mixinextras.injector.wrapoperation.WrapOperation; | ||
import net.minecraft.core.Holder; | ||
import net.minecraft.world.entity.LivingEntity; | ||
import net.minecraft.world.entity.monster.piglin.PiglinAi; | ||
import net.minecraft.world.item.ArmorItem; | ||
import net.minecraft.world.item.ArmorMaterial; | ||
import net.minecraft.world.item.ArmorMaterials; | ||
import net.minecraft.world.item.ItemStack; | ||
import org.spongepowered.asm.mixin.Mixin; | ||
import org.spongepowered.asm.mixin.injection.At; | ||
|
||
@Mixin(PiglinAi.class) | ||
public class PiglinAiMixin { | ||
// https://github.com/quiqueck/BetterNether/blob/e1c5bea37001728844d16feec3ef3b3f14ae5139/src/main/java/org/betterx/betternether/mixin/common/piglin/PiglinAiMixin.java | ||
@WrapOperation(method = "isWearingGold", at = @At(value = "INVOKE", target = "Lnet/minecraft/core/Holder;is(Lnet/minecraft/core/Holder;)Z")) | ||
private static boolean isWearingGold(Holder<ArmorMaterial> instance, Holder<ArmorMaterial> tHolder, Operation<Boolean> original) { | ||
return original.call(instance, tHolder) || instance.is(ArmorMaterials.DIAMOND); | ||
} | ||
|
||
@WrapOperation(method = "isWearingGold", at = @At(value = "INVOKE", target = "Lnet/minecraft/world/item/ItemStack;makesPiglinsNeutral(Lnet/minecraft/world/entity/LivingEntity;)Z")) | ||
private static boolean isWearingGoldExpected(ItemStack instance, LivingEntity tHolder, Operation<Boolean> original) { | ||
if (!(instance.getItem() instanceof ArmorItem)) { | ||
return original.call(instance, tHolder); | ||
} | ||
return original.call(instance, tHolder) || ((ArmorItem) instance.getItem()).getMaterial().is(ArmorMaterials.DIAMOND); | ||
} | ||
} |