refactor: Change all references from Array to Iterable

BREAKING CHANGE: arrayOf has to be changed to listOf.
This commit is contained in:
Lucaskyy 2022-04-11 16:29:53 +02:00
parent 4281546f69
commit 264989f488
No known key found for this signature in database
GPG key ID: 1530BFF96D1EEB89
4 changed files with 17 additions and 16 deletions

View file

@ -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>()
@ -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)
}

View file

@ -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>?
)

View file

@ -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()
@ -84,8 +84,8 @@ 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) } })
}
}
}
@ -93,13 +93,14 @@ internal class SignatureResolver(
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)
}

View file

@ -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
@ -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
@ -146,7 +146,7 @@ internal class PatcherTest {
return PatchResultSuccess()
}
}
)
))
// Apply all patches loaded in the patcher
val patchResult = patcher.applyPatches()