Example usage for org.objectweb.asm.tree InsnList add

List of usage examples for org.objectweb.asm.tree InsnList add

Introduction

In this page you can find the example usage for org.objectweb.asm.tree InsnList add.

Prototype

public void add(final InsnList insnList) 

Source Link

Document

Adds the given instructions to the end of this list.

Usage

From source file:nova.core.wrapper.mc.forge.v17.asm.lib.ASMHelper.java

License:Open Source License

public static InsnList cloneInsnList(Map<LabelNode, LabelNode> labelMap, InsnList insns) {
    InsnList clone = new InsnList();
    for (AbstractInsnNode insn = insns.getFirst(); insn != null; insn = insn.getNext()) {
        clone.add(insn.clone(labelMap));
    }//  w  w w  . j  a v  a2 s.com

    return clone;
}

From source file:nova.core.wrapper.mc.forge.v17.asm.lib.InstructionComparator.java

License:Open Source License

public static InsnList getImportantList(InsnList list) {
    if (list.size() == 0) {
        return list;
    }/*from  w  w w .  ja v  a 2  s.co  m*/

    HashMap<LabelNode, LabelNode> labels = new HashMap<LabelNode, LabelNode>();
    for (AbstractInsnNode insn = list.getFirst(); insn != null; insn = insn.getNext()) {
        if (insn instanceof LabelNode) {
            labels.put((LabelNode) insn, (LabelNode) insn);
        }
    }

    InsnList importantNodeList = new InsnList();
    for (AbstractInsnNode insn = list.getFirst(); insn != null; insn = insn.getNext()) {
        if (insn instanceof LabelNode || insn instanceof LineNumberNode) {
            continue;
        }

        importantNodeList.add(insn.clone(labels));
    }
    return importantNodeList;
}

From source file:nova.core.wrapper.mc.forge.v17.asm.transformers.ChunkTransformer.java

License:Open Source License

@Override
public void transform(ClassNode cnode) {
    System.out.println("[NOVA] Transforming Chunk class for chunkModified event.");

    ObfMapping obfMap = new ObfMapping("apx", "a", "(IIILaji;I)Z");
    ObfMapping srgMap = new ObfMapping("net/minecraft/world/chunk/Chunk", "func_150807_a",
            "(IIILnet/minecraft/block/Block;I)Z");

    MethodNode method = ASMHelper.findMethod(obfMap, cnode);

    if (method == null) {
        System.out.println("[NOVA] Lookup " + obfMap + " failed. You are probably in a deobf environment.");
        method = ASMHelper.findMethod(srgMap, cnode);

        if (method == null) {
            System.out.println("[NOVA] Lookup " + srgMap + " failed!");
        }/*from   w w w  .  j  a v a2  s  .c  o  m*/
    }

    System.out.println("[NOVA] Found method " + method.name);

    InsnList list = new InsnList();
    list.add(new VarInsnNode(ALOAD, 0));
    list.add(new VarInsnNode(ILOAD, 1));
    list.add(new VarInsnNode(ILOAD, 2));
    list.add(new VarInsnNode(ILOAD, 3));
    list.add(new VarInsnNode(ALOAD, 8));
    list.add(new VarInsnNode(ILOAD, 9));
    list.add(new VarInsnNode(ALOAD, 4));
    list.add(new VarInsnNode(ILOAD, 5));
    list.add(new MethodInsnNode(INVOKESTATIC, "nova/core/wrapper/mc/forge/v17/asm/StaticForwarder",
            "chunkSetBlockEvent",
            "(Lnet/minecraft/world/chunk/Chunk;IIILnet/minecraft/block/Block;ILnet/minecraft/block/Block;I)V",
            false));

    AbstractInsnNode lastInsn = method.instructions.getLast();
    while (lastInsn instanceof LabelNode || lastInsn instanceof LineNumberNode) {
        lastInsn = lastInsn.getPrevious();
    }

    if (ASMHelper.isReturn(lastInsn)) {
        method.instructions.insertBefore(lastInsn, list);
    } else {
        method.instructions.insert(list);
    }

    System.out.println("[NOVA] Injected instruction to method: " + method.name);
}

