mirror of
https://github.com/ReVanced/revanced-patcher.git
synced 2026-01-23 11:11:04 +00:00
Compare commits
8 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
567bf52e16 | ||
|
|
35c6489dba | ||
|
|
371f0c4d0b | ||
|
|
1b42f65d95 | ||
|
|
2aee0cbd0f | ||
|
|
19256b5437 | ||
|
|
67a5237541 | ||
|
|
4e2e772389 |
14
CHANGELOG.md
14
CHANGELOG.md
@@ -1,3 +1,17 @@
|
|||||||
|
# [3.2.0](https://github.com/revanced/revanced-patcher/compare/v3.1.0...v3.2.0) (2022-08-02)
|
||||||
|
|
||||||
|
|
||||||
|
### Features
|
||||||
|
|
||||||
|
* PatchOptions#nullify to nullify an option ([371f0c4](https://github.com/revanced/revanced-patcher/commit/371f0c4d0bf96e7f6db35085efccaed3000a096c))
|
||||||
|
|
||||||
|
# [3.1.0](https://github.com/revanced/revanced-patcher/compare/v3.0.0...v3.1.0) (2022-08-02)
|
||||||
|
|
||||||
|
|
||||||
|
### Features
|
||||||
|
|
||||||
|
* validator for patch options ([4e2e772](https://github.com/revanced/revanced-patcher/commit/4e2e77238957d7732326cfe5e05145bf7dab5bfb))
|
||||||
|
|
||||||
# [3.0.0](https://github.com/revanced/revanced-patcher/compare/v2.9.0...v3.0.0) (2022-08-02)
|
# [3.0.0](https://github.com/revanced/revanced-patcher/compare/v2.9.0...v3.0.0) (2022-08-02)
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -1,2 +1,2 @@
|
|||||||
kotlin.code.style = official
|
kotlin.code.style = official
|
||||||
version = 3.0.0
|
version = 3.2.0
|
||||||
|
|||||||
@@ -1,13 +1,16 @@
|
|||||||
|
@file:Suppress("CanBeParameter", "MemberVisibilityCanBePrivate")
|
||||||
|
|
||||||
package app.revanced.patcher.patch
|
package app.revanced.patcher.patch
|
||||||
|
|
||||||
@Suppress("CanBeParameter", "MemberVisibilityCanBePrivate")
|
|
||||||
class NoSuchOptionException(val option: String) : Exception("No such option: $option")
|
class NoSuchOptionException(val option: String) : Exception("No such option: $option")
|
||||||
|
class IllegalValueException(val value: Any?) : Exception("Illegal value: $value")
|
||||||
|
class InvalidTypeException(val got: String, val expected: String) :
|
||||||
|
Exception("Invalid option value type: $got, expected $expected")
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A registry for an array of [PatchOption]s.
|
* A registry for an array of [PatchOption]s.
|
||||||
* @param options An array of [PatchOption]s.
|
* @param options An array of [PatchOption]s.
|
||||||
*/
|
*/
|
||||||
@Suppress("MemberVisibilityCanBePrivate")
|
|
||||||
class PatchOptions(vararg val options: PatchOption<*>) : Iterable<PatchOption<*>> {
|
class PatchOptions(vararg val options: PatchOption<*>) : Iterable<PatchOption<*>> {
|
||||||
private val register = buildMap {
|
private val register = buildMap {
|
||||||
for (option in options) {
|
for (option in options) {
|
||||||
@@ -31,13 +34,22 @@ class PatchOptions(vararg val options: PatchOption<*>) : Iterable<PatchOption<*>
|
|||||||
* Please note that using the wrong value type results in a runtime error.
|
* Please note that using the wrong value type results in a runtime error.
|
||||||
*/
|
*/
|
||||||
inline operator fun <reified T> set(key: String, value: T) {
|
inline operator fun <reified T> set(key: String, value: T) {
|
||||||
@Suppress("UNCHECKED_CAST") val opt = get(key) as? PatchOption<T>
|
@Suppress("UNCHECKED_CAST") val opt = get(key) as PatchOption<T>
|
||||||
if (opt == null || opt.value !is T) throw IllegalArgumentException(
|
if (opt.value !is T) throw InvalidTypeException(
|
||||||
"The type of the option value is not the same as the type value provided"
|
T::class.java.canonicalName,
|
||||||
|
opt.value?.let { it::class.java.canonicalName } ?: "null"
|
||||||
)
|
)
|
||||||
opt.value = value
|
opt.value = value
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sets the value of a [PatchOption] to `null`.
|
||||||
|
* @param key The key of the [PatchOption].
|
||||||
|
*/
|
||||||
|
fun nullify(key: String) {
|
||||||
|
get(key).value = null
|
||||||
|
}
|
||||||
|
|
||||||
override fun iterator() = options.iterator()
|
override fun iterator() = options.iterator()
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -55,9 +67,16 @@ sealed class PatchOption<T>(
|
|||||||
default: T?,
|
default: T?,
|
||||||
val title: String,
|
val title: String,
|
||||||
val description: String,
|
val description: String,
|
||||||
val required: Boolean
|
val required: Boolean,
|
||||||
|
val validator: (T?) -> Boolean
|
||||||
) {
|
) {
|
||||||
var value: T? = default
|
var value: T? = default
|
||||||
|
set(value) {
|
||||||
|
if (!validator(value)) {
|
||||||
|
throw IllegalValueException(value)
|
||||||
|
}
|
||||||
|
field = value
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A [PatchOption] representing a [String].
|
* A [PatchOption] representing a [String].
|
||||||
@@ -68,9 +87,10 @@ sealed class PatchOption<T>(
|
|||||||
default: String?,
|
default: String?,
|
||||||
title: String,
|
title: String,
|
||||||
description: String,
|
description: String,
|
||||||
required: Boolean = false
|
required: Boolean = false,
|
||||||
|
validator: (String?) -> Boolean = { true }
|
||||||
) : PatchOption<String>(
|
) : PatchOption<String>(
|
||||||
key, default, title, description, required
|
key, default, title, description, required, validator
|
||||||
)
|
)
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -82,9 +102,10 @@ sealed class PatchOption<T>(
|
|||||||
default: Boolean?,
|
default: Boolean?,
|
||||||
title: String,
|
title: String,
|
||||||
description: String,
|
description: String,
|
||||||
required: Boolean = false
|
required: Boolean = false,
|
||||||
|
validator: (Boolean?) -> Boolean = { true }
|
||||||
) : PatchOption<Boolean>(
|
) : PatchOption<Boolean>(
|
||||||
key, default, title, description, required
|
key, default, title, description, required, validator
|
||||||
)
|
)
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -98,9 +119,12 @@ sealed class PatchOption<T>(
|
|||||||
val options: Iterable<E>,
|
val options: Iterable<E>,
|
||||||
title: String,
|
title: String,
|
||||||
description: String,
|
description: String,
|
||||||
required: Boolean = false
|
required: Boolean = false,
|
||||||
|
validator: (E?) -> Boolean = { true }
|
||||||
) : PatchOption<E>(
|
) : PatchOption<E>(
|
||||||
key, default, title, description, required
|
key, default, title, description, required, {
|
||||||
|
(it?.let { it in options } ?: true) && validator(it)
|
||||||
|
}
|
||||||
) {
|
) {
|
||||||
init {
|
init {
|
||||||
if (default !in options) {
|
if (default !in options) {
|
||||||
@@ -119,9 +143,10 @@ sealed class PatchOption<T>(
|
|||||||
options: Iterable<String>,
|
options: Iterable<String>,
|
||||||
title: String,
|
title: String,
|
||||||
description: String,
|
description: String,
|
||||||
required: Boolean = false
|
required: Boolean = false,
|
||||||
|
validator: (String?) -> Boolean = { true }
|
||||||
) : ListOption<String>(
|
) : ListOption<String>(
|
||||||
key, default, options, title, description, required
|
key, default, options, title, description, required, validator
|
||||||
)
|
)
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -134,8 +159,9 @@ sealed class PatchOption<T>(
|
|||||||
options: Iterable<Int>,
|
options: Iterable<Int>,
|
||||||
title: String,
|
title: String,
|
||||||
description: String,
|
description: String,
|
||||||
required: Boolean = false
|
required: Boolean = false,
|
||||||
|
validator: (Int?) -> Boolean = { true }
|
||||||
) : ListOption<Int>(
|
) : ListOption<Int>(
|
||||||
key, default, options, title, description, required
|
key, default, options, title, description, required, validator
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,68 @@
|
|||||||
|
package app.revanced.patcher.patch
|
||||||
|
|
||||||
|
import app.revanced.patcher.usage.bytecode.ExampleBytecodePatch
|
||||||
|
import org.junit.jupiter.api.Test
|
||||||
|
import org.junit.jupiter.api.assertThrows
|
||||||
|
|
||||||
|
internal class PatchOptionsTest {
|
||||||
|
private val options = ExampleBytecodePatch().options
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun `should not throw an exception`() {
|
||||||
|
for (option in options) {
|
||||||
|
when (option) {
|
||||||
|
is PatchOption.StringOption -> {
|
||||||
|
option.value = "Hello World"
|
||||||
|
}
|
||||||
|
is PatchOption.BooleanOption -> {
|
||||||
|
option.value = false
|
||||||
|
}
|
||||||
|
is PatchOption.StringListOption -> {
|
||||||
|
option.value = option.options.first()
|
||||||
|
for (choice in option.options) {
|
||||||
|
println(choice)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
is PatchOption.IntListOption -> {
|
||||||
|
option.value = option.options.first()
|
||||||
|
for (choice in option.options) {
|
||||||
|
println(choice)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
println(options["key1"].value)
|
||||||
|
options["key1"] = "Hello, world!"
|
||||||
|
println(options["key1"].value)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun `should be able to set value to null`() {
|
||||||
|
// Sadly, doing:
|
||||||
|
// > options["key1"] = null
|
||||||
|
// is not possible because Kotlin
|
||||||
|
// cannot reify the type "Nothing?".
|
||||||
|
options.nullify("key1")
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun `should fail because the option does not exist`() {
|
||||||
|
assertThrows<NoSuchOptionException> {
|
||||||
|
options["this option does not exist"] = 123
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun `should fail because of invalid value type`() {
|
||||||
|
assertThrows<InvalidTypeException> {
|
||||||
|
options["key1"] = 123
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun `should fail because of an illegal value`() {
|
||||||
|
assertThrows<IllegalValueException> {
|
||||||
|
options["key3"] = "this value is not an allowed option"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,37 +0,0 @@
|
|||||||
package app.revanced.patcher.usage
|
|
||||||
|
|
||||||
import app.revanced.patcher.patch.PatchOption
|
|
||||||
import app.revanced.patcher.usage.bytecode.ExampleBytecodePatch
|
|
||||||
import kotlin.test.Test
|
|
||||||
|
|
||||||
internal class PatchOptionsUsage {
|
|
||||||
@Test
|
|
||||||
fun patchOptionsUsage() {
|
|
||||||
val options = ExampleBytecodePatch().options
|
|
||||||
for (option in options) {
|
|
||||||
when (option) {
|
|
||||||
is PatchOption.StringOption -> {
|
|
||||||
option.value = "Hello World"
|
|
||||||
}
|
|
||||||
is PatchOption.BooleanOption -> {
|
|
||||||
option.value = false
|
|
||||||
}
|
|
||||||
is PatchOption.StringListOption -> {
|
|
||||||
option.value = option.options.first()
|
|
||||||
for (choice in option.options) {
|
|
||||||
println(choice)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
is PatchOption.IntListOption -> {
|
|
||||||
option.value = option.options.first()
|
|
||||||
for (choice in option.options) {
|
|
||||||
println(choice)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
println(options["key1"].value)
|
|
||||||
options["key1"] = "Hello, world!"
|
|
||||||
println(options["key1"].value)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user