Search in sources :

Example 41 with AbstractInsnNode

use of org.objectweb.asm.tree.AbstractInsnNode in project Random-Things by lumien231.

the class ClassTransformer method patchEntitySlime.

private byte[] patchEntitySlime(byte[] basicClass) {
    ClassNode classNode = new ClassNode();
    ClassReader classReader = new ClassReader(basicClass);
    classReader.accept(classNode, 0);
    logger.log(Level.DEBUG, "Found EntitySlime Class: " + classNode.name);
    MethodNode getCanSpawnHere = null;
    for (MethodNode mn : classNode.methods) {
        if (mn.name.equals(MCPNames.method("func_70601_bi"))) {
            getCanSpawnHere = mn;
            break;
        }
    }
    if (getCanSpawnHere != null) {
        logger.log(Level.DEBUG, " - Found getCanSpawnHere");
        for (int i = 0; i < getCanSpawnHere.instructions.size(); i++) {
            AbstractInsnNode ain = getCanSpawnHere.instructions.get(i);
            if (ain instanceof FieldInsnNode) {
                FieldInsnNode fin = (FieldInsnNode) ain;
                if (fin.name.equals("PEACEFUL")) {
                    logger.log(Level.DEBUG, " - Found Insertion Point");
                    AbstractInsnNode skip = getCanSpawnHere.instructions.get(i + 1);
                    LabelNode l0 = new LabelNode(new Label());
                    InsnList toInsert = new InsnList();
                    toInsert.add(new VarInsnNode(ALOAD, 0));
                    toInsert.add(new FieldInsnNode(Opcodes.GETFIELD, "net/minecraft/entity/monster/EntitySlime", MCPNames.field("field_70170_p"), "Lnet/minecraft/world/World;"));
                    toInsert.add(new VarInsnNode(ALOAD, 2));
                    toInsert.add(new MethodInsnNode(INVOKESTATIC, asmHandler, "overrideSlimeChunk", "(Lnet/minecraft/world/World;Lnet/minecraft/world/chunk/Chunk;)I", false));
                    toInsert.add(new InsnNode(DUP));
                    toInsert.add(new JumpInsnNode(IFLT, l0));
                    toInsert.add(new InsnNode(IRETURN));
                    toInsert.add(l0);
                    toInsert.add(new InsnNode(POP));
                    getCanSpawnHere.instructions.insert(skip, toInsert);
                }
            }
        }
    }
    CustomClassWriter writer = new CustomClassWriter(ClassWriter.COMPUTE_MAXS | ClassWriter.COMPUTE_FRAMES);
    classNode.accept(writer);
    return writer.toByteArray();
}
Also used : LabelNode(org.objectweb.asm.tree.LabelNode) ClassNode(org.objectweb.asm.tree.ClassNode) Label(org.objectweb.asm.Label) FieldInsnNode(org.objectweb.asm.tree.FieldInsnNode) AbstractInsnNode(org.objectweb.asm.tree.AbstractInsnNode) InsnList(org.objectweb.asm.tree.InsnList) FieldInsnNode(org.objectweb.asm.tree.FieldInsnNode) VarInsnNode(org.objectweb.asm.tree.VarInsnNode) MethodInsnNode(org.objectweb.asm.tree.MethodInsnNode) TypeInsnNode(org.objectweb.asm.tree.TypeInsnNode) JumpInsnNode(org.objectweb.asm.tree.JumpInsnNode) LdcInsnNode(org.objectweb.asm.tree.LdcInsnNode) AbstractInsnNode(org.objectweb.asm.tree.AbstractInsnNode) InsnNode(org.objectweb.asm.tree.InsnNode) MethodNode(org.objectweb.asm.tree.MethodNode) MethodInsnNode(org.objectweb.asm.tree.MethodInsnNode) ClassReader(org.objectweb.asm.ClassReader) JumpInsnNode(org.objectweb.asm.tree.JumpInsnNode) VarInsnNode(org.objectweb.asm.tree.VarInsnNode)

Example 42 with AbstractInsnNode

use of org.objectweb.asm.tree.AbstractInsnNode in project Random-Things by lumien231.

