feat: add PathOption back

Now backed by a String.
This commit is contained in:
Sculas 2022-09-09 16:23:21 +02:00
parent 456db7289a
commit 172655bde0
No known key found for this signature in database
GPG Key ID: 1530BFF96D1EEB89
3 changed files with 30 additions and 0 deletions

View File

@ -2,6 +2,8 @@
package app.revanced.patcher.patch package app.revanced.patcher.patch
import java.nio.file.Path
import kotlin.io.path.pathString
import kotlin.reflect.KProperty import kotlin.reflect.KProperty
class NoSuchOptionException(val option: String) : Exception("No such option: $option") class NoSuchOptionException(val option: String) : Exception("No such option: $option")
@ -227,4 +229,20 @@ sealed class PatchOption<T>(
) : ListOption<Int>( ) : ListOption<Int>(
key, default, options, title, description, required, validator 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<String>(
key, default?.pathString, title, description, required, validator
)
} }

View File

@ -3,6 +3,8 @@ package app.revanced.patcher.patch
import app.revanced.patcher.usage.bytecode.ExampleBytecodePatch import app.revanced.patcher.usage.bytecode.ExampleBytecodePatch
import org.junit.jupiter.api.Test import org.junit.jupiter.api.Test
import org.junit.jupiter.api.assertThrows import org.junit.jupiter.api.assertThrows
import kotlin.io.path.Path
import kotlin.io.path.pathString
import kotlin.test.assertNotEquals import kotlin.test.assertNotEquals
internal class PatchOptionsTest { internal class PatchOptionsTest {
@ -33,6 +35,10 @@ internal class PatchOptionsTest {
println(choice) println(choice)
} }
} }
is PatchOption.PathOption -> {
option.value = Path("test.txt").pathString
}
} }
} }
val option = options.get<String>("key1") val option = options.get<String>("key1")

View File

@ -32,6 +32,7 @@ import org.jf.dexlib2.immutable.reference.ImmutableFieldReference
import org.jf.dexlib2.immutable.reference.ImmutableStringReference import org.jf.dexlib2.immutable.reference.ImmutableStringReference
import org.jf.dexlib2.immutable.value.ImmutableFieldEncodedValue import org.jf.dexlib2.immutable.value.ImmutableFieldEncodedValue
import org.jf.dexlib2.util.Preconditions import org.jf.dexlib2.util.Preconditions
import kotlin.io.path.Path
@Patch @Patch
@Name("example-bytecode-patch") @Name("example-bytecode-patch")
@ -194,5 +195,10 @@ class ExampleBytecodePatch : BytecodePatch(listOf(ExampleFingerprint)) {
"key5", null, "title", "description", true "key5", null, "title", "description", true
) )
) )
private var key6 by option(
PatchOption.PathOption(
"key6", Path("test.txt"), "title", "description", true
)
)
} }
} }