From source file:nova.core.wrapper.mc.forge.v17.asm.transformers.TileEntityTransformer.java

License:Open Source License

@Override
public void transform(ClassNode cnode) {

    System.out.println("[NOVA] Transforming TileEntity class for dynamic instance injection.");

    ObfMapping obfMap = new ObfMapping("aor", "c", "(Ldh;)Laor;");
    ObfMapping deobfMap = new ObfMapping("net/minecraft/tileentity/TileEntity", "createAndLoadEntity",
            "(Lnet/minecraft/nbt/NBTTagCompound;)Lnet/minecraft/tileentity/TileEntity;");

    MethodNode method = ASMHelper.findMethod(obfMap, cnode);

    if (method == null) {
        System.out.println("[NOVA] Lookup " + obfMap + " failed. You are probably in a deobf environment.");
        method = ASMHelper.findMethod(deobfMap, cnode);

        if (method == null) {
            System.out.println("[NOVA] Lookup " + deobfMap + " failed!");
        }/* ww w  . jav a 2s  .  c o m*/
    }

    System.out.println("[NOVA] Transforming method " + method.name);

    ASMHelper.removeBlock(method.instructions,
            new InstructionComparator.InsnListSection(method.instructions, 23, 26));

    InsnList list = new InsnList();
    list.add(new VarInsnNode(ALOAD, 0));
    list.add(new VarInsnNode(ALOAD, 2));
    list.add(new MethodInsnNode(INVOKESTATIC, "nova/core/wrapper/mc/forge/v17/asm/StaticForwarder",
            "loadTileEntityHook",
            "(Lnet/minecraft/nbt/NBTTagCompound;Ljava/lang/Class;)Lnet/minecraft/tileentity/TileEntity;",
            false));
    list.add(new VarInsnNode(ASTORE, 1));

    method.instructions.insert(method.instructions.get(22), list);
}

From source file:nova.core.wrapper.mc.forge.v18.asm.transformers.ChunkTransformer.java

License:Open Source License

@Override
public void transform(ClassNode cnode) {
    System.out.println("[NOVA] Transforming Chunk class for chunkModified event.");

    //obf name: func_177436_a
    MethodNode method = ASMHelper.findMethod(new ObfMapping("net/minecraft/world/chunk/Chunk", "setBlockState",
            "(Lnet/minecraft/util/BlockPos;Lnet/minecraft/block/state/IBlockState;)Lnet/minecraft/block/state/IBlockState;"),
            cnode);/* w w  w.j  ava 2  s . c om*/

    System.out.println("[NOVA] Found method " + method.name);

    InsnList list = new InsnList();
    list.add(new VarInsnNode(ALOAD, 0)); //this
    list.add(new VarInsnNode(ALOAD, 1)); //BlockPos
    list.add(new VarInsnNode(ALOAD, 8)); //oldBlock IBlockState
    list.add(new VarInsnNode(ALOAD, 2)); //newBlock IBlockState
    list.add(new MethodInsnNode(INVOKESTATIC, "nova/core/wrapper/mc/forge/v18/asm/StaticForwarder",
            "chunkSetBlockEvent",
            "(Lnet/minecraft/world/chunk/Chunk;Lnet/minecraft/util/BlockPos;Lnet/minecraft/block/state/IBlockState;Lnet/minecraft/block/state/IBlockState;)V",
            false));

    AbstractInsnNode lastInsn = method.instructions.getLast();
    while (lastInsn instanceof LabelNode || lastInsn instanceof LineNumberNode) {
        lastInsn = lastInsn.getPrevious();
    }

    if (ASMHelper.isReturn(lastInsn)) {
        method.instructions.insertBefore(lastInsn, list);
    } else {
        method.instructions.insert(list);
    }

    System.out.println("[NOVA] Injected instruction to method: " + method.name);
}

From source file:nova.core.wrapper.mc.forge.v18.asm.transformers.TileEntityTransformer.java

