Skip to content

Commit

Permalink
refactor: Change all references from Array to Iterable
Browse files Browse the repository at this point in the history
BREAKING CHANGE: arrayOf has to be changed to listOf.
  • Loading branch information
Sculas authored and oSumAtrIX committed Jun 5, 2022
1 parent 9659a61 commit 72f3cad
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 16 deletions.
6 changes: 3 additions & 3 deletions src/main/kotlin/app/revanced/patcher/Patcher.kt
Expand Up @@ -20,11 +20,11 @@ val NAMER = BasicDexFileNamer()
/**
* ReVanced Patcher.
* @param input The input file (an apk or any other multi dex container).
* @param signatures An array of method signatures for the patches
* @param signatures A list of method signatures for the patches.
*/
class Patcher(
input: File,
signatures: Array<MethodSignature>,
signatures: Iterable<MethodSignature>,
) {
private val cache: Cache
private val patches = mutableSetOf<Patch>()
Expand Down Expand Up @@ -92,7 +92,7 @@ class Patcher(
* Add a patch to the patcher.
* @param patches The patches to add.
*/
fun addPatches(vararg patches: Patch) {
fun addPatches(patches: Iterable<Patch>) {
this.patches.addAll(patches)
}

Expand Down
Expand Up @@ -15,6 +15,6 @@ data class MethodSignature(
val name: String,
val returnType: String?,
val accessFlags: Int?,
val methodParameters: Array<String>?,
val opcodes: Array<Opcode>?
val methodParameters: Iterable<String>?,
val opcodes: Iterable<Opcode>?
)
Expand Up @@ -13,7 +13,7 @@ import org.jf.dexlib2.iface.instruction.Instruction
// TODO: add logger back
internal class SignatureResolver(
private val classes: Set<ClassDef>,
private val methodSignatures: Array<MethodSignature>
private val methodSignatures: Iterable<MethodSignature>
) {
fun resolve(): MethodMap {
val methodMap = MethodMap()
Expand Down Expand Up @@ -84,22 +84,23 @@ internal class SignatureResolver(
}
}

private fun compareParameterTypes(signature: Array<String>, original: MutableList<out CharSequence>): Boolean {
return signature.size != original.size || !(signature.all { a -> original.any { it.startsWith(a) } })
private fun compareParameterTypes(signature: Iterable<String>, original: MutableList<out CharSequence>): Boolean {
return signature.count() != original.size || !(signature.all { a -> original.any { it.startsWith(a) } })
}
}
}

private operator fun ClassDef.component1() = this
private operator fun ClassDef.component2() = this.methods

private fun MutableIterable<Instruction>.scanFor(pattern: Array<Opcode>): PatternScanResult? {
private fun MutableIterable<Instruction>.scanFor(pattern: Iterable<Opcode>): PatternScanResult? {
val count = this.count()
val size = pattern.count()
for (instructionIndex in 0 until count) {
var patternIndex = 0
while (instructionIndex + patternIndex < count) {
if (this.elementAt(instructionIndex + patternIndex).opcode != pattern[patternIndex]) break
if (++patternIndex < pattern.size) continue
if (this.elementAt(instructionIndex + patternIndex).opcode != pattern.elementAt(patternIndex)) break
if (++patternIndex < size) continue

return PatternScanResult(instructionIndex, instructionIndex + patternIndex)
}
Expand Down
10 changes: 5 additions & 5 deletions src/test/kotlin/app/revanced/patcher/PatcherTest.kt
Expand Up @@ -23,13 +23,13 @@ import kotlin.test.assertTrue

internal class PatcherTest {
companion object {
val testSignatures: Array<MethodSignature> = arrayOf(
val testSignatures = listOf(
MethodSignature(
"main-method",
"V",
AccessFlags.PUBLIC or AccessFlags.STATIC,
arrayOf("[L"),
arrayOf(
listOf("[L"),
listOf(
Opcode.CONST_STRING,
Opcode.INVOKE_VIRTUAL,
Opcode.RETURN_VOID
Expand All @@ -45,7 +45,7 @@ internal class PatcherTest {
testSignatures
)

patcher.addPatches(
patcher.addPatches(listOf(
object : Patch("TestPatch") {
override fun execute(cache: Cache): PatchResult {
// Get the result from the resolver cache
Expand Down Expand Up @@ -146,7 +146,7 @@ internal class PatcherTest {
return PatchResultSuccess()
}
}
)
))

// Apply all patches loaded in the patcher
val patchResult = patcher.applyPatches()
Expand Down

0 comments on commit 72f3cad

Please sign in to comment.