From 2a404573648349319dadfa1180f3d00f1d0559e4 Mon Sep 17 00:00:00 2001 From: fankesyooni Date: Wed, 14 Sep 2022 01:55:40 +0800 Subject: [PATCH] Modify change contains function from reflects to symbolName in NameConditions (conflict commit) --- .../hook/core/finder/tools/ReflectionTool.kt | 10 ++-- .../hook/core/finder/type/NameConditions.kt | 46 ++++++++----------- 2 files changed, 25 insertions(+), 31 deletions(-) diff --git a/yukihookapi/src/api/kotlin/com/highcapable/yukihookapi/hook/core/finder/tools/ReflectionTool.kt b/yukihookapi/src/api/kotlin/com/highcapable/yukihookapi/hook/core/finder/tools/ReflectionTool.kt index 0e397cc9..26ba7585 100644 --- a/yukihookapi/src/api/kotlin/com/highcapable/yukihookapi/hook/core/finder/tools/ReflectionTool.kt +++ b/yukihookapi/src/api/kotlin/com/highcapable/yukihookapi/hook/core/finder/tools/ReflectionTool.kt @@ -28,6 +28,7 @@ package com.highcapable.yukihookapi.hook.core.finder.tools import com.highcapable.yukihookapi.hook.core.finder.base.data.BaseRulesData +import com.highcapable.yukihookapi.hook.core.finder.classes.data.ClassRulesData import com.highcapable.yukihookapi.hook.core.finder.members.data.ConstructorRulesData import com.highcapable.yukihookapi.hook.core.finder.members.data.FieldRulesData import com.highcapable.yukihookapi.hook.core.finder.members.data.MemberRulesData @@ -107,7 +108,7 @@ internal object ReflectionTool { val iLType = type?.let(matchIndex) { e -> declares.filter { e == it.type }.lastIndex } ?: -1 val iLName = name.takeIf(matchIndex) { it.isNotBlank() }?.let { e -> declares.filter { e == it.name }.lastIndex } ?: -1 val iLModify = modifiers?.let(matchIndex) { e -> declares.filter { e.contains(it) }.lastIndex } ?: -1 - val iLNameCds = nameConditions?.let(matchIndex) { e -> declares.filter { e.contains(it) }.lastIndex } ?: -1 + val iLNameCds = nameConditions?.let(matchIndex) { e -> declares.filter { e.contains(it.name) }.lastIndex } ?: -1 declares.forEachIndexed { index, instance -> conditions { type?.also { @@ -129,7 +130,7 @@ internal object ReflectionTool { }) } nameConditions?.also { - and(it.contains(instance).let { hold -> + and(it.contains(instance.name).let { hold -> if (hold) iNameCds++ hold && matchIndex.compare(iNameCds, iLNameCds) }) @@ -174,7 +175,7 @@ internal object ReflectionTool { val iLParamTypes = paramTypes?.let(matchIndex) { e -> declares.filter { paramTypesEq(e, it.parameterTypes) }.lastIndex } ?: -1 val iLName = name.takeIf(matchIndex) { it.isNotBlank() }?.let { e -> declares.filter { e == it.name }.lastIndex } ?: -1 val iLModify = modifiers?.let(matchIndex) { e -> declares.filter { e.contains(it) }.lastIndex } ?: -1 - val iLNameCds = nameConditions?.let(matchIndex) { e -> declares.filter { e.contains(it) }.lastIndex } ?: -1 + val iLNameCds = nameConditions?.let(matchIndex) { e -> declares.filter { e.contains(it.name) }.lastIndex } ?: -1 declares.forEachIndexed { index, instance -> conditions { name.takeIf { it.isNotBlank() }?.also { @@ -220,7 +221,7 @@ internal object ReflectionTool { }) } nameConditions?.also { - and(it.contains(instance).let { hold -> + and(it.contains(instance.name).let { hold -> if (hold) iNameCds++ hold && matchIndex.compare(iNameCds, iLNameCds) }) @@ -331,6 +332,7 @@ internal object ReflectionTool { is FieldRulesData -> isInitialize.not() is MethodRulesData -> isInitialize.not() is ConstructorRulesData -> isInitialize.not() + is ClassRulesData -> isInitialize.not() else -> true }.takeIf { it }?.also { error("You must set a condition when finding a $objectName") } return result(this) 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 0458f88b..6bb8167f 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 @@ -319,37 +319,29 @@ class NameConditions @PublishedApi internal constructor() { } /** - * 对比 [Class]、[Member] 类型是否符合条件 - * @param reflects 实例 - 只支持 [Class]、[Method]、[Field] - * @param isUseSimple 是否使用 [Class.getSimpleName] - 只支持 [Class] - 默认否 + * 对比 [Class]、[Member] 名称是否符合条件 + * @param symbolName 符号名称 - 可以使用 [Class.getName]、[Class.getSimpleName]、[Field.getName]、[Method.getName] 获取 * @return [Boolean] 是否符合条件 */ @PublishedApi - internal fun contains(reflects: Any, isUseSimple: Boolean = false): Boolean { + internal fun contains(symbolName: String): Boolean { var conditions = true - when (reflects) { - is Class<*> -> if (isUseSimple) reflects.simpleName else reflects.name - is Method -> reflects.name - is Field -> reflects.name - else -> "" - }.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) } - } + 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 }