public static void renameClassNode(final String oldName, final String newName) {
    for (ClassNode c : BytecodeViewer.getLoadedClasses()) {
      for (Object oo : c.innerClasses) {
        InnerClassNode innerClassNode = (InnerClassNode) oo;
        if (innerClassNode.innerName != null && innerClassNode.innerName.equals(oldName)) {
          innerClassNode.innerName = newName;
        }
        if (innerClassNode.name.equals(oldName)) {
          innerClassNode.name = newName;
        }
        if (innerClassNode.outerName != null && innerClassNode.outerName.equals(oldName)) {
          innerClassNode.outerName = newName;
        }
      }

      if (c.signature != null) c.signature = c.signature.replace(oldName, newName);

      if (c.superName.equals(oldName)) {
        c.superName = newName;
      }
      for (Object o : c.fields.toArray()) {
        FieldNode f = (FieldNode) o;
        f.desc = f.desc.replace(oldName, newName);
      }
      for (Object o : c.interfaces.toArray()) {
        String truxerLipton = (String) o;
        truxerLipton = truxerLipton.replace(oldName, newName);
      }
      for (Object o : c.methods.toArray()) {
        MethodNode m = (MethodNode) o;

        if (m.localVariables != null) {
          for (LocalVariableNode node : (List<LocalVariableNode>) m.localVariables) {
            node.desc = node.desc.replace(oldName, newName);
          }
        }

        if (m.signature != null) m.signature = m.signature.replace(oldName, newName);

        for (int i = 0; i < m.exceptions.size(); i++) {
          if (m.exceptions.get(i).equals(oldName)) m.exceptions.set(i, newName);
        }

        for (AbstractInsnNode i : m.instructions.toArray()) {
          if (i instanceof TypeInsnNode) {
            TypeInsnNode t = (TypeInsnNode) i;
            if (t.desc.equals(oldName)) {
              t.desc = newName;
            }
          }
          if (i instanceof MethodInsnNode) {
            MethodInsnNode mi = (MethodInsnNode) i;
            if (mi.owner.equals(oldName)) mi.owner = newName;
            mi.desc = mi.desc.replace(oldName, newName);
          }
          if (i instanceof FieldInsnNode) {
            FieldInsnNode fi = (FieldInsnNode) i;
            if (fi.owner.equals(oldName)) fi.owner = newName;
            fi.desc = fi.desc.replace(oldName, newName);
          }
        }
      }
    }
  }