License:Open Source License

@Override
public void transform(ClassNode cnode) {

    System.out.println("[NOVA] Transforming TileEntity class for dynamic instance injection.");

    ObfMapping obfMap = new ObfMapping("bcm", "c", "(Lfn;)Lbcm;");
    ObfMapping deobfMap = new ObfMapping("net/minecraft/tileentity/TileEntity", "createAndLoadEntity",
            "(Lnet/minecraft/nbt/NBTTagCompound;)Lnet/minecraft/tileentity/TileEntity;");

    MethodNode method = ASMHelper.findMethod(obfMap, cnode);

    if (method == null) {
        System.out.println("[NOVA] Lookup " + obfMap + " failed. You are probably in a deobf environment.");
        method = ASMHelper.findMethod(deobfMap, cnode);

        if (method == null) {
            System.out.println("[NOVA] Lookup " + deobfMap + " failed!");
        }/*from   www. j a  v a  2 s.  c o m*/
    }

    System.out.println("[NOVA] Transforming method " + method.name);

    ASMHelper.removeBlock(method.instructions,
            new InstructionComparator.InsnListSection(method.instructions, 23, 26));

    InsnList list = new InsnList();
    list.add(new VarInsnNode(ALOAD, 0));
    list.add(new VarInsnNode(ALOAD, 2));
    list.add(new MethodInsnNode(INVOKESTATIC, "nova/core/wrapper/mc/forge/v18/asm/StaticForwarder",
            "loadTileEntityHook",
            "(Lnet/minecraft/nbt/NBTTagCompound;Ljava/lang/Class;)Lnet/minecraft/tileentity/TileEntity;",
            false));
    list.add(new VarInsnNode(ASTORE, 1));

    method.instructions.insert(method.instructions.get(22), list);
}

From source file:org.apache.commons.javaflow.providers.asm3.ContinuableMethodNode.java

License:Apache License

