From eeae46a415609716e1bc3dc5448b68387c36db9c Mon Sep 17 00:00:00 2001 From: Ax333l Date: Fri, 1 Sep 2023 10:50:13 +0200 Subject: [PATCH] chore: bump kotlinx.serialization plugin and patcher --- app/build.gradle.kts | 2 +- .../app/revanced/manager/patcher/Session.kt | 50 +++++++++++-------- .../manager/patcher/patch/PatchBundle.kt | 14 ++---- .../manager/patcher/patch/PatchInfo.kt | 2 +- .../manager/patcher/worker/PatcherWorker.kt | 3 +- .../ui/viewmodel/InstallerViewModel.kt | 29 ++++++++--- gradle/libs.versions.toml | 4 +- 7 files changed, 62 insertions(+), 42 deletions(-) diff --git a/app/build.gradle.kts b/app/build.gradle.kts index 981023fb..3e18d3e4 100644 --- a/app/build.gradle.kts +++ b/app/build.gradle.kts @@ -4,7 +4,7 @@ plugins { alias(libs.plugins.devtools) alias(libs.plugins.about.libraries) id("kotlin-parcelize") - kotlin("plugin.serialization") version "1.8.22" + kotlin("plugin.serialization") version "1.9.0" } android { diff --git a/app/src/main/java/app/revanced/manager/patcher/Session.kt b/app/src/main/java/app/revanced/manager/patcher/Session.kt index 14e26ade..54e028fa 100644 --- a/app/src/main/java/app/revanced/manager/patcher/Session.kt +++ b/app/src/main/java/app/revanced/manager/patcher/Session.kt @@ -1,25 +1,25 @@ package app.revanced.manager.patcher +import app.revanced.manager.ui.viewmodel.ManagerLogger import app.revanced.patcher.Patcher import app.revanced.patcher.PatcherOptions -import app.revanced.patcher.data.Context -import app.revanced.patcher.logging.Logger -import app.revanced.patcher.patch.Patch +import app.revanced.patcher.patch.PatchClass +import app.revanced.patcher.patch.PatchResult import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.withContext import java.io.Closeable import java.io.File import java.nio.file.Files import java.nio.file.StandardCopyOption +import java.util.logging.Logger -internal typealias PatchClass = Class> internal typealias PatchList = List class Session( cacheDir: String, frameworkDir: String, aaptPath: String, - private val logger: Logger, + private val logger: ManagerLogger, private val input: File, private val onStepSucceeded: suspend () -> Unit ) : Closeable { @@ -27,35 +27,39 @@ class Session( private val patcher = Patcher( PatcherOptions( inputFile = input, - resourceCacheDirectory = temporary.resolve("aapt-resources").path, - frameworkDirectory = frameworkDir, - aaptPath = aaptPath, - logger = logger, + resourceCachePath = temporary.resolve("aapt-resources"), + frameworkFileDirectory = frameworkDir, + aaptBinaryPath = aaptPath ) ) private suspend fun Patcher.applyPatchesVerbose() { - this.executePatches(true).forEach { (patch, result) -> - if (result.isSuccess) { + this.apply(true).collect { (patch, exception) -> + if (exception == null) { logger.info("$patch succeeded") onStepSucceeded() - return@forEach + return@collect } logger.error("$patch failed:") - result.exceptionOrNull()!!.let { - logger.error(result.exceptionOrNull()!!.stackTraceToString()) - - throw it - } + logger.error(exception.stackTraceToString()) + throw exception } } suspend fun run(output: File, selectedPatches: PatchList, integrations: List) { onStepSucceeded() // Unpacking + Logger.getLogger("").apply { + handlers.forEach { + it.close() + removeHandler(it) + } + + addHandler(logger) + } with(patcher) { logger.info("Merging integrations") - addIntegrations(integrations) {} - addPatches(selectedPatches) + acceptIntegrations(integrations) + acceptPatches(selectedPatches) onStepSucceeded() // Merging logger.info("Applying patches...") @@ -63,7 +67,7 @@ class Session( } logger.info("Writing patched files...") - val result = patcher.save() + val result = patcher.get() val aligned = temporary.resolve("aligned.apk").also { Aligning.align(result, input, it) } @@ -77,5 +81,11 @@ class Session( override fun close() { temporary.delete() + patcher.close() + } + + companion object { + operator fun PatchResult.component1() = patchName + operator fun PatchResult.component2() = exception } } \ No newline at end of file diff --git a/app/src/main/java/app/revanced/manager/patcher/patch/PatchBundle.kt b/app/src/main/java/app/revanced/manager/patcher/patch/PatchBundle.kt index a8e248d2..08b3c33d 100644 --- a/app/src/main/java/app/revanced/manager/patcher/patch/PatchBundle.kt +++ b/app/src/main/java/app/revanced/manager/patcher/patch/PatchBundle.kt @@ -1,24 +1,16 @@ package app.revanced.manager.patcher.patch import android.util.Log -import app.revanced.manager.patcher.PatchClass import app.revanced.manager.util.tag -import app.revanced.patcher.Patcher +import app.revanced.patcher.PatchBundleLoader import app.revanced.patcher.extensions.PatchExtensions.compatiblePackages -import app.revanced.patcher.util.patch.PatchBundle -import dalvik.system.PathClassLoader +import app.revanced.patcher.patch.PatchClass import java.io.File class PatchBundle(private val loader: Iterable, val integrations: File?) { constructor(bundleJar: File, integrations: File?) : this( object : Iterable { - private fun load(): List { - val path = bundleJar.absolutePath - return PatchBundle.Dex( - path, - PathClassLoader(path, Patcher::class.java.classLoader) - ).loadPatches() - } + private fun load(): List = PatchBundleLoader.Dex(bundleJar) override fun iterator() = load().iterator() }, diff --git a/app/src/main/java/app/revanced/manager/patcher/patch/PatchInfo.kt b/app/src/main/java/app/revanced/manager/patcher/patch/PatchInfo.kt index 13e37fea..20d1c886 100644 --- a/app/src/main/java/app/revanced/manager/patcher/patch/PatchInfo.kt +++ b/app/src/main/java/app/revanced/manager/patcher/patch/PatchInfo.kt @@ -1,7 +1,6 @@ package app.revanced.manager.patcher.patch import androidx.compose.runtime.Immutable -import app.revanced.manager.patcher.PatchClass import app.revanced.patcher.annotation.Package import app.revanced.patcher.extensions.PatchExtensions.compatiblePackages import app.revanced.patcher.extensions.PatchExtensions.dependencies @@ -9,6 +8,7 @@ import app.revanced.patcher.extensions.PatchExtensions.description import app.revanced.patcher.extensions.PatchExtensions.include import app.revanced.patcher.extensions.PatchExtensions.options import app.revanced.patcher.extensions.PatchExtensions.patchName +import app.revanced.patcher.patch.PatchClass import app.revanced.patcher.patch.PatchOption import kotlinx.collections.immutable.ImmutableList import kotlinx.collections.immutable.toImmutableList diff --git a/app/src/main/java/app/revanced/manager/patcher/worker/PatcherWorker.kt b/app/src/main/java/app/revanced/manager/patcher/worker/PatcherWorker.kt index 16b99e96..f2133a90 100644 --- a/app/src/main/java/app/revanced/manager/patcher/worker/PatcherWorker.kt +++ b/app/src/main/java/app/revanced/manager/patcher/worker/PatcherWorker.kt @@ -22,6 +22,7 @@ import app.revanced.manager.domain.worker.WorkerRepository import app.revanced.manager.patcher.Session import app.revanced.manager.patcher.aapt.Aapt import app.revanced.manager.ui.model.SelectedApp +import app.revanced.manager.ui.viewmodel.ManagerLogger import app.revanced.manager.util.Options import app.revanced.manager.util.PM import app.revanced.manager.util.PatchesSelection @@ -57,7 +58,7 @@ class PatcherWorker( val packageName: String, val packageVersion: String, val progress: MutableStateFlow>, - val logger: Logger + val logger: ManagerLogger ) companion object { diff --git a/app/src/main/java/app/revanced/manager/ui/viewmodel/InstallerViewModel.kt b/app/src/main/java/app/revanced/manager/ui/viewmodel/InstallerViewModel.kt index 82174d9a..315dd7e4 100644 --- a/app/src/main/java/app/revanced/manager/ui/viewmodel/InstallerViewModel.kt +++ b/app/src/main/java/app/revanced/manager/ui/viewmodel/InstallerViewModel.kt @@ -32,7 +32,6 @@ import app.revanced.manager.ui.destination.Destination import app.revanced.manager.util.PM import app.revanced.manager.util.tag import app.revanced.manager.util.toast -import app.revanced.patcher.logging.Logger import kotlinx.collections.immutable.ImmutableList import kotlinx.collections.immutable.toImmutableList import kotlinx.coroutines.Dispatchers @@ -45,6 +44,8 @@ import org.koin.core.component.inject import java.io.File import java.nio.file.Files import java.util.UUID +import java.util.logging.Level +import java.util.logging.LogRecord @Stable class InstallerViewModel(input: Destination.Installer) : ViewModel(), KoinComponent { @@ -207,7 +208,8 @@ class InstallerViewModel(input: Destination.Installer) : ViewModel(), KoinCompon } } -private class ManagerLogger : Logger { +// TODO: move this to a better place +class ManagerLogger : java.util.logging.Handler() { private val logs = mutableListOf>() private fun log(level: LogLevel, msg: String) { level.androidLog(msg) @@ -218,10 +220,25 @@ private class ManagerLogger : Logger { fun export() = logs.asSequence().map { (level, msg) -> "[${level.name}]: $msg" }.joinToString("\n") - override fun trace(msg: String) = log(LogLevel.TRACE, msg) - override fun info(msg: String) = log(LogLevel.INFO, msg) - override fun warn(msg: String) = log(LogLevel.WARN, msg) - override fun error(msg: String) = log(LogLevel.ERROR, msg) + fun trace(msg: String) = log(LogLevel.TRACE, msg) + fun info(msg: String) = log(LogLevel.INFO, msg) + fun warn(msg: String) = log(LogLevel.WARN, msg) + fun error(msg: String) = log(LogLevel.ERROR, msg) + override fun publish(record: LogRecord) { + val msg = record.message + val fn = when (record.level) { + Level.INFO -> ::info + Level.SEVERE -> ::error + Level.WARNING -> ::warn + else -> ::trace + } + + fn(msg) + } + + override fun flush() = Unit + + override fun close() = Unit } enum class LogLevel { diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index b5763f95..d02fedad 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -11,7 +11,7 @@ accompanist = "0.30.1" serialization = "1.6.0" collection = "0.3.5" room-version = "2.5.2" -patcher = "12.1.1" +patcher = "14.2.1" apksign = "8.1.1" bcpkix-jdk18on = "1.76" koin-version = "3.4.2" @@ -19,7 +19,7 @@ koin-version-compose = "3.4.5" reimagined-navigation = "1.4.0" ktor = "2.3.2" markdown = "0.4.1" -androidGradlePlugin = "8.1.1" +androidGradlePlugin = "8.0.2" kotlinGradlePlugin = "1.9.0" devToolsGradlePlugin = "1.9.0-1.0.12" aboutLibrariesGradlePlugin = "10.8.2"