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: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/*  www . j  a va  2  s .  co  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   ww w  .  j  a  va2s  .c om
        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 w  w  w.  j  av  a 2 s  . c om
        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.brutusin.instrumentation.Instrumentator.java

License:Apache License

private int addMethodParametersVariable(InsnList il) {
    il.add(TreeInstructions.getPushInstruction(this.methodArguments.length));
    il.add(new TypeInsnNode(Opcodes.ANEWARRAY, "java/lang/Object"));
    int methodParametersIndex = getFistAvailablePosition();
    il.add(new VarInsnNode(Opcodes.ASTORE, methodParametersIndex));
    this.mn.maxLocals++;
    for (int i = 0; i < this.methodArguments.length; i++) {
        il.add(new VarInsnNode(Opcodes.ALOAD, methodParametersIndex));
        il.add(TreeInstructions.getPushInstruction(i));
        il.add(TreeInstructions.getLoadInst(methodArguments[i], getArgumentPosition(i)));
        MethodInsnNode mNode = TreeInstructions.getWrapperContructionInst(methodArguments[i]);
        if (mNode != null) {
            il.add(mNode);//  w  ww .  j  a  v a 2 s  . c  o  m
        }
        il.add(new InsnNode(Opcodes.AASTORE));
    }
    return methodParametersIndex;
}

From source file:org.brutusin.instrumentation.Instrumentator.java

License:Apache License

private void addGetMethodInvocation(InsnList il) {
    il.add(TreeInstructions.getPushInstruction(this.methodArguments.length));
    il.add(new TypeInsnNode(Opcodes.ANEWARRAY, "java/lang/Class"));
    int parameterClassesIndex = getFistAvailablePosition();
    il.add(new VarInsnNode(Opcodes.ASTORE, parameterClassesIndex));
    this.mn.maxLocals++;
    for (int i = 0; i < this.methodArguments.length; i++) {
        il.add(new VarInsnNode(Opcodes.ALOAD, parameterClassesIndex));
        il.add(TreeInstructions.getPushInstruction(i));
        il.add(TreeInstructions.getClassReferenceInstruction(methodArguments[i], cn.version & 0xFFFF));
        il.add(new InsnNode(Opcodes.AASTORE));
    }/*from  w w w .jav a2s  .c  o  m*/
    il.add(TreeInstructions.getClassConstantReference(this.classType, cn.version & 0xFFFF));
    il.add(new LdcInsnNode(this.mn.name));
    il.add(new VarInsnNode(Opcodes.ALOAD, parameterClassesIndex));
    il.add(new MethodInsnNode(Opcodes.INVOKESTATIC, "org/brutusin/instrumentation/utils/Helper", "getSource",
            "(Ljava/lang/Class;Ljava/lang/String;[Ljava/lang/Class;)Ljava/lang/Object;", false));
}

From source file:org.brutusin.instrumentation.Instrumentator.java

License:Apache License

private void addStoreMethod(InsnList il) {
    this.methodVarIndex = getFistAvailablePosition();
    il.add(new VarInsnNode(Opcodes.ASTORE, this.methodVarIndex));
    this.mn.maxLocals++;
}

From source file:org.brutusin.instrumentation.Instrumentator.java

License:Apache License

private void addGetCallback(InsnList il) {
    il.add(new LdcInsnNode(this.callbackId));
    il.add(new MethodInsnNode(Opcodes.INVOKESTATIC, "org/brutusin/instrumentation/Callback", "getInstance",
            "(Ljava/lang/String;)Lorg/brutusin/instrumentation/Callback;", false));
}

From source file:org.brutusin.instrumentation.Instrumentator.java

License:Apache License

private void addTraceStart() {
    InsnList il = new InsnList();
    int methodParametersIndex = addMethodParametersVariable(il);
    addGetMethodInvocation(il);//from w ww.j  a  va  2s .  c o  m
    addStoreMethod(il);
    addGetCallback(il);

    il.add(new VarInsnNode(Opcodes.ALOAD, this.methodVarIndex));
    il.add(new VarInsnNode(Opcodes.ALOAD, methodParametersIndex));
    il.add(new MethodInsnNode(Opcodes.INVOKEVIRTUAL, "org/brutusin/instrumentation/Callback", "onStart",
            "(Ljava/lang/Object;[Ljava/lang/Object;)Ljava/lang/String;", false));

    this.executionIdIndex = getFistAvailablePosition();
    il.add(new VarInsnNode(Opcodes.ASTORE, this.executionIdIndex));
    this.mn.maxLocals++;
    this.startNode = new LabelNode();
    this.mn.instructions.insert(startNode);
    this.mn.instructions.insert(il);
}

From source file:org.brutusin.instrumentation.Instrumentator.java

License:Apache License

private void addTraceThrowablePassed() {

    InsnList il = this.mn.instructions;

    LabelNode endNode = new LabelNode();
    il.add(endNode);

    addCatchBlock(this.startNode, endNode);

}

From source file:org.brutusin.instrumentation.Instrumentator.java

License:Apache License

private void addCatchBlock(LabelNode startNode, LabelNode endNode) {

    InsnList il = new InsnList();
    LabelNode handlerNode = new LabelNode();
    il.add(handlerNode);

    int exceptionVariablePosition = getFistAvailablePosition();
    il.add(new VarInsnNode(Opcodes.ASTORE, exceptionVariablePosition));
    this.methodOffset++; // Actualizamos el offset
    addGetCallback(il);//  w w w  .  j av a2 s.c om
    il.add(new VarInsnNode(Opcodes.ALOAD, this.methodVarIndex));
    il.add(new VarInsnNode(Opcodes.ALOAD, exceptionVariablePosition));
    il.add(new VarInsnNode(Opcodes.ALOAD, this.executionIdIndex));
    il.add(new MethodInsnNode(Opcodes.INVOKEVIRTUAL, "org/brutusin/instrumentation/Callback",
            "onThrowableUncatched", "(Ljava/lang/Object;Ljava/lang/Throwable;Ljava/lang/String;)V", false));

    il.add(new VarInsnNode(Opcodes.ALOAD, exceptionVariablePosition));
    il.add(new InsnNode(Opcodes.ATHROW));

    TryCatchBlockNode blockNode = new TryCatchBlockNode(startNode, endNode, handlerNode, null);

    this.mn.tryCatchBlocks.add(blockNode);
    this.mn.instructions.add(il);
}