revanced-patcher icon indicating copy to clipboard operation
revanced-patcher copied to clipboard

feat: respecting order of insertions using new extension functions

Open 1fexd opened this issue 1 year ago • 0 comments

I have created a "concept" for new utility methods:

  • insertInstructions extension method on MutableMethod adds a simple way to insert multiple instructions without having to mange their index yourself

For example, if I want to insert two instructions at the beginning of a method, I could do this:

addInstructions(0, "instruction-1")
addInstructions(1, "instruction-2")

This works fine and is quite readable, but consider this:

addInstructions(0, """
    instruction-1
    instruction-2
    instruction-3
""")
addInstructions(3, "instruction-4")

In order to insert instruction-4 after the first 3 instructions, I have to calculate it's index like this: startIndex + amount_previously_added_instructions (e.g. 0 + 3), which might become a bit tedious.

Another option to achieve the desired result would be the following:

addInstructions(0, "instruction-4")
addInstructions(0, """
    instruction-1
    instruction-2
    instruction-3
""")

However, this might be confusing at first sight since one could assume the result would be this:

instruction-4
instruction-1
instruction-2
instruction-3

Therefore, I have added the insertInstructions which automatically calculates the index for the next instruction insert

  • hasInstruction checks if at least one instruction has the specified opcode and is the correct implementation of Instruction and returns a CustomFingerprint
  • ReferencedMethodDocumentation is an annotation that's only present in source code and allows the developer to specify exactly which method a fingerprint targets (and in which version of the app), so it can easily be found by the developer while debugging or updating a patch
  • ImmutableExtensions adds a match extension method on ImmutableMethodReference which allows to check if a ReferenceInstruction matches a ImmutableMethodReference (which could be used as a constant in the fingerprint)
    • This should also be added for other Immutable* classes to allow matching of other Instructions

To demostrate the use of these utilities, I have updated the patch from #2386 @ revanced-patches here: BadgeTabPatch.kt, ShowNotificationFingerprint.kt, CreateTabsFingerprint.kt

Let me know what you think.

1fexd avatar Jun 11 '23 11:06 1fexd