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:blockphysics.asm.BPTransformer.java

License:Open Source License

private byte[] transformGuiCreateWorld(byte[] bytes) {
    /*try//  w ww .ja  va  2 s  .  co m
    {
        FileOutputStream fos = new FileOutputStream("d:/GuiCreateWorld.orig.class");
        fos.write(bytes);
      fos.close();
    } catch (IOException e) {
       // TODO Auto-generated catch block
       e.printStackTrace();
    }*/

    System.out.print("[BlockPhysics] Patching GuiCreateWorld.class ..........");
    boolean ok = false, ok2 = false;

    ClassNode classNode = new ClassNode();
    ClassReader classReader = new ClassReader(bytes);
    classReader.accept(classNode, 0);

    MethodNode m;
    Iterator<MethodNode> methods = classNode.methods.iterator();
    while (methods.hasNext()) {
        m = methods.next();

        if (m.name.equals("A_") && m.desc.equals("()V")) {
            InsnList toInject = new InsnList();

            toInject.add(new VarInsnNode(ALOAD, 0));
            toInject.add(new InsnNode(ICONST_0));
            toInject.add(new FieldInsnNode(PUTFIELD, "auy", "v", "Z"));

            for (int index = m.instructions.size() - 1; index >= 0; index--) {
                if (m.instructions.get(index).getOpcode() == RETURN) {
                    m.instructions.insertBefore(m.instructions.get(index), toInject);
                    ok = true;
                    break;
                }
            }
        } else if (m.name.equals("a") && m.desc.equals("(Lauq;)V")) {
            for (int index = 0; index < m.instructions.size(); index++) {
                if (m.instructions.get(index).getOpcode() == INVOKEVIRTUAL
                        && m.instructions.get(index).getType() == AbstractInsnNode.METHOD_INSN
                        && ((MethodInsnNode) m.instructions.get(index)).owner.equals("ats")
                        && ((MethodInsnNode) m.instructions.get(index)).name.equals("a")
                        && ((MethodInsnNode) m.instructions.get(index)).desc
                                .equals("(Ljava/lang/String;Ljava/lang/String;Lacc;)V")) {
                    m.instructions.remove(m.instructions.get(index));

                    InsnList toInject = new InsnList();

                    toInject.add(new InsnNode(ICONST_1));
                    toInject.add(new MethodInsnNode(INVOKESPECIAL, "blockphysics/BGui", "<init>",
                            "(Lawb;Ljava/lang/String;Ljava/lang/String;Lacc;Z)V"));
                    toInject.add(new MethodInsnNode(INVOKEVIRTUAL, "ats", "a", "(Lawb;)V"));

                    m.instructions.insertBefore(m.instructions.get(index), toInject);

                    while (m.instructions.get(index).getOpcode() != GETFIELD
                            || m.instructions.get(index).getType() != AbstractInsnNode.FIELD_INSN
                            || !((FieldInsnNode) m.instructions.get(index)).owner.equals("auy")
                            || !((FieldInsnNode) m.instructions.get(index)).name.equals("f")
                            || !((FieldInsnNode) m.instructions.get(index)).desc.equals("Lats;")) {
                        index--;
                    }

                    toInject = new InsnList();

                    toInject.add(new TypeInsnNode(NEW, "blockphysics/BGui"));
                    toInject.add(new InsnNode(DUP));
                    toInject.add(new VarInsnNode(ALOAD, 0));

                    m.instructions.insert(m.instructions.get(index), toInject);

                    ok2 = true;
                    break;
                }
            }
        }
    }

    ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS);
    classNode.accept(cw);

    if (ok && ok2)
        System.out.println("OK");
    else
        System.out.println("Failed." + ok + ok2);

    /*try
    {
       FileOutputStream fos = new FileOutputStream("d:/GuiCreateWorld.class");
       fos.write(cw.toByteArray());
     fos.close();
    } catch (IOException e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
    }*/

    return cw.toByteArray();
}

From source file:blockphysics.asm.BPTransformer.java

License:Open Source License

