mirror of
https://github.com/ReVanced/revanced-patcher.git
synced 2024-11-10 01:02:22 +01:00
feat: improved Patch Options
Removed a lot of the type mess. There's still some duplicated code PatchOption.kt, but I'm afraid there's nothing I can do about that. It's not a big deal anyway.
This commit is contained in:
parent
c348c1f0a0
commit
e722e3f4f9
4 changed files with 45 additions and 11 deletions
|
@ -27,7 +27,7 @@ abstract class OptionsContainer {
|
|||
@Suppress("MemberVisibilityCanBePrivate")
|
||||
val options = PatchOptions()
|
||||
|
||||
protected fun option(opt: PatchOption<*>): PatchOption<*> {
|
||||
protected fun <T> option(opt: PatchOption<T>): PatchOption<T> {
|
||||
options.register(opt)
|
||||
return opt
|
||||
}
|
||||
|
|
|
@ -33,8 +33,22 @@ class PatchOptions(vararg options: PatchOption<*>) : Iterable<PatchOption<*>> {
|
|||
* Get a [PatchOption] by its key.
|
||||
* @param key The key of the [PatchOption].
|
||||
*/
|
||||
@JvmName("getUntyped")
|
||||
operator fun get(key: String) = register[key] ?: throw NoSuchOptionException(key)
|
||||
|
||||
/**
|
||||
* Get a [PatchOption] by its key.
|
||||
* @param key The key of the [PatchOption].
|
||||
*/
|
||||
inline operator fun <reified T> get(key: String): PatchOption<T> {
|
||||
val opt = get(key)
|
||||
if (opt.value !is T) throw InvalidTypeException(
|
||||
opt.value?.let { it::class.java.canonicalName } ?: "null",
|
||||
T::class.java.canonicalName
|
||||
)
|
||||
return opt as PatchOption<T>
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the value of a [PatchOption].
|
||||
* @param key The key of the [PatchOption].
|
||||
|
@ -42,7 +56,7 @@ class PatchOptions(vararg options: PatchOption<*>) : Iterable<PatchOption<*>> {
|
|||
* Please note that using the wrong value type results in a runtime error.
|
||||
*/
|
||||
inline operator fun <reified T> set(key: String, value: T) {
|
||||
@Suppress("UNCHECKED_CAST") val opt = get(key) as PatchOption<T>
|
||||
val opt = get<T>(key)
|
||||
if (opt.value !is T) throw InvalidTypeException(
|
||||
T::class.java.canonicalName,
|
||||
opt.value?.let { it::class.java.canonicalName } ?: "null"
|
||||
|
@ -99,7 +113,8 @@ sealed class PatchOption<T>(
|
|||
* Gets the value of the option.
|
||||
* Please note that using the wrong value type results in a runtime error.
|
||||
*/
|
||||
inline operator fun <reified V> getValue(thisRef: Any?, property: KProperty<*>): V? {
|
||||
@JvmName("getValueTyped")
|
||||
inline operator fun <reified V> getValue(thisRef: Nothing?, property: KProperty<*>): V? {
|
||||
if (value !is V?) throw InvalidTypeException(
|
||||
V::class.java.canonicalName,
|
||||
value?.let { it::class.java.canonicalName } ?: "null"
|
||||
|
@ -107,11 +122,14 @@ sealed class PatchOption<T>(
|
|||
return value as? V?
|
||||
}
|
||||
|
||||
operator fun getValue(thisRef: Any?, property: KProperty<*>) = value
|
||||
|
||||
/**
|
||||
* Gets the value of the option.
|
||||
* Please note that using the wrong value type results in a runtime error.
|
||||
*/
|
||||
inline operator fun <reified V> setValue(thisRef: Any?, property: KProperty<*>, new: V) {
|
||||
@JvmName("setValueTyped")
|
||||
inline operator fun <reified V> setValue(thisRef: Nothing?, property: KProperty<*>, new: V) {
|
||||
if (value !is V) throw InvalidTypeException(
|
||||
V::class.java.canonicalName,
|
||||
value?.let { it::class.java.canonicalName } ?: "null"
|
||||
|
@ -119,6 +137,10 @@ sealed class PatchOption<T>(
|
|||
value = new as T
|
||||
}
|
||||
|
||||
operator fun setValue(thisRef: Any?, property: KProperty<*>, new: T?) {
|
||||
value = new
|
||||
}
|
||||
|
||||
/**
|
||||
* A [PatchOption] representing a [String].
|
||||
* @see PatchOption
|
||||
|
|
|
@ -35,7 +35,9 @@ internal class PatchOptionsTest {
|
|||
}
|
||||
}
|
||||
}
|
||||
val option = options["key1"]
|
||||
val option = options.get<String>("key1")
|
||||
// or: val option: String? by options["key1"]
|
||||
// then you won't need `.value` every time
|
||||
println(option.value)
|
||||
options["key1"] = "Hello, world!"
|
||||
println(option.value)
|
||||
|
@ -55,6 +57,9 @@ internal class PatchOptionsTest {
|
|||
// > options["key2"] = null
|
||||
// is not possible because Kotlin
|
||||
// cannot reify the type "Nothing?".
|
||||
// So we have to do this instead:
|
||||
options["key2"] = null as Any?
|
||||
// This is a cleaner replacement for the above:
|
||||
options.nullify("key2")
|
||||
}
|
||||
|
||||
|
@ -66,12 +71,19 @@ internal class PatchOptionsTest {
|
|||
}
|
||||
|
||||
@Test
|
||||
fun `should fail because of invalid value type`() {
|
||||
fun `should fail because of invalid value type when setting an option`() {
|
||||
assertThrows<InvalidTypeException> {
|
||||
options["key1"] = 123
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `should fail because of invalid value type when getting an option`() {
|
||||
assertThrows<InvalidTypeException> {
|
||||
options.get<Int>("key1")
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `should fail because of an illegal value`() {
|
||||
assertThrows<IllegalValueException> {
|
||||
|
|
|
@ -169,27 +169,27 @@ class ExampleBytecodePatch : BytecodePatch(listOf(ExampleFingerprint)) {
|
|||
}
|
||||
|
||||
companion object : OptionsContainer() {
|
||||
private var key1: String? by option(
|
||||
private var key1 by option(
|
||||
PatchOption.StringOption(
|
||||
"key1", "default", "title", "description", true
|
||||
)
|
||||
)
|
||||
private var key2: Boolean? by option(
|
||||
private var key2 by option(
|
||||
PatchOption.BooleanOption(
|
||||
"key2", true, "title", "description" // required defaults to false
|
||||
)
|
||||
)
|
||||
private var key3: String? by option(
|
||||
private var key3 by option(
|
||||
PatchOption.StringListOption(
|
||||
"key3", "TEST", listOf("TEST", "TEST1", "TEST2"), "title", "description"
|
||||
)
|
||||
)
|
||||
private var key4: Int? by option(
|
||||
private var key4 by option(
|
||||
PatchOption.IntListOption(
|
||||
"key4", 1, listOf(1, 2, 3), "title", "description"
|
||||
)
|
||||
)
|
||||
private var key5: String? by option(
|
||||
private var key5 by option(
|
||||
PatchOption.StringOption(
|
||||
"key5", null, "title", "description", true
|
||||
)
|
||||
|
|
Loading…
Reference in a new issue