Fix the ItemStack transformer to find the method and field so it works with srg and mcp naming.

This commit is contained in:
cpw 2014-08-12 11:14:17 -04:00
parent cb62a70d66
commit 18c71e5f2f

View file

@ -10,10 +10,14 @@ import org.objectweb.asm.Opcodes;
import org.objectweb.asm.tree.AbstractInsnNode;
import org.objectweb.asm.tree.ClassNode;
import org.objectweb.asm.tree.FieldInsnNode;
import org.objectweb.asm.tree.FieldNode;
import org.objectweb.asm.tree.MethodInsnNode;
import org.objectweb.asm.tree.MethodNode;
public class ItemStackTransformer implements IClassTransformer {
private static final String ITEM_TYPE = "Lnet/minecraft/item/Item;";
private static final String GETITEM_DESC = "()"+ ITEM_TYPE;
@Override
public byte[] transform(String name, String transformedName, byte[] basicClass) {
if (!"net.minecraft.item.ItemStack".equals(name))
@ -22,6 +26,40 @@ public class ItemStackTransformer implements IClassTransformer {
ClassReader classReader = new ClassReader(basicClass);
classReader.accept(classNode, 0);
FieldNode itemField = null;
for (FieldNode f : classNode.fields)
{
if (ITEM_TYPE.equals(f.desc) && itemField == null)
{
itemField = f;
}
else if (ITEM_TYPE.equals(f.desc))
{
throw new RuntimeException("Error processing ItemStack - found a duplicate Item field");
}
}
if (itemField == null)
{
throw new RuntimeException("Error processing ItemStack - no Item field declared (is the code somehow obfuscated?)");
}
MethodNode getItemMethod = null;
for (MethodNode m: classNode.methods)
{
if (GETITEM_DESC.equals(m.desc) && getItemMethod == null)
{
getItemMethod = m;
}
else if (GETITEM_DESC.equals(m.desc))
{
throw new RuntimeException("Error processing ItemStack - duplicate getItem method found");
}
}
if (getItemMethod == null)
{
throw new RuntimeException("Error processing ItemStack - no getItem method found (is the code somehow obfuscated?)");
}
for (MethodNode m: classNode.methods)
{
for (ListIterator<AbstractInsnNode> it = m.instructions.iterator(); it.hasNext(); )
@ -30,10 +68,10 @@ public class ItemStackTransformer implements IClassTransformer {
if (insnNode.getType() == AbstractInsnNode.FIELD_INSN)
{
FieldInsnNode fi = (FieldInsnNode)insnNode;
if ("field_151002_e".equals(fi.name) && fi.getOpcode() == Opcodes.GETFIELD)
if (itemField.name.equals(fi.name) && fi.getOpcode() == Opcodes.GETFIELD)
{
it.remove();
MethodInsnNode replace = new MethodInsnNode(Opcodes.INVOKEVIRTUAL, "net/minecraft/item/ItemStack","getItem", "()Lnet/minecraft/item/Item;");
MethodInsnNode replace = new MethodInsnNode(Opcodes.INVOKEVIRTUAL, "net/minecraft/item/ItemStack",getItemMethod.name, getItemMethod.desc);
it.add(replace);
}
}