1
mirror of https://github.com/revanced/revanced-patcher synced 2025-09-06 16:38:50 +02:00

Compare commits

...

8 Commits

Author SHA1 Message Date
semantic-release-bot
3d61dacbda chore(release): 1.10.0 [skip ci]
# [1.10.0](https://github.com/revanced/revanced-patcher/compare/v1.9.0...v1.10.0) (2022-06-23)

### Features

* improve logging ([c20dfe1](c20dfe12d5))
2022-06-23 00:12:04 +00:00
oSumAtrIX
c20dfe12d5 feat: improve logging 2022-06-23 02:10:43 +02:00
semantic-release-bot
78663cde88 chore(release): 1.9.0 [skip ci]
# [1.9.0](https://github.com/revanced/revanced-patcher/compare/v1.8.0...v1.9.0) (2022-06-22)

### Bug Fixes

* callback for each file instead of class ([930768d](930768dfb3))

### Features

* yield the patch result ([dde5385](dde5385232))
2022-06-22 23:43:17 +00:00
oSumAtrIX
dde5385232 feat: yield the patch result 2022-06-23 01:41:52 +02:00
oSumAtrIX
930768dfb3 fix: callback for each file instead of class 2022-06-23 01:41:52 +02:00
semantic-release-bot
1f4bc5079f chore(release): 1.8.0 [skip ci]
# [1.8.0](https://github.com/revanced/revanced-patcher/compare/v1.7.2...v1.8.0) (2022-06-22)

### Features

* logging class ([caf2745](caf2745805))
2022-06-22 23:25:29 +00:00
oSumAtrIX
caf2745805 feat: logging class 2022-06-23 01:23:35 +02:00
oSumAtrIX
a4529c3fee refactor: logging and exception strings 2022-06-22 16:47:58 +02:00
7 changed files with 85 additions and 67 deletions

View File

