From 8d96ec83cb11ac9323ef268884912961a2405435 Mon Sep 17 00:00:00 2001 From: Lucaskyy Date: Mon, 11 Apr 2022 19:19:35 +0200 Subject: [PATCH] feat: Add progress bar --- build.gradle.kts | 1 + src/main/kotlin/app/revanced/cli/Main.kt | 55 ++++++++++++++++--- .../app/revanced/cli/utils/SignatureParser.kt | 16 ++++-- 3 files changed, 58 insertions(+), 14 deletions(-) diff --git a/build.gradle.kts b/build.gradle.kts index 9be26ee3..cc5c8043 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -28,6 +28,7 @@ dependencies { implementation(patchesDependency) implementation("com.google.code.gson:gson:2.9.0") + implementation("me.tongfei:progressbar:0.9.3") } tasks { diff --git a/src/main/kotlin/app/revanced/cli/Main.kt b/src/main/kotlin/app/revanced/cli/Main.kt index ab6f6080..2c8363ab 100644 --- a/src/main/kotlin/app/revanced/cli/Main.kt +++ b/src/main/kotlin/app/revanced/cli/Main.kt @@ -5,9 +5,12 @@ import app.revanced.cli.utils.Patches import app.revanced.cli.utils.Preconditions import app.revanced.cli.utils.SignatureParser import app.revanced.patcher.Patcher +import app.revanced.patcher.patch.PatchResult import kotlinx.cli.ArgParser import kotlinx.cli.ArgType import kotlinx.cli.required +import me.tongfei.progressbar.ProgressBarBuilder +import me.tongfei.progressbar.ProgressBarStyle import java.io.File import java.nio.file.Files @@ -23,35 +26,71 @@ class Main { inIntegrations: String?, inOutput: String, ) { + val bar = ProgressBarBuilder() + .setTaskName("Working..") + .setUpdateIntervalMillis(25) + .continuousUpdate() + .setStyle(ProgressBarStyle.ASCII) + .build() + .maxHint(1) + .setExtraMessage("Initializing") val apk = Preconditions.isFile(inApk) val signatures = Preconditions.isFile(inSignatures) val patchesFile = Preconditions.isFile(inPatches) val output = Preconditions.isDirectory(inOutput) + bar.step() val patcher = Patcher( apk, - SignatureParser - .parse(signatures.readText()) - .toTypedArray() + SignatureParser.parse(signatures.readText(), bar) ) inIntegrations?.let { + bar.reset().maxHint(1) + .extraMessage = "Merging integrations" val integrations = Preconditions.isFile(it) patcher.addFiles(integrations) + bar.step() } + bar.reset().maxHint(1) + .extraMessage = "Loading patches" PatchLoader.injectPatches(patchesFile) - val patches = Patches.loadPatches() - patcher.addPatches(*patches.map { it() }.toTypedArray()) + bar.step() - val results = patcher.applyPatches() - for ((name, result) in results) { - println("$name: $result") + val patches = Patches.loadPatches().map { it() } + patcher.addPatches(patches) + + val amount = patches.size.toLong() + bar.reset().maxHint(amount) + .extraMessage = "Applying patches" + val results = patcher.applyPatches { + bar.step().extraMessage = "Applying $it" } + bar.reset().maxHint(-1) + .extraMessage = "Generating dex files" val dexFiles = patcher.save() + bar.reset().maxHint(dexFiles.size.toLong()) + .extraMessage = "Saving dex files" dexFiles.forEach { (dexName, dexData) -> Files.write(File(output, dexName).toPath(), dexData.buffer) + bar.step() + } + bar.close() + + println("All done!") + printResults(results) + } + + private fun printResults(results: Map>) { + for ((name, result) in results) { + if (result.isSuccess) { + println("Patch $name was applied successfully!") + } else { + println("Patch $name failed to apply! Cause:") + result.exceptionOrNull()!!.printStackTrace() + } } } diff --git a/src/main/kotlin/app/revanced/cli/utils/SignatureParser.kt b/src/main/kotlin/app/revanced/cli/utils/SignatureParser.kt index d586b0fb..67aec9cc 100644 --- a/src/main/kotlin/app/revanced/cli/utils/SignatureParser.kt +++ b/src/main/kotlin/app/revanced/cli/utils/SignatureParser.kt @@ -2,13 +2,17 @@ package app.revanced.cli.utils import app.revanced.patcher.signature.MethodSignature import com.google.gson.JsonParser +import me.tongfei.progressbar.ProgressBar import org.jf.dexlib2.AccessFlags import org.jf.dexlib2.Opcodes class SignatureParser { companion object { - fun parse(json: String): List { - val signatures = JsonParser.parseString(json).asJsonObject.get("signatures").asJsonArray.map { sig -> + fun parse(json: String, bar: ProgressBar): List { + val tmp = JsonParser.parseString(json).asJsonObject.get("signatures").asJsonArray + bar.reset().maxHint(tmp.size().toLong()) + .extraMessage = "Parsing signatures" + val signatures = tmp.map { sig -> val signature = sig.asJsonObject val returnType = signature.get("returns").asString @@ -19,21 +23,21 @@ class SignatureParser { val parameters = signature.get("parameters").asJsonArray .map { it.asString } - .toTypedArray() val opcodes = signature.get("opcodes").asJsonArray .map { Opcodes.getDefault().getOpcodeByName(it.asString)!! } - .toTypedArray() + val name = signature.get("name").asString + bar.step() + .extraMessage = "Parsing $name" MethodSignature( - signature.get("name").asString, + name, returnType, accessFlags, parameters, opcodes ) } - return signatures } }