Last active
August 29, 2015 14:10
-
-
Save killjoy1221/a760684d3679f166b932 to your computer and use it in GitHub Desktop.
Packet IO Transformer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package mnm.mods.protocol4.asm; | |
import com.mumfrey.liteloader.core.runtime.Obf; | |
import com.mumfrey.liteloader.transformers.ClassTransformer; | |
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.MethodInsnNode; | |
import org.objectweb.asm.tree.MethodNode; | |
import org.objectweb.asm.tree.VarInsnNode; | |
import java.util.ListIterator; | |
public abstract class PacketIOTransformer extends ClassTransformer implements Opcodes { | |
private static final String READ_PACKET = "readPacketData"; | |
private static final String WRITE_PACKET = "writePackeDatat"; | |
protected Obf className; | |
private String packetIO;; | |
protected PacketIOTransformer(Obf className, String packetIOClass) { | |
this.className = className; | |
this.packetIO = packetIOClass; | |
} | |
@Override | |
public final byte[] transform(String name, String transformedName, byte[] basicClass) { | |
if (className.name.equals(name) && basicClass != null) { | |
ClassNode classNode = readClass(basicClass, false); | |
for (MethodNode mn : classNode.methods) { | |
if (mn.name.equals("readPacketData")) { | |
transformReadPacket(mn); | |
} | |
if (mn.name.equals("writePacketData")) { | |
transformWritePacket(mn); | |
} | |
} | |
basicClass = writeClass(classNode); | |
} | |
return basicClass; | |
} | |
protected void transformReadPacket(MethodNode mn) { | |
InsnList insn = mn.instructions; | |
InsnList list = new InsnList(); | |
{ | |
list.add(new VarInsnNode(ALOAD, 0)); | |
list.add(new MethodInsnNode(INVOKESTATIC, packetIO, READ_PACKET, mn.desc)); | |
list.add(new VarInsnNode(ASTORE, 0)); | |
} | |
// Insert at beginning | |
insn.insert(list); | |
} | |
protected void transformWritePacket(MethodNode mn) { | |
InsnList insn = mn.instructions; | |
InsnList list = new InsnList(); | |
{ | |
list.add(new VarInsnNode(ALOAD, 0)); | |
list.add(new MethodInsnNode(INVOKESTATIC, packetIO, WRITE_PACKET, mn.desc)); | |
list.add(new VarInsnNode(ASTORE, 0)); | |
} | |
// Insert before return | |
ListIterator<AbstractInsnNode> iter = insn.iterator(); | |
while (iter.hasNext()) { | |
AbstractInsnNode node = iter.next(); | |
if (node.getOpcode() == RETURN) { | |
insn.insertBefore(node, list); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment