Example usage for org.objectweb.asm.tree ClassNode accept

List of usage examples for org.objectweb.asm.tree ClassNode accept

Introduction

In this page you can find the example usage for org.objectweb.asm.tree ClassNode accept.

Prototype

public void accept(final ClassVisitor classVisitor) 

Source Link

Document

Makes the given class visitor visit this class.

Usage

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

License:MIT License

private byte[] transformSoundManager(final byte[] classBytes) {
    final String managerToReplace = "net/minecraft/client/audio/SoundManager";
    final String newManager = "org/blockartistry/DynSurround/client/sound/SoundManagerReplacement";

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

    for (final MethodNode m : cn.methods) {
        final ListIterator<AbstractInsnNode> itr = m.instructions.iterator();
        boolean foundNew = false;
        while (itr.hasNext()) {
            final AbstractInsnNode node = itr.next();
            if (node.getOpcode() == NEW) {
                final TypeInsnNode theNew = (TypeInsnNode) node;
                if (managerToReplace.equals(theNew.desc)) {
                    m.instructions.set(node, new TypeInsnNode(NEW, newManager));
                    foundNew = true;
                }
            } else if (node.getOpcode() == INVOKESPECIAL) {
                final MethodInsnNode theInvoke = (MethodInsnNode) node;
                if (managerToReplace.equals(theInvoke.owner)) {
                    if (foundNew) {
                        m.instructions.set(node, new MethodInsnNode(INVOKESPECIAL, newManager, theInvoke.name,
                                theInvoke.desc, false));
                        foundNew = false;
                    }
                }
            }
        }
    }

    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[] replaceRandom(final String name, final byte[] classBytes) {

    final String randomToReplace = "java/util/Random";
    final String newRandom = "org/blockartistry/lib/random/XorShiftRandom";

    boolean madeUpdate = false;

    final ClassReader cr = new ClassReader(classBytes);
    final ClassNode cn = new ClassNode(ASM5);
    cr.accept(cn, 0);//from   w  w w.  j  a  v  a2s .com

    for (final MethodNode m : cn.methods) {
        final ListIterator<AbstractInsnNode> itr = m.instructions.iterator();
        boolean foundNew = false;
        while (itr.hasNext()) {
            final AbstractInsnNode node = itr.next();
            if (node.getOpcode() == NEW) {
                final TypeInsnNode theNew = (TypeInsnNode) node;
                if (randomToReplace.equals(theNew.desc)) {
                    m.instructions.set(node, new TypeInsnNode(NEW, newRandom));
                    madeUpdate = true;
                    foundNew = true;
                }
            } else if (node.getOpcode() == INVOKESPECIAL) {
                final MethodInsnNode theInvoke = (MethodInsnNode) node;
                if (randomToReplace.equals(theInvoke.owner)) {
                    if (foundNew) {
                        m.instructions.set(node, new MethodInsnNode(INVOKESPECIAL, newRandom, theInvoke.name,
                                theInvoke.desc, false));
                        foundNew = false;
                    }
                }
            }
        }
    }

    if (madeUpdate) {
        logger.debug("Replaced Random in " + name);
        ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS);
        cn.accept(cw);
        return cw.toByteArray();
    }
    return classBytes;
}

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//from   w  w w . ja  v a2  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();
}

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

License:MIT License

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

    if (TransformLoader.runtimeDeobEnabled)
        names = new String[] { "func_73051_P" };
    else//from   w w  w .j  av  a  2 s .c o  m
        names = new String[] { "resetRainAndThunder" };

    final String targetName[] = new String[] { "resetRainAndThunder" };

    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]);
            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/mod/DynSurround/server/PlayerSleepHandler", targetName[0], 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[] transformWorld(final byte[] classBytes) {
    final String names[];

    if (TransformLoader.runtimeDeobEnabled)
        names = new String[] { "updateWeatherBody", "func_72896_J", "func_72911_I" };
    else/*from  w  w w.j  a v a2 s.c o  m*/
        names = new String[] { "updateWeatherBody", "isRaining", "isThundering" };

    final String targetName[] = new String[] { "updateWeatherBody", "isRaining", "isThundering" };

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

    for (final MethodNode m : cn.methods) {
        int idx = -1;
        if (m.name.equals(names[0]))
            idx = 0;
        else if (m.name.equals(names[1]))
            idx = 1;
        else if (m.name.equals(names[2]))
            idx = 2;
        else
            continue;

        logger.debug("Hooking " + names[idx]);
        InsnList list = new InsnList();
        list.add(new VarInsnNode(ALOAD, 0));
        final String sig;
        if (idx == 0)
            sig = "(Lnet/minecraft/world/World;)V";
        else
            sig = "(Lnet/minecraft/world/World;)Z";

        list.add(new MethodInsnNode(INVOKESTATIC, "org/blockartistry/mod/DynSurround/server/WorldHandler",
                targetName[idx], sig, false));

        if (idx == 0)
            list.add(new InsnNode(RETURN));
        else
            list.add(new InsnNode(IRETURN));
        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.mod.DynSurround.asm.Transformer.java

License:MIT License

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

    if (TransformLoader.runtimeDeobEnabled)
        names = new String[] { "func_188770_e" };
    else//from  ww  w.  ja v a 2 s  .  c o  m
        names = new String[] { "getClampedVolume" };

    final String targetName[] = new String[] { "getClampedVolume" };

    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, 1));
            final String sig = "(Lnet/minecraft/client/audio/ISound;)F";
            list.add(new MethodInsnNode(INVOKESTATIC,
                    "org/blockartistry/mod/DynSurround/client/sound/SoundManager", targetName[0], sig, false));
            list.add(new InsnNode(FRETURN));
            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.Jiffy.asm.Transformer.java

