forked from ReVanced/revanced-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
17 changed files
with
430 additions
and
426 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
109 changes: 109 additions & 0 deletions
109
src/main/kotlin/app/revanced/cli/command/MainCommand.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
package app.revanced.cli.command | ||
|
||
import app.revanced.cli.patcher.Patcher | ||
import app.revanced.cli.signing.Signing | ||
import app.revanced.patcher.PatcherOptions | ||
import app.revanced.patcher.extensions.PatchExtensions.patchName | ||
import app.revanced.patcher.util.patch.implementation.JarPatchBundle | ||
import app.revanced.utils.adb.Adb | ||
import picocli.CommandLine.Command | ||
import picocli.CommandLine.Option | ||
import java.io.File | ||
import java.nio.file.Files | ||
|
||
@Command( | ||
name = "ReVanced-CLI", version = ["1.0.0"], mixinStandardHelpOptions = true, | ||
) | ||
internal object MainCommand : Runnable { | ||
@Option(names = ["-a", "--apk"], description = ["Input file to be patched"], required = true) | ||
lateinit var inputFile: File | ||
|
||
@Option(names = ["-o", "--out"], description = ["Output file path"], required = true) | ||
lateinit var outputPath: String | ||
|
||
@Option( | ||
names = ["-i", "--include"], | ||
description = ["Which patches to include. If none is specified, all compatible default patches will be included"] | ||
) | ||
var includedPatches = arrayOf<String>() | ||
|
||
@Option(names = ["-r", "--resource-patcher"], description = ["Disable patching resources"]) | ||
var disableResourcePatching: Boolean = false | ||
|
||
@Option(names = ["--debugging"], description = ["Disable patch version compatibility"]) | ||
var debugging: Boolean = false | ||
|
||
@Option(names = ["-m", "--merge"], description = ["One or more dex file containers to merge"]) | ||
var mergeFiles = listOf<File>() | ||
|
||
@Option(names = ["-b", "--bundles"], description = ["One or more bundles of patches"]) | ||
var patchBundles = arrayOf<String>() | ||
|
||
@Option(names = ["-l", "--list"], description = ["List patches only"]) | ||
var listOnly: Boolean = false | ||
|
||
@Option(names = ["--install"], description = ["If specified, instead of mounting, install"]) | ||
var install: Boolean = false | ||
|
||
@Option(names = ["--cn"], description = ["Overwrite the default CN for the signed file"]) | ||
var cn = "ReVanced" | ||
|
||
@Option(names = ["-p", "--password"], description = ["Overwrite the default password for the signed file"]) | ||
var password = "ReVanced" | ||
|
||
@Option(names = ["-d", "--deploy-on"], description = ["If specified, deploy to adb device with given name"]) | ||
var deploy: String? = null | ||
|
||
@Option(names = ["-t", "--temp-dir"], description = ["Temporal resource cache directory"]) | ||
var cacheDirectory = "revanced-cache" | ||
|
||
@Option( | ||
names = ["-c", "--clean"], | ||
description = ["Clean the temporal resource cache directory. This will be done anyways when running the patcher"] | ||
) | ||
var clean: Boolean = false | ||
|
||
@Option(names = ["--sign"], description = ["Sign the apk file"]) | ||
var signApk: Boolean = false | ||
|
||
override fun run() { | ||
if (listOnly) { | ||
for (patchBundlePath in patchBundles) for (patch in JarPatchBundle(patchBundlePath).loadPatches()) { | ||
println("[available] ${patch.patchName}") | ||
} | ||
return | ||
} | ||
|
||
val patcher = app.revanced.patcher.Patcher(PatcherOptions(inputFile, cacheDirectory, !disableResourcePatching)) | ||
|
||
val outputFile = File(outputPath) | ||
|
||
val adb: Adb? = deploy?.let { | ||
Adb(outputFile, patcher.data.packageMetadata.packageName, deploy!!, install) | ||
} | ||
|
||
val patchedFile = if (signApk) File(cacheDirectory).resolve("raw.apk") else outputFile | ||
|
||
Patcher.start(patcher, patchedFile) | ||
|
||
if (signApk) { | ||
Signing.start( | ||
patchedFile, | ||
outputFile, | ||
cn, | ||
password, | ||
) | ||
} | ||
|
||
if (clean) File(cacheDirectory).deleteRecursively() | ||
|
||
adb?.let { | ||
println("[deploying]") | ||
it.deploy() | ||
} | ||
|
||
if (clean && deploy != null) Files.delete(outputFile.toPath()) | ||
|
||
println("[done]") | ||
} | ||
} |
3 changes: 2 additions & 1 deletion
3
src/main/kotlin/app/revanced/cli/Main.kt → ...main/kotlin/app/revanced/cli/main/Main.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package app.revanced.cli.patcher | ||
|
||
import app.revanced.cli.command.MainCommand.cacheDirectory | ||
import app.revanced.cli.command.MainCommand.disableResourcePatching | ||
import app.revanced.cli.command.MainCommand | ||
import app.revanced.cli.command.MainCommand.includedPatches | ||
import app.revanced.utils.filesystem.ZipFileSystemUtils | ||
import app.revanced.utils.patcher.addPatchesFiltered | ||
import app.revanced.utils.patcher.applyPatchesVerbose | ||
import app.revanced.utils.patcher.mergeFiles | ||
import java.io.File | ||
import java.nio.file.Files | ||
|
||
internal object Patcher { | ||
internal fun start(patcher: app.revanced.patcher.Patcher, output: File) { | ||
// merge files like necessary integrations | ||
patcher.mergeFiles() | ||
// add patches, but filter incompatible or excluded patches | ||
patcher.addPatchesFiltered(includeFilter = includedPatches.isNotEmpty()) | ||
// apply patches | ||
patcher.applyPatchesVerbose() | ||
|
||
// write output file | ||
if (output.exists()) Files.delete(output.toPath()) | ||
MainCommand.inputFile.copyTo(output) | ||
|
||
ZipFileSystemUtils(output).use { fileSystem -> | ||
// replace all dex files | ||
val result = patcher.save() | ||
result.dexFiles.forEach { | ||
fileSystem.write(it.name, it.memoryDataStore.data) | ||
} | ||
|
||
// write resources | ||
if (!disableResourcePatching) { | ||
fileSystem.writePathRecursively(File(cacheDirectory).resolve("build").toPath()) | ||
fileSystem.uncompress(*result.doNotCompress!!.toTypedArray()) | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package app.revanced.cli.signing | ||
|
||
import app.revanced.cli.command.MainCommand.cacheDirectory | ||
import app.revanced.utils.signing.Signer | ||
import app.revanced.utils.signing.align.ZipAligner | ||
import java.io.File | ||
|
||
object Signing { | ||
fun start(inputFile: File, outputFile: File, cn: String, password: String) { | ||
// align & sign | ||
val cacheDirectory = File(cacheDirectory) | ||
val alignedOutput = cacheDirectory.resolve("aligned.apk") | ||
val signedOutput = cacheDirectory.resolve("signed.apk") | ||
ZipAligner.align(inputFile, alignedOutput) | ||
Signer( | ||
cn, | ||
password | ||
).signApk(inputFile, signedOutput) | ||
|
||
// write to output | ||
signedOutput.copyTo(outputFile) | ||
} | ||
} |
Oops, something went wrong.