Jump to content

coremod: add return instruction


gargan

Recommended Posts

Hello!

I try to modify 2 functions: getSkinUrl and getCapeUrl with ASM in order to create my own skin system. In this way i can do event in my server. Tank's to this code I have managed to delete the content of function and i have add a invocation to a static method. But I want return the result and I don't know how do this!

 

package pickandcraftSkin;

 

import static org.objectweb.asm.Opcodes.ALOAD;

import static org.objectweb.asm.Opcodes.INVOKESTATIC;

 

import java.util.Iterator;

 

import org.objectweb.asm.ClassReader;

import org.objectweb.asm.ClassWriter;

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;

 

 

 

 

public class EDClassTransformer implements net.minecraft.launchwrapper.IClassTransformer {

 

@Override

public byte[] transform(String arg0, String arg1, byte[] arg2) {

 

if (arg0.equals("beu")) {

System.out.println("********* INSIDE OBFUSCATED AbstractClientPlayer TRANSFORMER ABOUT TO PATCH: " + arg0);

return patchClassASM(arg0, arg2, true);

        }

 

if (arg0.equals("net.minecraft.client.entity.AbstractClientPlayer")) {

System.out.println("********* INSIDE AbstractClientPlayer TRANSFORMER ABOUT TO PATCH: " + arg0);

return patchClassASM(arg0, arg2, false);

        }

        return arg2;

}

 

public byte[] patchClassASM(String name, byte[] bytes, boolean obfuscated) {

 

String targetMethodName = "";

       

        if(obfuscated == true)

        targetMethodName ="d";

        else

        targetMethodName ="getSkinUrl";

       

 

      //set up ASM class manipulation stuff. Consult the ASM docs for details

    ClassNode classNode = new ClassNode();

        ClassReader classReader = new ClassReader(bytes);

        classReader.accept(classNode, 0);

       

       

     

      //Now we loop over all of the methods declared inside the Explosion class until we get to the targetMethodName "doExplosionB"

 

        // find method to inject into

        @SuppressWarnings("unchecked")

        Iterator<MethodNode> methods = classNode.methods.iterator();

        while(methods.hasNext())

        {

            MethodNode m = methods.next();

            System.out.println("********* Method Name: "+m.name + " Desc:" + m.desc);

 

       

          //Check if this is doExplosionB and it's method signature is (Z)V which means that it accepts a boolean (Z) and returns a void (V)

            if ((m.name.equals(targetMethodName) && m.desc.equals("(Ljava/lang/String;)Ljava/lang/String;")))

            {

                System.out.println("********* Inside target method!");

                //on supprime tous les noeuds

                AbstractInsnNode targetNode = null;

               

                @SuppressWarnings("unchecked")

                Iterator<AbstractInsnNode> iter = m.instructions.iterator();

                int index = -1;

               

                while (iter.hasNext()) {

                    index++;

                    targetNode = iter.next();

                    m.instructions.remove(targetNode);

                }

           

                InsnList toInject = new InsnList();

             

toInject.add(new VarInsnNode(ALOAD, 0));

                    toInject.add(new MethodInsnNode(INVOKESTATIC, "pickandcraftSkin/PlayerCustom", "getURLSkincustom", "(Ljava/lang/String;)Ljava/lang/String;"));

               

                // inject new instruction list into method instruction list

                m.instructions.insert(toInject);

               

                System.out.println("Patching Complete!");

                break;

            }

        }

       

      //ASM specific for cleaning up and returning the final bytes for JVM processing.

        ClassWriter writer = new ClassWriter(ClassWriter.COMPUTE_MAXS | ClassWriter.COMPUTE_FRAMES);

        classNode.accept(writer);

        return writer.toByteArray();

      }

}

Link to comment
Share on other sites

But where add the return instruction? Before or after  toInject.add(new MethodInsnNode(INVOKESTATIC, "pickandcraftSkin/PlayerCustom", "getURLSkincustom", "(Ljava/lang/String;)Ljava/lang/String;")) ?

And how add the return instruction? I can't do this: toInject.add("ARETURN")!

 

 

Sorry for my english: I'm french and I don't speak very well english :)

Link to comment
Share on other sites

It should be safe to completely replace the instruction list of the method. It would be more efficient, and cleaner.

 

As for what to put in the new instruction list, here's what you need to do:

               if ((m.name.equals(targetMethodName) && m.desc.equals("(Ljava/lang/String;)Ljava/lang/String;")))
               {
                   System.out.println("********* Inside target method!");
                   InsnList toInject = new InsnList();
                     
                   toInject.add(new VarInsnNode(ALOAD, 0));
                   toInject.add(new MethodInsnNode(INVOKESTATIC, "pickandcraftSkin/PlayerCustom", "getURLSkincustom", "(Ljava/lang/String;)Ljava/lang/String;"));
                   toInject.add(new InsnNode(ARETURN));
                
                   // inject new instruction list into method instruction list
                   m.instructions = toInject;
                   
                   System.out.println("Patching Complete!");
                   break;
               }

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.