feat: Minor refactor and return proxy, if class has been proxied already

This commit is contained in:
oSumAtrIX 2022-03-30 19:15:00 +02:00
parent 6bc4e7eab7
commit 2d3c61113d
No known key found for this signature in database
GPG key ID: A9B3094ACDB604B4
3 changed files with 11 additions and 7 deletions

View file

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

View file

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

View file

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