Skip to content

Commit

Permalink
fix: resource patcher
Browse files Browse the repository at this point in the history
  • Loading branch information
oSumAtrIX committed Jun 11, 2022
1 parent 45171dd commit 9da4f70
Show file tree
Hide file tree
Showing 17 changed files with 430 additions and 426 deletions.
3 changes: 2 additions & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ group = "app.revanced"

repositories {
mavenCentral()
google()
mavenLocal()
maven {
url = uri("https://maven.pkg.github.com/revanced/multidexlib2")
Expand All @@ -27,7 +28,7 @@ dependencies {
implementation("app.revanced:revanced-patcher:1.0.0")

implementation("info.picocli:picocli:4.6.3")

implementation("com.android.tools.build:apksig:7.2.1")
implementation("com.github.li-wjohnson:jadb:master-SNAPSHOT") // using a fork instead.
implementation("org.bouncycastle:bcpkix-jdk15on:1.70")
implementation("org.jetbrains.kotlin:kotlin-reflect:1.6.21")
Expand Down
96 changes: 0 additions & 96 deletions src/main/kotlin/app/revanced/cli/MainCommand.kt

This file was deleted.

55 changes: 0 additions & 55 deletions src/main/kotlin/app/revanced/cli/Patcher.kt

This file was deleted.

109 changes: 109 additions & 0 deletions src/main/kotlin/app/revanced/cli/command/MainCommand.kt
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]")
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package app.revanced.cli
package app.revanced.cli.main

import app.revanced.cli.command.MainCommand
import picocli.CommandLine

internal fun main(args: Array<String>) {
Expand Down
41 changes: 41 additions & 0 deletions src/main/kotlin/app/revanced/cli/patcher/Patcher.kt
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())
}
}
}
}
23 changes: 23 additions & 0 deletions src/main/kotlin/app/revanced/cli/signing/Signing.kt
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)
}
}
Loading

0 comments on commit 9da4f70

Please sign in to comment.