the class ClassTransformer method patchRenderItem.

private byte[] patchRenderItem(byte[] basicClass) {
    ClassNode classNode = new ClassNode();
    ClassReader classReader = new ClassReader(basicClass);
    classReader.accept(classNode, 0);
    logger.log(Level.DEBUG, "Found RenderItem Class: " + classNode.name);
    MethodNode renderEffect = null;
    MethodNode renderItem = null;
    MethodNode renderQuads = null;
    for (MethodNode mn : classNode.methods) {
        if (mn.name.equals(MCPNames.method("func_191966_a"))) {
            renderEffect = mn;
        } else if (mn.name.equals(MCPNames.method("func_180454_a")) && mn.desc.equals("(Lnet/minecraft/item/ItemStack;Lnet/minecraft/client/renderer/block/model/IBakedModel;)V")) {
            renderItem = mn;
        } else if (mn.name.equals(MCPNames.method("func_191970_a"))) {
            renderQuads = mn;
        }
    }
    if (renderEffect != null) {
        logger.log(Level.DEBUG, "- Found renderEffect (1/3)");
        renderEffect.instructions.insert(new MethodInsnNode(INVOKESTATIC, asmHandler, "preEnchantment", "()V", false));
        for (int i = 0; i < renderEffect.instructions.size(); i++) {
            AbstractInsnNode ain = renderEffect.instructions.get(i);
            if (ain instanceof LdcInsnNode) {
                LdcInsnNode lin = (LdcInsnNode) ain;
                if (lin.cst.equals(new Integer(-8372020))) {
                    logger.log(Level.DEBUG, "- Found Texture Binding");
                    renderEffect.instructions.insert(lin, new MethodInsnNode(INVOKESTATIC, asmHandler, "enchantmentColorHook", "(I)I", false));
                }
            } else if (ain.getOpcode() == RETURN) {
                renderEffect.instructions.insertBefore(ain, new MethodInsnNode(INVOKESTATIC, asmHandler, "postEnchantment", "()V", false));
                i++;
            }
        }
    }
    if (renderItem != null) {
        boolean found = false;
        logger.log(Level.DEBUG, "- Found renderItem (2/3) (" + renderItem.desc + ")");
        for (int i = 0; i < renderItem.instructions.size(); i++) {
            AbstractInsnNode ain = renderItem.instructions.get(i);
            if (ain instanceof MethodInsnNode) {
                MethodInsnNode min = (MethodInsnNode) ain;
                if (!found) {
                    if (min.name.equals(MCPNames.method("func_191966_a"))) {
                        logger.log(Level.DEBUG, "- Found renderEffect calling");
                        InsnList toInsert = new InsnList();
                        toInsert.add(new VarInsnNode(ALOAD, 1));
                        toInsert.add(new FieldInsnNode(PUTSTATIC, asmHandler, "currentlyRendering", "Lnet/minecraft/item/ItemStack;"));
                        renderItem.instructions.insertBefore(min, toInsert);
                        toInsert = new InsnList();
                        toInsert.add(new InsnNode(ACONST_NULL));
                        toInsert.add(new FieldInsnNode(PUTSTATIC, asmHandler, "currentlyRendering", "Lnet/minecraft/item/ItemStack;"));
                        renderItem.instructions.insert(min, toInsert);
                        found = true;
                    }
                }
                if (min.name.equals(MCPNames.method("func_179022_a"))) {
                    LabelNode l1 = new LabelNode(new Label());
                    LabelNode l2 = new LabelNode(new Label());
                    logger.log(Level.DEBUG, "- Inserting TE Item Renderer");
                    InsnList insertBefore = new InsnList();
                    insertBefore.add(new FieldInsnNode(GETSTATIC, "lumien/randomthings/client/RandomThingsTEItemRenderer", "instance", "Llumien/randomthings/client/RandomThingsTEItemRenderer;"));
                    insertBefore.add(new VarInsnNode(ALOAD, 1));
                    insertBefore.add(new MethodInsnNode(INVOKEVIRTUAL, "lumien/randomthings/client/RandomThingsTEItemRenderer", "renderByItem", "(Lnet/minecraft/item/ItemStack;)Z", false));
                    insertBefore.add(new JumpInsnNode(IFEQ, l2));
                    insertBefore.add(new InsnNode(POP));
                    insertBefore.add(new InsnNode(POP));
                    insertBefore.add(new JumpInsnNode(GOTO, l1));
                    insertBefore.add(l2);
                    InsnList insertAfter = new InsnList();
                    insertAfter.add(l1);
                    renderItem.instructions.insertBefore(min, insertBefore);
                    renderItem.instructions.insert(min, insertAfter);
                    i += 8;
                }
            }
        }
    }
    if (renderQuads != null) {
        logger.log(Level.DEBUG, "- Found renderQuads (3/3) (" + renderQuads.desc + ")");
        String luminousHandler = "lumien/randomthings/handler/LuminousHandler";
        InsnList startInsrt = new InsnList();
        startInsrt.add(new VarInsnNode(ALOAD, 4));
        startInsrt.add(new VarInsnNode(ALOAD, 1));
        startInsrt.add(new MethodInsnNode(INVOKESTATIC, luminousHandler, "luminousHookStart", "(Lnet/minecraft/item/ItemStack;Lnet/minecraft/client/renderer/BufferBuilder;)V", false));
        renderQuads.instructions.insert(startInsrt);
        for (int i = 0; i < renderQuads.instructions.size(); i++) {
            AbstractInsnNode ain = renderQuads.instructions.get(i);
            if (ain instanceof MethodInsnNode) {
                MethodInsnNode min = (MethodInsnNode) ain;
                if (min.name.equals("renderQuadColor")) {
                    InsnList toInsert = new InsnList();
                    toInsert.add(new VarInsnNode(ALOAD, 8));
                    toInsert.add(new MethodInsnNode(INVOKEVIRTUAL, "net/minecraft/client/renderer/block/model/BakedQuad", MCPNames.method("func_178211_c"), "()I", false));
                    toInsert.add(new MethodInsnNode(INVOKESTATIC, luminousHandler, "luminousHookPre", "(I)V", false));
                    toInsert.add(new InsnNode(POP));
                    toInsert.add(new VarInsnNode(ALOAD, 4));
                    toInsert.add(new VarInsnNode(ILOAD, 9));
                    toInsert.add(new MethodInsnNode(INVOKESTATIC, asmHandler, "getColorFromItemStack", "(Lnet/minecraft/item/ItemStack;I)I", false));
                    renderQuads.instructions.insertBefore(min, toInsert);
                    i += 7;
                    renderQuads.instructions.insert(min, new MethodInsnNode(INVOKESTATIC, luminousHandler, "luminousHookPost", "()V", false));
                }
            } else if (ain.getOpcode() == RETURN) {
                renderQuads.instructions.insertBefore(ain, new MethodInsnNode(INVOKESTATIC, luminousHandler, "luminousHookEnd", "()V", false));
                i++;
            }
        }
    }
    CustomClassWriter writer = new CustomClassWriter(ClassWriter.COMPUTE_MAXS | ClassWriter.COMPUTE_FRAMES);
    classNode.accept(writer);
    return writer.toByteArray();
}
Also used : LabelNode(org.objectweb.asm.tree.LabelNode) ClassNode(org.objectweb.asm.tree.ClassNode) Label(org.objectweb.asm.Label) FieldInsnNode(org.objectweb.asm.tree.FieldInsnNode) AbstractInsnNode(org.objectweb.asm.tree.AbstractInsnNode) InsnList(org.objectweb.asm.tree.InsnList) FieldInsnNode(org.objectweb.asm.tree.FieldInsnNode) VarInsnNode(org.objectweb.asm.tree.VarInsnNode) MethodInsnNode(org.objectweb.asm.tree.MethodInsnNode) TypeInsnNode(org.objectweb.asm.tree.TypeInsnNode) JumpInsnNode(org.objectweb.asm.tree.JumpInsnNode) LdcInsnNode(org.objectweb.asm.tree.LdcInsnNode) AbstractInsnNode(org.objectweb.asm.tree.AbstractInsnNode) InsnNode(org.objectweb.asm.tree.InsnNode) LdcInsnNode(org.objectweb.asm.tree.LdcInsnNode) MethodNode(org.objectweb.asm.tree.MethodNode) MethodInsnNode(org.objectweb.asm.tree.MethodInsnNode) ClassReader(org.objectweb.asm.ClassReader) JumpInsnNode(org.objectweb.asm.tree.JumpInsnNode) VarInsnNode(org.objectweb.asm.tree.VarInsnNode)

