Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add replaceInstruction and removeInstruction extensions for parity with add #50

Merged
merged 1 commit into from
Jun 24, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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) {}
}
}