refactor: logging and exception strings

This commit is contained in:
oSumAtrIX 2022-06-22 16:47:58 +02:00
parent 835c0f9f7a
commit a4529c3fee
No known key found for this signature in database
GPG Key ID: A9B3094ACDB604B4

View File

@ -78,7 +78,7 @@ class Patcher(private val options: PatcherOptions) {
} }
} else { } else {
logger.info("Decoding resources manually because resource decoding is disabled!") logger.info("Decoding AndroidManifest.xml manually because resource patching is disabled")
// create decoder for the resource table // create decoder for the resource table
val decoder = ResAttrDecoder() val decoder = ResAttrDecoder()
@ -102,8 +102,9 @@ class Patcher(private val options: PatcherOptions) {
packageMetadata.metaInfo.versionInfo = resourceTable.versionInfo packageMetadata.metaInfo.versionInfo = resourceTable.versionInfo
packageMetadata.metaInfo.sdkInfo = resourceTable.sdkInfo packageMetadata.metaInfo.sdkInfo = resourceTable.sdkInfo
// read dex files
logger.info("Reading input as dex file") logger.info("Reading input as dex file")
// read dex files
val dexFile = MultiDexIO.readDexFile(true, options.inputFile, NAMER, null, null) val dexFile = MultiDexIO.readDexFile(true, options.inputFile, NAMER, null, null)
// get the opcodes // get the opcodes
opcodes = dexFile.opcodes opcodes = dexFile.opcodes
@ -127,25 +128,28 @@ class Patcher(private val options: PatcherOptions) {
callback: (File) -> Unit callback: (File) -> Unit
) { ) {
for (file in files) { for (file in files) {
var modified = false
for (classDef in MultiDexIO.readDexFile(true, file, NAMER, null, null).classes) { for (classDef in MultiDexIO.readDexFile(true, file, NAMER, null, null).classes) {
val e = data.bytecodeData.classes.internalClasses.findIndexed { it.type == classDef.type } val type = classDef.type
if (e != null) {
if (throwOnDuplicates) { val existingClass = data.bytecodeData.classes.internalClasses.findIndexed { it.type == type }
throw Exception("Class ${classDef.type} has already been added to the patcher.") if (existingClass == null) {
} if (throwOnDuplicates) throw Exception("Class $type has already been added to the patcher")
val (_, idx) = e
if (allowedOverwrites.contains(classDef.type)) { logger.trace("Merging $type")
data.bytecodeData.classes.internalClasses[idx] = classDef data.bytecodeData.classes.internalClasses.add(classDef)
modified = true
} callback(file)
continue continue
} }
data.bytecodeData.classes.internalClasses.add(classDef)
modified = true if (!allowedOverwrites.contains(type)) continue
}
if (modified) { logger.trace("Overwriting $type")
logger.trace("Added file ${file.name}")
val index = existingClass.second
data.bytecodeData.classes.internalClasses[index] = classDef
callback(file) callback(file)
} }
} }
@ -189,15 +193,13 @@ class Patcher(private val options: PatcherOptions) {
val resDirectory = cacheDirectory.resolve("res") val resDirectory = cacheDirectory.resolve("res")
val includedFiles = metaInfo.usesFramework.ids.map { id -> val includedFiles = metaInfo.usesFramework.ids.map { id ->
androlibResources.getFrameworkApk( androlibResources.getFrameworkApk(
id, id, metaInfo.usesFramework.tag
metaInfo.usesFramework.tag
) )
}.toTypedArray() }.toTypedArray()
logger.trace("Packaging using aapt") logger.trace("Packaging using aapt")
androlibResources.aaptPackage( androlibResources.aaptPackage(
aaptFile, manifestFile, resDirectory, null, aaptFile, manifestFile, resDirectory, null, null, includedFiles
null, includedFiles
) )
resourceFile = aaptFile resourceFile = aaptFile
@ -226,9 +228,7 @@ class Patcher(private val options: PatcherOptions) {
return PatcherResult( return PatcherResult(
dexFiles.map { dexFiles.map {
app.revanced.patcher.util.dex.DexFile(it.key, it.value.readAt(0)) app.revanced.patcher.util.dex.DexFile(it.key, it.value.readAt(0))
}, }, metaInfo.doNotCompress.toList(), resourceFile
metaInfo.doNotCompress.toList(),
resourceFile
) )
} }
@ -247,15 +247,16 @@ class Patcher(private val options: PatcherOptions) {
* @return The result of executing the [patch]. * @return The result of executing the [patch].
*/ */
private fun applyPatch( private fun applyPatch(
patch: Class<out Patch<Data>>, patch: Class<out Patch<Data>>, appliedPatches: MutableList<String>
appliedPatches: MutableList<String>
): PatchResult { ): PatchResult {
val patchName = patch.patchName val patchName = patch.patchName
logger.trace("Applying patch $patchName") logger.trace("Applying patch $patchName")
// if the patch has already applied silently skip it // if the patch has already applied silently skip it
if (appliedPatches.contains(patchName)) { if (appliedPatches.contains(patchName)) {
logger.trace("Skipping patch $patchName because it has already been applied") logger.trace("Skipping patch $patchName because it has already been applied")
return PatchResultSuccess() return PatchResultSuccess()
} }
appliedPatches.add(patchName) appliedPatches.add(patchName)
@ -290,6 +291,7 @@ class Patcher(private val options: PatcherOptions) {
} }
logger.trace("Executing patch $patchName of type: ${if (isResourcePatch) "resource" else "bytecode"}") logger.trace("Executing patch $patchName of type: ${if (isResourcePatch) "resource" else "bytecode"}")
return try { return try {
patchInstance.execute(data) patchInstance.execute(data)
} catch (e: Exception) { } catch (e: Exception) {
@ -305,8 +307,7 @@ class Patcher(private val options: PatcherOptions) {
* If the [Patch] failed to apply, an Exception will always be returned to the wrapping Result object. * If the [Patch] failed to apply, an Exception will always be returned to the wrapping Result object.
*/ */
fun applyPatches( fun applyPatches(
stopOnError: Boolean = false, stopOnError: Boolean = false, callback: (Class<out Patch<Data>>, Boolean) -> Unit = { _, _ -> }
callback: (Class<out Patch<Data>>, Boolean) -> Unit = { _, _ -> }
): Map<String, Result<PatchResultSuccess>> { ): Map<String, Result<PatchResultSuccess>> {
logger.trace("Applying all patches") logger.trace("Applying all patches")
val appliedPatches = mutableListOf<String>() val appliedPatches = mutableListOf<String>()