From 56fd65d6ce52c6e7a6178d2c731bdd0d3c5a9964 Mon Sep 17 00:00:00 2001 From: oSumAtrIX Date: Thu, 8 Jan 2026 00:51:56 +0100 Subject: [PATCH] formatting --- .../kotlin/app/revanced/patcher/Matching.kt | 262 +++++++----------- 1 file changed, 94 insertions(+), 168 deletions(-) diff --git a/patcher/src/commonMain/kotlin/app/revanced/patcher/Matching.kt b/patcher/src/commonMain/kotlin/app/revanced/patcher/Matching.kt index d813fef..e419f2a 100644 --- a/patcher/src/commonMain/kotlin/app/revanced/patcher/Matching.kt +++ b/patcher/src/commonMain/kotlin/app/revanced/patcher/Matching.kt @@ -226,20 +226,17 @@ object IterableClassDefMethodMatching { context(context: BytecodePatchContext) fun Iterable.firstMethodDeclarativelyOrNull( - vararg strings: String, - predicate: DeclarativePredicate + vararg strings: String, predicate: DeclarativePredicate ) = firstMethodOrNull(strings = strings) { rememberDeclarativePredicate(predicate) } context(context: BytecodePatchContext) fun Iterable.firstMethodDeclaratively( - vararg strings: String, - predicate: DeclarativePredicate + vararg strings: String, predicate: DeclarativePredicate ) = requireNotNull(firstMethodDeclarativelyOrNull(strings = strings, predicate)) context(context: BytecodePatchContext) fun Iterable.firstMutableMethodDeclarativelyOrNull( - vararg strings: String, - predicate: DeclarativePredicate + vararg strings: String, predicate: DeclarativePredicate ) = firstMutableMethodOrNull(strings = strings) { rememberDeclarativePredicate(predicate) } } @@ -299,48 +296,40 @@ object IterableClassDefClassDefMatching { ) = requireNotNull(firstClassDefOrNull(predicate)) fun Iterable.firstClassDefOrNull( - type: String? = null, - predicate: ClassDefPredicate = { true } + type: String? = null, predicate: ClassDefPredicate = { true } ) = if (type == null) firstClassDefOrNull(predicate) else withPredicateContext { firstOrNull { it.type == type && it.predicate() } } fun Iterable.firstClassDef( - type: String? = null, - predicate: ClassDefPredicate = { true } + type: String? = null, predicate: ClassDefPredicate = { true } ) = requireNotNull(firstClassDefOrNull(type, predicate)) context(context: BytecodePatchContext) fun Iterable.firstMutableClassDefOrNull( - type: String? = null, - predicate: ClassDefPredicate = { true } + type: String? = null, predicate: ClassDefPredicate = { true } ) = firstClassDefOrNull(type, predicate)?.let { context.classDefs.getOrReplaceMutable(it) } context(_: BytecodePatchContext) fun Iterable.firstMutableClassDef( - type: String? = null, - predicate: ClassDefPredicate = { true } + type: String? = null, predicate: ClassDefPredicate = { true } ) = requireNotNull(firstMutableClassDefOrNull(type, predicate)) fun Iterable.firstClassDefDeclarativelyOrNull( - type: String? = null, - predicate: DeclarativePredicate + type: String? = null, predicate: DeclarativePredicate ) = firstClassDefOrNull(type) { rememberDeclarativePredicate(predicate) } fun Iterable.firstClassDefDeclaratively( - type: String? = null, - predicate: DeclarativePredicate + type: String? = null, predicate: DeclarativePredicate ) = requireNotNull(firstClassDefDeclarativelyOrNull(type, predicate)) context(_: BytecodePatchContext) fun Iterable.firstMutableClassDefDeclarativelyOrNull( - type: String? = null, - predicate: DeclarativePredicate + type: String? = null, predicate: DeclarativePredicate ) = firstMutableClassDefOrNull(type) { rememberDeclarativePredicate(predicate) } context(_: BytecodePatchContext) fun Iterable.firstMutableClassDefDeclaratively( - type: String? = null, - predicate: DeclarativePredicate + type: String? = null, predicate: DeclarativePredicate ) = requireNotNull(firstMutableClassDefDeclarativelyOrNull(type, predicate)) } @@ -355,8 +344,12 @@ object BytecodePatchContextMethodMatching { fun BytecodePatchContext.firstMutableMethodOrNull( methodReference: MethodReference - ): MutableMethod? = firstMutableClassDefOrNull(methodReference.definingClass)?.methods - ?.first { MethodUtil.methodSignaturesMatch(methodReference, it) } + ): MutableMethod? = firstMutableClassDefOrNull(methodReference.definingClass)?.methods?.first { + MethodUtil.methodSignaturesMatch( + methodReference, + it + ) + } fun BytecodePatchContext.firstMutableMethod( method: MethodReference @@ -390,8 +383,7 @@ object BytecodePatchContextMethodMatching { } fun BytecodePatchContext.firstMutableMethod( - vararg strings: String, - predicate: MethodPredicate = { true } + vararg strings: String, predicate: MethodPredicate = { true } ) = requireNotNull(firstMutableMethodOrNull(strings = strings, predicate)) fun gettingFirstMethodOrNull( @@ -431,128 +423,104 @@ object BytecodePatchContextMethodMatching { ) = cachedReadOnlyProperty { firstMutableMethod(strings = strings, predicate) } fun BytecodePatchContext.firstMethodDeclarativelyOrNull( - vararg strings: String, - predicate: DeclarativePredicate = { } + vararg strings: String, predicate: DeclarativePredicate = { } ) = firstMethodOrNull(strings = strings) { rememberDeclarativePredicate(predicate) } fun BytecodePatchContext.firstMethodDeclaratively( - vararg strings: String, - predicate: DeclarativePredicate = { } + vararg strings: String, predicate: DeclarativePredicate = { } ) = requireNotNull(firstMethodDeclarativelyOrNull(strings = strings, predicate)) fun BytecodePatchContext.firstMutableMethodDeclarativelyOrNull( - vararg strings: String, - predicate: DeclarativePredicate = { } + vararg strings: String, predicate: DeclarativePredicate = { } ) = firstMutableMethodOrNull(strings = strings) { rememberDeclarativePredicate(predicate) } fun BytecodePatchContext.firstMutableMethodDeclaratively( - vararg strings: String, - predicate: DeclarativePredicate = { } + vararg strings: String, predicate: DeclarativePredicate = { } ) = requireNotNull(firstMutableMethodDeclarativelyOrNull(strings = strings, predicate)) fun gettingFirstMethodDeclarativelyOrNull( - vararg strings: String, - predicate: DeclarativePredicate = { } + vararg strings: String, predicate: DeclarativePredicate = { } ) = gettingFirstMethodOrNull(strings = strings) { rememberDeclarativePredicate(predicate) } fun gettingFirstMethodDeclaratively( - vararg strings: String, - predicate: DeclarativePredicate = { } + vararg strings: String, predicate: DeclarativePredicate = { } ) = gettingFirstMethod(strings = strings) { rememberDeclarativePredicate(predicate) } fun gettingFirstMutableMethodDeclarativelyOrNull( - vararg strings: String, - predicate: DeclarativePredicate = { } + vararg strings: String, predicate: DeclarativePredicate = { } ) = gettingFirstMutableMethodOrNull(strings = strings) { rememberDeclarativePredicate(predicate) } fun gettingFirstMutableMethodDeclaratively( - vararg strings: String, - predicate: DeclarativePredicate = { } + vararg strings: String, predicate: DeclarativePredicate = { } ) = gettingFirstMutableMethod(strings = strings) { rememberDeclarativePredicate(predicate) } } object BytecodePatchContextClassDefMatching { fun BytecodePatchContext.firstClassDefOrNull( - type: String? = null, - predicate: ClassDefPredicate = { true } + type: String? = null, predicate: ClassDefPredicate = { true } ) = withPredicateContext { if (type == null) classDefs.firstClassDefOrNull(predicate) else classDefs[type]?.takeIf { it.predicate() } } fun BytecodePatchContext.firstClassDef( - type: String? = null, - predicate: ClassDefPredicate = { true } + type: String? = null, predicate: ClassDefPredicate = { true } ) = requireNotNull(firstClassDefOrNull(type, predicate)) fun BytecodePatchContext.firstMutableClassDefOrNull( - type: String? = null, - predicate: ClassDefPredicate = { true } + type: String? = null, predicate: ClassDefPredicate = { true } ) = firstClassDefOrNull(type, predicate)?.let { classDefs.getOrReplaceMutable(it) } fun BytecodePatchContext.firstMutableClassDef( - type: String? = null, - predicate: ClassDefPredicate = { true } + type: String? = null, predicate: ClassDefPredicate = { true } ) = requireNotNull(firstMutableClassDefOrNull(type, predicate)) fun gettingFirstClassDefOrNull( - type: String? = null, - predicate: ClassDefPredicate = { true } + type: String? = null, predicate: ClassDefPredicate = { true } ) = cachedReadOnlyProperty { firstClassDefOrNull(type, predicate) } fun gettingFirstClassDef( - type: String? = null, - predicate: ClassDefPredicate = { true } + type: String? = null, predicate: ClassDefPredicate = { true } ) = requireNotNull(gettingFirstClassDefOrNull(type, predicate)) fun gettingFirstMutableClassDefOrNull( - type: String? = null, - predicate: ClassDefPredicate = { true } + type: String? = null, predicate: ClassDefPredicate = { true } ) = cachedReadOnlyProperty { firstMutableClassDefOrNull(type, predicate) } fun gettingFirstMutableClassDef( - type: String? = null, - predicate: ClassDefPredicate = { true } + type: String? = null, predicate: ClassDefPredicate = { true } ) = requireNotNull(gettingFirstMutableClassDefOrNull(type, predicate)) fun BytecodePatchContext.firstClassDefDeclarativelyOrNull( - type: String? = null, - predicate: DeclarativePredicate = { } + type: String? = null, predicate: DeclarativePredicate = { } ) = firstClassDefOrNull(type) { rememberDeclarativePredicate(predicate) } fun BytecodePatchContext.firstClassDefDeclaratively( - type: String? = null, - predicate: DeclarativePredicate = { } + type: String? = null, predicate: DeclarativePredicate = { } ) = requireNotNull(firstClassDefDeclarativelyOrNull(type, predicate)) fun BytecodePatchContext.firstMutableClassDefDeclarativelyOrNull( - type: String? = null, - predicate: DeclarativePredicate = { } + type: String? = null, predicate: DeclarativePredicate = { } ) = firstMutableClassDefOrNull(type) { rememberDeclarativePredicate(predicate) } fun BytecodePatchContext.firstMutableClassDefDeclaratively( - type: String? = null, - predicate: DeclarativePredicate = { } + type: String? = null, predicate: DeclarativePredicate = { } ) = requireNotNull(firstMutableClassDefDeclarativelyOrNull(type, predicate)) fun gettingFirstClassDefDeclarativelyOrNull( - type: String? = null, - predicate: DeclarativePredicate = { } + type: String? = null, predicate: DeclarativePredicate = { } ) = cachedReadOnlyProperty { firstClassDefDeclarativelyOrNull(type, predicate) } fun gettingFirstClassDefDeclaratively( - type: String? = null, - predicate: DeclarativePredicate = { } + type: String? = null, predicate: DeclarativePredicate = { } ) = requireNotNull(gettingFirstClassDefDeclarativelyOrNull(type, predicate)) fun gettingFirstMutableClassDefDeclarativelyOrNull( - type: String? = null, - predicate: DeclarativePredicate = { } + type: String? = null, predicate: DeclarativePredicate = { } ) = cachedReadOnlyProperty { firstMutableClassDefDeclarativelyOrNull(type, predicate) } fun gettingFirstMutableClassDefDeclaratively( - type: String? = null, - predicate: DeclarativePredicate = { } + type: String? = null, predicate: DeclarativePredicate = { } ) = requireNotNull(gettingFirstMutableClassDefDeclarativelyOrNull(type, predicate)) } @@ -566,11 +534,9 @@ private inline fun withPredicateContext(block: PredicateContext.() -> T) = P fun indexedMatcher() = IndexedMatcher() -fun indexedMatcher(build: Function>) = - IndexedMatcher().apply(build) +fun indexedMatcher(build: Function>) = IndexedMatcher().apply(build) -fun Iterable.matchIndexed(build: Function>) = - indexedMatcher(build)(this) +fun Iterable.matchIndexed(build: Function>) = indexedMatcher(build)(this) context(_: PredicateContext) fun Iterable.rememberMatchIndexed(key: Any, build: Function>) = @@ -578,29 +544,24 @@ fun Iterable.rememberMatchIndexed(key: Any, build: Function Iterable.matchIndexed( - key: Any, - vararg items: IndexedMatcherPredicate + key: Any, vararg items: IndexedMatcherPredicate ) = indexedMatcher()(key, this) { items.forEach { +it } } fun at( - index: Int = 0, - predicate: IndexedMatcherPredicate + index: Int = 0, predicate: IndexedMatcherPredicate ): IndexedMatcherPredicate = { lastMatchedIndex, currentIndex, setNextIndex -> currentIndex == index && predicate(lastMatchedIndex, currentIndex, setNextIndex) } -fun at(index: Int = 0, predicate: Predicate) = - at(index) { _, _, _ -> predicate() } +fun at(index: Int = 0, predicate: Predicate) = at(index) { _, _, _ -> predicate() } fun at(predicate: IndexedMatcherPredicate): IndexedMatcherPredicate = at(0) { lastMatchedIndex, currentIndex, setNextIndex -> predicate(lastMatchedIndex, currentIndex, setNextIndex) } -fun at(predicate: Predicate) = - at { _, _, _ -> predicate() } +fun at(predicate: Predicate) = at { _, _, _ -> predicate() } fun after( - range: IntRange = 1..1, - predicate: IndexedMatcherPredicate + range: IntRange = 1..1, predicate: IndexedMatcherPredicate ): IndexedMatcherPredicate = predicate@{ lastMatchedIndex, currentIndex, setNextIndex -> val distance = currentIndex - lastMatchedIndex @@ -615,16 +576,13 @@ fun after( false } -fun after(range: IntRange = 1..1, predicate: Predicate) = - after(range) { _, _, _ -> predicate() } +fun after(range: IntRange = 1..1, predicate: Predicate) = after(range) { _, _, _ -> predicate() } -fun after(predicate: IndexedMatcherPredicate) = - after(1..1) { lastMatchedIndex, currentIndex, setNextIndex -> - predicate(lastMatchedIndex, currentIndex, setNextIndex) - } +fun after(predicate: IndexedMatcherPredicate) = after(1..1) { lastMatchedIndex, currentIndex, setNextIndex -> + predicate(lastMatchedIndex, currentIndex, setNextIndex) +} -fun after(predicate: Predicate) = - after { _, _, _ -> predicate() } +fun after(predicate: Predicate) = after { _, _, _ -> predicate() } fun anyOf( @@ -648,11 +606,9 @@ fun noneOf( context(matcher: IndexedMatcher) operator fun IndexedMatcherPredicate.unaryPlus() = matcher.add(this) -typealias IndexedMatcherPredicate = - T.(lastMatchedIndex: Int, currentIndex: Int, setNextIndex: (Int?) -> Unit) -> Boolean +typealias IndexedMatcherPredicate = T.(lastMatchedIndex: Int, currentIndex: Int, setNextIndex: (Int?) -> Unit) -> Boolean -class IndexedMatcher : - Matcher>() { +class IndexedMatcher : Matcher>() { val indices: List field = mutableListOf() @@ -679,11 +635,7 @@ class IndexedMatcher : val stack = ArrayDeque() stack.add( Frame( - patternIndex = 0, - lastMatchedIndex = -1, - previousFrame = null, - nextHayIndex = 0, - matchedIndex = -1 + patternIndex = 0, lastMatchedIndex = -1, previousFrame = null, nextHayIndex = 0, matchedIndex = -1 ) ) @@ -738,9 +690,7 @@ class IndexedMatcher : context(context: PredicateContext) inline operator fun > M.invoke( - key: Any, - iterable: Iterable, - builder: Function + key: Any, iterable: Iterable, builder: Function ) = context.remember(key) { apply(builder) }(iterable) abstract class Matcher : MutableList by mutableListOf() { @@ -781,16 +731,14 @@ fun all(target: T): Boolean = list.all { target.it() } context(list: MutablePredicateList) fun any(target: T): Boolean = list.any { target.it() } -fun MutablePredicateList.accessFlags(vararg flags: AccessFlags) = - predicate { accessFlags(flags = flags) } +fun MutablePredicateList.accessFlags(vararg flags: AccessFlags) = predicate { accessFlags(flags = flags) } fun MutablePredicateList.returnType( predicate: Predicate ) = predicate { this.returnType.predicate() } fun MutablePredicateList.returnType( - returnType: String, - compare: String.(String) -> Boolean = String::startsWith + returnType: String, compare: String.(String) -> Boolean = String::startsWith ) = predicate { this.returnType.compare(returnType) } fun MutablePredicateList.name( @@ -798,8 +746,7 @@ fun MutablePredicateList.name( ) = predicate { this.name.predicate() } fun MutablePredicateList.name( - name: String, - compare: String.(String) -> Boolean = String::equals + name: String, compare: String.(String) -> Boolean = String::equals ) = predicate { this.name.compare(name) } fun MutablePredicateList.definingClass( @@ -807,8 +754,7 @@ fun MutablePredicateList.definingClass( ) = predicate { this.definingClass.predicate() } fun MutablePredicateList.definingClass( - definingClass: String, - compare: String.(String) -> Boolean = String::equals + definingClass: String, compare: String.(String) -> Boolean = String::equals ) = predicate { this.definingClass.compare(definingClass) } fun MutablePredicateList.parameterTypes(vararg parameterTypePrefixes: String) = predicate { @@ -853,31 +799,24 @@ inline fun `is`( fun instruction(predicate: Predicate = { true }): IndexedMatcherPredicate = { _, _, _ -> predicate() } -fun registers(predicate: Predicate = { true }): IndexedMatcherPredicate = - { _, _, _ -> - when (this) { - is RegisterRangeInstruction -> - IntArray(registerCount) { startRegister + it }.predicate() +fun registers(predicate: Predicate = { true }): IndexedMatcherPredicate = { _, _, _ -> + when (this) { + is RegisterRangeInstruction -> IntArray(registerCount) { startRegister + it }.predicate() - is FiveRegisterInstruction -> - intArrayOf(registerC, registerD, registerE, registerF, registerG).predicate() + is FiveRegisterInstruction -> intArrayOf(registerC, registerD, registerE, registerF, registerG).predicate() - is ThreeRegisterInstruction -> - intArrayOf(registerA, registerB, registerC).predicate() + is ThreeRegisterInstruction -> intArrayOf(registerA, registerB, registerC).predicate() - is TwoRegisterInstruction -> - intArrayOf(registerA, registerB).predicate() + is TwoRegisterInstruction -> intArrayOf(registerA, registerB).predicate() - is OneRegisterInstruction -> - intArrayOf(registerA).predicate() + is OneRegisterInstruction -> intArrayOf(registerA).predicate() - else -> false - } + else -> false } +} fun registers( - vararg registers: Int, - compare: IntArray.(registers: IntArray) -> Boolean = { registers -> + vararg registers: Int, compare: IntArray.(registers: IntArray) -> Boolean = { registers -> this.size >= registers.size && registers.indices.all { this[it] == registers[it] } } ) = registers({ compare(registers) }) @@ -885,8 +824,7 @@ fun registers( fun literal(predicate: Predicate = { true }): IndexedMatcherPredicate = { _, _, _ -> wideLiteral?.predicate() == true } -fun literal(literal: Long, compare: Long.(Long) -> Boolean = Long::equals) = - literal { compare(literal) } +fun literal(literal: Long, compare: Long.(Long) -> Boolean = Long::equals) = literal { compare(literal) } inline fun reference( @@ -896,39 +834,33 @@ inline fun reference( } fun reference( - reference: String, - compare: String.(String) -> Boolean = String::equals + reference: String, compare: String.(String) -> Boolean = String::equals ): IndexedMatcherPredicate = { _, _, _ -> this.reference?.toString()?.compare(reference) == true } fun field(predicate: Predicate = { true }): IndexedMatcherPredicate = { _, _, _ -> fieldReference?.predicate() == true } -fun field(name: String, compare: String.(String) -> Boolean = String::equals) = - field { this.name.compare(name) } +fun field(name: String, compare: String.(String) -> Boolean = String::equals) = field { this.name.compare(name) } fun type(predicate: Predicate = { true }): IndexedMatcherPredicate = { _, _, _ -> type?.predicate() == true } -fun type(type: String, compare: String.(type: String) -> Boolean = String::equals) = - type { compare(type) } +fun type(type: String, compare: String.(type: String) -> Boolean = String::equals) = type { compare(type) } fun method(predicate: Predicate = { true }): IndexedMatcherPredicate = { _, _, _ -> methodReference?.predicate() == true } -fun method(name: String, compare: String.(String) -> Boolean = String::equals) = - method { this.name.compare(name) } +fun method(name: String, compare: String.(String) -> Boolean = String::equals) = method { this.name.compare(name) } -fun string(compare: Predicate = { true }): IndexedMatcherPredicate = - predicate@{ _, _, _ -> - this@predicate.string?.compare() == true - } +fun string(compare: Predicate = { true }): IndexedMatcherPredicate = predicate@{ _, _, _ -> + this@predicate.string?.compare() == true +} context(stringsList: MutableList) fun string( - string: String, - compare: String.(String) -> Boolean = String::equals + string: String, compare: String.(String) -> Boolean = String::equals ): IndexedMatcherPredicate { if (compare == String::equals) stringsList += string @@ -944,18 +876,12 @@ operator fun String.invoke(compare: String.(String) -> Boolean = String::equals) return { _, _, _ -> string?.compare(this@invoke) == true } } -operator fun Opcode.invoke(): IndexedMatcherPredicate = - { _, _, _ -> opcode == this@invoke } +operator fun Opcode.invoke(): IndexedMatcherPredicate = { _, _, _ -> opcode == this@invoke } -private typealias BuildDeclarativePredicate = context( -PredicateContext, -IndexedMatcher, -MutableList -) MutablePredicateList.() -> Unit +private typealias BuildDeclarativePredicate = context(PredicateContext, IndexedMatcher, MutableList) MutablePredicateList.() -> Unit fun firstMethodComposite( - vararg strings: String, - build: BuildDeclarativePredicate + vararg strings: String, build: BuildDeclarativePredicate ) = MatchBuilder(strings = strings, build) class MatchBuilder private constructor( @@ -964,15 +890,19 @@ class MatchBuilder private constructor( build: BuildDeclarativePredicate, ) { - internal constructor(vararg strings: String, build: BuildDeclarativePredicate) : - this(strings = mutableListOf(elements = strings), indexedMatcher(), build) + internal constructor( + vararg strings: String, + build: BuildDeclarativePredicate + ) : this(strings = mutableListOf(elements = strings), indexedMatcher(), build) private val predicate: DeclarativePredicate = context(strings, indexedMatcher) { { build() } } val indices = indexedMatcher.indices - private val BytecodePatchContext.cachedImmutableMethodOrNull - by gettingFirstMethodDeclarativelyOrNull(strings = strings.toTypedArray(), predicate) + private val BytecodePatchContext.cachedImmutableMethodOrNull by gettingFirstMethodDeclarativelyOrNull( + strings = strings.toTypedArray(), + predicate + ) private val BytecodePatchContext.cachedImmutableClassDefOrNull by cachedReadOnlyProperty { val type = cachedImmutableMethodOrNull?.definingClass ?: return@cachedReadOnlyProperty null @@ -1014,16 +944,12 @@ class MatchBuilder private constructor( context(context: BytecodePatchContext) fun match(classDef: ClassDef) = Match( - context, - classDef.firstMethodDeclarativelyOrNull { predicate() }, - indices.toList() + context, classDef.firstMethodDeclarativelyOrNull { predicate() }, indices.toList() ) } class Match( - val context: BytecodePatchContext, - val immutableMethodOrNull: Method?, - val indices: List + val context: BytecodePatchContext, val immutableMethodOrNull: Method?, val indices: List ) { val immutableMethod by lazy { requireNotNull(immutableMethodOrNull) }