diff --git a/src/main/kotlin/app/revanced/patcher/patch/PatchOption.kt b/src/main/kotlin/app/revanced/patcher/patch/PatchOption.kt index 0573dee..9636f64 100644 --- a/src/main/kotlin/app/revanced/patcher/patch/PatchOption.kt +++ b/src/main/kotlin/app/revanced/patcher/patch/PatchOption.kt @@ -2,6 +2,8 @@ package app.revanced.patcher.patch +import java.nio.file.Path +import kotlin.io.path.pathString import kotlin.reflect.KProperty class NoSuchOptionException(val option: String) : Exception("No such option: $option") @@ -227,4 +229,20 @@ sealed class PatchOption( ) : ListOption( key, default, options, title, description, required, validator ) + + /** + * A [PatchOption] representing a [Path], backed by a [String]. + * The validator passes a [String], if you need a [Path] you will have to convert it yourself. + * @see PatchOption + */ + class PathOption( + key: String, + default: Path?, + title: String, + description: String, + required: Boolean = false, + validator: (String?) -> Boolean = { true } + ) : PatchOption( + key, default?.pathString, title, description, required, validator + ) } diff --git a/src/test/kotlin/app/revanced/patcher/patch/PatchOptionsTest.kt b/src/test/kotlin/app/revanced/patcher/patch/PatchOptionsTest.kt index 125b59b..d83e9d8 100644 --- a/src/test/kotlin/app/revanced/patcher/patch/PatchOptionsTest.kt +++ b/src/test/kotlin/app/revanced/patcher/patch/PatchOptionsTest.kt @@ -3,6 +3,8 @@ package app.revanced.patcher.patch import app.revanced.patcher.usage.bytecode.ExampleBytecodePatch import org.junit.jupiter.api.Test import org.junit.jupiter.api.assertThrows +import kotlin.io.path.Path +import kotlin.io.path.pathString import kotlin.test.assertNotEquals internal class PatchOptionsTest { @@ -33,6 +35,10 @@ internal class PatchOptionsTest { println(choice) } } + + is PatchOption.PathOption -> { + option.value = Path("test.txt").pathString + } } } val option = options.get("key1") diff --git a/src/test/kotlin/app/revanced/patcher/usage/bytecode/ExampleBytecodePatch.kt b/src/test/kotlin/app/revanced/patcher/usage/bytecode/ExampleBytecodePatch.kt index 2b3dce8..587e3c3 100644 --- a/src/test/kotlin/app/revanced/patcher/usage/bytecode/ExampleBytecodePatch.kt +++ b/src/test/kotlin/app/revanced/patcher/usage/bytecode/ExampleBytecodePatch.kt @@ -32,6 +32,7 @@ import org.jf.dexlib2.immutable.reference.ImmutableFieldReference import org.jf.dexlib2.immutable.reference.ImmutableStringReference import org.jf.dexlib2.immutable.value.ImmutableFieldEncodedValue import org.jf.dexlib2.util.Preconditions +import kotlin.io.path.Path @Patch @Name("example-bytecode-patch") @@ -194,5 +195,10 @@ class ExampleBytecodePatch : BytecodePatch(listOf(ExampleFingerprint)) { "key5", null, "title", "description", true ) ) + private var key6 by option( + PatchOption.PathOption( + "key6", Path("test.txt"), "title", "description", true + ) + ) } }