tterrag1098

Untitled

Nov 5th, 2013
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.53 KB | None | 0 0
  1. package niel.mod.core;
  2.  
  3. import java.util.Iterator;
  4.  
  5. import net.minecraft.launchwrapper.IClassTransformer;
  6.  
  7. import org.objectweb.asm.ClassReader;
  8. import org.objectweb.asm.ClassWriter;
  9. import org.objectweb.asm.Label;
  10. import org.objectweb.asm.Opcodes;
  11. import org.objectweb.asm.tree.AbstractInsnNode;
  12. import org.objectweb.asm.tree.ClassNode;
  13. import org.objectweb.asm.tree.InsnList;
  14. import org.objectweb.asm.tree.InsnNode;
  15. import org.objectweb.asm.tree.JumpInsnNode;
  16. import org.objectweb.asm.tree.LabelNode;
  17. import org.objectweb.asm.tree.MethodInsnNode;
  18. import org.objectweb.asm.tree.MethodNode;
  19. import org.objectweb.asm.tree.VarInsnNode;
  20.  
  21. /**
  22.  * @author fishtaco, tterrag
  23.  *
  24.  */
  25. public class CoreTransformer implements IClassTransformer{
  26.  
  27.         private final String CLASS_NAME = "net.minecraft.block.BlockFence";
  28.         private final String OBF_CLASS_NAME = "aoh";
  29.         private final String METHOD_NAME = "canConnectFenceTo";
  30.         private final String METHOD_NAME_OBF = "func_72250_d";
  31.         private final String METHOD_NOTCH = "a";
  32.         private final String METHOD_DESC = "(Lnet/minecraft/world/IBlockAccess;III)Z";
  33.         private final String OBF_RENSP_DESC = "(Lbeu;F)V";
  34.         private final String NEW_MOD_DESC = "(LIBlockAccess;III)Z";
  35.        
  36.         @Override
  37.         public byte[] transform(String arg0, String arg1, byte[] arg2) {
  38.                 if (arg1.compareTo(CLASS_NAME) == 0)
  39.                         return patchClassASM(arg1, arg2, true);
  40.                 return arg2;
  41.         }
  42.        
  43.         private byte[] patchClassASM(String name, byte[] bytes, boolean isObfuscated)
  44.         {              
  45.             System.out.println("Coremodding Shiz yo homeslice McRibs");
  46.                 ClassNode classNode = new ClassNode();
  47.         ClassReader classReader = new ClassReader(bytes);
  48.         classReader.accept(classNode, 0);
  49.        
  50.         Iterator<MethodNode> methods = classNode.methods.iterator();
  51.         while (methods.hasNext())
  52.         {
  53.             MethodNode m = methods.next();
  54.             if((m.name.equals(this.METHOD_NAME) || m.name.equals(this.METHOD_NAME_OBF) || m.name.equals(this.METHOD_NOTCH))
  55.                         && (m.desc.equals(this.METHOD_DESC) || m.desc.equals(OBF_RENSP_DESC)))
  56.             {
  57.                 System.out.println("BEGINNING TRANSFORMATION!");
  58.                 for (int index = 0; index < m.instructions.size(); index++)
  59.                 {
  60.                         if(m.instructions.get(index).getType() == AbstractInsnNode.METHOD_INSN)
  61.                         {
  62.                                 /*MethodInsnNode mdNode = (MethodInsnNode) m.instructions.get(index);
  63.                                 if(mdNode.name.equals(this.INSTRUCTION_LINE) || mdNode.name.equals(this.INSTRUCTION_LINE_OBF) || mdNode.name.equals(this.INSTRUCTION_LINE_NOTCH))
  64.                                 {               */                    
  65.                                         LabelNode lmm1Node = new LabelNode(new Label());
  66.                            
  67.                                         LabelNode jumpLabel = new LabelNode(new Label());
  68.                                        
  69.                             // make new instruction list
  70.                             InsnList toInject = new InsnList();
  71.                            
  72.                             toInject.add(new VarInsnNode(Opcodes.ALOAD, 1));
  73.                             toInject.add(new VarInsnNode(Opcodes.ILOAD, 2));
  74.                             toInject.add(new VarInsnNode(Opcodes.ILOAD, 3));
  75.                             toInject.add(new VarInsnNode(Opcodes.ILOAD, 4));
  76.                            
  77.                             toInject.add(new MethodInsnNode(Opcodes.INVOKESTATIC, "CoreMethods", "canConnectFenceTo", this.NEW_MOD_DESC));
  78.                            
  79.                             toInject.add(new JumpInsnNode(Opcodes.IFNE, lmm1Node));
  80.                             toInject.add(jumpLabel);
  81.                             toInject.add(new InsnNode(Opcodes.RETURN));
  82.                             toInject.add(lmm1Node);
  83.                            
  84.                             m.instructions.insert(m.instructions.get(index), toInject);
  85.                             System.out.println("TRANSFORMATION COMPLETE!");
  86.                             break;
  87.                                 }
  88.                         }
  89.                 }
  90.         }
  91.        
  92.        
  93.                 ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS);            
  94.         classNode.accept(cw);
  95.        
  96.         return cw.toByteArray();
  97.         }
  98. }
Advertisement
Add Comment
Please, Sign In to add comment