refactor: simplify loading patches (#172)

This commit is contained in:
Jim Man 2023-05-14 02:55:26 +03:00 committed by GitHub
parent 7f02b8df48
commit 239ea0bcaa
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 9 additions and 35 deletions

View File

@ -54,17 +54,6 @@ class BytecodeContext internal constructor(classes: MutableList<ClassDef>) : Con
} }
return proxy return proxy
} }
private companion object {
inline fun <reified T> Iterable<T>.find(predicate: (T) -> Boolean): T? {
for (element in this) {
if (predicate(element)) {
return element
}
}
return null
}
}
} }
/** /**

View File

@ -41,18 +41,13 @@ sealed class PatchBundle(path: String) : File(path) {
arrayOf(this.toURI().toURL()), arrayOf(this.toURI().toURL()),
Thread.currentThread().contextClassLoader // TODO: find out why this is required Thread.currentThread().contextClassLoader // TODO: find out why this is required
), ),
StringIterator( JarFile(this)
JarFile(this) .stream()
.entries() .filter {it.name.endsWith(".class") && !it.name.contains("$")}
.toList() // TODO: find a cleaner solution than that to filter non class files .map({it -> it.realName.replace('/', '.').replace(".class", "")}).iterator()
.filter { )
it.name.endsWith(".class") && !it.name.contains("$")
}
.iterator()
) {
it.realName.replace('/', '.').replace(".class", "")
}
)
} }
/** /**
@ -68,8 +63,8 @@ sealed class PatchBundle(path: String) : File(path) {
* Patches will be loaded to the provided [dexClassLoader]. * Patches will be loaded to the provided [dexClassLoader].
*/ */
fun loadPatches() = loadPatches(dexClassLoader, fun loadPatches() = loadPatches(dexClassLoader,
StringIterator(DexFileFactory.loadDexFile(path, null).classes.iterator()) { classDef -> DexFileFactory.loadDexFile(path, null).classes.asSequence().map({ classDef ->
classDef.type.substring(1, classDef.length - 1).replace('/', '.') classDef.type.substring(1, classDef.length - 1).replace('/', '.')
}) }).iterator())
} }
} }

View File

@ -1,10 +0,0 @@
package app.revanced.patcher.util.patch
internal class StringIterator<T, I : Iterator<T>>(
private val iterator: I,
private val _next: (T) -> String
) : Iterator<String> {
override fun hasNext() = iterator.hasNext()
override fun next() = _next(iterator.next())
}