private byte[] transformBlock(byte[] bytes) {
    /*try//from   w w  w  . jav a2s .c o m
    {
        FileOutputStream fos = new FileOutputStream("d:/Block.orig.class");
        fos.write(bytes);
      fos.close();
    } catch (IOException e) {
       // TODO Auto-generated catch block
       e.printStackTrace();
    }*/

    System.out.print("[BlockPhysics] Patching Block.class ...................");
    boolean ok2 = false;
    boolean ok3 = false;
    boolean ok4 = false;
    boolean ok5 = false;
    boolean ok6 = false;
    boolean ok7 = false;
    boolean ok8 = false;

    ClassNode classNode = new ClassNode();
    ClassReader classReader = new ClassReader(bytes);
    classReader.accept(classNode, 0);

    MethodNode m;
    Iterator<MethodNode> methods = classNode.methods.iterator();
    while (methods.hasNext()) {
        m = methods.next();

        if (m.name.equals("g") && m.desc.equals("(Labv;IIII)V")) {
            InsnList toInject = new InsnList();
            toInject.add(new VarInsnNode(ALOAD, 1));
            toInject.add(new VarInsnNode(ILOAD, 2));
            toInject.add(new VarInsnNode(ILOAD, 3));
            toInject.add(new VarInsnNode(ILOAD, 4));
            toInject.add(new VarInsnNode(ILOAD, 5));
            toInject.add(new VarInsnNode(ALOAD, 0));
            toInject.add(new FieldInsnNode(GETFIELD, "aqw", "cF", "I"));
            toInject.add(new MethodInsnNode(INVOKESTATIC, "blockphysics/BlockPhysics",
                    "onBlockDestroyedByPlayer", "(Labv;IIIII)V"));

            for (int index = m.instructions.size() - 1; index >= 0; index--) {
                if (m.instructions.get(index).getOpcode() == RETURN) {
                    m.instructions.insertBefore(m.instructions.get(index), toInject);
                    ok2 = true;
                    break;
                }
            }
        } else if (m.name.equals("a") && m.desc.equals("(Labv;IIII)V")) {
            InsnList toInject = new InsnList();
            toInject.add(new VarInsnNode(ALOAD, 1));
            toInject.add(new VarInsnNode(ILOAD, 2));
            toInject.add(new VarInsnNode(ILOAD, 3));
            toInject.add(new VarInsnNode(ILOAD, 4));
            toInject.add(new VarInsnNode(ALOAD, 0));
            toInject.add(new FieldInsnNode(GETFIELD, "aqw", "cF", "I"));
            toInject.add(new MethodInsnNode(INVOKESTATIC, "blockphysics/BlockPhysics", "onNeighborBlockChange",
                    "(Labv;IIII)V"));

            for (int index = m.instructions.size() - 1; index >= 0; index--) {
                if (m.instructions.get(index).getOpcode() == RETURN) {
                    m.instructions.insertBefore(m.instructions.get(index), toInject);
                    ok3 = true;
                    break;
                }
            }
        } else if (m.name.equals("b") && m.desc.equals("(Labv;IIILnm;)V")) {
            InsnList toInject = new InsnList();
            toInject.add(new VarInsnNode(ALOAD, 1));
            toInject.add(new VarInsnNode(ILOAD, 2));
            toInject.add(new VarInsnNode(ILOAD, 3));
            toInject.add(new VarInsnNode(ILOAD, 4));
            toInject.add(new VarInsnNode(ALOAD, 0));
            toInject.add(new FieldInsnNode(GETFIELD, "aqw", "cF", "I"));
            toInject.add(new MethodInsnNode(INVOKESTATIC, "blockphysics/BlockPhysics", "onNeighborBlockChange",
                    "(Labv;IIII)V"));

            for (int index = m.instructions.size() - 1; index >= 0; index--) {
                if (m.instructions.get(index).getOpcode() == RETURN) {
                    m.instructions.insertBefore(m.instructions.get(index), toInject);
                    ok4 = true;
                    break;
                }
            }
        } else if (m.name.equals("a") && m.desc.equals("(Labv;IIILnm;)V")) {
            InsnList toInject = new InsnList();
            toInject.add(new VarInsnNode(ALOAD, 1));
            toInject.add(new VarInsnNode(ILOAD, 2));
            toInject.add(new VarInsnNode(ILOAD, 3));
            toInject.add(new VarInsnNode(ILOAD, 4));
            toInject.add(new VarInsnNode(ALOAD, 0));
            toInject.add(new FieldInsnNode(GETFIELD, "aqw", "cF", "I"));
            toInject.add(new VarInsnNode(ALOAD, 5));
            toInject.add(new MethodInsnNode(INVOKESTATIC, "blockphysics/BlockPhysics",
                    "onEntityCollidedWithBlock", "(Labv;IIIILnm;)V"));

            for (int index = m.instructions.size() - 1; index >= 0; index--) {
                if (m.instructions.get(index).getOpcode() == RETURN) {
                    m.instructions.insertBefore(m.instructions.get(index), toInject);
                    ok5 = true;
                    break;
                }
            }
        } else if (m.name.equals("k") && m.desc.equals("(Labv;IIII)V")) {
            InsnList toInject = new InsnList();
            toInject.add(new VarInsnNode(ALOAD, 1));
            toInject.add(new VarInsnNode(ILOAD, 2));
            toInject.add(new VarInsnNode(ILOAD, 3));
            toInject.add(new VarInsnNode(ILOAD, 4));
            toInject.add(new VarInsnNode(ALOAD, 0));
            toInject.add(new FieldInsnNode(GETFIELD, "aqw", "cF", "I"));
            toInject.add(new VarInsnNode(ILOAD, 5));
            toInject.add(new MethodInsnNode(INVOKESTATIC, "blockphysics/BlockPhysics", "onPostBlockPlaced",
                    "(Labv;IIIII)V"));

            for (int index = m.instructions.size() - 1; index >= 0; index--) {
                if (m.instructions.get(index).getOpcode() == RETURN) {
                    m.instructions.insertBefore(m.instructions.get(index), toInject);
                    ok6 = true;
                    break;
                }
            }
        } else if (m.name.equals("a") && m.desc.equals("(Labv;IIILnm;F)V")) {
            InsnList toInject = new InsnList();
            toInject.add(new VarInsnNode(ALOAD, 1));
            toInject.add(new VarInsnNode(ILOAD, 2));
            toInject.add(new VarInsnNode(ILOAD, 3));
            toInject.add(new VarInsnNode(ILOAD, 4));
            toInject.add(new VarInsnNode(ALOAD, 0));
            toInject.add(new FieldInsnNode(GETFIELD, "aqw", "cF", "I"));
            toInject.add(new MethodInsnNode(INVOKESTATIC, "blockphysics/BlockPhysics", "onNeighborBlockChange",
                    "(Labv;IIII)V"));

            for (int index = m.instructions.size() - 1; index >= 0; index--) {
                if (m.instructions.get(index).getOpcode() == RETURN) {
                    m.instructions.insertBefore(m.instructions.get(index), toInject);
                    ok7 = true;
                    break;
                }
            }
        }
    }

    FieldNode f;
    Iterator<FieldNode> fields = classNode.fields.iterator();
    while (fields.hasNext()) {
        f = fields.next();

        if (f.name.equals("blockFlammability") && f.desc.equals("[I")) {
            f.access = ACC_PUBLIC + ACC_STATIC;
            ok8 = true;
            break;
        }
    }

    ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS);
    classNode.accept(cw);

    if (ok2 && ok3 && ok4 && ok5 && ok6 && ok7 && ok8)
        System.out.println("OK");
    else
        System.out.println("Failed." + ok2 + ok3 + ok4 + ok5 + ok6 + ok7 + ok8);

    /*try
    {
       FileOutputStream fos = new FileOutputStream("d:/Block.class");
       fos.write(cw.toByteArray());
     fos.close();
    } catch (IOException e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
    }*/

    return cw.toByteArray();
}

