1
mirror of https://github.com/revanced/revanced-patcher synced 2025-09-06 16:38:50 +02:00

Compare commits

...

15 Commits

Author SHA1 Message Date
semantic-release-bot
e0271790b8 chore(release): 4.4.2 [skip ci]
## [4.4.2](https://github.com/revanced/revanced-patcher/compare/v4.4.1...v4.4.2) (2022-09-18)

### Bug Fixes

* **fingerprint:** do not throw on `MethodFingerprint.result` getter ([2f7e62e](2f7e62ef65))

### Performance Improvements

* **fingerprint:** do not resolve already resolved fingerprints ([4bfd7eb](4bfd7ebff8))
2022-09-18 06:12:38 +00:00
oSumAtrIX
4bfd7ebff8 perf(fingerprint): do not resolve already resolved fingerprints
Signed-off-by: oSumAtrIX <johan.melkonyan1@web.de>
2022-09-18 07:35:08 +02:00
oSumAtrIX
2f7e62ef65 fix(fingerprint): do not throw on MethodFingerprint.result getter
Signed-off-by: oSumAtrIX <johan.melkonyan1@web.de>
2022-09-18 07:34:32 +02:00
semantic-release-bot
4485af8036 chore(release): 4.4.1 [skip ci]
## [4.4.1](https://github.com/revanced/revanced-patcher/compare/v4.4.0...v4.4.1) (2022-09-14)

### Bug Fixes