@@ -1,3 +1,29 @@
# [1.10.0](https://github.com/revanced/revanced-patcher/compare/v1.9.0...v1.10.0) (2022-06-23)
### Features
* improve logging ([c20dfe1](https://github.com/revanced/revanced-patcher/commit/c20dfe12d5c737264b844e6634de11bf1e1629f0))
# [1.9.0](https://github.com/revanced/revanced-patcher/compare/v1.8.0...v1.9.0) (2022-06-22)
### Bug Fixes
* callback for each file instead of class ([930768d](https://github.com/revanced/revanced-patcher/commit/930768dfb31dc5fa6c248050b08ac117c40ee0a3))
### Features
* yield the patch result ([dde5385](https://github.com/revanced/revanced-patcher/commit/dde5385232abddc8a85d6e9a939549b71dd9130e))
# [1.8.0](https://github.com/revanced/revanced-patcher/compare/v1.7.2...v1.8.0) (2022-06-22)
### Features
* logging class ([caf2745](https://github.com/revanced/revanced-patcher/commit/caf2745805ffd4b59fa81e79cc489b1a1a5c5d89))
## [1.7.2](https://github.com/revanced/revanced-patcher/compare/v1.7.1...v1.7.2) (2022-06-22)

View File

@@ -1,2 +1,2 @@
kotlin.code.style = official
version = 1.7.2
version = 1.10.0

View File

@@ -1,15 +0,0 @@
package app.revanced.patcher
interface PatchLogger {
fun error(msg: String)
fun warn(msg: String)
fun info(msg: String)
fun trace(msg: String)
}
internal object NoopPatchLogger : PatchLogger {
override fun error(msg: String) {}
override fun warn(msg: String) {}
override fun info(msg: String) {}
override fun trace(msg: String) {}
}

View File

@@ -62,7 +62,7 @@ class Patcher(private val options: PatcherOptions) {
val packageMetadata = PackageMetadata()
if (options.patchResources) {
logger.info("Decoding resources using Androlib, this may take a long time...")
logger.info("Decoding resources")
// decode resources to cache directory
androlib.decodeManifestWithResources(extInputFile, outDir, resourceTable)
@@ -78,7 +78,7 @@ class Patcher(private val options: PatcherOptions) {
}
} else {
logger.info("Decoding resources manually because resource decoding is disabled!")
logger.info("Only decoding AndroidManifest.xml because resource patching is disabled")
// create decoder for the resource table
val decoder = ResAttrDecoder()
@@ -102,8 +102,9 @@ class Patcher(private val options: PatcherOptions) {
packageMetadata.metaInfo.versionInfo = resourceTable.versionInfo
packageMetadata.metaInfo.sdkInfo = resourceTable.sdkInfo
logger.info("Reading dex files")
// read dex files
logger.info("Reading input as dex file")
val dexFile = MultiDexIO.readDexFile(true, options.inputFile, NAMER, null, null)
// get the opcodes
opcodes = dexFile.opcodes
@@ -127,26 +128,29 @@ class Patcher(private val options: PatcherOptions) {
callback: (File) -> Unit
) {
for (file in files) {
var modified = false
callback(file)
for (classDef in MultiDexIO.readDexFile(true, file, NAMER, null, null).classes) {
val e = data.bytecodeData.classes.internalClasses.findIndexed { it.type == classDef.type }
if (e != null) {
if (throwOnDuplicates) {
throw Exception("Class ${classDef.type} has already been added to the patcher.")
}
val (_, idx) = e
if (allowedOverwrites.contains(classDef.type)) {
data.bytecodeData.classes.internalClasses[idx] = classDef
modified = true
}
val type = classDef.type
val existingClass = data.bytecodeData.classes.internalClasses.findIndexed { it.type == type }
if (existingClass == null) {
if (throwOnDuplicates) throw Exception("Class $type has already been added to the patcher")
logger.trace("Merging $type")
data.bytecodeData.classes.internalClasses.add(classDef)
continue
}
data.bytecodeData.classes.internalClasses.add(classDef)
modified = true
}
if (modified) {
logger.trace("Added file ${file.name}")
callback(file)
if (!allowedOverwrites.contains(type)) continue
logger.trace("Overwriting $type")
val index = existingClass.second
data.bytecodeData.classes.internalClasses[index] = classDef
}
}
}
@@ -160,7 +164,6 @@ class Patcher(private val options: PatcherOptions) {
var resourceFile: File? = null
if (options.patchResources) {
logger.info("Patching resources")
val cacheDirectory = ExtFile(options.resourceCacheDirectory)
val androlibResources = AndrolibResources().also { resources ->
@@ -189,15 +192,13 @@ class Patcher(private val options: PatcherOptions) {
val resDirectory = cacheDirectory.resolve("res")
val includedFiles = metaInfo.usesFramework.ids.map { id ->
androlibResources.getFrameworkApk(
id,
metaInfo.usesFramework.tag
id, metaInfo.usesFramework.tag
)
}.toTypedArray()
logger.trace("Packaging using aapt")
logger.info("Compiling resources")
androlibResources.aaptPackage(
aaptFile, manifestFile, resDirectory, null,
null, includedFiles
aaptFile, manifestFile, resDirectory, null, null, includedFiles
)
resourceFile = aaptFile
@@ -226,9 +227,7 @@ class Patcher(private val options: PatcherOptions) {
return PatcherResult(
dexFiles.map {
app.revanced.patcher.util.dex.DexFile(it.key, it.value.readAt(0))
},
metaInfo.doNotCompress.toList(),
resourceFile
}, metaInfo.doNotCompress.toList(), resourceFile
)
}
@@ -247,15 +246,14 @@ class Patcher(private val options: PatcherOptions) {
* @return The result of executing the [patch].
*/
private fun applyPatch(
patch: Class<out Patch<Data>>,
appliedPatches: MutableList<String>
patch: Class<out Patch<Data>>, appliedPatches: MutableList<String>
): PatchResult {
val patchName = patch.patchName
logger.trace("Applying patch $patchName")
// if the patch has already applied silently skip it
if (appliedPatches.contains(patchName)) {
logger.trace("Skipping patch $patchName because it has already been applied")
return PatchResultSuccess()
}
appliedPatches.add(patchName)
@@ -290,6 +288,7 @@ class Patcher(private val options: PatcherOptions) {
}
logger.trace("Executing patch $patchName of type: ${if (isResourcePatch) "resource" else "bytecode"}")
return try {
patchInstance.execute(data)
} catch (e: Exception) {
@@ -300,30 +299,23 @@ class Patcher(private val options: PatcherOptions) {
/**
* Apply patches loaded into the patcher.
* @param stopOnError If true, the patches will stop on the first error.
* @return A map of [PatchResultSuccess]. If the [Patch] was successfully applied,
* [PatchResultSuccess] 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.
* @return A pair of the name of the [Patch] and its [PatchResult].
*/
fun applyPatches(
stopOnError: Boolean = false,
callback: (Class<out Patch<Data>>, Boolean) -> Unit = { _, _ -> }
): Map<String, Result<PatchResultSuccess>> {
fun applyPatches(stopOnError: Boolean = false) = sequence {
logger.trace("Applying all patches")
val appliedPatches = mutableListOf<String>()
return buildMap {
for (patch in data.patches) {
val result = applyPatch(patch, appliedPatches)
for (patch in data.patches) {
val patchResult = applyPatch(patch, appliedPatches)
this[patch.patchName] = if (result.isSuccess()) {
Result.success(result.success()!!)
} else {
Result.failure(result.error()!!)
}
callback(patch, result.isSuccess())
if (stopOnError && result.isError()) break
val result = if (patchResult.isSuccess()) {
Result.success(patchResult.success()!!)
} else {
Result.failure(patchResult.error()!!)
}
yield(patch.patchName to result)
if (stopOnError && patchResult.isError()) break
}
}
}

View File

@@ -1,5 +1,7 @@
package app.revanced.patcher
import app.revanced.patcher.logging.impl.NopLogger
import app.revanced.patcher.logging.Logger
import java.io.File
/**
@@ -17,5 +19,5 @@ data class PatcherOptions(
internal val patchResources: Boolean = false,
internal val aaptPath: String = "",
internal val frameworkFolderLocation: String? = null,
internal val logger: PatchLogger = NoopPatchLogger
internal val logger: Logger = NopLogger
)

View File

@@ -0,0 +1,8 @@
package app.revanced.patcher.logging
interface Logger {
fun error(msg: String) {}
fun warn(msg: String) {}
fun info(msg: String) {}
fun trace(msg: String) {}
}

View File

@@ -0,0 +1,5 @@
package app.revanced.patcher.logging.impl
import app.revanced.patcher.logging.Logger
object NopLogger : Logger