Jump to content

[1.7.2] Patching a vanilla method does not work


CreativeMD

Recommended Posts

Hi,

I read this tutorial: here

I don't understand everything, so please don't blame me i'm stupid :). I just thought this could work.

 

I want to replace this:

public void doRender(Entity par1Entity, double par2, double par4, double par6, float par8, float par9)
    {
        this.doRender((EntityItem)par1Entity, par2, par4, par6, par8, par9);
    }

with this

ItemDummyContainer.doRender((EntityItem)par1Entity, par2, par4, par6, par8, par9, renderInFrame, renderWithColor);

 

So for this i used this method:

 

@Override
public byte[] transform(String arg0, String arg1, byte[] arg2) {
	if (arg0.contains("net.minecraft.client.renderer.entity.RenderItem")) {
		System.out.println("********* INSIDE OBFUSCATED MINECRAFTLOADER TRANSFORMER ABOUT TO PATCH: " + arg0);
		arg2 = replaceMethod(arg0, arg2, ItemPatchingLoader.location, !arg0.contains("net.minecraft.client.renderer.entity.RenderItem"));
		return arg2;
	}

 

public byte[] replaceMethod(String name, byte[] bytes, File location, boolean obfuscated)
{
	String targetMethodName = "doRender";


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

	Iterator<MethodNode> methods = classNode.methods.iterator();
	while(methods.hasNext())
	{
		MethodNode m = methods.next();

		if ((m.name.equals(targetMethodName) && m.desc.equals("(Lnet/minecraft/entity/Entity;DDDFF)V")))
		{
			System.out.println("********* Inside target method!");

			AbstractInsnNode currentNode = null;

			@SuppressWarnings("unchecked")
			Iterator<AbstractInsnNode> iter = m.instructions.iterator();

			int index = -1;

			while (iter.hasNext())
			{
				index++;
				currentNode = iter.next();

				//Found it! save the index location of instruction FDIV and the node for this instruction
				if (currentNode instanceof MethodInsnNode)
				{
					m.instructions.insertBefore(currentNode, new FieldInsnNode(Opcodes.GETSTATIC, name, "renderInFrame", "Z"));
					m.instructions.insertBefore(currentNode, new FieldInsnNode(Opcodes.GETFIELD, name, "renderWithColor", "Z"));
					((MethodInsnNode) currentNode).desc = "(Lnet/minecraft/entity/item/EntityItem;DDDFFZZ)V";
					((MethodInsnNode) currentNode).owner = "com/creativemd/itemphysic/ItemDummyContainer";
					((MethodInsnNode) currentNode).setOpcode(INVOKESTATIC);
				}
			}

			m.visitEnd();
			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();
}

 

I just copy this code from the forum, but this part is programmed by myself:

while (iter.hasNext())
			{
				index++;
				currentNode = iter.next();

				//Found it! save the index location of instruction FDIV and the node for this instruction
				if (currentNode instanceof MethodInsnNode)
				{
					m.instructions.insertBefore(currentNode, new FieldInsnNode(Opcodes.GETSTATIC, name, "renderInFrame", "Z"));
					m.instructions.insertBefore(currentNode, new FieldInsnNode(Opcodes.GETFIELD, name, "renderWithColor", "Z"));
					((MethodInsnNode) currentNode).desc = "(Lnet/minecraft/entity/item/EntityItem;DDDFFZZ)V";
					((MethodInsnNode) currentNode).owner = "com/creativemd/itemphysic/ItemDummyContainer";
					((MethodInsnNode) currentNode).setOpcode(INVOKESTATIC);
				}
			}

 

This inserts the new two fields (renderInFrame and renderWithColor) and changes the method.

Like a said, i don't understand everything with is going on, but obviously this don't work, because this error comes up:

 

java.lang.NoClassDefFoundError: net/minecraft/client/renderer/entity/RenderItem
...
Caused by: java.lang.ClassNotFoundException: net.minecraft.client.renderer.entity.RenderItem

 

So hopefully i could give you an impression what is going on. I don't have an idea what is wrong.

I would be very very happy if someone can help me :)

 

EDIT

 

I noticed that when i let the new two booleans, out it works :), so the error caused by the insertBefore. I noticed that my two new FieldInNode have an index of 0 and doesn't fit into it.

 

Thanks previously

Link to comment
Share on other sites

Oh man your so right :D, of course i know this renderer, but i didn't thought about it.

Anyways i have to do something similar again, so can you tell me what i'm doing wrong.

I found out that i have to change this

m.instructions.insertBefore(currentNode, new FieldInsnNode(Opcodes.GETFIELD, name, "renderWithColor", "Z"));

to this

m.instructions.insertBefore(currentNode, new FieldInsnNode(Opcodes.GETFIELD, name.replace(".", "/"), "renderWithColor", "Z"));

 

but then this error comes up

java.lang.VerifyError: (class: net/minecraft/client/renderer/entity/RenderItem, method: doRender signature: (Lnet/minecraft/entity/Entity;DDDFF)V) Expecting to find object/array on stack

 

I would be very happy if you could help me with that :)

