Skip to content

Commit

Permalink
feat: Minor refactor and return proxy, if class has been proxied already
Browse files Browse the repository at this point in the history
  • Loading branch information
oSumAtrIX committed Jun 5, 2022
1 parent ce21bd6 commit 4b26305
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 7 deletions.
2 changes: 1 addition & 1 deletion src/main/kotlin/app/revanced/patcher/Patcher.kt
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class Patcher(
// TODO: the iterator would return the proxied class matching the current index of the list
// TODO: instead of the original class
for (classProxy in cache.classProxy) {
if (!classProxy.proxyused) continue
if (!classProxy.proxyUsed) continue
// TODO: merge this class with cache.classes somehow in an iterator
classProxy.mutatedClass
}
Expand Down
10 changes: 7 additions & 3 deletions src/main/kotlin/app/revanced/patcher/cache/Cache.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,15 @@ class Cache(
internal val classes: Set<ClassDef>,
val resolvedMethods: MethodMap
) {
internal val classProxy = mutableListOf<ClassProxy>()
internal val classProxy = mutableSetOf<ClassProxy>()

fun findClass(predicate: (ClassDef) -> Boolean): ClassProxy? {
// if a class has been found with the given predicate,
val foundClass = classes.singleOrNull(predicate) ?: return null
// if we already proxied the class matching the predicate,
val proxiedClass = classProxy.singleOrNull{classProxy -> predicate(classProxy.immutableClass)}
// return that proxy
if (proxiedClass != null) return proxiedClass
// else search the original class list
val foundClass = classes.singleOrNull(predicate) ?: return null
// create a class proxy with the index of the class in the classes list
// TODO: There might be a more elegant way to the comment above
val classProxy = ClassProxy(foundClass, classes.indexOf(foundClass))
Expand Down
6 changes: 3 additions & 3 deletions src/main/kotlin/app/revanced/patcher/proxy/ClassProxy.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ class ClassProxy(
val immutableClass: ClassDef,
val originalClassIndex: Int,
) {
internal var proxyused = false
internal var proxyUsed = false
internal lateinit var mutatedClass: MutableClass

fun resolve(): MutableClass {
if (!proxyused) {
proxyused = true
if (!proxyUsed) {
proxyUsed = true
mutatedClass = MutableClass(immutableClass)
}
return mutatedClass
Expand Down

0 comments on commit 4b26305

Please sign in to comment.