Esempio n. 2
0
  @SuppressWarnings("unchecked")
  private void modify(ClassNode node) {
    if (!mappings.containsKey(node.name)) return;
    JSONObject object = mappings.get(node.name);

    String identity = (String) object.get("identity");
    String parentOverride = (String) object.get("parent_override");
    if (parentOverride != null && !parentOverride.equals("null")) {
      node.superName = parentOverride;
      if (node.superName.contains("Canvas")) {
        for (MethodNode mn : (List<MethodNode>) node.methods) {
          if (mn.name.equals("<init>")) {
            for (AbstractInsnNode insn : mn.instructions.toArray()) {
              if (insn.getOpcode() == Opcodes.INVOKESPECIAL) {
                MethodInsnNode min = (MethodInsnNode) insn;
                if (min.owner.contains("Canvas")) {
                  min.owner = node.superName;
                }
              }
            }
          }
        }
      }

      // System.out.println("\t"+node.name+" parent changed to "+parentOverride);
    }

    JSONArray interfaces = (JSONArray) object.get("interfaces");
    for (int i = 0; i < interfaces.size(); i++) {
      if (!node.interfaces.contains(interfaces.get(i))) {
        // System.out.println("\tAdded interface " + interfaces.get(i) + " to " + identity + " (" +
        // node.name + ")");

        node.interfaces.add(interfaces.get(i));
      }
    }

    // remove interfaces not specified
    for (int i = 0; i < node.interfaces.size(); i++) {
      if (!interfaces.contains(node.interfaces.get(i))) {
        // System.out.println("\tRemoved interface " + node.interfaces.get(i) + " from " + identity
        // + " (" + node.name + ")");
        node.interfaces.remove(i);
      }
    }

    if (identity.equals("Renderable")) {

      for (MethodNode mn : (List<MethodNode>) node.methods) {
        if ((mn.access & Opcodes.ACC_STATIC) != 0) {
          continue;
        }

        if (!mn.desc.endsWith("V")) {
          continue;
        }

        InstructionSearcher searcher =
            new InstructionSearcher(mn.instructions, 0, Opcodes.INVOKEVIRTUAL, Opcodes.ASTORE);

        if (searcher.match()) {
          InstructionSearcher search =
              new InstructionSearcher(mn.instructions, 0, Opcodes.INVOKEVIRTUAL);
          AbstractInsnNode[] primMatches = searcher.getMatches().get(0);
          if (search.match()) {
            AbstractInsnNode[] matches = search.getMatches().get(1);

            VarInsnNode var = new VarInsnNode(Opcodes.ALOAD, 0);
            VarInsnNode var2 =
                new VarInsnNode(
                    Opcodes.ALOAD, ((VarInsnNode) primMatches[primMatches.length - 1]).var);

            mn.instructions.insert(matches[matches.length - 1], var);
            mn.instructions.insert(var, var2);
            mn.instructions.insert(
                var2,
                new MethodInsnNode(
                    Opcodes.INVOKESTATIC,
                    "org/vinsert/bot/script/callback/ModelCallback",
                    "callback",
                    "(Lorg/vinsert/insertion/IRenderable;Lorg/vinsert/insertion/IModel;)V"));

            // System.out.println("\tInserted model callback to "+node.name+"."+mn.name);
          }
        }
      }
    }

    //		if (identity.equals("GameObject")) {
    //			for (MethodNode mn : (List<MethodNode>) node.methods) {
    //				if ((mn.access & Opcodes.ACC_STATIC) != 0) {
    //					continue;
    //				}
    //
    //				if (mn.desc.endsWith("V")) {
    //					continue;
    //				}
    //
    //				InstructionSearcher searcher = new InstructionSearcher(mn.instructions, 10, Opcodes.LDC,
    // Opcodes.IMUL, Opcodes.LDC, Opcodes.INVOKEVIRTUAL, Opcodes.ARETURN);
    //
    //				if (searcher.match()) {
    //
    //					AbstractInsnNode[] matches = searcher.getMatches().get(0);
    //
    //					VarInsnNode var = new VarInsnNode(Opcodes.ALOAD, 0);
    //					VarInsnNode var2 = new VarInsnNode(Opcodes.ALOAD, ((VarInsnNode) matches[matches.length -
    // 1]).var);
    ////
    //					mn.instructions.insert(mn.instructions.get(mn.instructions.size() - 1), var);
    //					mn.instructions.insert(var, var2);
    //					mn.instructions.insert(var2, new MethodInsnNode(Opcodes.INVOKESTATIC,
    // "com/hijackrs/bot/script/callback/ModelCallback", "callback",
    // "(Lcom/hijackrs/insertion/Renderable;Lcom/hijackrs/insertion/Model;)V"));
    //					System.out.println("\tInserted model callback to "+node.name+"."+mn.name);
    //				}
    //			}
    //		}

    JSONArray mappings = (JSONArray) object.get("mappings");
    for (int i = 0; i < mappings.size(); i++) {
      JSONObject entry = (JSONObject) mappings.get(i);
      String parent = (String) entry.get("parent");
      String name = (String) entry.get("name");
      String getterName = (String) entry.get("identity");
      String signature = (String) entry.get("signature");
      String fieldSignature = (String) entry.get("field_signature");
      boolean isStatic = (Boolean) entry.get("static");
      int opLoad = ((Number) entry.get("op_load")).intValue();
      int opReturn = ((Number) entry.get("op_return")).intValue();
      int modBits = ((Number) entry.get("mod_bits")).intValue();
      int modDirect = ((Number) entry.get("mod_value")).intValue();
      boolean modInverse = (Boolean) entry.get("mod_inverse");

      insert(
          node,
          parent,
          name,
          getterName,
          signature,
          fieldSignature,
          isStatic,
          opLoad,
          opReturn,
          modBits,
          modDirect,
          modInverse);
    }
  }