fix: Merge all extensions before initializing lookup maps

This commit is contained in:
oSumAtrIX
2024-07-26 04:45:31 +02:00
parent a8e8fa4093
commit 328aa876d8
3 changed files with 102 additions and 54 deletions

View File

@@ -31,7 +31,9 @@ import java.util.logging.Logger
* @param config The [PatcherConfig] used to create this context.
*/
@Suppress("MemberVisibilityCanBePrivate")
class BytecodePatchContext internal constructor(private val config: PatcherConfig) : PatchContext<Set<PatcherResult.PatchedDexFile>> {
class BytecodePatchContext internal constructor(private val config: PatcherConfig) :
PatchContext<Set<PatcherResult.PatchedDexFile>>,
Closeable {
private val logger = Logger.getLogger(BytecodePatchContext::class.java.name)
/**
@@ -57,6 +59,13 @@ class BytecodePatchContext internal constructor(private val config: PatcherConfi
*/
internal val lookupMaps by lazy { LookupMaps(classes) }
/**
* A map for lookup by [merge].
*/
internal val classesByType = mutableMapOf<String, ClassDef>().apply {
classes.forEach { classDef -> put(classDef.type, classDef) }
}
/**
* Merge an extension to [classes].
*
@@ -66,11 +75,11 @@ class BytecodePatchContext internal constructor(private val config: PatcherConfi
val extension = extensionInputStream.readAllBytes()
RawDexIO.readRawDexFile(extension, 0, null).classes.forEach { classDef ->
val existingClass = lookupMaps.classesByType[classDef.type] ?: run {
val existingClass = classesByType[classDef.type] ?: run {
logger.fine("Adding class \"$classDef\"")
lookupMaps.classesByType[classDef.type] = classDef
classes += classDef
classesByType[classDef.type] = classDef
return@forEach
}
@@ -254,6 +263,12 @@ class BytecodePatchContext internal constructor(private val config: PatcherConfi
methodsByStrings.clear()
}
}
override fun close() {
lookupMaps.close()
classesByType.clear()
classes.clear()
}
}
/**