From source file:blockphysics.asm.BPTransformer.java

License:Open Source License

private byte[] transformBlockRailBase(byte[] bytes) {
    /*try//from  w  w w  . ja v a 2 s . com
    {
        FileOutputStream fos = new FileOutputStream("d:/BlockRailBase.orig.class");
        fos.write(bytes);
      fos.close();
    } catch (IOException e) {
       // TODO Auto-generated catch block
       e.printStackTrace();
    }*/

    System.out.print("[BlockPhysics] Patching BlockRailBase.class ...........");
    boolean ok = false;

    ClassNode classNode = new ClassNode();
    ClassReader classReader = new ClassReader(bytes);
    classReader.accept(classNode, 0);

    MethodNode m;
    Iterator<MethodNode> methods = classNode.methods.iterator();
    while (methods.hasNext()) {
        m = methods.next();
        if (m.name.equals("a") && m.desc.equals("(Labv;IIII)V")) {
            InsnList toInject = new InsnList();
            toInject.add(new VarInsnNode(ALOAD, 1));
            toInject.add(new VarInsnNode(ILOAD, 2));
            toInject.add(new VarInsnNode(ILOAD, 3));
            toInject.add(new VarInsnNode(ILOAD, 4));
            toInject.add(new VarInsnNode(ALOAD, 0));
            toInject.add(new FieldInsnNode(GETFIELD, "amv", "cF", "I"));
            toInject.add(new MethodInsnNode(INVOKESTATIC, "blockphysics/BlockPhysics", "onNeighborBlockChange",
                    "(Labv;IIII)V"));

            for (int index = m.instructions.size() - 1; index >= 0; index--) {
                if (m.instructions.get(index).getOpcode() == RETURN) {
                    m.instructions.insertBefore(m.instructions.get(index), toInject);
                    ok = true;
                    break;
                }
            }
        }
    }

    ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS);
    classNode.accept(cw);

    if (ok)
        System.out.println("OK");
    else
        System.out.println("Failed." + ok);

    /*try
    {
       FileOutputStream fos = new FileOutputStream("d:/BlockRailBase.class");
       fos.write(cw.toByteArray());
     fos.close();
    } catch (IOException e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
    }*/

    return cw.toByteArray();
}