Example 43 with AbstractInsnNode

use of org.objectweb.asm.tree.AbstractInsnNode in project Random-Things by lumien231.

the class ClassTransformer method patchLayerArmorBase.

private byte[] patchLayerArmorBase(byte[] basicClass) {
    ClassNode classNode = new ClassNode();
    ClassReader classReader = new ClassReader(basicClass);
    classReader.accept(classNode, 0);
    logger.log(Level.DEBUG, "Found LayerArmorBase Class: " + classNode.name);
    // Where the effect is rendered
    MethodNode renderEnchantedGlint = null;
    // Getting ItemStack from here
    MethodNode renderArmorLayer = null;
    for (MethodNode mn : classNode.methods) {
        if (mn.name.equals(MCPNames.method("func_188364_a"))) {
            renderEnchantedGlint = mn;
        } else if (mn.name.equals(MCPNames.method("func_188361_a"))) {
            renderArmorLayer = mn;
        }
    }
    if (renderEnchantedGlint != null) {
        logger.log(Level.DEBUG, "- Found renderEnchantedGlint (Effect Rendering 1/2)");
        renderEnchantedGlint.instructions.insert(new MethodInsnNode(INVOKESTATIC, asmHandler, "preEnchantment", "()V", false));
        for (int i = 0; i < renderEnchantedGlint.instructions.size(); i++) {
            AbstractInsnNode ain = renderEnchantedGlint.instructions.get(i);
            if (ain instanceof MethodInsnNode) {
                MethodInsnNode min = (MethodInsnNode) ain;
                if (min.owner.equals("net/minecraft/client/renderer/GlStateManager") && min.name.equals(MCPNames.method("func_179131_c"))) {
                    renderEnchantedGlint.instructions.insert(min, new MethodInsnNode(INVOKESTATIC, asmHandler, "armorEnchantmentHook", "()V", false));
                }
            } else if (ain.getOpcode() == RETURN) {
                renderEnchantedGlint.instructions.insertBefore(ain, new MethodInsnNode(INVOKESTATIC, asmHandler, "postEnchantment", "()V", false));
                i++;
            }
        }
    }
    int renderCounter = 0;
    if (renderArmorLayer != null) {
        logger.log(Level.DEBUG, "- Found renderArmorLayer (ItemStack Information & Armor Coloring 2/2)");
        for (int i = 0; i < renderArmorLayer.instructions.size(); i++) {
            AbstractInsnNode ain = renderArmorLayer.instructions.get(i);
            if (ain instanceof MethodInsnNode) {
                MethodInsnNode min = (MethodInsnNode) ain;
                if (min.name.equals(MCPNames.method("func_188364_a"))) {
                    logger.log(Level.DEBUG, "- Set currentlyRendering");
                    InsnList toInsert = new InsnList();
                    toInsert.add(new VarInsnNode(ALOAD, 10));
                    toInsert.add(new FieldInsnNode(PUTSTATIC, asmHandler, "currentlyRendering", "Lnet/minecraft/item/ItemStack;"));
                    renderArmorLayer.instructions.insertBefore(min, toInsert);
                    toInsert = new InsnList();
                    toInsert.add(new InsnNode(ACONST_NULL));
                    toInsert.add(new FieldInsnNode(PUTSTATIC, asmHandler, "currentlyRendering", "Lnet/minecraft/item/ItemStack;"));
                    renderArmorLayer.instructions.insert(min, toInsert);
                    i += 2;
                }
                if (min.name.equals(MCPNames.method("func_78088_a"))) {
                    if (renderCounter == 1) {
                        logger.log(Level.DEBUG, "- Found render");
                        InsnList toInsert = new InsnList();
                        toInsert.add(new VarInsnNode(ALOAD, 10));
                        toInsert.add(new MethodInsnNode(INVOKESTATIC, asmHandler, "armorColorHook", "(Lnet/minecraft/item/ItemStack;)V", false));
                        renderArmorLayer.instructions.insertBefore(min, toInsert);
                    }
                    renderCounter++;
                }
            }
        }
    }
    CustomClassWriter writer = new CustomClassWriter(ClassWriter.COMPUTE_MAXS | ClassWriter.COMPUTE_FRAMES);
    classNode.accept(writer);
    return writer.toByteArray();
}
Also used : ClassNode(org.objectweb.asm.tree.ClassNode) FieldInsnNode(org.objectweb.asm.tree.FieldInsnNode) VarInsnNode(org.objectweb.asm.tree.VarInsnNode) MethodInsnNode(org.objectweb.asm.tree.MethodInsnNode) TypeInsnNode(org.objectweb.asm.tree.TypeInsnNode) JumpInsnNode(org.objectweb.asm.tree.JumpInsnNode) LdcInsnNode(org.objectweb.asm.tree.LdcInsnNode) AbstractInsnNode(org.objectweb.asm.tree.AbstractInsnNode) InsnNode(org.objectweb.asm.tree.InsnNode) MethodNode(org.objectweb.asm.tree.MethodNode) MethodInsnNode(org.objectweb.asm.tree.MethodInsnNode) ClassReader(org.objectweb.asm.ClassReader) FieldInsnNode(org.objectweb.asm.tree.FieldInsnNode) AbstractInsnNode(org.objectweb.asm.tree.AbstractInsnNode) InsnList(org.objectweb.asm.tree.InsnList) VarInsnNode(org.objectweb.asm.tree.VarInsnNode)