License:MIT License

private byte[] transformMathUtils(final byte[] classBytes) {

    String names[] = null;/*ww  w .java2 s .c om*/

    if (TransformLoader.runtimeDeobEnabled)
        names = new String[] { "func_76126_a", "func_76134_b" };
    else
        names = new String[] { "sin", "cos" };

    final String targetName[] = new String[] { "sin", "cos" };

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

    for (final MethodNode m : cn.methods) {
        int targetId = -1;
        if (m.name.equals(names[0]))
            targetId = 0;
        else if (m.name.equals(names[1]))
            targetId = 1;

        if (targetId != -1) {
            m.localVariables = null;
            m.instructions.clear();
            m.instructions.add(new VarInsnNode(FLOAD, 0));
            final String sig = "(F)F";
            m.instructions.add(new MethodInsnNode(INVOKESTATIC, "org/blockartistry/util/MathHelper",
                    targetName[targetId], sig, false));
            m.instructions.add(new InsnNode(FRETURN));
        }
    }

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

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

License:MIT License

private byte[] transformBlockLeaves(final byte[] classBytes) {
    String names[] = null;//www. j a  va  2  s  . c  o m

    if (TransformLoader.runtimeDeobEnabled)
        names = new String[] { "beginLeavesDecay" };
    else
        names = new String[] { "beginLeavesDecay" };

    final String targetName[] = new String[] { "beginLeavesDecay" };

    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])) {
            m.localVariables = null;
            m.instructions.clear();
            m.instructions.add(new VarInsnNode(ALOAD, 1));
            m.instructions.add(new VarInsnNode(ILOAD, 2));
            m.instructions.add(new VarInsnNode(ILOAD, 3));
            m.instructions.add(new VarInsnNode(ILOAD, 4));
            final String sig = "(Lnet/minecraft/world/World;III)V";
            m.instructions.add(new MethodInsnNode(INVOKESTATIC, "org/blockartistry/block/BlockLeaves",
                    targetName[0], sig, false));
            m.instructions.add(new InsnNode(RETURN));
            break;
        }
    }

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

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

License:MIT License

private byte[] replaceRandom(final String name, final byte[] classBytes) {

    final String randomToReplace = "java/util/Random";
    final String newRandom = "org/blockartistry/util/XorShiftRandom";

    boolean madeUpdate = false;

    final ClassReader cr = new ClassReader(classBytes);
    final ClassNode cn = new ClassNode(ASM5);
    cr.accept(cn, 0);/*from w ww.  j  a v  a 2s.  c  om*/

    for (final MethodNode m : cn.methods) {
        final ListIterator<AbstractInsnNode> itr = m.instructions.iterator();
        boolean foundNew = false;
        while (itr.hasNext()) {
            final AbstractInsnNode node = itr.next();
            if (node.getOpcode() == NEW) {
                final TypeInsnNode theNew = (TypeInsnNode) node;
                if (randomToReplace.equals(theNew.desc)) {
                    m.instructions.set(node, new TypeInsnNode(NEW, newRandom));
                    madeUpdate = true;
                    foundNew = true;
                }
            } else if (node.getOpcode() == INVOKESPECIAL) {
                final MethodInsnNode theInvoke = (MethodInsnNode) node;
                if (randomToReplace.equals(theInvoke.owner)) {
                    if (foundNew) {
                        m.instructions.set(node, new MethodInsnNode(INVOKESPECIAL, newRandom, theInvoke.name,
                                theInvoke.desc, false));
                        foundNew = false;
                    }
                }
            }
        }
    }

    if (madeUpdate) {
        logger.debug("Replaced Random in " + name);
        ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS);
        cn.accept(cw);
        return cw.toByteArray();
    }
    return classBytes;
}

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

License:MIT License

private byte[] replaceMath(final String name, final byte[] classBytes) {

    boolean madeUpdate = false;

    final ClassReader cr = new ClassReader(classBytes);
    final ClassNode cn = new ClassNode(ASM5);
    cr.accept(cn, 0);/* w  w  w .  j  a va  2  s .  co m*/

    for (final MethodNode m : cn.methods) {
        final ListIterator<AbstractInsnNode> itr = m.instructions.iterator();
        while (itr.hasNext()) {
            final AbstractInsnNode node = itr.next();
            if (node.getOpcode() == INVOKESTATIC) {
                final MethodInsnNode theInvoke = (MethodInsnNode) node;
                if ("java/lang/Math".equals(theInvoke.owner)) {
                    if ("sin".equals(theInvoke.name) || "cos".equals(theInvoke.name)
                            || "random".equals(theInvoke.name)) {
                        madeUpdate = true;
                        m.instructions.set(node, new MethodInsnNode(INVOKESTATIC,
                                "org/blockartistry/util/MathHelper", theInvoke.name, theInvoke.desc, false));
                    }
                }
            }
        }
    }

    if (madeUpdate) {
        logger.debug("Replaced Math in " + name);
        ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS);
        cn.accept(cw);
        return cw.toByteArray();
    }
    return classBytes;
}