From source file:blockphysics.asm.BPTransformer.java

License:Open Source License

private byte[] transformBlockPistonBase(byte[] bytes) {
    /*try//from w  w w  .j a v a2  s . c  o  m
    {
        FileOutputStream fos = new FileOutputStream("d:/BlockPistonBase.orig.class");
        fos.write(bytes);
      fos.close();
    } catch (IOException e) {
       // TODO Auto-generated catch block
       e.printStackTrace();
    }*/

    System.out.print("[BlockPhysics] Patching BlockPistonBase.class .........");
    boolean ok = false, ok2 = false, ok3 = false;

    ClassNode classNode = new ClassNode();
    ClassReader classReader = new ClassReader(bytes);
    classReader.accept(classNode, 0);

    MethodNode m;
    Iterator<MethodNode> methods = classNode.methods.iterator();
    while (methods.hasNext()) {
        m = methods.next();

        if (m.name.equals("a") && m.desc.equals("(Labv;IIII)V")) {
            InsnList toInject = new InsnList();
            toInject.add(new VarInsnNode(ALOAD, 1));
            toInject.add(new VarInsnNode(ILOAD, 2));
            toInject.add(new VarInsnNode(ILOAD, 3));
            toInject.add(new VarInsnNode(ILOAD, 4));
            toInject.add(new VarInsnNode(ALOAD, 0));
            toInject.add(new FieldInsnNode(GETFIELD, "asq", "cF", "I"));
            toInject.add(new MethodInsnNode(INVOKESTATIC, "blockphysics/BlockPhysics", "onNeighborBlockChange",
                    "(Labv;IIII)V"));

            for (int index = m.instructions.size() - 1; index >= 0; index--) {
                if (m.instructions.get(index).getOpcode() == RETURN) {
                    m.instructions.insertBefore(m.instructions.get(index), toInject);
                    ok = true;
                    break;
                }
            }
        } else if (m.name.equals("k") && m.desc.equals("(Labv;III)V")) {
            InsnList toInject = new InsnList();
            toInject.add(new VarInsnNode(ALOAD, 1));
            toInject.add(new VarInsnNode(ILOAD, 2));
            toInject.add(new VarInsnNode(ILOAD, 3));
            toInject.add(new VarInsnNode(ILOAD, 4));
            toInject.add(new VarInsnNode(ALOAD, 0));
            toInject.add(new VarInsnNode(ALOAD, 0));
            toInject.add(new FieldInsnNode(GETFIELD, "asq", "a", "Z"));
            toInject.add(new MethodInsnNode(INVOKESTATIC, "blockphysics/BlockPhysics", "updatePistonState",
                    "(Labv;IIILasq;Z)V"));
            toInject.add(new InsnNode(RETURN));

            m.instructions.clear();
            m.localVariables.clear();
            m.instructions.add(toInject);
            ok2 = true;
        } else if (m.name.equals("b") && m.desc.equals("(Labv;IIIII)Z")) {
            InsnList toInject = new InsnList();
            toInject.add(new VarInsnNode(ALOAD, 1));
            toInject.add(new VarInsnNode(ILOAD, 2));
            toInject.add(new VarInsnNode(ILOAD, 3));
            toInject.add(new VarInsnNode(ILOAD, 4));
            toInject.add(new VarInsnNode(ILOAD, 5));
            toInject.add(new VarInsnNode(ILOAD, 6));
            toInject.add(new VarInsnNode(ALOAD, 0));
            toInject.add(new VarInsnNode(ALOAD, 0));
            toInject.add(new FieldInsnNode(GETFIELD, "asq", "a", "Z"));
            toInject.add(new MethodInsnNode(INVOKESTATIC, "blockphysics/BlockPhysics",
                    "onBlockPistonEventReceived", "(Labv;IIIIILasq;Z)Z"));
            toInject.add(new InsnNode(IRETURN));

            m.instructions.clear();
            m.localVariables.clear();
            m.instructions.add(toInject);
            ok3 = true;
        }
    }

    ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS);
    classNode.accept(cw);

    if (ok && ok2 && ok3)
        System.out.println("OK");
    else
        System.out.println("Failed." + ok + ok2 + ok3);

    /*try
    {
       FileOutputStream fos = new FileOutputStream("d:/BlockPistonBase.class");
       fos.write(cw.toByteArray());
     fos.close();
    } catch (IOException e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
    }*/

    return cw.toByteArray();
}