Example 44 with AbstractInsnNode

use of org.objectweb.asm.tree.AbstractInsnNode in project Random-Things by lumien231.

the class ClassTransformer method patchVertexLighterFlat.

private byte[] patchVertexLighterFlat(byte[] basicClass) {
    ClassNode classNode = new ClassNode();
    ClassReader classReader = new ClassReader(basicClass);
    classReader.accept(classNode, 0);
    logger.log(Level.DEBUG, "Found VertexLighterFlat Class: " + classNode.name);
    MethodNode processQuad = null;
    for (MethodNode mn : classNode.methods) {
        if (mn.name.equals("processQuad")) {
            processQuad = mn;
            break;
        }
    }
    if (processQuad != null) {
        logger.log(Level.DEBUG, " - Found processQuad");
        InsnList resetList = new InsnList();
        resetList.add(new VarInsnNode(ALOAD, 0));
        resetList.add(new InsnNode(ICONST_0));
        resetList.add(new FieldInsnNode(PUTFIELD, "net/minecraftforge/client/model/pipeline/VertexLighterFlat", "rtFullBright", "Z"));
        processQuad.instructions.insert(resetList);
        AbstractInsnNode tintTarget = null;
        int lightMapTarget = 0;
        AbstractInsnNode updateColorTarget = null;
        LabelNode firstLabel = null;
        LabelNode lastLabel = null;
        for (int i = 0; i < processQuad.instructions.size(); i++) {
            AbstractInsnNode ain = processQuad.instructions.get(i);
            if (ain instanceof VarInsnNode) {
                VarInsnNode vin = (VarInsnNode) ain;
                if (vin.var == 5 && (processQuad.instructions.get(i - 1) instanceof MethodInsnNode)) {
                    tintTarget = vin;
                }
            } else if (ain instanceof LabelNode) {
                LabelNode ln = (LabelNode) ain;
                if (firstLabel == null) {
                    firstLabel = ln;
                }
                lastLabel = ln;
            } else if (ain instanceof MethodInsnNode) {
                MethodInsnNode min = (MethodInsnNode) ain;
                if (min.name.equals(MCPNames.method("func_177369_e"))) {
                    lightMapTarget = i;
                } else if (min.name.equals("updateColor")) {
                    updateColorTarget = min;
                }
            }
        }
        FieldNode fullBrightField = new FieldNode(ACC_PRIVATE, "rtFullBright", "Z", null, false);
        classNode.fields.add(fullBrightField);
        if (lightMapTarget != 0) {
            logger.log(Level.DEBUG, " - Found patch target (lightmap) (1/4)");
            for (int i = lightMapTarget; i < processQuad.instructions.size(); i++) {
                AbstractInsnNode ain = processQuad.instructions.get(i);
                if (ain instanceof InsnNode) {
                    InsnNode in = (InsnNode) ain;
                    if (in.getOpcode() == AALOAD) {
                        logger.log(Level.DEBUG, " - Found lightmap array (2/4)");
                        LabelNode l0 = new LabelNode(new Label());
                        InsnList toInsert = new InsnList();
                        toInsert.add(new VarInsnNode(ALOAD, 0));
                        toInsert.add(new FieldInsnNode(GETFIELD, "net/minecraftforge/client/model/pipeline/VertexLighterFlat", "rtFullBright", "Z"));
                        toInsert.add(new JumpInsnNode(IFEQ, l0));
                        toInsert.add(new InsnNode(POP));
                        toInsert.add(new FieldInsnNode(GETSTATIC, "lumien/randomthings/lib/Constants", "FULLBRIGHT_OVERRIDE", "[F"));
                        toInsert.add(l0);
                        processQuad.instructions.insert(in, toInsert);
                        break;
                    }
                }
            }
        }
        if (tintTarget != null) {
            logger.log(Level.DEBUG, " - Found tintTarget (3/4");
            LabelNode l0 = new LabelNode(new Label());
            LabelNode l1 = new LabelNode(new Label());
            InsnList toInsert = new InsnList();
            toInsert.add(new VarInsnNode(ALOAD, 0));
            toInsert.add(new FieldInsnNode(GETFIELD, "net/minecraftforge/client/model/pipeline/VertexLighterFlat", "blockInfo", "Lnet/minecraftforge/client/model/pipeline/BlockInfo;"));
            toInsert.add(new MethodInsnNode(INVOKEVIRTUAL, "net/minecraftforge/client/model/pipeline/BlockInfo", "getState", "()Lnet/minecraft/block/state/IBlockState;", false));
            toInsert.add(new MethodInsnNode(INVOKEINTERFACE, "net/minecraft/block/state/IBlockState", MCPNames.method("func_177230_c"), "()Lnet/minecraft/block/Block;", true));
            toInsert.add(new TypeInsnNode(INSTANCEOF, "lumien/randomthings/lib/ILuminousBlock"));
            toInsert.add(new JumpInsnNode(IFEQ, l0));
            toInsert.add(new VarInsnNode(ALOAD, 0));
            toInsert.add(new FieldInsnNode(GETFIELD, "net/minecraftforge/client/model/pipeline/VertexLighterFlat", "blockInfo", "Lnet/minecraftforge/client/model/pipeline/BlockInfo;"));
            toInsert.add(new MethodInsnNode(INVOKEVIRTUAL, "net/minecraftforge/client/model/pipeline/BlockInfo", "getState", "()Lnet/minecraft/block/state/IBlockState;", false));
            toInsert.add(new InsnNode(DUP));
            toInsert.add(new MethodInsnNode(INVOKEINTERFACE, "net/minecraft/block/state/IBlockState", MCPNames.method("func_177230_c"), "()Lnet/minecraft/block/Block;", true));
            toInsert.add(new InsnNode(SWAP));
            toInsert.add(new VarInsnNode(ALOAD, 0));
            toInsert.add(new FieldInsnNode(GETFIELD, "net/minecraftforge/client/model/pipeline/VertexLighterFlat", "tint", "I"));
            toInsert.add(new MethodInsnNode(INVOKEINTERFACE, "lumien/randomthings/lib/ILuminousBlock", "shouldGlow", "(Lnet/minecraft/block/state/IBlockState;I)Z", true));
            toInsert.add(new JumpInsnNode(IFEQ, l0));
            toInsert.add(new VarInsnNode(ALOAD, 0));
            toInsert.add(new InsnNode(ICONST_0));
            toInsert.add(new FieldInsnNode(PUTFIELD, "net/minecraftforge/client/model/pipeline/VertexLighterFlat", "diffuse", "Z"));
            toInsert.add(new VarInsnNode(ALOAD, 0));
            toInsert.add(new InsnNode(ICONST_1));
            toInsert.add(new FieldInsnNode(PUTFIELD, "net/minecraftforge/client/model/pipeline/VertexLighterFlat", "rtFullBright", "Z"));
            toInsert.add(l0);
            processQuad.instructions.insertBefore(tintTarget, toInsert);
        }
        if (updateColorTarget != null) {
            logger.log(Level.DEBUG, " - Found updateColor target (tint) (4/4)");
            LabelNode l0 = new LabelNode(new Label());
            LabelNode l1 = new LabelNode(new Label());
            InsnList toInsert = new InsnList();
            toInsert.add(new VarInsnNode(ALOAD, 0));
            toInsert.add(new FieldInsnNode(GETFIELD, "net/minecraftforge/client/model/pipeline/VertexLighterFlat", "rtFullBright", "Z"));
            toInsert.add(new JumpInsnNode(IFEQ, l1));
            toInsert.add(new MethodInsnNode(INVOKESTATIC, asmHandler, "updateColor", "([F[FFFFFI)V", false));
            toInsert.add(new InsnNode(POP));
            toInsert.add(new JumpInsnNode(GOTO, l0));
            toInsert.add(l1);
            processQuad.instructions.insertBefore(updateColorTarget, toInsert);
            processQuad.instructions.insert(updateColorTarget, l0);
        }
    }
    CustomClassWriter writer = new CustomClassWriter(ClassWriter.COMPUTE_MAXS | ClassWriter.COMPUTE_FRAMES);
    classNode.accept(writer);
    try {
        byte[] result = writer.toByteArray();
        return result;
    } catch (Exception e) {
        e.printStackTrace();
        return basicClass;
    }
}
Also used : LabelNode(org.objectweb.asm.tree.LabelNode) ClassNode(org.objectweb.asm.tree.ClassNode) FieldNode(org.objectweb.asm.tree.FieldNode) Label(org.objectweb.asm.Label) FieldInsnNode(org.objectweb.asm.tree.FieldInsnNode) TypeInsnNode(org.objectweb.asm.tree.TypeInsnNode) InsnList(org.objectweb.asm.tree.InsnList) AbstractInsnNode(org.objectweb.asm.tree.AbstractInsnNode) FieldInsnNode(org.objectweb.asm.tree.FieldInsnNode) VarInsnNode(org.objectweb.asm.tree.VarInsnNode) MethodInsnNode(org.objectweb.asm.tree.MethodInsnNode) TypeInsnNode(org.objectweb.asm.tree.TypeInsnNode) JumpInsnNode(org.objectweb.asm.tree.JumpInsnNode) LdcInsnNode(org.objectweb.asm.tree.LdcInsnNode) AbstractInsnNode(org.objectweb.asm.tree.AbstractInsnNode) InsnNode(org.objectweb.asm.tree.InsnNode) MethodNode(org.objectweb.asm.tree.MethodNode) MethodInsnNode(org.objectweb.asm.tree.MethodInsnNode) ClassReader(org.objectweb.asm.ClassReader) JumpInsnNode(org.objectweb.asm.tree.JumpInsnNode) VarInsnNode(org.objectweb.asm.tree.VarInsnNode)

