diff options
author | alemidev <me@alemi.dev> | 2023-02-08 02:02:33 +0100 |
---|---|---|
committer | alemidev <me@alemi.dev> | 2023-02-08 02:02:33 +0100 |
commit | 7ff227622d7f5722e5fa8521a1e7018ed6b280c0 (patch) | |
tree | 08b1dbd5a10b83c4f31757c9fb79742bbef076de /src/main/java | |
parent | 382e57a2cfdd615e138ad83bbcf5268ae08a9fa7 (diff) |
feat: both directions on packet patch plus event
Diffstat (limited to 'src/main/java')
-rw-r--r-- | src/main/java/ftbsc/bscv/BoSCoVicino.java | 3 | ||||
-rw-r--r-- | src/main/java/ftbsc/bscv/events/PacketEvent.java | 24 | ||||
-rw-r--r-- | src/main/java/ftbsc/bscv/patches/PacketPatch.java | 72 |
3 files changed, 84 insertions, 15 deletions
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); + } + + } } |