From e6de90d300bc9c82ca1696cb898db04c65a1cd5b Mon Sep 17 00:00:00 2001 From: oSumAtrIX Date: Sun, 22 Oct 2023 01:03:07 +0200 Subject: [PATCH] feat: Add function to reset options to their default value --- api/revanced-patcher.api | 1 + .../patcher/patch/options/PatchOption.kt | 20 ++++++++++++++----- 2 files changed, 16 insertions(+), 5 deletions(-) diff --git a/api/revanced-patcher.api b/api/revanced-patcher.api index bd4b222..dccb89d 100644 --- a/api/revanced-patcher.api +++ b/api/revanced-patcher.api @@ -370,6 +370,7 @@ public abstract class app/revanced/patcher/patch/options/PatchOption { public final fun getValidator ()Lkotlin/jvm/functions/Function1; public final fun getValue ()Ljava/lang/Object; public final fun getValue (Ljava/lang/Object;Lkotlin/reflect/KProperty;)Ljava/lang/Object; + public fun reset ()V public final fun setValue (Ljava/lang/Object;)V public final fun setValue (Ljava/lang/Object;Lkotlin/reflect/KProperty;Ljava/lang/Object;)V } diff --git a/src/main/kotlin/app/revanced/patcher/patch/options/PatchOption.kt b/src/main/kotlin/app/revanced/patcher/patch/options/PatchOption.kt index e737d52..62d05ff 100644 --- a/src/main/kotlin/app/revanced/patcher/patch/options/PatchOption.kt +++ b/src/main/kotlin/app/revanced/patcher/patch/options/PatchOption.kt @@ -24,7 +24,7 @@ abstract class PatchOption( /** * The value of the [PatchOption]. */ - var value: T? = default + var value: T? /** * Set the value of the [PatchOption]. * @@ -37,7 +37,7 @@ abstract class PatchOption( assertRequiredButNotNull(value) assertValid(value) - field = value + uncheckedValue = value } /** * Get the value of the [PatchOption]. @@ -48,12 +48,22 @@ abstract class PatchOption( * @throws PatchOptionException.ValueValidationException If the value is invalid. */ get() { - assertRequiredButNotNull(field) - assertValid(field) + assertRequiredButNotNull(uncheckedValue) + assertValid(uncheckedValue) - return field + return uncheckedValue } + // The unchecked value is used to allow setting the value without validation. + private var uncheckedValue = default + + /** + * Reset the [PatchOption] to its default value. + */ + open fun reset() { + uncheckedValue = default + } + private fun assertRequiredButNotNull(value: T?) { if (required && value == null) throw PatchOptionException.ValueRequiredException(this) }