From c849690699098771e9dfa63d4c8a22fa725a330f Mon Sep 17 00:00:00 2001 From: fankesyooni Date: Sat, 23 Sep 2023 20:55:43 +0800 Subject: [PATCH] refactor: merge all util functions to factory --- .../hook/core/YukiMemberHookCreator.kt | 6 +- .../hook/core/finder/base/MemberBaseFinder.kt | 4 +- .../core/finder/classes/DexClassFinder.kt | 4 +- .../core/finder/members/ConstructorFinder.kt | 4 +- .../hook/core/finder/members/FieldFinder.kt | 2 +- .../hook/core/finder/members/MethodFinder.kt | 4 +- .../hook/core/finder/tools/ReflectionTool.kt | 2 +- .../yukihookapi/hook/log/LoggerFactory.kt | 2 +- .../yukihookapi/hook/param/PackageParam.kt | 2 +- .../hook/utils/factory/ThreadFactory.kt | 80 +++++++++++++++++++ .../hook/utils/factory/ThrowableFactory.kt | 39 +++++++++ .../VariableFactory.kt} | 62 +------------- .../xposed/channel/YukiHookDataChannel.kt | 2 +- .../prefs/ui/ModulePreferenceFragment.kt | 2 +- 14 files changed, 138 insertions(+), 77 deletions(-) create mode 100644 yukihookapi-core/src/main/java/com/highcapable/yukihookapi/hook/utils/factory/ThreadFactory.kt create mode 100644 yukihookapi-core/src/main/java/com/highcapable/yukihookapi/hook/utils/factory/ThrowableFactory.kt rename yukihookapi-core/src/main/java/com/highcapable/yukihookapi/hook/utils/{UtilsFactory.kt => factory/VariableFactory.kt} (78%) diff --git a/yukihookapi-core/src/main/java/com/highcapable/yukihookapi/hook/core/YukiMemberHookCreator.kt b/yukihookapi-core/src/main/java/com/highcapable/yukihookapi/hook/core/YukiMemberHookCreator.kt index edad39db..e133d9fe 100644 --- a/yukihookapi-core/src/main/java/com/highcapable/yukihookapi/hook/core/YukiMemberHookCreator.kt +++ b/yukihookapi-core/src/main/java/com/highcapable/yukihookapi/hook/core/YukiMemberHookCreator.kt @@ -66,9 +66,9 @@ import com.highcapable.yukihookapi.hook.type.java.JavaConstructorClass import com.highcapable.yukihookapi.hook.type.java.JavaFieldClass import com.highcapable.yukihookapi.hook.type.java.JavaMemberClass import com.highcapable.yukihookapi.hook.type.java.JavaMethodClass -import com.highcapable.yukihookapi.hook.utils.RandomSeed -import com.highcapable.yukihookapi.hook.utils.await -import com.highcapable.yukihookapi.hook.utils.conditions +import com.highcapable.yukihookapi.hook.utils.factory.RandomSeed +import com.highcapable.yukihookapi.hook.utils.factory.await +import com.highcapable.yukihookapi.hook.utils.factory.conditions import com.highcapable.yukihookapi.hook.xposed.bridge.type.HookEntryType import java.lang.reflect.Constructor import java.lang.reflect.Field diff --git a/yukihookapi-core/src/main/java/com/highcapable/yukihookapi/hook/core/finder/base/MemberBaseFinder.kt b/yukihookapi-core/src/main/java/com/highcapable/yukihookapi/hook/core/finder/base/MemberBaseFinder.kt index 8104b578..79b1f388 100644 --- a/yukihookapi-core/src/main/java/com/highcapable/yukihookapi/hook/core/finder/base/MemberBaseFinder.kt +++ b/yukihookapi-core/src/main/java/com/highcapable/yukihookapi/hook/core/finder/base/MemberBaseFinder.kt @@ -33,8 +33,8 @@ import com.highcapable.yukihookapi.hook.core.YukiMemberHookCreator import com.highcapable.yukihookapi.hook.core.api.compat.HookApiCategoryHelper import com.highcapable.yukihookapi.hook.log.yLoggerE import com.highcapable.yukihookapi.hook.log.yLoggerI -import com.highcapable.yukihookapi.hook.utils.await -import com.highcapable.yukihookapi.hook.utils.unit +import com.highcapable.yukihookapi.hook.utils.factory.await +import com.highcapable.yukihookapi.hook.utils.factory.unit import java.lang.reflect.Constructor import java.lang.reflect.Field import java.lang.reflect.Member diff --git a/yukihookapi-core/src/main/java/com/highcapable/yukihookapi/hook/core/finder/classes/DexClassFinder.kt b/yukihookapi-core/src/main/java/com/highcapable/yukihookapi/hook/core/finder/classes/DexClassFinder.kt index 25bc055b..083500ca 100644 --- a/yukihookapi-core/src/main/java/com/highcapable/yukihookapi/hook/core/finder/classes/DexClassFinder.kt +++ b/yukihookapi-core/src/main/java/com/highcapable/yukihookapi/hook/core/finder/classes/DexClassFinder.kt @@ -51,8 +51,8 @@ import com.highcapable.yukihookapi.hook.factory.searchClass import com.highcapable.yukihookapi.hook.factory.toClass import com.highcapable.yukihookapi.hook.log.yLoggerW import com.highcapable.yukihookapi.hook.param.PackageParam -import com.highcapable.yukihookapi.hook.utils.await -import com.highcapable.yukihookapi.hook.utils.runBlocking +import com.highcapable.yukihookapi.hook.utils.factory.await +import com.highcapable.yukihookapi.hook.utils.factory.runBlocking import com.highcapable.yukihookapi.hook.xposed.parasitic.AppParasitics import dalvik.system.BaseDexClassLoader import java.lang.reflect.Constructor diff --git a/yukihookapi-core/src/main/java/com/highcapable/yukihookapi/hook/core/finder/members/ConstructorFinder.kt b/yukihookapi-core/src/main/java/com/highcapable/yukihookapi/hook/core/finder/members/ConstructorFinder.kt index 9f861128..c79020ca 100644 --- a/yukihookapi-core/src/main/java/com/highcapable/yukihookapi/hook/core/finder/members/ConstructorFinder.kt +++ b/yukihookapi-core/src/main/java/com/highcapable/yukihookapi/hook/core/finder/members/ConstructorFinder.kt @@ -44,8 +44,8 @@ import com.highcapable.yukihookapi.hook.factory.hasExtends import com.highcapable.yukihookapi.hook.log.yLoggerW import com.highcapable.yukihookapi.hook.type.defined.UndefinedType import com.highcapable.yukihookapi.hook.type.defined.VagueType -import com.highcapable.yukihookapi.hook.utils.runBlocking -import com.highcapable.yukihookapi.hook.utils.unit +import com.highcapable.yukihookapi.hook.utils.factory.runBlocking +import com.highcapable.yukihookapi.hook.utils.factory.unit import java.lang.reflect.Constructor import java.lang.reflect.Member diff --git a/yukihookapi-core/src/main/java/com/highcapable/yukihookapi/hook/core/finder/members/FieldFinder.kt b/yukihookapi-core/src/main/java/com/highcapable/yukihookapi/hook/core/finder/members/FieldFinder.kt index 8a161035..9353c100 100644 --- a/yukihookapi-core/src/main/java/com/highcapable/yukihookapi/hook/core/finder/members/FieldFinder.kt +++ b/yukihookapi-core/src/main/java/com/highcapable/yukihookapi/hook/core/finder/members/FieldFinder.kt @@ -44,7 +44,7 @@ import com.highcapable.yukihookapi.hook.core.finder.type.factory.ObjectCondition import com.highcapable.yukihookapi.hook.factory.current import com.highcapable.yukihookapi.hook.factory.hasExtends import com.highcapable.yukihookapi.hook.log.yLoggerW -import com.highcapable.yukihookapi.hook.utils.runBlocking +import com.highcapable.yukihookapi.hook.utils.factory.runBlocking import java.lang.reflect.Field /** diff --git a/yukihookapi-core/src/main/java/com/highcapable/yukihookapi/hook/core/finder/members/MethodFinder.kt b/yukihookapi-core/src/main/java/com/highcapable/yukihookapi/hook/core/finder/members/MethodFinder.kt index 2fa45ed2..9702b282 100644 --- a/yukihookapi-core/src/main/java/com/highcapable/yukihookapi/hook/core/finder/members/MethodFinder.kt +++ b/yukihookapi-core/src/main/java/com/highcapable/yukihookapi/hook/core/finder/members/MethodFinder.kt @@ -47,8 +47,8 @@ import com.highcapable.yukihookapi.hook.factory.hasExtends import com.highcapable.yukihookapi.hook.log.yLoggerW import com.highcapable.yukihookapi.hook.type.defined.UndefinedType import com.highcapable.yukihookapi.hook.type.defined.VagueType -import com.highcapable.yukihookapi.hook.utils.runBlocking -import com.highcapable.yukihookapi.hook.utils.unit +import com.highcapable.yukihookapi.hook.utils.factory.runBlocking +import com.highcapable.yukihookapi.hook.utils.factory.unit import java.lang.reflect.Member import java.lang.reflect.Method diff --git a/yukihookapi-core/src/main/java/com/highcapable/yukihookapi/hook/core/finder/tools/ReflectionTool.kt b/yukihookapi-core/src/main/java/com/highcapable/yukihookapi/hook/core/finder/tools/ReflectionTool.kt index d5ab09d9..86174864 100644 --- a/yukihookapi-core/src/main/java/com/highcapable/yukihookapi/hook/core/finder/tools/ReflectionTool.kt +++ b/yukihookapi-core/src/main/java/com/highcapable/yukihookapi/hook/core/finder/tools/ReflectionTool.kt @@ -44,7 +44,7 @@ import com.highcapable.yukihookapi.hook.type.java.DalvikBaseDexClassLoader import com.highcapable.yukihookapi.hook.type.java.NoClassDefFoundErrorClass import com.highcapable.yukihookapi.hook.type.java.NoSuchFieldErrorClass import com.highcapable.yukihookapi.hook.type.java.NoSuchMethodErrorClass -import com.highcapable.yukihookapi.hook.utils.* +import com.highcapable.yukihookapi.hook.utils.factory.* import com.highcapable.yukihookapi.hook.xposed.parasitic.AppParasitics import dalvik.system.BaseDexClassLoader import java.lang.reflect.Constructor diff --git a/yukihookapi-core/src/main/java/com/highcapable/yukihookapi/hook/log/LoggerFactory.kt b/yukihookapi-core/src/main/java/com/highcapable/yukihookapi/hook/log/LoggerFactory.kt index 21abd334..4a24a45f 100644 --- a/yukihookapi-core/src/main/java/com/highcapable/yukihookapi/hook/log/LoggerFactory.kt +++ b/yukihookapi-core/src/main/java/com/highcapable/yukihookapi/hook/log/LoggerFactory.kt @@ -34,7 +34,7 @@ import android.util.Log import com.highcapable.yukihookapi.YukiHookAPI import com.highcapable.yukihookapi.hook.core.api.helper.YukiHookHelper import com.highcapable.yukihookapi.hook.factory.current -import com.highcapable.yukihookapi.hook.utils.toStackTrace +import com.highcapable.yukihookapi.hook.utils.factory.toStackTrace import com.highcapable.yukihookapi.hook.xposed.bridge.YukiXposedModule import com.highcapable.yukihookapi.hook.xposed.parasitic.AppParasitics import java.io.File diff --git a/yukihookapi-core/src/main/java/com/highcapable/yukihookapi/hook/param/PackageParam.kt b/yukihookapi-core/src/main/java/com/highcapable/yukihookapi/hook/param/PackageParam.kt index 70499f68..b64817f1 100644 --- a/yukihookapi-core/src/main/java/com/highcapable/yukihookapi/hook/param/PackageParam.kt +++ b/yukihookapi-core/src/main/java/com/highcapable/yukihookapi/hook/param/PackageParam.kt @@ -49,7 +49,7 @@ import com.highcapable.yukihookapi.hook.core.finder.type.factory.ClassConditions import com.highcapable.yukihookapi.hook.entity.YukiBaseHooker import com.highcapable.yukihookapi.hook.log.yLoggerW import com.highcapable.yukihookapi.hook.param.wrapper.PackageParamWrapper -import com.highcapable.yukihookapi.hook.utils.value +import com.highcapable.yukihookapi.hook.utils.factory.value import com.highcapable.yukihookapi.hook.xposed.bridge.YukiXposedModule import com.highcapable.yukihookapi.hook.xposed.bridge.resources.YukiModuleResources import com.highcapable.yukihookapi.hook.xposed.bridge.resources.YukiResources diff --git a/yukihookapi-core/src/main/java/com/highcapable/yukihookapi/hook/utils/factory/ThreadFactory.kt b/yukihookapi-core/src/main/java/com/highcapable/yukihookapi/hook/utils/factory/ThreadFactory.kt new file mode 100644 index 00000000..d827c1b7 --- /dev/null +++ b/yukihookapi-core/src/main/java/com/highcapable/yukihookapi/hook/utils/factory/ThreadFactory.kt @@ -0,0 +1,80 @@ +/* + * YukiHookAPI - An efficient Hook API and Xposed Module solution built in Kotlin. + * Copyright (C) 2019-2023 HighCapable + * https://github.com/fankes/YukiHookAPI + * + * MIT License + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + * + * This file is created by fankes on 2023/9/23. + */ +@file:Suppress("unused") + +package com.highcapable.yukihookapi.hook.utils.factory + +import java.util.concurrent.ExecutorService +import java.util.concurrent.Executors + +/** + * 创建当前线程池服务 + * @return [ExecutorService] + */ +private val currentThreadPool get() = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors()) + +/** + * 启动 [Thread] 延迟等待 [block] 的结果 [T] + * @param delayMs 延迟毫秒 - 默认 1 ms + * @param block 方法块 + * @return [T] + */ +internal inline fun T.await(delayMs: Long = 1, crossinline block: (T) -> Unit): T { + currentThreadPool.apply { + execute { + if (delayMs > 0) Thread.sleep(delayMs) + block(this@await) + shutdown() + } + } + return this +} + +/** + * 计算方法执行耗时 + * @param block 方法块 + * @return [RunBlockResult] + */ +internal inline fun runBlocking(block: () -> R): RunBlockResult { + val start = System.currentTimeMillis() + block() + return RunBlockResult(afterMs = System.currentTimeMillis() - start) +} + +/** + * 构造耗时计算结果类 + * @param afterMs 耗时 + */ +internal class RunBlockResult(internal val afterMs: Long) { + + /** + * 获取耗时计算结果 + * @param result 回调结果 - ([Long] 耗时) + */ + internal inline fun result(result: (Long) -> Unit) = result(afterMs) +} \ No newline at end of file diff --git a/yukihookapi-core/src/main/java/com/highcapable/yukihookapi/hook/utils/factory/ThrowableFactory.kt b/yukihookapi-core/src/main/java/com/highcapable/yukihookapi/hook/utils/factory/ThrowableFactory.kt new file mode 100644 index 00000000..ee30f717 --- /dev/null +++ b/yukihookapi-core/src/main/java/com/highcapable/yukihookapi/hook/utils/factory/ThrowableFactory.kt @@ -0,0 +1,39 @@ +/* + * YukiHookAPI - An efficient Hook API and Xposed Module solution built in Kotlin. + * Copyright (C) 2019-2023 HighCapable + * https://github.com/fankes/YukiHookAPI + * + * MIT License + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + * + * This file is created by fankes on 2023/9/23. + */ +@file:Suppress("unused") + +package com.highcapable.yukihookapi.hook.utils.factory + +import java.io.ByteArrayOutputStream +import java.io.PrintStream + +/** + * 获取完整的异常堆栈内容 + * @return [String] + */ +internal fun Throwable.toStackTrace() = ByteArrayOutputStream().also { printStackTrace(PrintStream(it)) }.toString() \ No newline at end of file diff --git a/yukihookapi-core/src/main/java/com/highcapable/yukihookapi/hook/utils/UtilsFactory.kt b/yukihookapi-core/src/main/java/com/highcapable/yukihookapi/hook/utils/factory/VariableFactory.kt similarity index 78% rename from yukihookapi-core/src/main/java/com/highcapable/yukihookapi/hook/utils/UtilsFactory.kt rename to yukihookapi-core/src/main/java/com/highcapable/yukihookapi/hook/utils/factory/VariableFactory.kt index 1ef82ecc..9d9770b8 100644 --- a/yukihookapi-core/src/main/java/com/highcapable/yukihookapi/hook/utils/UtilsFactory.kt +++ b/yukihookapi-core/src/main/java/com/highcapable/yukihookapi/hook/utils/factory/VariableFactory.kt @@ -23,22 +23,11 @@ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. * - * This file is created by fankes on 2022/2/5. + * This file is created by fankes on 2023/9/23. */ @file:Suppress("unused") -package com.highcapable.yukihookapi.hook.utils - -import java.io.ByteArrayOutputStream -import java.io.PrintStream -import java.util.concurrent.ExecutorService -import java.util.concurrent.Executors - -/** - * 创建当前线程池服务 - * @return [ExecutorService] - */ -private val currentThreadPool get() = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors()) +package com.highcapable.yukihookapi.hook.utils.factory /** * 对 [T] 返回无返回值的 [Unit] @@ -46,23 +35,6 @@ private val currentThreadPool get() = Executors.newFixedThreadPool(Runtime.getRu */ internal fun T?.unit() = let {} -/** - * 启动 [Thread] 延迟等待 [block] 的结果 [T] - * @param delayMs 延迟毫秒 - 默认 1 ms - * @param block 方法块 - * @return [T] - */ -internal inline fun T.await(delayMs: Long = 1, crossinline block: (T) -> Unit): T { - currentThreadPool.apply { - execute { - if (delayMs > 0) Thread.sleep(delayMs) - block(this@await) - shutdown() - } - } - return this -} - /** * 获取数组内容依次列出的字符串表示 * @return [String] @@ -116,36 +88,6 @@ internal inline fun runOrTrue(block: () -> Boolean) = runCatching { block() }.ge */ internal inline fun runOrFalse(block: () -> Boolean) = runCatching { block() }.getOrNull() ?: false -/** - * 获取完整的异常堆栈内容 - * @return [String] - */ -internal fun Throwable.toStackTrace() = ByteArrayOutputStream().also { printStackTrace(PrintStream(it)) }.toString() - -/** - * 计算方法执行耗时 - * @param block 方法块 - * @return [RunBlockResult] - */ -internal inline fun runBlocking(block: () -> R): RunBlockResult { - val start = System.currentTimeMillis() - block() - return RunBlockResult(afterMs = System.currentTimeMillis() - start) -} - -/** - * 构造耗时计算结果类 - * @param afterMs 耗时 - */ -internal class RunBlockResult(internal val afterMs: Long) { - - /** - * 获取耗时计算结果 - * @param result 回调结果 - ([Long] 耗时) - */ - internal inline fun result(result: (Long) -> Unit) = result(afterMs) -} - /** * 创建多项条件判断 - 条件对象 [T] * @param initiate 方法体 diff --git a/yukihookapi-core/src/main/java/com/highcapable/yukihookapi/hook/xposed/channel/YukiHookDataChannel.kt b/yukihookapi-core/src/main/java/com/highcapable/yukihookapi/hook/xposed/channel/YukiHookDataChannel.kt index fbb86a68..74244a7e 100644 --- a/yukihookapi-core/src/main/java/com/highcapable/yukihookapi/hook/xposed/channel/YukiHookDataChannel.kt +++ b/yukihookapi-core/src/main/java/com/highcapable/yukihookapi/hook/xposed/channel/YukiHookDataChannel.kt @@ -49,7 +49,7 @@ import com.highcapable.yukihookapi.hook.log.YukiHookLogger import com.highcapable.yukihookapi.hook.log.YukiLoggerData import com.highcapable.yukihookapi.hook.log.yLoggerE import com.highcapable.yukihookapi.hook.log.yLoggerW -import com.highcapable.yukihookapi.hook.utils.RandomSeed +import com.highcapable.yukihookapi.hook.utils.factory.RandomSeed import com.highcapable.yukihookapi.hook.xposed.application.ModuleApplication import com.highcapable.yukihookapi.hook.xposed.bridge.YukiXposedModule import com.highcapable.yukihookapi.hook.xposed.channel.data.ChannelData diff --git a/yukihookapi-core/src/main/java/com/highcapable/yukihookapi/hook/xposed/prefs/ui/ModulePreferenceFragment.kt b/yukihookapi-core/src/main/java/com/highcapable/yukihookapi/hook/xposed/prefs/ui/ModulePreferenceFragment.kt index 5085f58a..e03a21b8 100644 --- a/yukihookapi-core/src/main/java/com/highcapable/yukihookapi/hook/xposed/prefs/ui/ModulePreferenceFragment.kt +++ b/yukihookapi-core/src/main/java/com/highcapable/yukihookapi/hook/xposed/prefs/ui/ModulePreferenceFragment.kt @@ -37,7 +37,7 @@ import androidx.preference.PreferenceFragmentCompat import androidx.preference.PreferenceManager import androidx.preference.PreferenceScreen import com.highcapable.yukihookapi.YukiHookAPI -import com.highcapable.yukihookapi.hook.utils.unit +import com.highcapable.yukihookapi.hook.utils.factory.unit import com.highcapable.yukihookapi.hook.xposed.prefs.YukiHookPrefsBridge /**