Skip to content

Commit

Permalink
feat: Set patch options via CLI
Browse files Browse the repository at this point in the history
  • Loading branch information
oSumAtrIX committed Aug 9, 2024
1 parent 5a1642c commit 3cd6dbe
Show file tree
Hide file tree
Showing 7 changed files with 243 additions and 135 deletions.
82 changes: 72 additions & 10 deletions docs/1_usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,12 @@ java -jar revanced-cli.jar -h
## 📃 List patches

```bash
java -jar revanced-cli.jar list-patches --with-descriptions --with-packages --with-versions --with-options --with-universal-patches revanced-patches.rvp
java -jar revanced-cli.jar list-patches --with-packages --with-versions --with-options revanced-patches.rvp
```

## 💉 Patch an app with the default list of patches
## 💉 Patch an app

To patch an app using the default list of patches, use the `patch` command:

```bash
java -jar revanced-cli.jar patch -b revanced-patches.rvp input.apk
Expand All @@ -28,22 +30,37 @@ You can also use multiple patch bundles:
java -jar revanced-cli.jar patch -b revanced-patches.rvp -b another-patches.rvp input.apk
```

To manually include or exclude patches, use the options `-i` and `-e`.
Keep in mind the name of the patch must be an exact match.
You can also use the options `--ii` and `--ie` to include or exclude patches by their index
if two patches have the same name.
To know the indices of patches, use the option `--with-indices` when listing patches:
To change the default set of used patches, use the option `-i` or `-e` to use or disuse specific patches.
You can use the `list-patches` command to see which patches are used by default.

To only use specific patches, you can use the option `--exclusive` combined with `-i`.
Remember that the options `-i` and `-e` match the patch's name exactly. Here is an example:

```bash
java -jar revanced-cli.jar list-patches --with-indices revanced-patches.rvp
java -jar revanced-cli.jar patch -b revanced-patches.rvp --exclusive -i "Patch name" -i "Another patch name" input.apk
```

Then you can use the indices to include or exclude patches:
You can also use the options `--ii` and `--ie` to use or disuse patches by their index.
This is useful, if two patches happen to have the same name.
To know the indices of patches, use the command `list-patches`:

```bash
java -jar revanced-cli.jar list-patches revanced-patches.rvp
```

Then you can use the indices to use or disuse patches:

```bash
java -jar revanced-cli.jar patch -b revanced-patches.rvp --ii 123 --ie 456 input.apk
```

You can combine the option `-i`, `-e`, `--ii`, `--ie` and `--exclusive`. Here is an example:

```bash
java -jar revanced-cli.jar patch -b revanced-patches.rvp --exclusive -i "Patch name" --ii 123 input.apk
```


