|
| 1 | +package com.matt.packetlistener.patch; |
| 2 | + |
| 3 | +import com.matt.packetlistener.PacketCoreMod; |
| 4 | +import com.matt.packetlistener.Proxies; |
| 5 | +import com.matt.packetlistener.helper.AsmHelper; |
| 6 | +import com.matt.packetlistener.helper.AsmMethod; |
| 7 | +import com.matt.packetlistener.helper.IAsmTransformer; |
| 8 | +import org.objectweb.asm.tree.*; |
| 9 | + |
| 10 | +import static org.objectweb.asm.Opcodes.*; |
| 11 | + |
| 12 | +public class PatchNetManager implements IAsmTransformer { |
| 13 | + private final AsmMethod DISPATCH_PACKET = new AsmMethod() |
| 14 | + .setName("dispatchPacket") |
| 15 | + .setObfuscatedName("a") |
| 16 | + .setArgumentTypes(Proxies.INSTANCE.PACKET, "[Lio/netty/util/concurrent/GenericFutureListener;") |
| 17 | + .setReturnType(void.class); |
| 18 | + |
| 19 | + private final AsmMethod CHANNEL_READ0 = new AsmMethod() |
| 20 | + .setName("channelRead0") |
| 21 | + .setObfuscatedName("a") |
| 22 | + .setArgumentTypes("Lio/netty/channel/ChannelHandlerContext;", Proxies.INSTANCE.PACKET) |
| 23 | + .setReturnType(void.class); |
| 24 | + |
| 25 | + public void transform(ClassNode node) { |
| 26 | + for(MethodNode method : node.methods) { |
| 27 | + if(method.name.equals(DISPATCH_PACKET.getRuntimeName()) && |
| 28 | + method.desc.equals(DISPATCH_PACKET.getDescriptor())) { |
| 29 | + PacketCoreMod.logger.info("Patching method dispatchPacket"); |
| 30 | + patchDispatchPacket(method); |
| 31 | + } else if(method.name.equals(CHANNEL_READ0.getRuntimeName()) && |
| 32 | + method.desc.equals(CHANNEL_READ0.getDescriptor())) { |
| 33 | + PacketCoreMod.logger.info("Patching method channelRead0"); |
| 34 | + patchChannelRead0(method); |
| 35 | + } |
| 36 | + } |
| 37 | + } |
| 38 | + |
| 39 | + private final int[] patternPreDispatch = new int[] { |
| 40 | + ALOAD, ALOAD, IF_ACMPEQ, ALOAD, INSTANCEOF, IFNE, |
| 41 | + 0x00, 0x00, |
| 42 | + ALOAD, ALOAD, INVOKEVIRTUAL |
| 43 | + }; |
| 44 | + |
| 45 | + private final int[] patternPostDispatch = new int[] { |
| 46 | + POP, |
| 47 | + 0x00, 0x00, |
| 48 | + GOTO, |
| 49 | + 0x00, 0x00, 0x00, |
| 50 | + ALOAD, GETFIELD, INVOKEINTERFACE, NEW, DUP |
| 51 | + }; |
| 52 | + |
| 53 | + private void patchDispatchPacket(MethodNode method) { |
| 54 | + AbstractInsnNode preNode = AsmHelper.findPattern(method.instructions.getFirst(), |
| 55 | + patternPreDispatch, "xxxxxx??xxx"); |
| 56 | + AbstractInsnNode postNode = AsmHelper.findPattern(method.instructions.getFirst(), |
| 57 | + patternPostDispatch, "x??x???xxxxx"); |
| 58 | + |
| 59 | + if(preNode != null && postNode != null) { |
| 60 | + LabelNode endJump = new LabelNode(); |
| 61 | + |
| 62 | + InsnList insnPre = new InsnList(); |
| 63 | + insnPre.add(new VarInsnNode(ALOAD, 1)); |
| 64 | + insnPre.add(new MethodInsnNode(INVOKESTATIC, |
| 65 | + Proxies.INSTANCE.ON_SENDING_PACKET.getParentClassName(), |
| 66 | + Proxies.INSTANCE.ON_SENDING_PACKET.getRuntimeName(), |
| 67 | + Proxies.INSTANCE.ON_SENDING_PACKET.getDescriptor(), |
| 68 | + false |
| 69 | + )); |
| 70 | + insnPre.add(new JumpInsnNode(IFNE, endJump)); |
| 71 | + |
| 72 | + InsnList insnPost = new InsnList(); |
| 73 | + insnPost.add(new VarInsnNode(ALOAD, 1)); |
| 74 | + insnPost.add(new MethodInsnNode(INVOKESTATIC, |
| 75 | + Proxies.INSTANCE.ON_SENT_PACKET.getParentClassName(), |
| 76 | + Proxies.INSTANCE.ON_SENT_PACKET.getRuntimeName(), |
| 77 | + Proxies.INSTANCE.ON_SENT_PACKET.getDescriptor(), |
| 78 | + false |
| 79 | + )); |
| 80 | + insnPost.add(endJump); |
| 81 | + |
| 82 | + method.instructions.insertBefore(preNode, insnPre); |
| 83 | + method.instructions.insert(postNode, insnPost); |
| 84 | + } else { |
| 85 | + PacketCoreMod.logger.error("Failed to find nodes for dispatch packet"); |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + private final int[] patternPreSend = new int[] { |
| 90 | + ALOAD, ALOAD, GETFIELD, INVOKEINTERFACE |
| 91 | + }; |
| 92 | + |
| 93 | + private final int[] patternPostSend = new int[] { |
| 94 | + INVOKEINTERFACE, |
| 95 | + 0x00, 0x00, |
| 96 | + GOTO, |
| 97 | + }; |
| 98 | + |
| 99 | + private void patchChannelRead0(MethodNode method) { |
| 100 | + AbstractInsnNode preNode = AsmHelper.findPattern(method.instructions.getFirst(), |
| 101 | + patternPreSend, "xxxx"); |
| 102 | + AbstractInsnNode postNode = AsmHelper.findPattern(method.instructions.getFirst(), |
| 103 | + patternPostSend, "x??x"); |
| 104 | + if(preNode != null && postNode != null) { |
| 105 | + LabelNode endJump = new LabelNode(); |
| 106 | + |
| 107 | + InsnList insnPre = new InsnList(); |
| 108 | + insnPre.add(new VarInsnNode(ALOAD, 2)); |
| 109 | + insnPre.add(new MethodInsnNode(INVOKESTATIC, |
| 110 | + Proxies.INSTANCE.ON_PRE_RECEIVED.getParentClassName(), |
| 111 | + Proxies.INSTANCE.ON_PRE_RECEIVED.getRuntimeName(), |
| 112 | + Proxies.INSTANCE.ON_PRE_RECEIVED.getDescriptor(), |
| 113 | + false |
| 114 | + )); |
| 115 | + insnPre.add(new JumpInsnNode(IFNE, endJump)); |
| 116 | + |
| 117 | + InsnList insnPost = new InsnList(); |
| 118 | + insnPost.add(new VarInsnNode(ALOAD, 2)); |
| 119 | + insnPost.add(new MethodInsnNode(INVOKESTATIC, |
| 120 | + Proxies.INSTANCE.ON_POST_RECEIVED.getParentClassName(), |
| 121 | + Proxies.INSTANCE.ON_POST_RECEIVED.getRuntimeName(), |
| 122 | + Proxies.INSTANCE.ON_POST_RECEIVED.getDescriptor(), |
| 123 | + false |
| 124 | + )); |
| 125 | + insnPost.add(endJump); |
| 126 | + |
| 127 | + method.instructions.insertBefore(preNode, insnPre); |
| 128 | + method.instructions.insert(postNode, insnPost); |
| 129 | + } |
| 130 | + } |
| 131 | +} |
0 commit comments