mrkirby153

Untitled

Sep 9th, 2015
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.43 KB | None | 0 0
  1. package me.mrkirby153.AntiChatSpam.coremod;
  2.  
  3. import me.mrkirby153.AntiChatSpam.regex.ChatHandler;
  4. import net.minecraft.launchwrapper.IClassTransformer;
  5. import net.minecraft.util.IChatComponent;
  6. import org.apache.logging.log4j.Level;
  7. import org.apache.logging.log4j.LogManager;
  8. import org.apache.logging.log4j.Logger;
  9. import org.objectweb.asm.ClassReader;
  10. import org.objectweb.asm.ClassWriter;
  11. import org.objectweb.asm.Type;
  12. import org.objectweb.asm.tree.*;
  13.  
  14. import java.util.Arrays;
  15.  
  16. import static org.objectweb.asm.Opcodes.*;
  17.  
  18. public class ACSClassTransformer implements IClassTransformer{
  19.  
  20.     private Logger logger;
  21.  
  22.     public ACSClassTransformer(){
  23.         this.logger = LogManager.getLogger("ACS-Coremod");
  24.     }
  25.  
  26.     private static final String[] classToTransform = {"net.minecraft.client.entity.EntityPlayerSP"};
  27.  
  28.     @Override
  29.     public byte[] transform(String name, String deobfName, byte[] classBeingTransformed) {
  30.         boolean isObfuscated = !name.equalsIgnoreCase(deobfName);
  31.         int index = Arrays.asList(classToTransform).indexOf(deobfName);
  32.         return index != -1 ? transform(index, classBeingTransformed, isObfuscated) : classBeingTransformed;
  33.     }
  34.  
  35.     private byte[] transform(int index, byte[] classBeingTransformed, boolean obfuscated){
  36.         logger.info("Transforming "+classToTransform[index]);
  37.         try{
  38.             ClassNode classNode = new ClassNode();
  39.             ClassReader reader = new ClassReader(classBeingTransformed);
  40.             reader.accept(classNode, 0);
  41.             handleTransformation(index, classNode, obfuscated);
  42.             ClassWriter classWriter = new ClassWriter(ClassWriter.COMPUTE_FRAMES | ClassWriter.COMPUTE_MAXS);
  43.             classNode.accept(classWriter);
  44.             return classWriter.toByteArray();
  45.         } catch (Exception e){
  46.             logger.catching(Level.FATAL, e);
  47.         }
  48.         logger.warn("Something went wrong! Returning untransformed class");
  49.         return classBeingTransformed;
  50.     }
  51.  
  52.     private void handleTransformation(int index, ClassNode classNode, boolean obfuscated){
  53.         switch (index){
  54.             case 0:
  55.                 transformEntityPlayerSP(classNode, obfuscated);
  56.         }
  57.     }
  58.  
  59.     private void transformEntityPlayerSP(ClassNode entityPlayerSPClass, boolean obfuscated) {
  60.         final String ADD_CHAT_COMPONENT_MESSAGE = (obfuscated) ? "b" : "addChatComponentMessage";
  61.         final String ADD_CHAT_COMPONENT_DESCRIPTOR = (obfuscated) ? "(Lfj;)V" : "(Lnet/minecraft/util/IChatComponent;)V";
  62.         // Find target node
  63.         MethodNode targetMethod = findTargetNode(entityPlayerSPClass, ADD_CHAT_COMPONENT_MESSAGE, ADD_CHAT_COMPONENT_DESCRIPTOR);
  64.         if(targetMethod == null)
  65.             return;
  66.         AbstractInsnNode targetNode = null;
  67.         for(AbstractInsnNode instruction : targetMethod.instructions.toArray()){
  68.             if(instruction.getOpcode() == ALOAD){
  69.                 if(((VarInsnNode) instruction).var == 0 && instruction.getNext().getOpcode() == GETFIELD){
  70.                     targetNode = instruction;
  71.                     break;
  72.                 }
  73.             }
  74.         }
  75.         if(targetNode == null)
  76.             return;
  77.         /*
  78.         Replace:
  79.         this.mc.ingameGui.getChatGUI().printChatMessage(p_146105_1_);
  80.         WITH:
  81.         if(!ChatHandler.hasHandledChat(p_146105_1_){
  82.             this.mc.ingameGui.getChatGUI().printChatMessage();
  83.          }
  84.          BYTECODE:
  85.          REPLACE:
  86.             mv.visitVarInsn(ALOAD, 0);
  87.             mv.visitFieldInsn(GETFIELD, "net/minecraft/client/entity/EntityPlayerSP", "mc", "Lnet/minecraft/client/Minecraft;");
  88.             v.visitFieldInsn(GETFIELD, "net/minecraft/client/Minecraft", "ingameGUI", "Lnet/minecraft/client/gui/GuiIngame;");
  89.             mv.visitMethodInsn(INVOKEVIRTUAL, "net/minecraft/client/gui/GuiIngame", "getChatGUI", "()Lnet/minecraft/client/gui/GuiNewChat;", false);
  90.             mv.visitVarInsn(ALOAD, 1);
  91.             mv.visitMethodInsn(INVOKEVIRTUAL, "net/minecraft/client/gui/GuiNewChat", "printChatMessage", "(Lnet/minecraft/util/IChatComponent;)V", false);
  92.          WIT
  93.             ALOAD 1
  94.             INVOKESTATIC me/mrkirby153/AntiChatSpam/regex/ChatHandler.hasHandledChat (Lnet/minecraft/util/IChatComponent;)Z
  95.             IFEQ newLabelNode
  96.             newLableNode
  97.             RETURN
  98.             mv.visitVarInsn(ALOAD, 0);
  99.             mv.visitFieldInsn(GETFIELD, "net/minecraft/client/entity/EntityPlayerSP", "mc", "Lnet/minecraft/client/Minecraft;");
  100.             v.visitFieldInsn(GETFIELD, "net/minecraft/client/Minecraft", "ingameGUI", "Lnet/minecraft/client/gui/GuiIngame;");
  101.             mv.visitMethodInsn(INVOKEVIRTUAL, "net/minecraft/client/gui/GuiIngame", "getChatGUI", "()Lnet/minecraft/client/gui/GuiNewChat;", false);
  102.             mv.visitVarInsn(ALOAD, 1);
  103.             mv.visitMethodInsn(INVOKEVIRTUAL, "net/minecraft/client/gui/GuiNewChat", "printChatMessage", "(Lnet/minecraft/util/IChatComponent;)V", false);
  104.             LABEL newLabelNode
  105.          */
  106.  
  107.         AbstractInsnNode insertPoint = targetNode.getPrevious();
  108.         LabelNode returnLLabel = new LabelNode();
  109.         LabelNode continueLabel = new LabelNode();
  110.         InsnList toInsert = new InsnList();
  111.         Type chatHandlerType = Type.getType(ChatHandler.class);
  112.         String handlerDescriptor;
  113.         try {
  114.             handlerDescriptor = Type.getMethodDescriptor(ChatHandler.class.getMethod("hasHandledChat", IChatComponent.class));
  115.         } catch (Exception e){
  116.             logger.catching(e);
  117.             return;
  118.         }
  119.         toInsert.add(new VarInsnNode(ALOAD, 1));
  120.         toInsert.add(new MethodInsnNode(INVOKESTATIC, chatHandlerType.getInternalName(), "hasHandledChat", handlerDescriptor, false));
  121.         toInsert.add(new JumpInsnNode(IFNE, returnLLabel));
  122.         toInsert.add(returnLLabel);
  123.         toInsert.add(new InsnNode(RETURN));
  124.         toInsert.add(continueLabel);
  125.         targetMethod.instructions.insert(insertPoint, toInsert);
  126.  
  127. //        AbstractInsnNode invokeVirtualPrint = targetNode;
  128. //        for(int i = 0; i < 5; i++){
  129. //            invokeVirtualPrint = invokeVirtualPrint.getNext();
  130. //        }
  131. //        LabelNode newLabelNode = new LabelNode();
  132. //        InsnList toInsert = new InsnList();
  133. //        toInsert.add(new VarInsnNode(ALOAD, 1));
  134. //        final String HAS_HANDLED_CHAT_DESCRIPTOR = (obfuscated)? "(Lfj;)V" : "(Lnet/minecraft/util/IChatComponent;)V";
  135. //        toInsert.add(new MethodInsnNode(INVOKESTATIC, Type.getInternalName(ChatHandler.class), "hasHandledChat", HAS_HANDLED_CHAT_DESCRIPTOR, false));
  136. //        toInsert.add(new JumpInsnNode(IFNE, newLabelNode));
  137. //
  138. //        targetMethod.instructions.insertBefore(targetNode, toInsert);
  139. //        targetMethod.instructions.insert(invokeVirtualPrint, newLabelNode);
  140. //
  141.         for(AbstractInsnNode n : targetMethod.instructions.toArray()){
  142.             System.out.println(n.getOpcode());
  143.         }
  144.     }
  145.  
  146.     private MethodNode findTargetNode(ClassNode classNode, String methodName, String methodDescriptor) {
  147.         logger.info(String.format("Finding method with name %s and descriptor %s", methodName, methodDescriptor));
  148.        for(MethodNode method : classNode.methods){
  149.            if(method.name.equals(methodName) && method.desc.equals(methodDescriptor)){
  150.                logger.info("Found method with name "+method.name+" and descriptor "+method.desc);
  151.                return method;
  152.            }
  153.        }
  154.         return null;
  155.     }
  156. }
Advertisement
Add Comment
Please, Sign In to add comment