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:vazkii.quark.base.asm.ClassTransformer.java

License:Creative Commons License

private static byte[] transformRenderBoat(byte[] basicClass) {
    log("Transforming RenderBoat");
    MethodSignature sig = new MethodSignature("doRender", "func_188300_b", "b",
            "(Lnet/minecraft/entity/item/EntityBoat;DDDFF)V");

    return transform(basicClass, Pair.of(sig, combine((AbstractInsnNode node) -> { // Filter
        return (node.getOpcode() == Opcodes.INVOKEVIRTUAL || node.getOpcode() == Opcodes.INVOKEINTERFACE)
                && checkDesc(((MethodInsnNode) node).desc, "(Lnet/minecraft/entity/Entity;FFFFFF)V");
    }, (MethodNode method, AbstractInsnNode node) -> { // Action
        InsnList newInstructions = new InsnList();

        newInstructions.add(new VarInsnNode(Opcodes.ALOAD, 1));
        newInstructions.add(new VarInsnNode(Opcodes.FLOAD, 9));
        newInstructions.add(new MethodInsnNode(Opcodes.INVOKESTATIC, ASM_HOOKS, "renderBannerOnBoat",
                "(Lnet/minecraft/entity/item/EntityBoat;F)V"));

        method.instructions.insert(node, newInstructions);
        return true;
    })));//  w  ww . j  a v a  2  s. c o  m
}

From source file:vazkii.quark.base.asm.ClassTransformer.java

License:Creative Commons License

private static byte[] transformBlockPistonBase(byte[] basicClass) {
    log("Transforming BlockPistonBase");
    MethodSignature sig1 = new MethodSignature("doMove", "func_176319_a", "a",
            "(Lnet/minecraft/world/World;Lnet/minecraft/util/math/BlockPos;Lnet/minecraft/util/EnumFacing;Z)Z");
    MethodSignature sig2 = new MethodSignature("canPush", "func_185646_a", "a",
            "(Lnet/minecraft/block/state/IBlockState;Lnet/minecraft/world/World;Lnet/minecraft/util/math/BlockPos;Lnet/minecraft/util/EnumFacing;ZLnet/minecraft/util/EnumFacing;)Z");

    byte[] transClass = transform(basicClass, Pair.of(sig1, combine((AbstractInsnNode node) -> { // Filter
        return node.getOpcode() == Opcodes.ASTORE && ((VarInsnNode) node).var == 11;
    }, (MethodNode method, AbstractInsnNode node) -> { // Action
        InsnList newInstructions = new InsnList();

        newInstructions.add(new VarInsnNode(Opcodes.ALOAD, 1));
        newInstructions.add(new VarInsnNode(Opcodes.ALOAD, 2));
        newInstructions.add(new VarInsnNode(Opcodes.ALOAD, 6));
        newInstructions.add(new VarInsnNode(Opcodes.ALOAD, 8));
        newInstructions.add(new VarInsnNode(Opcodes.ALOAD, 11));
        newInstructions.add(new VarInsnNode(Opcodes.ILOAD, 4));
        newInstructions.add(new MethodInsnNode(Opcodes.INVOKESTATIC, ASM_HOOKS, "breakStuffWithSpikes",
                "(Lnet/minecraft/world/World;Lnet/minecraft/util/math/BlockPos;Ljava/util/List;Ljava/util/List;Lnet/minecraft/util/EnumFacing;Z)Z"));

        // recalculate the list and array sizes
        LabelNode label = new LabelNode();
        newInstructions.add(new JumpInsnNode(Opcodes.IFEQ, label));

        newInstructions.add(new VarInsnNode(Opcodes.ALOAD, 6));
        newInstructions.add(new MethodInsnNode(Opcodes.INVOKEINTERFACE, "java/util/List", "size", "()I"));
        newInstructions.add(new VarInsnNode(Opcodes.ALOAD, 8));
        newInstructions.add(new MethodInsnNode(Opcodes.INVOKEINTERFACE, "java/util/List", "size", "()I"));
        newInstructions.add(new InsnNode(Opcodes.IADD));
        newInstructions.add(new VarInsnNode(Opcodes.ISTORE, 9));
        newInstructions.add(new VarInsnNode(Opcodes.ILOAD, 9));

        AbstractInsnNode newNode = node.getPrevious();
        while (true) {
            if (newNode.getOpcode() == Opcodes.ANEWARRAY) {
                newInstructions.add(new TypeInsnNode(Opcodes.ANEWARRAY, ((TypeInsnNode) newNode).desc));
                break;
            }//from w  w  w. j av  a 2s  .c o  m
            newNode = newNode.getPrevious();
        }

        newInstructions.add(new VarInsnNode(Opcodes.ASTORE, 10));
        newInstructions.add(label);

        method.instructions.insert(node, newInstructions);
        return true;
    })));

    transClass = transform(transClass, Pair.of(sig2, combine((AbstractInsnNode node) -> { // Filter
        return node.getOpcode() == Opcodes.INVOKEVIRTUAL
                && ((MethodInsnNode) node).name.equals("hasTileEntity");
    }, (MethodNode method, AbstractInsnNode node) -> { // Action
        InsnList newInstructions = new InsnList();

        newInstructions.add(new VarInsnNode(Opcodes.ALOAD, 0));
        newInstructions.add(new MethodInsnNode(Opcodes.INVOKESTATIC, ASM_HOOKS, "shouldPistonMoveTE",
                "(ZLnet/minecraft/block/state/IBlockState;)Z"));

        method.instructions.insert(node, newInstructions);
        return true;
    })));

    return transClass;
}

