Skip to content

Commit

Permalink
feat: add replace and remove extensions (ReVanced#50)
Browse files Browse the repository at this point in the history
  • Loading branch information
bogadana authored Jun 24, 2022
1 parent 070c09c commit 92ac5e4
Showing 1 changed file with 46 additions and 3 deletions.
49 changes: 46 additions & 3 deletions src/main/kotlin/app/revanced/patcher/extensions/Extensions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,18 @@ fun MutableMethodImplementation.addInstructions(index: Int, instructions: List<B
}
}

fun MutableMethodImplementation.replaceInstructions(index: Int, instructions: List<BuilderInstruction>) {
for (i in instructions.lastIndex downTo 0) {
this.replaceInstruction(index + i, instructions[i])
}
}

fun MutableMethodImplementation.removeInstructions(index: Int, count: Int) {
for (i in count downTo 0) {
this.removeInstruction(index + i)
}
}

/**
* Compare a method to another, considering constructors and parameters.
* @param otherMethod The method to compare against.
Expand Down Expand Up @@ -66,13 +78,28 @@ internal fun Method.clone(
}

/**
* Add smali instructions to the method.
* @param index The index to insert the instructions at.
* Add a smali instruction to the method.
* @param index The index to insert the instruction at.
* @param instruction The smali instruction to add.
*/
fun MutableMethod.addInstruction(index: Int, instruction: String) =
this.implementation!!.addInstruction(index, instruction.toInstruction(this))

/**
* Replace a smali instruction within the method.
* @param index The index to replace the instruction at.
* @param instruction The smali instruction to place.
*/
fun MutableMethod.replaceInstruction(index: Int, instruction: String) =
this.implementation!!.replaceInstruction(index, instruction.toInstruction(this))

/**
* Remove a smali instruction within the method.
* @param index The index to delete the instruction at.
*/
fun MutableMethod.removeInstruction(index: Int) =
this.implementation!!.removeInstruction(index)

/**
* Add smali instructions to the method.
* @param index The index to insert the instructions at.
Expand All @@ -81,6 +108,22 @@ fun MutableMethod.addInstruction(index: Int, instruction: String) =
fun MutableMethod.addInstructions(index: Int, instructions: String) =
this.implementation!!.addInstructions(index, instructions.toInstructions(this))

/**
* Replace smali instructions within the method.
* @param index The index to replace the instructions at.
* @param instructions The smali instructions to place.
*/
fun MutableMethod.replaceInstructions(index: Int, instructions: String) =
this.implementation!!.replaceInstructions(index, instructions.toInstructions(this))

/**
* Remove smali instructions from the method.
* @param index The index to remove the instructions at.
* @param count The amount of instructions to remove.
*/
fun MutableMethod.removeInstructions(index: Int, count: Int) =
this.implementation!!.removeInstructions(index, count)

/**
* Clones the method.
* @param registerCount This parameter allows you to change the register count of the method.
Expand Down Expand Up @@ -108,4 +151,4 @@ internal fun parametersEqual(
internal val nullOutputStream: OutputStream =
object : OutputStream() {
override fun write(b: Int) {}
}
}

0 comments on commit 92ac5e4

Please sign in to comment.