From ca832aeac96c1c6ffc4ed15c3bf700a800cd87e4 Mon Sep 17 00:00:00 2001 From: jchung01 Date: Thu, 26 Sep 2024 20:48:59 -0700 Subject: [PATCH] Make AR transformer compatible with fork version --- .../fugue/config/ModPatchConfig.java | 4 ++ .../ClassTransformerTransformer.java | 41 ++++++++++++++++--- 2 files changed, 40 insertions(+), 5 deletions(-) diff --git a/src/main/java/com/cleanroommc/fugue/config/ModPatchConfig.java b/src/main/java/com/cleanroommc/fugue/config/ModPatchConfig.java index 3a43f70..4b07d09 100644 --- a/src/main/java/com/cleanroommc/fugue/config/ModPatchConfig.java +++ b/src/main/java/com/cleanroommc/fugue/config/ModPatchConfig.java @@ -5,6 +5,10 @@ public class ModPatchConfig { @Config.Name("Enable Ender Core Patch") public boolean enableEnderCore = true; + @Config.Comment({ + "This patch is only for Advanced Rocketry by zmaster587.", + "Advanced Rocketry - Reworked by MarvinEckhardt doesn't need this!" + }) @Config.Name("Enable Advanced Rocketry Patch") public boolean enableAR = true; @Config.Name("Enable Shoulder Surfing Reloaded Patch") diff --git a/src/main/java/com/cleanroommc/fugue/transformer/ClassTransformerTransformer.java b/src/main/java/com/cleanroommc/fugue/transformer/ClassTransformerTransformer.java index a60efc3..62aff34 100644 --- a/src/main/java/com/cleanroommc/fugue/transformer/ClassTransformerTransformer.java +++ b/src/main/java/com/cleanroommc/fugue/transformer/ClassTransformerTransformer.java @@ -1,5 +1,6 @@ package com.cleanroommc.fugue.transformer; +import com.cleanroommc.fugue.common.Fugue; import org.objectweb.asm.ClassReader; import org.objectweb.asm.ClassWriter; import org.objectweb.asm.Opcodes; @@ -22,16 +23,46 @@ public byte[] transform(byte[] bytes) { InsnList instructions = methodNode.instructions; if (instructions != null) { - for (AbstractInsnNode insnNode : instructions) + int fromIndex = 0; + int toIndex = 0; + for (int i = 0; i < instructions.size(); i++) { - if (insnNode instanceof LineNumberNode lineNumberNode) + AbstractInsnNode insnNode = instructions.get(i); + // Find beginning of EntityPlayer patch block + if (insnNode.getOpcode() == Opcodes.LDC && ((LdcInsnNode) insnNode).cst.equals("net.minecraft.entity.player.EntityPlayer")) { - if (lineNumberNode.line == 693) { - instructions.remove(instructions.get(instructions.indexOf(lineNumberNode) + 1)); - instructions.insert(lineNumberNode, new InsnNode(Opcodes.ICONST_0)); + fromIndex = i; + } + // Then find start of loop + else if (fromIndex > 0 && insnNode instanceof FrameNode) + { + toIndex = i; + break; + } + } + int constCounts = 0; + AbstractInsnNode targetNode = null; + // Now count number of ICONST opcodes + for (; fromIndex < toIndex; fromIndex++) + { + AbstractInsnNode insnNode = instructions.get(fromIndex); + if (insnNode.getOpcode() == Opcodes.ICONST_0 || insnNode.getOpcode() == Opcodes.ICONST_1) + { + if (targetNode == null) + { + targetNode = insnNode; } + constCounts++; } } + Fugue.LOGGER.debug("Advanced Rocketry: Found {} ICONST invocations", constCounts); + // This is regular AR, patch + if (constCounts == 2) + { + instructions.insertBefore(targetNode, new InsnNode(Opcodes.ICONST_0)); + instructions.remove(targetNode); + } + break; } } }