From source file:blockphysics.asm.BPTransformer.java

License:Open Source License

private byte[] transformBlockSand(byte[] bytes) {
    /*try/*ww  w.  ja  va 2  s .c o  m*/
    {
        FileOutputStream fos = new FileOutputStream("d:/BlockSand.orig.class");
        fos.write(bytes);
      fos.close();
    } catch (IOException e) {
       // TODO Auto-generated catch block
       e.printStackTrace();
    }*/

    System.out.print("[BlockPhysics] Patching BlockSand.class ...............");
    boolean ok = false;
    boolean ok2 = false;
    boolean ok3 = false;
    boolean ok4 = false;

    ClassNode classNode = new ClassNode();
    ClassReader classReader = new ClassReader(bytes);
    classReader.accept(classNode, 0);

    MethodNode m;
    Iterator<MethodNode> methods = classNode.methods.iterator();
    while (methods.hasNext()) {
        m = methods.next();

        if (m.name.equals("a") && m.desc.equals("(Labv;IIII)V")) {
            InsnList toInject = new InsnList();
            toInject.add(new VarInsnNode(ALOAD, 1));
            toInject.add(new VarInsnNode(ILOAD, 2));
            toInject.add(new VarInsnNode(ILOAD, 3));
            toInject.add(new VarInsnNode(ILOAD, 4));
            toInject.add(new VarInsnNode(ALOAD, 0));
            toInject.add(new FieldInsnNode(GETFIELD, "aop", "cF", "I"));
            toInject.add(new MethodInsnNode(INVOKESTATIC, "blockphysics/BlockPhysics", "onNeighborBlockChange",
                    "(Labv;IIII)V"));
            toInject.add(new InsnNode(RETURN));

            m.instructions.clear();
            m.localVariables.clear();
            m.instructions.add(toInject);
            ok = true;
        } else if (m.name.equals("a") && m.desc.equals("(Labv;IIILjava/util/Random;)V")) {
            m.instructions.clear();
            m.localVariables.clear();
            m.instructions.insert(new InsnNode(RETURN));
            ok2 = true;
        } else if (m.name.equals("k") && m.desc.equals("(Labv;III)V")) {
            m.instructions.clear();
            m.localVariables.clear();
            m.instructions.insert(new InsnNode(RETURN));
            ok3 = true;
        } else if (m.name.equals("a_") && m.desc.equals("(Labv;III)Z")) {
            InsnList toInject = new InsnList();
            toInject.add(new VarInsnNode(ALOAD, 0));
            toInject.add(new VarInsnNode(ILOAD, 1));
            toInject.add(new VarInsnNode(ILOAD, 2));
            toInject.add(new VarInsnNode(ILOAD, 3));
            toInject.add(new InsnNode(ICONST_0));
            toInject.add(
                    new MethodInsnNode(INVOKESTATIC, "blockphysics/BlockPhysics", "canMoveTo", "(Labv;IIII)Z"));
            toInject.add(new InsnNode(IRETURN));

            m.instructions.clear();
            m.localVariables.clear();
            m.instructions.add(toInject);
            ok4 = true;
        }
    }

    ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS);
    classNode.accept(cw);

    if (ok && ok2 && ok3 && ok4)
        System.out.println("OK");
    else
        System.out.println("Failed." + ok + ok2 + ok3 + ok4);

    /*try
    {
       FileOutputStream fos = new FileOutputStream("d:/BlockSand.class");
       fos.write(cw.toByteArray());
     fos.close();
    } catch (IOException e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
    }*/

    return cw.toByteArray();
}

