diff --git a/patcher/src/commonMain/kotlin/app/revanced/patcher/Matching.kt b/patcher/src/commonMain/kotlin/app/revanced/patcher/Matching.kt index b3815f9..36cf6a6 100644 --- a/patcher/src/commonMain/kotlin/app/revanced/patcher/Matching.kt +++ b/patcher/src/commonMain/kotlin/app/revanced/patcher/Matching.kt @@ -131,7 +131,8 @@ fun Iterable.firstMutableMethodOrNull(methodReference: MethodReference) @JvmName("firstMutableMethodInMethods") context(_: BytecodePatchContext) -fun Iterable.firstMutableMethod(methodReference: MethodReference) = requireNotNull(firstMutableMethodOrNull(methodReference)) +fun Iterable.firstMutableMethod(methodReference: MethodReference) = + requireNotNull(firstMutableMethodOrNull(methodReference)) @JvmName("firstMethodOrNullInMethods") fun Iterable.firstMethodOrNull( @@ -209,7 +210,8 @@ fun Iterable.firstMethodOrNull(methodReference: MethodReference) = asSequence().flatMap { it.methods.asSequence() }.asIterable().firstMethodOrNull(methodReference) @JvmName("firstMethodInClassDefs") -fun Iterable.firstMethod(methodReference: MethodReference) = requireNotNull(firstMethodOrNull(methodReference)) +fun Iterable.firstMethod(methodReference: MethodReference) = + requireNotNull(firstMethodOrNull(methodReference)) @JvmName("firstMutableMethodOrNullInClassDefs") context(_: BytecodePatchContext) @@ -218,7 +220,8 @@ fun Iterable.firstMutableMethodOrNull(methodReference: MethodReference @JvmName("firstMutableMethodInClassDefs") context(_: BytecodePatchContext) -fun Iterable.firstMutableMethod(methodReference: MethodReference) = requireNotNull(firstMutableMethodOrNull(methodReference)) +fun Iterable.firstMutableMethod(methodReference: MethodReference) = + requireNotNull(firstMutableMethodOrNull(methodReference)) @JvmName("firstMethodOrNullInClassDefs") fun Iterable.firstMethodOrNull(predicate: MethodPredicate = { true }) = @@ -293,11 +296,13 @@ fun ClassDef.firstMethod(methodReference: MethodReference) = requireNotNull(firs @JvmName("firstMutableMethodOrNullInClassDef") context(_: BytecodePatchContext) -fun ClassDef.firstMutableMethodOrNull(methodReference: MethodReference) = methods.firstMutableMethodOrNull(methodReference) +fun ClassDef.firstMutableMethodOrNull(methodReference: MethodReference) = + methods.firstMutableMethodOrNull(methodReference) @JvmName("firstMutableMethodInClassDef") context(_: BytecodePatchContext) -fun ClassDef.firstMutableMethod(methodReference: MethodReference) = requireNotNull(firstMutableMethodOrNull(methodReference)) +fun ClassDef.firstMutableMethod(methodReference: MethodReference) = + requireNotNull(firstMutableMethodOrNull(methodReference)) @JvmName("firstMethodOrNullInClassDef") fun ClassDef.firstMethodOrNull( @@ -476,7 +481,8 @@ fun gettingFirstMethodOrNull(method: MethodReference) = cachedReadOnlyProperty { fun gettingFirstMethod(method: MethodReference) = cachedReadOnlyProperty { firstMethod(method) } -fun gettingFirstMutableMethodOrNull(method: MethodReference) = cachedReadOnlyProperty { firstMutableMethodOrNull(method) } +fun gettingFirstMutableMethodOrNull(method: MethodReference) = + cachedReadOnlyProperty { firstMutableMethodOrNull(method) } fun gettingFirstMutableMethod(method: MethodReference) = cachedReadOnlyProperty { firstMutableMethod(method) } @@ -953,9 +959,9 @@ fun MutablePredicateList.definingClass( fun MutablePredicateList.parameterTypes(vararg parameterTypePrefixes: String) = predicate { parameterTypes.size == parameterTypePrefixes.size && - parameterTypes - .zip(parameterTypePrefixes) - .all { (a, b) -> a.startsWith(b) } + parameterTypes + .zip(parameterTypePrefixes) + .all { (a, b) -> a.startsWith(b) } } fun MutablePredicateList.strings(build: Function>) { @@ -1033,7 +1039,8 @@ fun MutablePredicateList.opcodes(vararg opcodes: Opcode) = instructions inline fun `is`(crossinline predicate: Predicate = { true }): IndexedMatcherPredicate = { _, _, _ -> (this as? T)?.predicate() == true } -fun instruction(predicate: Predicate = { true }): IndexedMatcherPredicate = { _, _, _ -> predicate() } +fun instruction(predicate: Predicate = { true }): IndexedMatcherPredicate = + { _, _, _ -> predicate() } fun registers(predicate: Predicate = { true }): IndexedMatcherPredicate = { _, _, _ -> @@ -1077,7 +1084,8 @@ fun registers( }, ) = registers({ compare(registers) }) -fun literal(predicate: Predicate = { true }): IndexedMatcherPredicate = { _, _, _ -> wideLiteral?.predicate() == true } +fun literal(predicate: Predicate = { true }): IndexedMatcherPredicate = + { _, _, _ -> wideLiteral?.predicate() == true } fun literal( literal: Long, @@ -1104,7 +1112,8 @@ fun field( compare: String.(String) -> Boolean = String::equals, ) = field { this.name.compare(name) } -fun type(predicate: Predicate = { true }): IndexedMatcherPredicate = { _, _, _ -> type?.predicate() == true } +fun type(predicate: Predicate = { true }): IndexedMatcherPredicate = + { _, _, _ -> type?.predicate() == true } fun type( type: String, @@ -1149,13 +1158,13 @@ operator fun String.invoke(compare: String.(String) -> Boolean = String::equals) operator fun Opcode.invoke(): IndexedMatcherPredicate = { _, _, _ -> opcode == this@invoke } typealias BuildCompositeDeclarativePredicate = - context( + context( BytecodePatchContext, PredicateContext, IndexedMatcher, MutableList - ) - MutablePredicateList.() -> Unit + ) + MutablePredicateList.() -> Unit fun firstMethodComposite( vararg strings: String, @@ -1164,7 +1173,7 @@ fun firstMethodComposite( class MatchBuilder private constructor( private val strings: MutableList, - indexedMatcher: IndexedMatcher = indexedMatcher(), + private val indexedMatcher: IndexedMatcher = indexedMatcher(), build: BuildCompositeDeclarativePredicate = { }, ) { internal constructor( @@ -1176,7 +1185,12 @@ class MatchBuilder private constructor( context(strings, indexedMatcher) { build() } } - val indices = indexedMatcher.indices + context(_: BytecodePatchContext) + val indices: List + get() { + immutableMethod // Ensure matched. + return indexedMatcher.indices + } private val BytecodePatchContext.cachedImmutableMethodOrNull by gettingFirstMethodDeclarativelyOrNull( strings = strings.toTypedArray(), @@ -1231,9 +1245,9 @@ class MatchBuilder private constructor( } class Match( - val context: BytecodePatchContext, + private val context: BytecodePatchContext, val immutableMethodOrNull: Method?, - val indices: List, + private val _indices: List, ) { val immutableMethod by lazy { requireNotNull(immutableMethodOrNull) } @@ -1252,6 +1266,13 @@ class Match( } val classDef by lazy { requireNotNull(classDefOrNull) } + + context(_: BytecodePatchContext) + val indices: List + get() { + immutableMethod // Ensure matched. + return _indices + } } context(context: BytecodePatchContext)