Link to comment
Share on other sites

Basically i have no clue at all :). I read the tutorial on replacing a vanilla method and start debugging the code and try to get an impression what is going on.

So i just look at all variables and thought this could work. Changing the method like this:

((MethodInsnNode) currentNode).desc = "(Lnet/minecraft/entity/item/EntityItem;DDDFF)V";
((MethodInsnNode) currentNode).owner = "com/creativemd/itemphysic/ItemDummyContainer";
((MethodInsnNode) currentNode).setOpcode(INVOKESTATIC);

does obviously work out :). I installed a ByteCode plugin in my eclipse and look up how this method looks like. I recognized that the variables are "visited" before the method is called and after that they get an label like this

mv = cw.visitMethod(ACC_PUBLIC, "doRender", "(Lnet/minecraft/entity/Entity;DDDFF)V", null, null);
mv.visitCode();
Label l0 = new Label();
mv.visitLabel(l0);
mv.visitLineNumber(756, l0);
mv.visitVarInsn(ALOAD, 0);
mv.visitVarInsn(ALOAD, 1);
mv.visitTypeInsn(CHECKCAST, "net/minecraft/entity/item/EntityItem");
mv.visitVarInsn(DLOAD, 2);
mv.visitVarInsn(DLOAD, 4);
mv.visitVarInsn(DLOAD, 6);
mv.visitVarInsn(FLOAD, ;
mv.visitVarInsn(FLOAD, 9);
mv.visitMethodInsn(INVOKEVIRTUAL, "net/minecraft/client/renderer/entity/RenderItem", "doRender", "(Lnet/minecraft/entity/item/EntityItem;DDDFF)V", false);
Label l1 = new Label();
mv.visitLabel(l1);
mv.visitLineNumber(757, l1);
mv.visitInsn(RETURN);
Label l2 = new Label();
mv.visitLabel(l2);
mv.visitLocalVariable("this", "Lnet/minecraft/client/renderer/entity/RenderItem;", null, l0, l2, 0);
mv.visitLocalVariable("par1Entity", "Lnet/minecraft/entity/Entity;", null, l0, l2, 1);
mv.visitLocalVariable("par2", "D", null, l0, l2, 2);
mv.visitLocalVariable("par4", "D", null, l0, l2, 4);
mv.visitLocalVariable("par6", "D", null, l0, l2, 6);
mv.visitLocalVariable("par8", "F", null, l0, l2, ;
mv.visitLocalVariable("par9", "F", null, l0, l2, 9);
mv.visitMaxs(10, 10);
mv.visitEnd();

So i tried to add a new parameter to the method by just adding this

m.instructions.insertBefore(currentNode, new FieldInsnNode(Opcodes.GETFIELD, name.replace(".", "/"), "renderWithColor", "Z"));

But this obviously don't work, i just look into m.instructions variable and tried to do it the same way. Yes and of course i changed the desc to that "(Lnet/minecraft/entity/item/EntityItem;DDDFFZ)V", but it's still not working.

 

So hopefully this can help you to help me :P.

Link to comment
Share on other sites

You are completely right, but i found no tutorials :(.

I know this is difficulty because i understand only 20% of what is going on, because of that i looked into that code also in variables of the instructions, but this was obviously not enough :(.

So i would be very happy if you could give me a link to a tutorial :) and i also would be very happy if you could tell me why this is not working, isn't their a possibility to add instructions?

Probably you don't understand it because you don't have enough information :(

Link to comment
Share on other sites

m.instructions.insertBefore(currentNode, new VarInsnNode(ALOAD, 0)); //mv.visitVarInsn(ALOAD, 0);
m.instructions.insertBefore(currentNode, new FieldInsnNode(Opcodes.GETFIELD, name.replace(".", "/"), "renderWithColor", "Z"));
((MethodInsnNode) currentNode).desc = "(Lnet/minecraft/entity/item/EntityItem;DDDFFZ)V";
((MethodInsnNode) currentNode).owner = "com/creativemd/itemphysic/ItemDummyContainer";
((MethodInsnNode) currentNode).setOpcode(INVOKESTATIC);

i only need to add this line :D, thank you very much. If this would be a getstatic this line before is not needed.

Link to comment
Share on other sites

I'm sad to tell this, but there is new error.

My Code

public byte[] replaceMethod(String name, byte[] bytes, File location, boolean obfuscated)
{
	String targetMethodName = "doRender";
	String targetDESC = "(Lnet/minecraft/entity/Entity;DDDFF)V";
	String newDESC = "(Lnet/minecraft/entity/Entity;DDDFFZ)V";
	String targetVar = "renderWithColor";

	//Our target method
	if(obfuscated == true)
	{
		System.out.println("Obfuscated");
		targetMethodName ="a";
		targetDESC = "(Lqn;DDDFF)V";
		newDESC = "(Lqn;DDDFFZ)V";
		targetVar = "field_77024_a";
	}


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

	Iterator<MethodNode> methods = classNode.methods.iterator();
	while(methods.hasNext())
	{
		MethodNode m = methods.next();
		System.out.println("Search in " + m.name + ": " + m.desc);
		if ((m.name.equals(targetMethodName) && m.desc.equals(targetDESC)))
		{
			System.out.println("********* Inside target method!");

			AbstractInsnNode currentNode = null;

			@SuppressWarnings("unchecked")
			Iterator<AbstractInsnNode> iter = m.instructions.iterator();

			while (iter.hasNext())
			{
				currentNode = iter.next();
				if (currentNode instanceof MethodInsnNode)
				{
					//m.instructions.insertBefore(currentNode, new FieldInsnNode(Opcodes.GETSTATIC, RenderItem.class.get, "renderInFrame", "Z"));
					m.instructions.insertBefore(currentNode, new VarInsnNode(ALOAD, 0)); //mv.visitVarInsn(ALOAD, 0);
					m.instructions.insertBefore(currentNode, new FieldInsnNode(Opcodes.GETFIELD, name.replace(".", "/"), targetVar, "Z"));
					((MethodInsnNode) currentNode).desc = newDESC;
					((MethodInsnNode) currentNode).owner = "com/creativemd/itemphysic/ItemDummyContainer";
					((MethodInsnNode) currentNode).setOpcode(INVOKESTATIC);
				}
			}
			m.visitEnd();
			System.out.println("Patching Complete!");
			break;
		}
	}
	System.out.println("Search in " + name);
	//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();
}

I tried to release this, but then this error comes up:

---- Minecraft Crash Report ----
// Quite honestly, I wouldn't worry myself about that.

Time: 15.05.14 15:28
Description: Initializing game

java.lang.NoClassDefFoundError: net/minecraft/client/renderer/entity/RenderItem
at net.minecraft.client.gui.achievement.GuiAchievement.<init>(SourceFile:29)
at net.minecraft.client.Minecraft.func_71384_a(Minecraft.java:454)
at net.minecraft.client.Minecraft.func_99999_d(Minecraft.java:813)
at net.minecraft.client.main.Main.main(SourceFile:103)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at net.minecraft.launchwrapper.Launch.launch(Launch.java:134)
at net.minecraft.launchwrapper.Launch.main(Launch.java:28)
Caused by: java.lang.ClassNotFoundException: net.minecraft.client.renderer.entity.RenderItem
at net.minecraft.launchwrapper.LaunchClassLoader.findClass(LaunchClassLoader.java:188)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
... 10 more
Caused by: java.lang.RuntimeException: java.lang.ClassNotFoundException: bpv
at org.objectweb.asm.ClassWriter.getCommonSuperClass(Unknown Source)
at org.objectweb.asm.ClassWriter.a(Unknown Source)
at org.objectweb.asm.Frame.a(Unknown Source)
at org.objectweb.asm.Frame.a(Unknown Source)
at org.objectweb.asm.MethodWriter.visitMaxs(Unknown Source)
at org.objectweb.asm.tree.MethodNode.accept(Unknown Source)
at org.objectweb.asm.tree.MethodNode.accept(Unknown Source)
at org.objectweb.asm.tree.ClassNode.accept(Unknown Source)
at com.creativemd.itemphysic.ItemTransformer.replaceMethod(ItemTra
nsformer.java:125)
at com.creativemd.itemphysic.ItemTransformer.transform(ItemTransformer.java:54)
at net.minecraft.launchwrapper.LaunchClassLoader.runTransformers(LaunchClassLoader.java:276)
at net.minecraft.launchwrapper.LaunchClassLoader.findClass(LaunchClassLoader.java:174)
... 12 more

I have no idea what is wrong :(

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.

Announcements



×
×
  • Create New...

Important Information

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