Patcher: Fix fail when select installed app

This commit is contained in:
WeiguangTWK 2026-04-02 20:43:21 +08:00
parent 98cd89f9d0
commit fced5612fb
1 changed files with 35 additions and 3 deletions

View File

@ -12,6 +12,7 @@ import org.lsposed.patch.NPatch
import org.lsposed.patch.util.Logger
import java.io.File
import java.io.IOException
import java.util.zip.ZipFile
object Patcher {
@ -22,7 +23,37 @@ object Patcher {
private val apkPaths: List<String>,
private val embeddedModules: List<String>?
) {
fun toStringArray(): Array<String> {
fun resolveApkPaths(logger: Logger): List<String> {
val restoredDir = File(lspApp.tmpApkDir, "origin_sources")
if (!restoredDir.exists()) {
restoredDir.mkdirs()
}
return apkPaths.map { sourcePath ->
try {
ZipFile(sourcePath).use { zip ->
val originEntry = zip.getEntry(Constants.ORIGINAL_APK_ASSET_PATH)
?: zip.getEntry("assets/npatch/origin_apk.bin")
if (originEntry == null) {
sourcePath
} else {
val sourceName = File(sourcePath).name
val restoredApk = File(restoredDir, sourceName)
zip.getInputStream(originEntry).use { input ->
restoredApk.outputStream().use { output ->
input.copyTo(output)
}
}
logger.i("Detected patched APK, fallback to embedded origin: $sourcePath -> ${restoredApk.absolutePath}")
restoredApk.absolutePath
}
}
} catch (_: Throwable) {
sourcePath
}
}
}
fun toStringArray(resolvedApkPaths: List<String> = apkPaths): Array<String> {
return buildList {
add("-o"); add(lspApp.tmpApkDir.absolutePath)
add("-p"); add(config.newPackage)
@ -40,14 +71,15 @@ object Patcher {
if (!MyKeyStore.useDefault) {
addAll(arrayOf("-k", MyKeyStore.file.path, Configs.keyStorePassword, Configs.keyStoreAlias, Configs.keyStoreAliasPassword))
}
addAll(apkPaths)
addAll(resolvedApkPaths)
}.toTypedArray()
}
}
suspend fun patch(logger: Logger, options: Options) {
withContext(Dispatchers.IO) {
NPatch(logger, *options.toStringArray()).doCommandLine()
val resolvedApkPaths = options.resolveApkPaths(logger)
NPatch(logger, *options.toStringArray(resolvedApkPaths)).doCommandLine()
val uri = Configs.storageDirectory?.toUri()
?: throw IOException("Uri is null")