Skip to content

Commit

Permalink
simplify
Browse files Browse the repository at this point in the history
  • Loading branch information
oSumAtrIX committed Oct 15, 2024
1 parent 7bb1cab commit 2caad08
Showing 1 changed file with 6 additions and 27 deletions.
33 changes: 6 additions & 27 deletions src/main/kotlin/app/revanced/patcher/patch/BytecodePatchContext.kt
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,8 @@ import lanchon.multidexlib2.BasicDexFileNamer
import lanchon.multidexlib2.DexIO
import lanchon.multidexlib2.MultiDexIO
import lanchon.multidexlib2.RawDexIO
import java.io.ByteArrayOutputStream
import java.io.Closeable
import java.io.FileFilter
import java.io.InputStream
import java.util.*
import java.util.logging.Logger

Expand Down Expand Up @@ -60,41 +58,20 @@ class BytecodePatchContext internal constructor(private val config: PatcherConfi
*/
internal val lookupMaps by lazy { LookupMaps(classes) }

/**
* Because InputStream.readAllBytes() is not available with Android until 13.0,
* roll our own implementation until this project uses Kotlin multiplatform.
*/
private fun InputStream.readAllBytesBackwardsCompatible(): ByteArray {
val buffer = ByteArrayOutputStream()
val data = ByteArray(1024)

while (true) {
var length = this.read(data)
if (length >= 0) {
buffer.write(data, 0, length)
} else {
break
}
}

return buffer.toByteArray()
}

/**
* Merge the extensions for this set of patches.
*/
internal fun Set<Patch<*>>.mergeExtensions() {
// Lookup map for fast checking if a class exists by its type.
// Lookup map to check if a class exists by its type quickly.
val classesByType = mutableMapOf<String, ClassDef>().apply {
classes.forEach { classDef -> put(classDef.type, classDef) }
}

forEachRecursively { patch ->
if (patch is BytecodePatch && patch.extension != null) {
if (patch !is BytecodePatch) return@forEachRecursively

val extension = patch.extension.readAllBytesBackwardsCompatible()

RawDexIO.readRawDexFile(extension, 0, null).classes.forEach { classDef ->
patch.extension?.use { extensionStream ->
RawDexIO.readRawDexFile(extensionStream, 1024, null).classes.forEach { classDef ->
val existingClass = classesByType[classDef.type] ?: run {
logger.fine("Adding class \"$classDef\"")

Expand All @@ -116,6 +93,8 @@ class BytecodePatchContext internal constructor(private val config: PatcherConfi
classes += mergedClass
}
}

patch.extension.close()
}
}
}
Expand Down

0 comments on commit 2caad08

Please sign in to comment.