refactor: rename & change some things

This commit is contained in:
Lucaskyy 2022-04-16 13:49:25 +02:00
parent fb068ef753
commit 58213781e1
No known key found for this signature in database
GPG Key ID: 1530BFF96D1EEB89
2 changed files with 60 additions and 33 deletions

View File

@ -1,6 +1,6 @@
package app.revanced.cli package app.revanced.cli
import app.revanced.cli.runner.Emulator import app.revanced.cli.runner.AdbRunner
import app.revanced.cli.utils.PatchLoader import app.revanced.cli.utils.PatchLoader
import app.revanced.cli.utils.Patches import app.revanced.cli.utils.Patches
import app.revanced.cli.utils.Preconditions import app.revanced.cli.utils.Preconditions
@ -25,9 +25,10 @@ class Main {
inApk: String, inApk: String,
inPatches: String, inPatches: String,
inIntegrations: String?, inIntegrations: String?,
inOutput: String?, inOutput: String,
inEmulate: String?, inRunOnAdb: String?,
hideResults: Boolean, hideResults: Boolean,
noLogging: Boolean,
) { ) {
val bar = ProgressBarBuilder() val bar = ProgressBarBuilder()
.setTaskName("Working..") .setTaskName("Working..")
@ -39,6 +40,7 @@ class Main {
.setExtraMessage("Initializing") .setExtraMessage("Initializing")
val apk = Preconditions.isFile(inApk) val apk = Preconditions.isFile(inApk)
val patchesFile = Preconditions.isFile(inPatches) val patchesFile = Preconditions.isFile(inPatches)
val output = Preconditions.isDirectory(inOutput)
bar.step() bar.step()
val patcher = Patcher(apk) val patcher = Patcher(apk)
@ -63,8 +65,8 @@ class Main {
patcher.resolveSignatures() patcher.resolveSignatures()
bar.step() bar.step()
val amount = patches.size.toLong() val szPatches = patches.size.toLong()
bar.reset().maxHint(amount) bar.reset().maxHint(szPatches)
.extraMessage = "Applying patches" .extraMessage = "Applying patches"
val results = patcher.applyPatches { val results = patcher.applyPatches {
bar.step().extraMessage = "Applying $it" bar.step().extraMessage = "Applying $it"
@ -74,25 +76,24 @@ class Main {
.extraMessage = "Generating dex files" .extraMessage = "Generating dex files"
val dexFiles = patcher.save() val dexFiles = patcher.save()
inOutput?.let { val szDexFiles = dexFiles.size.toLong()
val output = Preconditions.isDirectory(it) bar.reset().maxHint(szDexFiles)
val amount = dexFiles.size.toLong() .extraMessage = "Saving dex files"
bar.reset().maxHint(amount) dexFiles.forEach { (dexName, dexData) ->
.extraMessage = "Saving dex files" Files.write(File(output, dexName).toPath(), dexData.data)
dexFiles.forEach { (dexName, dexData) -> bar.step()
Files.write(File(output, dexName).toPath(), dexData.data)
bar.step()
}
bar.stepTo(amount)
} }
bar.stepTo(szDexFiles)
bar.close() bar.close()
inEmulate?.let { device -> inRunOnAdb?.let { device ->
Emulator.emulate( AdbRunner.runApk(
apk, apk,
dexFiles, dexFiles,
device output,
device,
noLogging
) )
} }
@ -144,8 +145,8 @@ class Main {
fullName = "output", fullName = "output",
shortName = "o", shortName = "o",
description = "Output directory" description = "Output directory"
) ).required()
val emulate by parser.option( val runOnAdb by parser.option(
ArgType.String, ArgType.String,
fullName = "run-on", fullName = "run-on",
description = "After the CLI is done building, which ADB device should it run on?" description = "After the CLI is done building, which ADB device should it run on?"
@ -156,6 +157,11 @@ class Main {
fullName = "hide-results", fullName = "hide-results",
description = "Don't print the patch results." description = "Don't print the patch results."
).default(false) ).default(false)
val noLogging by parser.option(
ArgType.Boolean,
fullName = "no-logging",
description = "Don't print the output of the application when used in combination with \"run-on\"."
).default(false)
parser.parse(args) parser.parse(args)
runCLI( runCLI(
@ -163,8 +169,9 @@ class Main {
patches, patches,
integrations, integrations,
output, output,
emulate, runOnAdb,
hideResults, hideResults,
noLogging,
) )
} }
} }

View File

@ -12,14 +12,15 @@ import se.vidstige.jadb.JadbDevice
import se.vidstige.jadb.RemoteFile import se.vidstige.jadb.RemoteFile
import se.vidstige.jadb.ShellProcessBuilder import se.vidstige.jadb.ShellProcessBuilder
import java.io.File import java.io.File
import java.nio.file.Files
import java.util.concurrent.Executors import java.util.concurrent.Executors
object Emulator { object AdbRunner {
fun emulate( fun runApk(
apk: File, apk: File,
dexFiles: Map<String, MemoryDataStore>, dexFiles: Map<String, MemoryDataStore>,
deviceName: String outputDir: File,
deviceName: String,
noLogging: Boolean
) { ) {
lateinit var dvc: JadbDevice lateinit var dvc: JadbDevice
pbar("Initializing").use { bar -> pbar("Initializing").use { bar ->
@ -33,14 +34,23 @@ object Emulator {
lateinit var tmpFile: File // we need this file at the end to clean up. lateinit var tmpFile: File // we need this file at the end to clean up.
pbar("Generating APK file", 3).use { bar -> pbar("Generating APK file", 3).use { bar ->
bar.step().extraMessage = "Creating APK file" bar.step().extraMessage = "Creating APK file"
tmpFile = Files.createTempFile("rvc-cli", ".apk").toFile() tmpFile = File(outputDir, "revanced.apk")
apk.copyTo(tmpFile, true) apk.copyTo(tmpFile, true)
bar.step().extraMessage = "Replacing dex files" bar.step().extraMessage = "Replacing dex files"
DexReplacer.replaceDex(tmpFile, dexFiles) DexReplacer.replaceDex(tmpFile, dexFiles)
bar.step().extraMessage = "Signing APK file" bar.step().extraMessage = "Signing APK file"
Signer.signApk(tmpFile) try {
Signer.signApk(tmpFile)
} catch (e: SecurityException) {
throw IllegalStateException(
"A security exception occurred when signing the APK! " +
"If it has anything to with \"cannot authenticate\" then please make sure " +
"you are using Zulu or OpenJDK as they do work when using the adb runner.",
e
)
}
} }
pbar("Running application", 6, false).use { bar -> pbar("Running application", 6, false).use { bar ->
@ -63,14 +73,26 @@ object Emulator {
bar.step().setExtraMessage("Debugging APK file").refresh() bar.step().setExtraMessage("Debugging APK file").refresh()
println("\nWaiting until app is closed.") println("\nWaiting until app is closed.")
val executor = Executors.newSingleThreadExecutor() val executor = Executors.newSingleThreadExecutor()
val pipe = if (noLogging) {
ProcessBuilder.Redirect.PIPE
} else {
ProcessBuilder.Redirect.INHERIT
}
val p = dvc.cmd(Scripts.LOGCAT_COMMAND) val p = dvc.cmd(Scripts.LOGCAT_COMMAND)
.redirectOutput(ProcessBuilder.Redirect.INHERIT) .redirectOutput(pipe)
.redirectError(ProcessBuilder.Redirect.INHERIT) .redirectError(pipe)
.useExecutor(executor) .useExecutor(executor)
.start() .start()
Thread.sleep(250) // give the app some time to start up. Thread.sleep(250) // give the app some time to start up.
while (dvc.cmd(Scripts.PIDOF_APP_COMMAND).startAndWait() == 0) { while (true) {
Thread.sleep(250) try {
while (dvc.cmd(Scripts.PIDOF_APP_COMMAND).startAndWait() == 0) {
Thread.sleep(250)
}
break
} catch (e: Exception) {
throw RuntimeException("An error occurred while monitoring state of app", e)
}
} }
println("App closed, continuing.") println("App closed, continuing.")
p.destroy() p.destroy()
@ -82,8 +104,6 @@ object Emulator {
exitCode = dvc.cmd(Scripts.UNMOUNT_COMMAND).startAndWait() exitCode = dvc.cmd(Scripts.UNMOUNT_COMMAND).startAndWait()
} while (exitCode != 0) } while (exitCode != 0)
} }
tmpFile.delete()
} }
} }