void moveNew() throws AnalyzerException {
    final SourceInterpreter i = new SourceInterpreter();
    final Analyzer a = new Analyzer(i);
    a.analyze(className, this);

    final HashMap<AbstractInsnNode, MethodInsnNode> movable = new HashMap<AbstractInsnNode, MethodInsnNode>();

    final Frame[] frames = a.getFrames();
    for (int j = 0; j < methods.size(); j++) {
        final MethodInsnNode mnode = (MethodInsnNode) methods.get(j);
        // require to move NEW instruction
        int n = instructions.indexOf(mnode);
        Frame f = frames[n];//from  w ww  . java2  s  . c  o  m
        Type[] args = Type.getArgumentTypes(mnode.desc);

        SourceValue v = (SourceValue) f.getStack(f.getStackSize() - args.length - 1);
        @SuppressWarnings("unchecked")
        Set<AbstractInsnNode> insns = v.insns;
        for (final AbstractInsnNode ins : insns) {
            if (ins.getOpcode() == NEW) {
                movable.put(ins, mnode);
            } else {
                // other known patterns
                int n1 = instructions.indexOf(ins);
                if (ins.getOpcode() == DUP) { // <init> with params
                    AbstractInsnNode ins1 = instructions.get(n1 - 1);
                    if (ins1.getOpcode() == NEW) {
                        movable.put(ins1, mnode);
                    }
                } else if (ins.getOpcode() == SWAP) { // in exception handler
                    AbstractInsnNode ins1 = instructions.get(n1 - 1);
                    AbstractInsnNode ins2 = instructions.get(n1 - 2);
                    if (ins1.getOpcode() == DUP_X1 && ins2.getOpcode() == NEW) {
                        movable.put(ins2, mnode);
                    }
                }
            }
        }
    }

    int updateMaxStack = 0;
    for (final Map.Entry<AbstractInsnNode, MethodInsnNode> e : movable.entrySet()) {
        AbstractInsnNode node1 = e.getKey();
        int n1 = instructions.indexOf(node1);
        AbstractInsnNode node2 = instructions.get(n1 + 1);
        AbstractInsnNode node3 = instructions.get(n1 + 2);
        int producer = node2.getOpcode();

        instructions.remove(node1); // NEW
        boolean requireDup = false;
        if (producer == DUP) {
            instructions.remove(node2); // DUP
            requireDup = true;
        } else if (producer == DUP_X1) {
            instructions.remove(node2); // DUP_X1
            instructions.remove(node3); // SWAP
            requireDup = true;
        }

        MethodInsnNode mnode = (MethodInsnNode) e.getValue();
        AbstractInsnNode nm = mnode;

        int varOffset = stackRecorderVar + 1;
        Type[] args = Type.getArgumentTypes(mnode.desc);

        // optimizations for some common cases
        if (args.length == 0) {
            final InsnList doNew = new InsnList();
            doNew.add(node1); // NEW
            if (requireDup)
                doNew.add(new InsnNode(DUP));
            instructions.insertBefore(nm, doNew);
            nm = doNew.getLast();
            continue;
        }

        if (args.length == 1 && args[0].getSize() == 1) {
            final InsnList doNew = new InsnList();
            doNew.add(node1); // NEW
            if (requireDup) {
                doNew.add(new InsnNode(DUP));
                doNew.add(new InsnNode(DUP2_X1));
                doNew.add(new InsnNode(POP2));
                updateMaxStack = updateMaxStack < 2 ? 2 : updateMaxStack; // a two extra slots for temp values
            } else
                doNew.add(new InsnNode(SWAP));
            instructions.insertBefore(nm, doNew);
            nm = doNew.getLast();
            continue;
        }

        // TODO this one untested!
        if ((args.length == 1 && args[0].getSize() == 2)
                || (args.length == 2 && args[0].getSize() == 1 && args[1].getSize() == 1)) {
            final InsnList doNew = new InsnList();
            doNew.add(node1); // NEW
            if (requireDup) {
                doNew.add(new InsnNode(DUP));
                doNew.add(new InsnNode(DUP2_X2));
                doNew.add(new InsnNode(POP2));
                updateMaxStack = updateMaxStack < 2 ? 2 : updateMaxStack; // a two extra slots for temp values
            } else {
                doNew.add(new InsnNode(DUP_X2));
                doNew.add(new InsnNode(POP));
                updateMaxStack = updateMaxStack < 1 ? 1 : updateMaxStack; // an extra slot for temp value
            }
            instructions.insertBefore(nm, doNew);
            nm = doNew.getLast();
            continue;
        }

        final InsnList doNew = new InsnList();
        // generic code using temporary locals
        // save stack
        for (int j = args.length - 1; j >= 0; j--) {
            Type type = args[j];

            doNew.add(new VarInsnNode(type.getOpcode(ISTORE), varOffset));
            varOffset += type.getSize();
        }
        if (varOffset > maxLocals) {
            maxLocals = varOffset;
        }

        doNew.add(node1); // NEW

        if (requireDup)
            doNew.add(new InsnNode(DUP));

        // restore stack
        for (int j = 0; j < args.length; j++) {
            Type type = args[j];
            varOffset -= type.getSize();

            doNew.add(new VarInsnNode(type.getOpcode(ILOAD), varOffset));

            // clean up store to avoid memory leak?
            if (type.getSort() == Type.OBJECT || type.getSort() == Type.ARRAY) {
                updateMaxStack = updateMaxStack < 1 ? 1 : updateMaxStack; // an extra slot for ACONST_NULL

                doNew.add(new InsnNode(ACONST_NULL));

                doNew.add(new VarInsnNode(type.getOpcode(ISTORE), varOffset));
            }
        }
        instructions.insertBefore(nm, doNew);
        nm = doNew.getLast();
    }

    maxStack += updateMaxStack;
}

From source file:org.blockartistry.DynSurround.asm.Transformer.java

License:MIT License

