diff --git a/docs/api/public/NameConditions.md b/docs/api/public/NameConditions.md index 0aa0e973..5d971baf 100644 --- a/docs/api/public/NameConditions.md +++ b/docs/api/public/NameConditions.md @@ -28,6 +28,8 @@ fun equalsOf(other: String, isIgnoreCase: Boolean) > 完全字符匹配。 +可以重复使用,最终会选择完全匹配的一个。 + ### startsWith *- method* ```kotlin @@ -42,6 +44,8 @@ fun startsWith(prefix: String, startIndex: Int, isIgnoreCase: Boolean) > 起始字符匹配。 +可以重复使用,最终会选择完全匹配的一个。 + ### endsWith *- method* ```kotlin @@ -56,6 +60,8 @@ fun endsWith(suffix: String, isIgnoreCase: Boolean) > 结束字符匹配。 +可以重复使用,最终会选择完全匹配的一个。 + ### contains *- method* ```kotlin @@ -70,6 +76,8 @@ fun contains(other: String, isIgnoreCase: Boolean) > 包含字符匹配。 +可以重复使用,最终会选择完全匹配的一个。 + ### matches *- method* ```kotlin @@ -88,6 +96,8 @@ fun matches(regex: Regex) > 正则字符匹配。 +可以重复使用,最终会选择完全匹配的一个。 + ### length *- method* ```kotlin @@ -110,6 +120,8 @@ fun length(conditions: IntConditions) > 字符长度与范围及条件匹配。 +不可重复使用,重复使用旧的条件会被当前条件替换。 + ### thisSynthetic0 *- method* ```kotlin diff --git a/yukihookapi/src/api/kotlin/com/highcapable/yukihookapi/hook/core/finder/type/NameConditions.kt b/yukihookapi/src/api/kotlin/com/highcapable/yukihookapi/hook/core/finder/type/NameConditions.kt index 74291a37..0458f88b 100644 --- a/yukihookapi/src/api/kotlin/com/highcapable/yukihookapi/hook/core/finder/type/NameConditions.kt +++ b/yukihookapi/src/api/kotlin/com/highcapable/yukihookapi/hook/core/finder/type/NameConditions.kt @@ -41,20 +41,20 @@ import java.lang.reflect.Method */ class NameConditions @PublishedApi internal constructor() { - /** 完全字符匹配条件 */ - private var cdsEqualsOf: Pair? = null + /** 完全字符匹配条件数组 */ + private var cdsEqualsOfs = ArrayList>() - /** 起始字符匹配条件 */ - private var cdsStartsWith: Triple? = null + /** 起始字符匹配条件数组 */ + private var cdsStartsWiths = ArrayList>() - /** 结束字符匹配条件 */ - private var cdsEndsWith: Pair? = null + /** 结束字符匹配条件数组 */ + private var cdsEndsWiths = ArrayList>() - /** 包含字符匹配条件 */ - private var cdsContains: Pair? = null + /** 包含字符匹配条件数组 */ + private var cdsContains = ArrayList>() - /** 正则字符匹配条件 */ - private var cdsMatches: Regex? = null + /** 正则字符匹配条件数组 */ + private var cdsMatches = ArrayList() /** 字符长度匹配条件 */ private var cdsLength = -1 @@ -89,47 +89,80 @@ class NameConditions @PublishedApi internal constructor() { /** * 完全字符匹配 * - * 例如匹配 catMonitor 可设置为 ↓ + * 例如匹配 "catMonitor" 可设置为 ↓ * * ```kotlin * equalsOf(other = "catMonitor") * ``` + * + * - 可以重复使用 - 最终会选择完全匹配的一个 + * + * 例如匹配 "cargoSale" or "catMonitor" 可设置为 ↓ + * + * ```kotlin + * name { + * equalsOf(other = "cargoSale") + * equalsOf(other = "catMonitor") + * } + * ``` * @param other 字符匹配 * @param isIgnoreCase 是否忽略字符中的大小写 - 默认否 */ fun equalsOf(other: String, isIgnoreCase: Boolean = false) { - cdsEqualsOf = Pair(other, isIgnoreCase) + cdsEqualsOfs.add(Pair(other, isIgnoreCase)) } /** * 起始字符匹配 * - * 例如匹配 catMonitor 可设置为 ↓ + * 例如匹配 "catMonitor" 可设置为 ↓ * * ```kotlin * startsWith(prefix = "cat") * ``` + * + * - 可以重复使用 - 最终会选择完全匹配的一个 + * + * 例如匹配 "cargoSale" or "catMonitor" 可设置为 ↓ + * + * ```kotlin + * name { + * startsWith(prefix = "cargo") + * startsWith(prefix = "cat") + * } + * ``` * @param prefix 起始字符匹配 * @param startIndex 起始字符下标 - 默认从 0 开始 * @param isIgnoreCase 是否忽略字符中的大小写 - 默认否 */ fun startsWith(prefix: String, startIndex: Int = 0, isIgnoreCase: Boolean = false) { - cdsStartsWith = Triple(prefix, startIndex, isIgnoreCase) + cdsStartsWiths.add(Triple(prefix, startIndex, isIgnoreCase)) } /** * 结束字符匹配 * - * 例如匹配 catMonitor 可设置为 ↓ + * 例如匹配 "catMonitor" 可设置为 ↓ * * ```kotlin * endsWith(suffix = "Monitor") * ``` + * + * - 可以重复使用 - 最终会选择完全匹配的一个 + * + * 例如匹配 "cargoSale" or "catMonitor" 可设置为 ↓ + * + * ```kotlin + * name { + * endsWith(suffix = "Sale") + * endsWith(suffix = "Monitor") + * } + * ``` * @param suffix 结束字符匹配 * @param isIgnoreCase 是否忽略字符中的大小写 - 默认否 */ fun endsWith(suffix: String, isIgnoreCase: Boolean = false) { - cdsEndsWith = Pair(suffix, isIgnoreCase) + cdsEndsWiths.add(Pair(suffix, isIgnoreCase)) } /** @@ -140,33 +173,48 @@ class NameConditions @PublishedApi internal constructor() { * ```kotlin * contains(other = "atMoni") * ``` + * + * - 可以重复使用 - 最终会选择完全匹配的一个 + * + * 例如匹配 "cargoSale" or "catMonitor" 可设置为 ↓ + * + * ```kotlin + * name { + * contains(other = "goSal") + * contains(other = "atMoni") + * } + * ``` * @param other 包含字符匹配 * @param isIgnoreCase 是否忽略字符中的大小写 - 默认否 */ fun contains(other: String, isIgnoreCase: Boolean = false) { - cdsContains = Pair(other, isIgnoreCase) + cdsContains.add(Pair(other, isIgnoreCase)) } /** * 正则字符匹配 * + * - 可以重复使用 - 最终会选择完全匹配的一个 * @param regex 正则字符 */ fun matches(regex: String) { - cdsMatches = regex.toRegex() + cdsMatches.add(regex.toRegex()) } /** * 正则字符匹配 * + * - 可以重复使用 - 最终会选择完全匹配的一个 * @param regex 正则字符 */ fun matches(regex: Regex) { - cdsMatches = regex + cdsMatches.add(regex) } /** * 字符长度匹配 + * + * - 不可重复使用 - 重复使用旧的条件会被当前条件替换 * @param num 预期的长度 */ fun length(num: Int) { @@ -175,6 +223,8 @@ class NameConditions @PublishedApi internal constructor() { /** * 字符长度范围匹配 + * + * - 不可重复使用 - 重复使用旧的条件会被当前条件替换 * @param numRange 预期的长度范围 */ fun length(numRange: IntRange) { @@ -183,6 +233,8 @@ class NameConditions @PublishedApi internal constructor() { /** * 字符长度条件匹配 + * + * - 不可重复使用 - 重复使用旧的条件会被当前条件替换 * @param conditions 条件方法体 */ fun length(conditions: IntConditions) { @@ -280,22 +332,23 @@ class NameConditions @PublishedApi internal constructor() { is Method -> reflects.name is Field -> reflects.name else -> "" - }.also { - if (isThisSynthetic0) conditions = conditions && it == "this$0" - if (isOnlySymbols) conditions = conditions && it.matches("[*,.:~`'\"|/\\\\?!^()\\[\\]{}%@#$&\\-_+=<>]+".toRegex()) - if (isOnlyLetters) conditions = conditions && it.matches("[a-zA-Z]+".toRegex()) - if (isOnlyNumbers) conditions = conditions && it.matches("[0-9]+".toRegex()) - if (isOnlyLettersNumbers) conditions = conditions && it.matches("[a-zA-Z0-9]+".toRegex()) - if (isOnlyLowercase) conditions = conditions && it.matches("[a-z]+".toRegex()) - if (isOnlyUppercase) conditions = conditions && it.matches("[A-Z]+".toRegex()) - if (cdsEqualsOf != null) cdsEqualsOf?.apply { conditions = conditions && it.equals(first, second) } - if (cdsStartsWith != null) cdsStartsWith?.apply { conditions = conditions && it.startsWith(first, second, third) } - if (cdsEndsWith != null) cdsEndsWith?.apply { conditions = conditions && it.endsWith(first, second) } - if (cdsContains != null) cdsContains?.apply { conditions = conditions && it.contains(first, second) } - if (cdsMatches != null) cdsMatches?.apply { conditions = conditions && it.matches(regex = this) } - if (cdsLength >= 0) conditions = conditions && it.length == cdsLength - if (cdsLengthRange.isEmpty().not()) conditions = conditions && it.length in cdsLengthRange - if (cdsLengthConditions != null) conditions = conditions && cdsLengthConditions?.invoke(it.length) == true + }.also { symbolName -> + if (isThisSynthetic0) conditions = conditions && symbolName == "this$0" + if (isOnlySymbols) conditions = conditions && symbolName.matches("[*,.:~`'\"|/\\\\?!^()\\[\\]{}%@#$&\\-_+=<>]+".toRegex()) + if (isOnlyLetters) conditions = conditions && symbolName.matches("[a-zA-Z]+".toRegex()) + if (isOnlyNumbers) conditions = conditions && symbolName.matches("[0-9]+".toRegex()) + if (isOnlyLettersNumbers) conditions = conditions && symbolName.matches("[a-zA-Z0-9]+".toRegex()) + if (isOnlyLowercase) conditions = conditions && symbolName.matches("[a-z]+".toRegex()) + if (isOnlyUppercase) conditions = conditions && symbolName.matches("[A-Z]+".toRegex()) + cdsEqualsOfs.takeIf { it.isNotEmpty() }?.also { conditions = conditions && it.any { e -> symbolName.equals(e.first, e.second) } } + cdsStartsWiths.takeIf { it.isNotEmpty() } + ?.also { conditions = conditions && it.any { e -> symbolName.startsWith(e.first, e.second, e.third) } } + cdsEndsWiths.takeIf { it.isNotEmpty() }?.also { conditions = conditions && it.any { e -> symbolName.endsWith(e.first, e.second) } } + cdsContains.takeIf { it.isNotEmpty() }?.also { conditions = conditions && it.any { e -> symbolName.contains(e.first, e.second) } } + cdsMatches.takeIf { it.isNotEmpty() }?.also { conditions = conditions && it.any { e -> symbolName.matches(e) } } + cdsLength.takeIf { it >= 0 }?.also { conditions = conditions && symbolName.length == it } + cdsLengthRange.takeIf { it.isEmpty().not() }?.also { conditions = conditions && symbolName.length in it } + cdsLengthConditions?.also { conditions = conditions && it(symbolName.length) } } return conditions } @@ -309,14 +362,14 @@ class NameConditions @PublishedApi internal constructor() { if (isOnlyLettersNumbers) conditions += " " if (isOnlyLowercase) conditions += " " if (isOnlyUppercase) conditions += " " - if (cdsEqualsOf != null) cdsEqualsOf?.apply { conditions += " " } - if (cdsStartsWith != null) cdsStartsWith?.apply { conditions += " " } - if (cdsEndsWith != null) cdsEndsWith?.apply { conditions += " " } - if (cdsContains != null) cdsContains?.apply { conditions += " " } - if (cdsMatches != null) cdsMatches?.apply { conditions += " " } - if (cdsLength >= 0) conditions += " " - if (cdsLengthRange.isEmpty().not()) conditions += " " - if (cdsLengthConditions != null) conditions += " " + if (cdsEqualsOfs.isNotEmpty()) conditions += " " + if (cdsStartsWiths.isNotEmpty()) conditions += " " + if (cdsEndsWiths.isNotEmpty()) conditions += " " + if (cdsContains.isNotEmpty()) conditions += " " + if (cdsMatches.isNotEmpty()) conditions += " " + if (cdsLength >= 0) conditions += " " + if (cdsLengthRange.isEmpty().not()) conditions += " " + if (cdsLengthConditions != null) conditions += " " return "[${conditions.trim()}]" } } \ No newline at end of file