Skip to content

Commit

Permalink
Improve Future compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
MineGame159 committed Aug 15, 2023
1 parent 8cabb2a commit b1643d7
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 24 deletions.
2 changes: 2 additions & 0 deletions src/main/java/meteordevelopment/meteorclient/asm/Asm.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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('/', '.'));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
@@ -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()");
}
}

This file was deleted.

1 change: 0 additions & 1 deletion src/main/resources/meteor-client.mixins.json
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,6 @@
"MouseMixin",
"MultiplayerScreenMixin",
"PacketByteBufMixin",
"PacketInflaterMixin",
"ParticleManagerMixin",
"PlayerArmorSlotMixin",
"PlayerEntityMixin",
Expand Down

0 comments on commit b1643d7

Please sign in to comment.