You've already forked revanced-patcher
mirror of
https://github.com/revanced/revanced-patcher
synced 2025-09-10 05:30:49 +02:00
Compare commits
7 Commits
Author | SHA1 | Date | |
---|---|---|---|
![]() |
5eb8b428b9 | ||
![]() |
3a118d9b9d | ||
![]() |
14a73bfcaf | ||
![]() |
567bf52e16 | ||
![]() |
35c6489dba | ||
![]() |
371f0c4d0b | ||
![]() |
1b42f65d95 |
14
CHANGELOG.md
14
CHANGELOG.md
@@ -1,3 +1,17 @@
|
|||||||
|
## [3.2.1](https://github.com/revanced/revanced-patcher/compare/v3.2.0...v3.2.1) (2022-08-02)
|
||||||
|
|
||||||
|
|
||||||
|
### Bug Fixes
|
||||||
|
|
||||||
|
* check if patch option requirement is met ([14a73bf](https://github.com/revanced/revanced-patcher/commit/14a73bfcafac36bce2b8466788d460edde7a14fd))
|
||||||
|
|
||||||
|
# [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)
|
# [3.1.0](https://github.com/revanced/revanced-patcher/compare/v3.0.0...v3.1.0) (2022-08-02)
|
||||||
|
|
||||||
|
|
||||||
|
@@ -1,2 +1,2 @@
|
|||||||
kotlin.code.style = official
|
kotlin.code.style = official
|
||||||
version = 3.1.0
|
version = 3.2.1
|
||||||
|
@@ -1,13 +1,17 @@
|
|||||||
|
@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")
|
||||||
|
class RequirementNotMetException : Exception("null was passed into an option that requires a value")
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 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 +35,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()
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -60,8 +73,11 @@ sealed class PatchOption<T>(
|
|||||||
) {
|
) {
|
||||||
var value: T? = default
|
var value: T? = default
|
||||||
set(value) {
|
set(value) {
|
||||||
|
if (value == null && required) {
|
||||||
|
throw RequirementNotMetException()
|
||||||
|
}
|
||||||
if (!validator(value)) {
|
if (!validator(value)) {
|
||||||
throw IllegalArgumentException("Illegal value: $value")
|
throw IllegalValueException(value)
|
||||||
}
|
}
|
||||||
field = value
|
field = value
|
||||||
}
|
}
|
||||||
|
@@ -36,6 +36,15 @@ internal class PatchOptionsTest {
|
|||||||
println(options["key1"].value)
|
println(options["key1"].value)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun `should be able to set value to null`() {
|
||||||
|
// Sadly, doing:
|
||||||
|
// > options["key2"] = null
|
||||||
|
// is not possible because Kotlin
|
||||||
|
// cannot reify the type "Nothing?".
|
||||||
|
options.nullify("key2")
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
fun `should fail because the option does not exist`() {
|
fun `should fail because the option does not exist`() {
|
||||||
assertThrows<NoSuchOptionException> {
|
assertThrows<NoSuchOptionException> {
|
||||||
@@ -45,15 +54,22 @@ internal class PatchOptionsTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
fun `should fail because of invalid value type`() {
|
fun `should fail because of invalid value type`() {
|
||||||
assertThrows<IllegalArgumentException> {
|
assertThrows<InvalidTypeException> {
|
||||||
options["key1"] = 123
|
options["key1"] = 123
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
fun `should fail because of an illegal value`() {
|
fun `should fail because of an illegal value`() {
|
||||||
assertThrows<IllegalArgumentException> {
|
assertThrows<IllegalValueException> {
|
||||||
options["key3"] = "this value is not an allowed option"
|
options["key3"] = "this value is not an allowed option"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun `should fail because of the requirement is not met`() {
|
||||||
|
assertThrows<RequirementNotMetException> {
|
||||||
|
options.nullify("key1")
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
Reference in New Issue
Block a user