2022-08-06 23:35:35 +02:00
|
|
|
package app.revanced.manager
|
2022-07-30 21:09:59 +02:00
|
|
|
|
2022-08-06 15:04:18 +02:00
|
|
|
import androidx.annotation.NonNull
|
2022-08-13 11:56:30 +02:00
|
|
|
import app.revanced.manager.utils.Aapt
|
|
|
|
import app.revanced.manager.utils.aligning.ZipAligner
|
|
|
|
import app.revanced.manager.utils.signing.Signer
|
|
|
|
import app.revanced.manager.utils.zip.ZipFile
|
|
|
|
import app.revanced.manager.utils.zip.structures.ZipEntry
|
|
|
|
import app.revanced.patcher.Patcher
|
|
|
|
import app.revanced.patcher.PatcherOptions
|
2022-08-06 15:04:18 +02:00
|
|
|
import app.revanced.patcher.data.Data
|
|
|
|
import app.revanced.patcher.extensions.PatchExtensions.compatiblePackages
|
|
|
|
import app.revanced.patcher.extensions.PatchExtensions.description
|
|
|
|
import app.revanced.patcher.extensions.PatchExtensions.patchName
|
|
|
|
import app.revanced.patcher.extensions.PatchExtensions.version
|
|
|
|
import app.revanced.patcher.patch.Patch
|
|
|
|
import app.revanced.patcher.util.patch.implementation.DexPatchBundle
|
|
|
|
import dalvik.system.DexClassLoader
|
2022-07-30 21:09:59 +02:00
|
|
|
import io.flutter.embedding.android.FlutterActivity
|
2022-08-06 15:04:18 +02:00
|
|
|
import io.flutter.embedding.engine.FlutterEngine
|
|
|
|
import io.flutter.plugin.common.MethodChannel
|
2022-08-13 11:56:30 +02:00
|
|
|
import java.io.File
|
|
|
|
import java.nio.file.Files
|
|
|
|
import java.nio.file.StandardCopyOption
|
2022-07-30 21:09:59 +02:00
|
|
|
|
2022-08-06 15:04:18 +02:00
|
|
|
class MainActivity : FlutterActivity() {
|
2022-08-13 11:56:30 +02:00
|
|
|
private val CHANNEL = "app.revanced.manager/patcher"
|
2022-08-06 15:04:18 +02:00
|
|
|
private var patches = mutableListOf<Class<out Patch<Data>>>()
|
2022-08-13 11:56:30 +02:00
|
|
|
private val tag = "Patcher"
|
|
|
|
private lateinit var methodChannel: MethodChannel
|
|
|
|
private lateinit var patcher: Patcher
|
2022-08-06 15:04:18 +02:00
|
|
|
|
|
|
|
override fun configureFlutterEngine(@NonNull flutterEngine: FlutterEngine) {
|
|
|
|
super.configureFlutterEngine(flutterEngine)
|
2022-08-13 11:56:30 +02:00
|
|
|
methodChannel = MethodChannel(flutterEngine.dartExecutor.binaryMessenger, CHANNEL)
|
|
|
|
methodChannel.setMethodCallHandler { call, result ->
|
2022-08-06 15:04:18 +02:00
|
|
|
when (call.method) {
|
|
|
|
"loadPatches" -> {
|
|
|
|
val pathBundlesPaths = call.argument<List<String>>("pathBundlesPaths")
|
|
|
|
if (pathBundlesPaths != null) {
|
2022-08-13 11:56:30 +02:00
|
|
|
result.success(loadPatches(pathBundlesPaths))
|
2022-08-06 15:04:18 +02:00
|
|
|
} else {
|
|
|
|
result.notImplemented()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"getCompatiblePackages" -> result.success(getCompatiblePackages())
|
|
|
|
"getFilteredPatches" -> {
|
|
|
|
val targetPackage = call.argument<String>("targetPackage")
|
|
|
|
val targetVersion = call.argument<String>("targetVersion")
|
|
|
|
val ignoreVersion = call.argument<Boolean>("ignoreVersion")
|
|
|
|
if (targetPackage != null && targetVersion != null && ignoreVersion != null) {
|
2022-08-13 11:56:30 +02:00
|
|
|
result.success(
|
|
|
|
getFilteredPatches(targetPackage, targetVersion, ignoreVersion)
|
|
|
|
)
|
|
|
|
} else {
|
|
|
|
result.notImplemented()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"copyInputFile" -> {
|
|
|
|
val originalFilePath = call.argument<String>("originalFilePath")
|
|
|
|
val inputFilePath = call.argument<String>("inputFilePath")
|
|
|
|
if (originalFilePath != null && inputFilePath != null) {
|
|
|
|
result.success(copyInputFile(originalFilePath, inputFilePath))
|
|
|
|
} else {
|
|
|
|
result.notImplemented()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"createPatcher" -> {
|
|
|
|
val inputFilePath = call.argument<String>("inputFilePath")
|
|
|
|
val cacheDirPath = call.argument<String>("cacheDirPath")
|
|
|
|
if (inputFilePath != null && cacheDirPath != null) {
|
|
|
|
result.success(createPatcher(inputFilePath, cacheDirPath))
|
|
|
|
} else {
|
|
|
|
result.notImplemented()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"mergeIntegrations" -> {
|
|
|
|
val integrationsPath = call.argument<String>("integrationsPath")
|
|
|
|
if (integrationsPath != null) {
|
|
|
|
result.success(mergeIntegrations(integrationsPath))
|
|
|
|
} else {
|
|
|
|
result.notImplemented()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"applyPatches" -> {
|
|
|
|
val selectedPatches = call.argument<List<String>>("selectedPatches")
|
|
|
|
if (selectedPatches != null) {
|
|
|
|
result.success(applyPatches(selectedPatches))
|
|
|
|
} else {
|
|
|
|
result.notImplemented()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"repackPatchedFile" -> {
|
|
|
|
val inputFilePath = call.argument<String>("inputFilePath")
|
|
|
|
val patchedFilePath = call.argument<String>("patchedFilePath")
|
|
|
|
if (inputFilePath != null && patchedFilePath != null) {
|
|
|
|
result.success(repackPatchedFile(inputFilePath, patchedFilePath))
|
|
|
|
} else {
|
|
|
|
result.notImplemented()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"signPatchedFile" -> {
|
|
|
|
val patchedFilePath = call.argument<String>("patchedFilePath")
|
|
|
|
val outFilePath = call.argument<String>("outFilePath")
|
|
|
|
if (patchedFilePath != null && outFilePath != null) {
|
|
|
|
result.success(signPatchedFile(patchedFilePath, outFilePath))
|
2022-08-06 15:04:18 +02:00
|
|
|
} else {
|
|
|
|
result.notImplemented()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else -> result.notImplemented()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-13 11:56:30 +02:00
|
|
|
fun loadPatches(pathBundlesPaths: List<String>): Boolean {
|
|
|
|
try {
|
|
|
|
pathBundlesPaths.forEach { path ->
|
|
|
|
patches.addAll(
|
|
|
|
DexPatchBundle(
|
|
|
|
path,
|
|
|
|
DexClassLoader(
|
|
|
|
path,
|
2022-08-14 04:07:28 +02:00
|
|
|
applicationContext.cacheDir.path,
|
2022-08-13 11:56:30 +02:00
|
|
|
null,
|
|
|
|
javaClass.classLoader
|
|
|
|
)
|
|
|
|
)
|
|
|
|
.loadPatches()
|
2022-08-06 15:04:18 +02:00
|
|
|
)
|
2022-08-13 11:56:30 +02:00
|
|
|
}
|
|
|
|
} catch (e: Exception) {
|
|
|
|
return false
|
2022-08-06 15:04:18 +02:00
|
|
|
}
|
2022-08-13 11:56:30 +02:00
|
|
|
return true
|
2022-08-06 15:04:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
fun getCompatiblePackages(): List<String> {
|
|
|
|
val filteredPackages = mutableListOf<String>()
|
|
|
|
patches.forEach patch@{ patch ->
|
2022-08-13 11:56:30 +02:00
|
|
|
patch.compatiblePackages?.forEach { pkg -> filteredPackages.add(pkg.name) }
|
2022-08-06 15:04:18 +02:00
|
|
|
}
|
|
|
|
return filteredPackages.distinct()
|
|
|
|
}
|
|
|
|
|
2022-08-13 11:56:30 +02:00
|
|
|
fun getFilteredPatches(
|
|
|
|
targetPackage: String,
|
|
|
|
targetVersion: String,
|
|
|
|
ignoreVersion: Boolean
|
|
|
|
): List<Map<String, String?>> {
|
2022-08-06 15:04:18 +02:00
|
|
|
val filteredPatches = mutableListOf<Map<String, String?>>()
|
|
|
|
patches.forEach patch@{ patch ->
|
|
|
|
patch.compatiblePackages?.forEach { pkg ->
|
2022-08-13 11:56:30 +02:00
|
|
|
if (pkg.name == targetPackage &&
|
|
|
|
(ignoreVersion ||
|
|
|
|
pkg.versions.isNotEmpty() ||
|
|
|
|
pkg.versions.contains(targetVersion))
|
|
|
|
) {
|
|
|
|
var p = mutableMapOf<String, String?>()
|
|
|
|
p.put("name", patch.patchName)
|
|
|
|
p.put("version", patch.version)
|
|
|
|
p.put("description", patch.description)
|
2022-08-06 15:04:18 +02:00
|
|
|
filteredPatches.add(p)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return filteredPatches
|
|
|
|
}
|
2022-08-13 11:56:30 +02:00
|
|
|
|
|
|
|
private fun findPatchesByIds(ids: Iterable<String>): List<Class<out Patch<Data>>> {
|
|
|
|
return patches.filter { patch -> ids.any { it == patch.patchName } }
|
|
|
|
}
|
|
|
|
|
|
|
|
fun copyInputFile(originalFilePath: String, inputFilePath: String): Boolean {
|
|
|
|
val originalFile = File(originalFilePath)
|
|
|
|
val inputFile = File(inputFilePath)
|
|
|
|
Files.copy(originalFile.toPath(), inputFile.toPath(), StandardCopyOption.REPLACE_EXISTING)
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
fun createPatcher(inputFilePath: String, cacheDirPath: String): Boolean {
|
|
|
|
val inputFile = File(inputFilePath)
|
2022-08-14 04:07:28 +02:00
|
|
|
val aaptPath = Aapt.binary(applicationContext).absolutePath
|
2022-08-13 11:56:30 +02:00
|
|
|
patcher = Patcher(PatcherOptions(inputFile, cacheDirPath, true, aaptPath, cacheDirPath))
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
fun mergeIntegrations(integrationsPath: String): Boolean {
|
|
|
|
val integrations = File(integrationsPath)
|
|
|
|
if (patcher == null) return false
|
|
|
|
patcher.addFiles(listOf(integrations)) {}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
fun applyPatches(selectedPatches: List<String>): Boolean {
|
|
|
|
val patches = findPatchesByIds(selectedPatches)
|
|
|
|
if (patches.isEmpty()) return false
|
|
|
|
if (patcher == null) return false
|
|
|
|
patcher.addPatches(patches)
|
|
|
|
patcher.applyPatches().forEach { (patch, result) ->
|
|
|
|
if (result.isSuccess) {
|
|
|
|
val msg = "[success] $patch"
|
|
|
|
methodChannel.invokeMethod("updateInstallerLog", msg)
|
|
|
|
return@forEach
|
|
|
|
}
|
|
|
|
val msg = "[error] $patch:" + result.exceptionOrNull()!!
|
|
|
|
methodChannel.invokeMethod("updateInstallerLog", msg)
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
fun repackPatchedFile(inputFilePath: String, patchedFilePath: String): Boolean {
|
|
|
|
val inputFile = File(inputFilePath)
|
|
|
|
val patchedFile = File(patchedFilePath)
|
|
|
|
if (patcher == null) return false
|
|
|
|
val result = patcher.save()
|
|
|
|
ZipFile(patchedFile).use { file ->
|
|
|
|
result.dexFiles.forEach {
|
|
|
|
file.addEntryCompressData(
|
|
|
|
ZipEntry.createWithName(it.name),
|
|
|
|
it.dexFileInputStream.readBytes()
|
|
|
|
)
|
|
|
|
}
|
|
|
|
result.resourceFile?.let {
|
|
|
|
file.copyEntriesFromFileAligned(ZipFile(it), ZipAligner::getEntryAlignment)
|
|
|
|
}
|
|
|
|
file.copyEntriesFromFileAligned(ZipFile(inputFile), ZipAligner::getEntryAlignment)
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
fun signPatchedFile(patchedFilePath: String, outFilePath: String): Boolean {
|
|
|
|
val patchedFile = File(patchedFilePath)
|
|
|
|
val outFile = File(outFilePath)
|
|
|
|
Signer("ReVanced", "s3cur3p@ssw0rd").signApk(patchedFile, outFile)
|
|
|
|
return true
|
|
|
|
}
|
2022-07-30 21:09:59 +02:00
|
|
|
}
|