Example #1
0
  public void addTinkersDrawHoveringTextHook(
      MethodNode method,
      Class<?> hookClass,
      String hookMethod,
      String hookDesc,
      boolean isObfuscated) {
    AbstractInsnNode targetNode = null;

    // get last drawGradientRect call
    for (AbstractInsnNode instruction : method.instructions.toArray()) {
      if (instruction.getOpcode() == INVOKEVIRTUAL) {
        MethodInsnNode methodInsn = (MethodInsnNode) instruction;

        if (methodInsn.desc.equals("(IIIIII)V")) targetNode = instruction;
      }
    }
    if (targetNode == null) {
      AppleCore.Log.warn("Could not patch " + method.name + "; target node not found");
      return;
    }

    LocalVariableNode x = ASMHelper.findLocalVariableOfMethod(method, "i1", "I");
    LocalVariableNode y = ASMHelper.findLocalVariableOfMethod(method, "j1", "I");
    LocalVariableNode w = ASMHelper.findLocalVariableOfMethod(method, "k", "I");
    LocalVariableNode h = ASMHelper.findLocalVariableOfMethod(method, "k1", "I");

    if (x == null || y == null || w == null || h == null) {
      AppleCore.Log.warn("Could not patch " + method.name + "; local variables not found");
      return;
    }

    InsnList toInject = new InsnList();

    /*
    // equivalent to:
    Hooks.onDrawHoveringText(0, 0, 0, 0);
    */

    toInject.add(new VarInsnNode(ILOAD, x.index));
    toInject.add(new VarInsnNode(ILOAD, y.index));
    toInject.add(new VarInsnNode(ILOAD, w.index));
    toInject.add(new VarInsnNode(ILOAD, h.index));
    toInject.add(
        new MethodInsnNode(
            INVOKESTATIC, hookClass.getName().replace('.', '/'), hookMethod, hookDesc, false));

    method.instructions.insert(targetNode, toInject);
  }