From 6bf1b55d1400dfafb3784cb5b37c76f4a8c5f92c Mon Sep 17 00:00:00 2001 From: Fankesyooni Date: Fri, 25 Mar 2022 00:21:50 +0800 Subject: [PATCH] Add new function --- .../hook/core/finder/ConstructorFinder.kt | 8 +-- .../hook/core/finder/FieldFinder.kt | 72 +++++++++++++++++++ .../hook/core/finder/MethodFinder.kt | 69 ++++++++++++++++-- 3 files changed, 141 insertions(+), 8 deletions(-) diff --git a/yukihookapi/src/api/kotlin/com/highcapable/yukihookapi/hook/core/finder/ConstructorFinder.kt b/yukihookapi/src/api/kotlin/com/highcapable/yukihookapi/hook/core/finder/ConstructorFinder.kt index d2fe16b0..58100650 100644 --- a/yukihookapi/src/api/kotlin/com/highcapable/yukihookapi/hook/core/finder/ConstructorFinder.kt +++ b/yukihookapi/src/api/kotlin/com/highcapable/yukihookapi/hook/core/finder/ConstructorFinder.kt @@ -59,7 +59,7 @@ class ConstructorFinder( private var remedyPlansCallback: (() -> Unit)? = null /** [Constructor] 参数数组 */ - private var params: Array>? = null + private var paramTypes: Array>? = null /** * [Constructor] 参数 @@ -71,7 +71,7 @@ class ConstructorFinder( */ fun param(vararg paramType: Class<*>) { if (paramType.isEmpty()) error("paramType is empty, please delete param() method") - params = paramType + paramTypes = paramType } /** @@ -80,8 +80,8 @@ class ConstructorFinder( * @throws NoSuchMethodError 如果找不到构造方法 */ private val result - get() = if (params != null) - ReflectionUtils.findConstructorExact(classSet, *params!!) + get() = if (paramTypes != null) + ReflectionUtils.findConstructorExact(classSet, *paramTypes!!) else ReflectionUtils.findConstructorExact(classSet) /** diff --git a/yukihookapi/src/api/kotlin/com/highcapable/yukihookapi/hook/core/finder/FieldFinder.kt b/yukihookapi/src/api/kotlin/com/highcapable/yukihookapi/hook/core/finder/FieldFinder.kt index 81755de8..5e533e5b 100644 --- a/yukihookapi/src/api/kotlin/com/highcapable/yukihookapi/hook/core/finder/FieldFinder.kt +++ b/yukihookapi/src/api/kotlin/com/highcapable/yukihookapi/hook/core/finder/FieldFinder.kt @@ -141,6 +141,78 @@ class FieldFinder( */ fun of(instance: Any? = null) = get(instance).self as? T? + /** + * 得到变量的 [Int] 实例 + * + * - ❗请确认目标变量的类型 - 发生错误会返回默认值 + * @param instance 变量所在的实例对象 - 如果是静态可不填 - 默认 null + * @return [Int] 取不到返回 0 + */ + fun ofInt(instance: Any? = null) = of(instance) ?: 0 + + /** + * 得到变量的 [Long] 实例 + * + * - ❗请确认目标变量的类型 - 发生错误会返回默认值 + * @param instance 变量所在的实例对象 - 如果是静态可不填 - 默认 null + * @return [Long] 取不到返回 0L + */ + fun ofLong(instance: Any? = null) = of(instance) ?: 0L + + /** + * 得到变量的 [Short] 实例 + * + * - ❗请确认目标变量的类型 - 发生错误会返回默认值 + * @param instance 变量所在的实例对象 - 如果是静态可不填 - 默认 null + * @return [Short] 取不到返回 0 + */ + fun ofShort(instance: Any? = null) = of(instance) ?: 0 + + /** + * 得到变量的 [Double] 实例 + * + * - ❗请确认目标变量的类型 - 发生错误会返回默认值 + * @param instance 变量所在的实例对象 - 如果是静态可不填 - 默认 null + * @return [Double] 取不到返回 0.0 + */ + fun ofDouble(instance: Any? = null) = of(instance) ?: 0.0 + + /** + * 得到变量的 [Float] 实例 + * + * - ❗请确认目标变量的类型 - 发生错误会返回默认值 + * @param instance 变量所在的实例对象 - 如果是静态可不填 - 默认 null + * @return [Float] 取不到返回 0f + */ + fun ofFloat(instance: Any? = null) = of(instance) ?: 0f + + /** + * 得到变量的 [String] 实例 + * + * - ❗请确认目标变量的类型 - 发生错误会返回默认值 + * @param instance 变量所在的实例对象 - 如果是静态可不填 - 默认 null + * @return [String] 取不到返回 "" + */ + fun ofString(instance: Any? = null) = of(instance) ?: "" + + /** + * 得到变量的 [Boolean] 实例 + * + * - ❗请确认目标变量的类型 - 发生错误会返回默认值 + * @param instance 变量所在的实例对象 - 如果是静态可不填 - 默认 null + * @return [Boolean] 取不到返回 false + */ + fun ofBoolean(instance: Any? = null) = of(instance) ?: false + + /** + * 得到变量的 [Any] 实例 + * + * - ❗请确认目标变量的类型 - 发生错误会返回默认值 + * @param instance 变量所在的实例对象 - 如果是静态可不填 - 默认 null + * @return [Any] or null + */ + fun ofAny(instance: Any? = null) = of(instance) + /** * 得到变量本身 * @return [Field] or null diff --git a/yukihookapi/src/api/kotlin/com/highcapable/yukihookapi/hook/core/finder/MethodFinder.kt b/yukihookapi/src/api/kotlin/com/highcapable/yukihookapi/hook/core/finder/MethodFinder.kt index 92ba8865..bc8f8b8a 100644 --- a/yukihookapi/src/api/kotlin/com/highcapable/yukihookapi/hook/core/finder/MethodFinder.kt +++ b/yukihookapi/src/api/kotlin/com/highcapable/yukihookapi/hook/core/finder/MethodFinder.kt @@ -60,7 +60,7 @@ class MethodFinder( private var remedyPlansCallback: (() -> Unit)? = null /** [Method] 参数数组 */ - private var params: Array>? = null + private var paramTypes: Array>? = null /** * [Method] 名称 @@ -86,7 +86,7 @@ class MethodFinder( */ fun param(vararg paramType: Class<*>) { if (paramType.isEmpty()) error("paramType is empty, please delete param() method") - params = paramType + paramTypes = paramType } /** @@ -96,8 +96,8 @@ class MethodFinder( * @throws NoSuchMethodError 如果找不到方法 */ private val result - get() = if (params != null) - ReflectionUtils.findMethodBestMatch(classSet, returnType, name, *params!!) + get() = if (paramTypes != null) + ReflectionUtils.findMethodBestMatch(classSet, returnType, name, *paramTypes!!) else ReflectionUtils.findMethodNoParam(classSet, returnType, name) /** @@ -332,6 +332,67 @@ class MethodFinder( */ fun invoke(vararg param: Any?) = baseCall(*param) as? T? + /** + * 执行方法 - 指定 [Int] 返回值类型 + * + * - ❗请确认目标方法的返回值 - 发生错误会返回默认值 + * @param param 方法参数 + * @return [Int] 取不到返回 0 + */ + fun callInt(vararg param: Any?) = invoke(param) ?: 0 + + /** + * 执行方法 - 指定 [Long] 返回值类型 + * + * - ❗请确认目标方法的返回值 - 发生错误会返回默认值 + * @param param 方法参数 + * @return [Long] 取不到返回 0 + */ + fun callLong(vararg param: Any?) = invoke(param) ?: 0L + + /** + * 执行方法 - 指定 [Short] 返回值类型 + * + * - ❗请确认目标方法的返回值 - 发生错误会返回默认值 + * @param param 方法参数 + * @return [Short] 取不到返回 0 + */ + fun callShort(vararg param: Any?) = invoke(param) ?: 0 + + /** + * 执行方法 - 指定 [Double] 返回值类型 + * + * - ❗请确认目标方法的返回值 - 发生错误会返回默认值 + * @param param 方法参数 + * @return [Double] 取不到返回 0 + */ + fun callDouble(vararg param: Any?) = invoke(param) ?: 0.0 + + /** + * 执行方法 - 指定 [Float] 返回值类型 + * + * - ❗请确认目标方法的返回值 - 发生错误会返回默认值 + * @param param 方法参数 + * @return [Float] 取不到返回 0f + */ + fun callFloat(vararg param: Any?) = invoke(param) ?: 0f + + /** + * 执行方法 - 指定 [String] 返回值类型 + * @param param 方法参数 + * @return [String] 取不到返回 "" + */ + fun callString(vararg param: Any?) = invoke(param) ?: "" + + /** + * 执行方法 - 指定 [Boolean] 返回值类型 + * + * - ❗请确认目标方法的返回值 - 发生错误会返回默认值 + * @param param 方法参数 + * @return [Boolean] 取不到返回 false + */ + fun callBoolean(vararg param: Any?) = invoke(param) ?: false + override fun toString() = "[${(memberInstance as? Method?)?.name ?: ""}] in [${instance?.javaClass?.name ?: ""}]" }