feat: both directions on packet patch plus event

This commit is contained in:
əlemi 2023-02-08 02:02:33 +01:00
parent 382e57a2cf
commit 7ff227622d
No known key found for this signature in database
GPG key ID: BBCBFE5D7244634E
4 changed files with 85 additions and 15 deletions

View file

@ -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

View file

@ -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;
}
}

View file

@ -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);
}
}
}

View file

@ -1 +1,2 @@
ftbsc.bscv.patches.PacketPatch$IncomingPacketInterceptor
ftbsc.bscv.patches.PacketPatch$OutgoingPacketInterceptor