private byte[] transformEntityRenderer(final byte[] classBytes) {
    final String names[] = { "func_78484_h", "addRainParticles" };
    final String targetName = "addRainParticles";

    final ClassReader cr = new ClassReader(classBytes);
    final ClassNode cn = new ClassNode(ASM5);
    cr.accept(cn, 0);//w  ww  .ja  v  a 2  s .  c  o  m

    for (final MethodNode m : cn.methods) {
        if (isOneOf(m.name, names)) {
            logger.debug("Hooking " + m.name);
            final InsnList list = new InsnList();
            list.add(new VarInsnNode(ALOAD, 0));
            final String sig = "(Lnet/minecraft/client/renderer/EntityRenderer;)V";
            list.add(new MethodInsnNode(INVOKESTATIC,
                    "org/blockartistry/DynSurround/client/weather/RenderWeather", targetName, sig, false));
            list.add(new InsnNode(RETURN));
            m.instructions.insertBefore(m.instructions.getFirst(), list);
        }
    }

    final ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS);
    cn.accept(cw);
    return cw.toByteArray();
}

From source file:org.blockartistry.DynSurround.asm.Transformer.java

License:MIT License

private byte[] transformWorldServer(final byte[] classBytes) {
    final String names[] = { "func_73051_P", "resetRainAndThunder" };
    final String targetName = "resetRainAndThunder";

    final ClassReader cr = new ClassReader(classBytes);
    final ClassNode cn = new ClassNode(ASM5);
    cr.accept(cn, 0);/*  www . jav  a2 s . c om*/

    for (final MethodNode m : cn.methods) {
        if (isOneOf(m.name, names)) {
            logger.debug("Hooking " + m.name);
            InsnList list = new InsnList();
            list.add(new VarInsnNode(ALOAD, 0));
            final String sig = "(Lnet/minecraft/world/WorldServer;)V";
            list.add(new MethodInsnNode(INVOKESTATIC, "org/blockartistry/DynSurround/server/PlayerSleepHandler",
                    targetName, sig, false));
            list.add(new InsnNode(RETURN));
            m.instructions.insertBefore(m.instructions.getFirst(), list);
            break;
        }
    }

    final ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS);
    cn.accept(cw);
    return cw.toByteArray();
}

From source file:org.blockartistry.mod.DynSurround.asm.Transformer.java

License:MIT License

private byte[] transformEntityRenderer(final byte[] classBytes) {
    final String names[];

    if (TransformLoader.runtimeDeobEnabled)
        names = new String[] { "func_78474_d", "func_78484_h" };
    else/* w ww.  j a  v  a  2 s  .co m*/
        names = new String[] { "renderRainSnow", "addRainParticles" };

    final String targetName[] = new String[] { "renderRainSnow", "addRainParticles" };

    final ClassReader cr = new ClassReader(classBytes);
    final ClassNode cn = new ClassNode(ASM5);
    cr.accept(cn, 0);

    for (final MethodNode m : cn.methods) {
        if (m.name.equals(names[0])) {
            logger.debug("Hooking " + names[0]);
            final InsnList list = new InsnList();
            list.add(new VarInsnNode(ALOAD, 0));
            list.add(new VarInsnNode(FLOAD, 1));
            final String sig = "(Lnet/minecraft/client/renderer/EntityRenderer;F)V";
            list.add(new MethodInsnNode(INVOKESTATIC, "org/blockartistry/mod/DynSurround/client/RenderWeather",
                    targetName[0], sig, false));
            list.add(new InsnNode(RETURN));
            m.instructions.insertBefore(m.instructions.getFirst(), list);
        } else if (m.name.equals(names[1])) {
            logger.debug("Hooking " + names[1]);
            final InsnList list = new InsnList();
            list.add(new VarInsnNode(ALOAD, 0));
            final String sig = "(Lnet/minecraft/client/renderer/EntityRenderer;)V";
            list.add(new MethodInsnNode(INVOKESTATIC, "org/blockartistry/mod/DynSurround/client/RenderWeather",
                    targetName[1], sig, false));
            list.add(new InsnNode(RETURN));
            m.instructions.insertBefore(m.instructions.getFirst(), list);
        }
    }

    final ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS);
    cn.accept(cw);
    return cw.toByteArray();
}