Jump to content

Recommended Posts

Posted

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();

      }

}

Posted

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 :)

Posted

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;
               }

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.

Announcements



  • Recently Browsing

    • No registered users viewing this page.
  • Posts

    • Betafort Recovery has emerged as a prominent figure in the realm of cryptocurrency recovery, gaining a reputation for their exceptional ability to retrieve lost Bitcoin (BTC) and other cryptocurrencies. Their expertise and track record have made them a beacon of hope for individuals facing the distressing situation of lost or inaccessible crypto assets.  
    • When you name a method like that, with no return value, it is a constructor. The constructor must have the same name as the class it constructs, in this case, ModItems. I would strongly advise reading up on some basic Java tutorials, because you will definitely be running into a lot more issues as you go along without the basics. *I should also add that the Forge documentation is a reference, not a tutorial. Even following tutorials, you should know Java basics, otherwise the smallest of mistakes will trip you up as you copy someone elses code.
    • so, I'm starting modding and I'm following the official documantation for forge: https://docs.minecraftforge.net, but in the registries part it is not working as it is in the docs:   public class ModItems { private static final DeferredRegister<Item> ITEMS = DeferredRegister.create(ForgeRegistries.ITEMS, DarkStarvation.MOD_ID); public static final RegistryObject<Item> TEST_ITEM = ITEMS.register("test_item", () -> new Item(new Item.Properties())); public DarkStarvation(FMLJavaModLoadingContext context) { ITEMS.register(context.getModEventBus()); } } in 'public DarkStarvation(...' the DarkStarvation has this error: Invalid method declaration; return type required and the getModEventBus(): Cannot resolve method 'getModEventBus' in 'FMLJavaModLoadingContext' please help, I asked gpt but it is saying that I'm using an old method, but I'm following the latest version of Forge Docs???
    • I merged your second post with the original , there is no need to post a new thread asking for an answer. If someone sees your post and can help, they will reply. If you are seeking a quicker response, you could try asking in the Minecraft Forge diacord.
    • Create a new instance and start with cobblemon - if this works, add the rest of your mods in groups   Maybe another mod is conflicting - like Sodium/Iris or Radical Cobblemon Trainers
  • Topics

×
×
  • Create New...

Important Information

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