* compare any methods parameters ([#101](https://github.com/revanced/revanced-patcher/issues/101)) ([085a3a4](085a3a479d))
2022-09-14 16:36:34 +00:00
d4rkk3y
085a3a479d fix: compare any methods parameters (#101) 2022-09-14 18:34:58 +02:00
semantic-release-bot
f75c9a78b8 chore(release): 4.4.0 [skip ci]
# [4.4.0](https://github.com/revanced/revanced-patcher/compare/v4.3.0...v4.4.0) (2022-09-09)

### Features

* add PathOption back ([172655b](172655bde0))
2022-09-09 14:24:48 +00:00
Sculas
172655bde0 feat: add PathOption back
Now backed by a String.
2022-09-09 16:23:21 +02:00
semantic-release-bot
456db7289a chore(release): 4.3.0 [skip ci]
# [4.3.0](https://github.com/revanced/revanced-patcher/compare/v4.2.3...v4.3.0) (2022-09-09)

### Features

* improved Patch Options ([e722e3f](e722e3f4f9))
2022-09-09 14:11:58 +00:00
Sculas
e722e3f4f9 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.
2022-09-09 16:10:30 +02:00
Sculas
c348c1f0a0 refactor: remove PathOption and FileOption 2022-09-09 15:04:17 +02:00
semantic-release-bot
ed1851013e chore(release): 4.2.3 [skip ci]
## [4.2.3](https://github.com/revanced/revanced-patcher/compare/v4.2.2...v4.2.3) (2022-09-08)

### Bug Fixes

* wrong value for iterator in PatchOptions ([e31ac1f](e31ac1f132))
2022-09-08 15:39:04 +00:00
Sculas
e31ac1f132 fix: wrong value for iterator in PatchOptions 2022-09-08 17:37:31 +02:00
semantic-release-bot
8f78f85e4a chore(release): 4.2.2 [skip ci]
## [4.2.2](https://github.com/revanced/revanced-patcher/compare/v4.2.1...v4.2.2) (2022-09-08)

### Bug Fixes

* invalid type propagation in options ([b873228](b873228ef0)), closes [#98](https://github.com/revanced/revanced-patcher/issues/98)
2022-09-08 14:50:38 +00:00
Sculas
0be2677519 Merge remote-tracking branch 'origin/main' into main 2022-09-08 16:49:26 +02:00
Sculas
b873228ef0 fix: invalid type propagation in options
Fixes #98
2022-09-08 16:49:06 +02:00
10 changed files with 141 additions and 61 deletions

View File

@@ -1,3 +1,50 @@
## [4.4.2](https://github.com/revanced/revanced-patcher/compare/v4.4.1...v4.4.2) (2022-09-18)
### Bug Fixes
* **fingerprint:** do not throw on `MethodFingerprint.result` getter ([2f7e62e](https://github.com/revanced/revanced-patcher/commit/2f7e62ef65422f2c75ef8b09b9cd27076e172b30))
### Performance Improvements
* **fingerprint:** do not resolve already resolved fingerprints ([4bfd7eb](https://github.com/revanced/revanced-patcher/commit/4bfd7ebff8b6623b0da4a46d6048bed08c5070d4))
## [4.4.1](https://github.com/revanced/revanced-patcher/compare/v4.4.0...v4.4.1) (2022-09-14)
### Bug Fixes
* compare any methods parameters ([#101](https://github.com/revanced/revanced-patcher/issues/101)) ([085a3a4](https://github.com/revanced/revanced-patcher/commit/085a3a479d7bd411dcb0492b283daca538c824a1))
# [4.4.0](https://github.com/revanced/revanced-patcher/compare/v4.3.0...v4.4.0) (2022-09-09)
### Features
* add PathOption back ([172655b](https://github.com/revanced/revanced-patcher/commit/172655bde06efdb0955431b44d269e6a64fe317a))
# [4.3.0](https://github.com/revanced/revanced-patcher/compare/v4.2.3...v4.3.0) (2022-09-09)
### Features
* improved Patch Options ([e722e3f](https://github.com/revanced/revanced-patcher/commit/e722e3f4f9dc64acf53595802a0a83cf46ee96b8))
## [4.2.3](https://github.com/revanced/revanced-patcher/compare/v4.2.2...v4.2.3) (2022-09-08)
### Bug Fixes
* wrong value for iterator in PatchOptions ([e31ac1f](https://github.com/revanced/revanced-patcher/commit/e31ac1f132df56ba7d2f8446d289ae03ef28f67d))
## [4.2.2](https://github.com/revanced/revanced-patcher/compare/v4.2.1...v4.2.2) (2022-09-08)
### Bug Fixes
* invalid type propagation in options ([b873228](https://github.com/revanced/revanced-patcher/commit/b873228ef0a9e6e431a4278c979caa5fcc508e0d)), closes [#98](https://github.com/revanced/revanced-patcher/issues/98)
## [4.2.1](https://github.com/revanced/revanced-patcher/compare/v4.2.0...v4.2.1) (2022-09-08)

View File

@@ -1,2 +1,2 @@
kotlin.code.style = official
version = 4.2.1
version = 4.4.2

View File

@@ -16,7 +16,6 @@ import org.jf.dexlib2.iface.instruction.Instruction
import org.jf.dexlib2.iface.reference.MethodReference
import org.jf.dexlib2.immutable.ImmutableMethod
import org.jf.dexlib2.immutable.ImmutableMethodImplementation
import org.jf.dexlib2.util.MethodUtil
import java.io.OutputStream
infix fun AccessFlags.or(other: AccessFlags) = this.value or other.value
@@ -47,16 +46,14 @@ fun MutableMethodImplementation.removeInstructions(index: Int, count: Int) {
}
/**
* Compare a method to another, considering constructors and parameters.
* Compare a method to another, considering name and parameters.
* @param otherMethod The method to compare against.
* @return True if the methods match given the conditions.
*/
fun Method.softCompareTo(otherMethod: MethodReference): Boolean {
if (MethodUtil.isConstructor(this) && !parametersEqual(
this.parameterTypes, otherMethod.parameterTypes
)
) return false
return this.name == otherMethod.name
return this.name == otherMethod.name && parametersEqual(
this.parameterTypes, otherMethod.parameterTypes
)
}
/**

View File

@@ -1,8 +1,6 @@
package app.revanced.patcher.fingerprint.method.impl
import app.revanced.patcher.data.impl.BytecodeData
import app.revanced.patcher.data.impl.MethodNotFoundException
import app.revanced.patcher.extensions.MethodFingerprintExtensions.name
import app.revanced.patcher.extensions.softCompareTo
import app.revanced.patcher.fingerprint.Fingerprint
import app.revanced.patcher.fingerprint.method.utils.MethodFingerprintUtils
@@ -31,10 +29,8 @@ abstract class MethodFingerprint(
) : Fingerprint {
/**
* The result of the [MethodFingerprint] the [Method].
* @throws MethodNotFoundException If the resolution of the [Method] has not happened.
*/
var result: MethodFingerprintResult? = null
get() = field ?: throw Exception("${this.name} has not been resolved yet.")
}
/**

View File

@@ -50,11 +50,13 @@ object MethodFingerprintUtils {
* @param context The context on which to resolve the [MethodFingerprint].
* @param classDef The class of the matching [Method].
* @param forData The [BytecodeData] to host proxies.
* @return True if the resolution was successful, false otherwise.
* @return True if the resolution was successful or if the fingerprint is already resolved, false otherwise.
*/
fun MethodFingerprint.resolve(forData: BytecodeData, context: Method, classDef: ClassDef): Boolean {
val methodFingerprint = this
if (methodFingerprint.result != null) return true
if (methodFingerprint.returnType != null && !context.returnType.startsWith(methodFingerprint.returnType))
return false

View File

@@ -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
}

View File

@@ -2,8 +2,8 @@
package app.revanced.patcher.patch
import java.io.File
import java.nio.file.Path
import kotlin.io.path.pathString
import kotlin.reflect.KProperty
class NoSuchOptionException(val option: String) : Exception("No such option: $option")
@@ -17,7 +17,7 @@ object RequirementNotMetException : Exception("null was passed into an option th
* A registry for an array of [PatchOption]s.
* @param options An array of [PatchOption]s.
*/
class PatchOptions(vararg val options: PatchOption<*>) : Iterable<PatchOption<*>> {
class PatchOptions(vararg options: PatchOption<*>) : Iterable<PatchOption<*>> {
private val register = mutableMapOf<String, PatchOption<*>>()
init {
@@ -35,8 +35,22 @@ class PatchOptions(vararg val 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].
@@ -44,7 +58,7 @@ class PatchOptions(vararg val 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"
@@ -60,7 +74,7 @@ class PatchOptions(vararg val options: PatchOption<*>) : Iterable<PatchOption<*>
get(key).value = null
}
override fun iterator() = options.iterator()
override fun iterator() = register.values.iterator()
}
/**
@@ -101,17 +115,23 @@ 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<*>) =
value as? V ?: throw InvalidTypeException(
@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"
)
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 +139,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
@@ -207,34 +231,18 @@ sealed class PatchOption<T>(
)
/**
* A [PatchOption] representing a [Path].
* 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
*/
open class PathOption(
class PathOption(
key: String,
default: Path?,
title: String,
description: String,
required: Boolean = false,
validator: (Path?) -> Boolean = { true }
) : PatchOption<Path>(
key, default, title, description, required, validator
)
/**
* A [PathOption] of type [File].
* @see PathOption
*/
class FileOption(
key: String,
default: File?,
title: String,
description: String,
required: Boolean = false,
validator: (File?) -> Boolean = { true }
) : PathOption(
key, default?.toPath(), title, description, required, {
validator(it?.toFile())
}
validator: (String?) -> Boolean = { true }
) : PatchOption<String>(
key, default?.pathString, title, description, required, validator
)
}

View File

@@ -0,0 +1,18 @@
package app.revanced.patcher.issues
import app.revanced.patcher.patch.PatchOption
import org.junit.jupiter.api.Test
import kotlin.test.assertNull
internal class Issue98 {
companion object {
var key1: String? by PatchOption.StringOption(
"key1", null, "title", "description"
)
}
@Test
fun `should infer nullable type correctly`() {
assertNull(key1)
}
}

View File

@@ -3,7 +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 java.io.File
import kotlin.io.path.Path
import kotlin.io.path.pathString
import kotlin.test.assertNotEquals
internal class PatchOptionsTest {
@@ -36,11 +37,13 @@ internal class PatchOptionsTest {
}
is PatchOption.PathOption -> {
option.value = File("test.txt").toPath()
option.value = Path("test.txt").pathString
}
}
}
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)
@@ -48,7 +51,7 @@ internal class PatchOptionsTest {
@Test
fun `should return a different value when changed`() {
var value: String by options["key1"]
var value: String? by options["key1"]
val current = value + "" // force a copy
value = "Hello, world!"
assertNotEquals(current, value)
@@ -60,6 +63,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")
}
@@ -71,12 +77,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> {
@@ -94,7 +107,7 @@ internal class PatchOptionsTest {
@Test
fun `should fail because getting a non-initialized option is illegal`() {
assertThrows<RequirementNotMetException> {
println(options["key6"].value)
println(options["key5"].value)
}
}
}

View File

@@ -32,8 +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 java.io.File
import java.nio.file.Path
import kotlin.io.path.Path
@Patch
@Name("example-bytecode-patch")
@@ -171,34 +170,34 @@ 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: Path by option(
PatchOption.PathOption(
"key5", File("test.txt").toPath(), "title", "description"
private var key5 by option(
PatchOption.StringOption(
"key5", null, "title", "description", true
)
)
private var key6: String by option(
PatchOption.StringOption(
"key6", null, "title", "description", true
private var key6 by option(
PatchOption.PathOption(
"key6", Path("test.txt"), "title", "description", true
)
)
}