mirror of
https://github.com/ReVanced/revanced-cli.git
synced 2026-01-22 02:43:57 +00:00
add: resource patcher
Signed-off-by: oSumAtrIX <johan.melkonyan1@web.de>
This commit is contained in:
@@ -1,40 +1,41 @@
|
||||
package app.revanced.cli
|
||||
|
||||
import app.revanced.cli.MainCommand.excludedPatches
|
||||
import app.revanced.cli.MainCommand.patchBundles
|
||||
import app.revanced.patcher.Patcher
|
||||
import app.revanced.patcher.patch.Patch
|
||||
import app.revanced.utils.dex.DexReplacer
|
||||
import app.revanced.utils.patch.PatchLoader
|
||||
import app.revanced.utils.patch.Patches
|
||||
import app.revanced.utils.signing.Signer
|
||||
import app.revanced.patch.PatchLoader
|
||||
import app.revanced.patch.Patches
|
||||
import picocli.CommandLine
|
||||
import picocli.CommandLine.*
|
||||
import java.io.File
|
||||
|
||||
@Command(
|
||||
name = "ReVanced-CLI",
|
||||
version = ["1.0.0"],
|
||||
mixinStandardHelpOptions = true
|
||||
name = "ReVanced-CLI", version = ["1.0.0"], mixinStandardHelpOptions = true
|
||||
)
|
||||
object MainCommand : Runnable {
|
||||
internal object MainCommand : Runnable {
|
||||
@Option(names = ["-p", "--patches"], description = ["One or more bundles of patches"])
|
||||
var patchBundles = arrayOf<File>()
|
||||
internal var patchBundles = arrayOf<File>()
|
||||
|
||||
@Parameters(paramLabel = "EXCLUDE", description = ["Which patches to exclude"])
|
||||
var excludedPatches = arrayOf<String>()
|
||||
@Parameters(
|
||||
paramLabel = "INCLUDE",
|
||||
description = ["Which patches to include. If none is specified, all compatible patches will be included."]
|
||||
)
|
||||
internal var includedPatches = arrayOf<String>()
|
||||
|
||||
@Option(names = ["-c", "--cache"], description = ["Output resource cache directory"], required = true)
|
||||
internal lateinit var cacheDirectory: String
|
||||
|
||||
@Option(names = ["-r", "--resource-patcher"], description = ["Enable patching resources"])
|
||||
internal var patchResources: Boolean = false
|
||||
|
||||
@Option(names = ["-l", "--list"], description = ["List patches only"])
|
||||
var listOnly: Boolean = false
|
||||
internal var listOnly: Boolean = false
|
||||
|
||||
@Option(names = ["-m", "--merge"], description = ["One or more dex file containers to merge"])
|
||||
var mergeFiles = listOf<File>()
|
||||
internal var mergeFiles = listOf<File>()
|
||||
|
||||
@Option(names = ["-a", "--apk"], description = ["Input file to be patched"], required = true)
|
||||
lateinit var inputFile: File
|
||||
internal lateinit var inputFile: File
|
||||
|
||||
@Option(names = ["-o", "--out"], description = ["Output file path"], required = true)
|
||||
lateinit var outputPath: String
|
||||
internal lateinit var outputPath: String
|
||||
|
||||
override fun run() {
|
||||
if (listOnly) {
|
||||
@@ -47,64 +48,10 @@ object MainCommand : Runnable {
|
||||
return
|
||||
}
|
||||
|
||||
val patcher = Patcher(inputFile)
|
||||
// merge files like necessary integrations
|
||||
patcher.addFiles(mergeFiles)
|
||||
// add patches, but filter incompatible or excluded patches
|
||||
patcher.addPatchesFiltered()
|
||||
// apply patches
|
||||
for (patchResult in patcher.applyPatches {
|
||||
println("Applying: $it")
|
||||
}) {
|
||||
println(patchResult)
|
||||
}
|
||||
|
||||
// write output file
|
||||
val outFile = File(outputPath)
|
||||
inputFile.copyTo(outFile)
|
||||
DexReplacer.replaceDex(outFile, patcher.save())
|
||||
|
||||
// sign the apk file
|
||||
Signer.signApk(outFile)
|
||||
Patcher.run()
|
||||
}
|
||||
}
|
||||
|
||||
private fun Patcher.addPatchesFiltered() {
|
||||
// TODO: get package metadata (outside of this method) for apk file which needs to be patched
|
||||
val packageName = "com.example.exampleApp"
|
||||
val packageVersion = "1.2.3"
|
||||
|
||||
patchBundles.forEach { bundle ->
|
||||
PatchLoader.injectPatches(bundle)
|
||||
val includedPatches = mutableListOf<Patch>()
|
||||
Patches.loadPatches().forEach patch@{
|
||||
val patch = it()
|
||||
|
||||
// TODO: filter out incompatible patches with package metadata
|
||||
val filterOutPatches = true
|
||||
if (filterOutPatches &&
|
||||
!patch.metadata.compatiblePackages.any { packageMetadata ->
|
||||
packageMetadata.name == packageName &&
|
||||
packageMetadata.versions.any {
|
||||
it == packageVersion
|
||||
}
|
||||
}
|
||||
) {
|
||||
// TODO: report to stdout
|
||||
return@patch
|
||||
}
|
||||
|
||||
if (excludedPatches.contains(patch.metadata.shortName)) {
|
||||
// TODO: report to stdout
|
||||
return@patch
|
||||
}
|
||||
|
||||
includedPatches.add(patch)
|
||||
}
|
||||
this.addPatches(includedPatches)
|
||||
}
|
||||
}
|
||||
|
||||
fun main(args: Array<String>) {
|
||||
internal fun main(args: Array<String>) {
|
||||
CommandLine(MainCommand).execute(*args)
|
||||
}
|
||||
96
src/main/kotlin/app/revanced/cli/Patcher.kt
Normal file
96
src/main/kotlin/app/revanced/cli/Patcher.kt
Normal file
@@ -0,0 +1,96 @@
|
||||
package app.revanced.cli
|
||||
|
||||
import app.revanced.patch.PatchLoader
|
||||
import app.revanced.patch.Patches
|
||||
import app.revanced.patcher.data.base.Data
|
||||
import app.revanced.patcher.patch.base.Patch
|
||||
import app.revanced.utils.filesystem.FileSystemUtils
|
||||
import app.revanced.utils.signing.Signer
|
||||
import java.io.File
|
||||
|
||||
internal class Patcher {
|
||||
internal companion object {
|
||||
internal fun run() {
|
||||
val patcher = app.revanced.patcher.Patcher(
|
||||
MainCommand.inputFile,
|
||||
MainCommand.cacheDirectory,
|
||||
MainCommand.patchResources
|
||||
)
|
||||
|
||||
// merge files like necessary integrations
|
||||
patcher.addFiles(MainCommand.mergeFiles)
|
||||
// add patches, but filter incompatible or excluded patches
|
||||
patcher.addPatchesFiltered()
|
||||
// apply patches
|
||||
for (patchResult in patcher.applyPatches {
|
||||
println("Applying: $it")
|
||||
}) {
|
||||
println(patchResult)
|
||||
}
|
||||
|
||||
// write output file
|
||||
val outFile = File(MainCommand.outputPath)
|
||||
if (outFile.exists()) outFile.delete()
|
||||
MainCommand.inputFile.copyTo(outFile)
|
||||
|
||||
val zipFileSystem = FileSystemUtils(outFile)
|
||||
|
||||
// replace all dex files
|
||||
for ((name, data) in patcher.save()) {
|
||||
zipFileSystem.replaceFile(name, data.data)
|
||||
}
|
||||
|
||||
if (MainCommand.patchResources) {
|
||||
for (file in File(MainCommand.cacheDirectory).resolve("build/").listFiles().first().listFiles()) {
|
||||
if (!file.isDirectory) {
|
||||
zipFileSystem.replaceFile(file.name, file.readBytes())
|
||||
continue
|
||||
}
|
||||
zipFileSystem.replaceDirectory(file)
|
||||
}
|
||||
}
|
||||
|
||||
// finally close the stream
|
||||
zipFileSystem.close()
|
||||
|
||||
// and sign the apk file
|
||||
Signer.signApk(outFile)
|
||||
}
|
||||
|
||||
private fun app.revanced.patcher.Patcher.addPatchesFiltered() {
|
||||
// TODO: get package metadata (outside of this method) for apk file which needs to be patched
|
||||
val packageName = this.packageName
|
||||
val packageVersion = this.packageVersion
|
||||
|
||||
val checkInclude = MainCommand.includedPatches.isNotEmpty()
|
||||
|
||||
MainCommand.patchBundles.forEach { bundle ->
|
||||
PatchLoader.injectPatches(bundle)
|
||||
val includedPatches = mutableListOf<Patch<Data>>()
|
||||
Patches.loadPatches().forEach patch@{
|
||||
val patch = it()
|
||||
|
||||
// TODO: filter out incompatible patches with package metadata
|
||||
val filterOutPatches = true
|
||||
if (filterOutPatches && !patch.metadata.compatiblePackages.any { packageMetadata ->
|
||||
packageMetadata.name == packageName && packageMetadata.versions.any {
|
||||
it == packageVersion
|
||||
}
|
||||
}) {
|
||||
// TODO: report to stdout
|
||||
return@patch
|
||||
}
|
||||
|
||||
if (checkInclude && !MainCommand.includedPatches.contains(patch.metadata.shortName)) {
|
||||
return@patch
|
||||
}
|
||||
|
||||
// TODO: report to stdout
|
||||
includedPatches.add(patch)
|
||||
|
||||
}
|
||||
this.addPatches(includedPatches)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user