Skip to content

Commit 8f7911e

Browse files
Refactor: Simplify
1 parent 772c0e6 commit 8f7911e

File tree

2 files changed

+19
-24
lines changed

2 files changed

+19
-24
lines changed

src/main/kotlin/app/revanced/patcher/patch/BytecodePatchContext.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ class BytecodePatchContext internal constructor(private val config: PatcherConfi
4141
internal val opcodes: Opcodes
4242

4343
/**
44-
* The list of classes.
44+
* All classes for the target app and any extension classes.
4545
*/
4646
val classes = PatchClasses(
4747
MultiDexIO.readDexFile(
@@ -50,7 +50,7 @@ class BytecodePatchContext internal constructor(private val config: PatcherConfi
5050
BasicDexFileNamer(),
5151
null,
5252
null,
53-
).also { opcodes = it.opcodes }.classes.associateBy { it.type }.toMutableMap(),
53+
).also { opcodes = it.opcodes }.classes
5454
)
5555

5656
/**

src/main/kotlin/app/revanced/patcher/util/PatchClasses.kt

Lines changed: 17 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3,32 +3,29 @@ package app.revanced.patcher.util
33
import app.revanced.patcher.patch.PatchException
44
import app.revanced.patcher.util.proxy.mutableTypes.MutableClass
55
import com.android.tools.smali.dexlib2.iface.ClassDef
6-
import kotlin.collections.mutableMapOf
76

87
@Deprecated("Instead use PatchClasses")
98
typealias ProxyClassList = PatchClasses
109

1110
/**
12-
* A set of all classes for the target app and any extension classes.
11+
* All classes for the target app and any extension classes.
1312
*/
1413
class PatchClasses internal constructor(
1514
/**
1615
* Pool of both immutable and mutable classes.
1716
*/
1817
internal val pool: MutableMap<String, ClassDef>
1918
) {
20-
/**
21-
* Mutable classes. All instances are also found in [pool].
22-
*/
23-
private val mutablePool = mutableMapOf<String, MutableClass>()
19+
20+
internal constructor(set: Set<ClassDef>) :
21+
this(set.associateByTo(mutableMapOf()) { it.type })
2422

2523
internal fun addClass(classDef: ClassDef) {
2624
pool[classDef.type] = classDef
2725
}
2826

2927
internal fun close() {
3028
pool.clear()
31-
mutablePool.clear()
3229
}
3330

3431
/**
@@ -61,33 +58,31 @@ class PatchClasses internal constructor(
6158
* @param classDefType The full classname.
6259
* @return A mutable version of the class type.
6360
*/
64-
fun mutableClassBy(classDefType: String) =
65-
mutablePool[classDefType] ?: MutableClass(
66-
pool.get(classDefType) ?: throw PatchException("Could not find class: $classDefType")
67-
).also {
68-
mutablePool[classDefType] = it
69-
pool[classDefType] = it
61+
fun mutableClassBy(classDefType: String) : MutableClass {
62+
var classDef = pool[classDefType] ?: throw PatchException("Could not find class: $classDefType")
63+
if (classDef is MutableClass) {
64+
return classDef
7065
}
66+
classDef = MutableClass(classDef)
67+
pool[classDefType] = classDef
68+
return classDef
69+
}
7170

7271
/**
73-
* Find a class with a predicate.
74-
*
7572
* @param classDef An immutable class.
7673
* @return A mutable version of the class definition.
7774
*/
7875
fun mutableClassBy(classDef: ClassDef) =
79-
mutablePool[classDef.type] ?: MutableClass(classDef).also {
80-
val classType = classDef.type
81-
mutablePool[classType] = it
82-
pool[classType] = it
83-
}
76+
if (classDef is MutableClass) classDef else mutableClassBy(classDef.type)
8477

8578
/**
86-
* Find a class with a predicate.
79+
* Find a mutable class with a predicate.
8780
*
8881
* @param predicate A predicate to match the class.
8982
* @return A mutable class that matches the predicate.
9083
*/
9184
fun mutableClassBy(predicate: (ClassDef) -> Boolean) =
92-
mutablePool.values.find { predicate(it) } ?: pool.values.find(predicate)?.let { mutableClassBy(it) }
85+
classBy(predicate)?.let {
86+
if (it is MutableClass) it else mutableClassBy(it.type)
87+
}
9388
}

0 commit comments

Comments
 (0)