From b1643d7026277c3777d32c1c7b3732b2e7095966 Mon Sep 17 00:00:00 2001 From: MineGame159 Date: Wed, 16 Aug 2023 00:18:52 +0200 Subject: [PATCH] Improve Future compatibility --- .../meteorclient/asm/Asm.java | 2 + .../meteorclient/asm/AsmTransformer.java | 5 ++ .../transformers/GameRendererTransformer.java | 5 +- .../PacketInflaterTransformer.java | 62 +++++++++++++++++++ .../mixin/PacketInflaterMixin.java | 21 ------- src/main/resources/meteor-client.mixins.json | 1 - 6 files changed, 72 insertions(+), 24 deletions(-) create mode 100644 src/main/java/meteordevelopment/meteorclient/asm/transformers/PacketInflaterTransformer.java delete mode 100644 src/main/java/meteordevelopment/meteorclient/mixin/PacketInflaterMixin.java diff --git a/src/main/java/meteordevelopment/meteorclient/asm/Asm.java b/src/main/java/meteordevelopment/meteorclient/asm/Asm.java index ee8b8a4376..78c7ac0ca8 100644 --- a/src/main/java/meteordevelopment/meteorclient/asm/Asm.java +++ b/src/main/java/meteordevelopment/meteorclient/asm/Asm.java @@ -7,6 +7,7 @@ import meteordevelopment.meteorclient.asm.transformers.CanvasWorldRendererTransformer; import meteordevelopment.meteorclient.asm.transformers.GameRendererTransformer; +import meteordevelopment.meteorclient.asm.transformers.PacketInflaterTransformer; import net.fabricmc.loader.api.FabricLoader; import org.objectweb.asm.ClassReader; import org.objectweb.asm.ClassWriter; @@ -41,6 +42,7 @@ public static void init() { INSTANCE = new Asm(System.getProperty("meteor.asm.export") != null); INSTANCE.add(new GameRendererTransformer()); INSTANCE.add(new CanvasWorldRendererTransformer()); + INSTANCE.add(new PacketInflaterTransformer()); } private void add(AsmTransformer transformer) { diff --git a/src/main/java/meteordevelopment/meteorclient/asm/AsmTransformer.java b/src/main/java/meteordevelopment/meteorclient/asm/AsmTransformer.java index e0020c2bf9..573da6f774 100644 --- a/src/main/java/meteordevelopment/meteorclient/asm/AsmTransformer.java +++ b/src/main/java/meteordevelopment/meteorclient/asm/AsmTransformer.java @@ -26,6 +26,11 @@ protected MethodNode getMethod(ClassNode klass, MethodInfo methodInfo) { return null; } + protected static void error(String message) { + System.err.println(message); + throw new RuntimeException(message); + } + protected static String mapClassName(String name) { return FabricLoader.getInstance().getMappingResolver().mapClassName("intermediary", name.replace('/', '.')); } diff --git a/src/main/java/meteordevelopment/meteorclient/asm/transformers/GameRendererTransformer.java b/src/main/java/meteordevelopment/meteorclient/asm/transformers/GameRendererTransformer.java index 459d063cfe..c8c5ea1fb2 100644 --- a/src/main/java/meteordevelopment/meteorclient/asm/transformers/GameRendererTransformer.java +++ b/src/main/java/meteordevelopment/meteorclient/asm/transformers/GameRendererTransformer.java @@ -24,10 +24,11 @@ public GameRendererTransformer() { public void transform(ClassNode klass) { // Modify GameRenderer.getFov() MethodNode method = getMethod(klass, getFovMethod); - if (method == null) throw new RuntimeException("[Meteor Client] Could not find method GameRenderer.getFov()"); + if (method == null) error("[Meteor Client] Could not find method GameRenderer.getFov()"); int injectionCount = 0; + //noinspection DataFlowIssue for (AbstractInsnNode insn : method.instructions) { if (insn instanceof LdcInsnNode in && in.cst instanceof Double && (double) in.cst == 90) { InsnList insns = new InsnList(); @@ -52,7 +53,7 @@ else if ( } } - if (injectionCount < 2) throw new RuntimeException("[Meteor Client] Failed to modify GameRenderer.getFov()"); + if (injectionCount < 2) error("[Meteor Client] Failed to modify GameRenderer.getFov()"); } private void generateEventCall(InsnList insns, AbstractInsnNode loadPreviousFov) { diff --git a/src/main/java/meteordevelopment/meteorclient/asm/transformers/PacketInflaterTransformer.java b/src/main/java/meteordevelopment/meteorclient/asm/transformers/PacketInflaterTransformer.java new file mode 100644 index 0000000000..0e30913175 --- /dev/null +++ b/src/main/java/meteordevelopment/meteorclient/asm/transformers/PacketInflaterTransformer.java @@ -0,0 +1,62 @@ +/* + * This file is part of the Meteor Client distribution (https://github.com/MeteorDevelopment/meteor-client). + * Copyright (c) Meteor Development. + */ + +package meteordevelopment.meteorclient.asm.transformers; + +import meteordevelopment.meteorclient.asm.AsmTransformer; +import meteordevelopment.meteorclient.asm.Descriptor; +import meteordevelopment.meteorclient.asm.MethodInfo; +import meteordevelopment.meteorclient.systems.modules.misc.AntiPacketKick; +import org.objectweb.asm.Label; +import org.objectweb.asm.Opcodes; +import org.objectweb.asm.Type; +import org.objectweb.asm.tree.*; + +// Future compatibility +// Future uses @ModifyConstant which does not chain when multiple mods do it and mixins / mixinextra can't target throw +// statements. So using a custom ASM transformer we wrap the throw statement inside another if statement. +public class PacketInflaterTransformer extends AsmTransformer { + private final MethodInfo decodeMethod; + + public PacketInflaterTransformer() { + super(mapClassName("net/minecraft/class_2532")); + + decodeMethod = new MethodInfo("net/minecraft/class_2532", "decode", new Descriptor("Lio/netty/channel/ChannelHandlerContext;", "Lio/netty/buffer/ByteBuf;", "Ljava/util/List;", "V"), true); + } + + @Override + public void transform(ClassNode klass) { + MethodNode method = getMethod(klass, decodeMethod); + if (method == null) error("[Meteor Client] Could not find method PacketInflater.decode()"); + + int newCount = 0; + LabelNode label = new LabelNode(new Label()); + + //noinspection DataFlowIssue + for (AbstractInsnNode insn : method.instructions) { + if (insn instanceof TypeInsnNode typeInsn && typeInsn.getOpcode() == Opcodes.NEW && typeInsn.desc.equals("io/netty/handler/codec/DecoderException")) { + newCount++; + + if (newCount == 2) { + InsnList list = new InsnList(); + + list.add(new MethodInsnNode(Opcodes.INVOKESTATIC, "meteordevelopment/meteorclient/systems/modules/Modules", "get", "()Lmeteordevelopment/meteorclient/systems/modules/Modules;", false)); + list.add(new LdcInsnNode(Type.getType(AntiPacketKick.class))); + list.add(new MethodInsnNode(Opcodes.INVOKEVIRTUAL, "meteordevelopment/meteorclient/systems/modules/Modules", "isActive", "(Ljava/lang/Class;)Z", false)); + + list.add(new JumpInsnNode(Opcodes.IFNE, label)); + + method.instructions.insertBefore(insn, list); + } + } + else if (newCount == 2 && insn.getOpcode() == Opcodes.ATHROW) { + method.instructions.insert(insn, label); + return; + } + } + + error("[Meteor Client] Failed to modify PacketInflater.decode()"); + } +} diff --git a/src/main/java/meteordevelopment/meteorclient/mixin/PacketInflaterMixin.java b/src/main/java/meteordevelopment/meteorclient/mixin/PacketInflaterMixin.java deleted file mode 100644 index 9d2c502d92..0000000000 --- a/src/main/java/meteordevelopment/meteorclient/mixin/PacketInflaterMixin.java +++ /dev/null @@ -1,21 +0,0 @@ -/* - * This file is part of the Meteor Client distribution (https://github.com/MeteorDevelopment/meteor-client). - * Copyright (c) Meteor Development. - */ - -package meteordevelopment.meteorclient.mixin; - -import meteordevelopment.meteorclient.systems.modules.Modules; -import meteordevelopment.meteorclient.systems.modules.misc.AntiPacketKick; -import net.minecraft.network.PacketInflater; -import org.spongepowered.asm.mixin.Mixin; -import org.spongepowered.asm.mixin.injection.Constant; -import org.spongepowered.asm.mixin.injection.ModifyConstant; - -@Mixin(PacketInflater.class) -public class PacketInflaterMixin { - @ModifyConstant(method = "decode", constant = @Constant(intValue = 8388608), require = 0) - private int maxValue(int value) { - return Modules.get().isActive(AntiPacketKick.class) ? Integer.MAX_VALUE : value; - } -} diff --git a/src/main/resources/meteor-client.mixins.json b/src/main/resources/meteor-client.mixins.json index 711924c05c..b0ad1f5301 100644 --- a/src/main/resources/meteor-client.mixins.json +++ b/src/main/resources/meteor-client.mixins.json @@ -118,7 +118,6 @@ "MouseMixin", "MultiplayerScreenMixin", "PacketByteBufMixin", - "PacketInflaterMixin", "ParticleManagerMixin", "PlayerArmorSlotMixin", "PlayerEntityMixin",