From source file:blockphysics.asm.BPTransformer.java

License:Open Source License

private byte[] transformBlockRedstoneLight(byte[] bytes) {
    /*try//from   w ww .ja v  a  2 s  .  c om
    {
        FileOutputStream fos = new FileOutputStream("d:/BlockRedstoneLight.orig.class");
        fos.write(bytes);
      fos.close();
    } catch (IOException e) {
       // TODO Auto-generated catch block
       e.printStackTrace();
    }*/

    System.out.print("[BlockPhysics] Patching BlockRedstoneLight.class ......");
    boolean ok = false;

    ClassNode classNode = new ClassNode();
    ClassReader classReader = new ClassReader(bytes);
    classReader.accept(classNode, 0);

    MethodNode m;
    Iterator<MethodNode> methods = classNode.methods.iterator();
    while (methods.hasNext()) {
        m = methods.next();

        if (m.name.equals("a") && m.desc.equals("(Labv;IIII)V")) {
            InsnList toInject = new InsnList();
            toInject.add(new VarInsnNode(ALOAD, 1));
            toInject.add(new VarInsnNode(ILOAD, 2));
            toInject.add(new VarInsnNode(ILOAD, 3));
            toInject.add(new VarInsnNode(ILOAD, 4));
            toInject.add(new VarInsnNode(ALOAD, 0));
            toInject.add(new FieldInsnNode(GETFIELD, "aqa", "cF", "I"));
            toInject.add(new MethodInsnNode(INVOKESTATIC, "blockphysics/BlockPhysics", "onNeighborBlockChange",
                    "(Labv;IIII)V"));

            for (int index = m.instructions.size() - 1; index >= 0; index--) {
                if (m.instructions.get(index).getOpcode() == RETURN) {
                    m.instructions.insertBefore(m.instructions.get(index), toInject);
                    ok = true;
                    break;
                }
            }
        }
    }

    ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS);
    classNode.accept(cw);

    if (ok)
        System.out.println("OK");
    else
        System.out.println("Failed." + ok);

    /*try
    {
       FileOutputStream fos = new FileOutputStream("d:/BlockRedstoneLight.class");
       fos.write(cw.toByteArray());
     fos.close();
    } catch (IOException e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
    }*/

    return cw.toByteArray();
}

From source file:blockphysics.asm.BPTransformer.java

License:Open Source License

