From fcdaf324fe1c40ffa8fa51f1438a0515412e0611 Mon Sep 17 00:00:00 2001 From: oSumAtrIX Date: Sun, 23 Nov 2025 16:02:26 +0100 Subject: [PATCH] fix: Do not create a new matcher instance if provided with one --- src/main/kotlin/app/revanced/patcher/Fingerprint.kt | 4 ++-- .../app/revanced/patcher/InstructionFilter.kt | 2 +- src/main/kotlin/app/revanced/patcher/Matching.kt | 13 +++++++------ src/main/kotlin/app/revanced/patcher/Patcher.kt | 2 +- 4 files changed, 11 insertions(+), 10 deletions(-) diff --git a/src/main/kotlin/app/revanced/patcher/Fingerprint.kt b/src/main/kotlin/app/revanced/patcher/Fingerprint.kt index 35af339..f87183b 100644 --- a/src/main/kotlin/app/revanced/patcher/Fingerprint.kt +++ b/src/main/kotlin/app/revanced/patcher/Fingerprint.kt @@ -164,7 +164,7 @@ class Fingerprint internal constructor( is StringFilter -> { val string = instruction.stringReference?.string ?: return false - val filterString = string() + val filterString = stringValue when (comparison) { StringComparisonType.EQUALS -> string == filterString StringComparisonType.CONTAINS -> string.contains(filterString) @@ -212,7 +212,7 @@ class Fingerprint internal constructor( it.filters.filterIsInstance() }) ) - }.map { it.string() } + (strings ?: emptyList()) + }.map { it.stringValue } + (strings ?: emptyList()) val method = if (allStrings.isNotEmpty()) context.firstMethodOrNull(strings = allStrings.toTypedArray()) { match() } ?: diff --git a/src/main/kotlin/app/revanced/patcher/InstructionFilter.kt b/src/main/kotlin/app/revanced/patcher/InstructionFilter.kt index 4545b95..99ce4a8 100644 --- a/src/main/kotlin/app/revanced/patcher/InstructionFilter.kt +++ b/src/main/kotlin/app/revanced/patcher/InstructionFilter.kt @@ -844,7 +844,7 @@ class StringFilter internal constructor( /** * Store the lambda value instead of calling it more than once. */ - private val stringValue: String by lazy (string) + internal val stringValue: String by lazy (string) override fun matches( enclosingMethod: Method, diff --git a/src/main/kotlin/app/revanced/patcher/Matching.kt b/src/main/kotlin/app/revanced/patcher/Matching.kt index 02dffb8..68b5e60 100644 --- a/src/main/kotlin/app/revanced/patcher/Matching.kt +++ b/src/main/kotlin/app/revanced/patcher/Matching.kt @@ -229,7 +229,7 @@ fun Iterable.matchIndexed(build: IndexedMatcher.() -> Unit) = context(_: MatchContext) operator fun IndexedMatcher.invoke(iterable: Iterable, key: String, builder: IndexedMatcher.() -> Unit) = - remember(key) { IndexedMatcher().apply(builder) }(iterable) + remember(key) { apply(builder) }(iterable) context(_: MatchContext) fun Iterable.matchIndexed(key: String, build: IndexedMatcher.() -> Unit) = @@ -241,12 +241,11 @@ abstract class Matcher : MutableList by mutableListOf() { abstract operator fun invoke(haystack: Iterable): Boolean - class MatchContext internal constructor() : MutableMap by mutableMapOf() { - } + class MatchContext internal constructor() : MutableMap by mutableMapOf() } context(context: MatchContext) -inline fun remember(key: String, defaultValue: () -> V) = +inline fun remember(key: Any, defaultValue: () -> V) = context[key] as? V ?: defaultValue().also { context[key] = it } class IndexedMatcher() : Matcher Boolean>() { @@ -257,6 +256,7 @@ class IndexedMatcher() : Matcher Boolean>() { private var currentIndex = -1 override fun invoke(haystack: Iterable): Boolean { + // Defensive, in case haystack is not a list. val hayList = haystack as? List ?: haystack.toList() _indices.clear() @@ -278,7 +278,7 @@ class IndexedMatcher() : Matcher Boolean>() { currentIndex = subIndex val element = hayList[subIndex] if (this[predicateIndex](element)) { - tempIndices.add(subIndex) + tempIndices += subIndex lastMatchedIndex = subIndex predicateMatched = true subIndex++ @@ -296,7 +296,7 @@ class IndexedMatcher() : Matcher Boolean>() { } if (matchedAll) { - _indices.addAll(tempIndices) + _indices += tempIndices return true } } @@ -304,6 +304,7 @@ class IndexedMatcher() : Matcher Boolean>() { return false } + fun first(predicate: T.() -> Boolean) = add { if (lastMatchedIndex != -1) false else predicate() diff --git a/src/main/kotlin/app/revanced/patcher/Patcher.kt b/src/main/kotlin/app/revanced/patcher/Patcher.kt index e7d4915..1247cb9 100644 --- a/src/main/kotlin/app/revanced/patcher/Patcher.kt +++ b/src/main/kotlin/app/revanced/patcher/Patcher.kt @@ -69,7 +69,7 @@ class Patcher(private val config: PatcherConfig) : Closeable { // Recursively execute all dependency patches. dependencies.forEach { dependency -> - dependency.execute(executedPatches).exception?.let { + dependency.execute(executedPatches).exception?.let{ return PatchResult( this, PatchException(