Example 45 with AbstractInsnNode

use of org.objectweb.asm.tree.AbstractInsnNode in project Random-Things by lumien231.

the class ClassTransformer method patchOceanMonument.

private byte[] patchOceanMonument(byte[] basicClass) {
    ClassNode classNode = new ClassNode();
    ClassReader classReader = new ClassReader(basicClass);
    classReader.accept(classNode, 0);
    logger.log(Level.DEBUG, "Found MonumentCoreRoom Class: " + classNode.name);
    MethodNode addComponentParts = null;
    for (MethodNode mn : classNode.methods) {
        if (mn.name.equals(MCPNames.method("func_74875_a"))) {
            addComponentParts = mn;
        }
    }
    if (addComponentParts != null) {
        logger.log(Level.DEBUG, " - Found addComponentParts");
        for (int i = 0; i < addComponentParts.instructions.size(); i++) {
            AbstractInsnNode ain = addComponentParts.instructions.get(i);
            if (ain instanceof InsnNode) {
                InsnNode in = (InsnNode) ain;
                if (in.getOpcode() == Opcodes.IRETURN) {
                    logger.log(Level.DEBUG, " - Patched addComponentParts");
                    AbstractInsnNode before = addComponentParts.instructions.get(i - 1);
                    InsnList toInsert = new InsnList();
                    toInsert.add(new VarInsnNode(Opcodes.ALOAD, 1));
                    toInsert.add(new VarInsnNode(Opcodes.ALOAD, 2));
                    toInsert.add(new VarInsnNode(Opcodes.ALOAD, 3));
                    toInsert.add(new VarInsnNode(Opcodes.ALOAD, 0));
                    toInsert.add(new MethodInsnNode(Opcodes.INVOKESTATIC, "lumien/randomthings/worldgen/WorldGenOceanChest", "addComponentParts", "(Lnet/minecraft/world/World;Ljava/util/Random;Lnet/minecraft/world/gen/structure/StructureBoundingBox;Lnet/minecraft/world/gen/structure/StructureOceanMonumentPieces$MonumentCoreRoom;)V", false));
                    i += 5;
                    addComponentParts.instructions.insertBefore(before, toInsert);
                }
            }
        }
    }
    CustomClassWriter writer = new CustomClassWriter(ClassWriter.COMPUTE_MAXS | ClassWriter.COMPUTE_FRAMES);
    classNode.accept(writer);
    return writer.toByteArray();
}
Also used : ClassNode(org.objectweb.asm.tree.ClassNode) FieldInsnNode(org.objectweb.asm.tree.FieldInsnNode) VarInsnNode(org.objectweb.asm.tree.VarInsnNode) MethodInsnNode(org.objectweb.asm.tree.MethodInsnNode) TypeInsnNode(org.objectweb.asm.tree.TypeInsnNode) JumpInsnNode(org.objectweb.asm.tree.JumpInsnNode) LdcInsnNode(org.objectweb.asm.tree.LdcInsnNode) AbstractInsnNode(org.objectweb.asm.tree.AbstractInsnNode) InsnNode(org.objectweb.asm.tree.InsnNode) MethodNode(org.objectweb.asm.tree.MethodNode) MethodInsnNode(org.objectweb.asm.tree.MethodInsnNode) ClassReader(org.objectweb.asm.ClassReader) AbstractInsnNode(org.objectweb.asm.tree.AbstractInsnNode) InsnList(org.objectweb.asm.tree.InsnList) VarInsnNode(org.objectweb.asm.tree.VarInsnNode)

