Skip to content

Commit

Permalink
feat: Add progress bar
Browse files Browse the repository at this point in the history
  • Loading branch information
Sculas committed Apr 11, 2022
1 parent bea8b82 commit 8d96ec8
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 14 deletions.
1 change: 1 addition & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ dependencies {
implementation(patchesDependency)

implementation("com.google.code.gson:gson:2.9.0")
implementation("me.tongfei:progressbar:0.9.3")
}

tasks {
Expand Down
55 changes: 47 additions & 8 deletions src/main/kotlin/app/revanced/cli/Main.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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()
}
}
}

Expand Down
16 changes: 10 additions & 6 deletions src/main/kotlin/app/revanced/cli/utils/SignatureParser.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
}
}
Expand Down

0 comments on commit 8d96ec8

Please sign in to comment.