1
mirror of https://github.com/revanced/revanced-patcher synced 2025-09-10 05:30:49 +02:00

Compare commits

..

11 Commits

Author SHA1 Message Date
semantic-release-bot
8efcf329bb chore(release): 1.10.1 [skip ci]
## [1.10.1](https://github.com/revanced/revanced-patcher/compare/v1.10.0...v1.10.1) (2022-06-23)

### Bug Fixes

* callback only when inteded ([e3bf367](e3bf367ad6))
* mutability of local variable `modified` ([0e87ef5](0e87ef56c4))
2022-06-23 06:59:43 +00:00
oSumAtrIX
0e87ef56c4 fix: mutability of local variable modified 2022-06-23 08:57:41 +02:00
oSumAtrIX
e3bf367ad6 fix: callback only when inteded 2022-06-23 08:54:47 +02:00
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 92 additions and 66 deletions

View File

@@ -1,3 +1,37 @@
## [1.10.1](https://github.com/revanced/revanced-patcher/compare/v1.10.0...v1.10.1) (2022-06-23)
### Bug Fixes
* callback only when inteded ([e3bf367](https://github.com/revanced/revanced-patcher/commit/e3bf367ad6615b30b06027d65f906b2588567a7f))
* mutability of local variable `modified` ([0e87ef5](https://github.com/revanced/revanced-patcher/commit/0e87ef56c418d5c37d58abb9b27f85e25fd44f81))
# [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) ## [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 kotlin.code.style = official
version = 1.7.2 version = 1.10.1

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() val packageMetadata = PackageMetadata()
if (options.patchResources) { if (options.patchResources) {
logger.info("Decoding resources using Androlib, this may take a long time...") logger.info("Decoding resources")
// decode resources to cache directory // decode resources to cache directory
androlib.decodeManifestWithResources(extInputFile, outDir, resourceTable) androlib.decodeManifestWithResources(extInputFile, outDir, resourceTable)
@@ -78,7 +78,7 @@ class Patcher(private val options: PatcherOptions) {
} }
} else { } 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 // 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
logger.info("Reading dex files")
// read dex files // read dex files
logger.info("Reading input as dex file")
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
@@ -129,25 +130,28 @@ class Patcher(private val options: PatcherOptions) {
for (file in files) { for (file in files) {
var modified = false 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 modified = true
}
continue continue
} }
data.bytecodeData.classes.internalClasses.add(classDef)
if (!allowedOverwrites.contains(type)) continue
logger.trace("Overwriting $type")
val index = existingClass.second
data.bytecodeData.classes.internalClasses[index] = classDef
modified = true modified = true
} }
if (modified) { if (modified) callback(file)
logger.trace("Added file ${file.name}")
callback(file)
}
} }
} }
@@ -160,7 +164,6 @@ class Patcher(private val options: PatcherOptions) {
var resourceFile: File? = null var resourceFile: File? = null
if (options.patchResources) { if (options.patchResources) {
logger.info("Patching resources")
val cacheDirectory = ExtFile(options.resourceCacheDirectory) val cacheDirectory = ExtFile(options.resourceCacheDirectory)
val androlibResources = AndrolibResources().also { resources -> val androlibResources = AndrolibResources().also { resources ->
@@ -189,15 +192,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.info("Compiling resources")
androlibResources.aaptPackage( androlibResources.aaptPackage(
aaptFile, manifestFile, resDirectory, null, aaptFile, manifestFile, resDirectory, null, null, includedFiles
null, includedFiles
) )
resourceFile = aaptFile resourceFile = aaptFile
@@ -226,9 +227,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 +246,14 @@ 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")
// 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 +288,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) {
@@ -300,30 +299,23 @@ class Patcher(private val options: PatcherOptions) {
/** /**
* Apply patches loaded into the patcher. * Apply patches loaded into the patcher.
* @param stopOnError If true, the patches will stop on the first error. * @param stopOnError If true, the patches will stop on the first error.
* @return A map of [PatchResultSuccess]. If the [Patch] was successfully applied, * @return A pair of the name of the [Patch] and its [PatchResult].
* [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.
*/ */
fun applyPatches( fun applyPatches(stopOnError: Boolean = false) = sequence {
stopOnError: Boolean = false,
callback: (Class<out Patch<Data>>, Boolean) -> Unit = { _, _ -> }
): Map<String, Result<PatchResultSuccess>> {
logger.trace("Applying all patches") logger.trace("Applying all patches")
val appliedPatches = mutableListOf<String>() val appliedPatches = mutableListOf<String>()
return buildMap { for (patch in data.patches) {
for (patch in data.patches) { val patchResult = applyPatch(patch, appliedPatches)
val result = applyPatch(patch, appliedPatches)
this[patch.patchName] = if (result.isSuccess()) { val result = if (patchResult.isSuccess()) {
Result.success(result.success()!!) Result.success(patchResult.success()!!)
} else { } else {
Result.failure(result.error()!!) Result.failure(patchResult.error()!!)
}
callback(patch, result.isSuccess())
if (stopOnError && result.isError()) break
} }
yield(patch.patchName to result)
if (stopOnError && patchResult.isError()) break
} }
} }
} }
@@ -332,4 +324,4 @@ private fun BuildOptions.setBuildOptions(options: PatcherOptions) {
this.aaptPath = options.aaptPath this.aaptPath = options.aaptPath
this.useAapt2 = true this.useAapt2 = true
this.frameworkFolderLocation = options.frameworkFolderLocation this.frameworkFolderLocation = options.frameworkFolderLocation
} }

View File

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