feat: Add progress bar

This commit is contained in:
Lucaskyy 2022-04-11 19:19:35 +02:00
parent bea8b829c7
commit 8d96ec83cb
No known key found for this signature in database
GPG Key ID: 1530BFF96D1EEB89
3 changed files with 58 additions and 14 deletions

View File

@ -28,6 +28,7 @@ dependencies {
implementation(patchesDependency)
implementation("com.google.code.gson:gson:2.9.0")
implementation("me.tongfei:progressbar:0.9.3")
}
tasks {

View File

@ -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<String, Result<PatchResult>>) {
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()
}
}
}

View File

@ -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<MethodSignature> {
val signatures = JsonParser.parseString(json).asJsonObject.get("signatures").asJsonArray.map { sig ->
fun parse(json: String, bar: ProgressBar): List<MethodSignature> {
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
}
}