private byte[] transformBlockTNT(byte[] bytes) {
    /*try//from www  . j  ava  2s  .  c o m
    {
        FileOutputStream fos = new FileOutputStream("d:/BlockTNT.orig.class");
        fos.write(bytes);
      fos.close();
    } catch (IOException e) {
       // TODO Auto-generated catch block
       e.printStackTrace();
    }*/

    System.out.print("[BlockPhysics] Patching BlockTNT.class ................");
    boolean ok = false;
    boolean ok2 = false;

    ClassNode classNode = new ClassNode();
    ClassReader classReader = new ClassReader(bytes);
    classReader.accept(classNode, 0);

    MethodNode m;
    Iterator<MethodNode> methods = classNode.methods.iterator();
    while (methods.hasNext()) {
        m = methods.next();

        if (m.name.equals("a") && m.desc.equals("(Labv;IIII)V")) {
            InsnList toInject = new InsnList();
            toInject.add(new VarInsnNode(ALOAD, 1));
            toInject.add(new VarInsnNode(ILOAD, 2));
            toInject.add(new VarInsnNode(ILOAD, 3));
            toInject.add(new VarInsnNode(ILOAD, 4));
            toInject.add(new VarInsnNode(ALOAD, 0));
            toInject.add(new FieldInsnNode(GETFIELD, "arb", "cF", "I"));
            toInject.add(new MethodInsnNode(INVOKESTATIC, "blockphysics/BlockPhysics", "onNeighborBlockChange",
                    "(Labv;IIII)V"));

            for (int index = m.instructions.size() - 1; index >= 0; index--) {
                if (m.instructions.get(index).getOpcode() == RETURN) {
                    m.instructions.insertBefore(m.instructions.get(index), toInject);
                    ok = true;
                    break;
                }
            }
        } else if (m.name.equals("g") && m.desc.equals("(Labv;IIII)V")) {
            InsnList toInject = new InsnList();
            toInject.add(new VarInsnNode(ALOAD, 1));
            toInject.add(new VarInsnNode(ILOAD, 2));
            toInject.add(new VarInsnNode(ILOAD, 3));
            toInject.add(new VarInsnNode(ILOAD, 4));
            toInject.add(new VarInsnNode(ILOAD, 5));
            toInject.add(new VarInsnNode(ALOAD, 0));
            toInject.add(new FieldInsnNode(GETFIELD, "arb", "cF", "I"));
            toInject.add(new MethodInsnNode(INVOKESTATIC, "blockphysics/BlockPhysics",
                    "onBlockDestroyedByPlayer", "(Labv;IIIII)V"));

            for (int index = m.instructions.size() - 1; index >= 0; index--) {
                if (m.instructions.get(index).getOpcode() == RETURN) {
                    m.instructions.insertBefore(m.instructions.get(index), toInject);
                    ok2 = true;
                    break;
                }
            }
        }
    }

    ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS);
    classNode.accept(cw);

    if (ok && ok2)
        System.out.println("OK");
    else
        System.out.println("Failed." + ok + ok2);

    /*try
    {
       FileOutputStream fos = new FileOutputStream("d:/BlockTNT.class");
       fos.write(cw.toByteArray());
     fos.close();
    } catch (IOException e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
    }*/

    return cw.toByteArray();
}

From source file:blockphysics.asm.BPTransformer.java

License:Open Source License

private byte[] transformBlockFarmland(byte[] bytes) {
    /*try// www.  ja v  a  2  s .  co  m
    {
        FileOutputStream fos = new FileOutputStream("d:/BlockFarmland.orig.class");
        fos.write(bytes);
      fos.close();
    } catch (IOException e) {
       // TODO Auto-generated catch block
       e.printStackTrace();
    }*/

    System.out.print("[BlockPhysics] Patching BlockFarmland.class ...........");
    boolean ok = false;

    ClassNode classNode = new ClassNode();
    ClassReader classReader = new ClassReader(bytes);
    classReader.accept(classNode, 0);

    MethodNode m;
    Iterator<MethodNode> methods = classNode.methods.iterator();
    while (methods.hasNext()) {
        m = methods.next();

        if (m.name.equals("a") && m.desc.equals("(Labv;IIILnm;F)V")) {
            InsnList toInject = new InsnList();

            toInject.add(new VarInsnNode(ALOAD, 1));
            toInject.add(new VarInsnNode(ILOAD, 2));
            toInject.add(new VarInsnNode(ILOAD, 3));
            toInject.add(new VarInsnNode(ILOAD, 4));
            toInject.add(new VarInsnNode(ALOAD, 0));
            toInject.add(new FieldInsnNode(GETFIELD, "aoc", "cF", "I"));
            toInject.add(new MethodInsnNode(INVOKESTATIC, "blockphysics/BlockPhysics", "onNeighborBlockChange",
                    "(Labv;IIII)V"));

            for (int index = m.instructions.size() - 1; index >= 0; index--) {
                if (m.instructions.get(index).getOpcode() == RETURN) {
                    m.instructions.insertBefore(m.instructions.get(index), toInject);
                    ok = true;
                    break;
                }
            }
        }
    }

    ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS);
    classNode.accept(cw);

    if (ok)
        System.out.println("OK");
    else
        System.out.println("Failed." + ok);

    /*try
    {
       FileOutputStream fos = new FileOutputStream("d:/BlockFarmland.class");
       fos.write(cw.toByteArray());
     fos.close();
    } catch (IOException e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
    }*/

    return cw.toByteArray();
}

From source file:blockphysics.asm.BPTransformer.java

License:Open Source License