Aggregations

AbstractInsnNode (org.objectweb.asm.tree.AbstractInsnNode)185 MethodInsnNode (org.objectweb.asm.tree.MethodInsnNode)68 MethodNode (org.objectweb.asm.tree.MethodNode)54 InsnList (org.objectweb.asm.tree.InsnList)53 InsnNode (org.objectweb.asm.tree.InsnNode)41 VarInsnNode (org.objectweb.asm.tree.VarInsnNode)38 LdcInsnNode (org.objectweb.asm.tree.LdcInsnNode)37 FieldInsnNode (org.objectweb.asm.tree.FieldInsnNode)35 ClassNode (org.objectweb.asm.tree.ClassNode)33 LabelNode (org.objectweb.asm.tree.LabelNode)27 JumpInsnNode (org.objectweb.asm.tree.JumpInsnNode)26 Test (org.junit.Test)25 Label (org.objectweb.asm.Label)25 ClassReader (org.objectweb.asm.ClassReader)23 TypeInsnNode (org.objectweb.asm.tree.TypeInsnNode)23 HashSet (java.util.HashSet)16 Type (org.objectweb.asm.Type)15 Frame (org.objectweb.asm.tree.analysis.Frame)13 ArrayList (java.util.ArrayList)12 TryCatchBlockNode (org.objectweb.asm.tree.TryCatchBlockNode)11