> [!TIP]
> You can use the option `-d` to automatically install the patched app after patching.
> Make sure ADB is working:
Expand All @@ -62,7 +79,52 @@ java -jar revanced-cli.jar patch -b revanced-patches.rvp --ii 123 --ie 456 input
> adb install input.apk
> ```
## 📦 Install an app manually
Patches can have options you can set using the option `--set-options`.
To know the options of a patch, use the option `--with-options` when listing patches:
```bash
java -jar revanced-cli.jar list-patches --with-options revanced-patches.rvp
```
Each patch can have multiple options. You can set them using the option `--set-options`.
For example, to set the options for the patch with the name `Patch name`
with the key `key1` and `key2` to `value1` and `value2` respectively, use the following command:

```bash
java -jar revanced-cli.jar patch -b revanced-patches.rvp --set-options "Patch name" -Okey1=value1 -Okey2=value2 input.apk
```

If you want to set a value to `null`, you can omit the value:

```bash
java -jar revanced-cli.jar patch -b revanced-patches.rvp --set-options "Patch name" -Okey1 input.apk
```

> [!WARNING]
> The values of options are typed. If you set a value with the wrong type, the patching process will fail.
> The type of the option value can be seen when listing patches with the option `--with-options`.
>
> Example values:
>
> String: `string`
> Boolean: `true`, `false`
> Integer: `123`
> Double: `1.0`
> Float: `1.0f`
> Long: `1234567890`, `1L`
> List: `item1,item2,item3`
>
> In addition to that, you can escape quotes (`\"`, `\'`) and commas (`\,`) to treat values as string literals:
>
> Integer as string: `\'123\'`
> List with an integer, an integer as a string and a string with a comma: `123,\'123\',str\,ing`
>
> Example command with escaped quotes:
>
> ```bash
> java -jar revanced-cli.jar -b revanced-patches.rvp --set-options "Patch name" -OstringKey=\'1\' input.apk
> ```
## 📦 Install an app manually
```bash
java -jar revanced-cli.jar utility install -a input.apk
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package app.revanced.cli.command

import app.revanced.library.PackageName
import app.revanced.library.PatchUtils
import app.revanced.library.VersionMap
import app.revanced.library.mostCommonCompatibleVersions
import app.revanced.patcher.patch.loadPatchesFromJar
import picocli.CommandLine
import java.io.File
Expand All @@ -12,7 +12,7 @@ import java.util.logging.Logger
name = "list-versions",
description = [
"List the most common compatible versions of apps that are compatible " +
"with the patches in the supplied patch bundles.",
"with the patches in the supplied patch bundles.",
],
)
internal class ListCompatibleVersions : Runnable {
Expand Down Expand Up @@ -58,8 +58,7 @@ internal class ListCompatibleVersions : Runnable {

val patches = loadPatchesFromJar(patchBundles)

PatchUtils.getMostCommonCompatibleVersions(
patches,
patches.mostCommonCompatibleVersions(
packageNames,
countUnusedPatches,
).entries.joinToString("\n", transform = ::buildString).let(logger::info)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,11 @@ internal object ListPatchesCommand : Runnable {
} ?: append("Key: $key")

values?.let { values ->
appendLine("\nValid values:")
appendLine("\nPossible values:")
append(values.map { "${it.value} (${it.key})" }.joinToString("\n").prependIndent("\t"))
}

append("\nType: $type")
}

fun IndexedValue<Patch<*>>.buildString() =
Expand Down
1 change: 0 additions & 1 deletion src/main/kotlin/app/revanced/cli/command/MainCommand.kt
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ private object CLIVersionProvider : IVersionProvider {
versionProvider = CLIVersionProvider::class,
subcommands = [
PatchCommand::class,
OptionsCommand::class,
ListPatchesCommand::class,
ListCompatibleVersions::class,
UtilityCommand::class,
Expand Down
62 changes: 0 additions & 62 deletions src/main/kotlin/app/revanced/cli/command/OptionsCommand.kt
Original file line number Diff line number Diff line change
@@ -1,62 +0,0 @@
package app.revanced.cli.command

import app.revanced.library.Options
import app.revanced.library.Options.setOptions
import app.revanced.patcher.patch.loadPatchesFromJar
import picocli.CommandLine
import picocli.CommandLine.Help.Visibility.ALWAYS
import java.io.File
import java.util.logging.Logger

@CommandLine.Command(
name = "options",
description = ["Generate options file from patches."],
)
internal object OptionsCommand : Runnable {
private val logger = Logger.getLogger(OptionsCommand::class.java.name)

@CommandLine.Parameters(
description = ["Paths to patch bundles."],
arity = "1..*",
)
private lateinit var patchBundles: Set<File>

@CommandLine.Option(
names = ["-p", "--path"],
description = ["Path to patch options JSON file."],
showDefaultValue = ALWAYS,
)
private var filePath: File = File("options.json")

@CommandLine.Option(
names = ["-o", "--overwrite"],
description = ["Overwrite existing options file."],
showDefaultValue = ALWAYS,
)
private var overwrite: Boolean = false

@CommandLine.Option(
names = ["-u", "--update"],
description = ["Update existing options by adding missing and removing non-existent options."],
showDefaultValue = ALWAYS,
)
private var update: Boolean = false

override fun run() =
try {
loadPatchesFromJar(patchBundles).let { patches ->
val exists = filePath.exists()
if (!exists || overwrite) {
if (exists && update) patches.setOptions(filePath)

Options.serialize(patches, prettyPrint = true).let(filePath::writeText)
} else {
throw OptionsFileAlreadyExistsException()
}
}
} catch (ex: OptionsFileAlreadyExistsException) {
logger.severe("Options file already exists, use --overwrite to override it")
}

class OptionsFileAlreadyExistsException : Exception()
}
Loading

0 comments on commit 3cd6dbe

Please sign in to comment.