From source file:vazkii.quark.base.asm.ClassTransformer.java

License:Creative Commons License

private static byte[] transformTransferStackInSlot(byte[] basicClass, int min, int max, String hook) {
    MethodSignature sig = new MethodSignature("transferStackInSlot", "func_82846_b", "b",
            "(Lnet/minecraft/entity/player/EntityPlayer;I)Lnet/minecraft/item/ItemStack;");

    bipushCount = 0;// w  ww .ja v  a  2  s  .  com
    return transform(basicClass, Pair.of(sig, combine((AbstractInsnNode node) -> { // Filte
        return node.getOpcode() == Opcodes.BIPUSH;
    }, (MethodNode method, AbstractInsnNode node) -> { // Action
        InsnList newInstructions = new InsnList();
        bipushCount++;

        if (bipushCount != min && bipushCount != max)
            return false;

        log("Adding invokestatic to " + ((IntInsnNode) node).operand + "/" + bipushCount);
        newInstructions.add(new MethodInsnNode(Opcodes.INVOKESTATIC, ASM_HOOKS, hook, "(I)I"));

        method.instructions.insert(node, newInstructions);
        return bipushCount == max;
    })));
}

From source file:vazkii.quark.base.asm.ClassTransformer.java

License:Creative Commons License

private static byte[] transformTileEntityPiston(byte[] basicClass) {
    log("Transforming TileEntityPiston");
    MethodSignature sig1 = new MethodSignature("clearPistonTileEntity", "func_145866_f", "j", "()V");
    MethodSignature sig2 = new MethodSignature("update", "func_73660_a", "e", "()V");

    MethodAction action = combine((AbstractInsnNode node) -> { // Filter
        return node.getOpcode() == Opcodes.INVOKEVIRTUAL && checkDesc(((MethodInsnNode) node).desc,
                "(Lnet/minecraft/util/math/BlockPos;Lnet/minecraft/block/state/IBlockState;I)Z");
    }, (MethodNode method, AbstractInsnNode node) -> { // Action
        InsnList newInstructions = new InsnList();

        newInstructions.add(new MethodInsnNode(Opcodes.INVOKESTATIC, ASM_HOOKS, "setPistonBlock",
                "(Lnet/minecraft/world/World;Lnet/minecraft/util/math/BlockPos;Lnet/minecraft/block/state/IBlockState;I)Z"));

        method.instructions.insert(node, newInstructions);
        method.instructions.remove(node);

        return true;
    });//from w  ww .  j a  v a  2s  .  c  o  m

    byte[] transClass = transform(basicClass, Pair.of(sig1, action));
    return transform(transClass, Pair.of(sig2, action));
}

From source file:vazkii.quark.base.asm.ClassTransformer.java

License:Creative Commons License

private static byte[] transformTileEntityPistonRenderer(byte[] basicClass) {
    log("Transforming TileEntityPistonRenderer");
    MethodSignature sig = new MethodSignature("renderStateModel", "func_188186_a", "a",
            "(Lnet/minecraft/util/math/BlockPos;Lnet/minecraft/block/state/IBlockState;Lnet/minecraft/client/renderer/BufferBuilder;Lnet/minecraft/world/World;Z)Z");

    return transform(basicClass, Pair.of(sig, combine((AbstractInsnNode node) -> { // Filter
        return true;
    }, (MethodNode method, AbstractInsnNode node) -> { // Action
        InsnList newInstructions = new InsnList();

        for (int i = 1; i <= 4; i++)
            newInstructions.add(new VarInsnNode(Opcodes.ALOAD, i));
        newInstructions.add(new VarInsnNode(Opcodes.ILOAD, 5));
        newInstructions.add(new MethodInsnNode(Opcodes.INVOKESTATIC, ASM_HOOKS, "renderPistonBlock",
                "(Lnet/minecraft/util/math/BlockPos;Lnet/minecraft/block/state/IBlockState;Lnet/minecraft/client/renderer/BufferBuilder;Lnet/minecraft/world/World;Z)Z"));
        newInstructions.add(new InsnNode(Opcodes.IRETURN));

        method.instructions = newInstructions;
        return true;
    })));/*w w w. j  a va 2  s . co  m*/
}

From source file:vazkii.quark.base.asm.ClassTransformer.java

License:Creative Commons License

