summaryrefslogtreecommitdiff
path: root/src/main/java/ftbsc/bscv/patches/PacketPatch.java
blob: 610638ec57af3cf99a37ee5ba6f2b659a3231e29 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
package ftbsc.bscv.patches;

import ftbsc.lll.processor.annotations.Injector;
import ftbsc.lll.processor.annotations.Patch;
import ftbsc.lll.processor.annotations.Target;
import io.netty.channel.ChannelHandlerContext;
import io.netty.util.concurrent.GenericFutureListener;
import net.minecraft.network.IPacket;
import net.minecraft.network.NetworkManager;
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.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.events.PacketEvent;
import ftbsc.lll.IInjector;
import ftbsc.lll.tools.DescriptorBuilder;
import ftbsc.lll.tools.InsnSequence;
import ftbsc.lll.tools.PatternMatcher;

public class PacketPatch {

   public static class PacketHook {
      public static boolean pktIn(IPacket<?> pkt) {
         return MinecraftForge.EVENT_BUS.post(new PacketEvent(pkt, false));
      }

      public static boolean pktOut(IPacket<?> pkt) {
         return MinecraftForge.EVENT_BUS.post(new PacketEvent(pkt, true));
      }
   }

   @Patch(value = NetworkManager.class, reason = "add hook to intercept and alter/cancel incoming packets")
   public abstract static class IncomingPacketInterceptor implements Opcodes {
      @Target
      @SuppressWarnings("rawtypes")
      public abstract void channelRead0(ChannelHandlerContext ctx, IPacket pak);

      @Injector
      public static void inject(ClassNode clazz, MethodNode main) {
         // ALOAD, GETFIELD, INVOKEINTERFACE, IFEQ
         AbstractInsnNode found = PatternMatcher.builder()
            .opcodes(ALOAD, GETFIELD, INVOKEINTERFACE, IFEQ)
            .ignoreFrames()
            .ignoreLabels()
            .ignoreLineNumbers()
            .build()
            .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$PacketHook",
            "pktIn",
            "(Lnet/minecraft/network/IPacket;)Z"
         ));
         is.add(new JumpInsnNode(IFEQ, skip));
         is.add(new InsnNode(RETURN));
         is.add(skip);

         main.instructions.insert(found, is);
      }
   }

   @Patch(value = NetworkManager.class, reason = "add hook to intercept and alter/cancel outgoing packets")
   public abstract static class OutgoingPacketInterceptor implements Opcodes {
      @Target
      @SuppressWarnings("rawtypes")
      public abstract void sendPacket(IPacket pak, GenericFutureListener gfl);

      @Injector
      public static 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;)Z"
         ));
         is.add(new JumpInsnNode(IFEQ, skip));
         is.add(new InsnNode(RETURN));
         is.add(skip);

         main.instructions.insert(is);
      }

   }
}