diff --git a/patcher/src/commonMain/kotlin/app/revanced/patcher/Matching.kt b/patcher/src/commonMain/kotlin/app/revanced/patcher/Matching.kt index 5aa1b0e..44fcc98 100644 --- a/patcher/src/commonMain/kotlin/app/revanced/patcher/Matching.kt +++ b/patcher/src/commonMain/kotlin/app/revanced/patcher/Matching.kt @@ -59,10 +59,13 @@ fun Iterable.anyInstruction(predicate: Predicate) = an typealias ClassDefPredicate = context(PredicateContext) ClassDef.() -> Boolean + typealias MethodPredicate = context(PredicateContext) Method.() -> Boolean + typealias BytecodePatchContextMethodPredicate = context(BytecodePatchContext, PredicateContext) Method.() -> Boolean + typealias BytecodePatchContextClassDefPredicate = context(BytecodePatchContext, PredicateContext) ClassDef.() -> Boolean @@ -93,6 +96,7 @@ class MutablePredicateList internal constructor() : MutableList> typealias DeclarativePredicate = context(PredicateContext) MutablePredicateList.() -> Unit + typealias BytecodePatchContextDeclarativePredicate = context(BytecodePatchContext, PredicateContext) MutablePredicateList.() -> Unit @@ -217,15 +221,8 @@ context(_: BytecodePatchContext) fun Iterable.firstMutableMethod(methodReference: MethodReference) = requireNotNull(firstMutableMethodOrNull(methodReference)) @JvmName("firstMethodOrNullInClassDefs") -fun Iterable.firstMethodOrNull(predicate: MethodPredicate = { true }): Method? { - forEach { classDef -> - with(classDef) { - classDef.methods.firstMethodOrNull { predicate() }?.let { return it } - } - } - - return null -} +fun Iterable.firstMethodOrNull(predicate: MethodPredicate = { true }) = + asSequence().flatMap { it.methods.asSequence() }.asIterable().firstMethodOrNull(strings = emptyArray(), predicate) @JvmName("firstMethodInClassDefs") fun Iterable.firstMethod(predicate: MethodPredicate = { true }) = requireNotNull(firstMethodOrNull(predicate)) @@ -234,15 +231,7 @@ fun Iterable.firstMethod(predicate: MethodPredicate = { true }) = requ fun Iterable.firstMethodOrNull( vararg strings: String, predicate: MethodPredicate = { true }, -): Method? { - forEach { classDef -> - with(classDef) { - classDef.methods.firstMethodOrNull(strings = strings) { predicate() }?.let { return it } - } - } - - return null -} +) = asSequence().flatMap { it.methods.asSequence() }.asIterable().firstMethodOrNull(strings = strings, predicate) @JvmName("firstMethodInClassDefs") fun Iterable.firstMethod(