private byte[] transformBlockDragonEgg(byte[] bytes) {
    /*try//  w  ww  . j  ava 2  s  . co m
    {
        FileOutputStream fos = new FileOutputStream("d:/BlockDragonEgg.orig.class");
        fos.write(bytes);
      fos.close();
    } catch (IOException e) {
       // TODO Auto-generated catch block
       e.printStackTrace();
    }*/

    System.out.print("[BlockPhysics] Patching BlockDragonEgg.class ..........");
    boolean ok = false, ok2 = false, ok3 = false, ok4 = false;

    ClassNode classNode = new ClassNode();
    ClassReader classReader = new ClassReader(bytes);
    classReader.accept(classNode, 0);

    MethodNode m;
    Iterator<MethodNode> methods = classNode.methods.iterator();
    while (methods.hasNext()) {
        m = methods.next();

        if (m.name.equals("a") && m.desc.equals("(Labv;IIII)V")) {
            InsnList toInject = new InsnList();
            toInject.add(new VarInsnNode(ALOAD, 1));
            toInject.add(new VarInsnNode(ILOAD, 2));
            toInject.add(new VarInsnNode(ILOAD, 3));
            toInject.add(new VarInsnNode(ILOAD, 4));
            toInject.add(new VarInsnNode(ALOAD, 0));
            toInject.add(new FieldInsnNode(GETFIELD, "any", "cF", "I"));
            toInject.add(new MethodInsnNode(INVOKESTATIC, "blockphysics/BlockPhysics", "onNeighborBlockChange",
                    "(Labv;IIII)V"));
            toInject.add(new InsnNode(RETURN));

            m.instructions.clear();
            m.localVariables.clear();
            m.instructions.add(toInject);
            ok = true;

        } else if (m.name.equals("a") && m.desc.equals("(Labv;IIILjava/util/Random;)V")) {
            m.instructions.clear();
            m.localVariables.clear();
            m.instructions.insert(new InsnNode(RETURN));
            ok2 = true;
        } else if (m.name.equals("k") && m.desc.equals("(Labv;III)V")) {
            m.instructions.clear();
            m.localVariables.clear();
            m.instructions.insert(new InsnNode(RETURN));
            ok3 = true;
        } else if (m.name.equals("m") && m.desc.equals("(Labv;III)V")) {
            InsnList toInject = new InsnList();
            toInject.add(new VarInsnNode(ALOAD, 1));
            toInject.add(new VarInsnNode(ILOAD, 6));
            toInject.add(new VarInsnNode(ILOAD, 7));
            toInject.add(new VarInsnNode(ILOAD, 8));
            toInject.add(
                    new MethodInsnNode(INVOKESTATIC, "blockphysics/BlockPhysics", "notifyMove", "(Labv;III)V"));

            for (int index = 0; index < m.instructions.size(); index++) {
                if (m.instructions.get(index).getOpcode() == INVOKEVIRTUAL
                        && m.instructions.get(index).getType() == AbstractInsnNode.METHOD_INSN
                        && ((MethodInsnNode) m.instructions.get(index)).owner.equals("abv")
                        && ((MethodInsnNode) m.instructions.get(index)).name.equals("i")
                        && ((MethodInsnNode) m.instructions.get(index)).desc.equals("(III)Z")) {

                    if (m.instructions.get(index + 1).getOpcode() == POP) {
                        m.instructions.insert(m.instructions.get(index + 1), toInject);
                    } else
                        m.instructions.insert(m.instructions.get(index), toInject);

                    ok4 = true;
                    break;
                }
            }
        }
    }

    ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS);
    classNode.accept(cw);

    if (ok && ok2 && ok3 & ok4)
        System.out.println("OK");
    else
        System.out.println("Failed." + ok + ok2 + ok3 + ok4);

    /*try
    {
       FileOutputStream fos = new FileOutputStream("d:/BlockDragonEgg.class");
       fos.write(cw.toByteArray());
     fos.close();
    } catch (IOException e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
    }*/

    return cw.toByteArray();
}

From source file:br.usp.each.saeg.badua.core.internal.instr.LabelFrameNode.java

License:Open Source License

public static void insertBefore(final AbstractInsnNode location, final InsnList insns, final InsnList insert) {

    final LabelFrameNode lfn = create(location);
    if (lfn != null) {
        insert.add(lfn);
    }/* w ww  .  ja  va2 s  . co  m*/
    insns.insertBefore(location, insert);
}