From cad7263d9e7cd970a57f26949140914046edf22b Mon Sep 17 00:00:00 2001 From: Cynthia Foxwell Date: Thu, 14 Mar 2024 13:31:52 -0600 Subject: [PATCH] Fix existing transformers --- build.gradle | 3 +- .../pm/c7/scout/mixin/ItemStackMixin.java | 5 +- .../scout/mixin/ScreenHandlerTransformer.java | 132 +++++++----------- .../mixin/client/HandledScreenMixin.java | 5 +- .../client/HandledScreenTransformer.java | 64 ++++----- 5 files changed, 85 insertions(+), 124 deletions(-) diff --git a/build.gradle b/build.gradle index 4122399..84d0cde 100644 --- a/build.gradle +++ b/build.gradle @@ -48,7 +48,8 @@ dependencies { modImplementation libs.quilted.fabric.api - modApi include("dev.emi:trinkets:${libs.versions.trinkets.get()}") + // not JiJ because it causes its mixins to fail to load + modApi "dev.emi:trinkets:${libs.versions.trinkets.get()}" modApi include("com.unascribed:lib39-core:${libs.versions.lib39.get()}+${libs.versions.minecraft.get()}") modApi include("com.unascribed:lib39-dessicant:${libs.versions.lib39.get()}+${libs.versions.minecraft.get()}") diff --git a/src/main/java/pm/c7/scout/mixin/ItemStackMixin.java b/src/main/java/pm/c7/scout/mixin/ItemStackMixin.java index d5ec36c..d80a201 100644 --- a/src/main/java/pm/c7/scout/mixin/ItemStackMixin.java +++ b/src/main/java/pm/c7/scout/mixin/ItemStackMixin.java @@ -14,9 +14,8 @@ public class ItemStackMixin { // Excluding ourselves from this check to force unequip/equip when switching bag items fixes a duplication bug // Gross and hacky but oh well, can't mixin mixins. @Inject(method = "areEqual", at = @At("HEAD"), cancellable = true) - private void scout$grossTrinketsEquipFix(ItemStack newStack, CallbackInfoReturnable callbackInfo) { - ItemStack self = (ItemStack) (Object) this; - if (self.getItem() instanceof BaseBagItem && newStack.getItem() instanceof BaseBagItem) { + private static void scout$grossTrinketsEquipFix(ItemStack stack, ItemStack otherStack, CallbackInfoReturnable callbackInfo) { + if (stack.getItem() instanceof BaseBagItem && otherStack.getItem() instanceof BaseBagItem) { callbackInfo.setReturnValue(false); } } diff --git a/src/main/java/pm/c7/scout/mixin/ScreenHandlerTransformer.java b/src/main/java/pm/c7/scout/mixin/ScreenHandlerTransformer.java index 7cba273..7d06a56 100644 --- a/src/main/java/pm/c7/scout/mixin/ScreenHandlerTransformer.java +++ b/src/main/java/pm/c7/scout/mixin/ScreenHandlerTransformer.java @@ -2,6 +2,7 @@ package pm.c7.scout.mixin; import org.objectweb.asm.tree.*; import org.quiltmc.loader.api.QuiltLoader; + import pm.c7.scout.mixinsupport.ClassNodeTransformer; import static org.objectweb.asm.Opcodes.*; @@ -9,31 +10,22 @@ import static org.objectweb.asm.Opcodes.*; public class ScreenHandlerTransformer implements ClassNodeTransformer { @Override public void transform(String name, ClassNode node) { - var internalOnSlotClick = "m_nqfgpzfl"; - var insertItem = "m_jpjdgbxy"; + var resolver = QuiltLoader.getMappingResolver(); + var namespace = "intermediary"; - var PlayerEntity = "net/minecraft/unmapped/C_jzrpycqo"; - var playerScreenHandler = "f_xvlfpipb"; - - var PlayerScreenHandler = "net/minecraft/unmapped/C_wgehrbdx"; - var Slot = "net/minecraft/unmapped/C_nhvqfffd"; - var DefaultedList = "net/minecraft/unmapped/C_rnrfftze"; - - if (QuiltLoader.isDevelopmentEnvironment()) { - internalOnSlotClick = "internalOnSlotClick"; - insertItem = "insertItem"; - - PlayerEntity = "net/minecraft/entity/player/PlayerEntity"; - playerScreenHandler = "playerScreenHandler"; - - PlayerScreenHandler = "net/minecraft/screen/PlayerScreenHandler"; - Slot = "net/minecraft/screen/slot/Slot"; - DefaultedList = "net/minecraft/util/collection/DefaultedList"; - } + var internalOnSlotClick = resolver.mapMethodName(namespace, name, "method_30010", "(IILnet/minecraft/class_1713;Lnet/minecraft/class_1657;)V"); + var PlayerEntity = resolver.mapClassName(namespace, "net.minecraft.class_1657"); + var PlayerScreenHandler = slash(resolver.mapClassName(namespace, "net.minecraft.class_1723")); var LPlayerScreenHandler = L(PlayerScreenHandler); + String playerScreenHandler = resolver.mapFieldName(namespace, PlayerEntity, "field_7498", LPlayerScreenHandler); + PlayerEntity = slash(PlayerEntity); + + var Slot = slash(resolver.mapClassName(namespace, "net.minecraft.class_1735")); var LSlot = L(Slot); + var DefaultedList = slash(resolver.mapClassName(namespace, "net.minecraft.class_2371")); + for (var mn : node.methods) { if (mn.name.equals(internalOnSlotClick)) { for (var insn : mn.instructions) { @@ -42,32 +34,32 @@ public class ScreenHandlerTransformer implements ClassNodeTransformer { if (insn.getNext() instanceof JumpInsnNode nextInsn && nextInsn.getOpcode() == IFGE) { var jumpTo = nextInsn.label; mn.instructions.insert(nextInsn, insns( - ILOAD(1), - INVOKESTATIC("pm/c7/scout/ScoutUtil", "isBagSlot", "(I)Z"), - IFNE(jumpTo) + ILOAD(1), + INVOKESTATIC("pm/c7/scout/ScoutUtil", "isBagSlot", "(I)Z"), + IFNE(jumpTo) )); } - } else if (vin.getOpcode() == ASTORE && (vin.var == 6 || vin.var == 7)) { + } else if (vin.getOpcode() == ASTORE && vin.var == 7) { if (vin.getPrevious() instanceof TypeInsnNode prevInsn && prevInsn.getOpcode() == CHECKCAST && prevInsn.desc.equals(Slot)) { if (prevInsn.getPrevious() instanceof MethodInsnNode prevPrevInsn && prevPrevInsn.getOpcode() == INVOKEVIRTUAL) { if(prevPrevInsn.owner.equals(DefaultedList)) { LabelNode LnotBag = new LabelNode(); LabelNode Lend = new LabelNode(); mn.instructions.insertBefore(prevPrevInsn.getPrevious().getPrevious().getPrevious(), insns( - ILOAD(1), - INVOKESTATIC("pm/c7/scout/ScoutUtil", "isBagSlot", "(I)Z"), - IFEQ(LnotBag), - ILOAD(1), - ALOAD(4), - GETFIELD(PlayerEntity, playerScreenHandler, LPlayerScreenHandler), - INVOKESTATIC("pm/c7/scout/ScoutUtil", "getBagSlot", "(I" + LPlayerScreenHandler + ")" + LSlot), - CHECKCAST(Slot), - ASTORE(vin.var), - GOTO(Lend), - LnotBag + ILOAD(1), + INVOKESTATIC("pm/c7/scout/ScoutUtil", "isBagSlot", "(I)Z"), + IFEQ(LnotBag), + ILOAD(1), + ALOAD(4), + GETFIELD(PlayerEntity, playerScreenHandler, LPlayerScreenHandler), + INVOKESTATIC("pm/c7/scout/ScoutUtil", "getBagSlot", "(I" + LPlayerScreenHandler + ")" + LSlot), + CHECKCAST(Slot), + ASTORE(vin.var), + GOTO(Lend), + LnotBag )); mn.instructions.insert(vin, insns( - Lend + Lend )); } } @@ -75,44 +67,14 @@ public class ScreenHandlerTransformer implements ClassNodeTransformer { } } } - }/* else if (mn.name.equals(insertItem)) { - for (var insn : mn.instructions) { - if (insn instanceof VarInsnNode vin && vin.getOpcode() == ASTORE && vin.var == 7) { - if (vin.getPrevious() instanceof TypeInsnNode prevInsn && prevInsn.getOpcode() == CHECKCAST && prevInsn.desc.equals(Slot)) { - if (prevInsn.getPrevious() instanceof MethodInsnNode prevPrevInsn && prevPrevInsn.getOpcode() == INVOKEVIRTUAL) { - if(prevPrevInsn.owner.equals(DefaultedList)) { - LabelNode LnotBag = new LabelNode(); - LabelNode LpastSlot = new LabelNode(); - mn.instructions.insertBefore(prevPrevInsn.getPrevious().getPrevious().getPrevious(), insns( - INVOKESTATIC("pm/c7/scout/ScoutUtil", "getPlayerScreenHandler", "()" + LPlayerScreenHandler), - ASTORE(20), - ALOAD(20), - IFNULL(LpastSlot), - ILOAD(6), - INVOKESTATIC("pm/c7/scout/ScoutUtil", "isBagSlot", "(I)Z"), - IFEQ(LnotBag), - ILOAD(6), - ALOAD(20), - INVOKESTATIC("pm/c7/scout/ScoutUtil", "getBagSlot", "(I" + LPlayerScreenHandler + ")" + LSlot), - CHECKCAST(Slot), - ASTORE(vin.var), - LnotBag, - ILOAD(6), - INVOKESTATIC("pm/c7/scout/ScoutUtil", "isBagSlot", "(I)Z"), - IFNE(LpastSlot) - )); - mn.instructions.insert(vin, insns( - LpastSlot - )); - } - } - } - } - } - }*/ + } } } + private String slash(String clazz) { + return clazz.replaceAll("\\.", "/"); + } + private String L(String clazz) { return "L" + clazz + ";"; } @@ -122,34 +84,34 @@ public class ScreenHandlerTransformer implements ClassNodeTransformer { for (var i : insns) li.add(i); return li; } - private static VarInsnNode ILOAD(int var) { - return new VarInsnNode(ILOAD, var); + private static VarInsnNode ILOAD(int v) { + return new VarInsnNode(ILOAD, v); } private static MethodInsnNode INVOKESTATIC(String owner, String name, String desc) { return new MethodInsnNode(INVOKESTATIC, owner, name, desc); } - private static JumpInsnNode IFNE(LabelNode var) { - return new JumpInsnNode(IFNE, var); + private static JumpInsnNode IFNE(LabelNode v) { + return new JumpInsnNode(IFNE, v); } - private static JumpInsnNode IFEQ(LabelNode var) { - return new JumpInsnNode(IFEQ, var); + private static JumpInsnNode IFEQ(LabelNode v) { + return new JumpInsnNode(IFEQ, v); } - private static VarInsnNode ALOAD(int var) { - return new VarInsnNode(ALOAD, var); + private static VarInsnNode ALOAD(int v) { + return new VarInsnNode(ALOAD, v); } - private static VarInsnNode ASTORE(int var) { - return new VarInsnNode(ASTORE, var); + private static VarInsnNode ASTORE(int v) { + return new VarInsnNode(ASTORE, v); } private static FieldInsnNode GETFIELD(String owner, String name, String desc) { return new FieldInsnNode(GETFIELD, owner, name, desc); } - private static JumpInsnNode IFNULL(LabelNode var) { - return new JumpInsnNode(IFNULL, var); + private static JumpInsnNode IFNULL(LabelNode v) { + return new JumpInsnNode(IFNULL, v); } private static TypeInsnNode CHECKCAST(String desc) { return new TypeInsnNode(CHECKCAST, desc); } - private static JumpInsnNode GOTO(LabelNode var) { - return new JumpInsnNode(GOTO, var); + private static JumpInsnNode GOTO(LabelNode v) { + return new JumpInsnNode(GOTO, v); } } diff --git a/src/main/java/pm/c7/scout/mixin/client/HandledScreenMixin.java b/src/main/java/pm/c7/scout/mixin/client/HandledScreenMixin.java index b80d97d..9a02f3a 100644 --- a/src/main/java/pm/c7/scout/mixin/client/HandledScreenMixin.java +++ b/src/main/java/pm/c7/scout/mixin/client/HandledScreenMixin.java @@ -3,7 +3,6 @@ package pm.c7.scout.mixin.client; import com.mojang.blaze3d.systems.RenderSystem; import net.fabricmc.api.EnvType; import net.fabricmc.api.Environment; -import net.minecraft.client.MinecraftClient; import net.minecraft.client.gui.GuiGraphics; import net.minecraft.client.gui.screen.Screen; import net.minecraft.client.gui.screen.ingame.GenericContainerScreen; @@ -46,7 +45,7 @@ public abstract class HandledScreenMixin extends Screen @Shadow protected int backgroundHeight; - @Inject(method = "render", at = @At(value = "INVOKE", target = "Lnet/minecraft/client/gui/screen/ingame/HandledScreen;drawBackground(Lnet/minecraft/client/util/math/MatrixStack;FII)V")) + @Inject(method = "render", at = @At(value = "INVOKE", target = "Lnet/minecraft/client/gui/screen/ingame/HandledScreen;drawBackground(Lnet/minecraft/client/gui/GuiGraphics;FII)V")) private void scout$drawSatchelRow(GuiGraphics graphics, int mouseX, int mouseY, float delta, CallbackInfo ci) { if (this.client != null && this.client.player != null && !ScoutUtil.isScreenBlacklisted(this)) { ItemStack backStack = ScoutUtil.findBagItem(this.client.player, BaseBagItem.BagType.SATCHEL, false); @@ -272,7 +271,7 @@ public abstract class HandledScreenMixin extends Screen } } - @Inject(method = "render", at = @At(value = "INVOKE", target = "Lnet/minecraft/client/gui/screen/ingame/HandledScreen;drawForeground(Lnet/minecraft/client/util/math/MatrixStack;II)V")) + @Inject(method = "render", at = @At(value = "INVOKE", target = "Lnet/minecraft/client/gui/screen/ingame/HandledScreen;drawForeground(Lnet/minecraft/client/gui/GuiGraphics;II)V")) public void scout$drawOurSlots(GuiGraphics graphics, int mouseX, int mouseY, float delta, CallbackInfo ci) { if (this.client != null && this.client.player != null && !ScoutUtil.isScreenBlacklisted(this)) { for (int i = ScoutUtil.SATCHEL_SLOT_START; i > ScoutUtil.BAG_SLOTS_END; i--) { diff --git a/src/main/java/pm/c7/scout/mixin/client/HandledScreenTransformer.java b/src/main/java/pm/c7/scout/mixin/client/HandledScreenTransformer.java index 67b1a12..37c203f 100644 --- a/src/main/java/pm/c7/scout/mixin/client/HandledScreenTransformer.java +++ b/src/main/java/pm/c7/scout/mixin/client/HandledScreenTransformer.java @@ -2,25 +2,21 @@ package pm.c7.scout.mixin.client; import org.objectweb.asm.tree.*; import org.quiltmc.loader.api.QuiltLoader; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; + import pm.c7.scout.mixinsupport.ClassNodeTransformer; import static org.objectweb.asm.Opcodes.*; public class HandledScreenTransformer implements ClassNodeTransformer { - private static Logger LOGGER = LoggerFactory.getLogger("Scout:HandledScreenTransformer"); @Override public void transform(String name, ClassNode node) { - var drawSlot = "m_zioswvnu"; - var Slot = "net/minecraft/unmapped/C_nhvqfffd"; - var y = "f_tttqoodj"; + var resolver = QuiltLoader.getMappingResolver(); + var namespace = "intermediary"; - if (QuiltLoader.isDevelopmentEnvironment()) { - drawSlot = "drawSlot"; - Slot = "net/minecraft/screen/slot/Slot"; - y = "y"; - } + var drawSlot = resolver.mapMethodName(namespace, name, "method_2385", "(Lnet/minecraft/class_332;Lnet/minecraft/class_1735;)V"); + var Slot = resolver.mapClassName(namespace, "net.minecraft.class_1735"); + var y = resolver.mapFieldName(namespace, Slot, "field_7872", "I"); + Slot = slash(Slot); for (var mn : node.methods) { if (mn.name.equals(drawSlot)) { @@ -32,19 +28,19 @@ public class HandledScreenTransformer implements ClassNodeTransformer { LabelNode LnotBag = new LabelNode(); int SAFE_REGISTER = 20; mn.instructions.insert(vin, insns( - ALOAD(2), - INSTANCEOF("pm/c7/scout/screen/BagSlot"), - IFEQ(LnotBag), - ALOAD(2), - CHECKCAST("pm/c7/scout/screen/BagSlot"), - ASTORE(SAFE_REGISTER), - ALOAD(SAFE_REGISTER), - INVOKEVIRTUAL("pm/c7/scout/screen/BagSlot", "getX", "()I"), - ISTORE(vin.var - 1), - ALOAD(SAFE_REGISTER), - INVOKEVIRTUAL("pm/c7/scout/screen/BagSlot", "getY", "()I"), - ISTORE(vin.var), - LnotBag + ALOAD(2), + INSTANCEOF("pm/c7/scout/screen/BagSlot"), + IFEQ(LnotBag), + ALOAD(2), + CHECKCAST("pm/c7/scout/screen/BagSlot"), + ASTORE(SAFE_REGISTER), + ALOAD(SAFE_REGISTER), + INVOKEVIRTUAL("pm/c7/scout/screen/BagSlot", "getX", "()I"), + ISTORE(vin.var - 1), + ALOAD(SAFE_REGISTER), + INVOKEVIRTUAL("pm/c7/scout/screen/BagSlot", "getY", "()I"), + ISTORE(vin.var), + LnotBag )); } } @@ -55,19 +51,23 @@ public class HandledScreenTransformer implements ClassNodeTransformer { } } + private String slash(String clazz) { + return clazz.replaceAll("\\.", "/"); + } + private InsnList insns(AbstractInsnNode... insns) { var li = new InsnList(); for (var i : insns) li.add(i); return li; } - private static JumpInsnNode IFEQ(LabelNode var) { - return new JumpInsnNode(IFEQ, var); + private static JumpInsnNode IFEQ(LabelNode v) { + return new JumpInsnNode(IFEQ, v); } - private static VarInsnNode ALOAD(int var) { - return new VarInsnNode(ALOAD, var); + private static VarInsnNode ALOAD(int v) { + return new VarInsnNode(ALOAD, v); } - private static VarInsnNode ASTORE(int var) { - return new VarInsnNode(ASTORE, var); + private static VarInsnNode ASTORE(int v) { + return new VarInsnNode(ASTORE, v); } private static TypeInsnNode INSTANCEOF(String desc) { return new TypeInsnNode(INSTANCEOF, desc); @@ -78,7 +78,7 @@ public class HandledScreenTransformer implements ClassNodeTransformer { private static MethodInsnNode INVOKEVIRTUAL(String owner, String name, String desc) { return new MethodInsnNode(INVOKEVIRTUAL, owner, name, desc); } - private static VarInsnNode ISTORE(int var) { - return new VarInsnNode(ISTORE, var); + private static VarInsnNode ISTORE(int v) { + return new VarInsnNode(ISTORE, v); } }