private static byte[] transformWorldServer(byte[] basicClass) {
    log("Transforming WorldServer");
    MethodSignature sig = new MethodSignature("areAllPlayersAsleep", "func_73056_e", "g", "()Z");

    return transform(basicClass, Pair.of(sig, combine((AbstractInsnNode node) -> { // Filter
        return true;
    }, (MethodNode method, AbstractInsnNode node) -> { // Action
        InsnList newInstructions = new InsnList();

        newInstructions.add(new VarInsnNode(Opcodes.ALOAD, 0));
        newInstructions.add(new MethodInsnNode(Opcodes.INVOKESTATIC, ASM_HOOKS, "isEveryoneAsleep",
                "(Lnet/minecraft/world/World;)I"));
        newInstructions.add(new InsnNode(Opcodes.DUP));
        LabelNode label = new LabelNode();
        newInstructions.add(new JumpInsnNode(Opcodes.IFEQ, label));
        newInstructions.add(new InsnNode(Opcodes.ICONST_1));
        newInstructions.add(new InsnNode(Opcodes.ISUB));
        newInstructions.add(new InsnNode(Opcodes.IRETURN));
        newInstructions.add(label);// ww  w . j a  va2s  . c o  m

        method.instructions.insertBefore(node, newInstructions);
        return true;
    })));
}

From source file:vazkii.quark.base.asm.ClassTransformer.java

License:Creative Commons License

private static byte[] transformBlockModelRenderer(byte[] basicClass) {
    log("Transforming BlockModelRenderer");
    MethodSignature sig1 = new MethodSignature("renderQuadsFlat", "func_187496_a", "a",
            "(Lnet/minecraft/world/IBlockAccess;Lnet/minecraft/block/state/IBlockState;Lnet/minecraft/util/math/BlockPos;IZLnet/minecraft/client/renderer/BufferBuilder;Ljava/util/List;Ljava/util/BitSet;)V");

    if (hasOptifine(sig1.toString()))
        return basicClass;

    return transform(basicClass, Pair.of(sig1, combine((AbstractInsnNode node) -> { // Filter
        return node.getOpcode() == Opcodes.INVOKEVIRTUAL && checkDesc(((MethodInsnNode) node).desc, "(DDD)V");
    }, (MethodNode method, AbstractInsnNode node) -> { // Action
        InsnList newInstructions = new InsnList();

        newInstructions.add(new VarInsnNode(Opcodes.ALOAD, 1));
        newInstructions.add(new VarInsnNode(Opcodes.ALOAD, 2));
        newInstructions.add(new VarInsnNode(Opcodes.ALOAD, 3));
        newInstructions.add(new VarInsnNode(Opcodes.ALOAD, 6));
        newInstructions.add(new VarInsnNode(Opcodes.ALOAD, 18));
        newInstructions.add(new VarInsnNode(Opcodes.ILOAD, 4));
        newInstructions.add(new MethodInsnNode(Opcodes.INVOKESTATIC, ASM_HOOKS, "putColorsFlat",
                "(Lnet/minecraft/world/IBlockAccess;Lnet/minecraft/block/state/IBlockState;Lnet/minecraft/util/math/BlockPos;Lnet/minecraft/client/renderer/BufferBuilder;Lnet/minecraft/client/renderer/block/model/BakedQuad;I)V"));

        method.instructions.insertBefore(node, newInstructions);
        return true;
    })));/*from  w  w  w  .j av a2 s .c  om*/
}

From source file:vazkii.quark.base.asm.ClassTransformer.java

License:Creative Commons License

private static byte[] transformRender(byte[] basicClass) {
    log("Transforming Render");

    MethodSignature sig = new MethodSignature("renderEntityOnFire", "func_76977_a", "a",
            "(Lnet/minecraft/entity/Entity;DDDF)V");

    return transform(basicClass, Pair.of(sig, combine((AbstractInsnNode node) -> { // Filter
        return true;
    }, (MethodNode method, AbstractInsnNode node) -> { // Action
        InsnList newInstructions = new InsnList();

        newInstructions.add(new VarInsnNode(Opcodes.ALOAD, 1));
        newInstructions.add(new VarInsnNode(Opcodes.DLOAD, 2));
        newInstructions.add(new VarInsnNode(Opcodes.DLOAD, 4));
        newInstructions.add(new VarInsnNode(Opcodes.DLOAD, 6));
        newInstructions.add(new VarInsnNode(Opcodes.FLOAD, 8));
        newInstructions.add(new MethodInsnNode(Opcodes.INVOKESTATIC, ASM_HOOKS, "renderFire",
                "(Lnet/minecraft/entity/Entity;DDDF)Z"));
        LabelNode label = new LabelNode();
        newInstructions.add(new JumpInsnNode(Opcodes.IFEQ, label));
        newInstructions.add(new InsnNode(Opcodes.RETURN));
        newInstructions.add(label);/*  w  w w  .j  a v  a 2s .  com*/

        method.instructions.insertBefore(node, newInstructions);
        return true;
    })));
}