From 7ff227622d7f5722e5fa8521a1e7018ed6b280c0 Mon Sep 17 00:00:00 2001 From: alemidev Date: Wed, 8 Feb 2023 02:02:33 +0100 Subject: [PATCH] feat: both directions on packet patch plus event --- src/main/java/ftbsc/bscv/BoSCoVicino.java | 3 + .../java/ftbsc/bscv/events/PacketEvent.java | 24 +++++++ .../java/ftbsc/bscv/patches/PacketPatch.java | 72 +++++++++++++++---- .../META-INF/services/ftbsc.lll.IInjector | 1 + 4 files changed, 85 insertions(+), 15 deletions(-) create mode 100644 src/main/java/ftbsc/bscv/events/PacketEvent.java diff --git a/src/main/java/ftbsc/bscv/BoSCoVicino.java b/src/main/java/ftbsc/bscv/BoSCoVicino.java index 5ea29b1..8290741 100644 --- a/src/main/java/ftbsc/bscv/BoSCoVicino.java +++ b/src/main/java/ftbsc/bscv/BoSCoVicino.java @@ -29,6 +29,7 @@ import java.util.List; import ftbsc.bscv.module.Module; import ftbsc.bscv.module.vision.*; +import ftbsc.bscv.patches.PacketPatch; import ftbsc.bscv.module.motion.*; import ftbsc.bscv.module.self.*; import ftbsc.bscv.module.hud.*; @@ -61,6 +62,8 @@ public class BoSCoVicino { ForgeConfigSpec.Builder builder = new ForgeConfigSpec.Builder(); + PacketPatch.PacketHook hook = PacketPatch.PacketHook.packetHook(); // make sure Forge doesn't strip this + // TODO also push! // modules cannot easily pop from their builder, but here we can't easily get // the module name yet. We should push and pop the builder ourselves and not diff --git a/src/main/java/ftbsc/bscv/events/PacketEvent.java b/src/main/java/ftbsc/bscv/events/PacketEvent.java new file mode 100644 index 0000000..7c2e431 --- /dev/null +++ b/src/main/java/ftbsc/bscv/events/PacketEvent.java @@ -0,0 +1,24 @@ +package ftbsc.bscv.events; + +import net.minecraft.network.IPacket; +import net.minecraftforge.eventbus.api.Cancelable; +import net.minecraftforge.eventbus.api.Event; + +@Cancelable +public class PacketEvent extends Event { + // TODO redo this as two subclasses rather than a BOOL????? + + public IPacket packet; + public boolean outgoing; + public boolean canceled; + + public PacketEvent(IPacket pkt, boolean outgoing) { + this.packet = pkt; + this.canceled = false; + this.outgoing = outgoing; + } + + public void cancel() { + this.canceled = true; + } +} diff --git a/src/main/java/ftbsc/bscv/patches/PacketPatch.java b/src/main/java/ftbsc/bscv/patches/PacketPatch.java index 6588f0b..786f900 100644 --- a/src/main/java/ftbsc/bscv/patches/PacketPatch.java +++ b/src/main/java/ftbsc/bscv/patches/PacketPatch.java @@ -1,38 +1,42 @@ package ftbsc.bscv.patches; -import org.apache.logging.log4j.LogManager; -import org.apache.logging.log4j.Logger; +import net.minecraft.network.IPacket; +import net.minecraftforge.common.MinecraftForge; + import org.objectweb.asm.Opcodes; import org.objectweb.asm.tree.AbstractInsnNode; import org.objectweb.asm.tree.ClassNode; -import org.objectweb.asm.tree.InsnList; import org.objectweb.asm.tree.InsnNode; +import org.objectweb.asm.tree.JumpInsnNode; +import org.objectweb.asm.tree.LabelNode; import org.objectweb.asm.tree.MethodInsnNode; import org.objectweb.asm.tree.MethodNode; import org.objectweb.asm.tree.VarInsnNode; import ftbsc.bscv.BoSCoVicino; +import ftbsc.bscv.events.PacketEvent; import ftbsc.lll.IInjector; import ftbsc.lll.tools.DescriptorBuilder; import ftbsc.lll.tools.InsnSequence; import ftbsc.lll.tools.PatternMatcher; -import net.minecraft.network.IPacket; public class PacketPatch { - public static class LazyPacketCatcher { - private static LazyPacketCatcher INSTANCE = new LazyPacketCatcher(); + public static class PacketHook { + private static final PacketHook INSTANCE = new PacketHook(); - public static LazyPacketCatcher getInstance() { - return LazyPacketCatcher.INSTANCE; + public static PacketHook packetHook() { + return PacketHook.INSTANCE; } - public void debug_pkt(IPacket pkt) { - BoSCoVicino.LOGGER.info("got pkt : {}", pkt.toString()); + public static void pktIn(IPacket pkt) { + BoSCoVicino.LOGGER.info("<[pkt] {}", pkt); + MinecraftForge.EVENT_BUS.post(new PacketEvent(pkt, false)); // return post() } - public static void debug_static_pkt(IPacket pkt) { - BoSCoVicino.LOGGER.info("got pkt : {}", pkt.toString()); + public static void pktOut(IPacket pkt) { + BoSCoVicino.LOGGER.info("[pkt]> {}", pkt); + MinecraftForge.EVENT_BUS.post(new PacketEvent(pkt, true)); // return post() } } @@ -61,16 +65,54 @@ public class PacketPatch { .find(main) .getLast(); + LabelNode skip = new LabelNode(); InsnSequence is = new InsnSequence(); is.add(new VarInsnNode(ALOAD, 2)); is.add(new MethodInsnNode( INVOKESTATIC, - "ftbsc/bscv/patches/PacketPatch$LazyPacketCatcher", - "debug_static_pkt", - new DescriptorBuilder().addParameter("net.minecraft.network.IPacket").build() + "ftbsc/bscv/patches/PacketPatch$PacketHook", + "pktIn", + "(Lnet/minecraft/network/IPacket;)V" // Z for bool return )); + // is.add(new JumpInsnNode(IFEQ, skip)); + // is.add(new InsnNode(RET)); + // is.add(skip); main.instructions.insert(found, is); } } + + public static class OutgoingPacketInterceptor implements IInjector, Opcodes { + public String name() { return "OutgoingPacketInterceptor"; } + public String reason() { return "add hook to intercept and alter/cancel outgoing packets"; } + public String targetClass() { return "net.minecraft.network.NetworkManager"; } + public String methodName() { return "func_150732_b"; } + public String methodDesc() { + return + new DescriptorBuilder() + .setReturnType(void.class) + .addParameter("net.minecraft.network.IPacket") + .addParameter("io.netty.util.concurrent.GenericFutureListener") + .build(); + } + + public void inject(ClassNode clazz, MethodNode main) { + // hook at the top + LabelNode skip = new LabelNode(); + InsnSequence is = new InsnSequence(); + is.add(new VarInsnNode(ALOAD, 1)); + is.add(new MethodInsnNode( + INVOKESTATIC, + "ftbsc/bscv/patches/PacketPatch$PacketHook", + "pktOut", + "(Lnet/minecraft/network/IPacket;)V" // Z for bool return + )); + // is.add(new JumpInsnNode(IFEQ, skip)); + // is.add(new InsnNode(RET)); + // is.add(skip); + + main.instructions.insert(is); + } + + } } diff --git a/src/main/resources/META-INF/services/ftbsc.lll.IInjector b/src/main/resources/META-INF/services/ftbsc.lll.IInjector index 2aaf90b..bda5738 100644 --- a/src/main/resources/META-INF/services/ftbsc.lll.IInjector +++ b/src/main/resources/META-INF/services/ftbsc.lll.IInjector @@ -1 +1,2 @@ ftbsc.bscv.patches.PacketPatch$IncomingPacketInterceptor +ftbsc.bscv.patches.PacketPatch$OutgoingPacketInterceptor