From 58213781e13993cb62942452add2fd6451f16bc6 Mon Sep 17 00:00:00 2001 From: Lucaskyy Date: Sat, 16 Apr 2022 13:49:25 +0200 Subject: [PATCH] refactor: rename & change some things --- src/main/kotlin/app/revanced/cli/Main.kt | 49 +++++++++++-------- .../app/revanced/cli/runner/AdbRunner.kt | 44 ++++++++++++----- 2 files changed, 60 insertions(+), 33 deletions(-) diff --git a/src/main/kotlin/app/revanced/cli/Main.kt b/src/main/kotlin/app/revanced/cli/Main.kt index e5cf5d8..a598750 100644 --- a/src/main/kotlin/app/revanced/cli/Main.kt +++ b/src/main/kotlin/app/revanced/cli/Main.kt @@ -1,6 +1,6 @@ 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.Patches import app.revanced.cli.utils.Preconditions @@ -25,9 +25,10 @@ class Main { inApk: String, inPatches: String, inIntegrations: String?, - inOutput: String?, - inEmulate: String?, + inOutput: String, + inRunOnAdb: String?, hideResults: Boolean, + noLogging: Boolean, ) { val bar = ProgressBarBuilder() .setTaskName("Working..") @@ -39,6 +40,7 @@ class Main { .setExtraMessage("Initializing") val apk = Preconditions.isFile(inApk) val patchesFile = Preconditions.isFile(inPatches) + val output = Preconditions.isDirectory(inOutput) bar.step() val patcher = Patcher(apk) @@ -63,8 +65,8 @@ class Main { patcher.resolveSignatures() bar.step() - val amount = patches.size.toLong() - bar.reset().maxHint(amount) + val szPatches = patches.size.toLong() + bar.reset().maxHint(szPatches) .extraMessage = "Applying patches" val results = patcher.applyPatches { bar.step().extraMessage = "Applying $it" @@ -74,25 +76,24 @@ class Main { .extraMessage = "Generating dex files" val dexFiles = patcher.save() - inOutput?.let { - val output = Preconditions.isDirectory(it) - val amount = dexFiles.size.toLong() - bar.reset().maxHint(amount) - .extraMessage = "Saving dex files" - dexFiles.forEach { (dexName, dexData) -> - Files.write(File(output, dexName).toPath(), dexData.data) - bar.step() - } - bar.stepTo(amount) + val szDexFiles = dexFiles.size.toLong() + bar.reset().maxHint(szDexFiles) + .extraMessage = "Saving dex files" + dexFiles.forEach { (dexName, dexData) -> + Files.write(File(output, dexName).toPath(), dexData.data) + bar.step() } + bar.stepTo(szDexFiles) bar.close() - inEmulate?.let { device -> - Emulator.emulate( + inRunOnAdb?.let { device -> + AdbRunner.runApk( apk, dexFiles, - device + output, + device, + noLogging ) } @@ -144,8 +145,8 @@ class Main { fullName = "output", shortName = "o", description = "Output directory" - ) - val emulate by parser.option( + ).required() + val runOnAdb by parser.option( ArgType.String, fullName = "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", description = "Don't print the patch results." ).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) runCLI( @@ -163,8 +169,9 @@ class Main { patches, integrations, output, - emulate, + runOnAdb, hideResults, + noLogging, ) } } diff --git a/src/main/kotlin/app/revanced/cli/runner/AdbRunner.kt b/src/main/kotlin/app/revanced/cli/runner/AdbRunner.kt index 1a05d57..99e94b9 100644 --- a/src/main/kotlin/app/revanced/cli/runner/AdbRunner.kt +++ b/src/main/kotlin/app/revanced/cli/runner/AdbRunner.kt @@ -12,14 +12,15 @@ import se.vidstige.jadb.JadbDevice import se.vidstige.jadb.RemoteFile import se.vidstige.jadb.ShellProcessBuilder import java.io.File -import java.nio.file.Files import java.util.concurrent.Executors -object Emulator { - fun emulate( +object AdbRunner { + fun runApk( apk: File, dexFiles: Map, - deviceName: String + outputDir: File, + deviceName: String, + noLogging: Boolean ) { lateinit var dvc: JadbDevice pbar("Initializing").use { bar -> @@ -33,14 +34,23 @@ object Emulator { lateinit var tmpFile: File // we need this file at the end to clean up. pbar("Generating APK file", 3).use { bar -> bar.step().extraMessage = "Creating APK file" - tmpFile = Files.createTempFile("rvc-cli", ".apk").toFile() + tmpFile = File(outputDir, "revanced.apk") apk.copyTo(tmpFile, true) bar.step().extraMessage = "Replacing dex files" DexReplacer.replaceDex(tmpFile, dexFiles) 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 -> @@ -63,14 +73,26 @@ object Emulator { bar.step().setExtraMessage("Debugging APK file").refresh() println("\nWaiting until app is closed.") val executor = Executors.newSingleThreadExecutor() + val pipe = if (noLogging) { + ProcessBuilder.Redirect.PIPE + } else { + ProcessBuilder.Redirect.INHERIT + } val p = dvc.cmd(Scripts.LOGCAT_COMMAND) - .redirectOutput(ProcessBuilder.Redirect.INHERIT) - .redirectError(ProcessBuilder.Redirect.INHERIT) + .redirectOutput(pipe) + .redirectError(pipe) .useExecutor(executor) .start() Thread.sleep(250) // give the app some time to start up. - while (dvc.cmd(Scripts.PIDOF_APP_COMMAND).startAndWait() == 0) { - Thread.sleep(250) + while (true) { + 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.") p.destroy() @@ -82,8 +104,6 @@ object Emulator { exitCode = dvc.cmd(Scripts.UNMOUNT_COMMAND).startAndWait() } while (exitCode != 0) } - - tmpFile.delete() } }