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

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

Introduction

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

Prototype

public ClassNode() 

Source Link

Document

Constructs a new ClassNode .

Usage

From source file:blockphysics.asm.BPTransformer.java

License:Open Source License

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

    System.out.print("[BlockPhysics] Patching EntityMinecart.class ..........");

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

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

    MethodVisitor mv = cw.visitMethod(ACC_PUBLIC, "al", "()V", null, null);
    mv.visitCode();
    mv.visitInsn(RETURN);
    mv.visitMaxs(0, 1);
    mv.visitEnd();

    cw.visitEnd();

    System.out.println("OK");

    /*try
    {
       FileOutputStream fos = new FileOutputStream("d:/EntityMinecart.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[] transformEntityXPOrb(byte[] bytes) {
    /*try//from ww w  . ja  v  a  2s.  c  om
    {
        FileOutputStream fos = new FileOutputStream("d:/EntityXPOrb.orig.class");
        fos.write(bytes);
      fos.close();
    } catch (IOException e) {
       // TODO Auto-generated catch block
       e.printStackTrace();
    }*/

    System.out.print("[BlockPhysics] Patching EntityXPOrb.class .............");

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

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

    MethodVisitor mv = cw.visitMethod(ACC_PUBLIC, "al", "()V", null, null);
    mv.visitCode();
    mv.visitInsn(RETURN);
    mv.visitMaxs(0, 1);
    mv.visitEnd();

    cw.visitEnd();

    System.out.println("OK");

    /*try
    {
       FileOutputStream fos = new FileOutputStream("d:/EntityXPOrb.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[] transformEntityBoat(byte[] bytes) {
    /*try//from www. ja  v  a  2 s. c o m
    {
        FileOutputStream fos = new FileOutputStream("d:/EntityBoat.orig.class");
        fos.write(bytes);
      fos.close();
    } catch (IOException e) {
       // TODO Auto-generated catch block
       e.printStackTrace();
    }*/

    System.out.print("[BlockPhysics] Patching EntityBoat.class ..............");

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

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

    MethodVisitor mv = cw.visitMethod(ACC_PUBLIC, "al", "()V", null, null);
    mv.visitCode();
    mv.visitInsn(RETURN);
    mv.visitMaxs(0, 1);
    mv.visitEnd();

    cw.visitEnd();

    System.out.println("OK");

    /*try
    {
       FileOutputStream fos = new FileOutputStream("d:/EntityBoat.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[] transformBlockWeb(byte[] bytes) {
    /*try/*from ww  w.  j  a v  a 2s . co  m*/
    {
        FileOutputStream fos = new FileOutputStream("d:/BlockWeb.orig.class");
        fos.write(bytes);
      fos.close();
    } catch (IOException e) {
       // TODO Auto-generated catch block
       e.printStackTrace();
    }*/

    System.out.print("[BlockPhysics] Patching BlockWeb.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;)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, "arm", "cF", "I"));
            toInject.add(new VarInsnNode(ALOAD, 5));
            toInject.add(new MethodInsnNode(INVOKESTATIC, "blockphysics/BlockPhysics",
                    "onEntityCollidedWithBlock", "(Labv;IIIILnm;)V"));
            toInject.add(new InsnNode(RETURN));

            m.instructions.clear();
            m.localVariables.clear();
            m.instructions.add(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:/BlockWeb.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[] transformWorld(byte[] bytes) {
    /*try//from  w ww  . j  av a  2 s. c o  m
    {
        FileOutputStream fos = new FileOutputStream("d:/World.orig.class");
        fos.write(bytes);
      fos.close();
    } catch (IOException e) {
       // TODO Auto-generated catch block
       e.printStackTrace();
    }*/

    System.out.print("[BlockPhysics] Patching World.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("<init>")) {
            for (int index = m.instructions.size() - 1; index >= 0; index--) {
                if (m.instructions.get(index).getOpcode() == RETURN) {
                    InsnList toInject = new InsnList();
                    toInject.add(new VarInsnNode(ALOAD, 0));
                    toInject.add(new TypeInsnNode(NEW, "blockphysics/BTickList"));
                    toInject.add(new InsnNode(DUP));
                    toInject.add(new MethodInsnNode(INVOKESPECIAL, "blockphysics/BTickList", "<init>", "()V"));
                    toInject.add(
                            new FieldInsnNode(PUTFIELD, "abv", "moveTickList", "Lblockphysics/BTickList;"));
                    toInject.add(new VarInsnNode(ALOAD, 0));
                    toInject.add(new TypeInsnNode(NEW, "java/util/HashSet"));
                    toInject.add(new InsnNode(DUP));
                    toInject.add(new MethodInsnNode(INVOKESPECIAL, "java/util/HashSet", "<init>", "()V"));
                    toInject.add(new FieldInsnNode(PUTFIELD, "abv", "pistonMoveBlocks", "Ljava/util/HashSet;"));
                    toInject.add(new VarInsnNode(ALOAD, 0));
                    toInject.add(new TypeInsnNode(NEW, "blockphysics/ExplosionQueue"));
                    toInject.add(new InsnNode(DUP));
                    toInject.add(
                            new MethodInsnNode(INVOKESPECIAL, "blockphysics/ExplosionQueue", "<init>", "()V"));
                    toInject.add(new FieldInsnNode(PUTFIELD, "abv", "explosionQueue",
                            "Lblockphysics/ExplosionQueue;"));

                    m.instructions.insertBefore(m.instructions.get(index), toInject);
                    ok = true;
                }
            }
        } else if (m.name.equals("a") && m.desc.equals("(Lnm;DDDFZZ)Labq;"))
            ;
        {
            for (int index = m.instructions.size() - 1; index > 0; index--) {
                if (m.instructions.get(index).getOpcode() == INVOKEVIRTUAL
                        && m.instructions.get(index).getType() == AbstractInsnNode.METHOD_INSN
                        && ((MethodInsnNode) m.instructions.get(index)).owner.equals("abq")
                        && ((MethodInsnNode) m.instructions.get(index)).name.equals("a")
                        && ((MethodInsnNode) m.instructions.get(index)).desc.equals("(Z)V")) {

                    while (!(m.instructions.get(index).getOpcode() == INVOKEVIRTUAL
                            && m.instructions.get(index).getType() == AbstractInsnNode.METHOD_INSN
                            && ((MethodInsnNode) m.instructions.get(index)).owner.equals("abq")
                            && ((MethodInsnNode) m.instructions.get(index)).name.equals("a")
                            && ((MethodInsnNode) m.instructions.get(index)).desc.equals("()V"))) {
                        m.instructions.remove(m.instructions.get(index));
                        index--;
                    }
                    m.instructions.remove(m.instructions.get(index - 1));
                    m.instructions.remove(m.instructions.get(index - 1));

                    InsnList toInject = new InsnList();
                    toInject.add(new VarInsnNode(ALOAD, 0));
                    toInject.add(new FieldInsnNode(GETFIELD, "abv", "explosionQueue",
                            "Lblockphysics/ExplosionQueue;"));
                    toInject.add(new VarInsnNode(ALOAD, 11));
                    toInject.add(new MethodInsnNode(INVOKEVIRTUAL, "blockphysics/ExplosionQueue", "add",
                            "(Labq;)V"));

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

                    ok2 = true;
                    break;
                }
            }
        }
    }

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

    FieldVisitor fv;

    fv = cw.visitField(ACC_PUBLIC, "moveTickList", "Lblockphysics/BTickList;", null, null);
    fv.visitEnd();

    fv = cw.visitField(ACC_PUBLIC, "pistonMoveBlocks", "Ljava/util/HashSet;",
            "Ljava/util/HashSet<Ljava/lang/String;>;", null);
    fv.visitEnd();

    fv = cw.visitField(ACC_PUBLIC, "explosionQueue", "Lblockphysics/ExplosionQueue;", null, null);
    fv.visitEnd();

    cw.visitEnd();

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

    /*try
    {
       FileOutputStream fos = new FileOutputStream("d:/World.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[] transformWorldServer(byte[] bytes) {
    /*try/* ww w.  j  av a 2 s  .co m*/
    {
        FileOutputStream fos = new FileOutputStream("d:/WorldServer.orig.class");
        fos.write(bytes);
      fos.close();
    } catch (IOException e) {
       // TODO Auto-generated catch block
       e.printStackTrace();
    }*/

    boolean ok = false, ok2 = false;

    boolean bukkit = false;
    try {
        Class.forName("org.bukkit.Bukkit");
        System.out.println("[BlockPhysics] Bukkit detected.");
        bukkit = true;
        ok2 = true;
    } catch (ClassNotFoundException e) {

    }

    System.out.print("[BlockPhysics] Patching WorldServer.class .............");
    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("b") && m.desc.equals("()V")) {
            for (int index = m.instructions.size() - 1; index >= 0; index--) {
                if (m.instructions.get(index).getOpcode() == RETURN) {
                    InsnList toInject = new InsnList();
                    toInject.add(new VarInsnNode(ALOAD, 0));
                    toInject.add(new MethodInsnNode(INVOKESTATIC, "blockphysics/BlockPhysics",
                            "tickBlocksRandomMove", "(Ljr;)V"));
                    toInject.add(new VarInsnNode(ALOAD, 0));
                    toInject.add(new FieldInsnNode(GETFIELD, "jr", "moveTickList", "Lblockphysics/BTickList;"));
                    toInject.add(new VarInsnNode(ALOAD, 0));
                    toInject.add(new MethodInsnNode(INVOKEVIRTUAL, "blockphysics/BTickList", "tickMoveUpdates",
                            "(Labv;)V"));
                    toInject.add(new VarInsnNode(ALOAD, 0));
                    toInject.add(new FieldInsnNode(GETFIELD, "jr", "pistonMoveBlocks", "Ljava/util/HashSet;"));
                    toInject.add(new MethodInsnNode(INVOKEVIRTUAL, "java/util/HashSet", "clear", "()V"));
                    toInject.add(new VarInsnNode(ALOAD, 0));
                    toInject.add(new FieldInsnNode(GETFIELD, "jr", "explosionQueue",
                            "Lblockphysics/ExplosionQueue;"));
                    toInject.add(new MethodInsnNode(INVOKEVIRTUAL, "blockphysics/ExplosionQueue",
                            "doNextExplosion", "()V"));

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

                    ok = true;
                    break;
                }
            }
        } else if (!bukkit && m.name.equals("a") && m.desc.equals("(Lnm;DDDFZZ)Labq;"))
            ;
        {
            for (int index = m.instructions.size() - 1; index > 0; index--) {
                if (m.instructions.get(index).getOpcode() == INVOKEVIRTUAL
                        && m.instructions.get(index).getType() == AbstractInsnNode.METHOD_INSN
                        && ((MethodInsnNode) m.instructions.get(index)).owner.equals("abq")
                        && ((MethodInsnNode) m.instructions.get(index)).name.equals("a")
                        && ((MethodInsnNode) m.instructions.get(index)).desc.equals("(Z)V")) {

                    while (!(m.instructions.get(index).getOpcode() == INVOKEVIRTUAL
                            && m.instructions.get(index).getType() == AbstractInsnNode.METHOD_INSN
                            && ((MethodInsnNode) m.instructions.get(index)).owner.equals("abq")
                            && ((MethodInsnNode) m.instructions.get(index)).name.equals("a")
                            && ((MethodInsnNode) m.instructions.get(index)).desc.equals("()V"))) {
                        m.instructions.remove(m.instructions.get(index));
                        index--;
                    }
                    m.instructions.remove(m.instructions.get(index - 1));
                    m.instructions.remove(m.instructions.get(index - 1));

                    InsnList toInject = new InsnList();
                    toInject.add(new VarInsnNode(ALOAD, 0));
                    toInject.add(new FieldInsnNode(GETFIELD, "jr", "explosionQueue",
                            "Lblockphysics/ExplosionQueue;"));
                    toInject.add(new VarInsnNode(ALOAD, 11));
                    toInject.add(new MethodInsnNode(INVOKEVIRTUAL, "blockphysics/ExplosionQueue", "add",
                            "(Labq;)V"));

                    m.instructions.insertBefore(m.instructions.get(index - 1), 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:/WorldServer.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[] transformExplosion(byte[] bytes) {
    /*try//w  w  w.  j a  v  a 2 s  . com
    {
        FileOutputStream fos = new FileOutputStream("d:/Explosion.orig.class");
        fos.write(bytes);
      fos.close();
    } catch (IOException e) {
       // TODO Auto-generated catch block
       e.printStackTrace();
    }*/

    System.out.print("[BlockPhysics] Patching Explosion.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("<init>") && m.desc.equals("(Labv;Lnm;DDDF)V"))) {
            InsnList toInject = new InsnList();
            toInject.add(new VarInsnNode(ALOAD, 0));
            toInject.add(new InsnNode(ICONST_0));
            toInject.add(new FieldInsnNode(PUTFIELD, "abq", "impact", "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("()V"))) {
            InsnList toInject = new InsnList();
            toInject.add(new VarInsnNode(ALOAD, 0));
            toInject.add(new FieldInsnNode(GETFIELD, "abq", "k", "Labv;"));
            toInject.add(new VarInsnNode(ALOAD, 0));
            toInject.add(new MethodInsnNode(INVOKESTATIC, "blockphysics/BlockPhysics", "doExplosionA",
                    "(Labv;Labq;)V"));
            toInject.add(new InsnNode(RETURN));

            m.instructions.clear();
            m.localVariables.clear();
            m.instructions.add(toInject);
            ok2 = true;
        } else if (m.name.equals("a") && m.desc.equals("(Z)V")) {

            InsnList toInject = new InsnList();
            toInject.add(new VarInsnNode(ALOAD, 0));
            toInject.add(new FieldInsnNode(GETFIELD, "abq", "k", "Labv;"));
            toInject.add(new VarInsnNode(ALOAD, 0));
            toInject.add(new VarInsnNode(ILOAD, 1));
            toInject.add(new MethodInsnNode(INVOKESTATIC, "blockphysics/BlockPhysics", "doExplosionB",
                    "(Labv;Labq;Z)V"));
            toInject.add(new InsnNode(RETURN));

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

    }

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

    FieldVisitor fv;
    fv = cw.visitField(ACC_PUBLIC, "impact", "Z", null, null);
    fv.visitEnd();

    cw.visitEnd();

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

    /*try
    {
       FileOutputStream fos = new FileOutputStream("d:/Explosion.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[] transformMinecraftServer(byte[] bytes) {
    /*try//w  w  w .j  a  v  a  2s  .  c om
    {
        FileOutputStream fos = new FileOutputStream("d:/MinecraftServer.orig.class");
        fos.write(bytes);
      fos.close();
    } catch (IOException e) {
       // TODO Auto-generated catch block
       e.printStackTrace();
    }*/

    boolean bukkit = false;
    try {
        Class.forName("org.bukkit.Bukkit");
        System.out.println("[BlockPhysics] Bukkit detected.");
        bukkit = true;
    } catch (ClassNotFoundException e) {

    }

    System.out.print("[BlockPhysics] Patching MinecraftServer.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("run") && m.desc.equals("()V")) {
            if (bukkit) {
                int index;
                for (index = 0; index < m.instructions.size(); index++) {
                    if (m.instructions.get(index).getType() == AbstractInsnNode.METHOD_INSN
                            && m.instructions.get(index).getOpcode() == INVOKESTATIC
                            && ((MethodInsnNode) m.instructions.get(index)).owner.equals("java/lang/System")
                            && ((MethodInsnNode) m.instructions.get(index)).name.equals("nanoTime")) {
                        ok = true;
                        break;
                    }
                }

                if (ok) {
                    while (index < m.instructions.size()) {
                        if (m.instructions.get(index).getOpcode() == LSUB) {
                            InsnList toInject = new InsnList();
                            toInject.add(new MethodInsnNode(INVOKESTATIC, "blockphysics/BlockPhysics",
                                    "setSkipMoveM", "(J)J"));

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

                            ok2 = true;
                            break;
                        }
                        index++;
                    }
                }
            } else {
                int index;
                for (index = 0; index < m.instructions.size(); index++) {
                    if (m.instructions.get(index).getType() == AbstractInsnNode.METHOD_INSN
                            && m.instructions.get(index).getOpcode() == INVOKEVIRTUAL
                            && ((MethodInsnNode) m.instructions.get(index)).owner
                                    .equals("net/minecraft/server/MinecraftServer")
                            && ((MethodInsnNode) m.instructions.get(index)).name.equals("s")) {
                        ok = true;
                        break;
                    }
                }

                if (ok) {
                    while (index > 0) {

                        if (m.instructions.get(index).getOpcode() == LLOAD
                                && m.instructions.get(index).getType() == AbstractInsnNode.VAR_INSN
                                && ((VarInsnNode) m.instructions.get(index)).var == 3) {
                            InsnList toInject = new InsnList();
                            toInject.add(new VarInsnNode(LLOAD, 7));
                            toInject.add(new MethodInsnNode(INVOKESTATIC, "blockphysics/BlockPhysics",
                                    "setSkipMove", "(J)V"));

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

                            ok2 = true;
                            break;
                        }
                        index--;
                    }
                }
            }
        }
    }

    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:/MinecraftServer.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[] transformEntityTrackerEntry(byte[] bytes) {
    /*try//from  w  w w  .  jav  a  2 s .  co m
    {
        FileOutputStream fos = new FileOutputStream("d:/EntityTrackerEntry.orig.class");
        fos.write(bytes);
      fos.close();
    } catch (IOException e) {
       // TODO Auto-generated catch block
       e.printStackTrace();
    }*/

    System.out.print("[BlockPhysics] Patching EntityTrackerEntry.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("c") && m.desc.equals("()Lex;")) {
            for (int index = 0; index < m.instructions.size(); index++) {
                if (m.instructions.get(index).getType() == AbstractInsnNode.TYPE_INSN
                        && m.instructions.get(index).getOpcode() == INSTANCEOF
                        && ((TypeInsnNode) m.instructions.get(index)).desc.equals("sq")) {
                    while (m.instructions.get(index).getOpcode() != IFEQ) {
                        index++;
                    }
                    index++;

                    while (m.instructions.get(index).getOpcode() != ARETURN) {
                        m.instructions.remove(m.instructions.get(index));
                    }

                    InsnList toInject = new InsnList();
                    toInject.add(new VarInsnNode(ALOAD, 0));
                    toInject.add(new FieldInsnNode(GETFIELD, "jw", "a", "Lnm;"));
                    toInject.add(new TypeInsnNode(CHECKCAST, "sq"));
                    toInject.add(new MethodInsnNode(INVOKESTATIC, "blockphysics/BlockPhysics",
                            "spawnFallingSandPacket", "(Lsq;)Ldc;"));

                    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:/EntityTrackerEntry.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[] transformEntityTracker(byte[] bytes) {
    /*try//from   w  w  w  .ja v  a 2s  . co m
    {
        FileOutputStream fos = new FileOutputStream("d:/EntityTracker.orig.class");
        fos.write(bytes);
      fos.close();
    } catch (IOException e) {
       // TODO Auto-generated catch block
       e.printStackTrace();
    }*/

    System.out.print("[BlockPhysics] Patching EntityTracker.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("<init>") && m.desc.equals("(Ljr;)V")) {
            for (int index = m.instructions.size() - 1; index >= 0; index--) {
                if (m.instructions.get(index).getOpcode() == RETURN) {
                    InsnList toInject = new InsnList();
                    toInject.add(new VarInsnNode(ALOAD, 0));
                    toInject.add(new InsnNode(ICONST_0));
                    toInject.add(new FieldInsnNode(PUTFIELD, "jl", "movingblocks", "I"));

                    m.instructions.insertBefore(m.instructions.get(index), toInject);
                    ok = true;
                    break;
                }
            }
        } else if (m.name.equals("a") && m.desc.equals("(Lnm;)V")) {
            for (int index = 0; index < m.instructions.size(); index++) {
                if (m.instructions.get(index).getType() == AbstractInsnNode.TYPE_INSN
                        && m.instructions.get(index).getOpcode() == INSTANCEOF
                        && ((TypeInsnNode) m.instructions.get(index)).desc.equals("tb")) {
                    m.instructions.remove(m.instructions.get(index - 1));

                    while (m.instructions.get(index).getType() != AbstractInsnNode.TYPE_INSN
                            || m.instructions.get(index).getOpcode() != INSTANCEOF) {
                        m.instructions.remove(m.instructions.get(index - 1));
                        ok2 = true;
                    }
                    break;
                }
            }

            for (int index = 0; index < m.instructions.size(); index++) {
                if (m.instructions.get(index).getType() == AbstractInsnNode.TYPE_INSN
                        && m.instructions.get(index).getOpcode() == INSTANCEOF
                        && ((TypeInsnNode) m.instructions.get(index)).desc.equals("sq")) {
                    while (m.instructions.get(index).getOpcode() != GOTO) {
                        index++;
                        if (m.instructions.get(index).getType() == AbstractInsnNode.INT_INSN
                                && m.instructions.get(index).getOpcode() == BIPUSH
                                && ((IntInsnNode) m.instructions.get(index)).operand == 20) {
                            m.instructions.set(m.instructions.get(index), new IntInsnNode(BIPUSH, 40));
                            ok3 = true;
                        }
                    }

                    InsnList toInject = new InsnList();
                    toInject.add(new VarInsnNode(ALOAD, 0));
                    toInject.add(new InsnNode(DUP));
                    toInject.add(new FieldInsnNode(GETFIELD, "jl", "movingblocks", "I"));
                    toInject.add(new InsnNode(ICONST_1));
                    toInject.add(new InsnNode(IADD));
                    toInject.add(new FieldInsnNode(PUTFIELD, "jl", "movingblocks", "I"));

                    m.instructions.insertBefore(m.instructions.get(index), toInject);
                    break;
                }
            }
        } else if (m.name.equals("b") && m.desc.equals("(Lnm;)V")) {
            InsnList toInject = new InsnList();

            toInject.add(new VarInsnNode(ALOAD, 1));
            toInject.add(new TypeInsnNode(INSTANCEOF, "sq"));
            LabelNode l3 = new LabelNode();
            toInject.add(new JumpInsnNode(IFEQ, l3));
            toInject.add(new VarInsnNode(ALOAD, 0));
            toInject.add(new InsnNode(DUP));
            toInject.add(new FieldInsnNode(GETFIELD, "jl", "movingblocks", "I"));
            toInject.add(new InsnNode(ICONST_1));
            toInject.add(new InsnNode(ISUB));
            toInject.add(new FieldInsnNode(PUTFIELD, "jl", "movingblocks", "I"));
            toInject.add(l3);

            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;
                }
            }
        }
    }

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

    FieldVisitor fv;

    fv = cw.visitField(ACC_PUBLIC, "movingblocks", "I", null, null);
    fv.visitEnd();

    cw.visitEnd();

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

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

    return cw.toByteArray();
}