Skip to content

Commit

Permalink
feat: use of java.util.logging.Logger
Browse files Browse the repository at this point in the history
  • Loading branch information
oSumAtrIX committed Jun 22, 2022
1 parent a48c086 commit 07f6bdf
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 17 deletions.
18 changes: 10 additions & 8 deletions src/main/kotlin/app/revanced/cli/command/MainCommand.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,14 @@ import app.revanced.utils.adb.Adb
import picocli.CommandLine.*
import java.io.File
import java.nio.file.Files
import java.util.logging.Logger

@Command(
name = "ReVanced-CLI", version = ["1.0.0"], mixinStandardHelpOptions = true
)
internal object MainCommand : Runnable {
val logger: Logger = Logger.getLogger(MainCommand.javaClass.name)

@ArgGroup(exclusive = false, multiplicity = "1")
lateinit var args: Args

Expand Down Expand Up @@ -76,15 +79,19 @@ internal object MainCommand : Runnable {
}

override fun run() {
System.setProperty("java.util.logging.SimpleFormatter.format", "%4\$s: %5\$s %n")

if (args.lArgs?.listOnly == true) {
for (patchBundlePath in args.patchBundles) for (patch in JarPatchBundle(patchBundlePath).loadPatches()) {
println("[available] ${patch.patchName}: ${patch.description}")
logger.info("${patch.patchName}: ${patch.description}")
}
return
}

val args = args.pArgs ?: return

logger.info("Initialize patcher")

val patcher = app.revanced.patcher.Patcher(
PatcherOptions(
args.inputFile, args.cacheDirectory, !args.disableResourcePatching
Expand All @@ -102,8 +109,6 @@ internal object MainCommand : Runnable {

Patcher.start(patcher, patchedFile)

println("[aligning & signing]")

if (!args.mount) {
Signing.start(
patchedFile,
Expand All @@ -115,13 +120,10 @@ internal object MainCommand : Runnable {

if (args.clean) File(args.cacheDirectory).deleteRecursively()

adb?.let {
println("[deploying]")
it.deploy()
}
adb?.deploy()

if (args.clean && args.deploy != null) Files.delete(outputFile.toPath())

println("[done]")
logger.info("Done")
}
}
5 changes: 5 additions & 0 deletions src/main/kotlin/app/revanced/cli/patcher/Patcher.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package app.revanced.cli.patcher

import app.revanced.cli.command.MainCommand.args
import app.revanced.cli.command.MainCommand.logger
import app.revanced.utils.filesystem.ZipFileSystemUtils
import app.revanced.utils.patcher.addPatchesFiltered
import app.revanced.utils.patcher.applyPatchesVerbose
Expand Down Expand Up @@ -30,9 +31,13 @@ internal object Patcher {
ZipFileSystemUtils(inputFile, output).use { fileSystem ->
// replace all dex files
result.dexFiles.forEach {
logger.info("Write ${it.name}")

fileSystem.write(it.name, it.dexFileInputStream.readAllBytes())
}

logger.info("Write resources")

// inputFile being null implies resource patching being disabled
if (inputFile != null) {
// write resources
Expand Down
6 changes: 6 additions & 0 deletions src/main/kotlin/app/revanced/cli/signing/Signing.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package app.revanced.cli.signing

import app.revanced.cli.command.MainCommand.args
import app.revanced.cli.command.MainCommand.logger
import app.revanced.utils.signing.Signer
import app.revanced.utils.signing.align.ZipAligner
import java.io.File
Expand All @@ -11,8 +12,13 @@ object Signing {
val alignedOutput = cacheDirectory.resolve("${outputFile.nameWithoutExtension}_aligned.apk")
val signedOutput = cacheDirectory.resolve("${outputFile.nameWithoutExtension}_signed.apk")

logger.info("Align zip entries")

// align the inputFile and write to alignedOutput
ZipAligner.align(inputFile, alignedOutput)

logger.info("Sign zip file")

// sign the alignedOutput and write to signedOutput
// the reason is, in case the signer fails
// it does not damage the output file
Expand Down
5 changes: 5 additions & 0 deletions src/main/kotlin/app/revanced/utils/adb/Adb.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package app.revanced.utils.adb

import app.revanced.cli.command.MainCommand.logger
import se.vidstige.jadb.JadbConnection
import se.vidstige.jadb.JadbDevice
import se.vidstige.jadb.managers.PackageManager
Expand Down Expand Up @@ -29,8 +30,12 @@ internal class Adb(

internal fun deploy() {
if (modeInstall) {
logger.info("Install without mount")

PackageManager(device).install(file)
} else {
logger.info("Install by mount")

// push patched file
device.copy(Constants.PATH_INIT_PUSH, file)

Expand Down
22 changes: 13 additions & 9 deletions src/main/kotlin/app/revanced/utils/patcher/Patcher.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package app.revanced.utils.patcher

import app.revanced.cli.command.MainCommand
import app.revanced.cli.command.MainCommand.args
import app.revanced.cli.command.MainCommand.logger
import app.revanced.patcher.Patcher
import app.revanced.patcher.data.base.Data
import app.revanced.patcher.extensions.PatchExtensions.compatiblePackages
Expand All @@ -22,33 +23,34 @@ fun Patcher.addPatchesFiltered(
val compatiblePackages = patch.compatiblePackages
val patchName = patch.patchName

val prefix = "[skipped] $patchName"
val prefix = "Skip $patchName"

val args = MainCommand.args.pArgs!!

if (excludePatches && args.excludedPatches.contains(patchName)) {
println("$prefix: Explicitly excluded.")
logger.info("$prefix: Explicitly excluded")
return@patch
} else if (!patch.include) {
println("$prefix: Implicitly excluded.")
logger.info("$prefix: Implicitly excluded")
return@patch
}

if (compatiblePackages == null) println("$prefix: Missing compatibility annotation. Continuing.")
if (compatiblePackages == null) logger.warning("$prefix: Missing compatibility annotation. Continuing.")
else {
if (!compatiblePackages.any { it.name == packageName }) {
println("$prefix: Incompatible package.")
logger.info("$prefix: Incompatible package")
return@patch
}

if (!(args.experimental || compatiblePackages.any { it.versions.isEmpty() || it.versions.any { version -> version == packageVersion } })) {
println("$prefix: The package version is $packageVersion and is incompatible.")
logger.info("$prefix: The package version is $packageVersion and is incompatible")
return@patch
}
}

logger.info("Add $patchName")

includedPatches.add(patch)
println("[added] $patchName")
}
this.addPatches(includedPatches)
}
Expand All @@ -57,10 +59,12 @@ fun Patcher.addPatchesFiltered(
fun Patcher.applyPatchesVerbose() {
this.applyPatches().forEach { (patch, result) ->
if (result.isSuccess) {
println("[success] $patch")
logger.info("Success: $patch")

return@forEach
}
println("[error] $patch:")
logger.severe("Error: $patch")

result.exceptionOrNull()!!.printStackTrace()
}
}
Expand Down

0 comments on commit 07f6bdf

Please sign in to comment.