diff --git a/yukihookapi/src/api/kotlin/com/highcapable/yukihookapi/hook/core/YukiMemberHookCreater.kt b/yukihookapi/src/api/kotlin/com/highcapable/yukihookapi/hook/core/YukiMemberHookCreater.kt index 0751329f..3a36b6d1 100644 --- a/yukihookapi/src/api/kotlin/com/highcapable/yukihookapi/hook/core/YukiMemberHookCreater.kt +++ b/yukihookapi/src/api/kotlin/com/highcapable/yukihookapi/hook/core/YukiMemberHookCreater.kt @@ -29,7 +29,6 @@ package com.highcapable.yukihookapi.hook.core -import android.os.SystemClock import com.highcapable.yukihookapi.YukiHookAPI import com.highcapable.yukihookapi.hook.bean.HookClass import com.highcapable.yukihookapi.hook.core.finder.ConstructorFinder @@ -46,6 +45,7 @@ import com.highcapable.yukihookapi.hook.param.PackageParam import com.highcapable.yukihookapi.hook.param.type.HookEntryType import com.highcapable.yukihookapi.hook.param.wrapper.HookParamWrapper import com.highcapable.yukihookapi.hook.type.java.* +import com.highcapable.yukihookapi.hook.utils.await import com.highcapable.yukihookapi.hook.xposed.bridge.YukiHookBridge import com.highcapable.yukihookapi.hook.xposed.bridge.factory.YukiHookHelper import com.highcapable.yukihookapi.hook.xposed.bridge.factory.YukiHookPriority @@ -115,22 +115,18 @@ class YukiMemberHookCreater(@PublishedApi internal val packageParam: PackagePara /** 过滤 [HookEntryType.ZYGOTE] 与 [HookEntryType.PACKAGE] 或 [HookParam.isCallbackCalled] 已被执行 */ packageParam.wrapper?.type == HookEntryType.RESOURCES && HookParam.isCallbackCalled.not() -> Result() preHookMembers.isEmpty() -> error("Hook Members is empty, hook aborted") - else -> Result().also { + else -> Result().await { warnTerribleHookClass() - Thread { - /** 延迟使得方法取到返回值 */ - SystemClock.sleep(1) - when { - isDisableCreaterRunHook.not() && hookClass.instance != null -> { - it.onPrepareHook?.invoke() - preHookMembers.forEach { (_, m) -> m.hook() } - } - isDisableCreaterRunHook.not() && hookClass.instance == null -> - if (onHookClassNotFoundFailureCallback == null) - yLoggerE(msg = "HookClass [${hookClass.name}] not found", e = hookClass.throwable) - else onHookClassNotFoundFailureCallback?.invoke(hookClass.throwable ?: Throwable("[${hookClass.name}] not found")) + when { + isDisableCreaterRunHook.not() && hookClass.instance != null -> { + it.onPrepareHook?.invoke() + preHookMembers.forEach { (_, m) -> m.hook() } } - }.start() + isDisableCreaterRunHook.not() && hookClass.instance == null -> + if (onHookClassNotFoundFailureCallback == null) + yLoggerE(msg = "HookClass [${hookClass.name}] not found", e = hookClass.throwable) + else onHookClassNotFoundFailureCallback?.invoke(hookClass.throwable ?: Throwable("[${hookClass.name}] not found")) + } } } 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 91fa5957..6092d1e5 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 @@ -29,7 +29,6 @@ package com.highcapable.yukihookapi.hook.core.finder -import android.os.SystemClock import com.highcapable.yukihookapi.annotation.YukiPrivateApi import com.highcapable.yukihookapi.hook.bean.VariousClass import com.highcapable.yukihookapi.hook.core.YukiMemberHookCreater @@ -39,6 +38,7 @@ import com.highcapable.yukihookapi.hook.core.finder.type.NameConditions import com.highcapable.yukihookapi.hook.core.reflex.tools.ReflectionTool import com.highcapable.yukihookapi.hook.factory.hasExtends import com.highcapable.yukihookapi.hook.log.yLoggerW +import com.highcapable.yukihookapi.hook.utils.await import com.highcapable.yukihookapi.hook.utils.runBlocking import java.lang.reflect.Field @@ -203,12 +203,7 @@ class FieldFinder @PublishedApi internal constructor( Result() } else Result(isNoSuch = true, Throwable("classSet is null")) } catch (e: Throwable) { - Thread { - /** 延迟使得方法取到返回值 */ - SystemClock.sleep(1) - onFailureMsg(throwable = e) - }.start() - Result(isNoSuch = true, e) + Result(isNoSuch = true, e).await { onFailureMsg(throwable = e) } } /** diff --git a/yukihookapi/src/api/kotlin/com/highcapable/yukihookapi/hook/core/finder/base/BaseFinder.kt b/yukihookapi/src/api/kotlin/com/highcapable/yukihookapi/hook/core/finder/base/BaseFinder.kt index 3b941aba..d987eb01 100644 --- a/yukihookapi/src/api/kotlin/com/highcapable/yukihookapi/hook/core/finder/base/BaseFinder.kt +++ b/yukihookapi/src/api/kotlin/com/highcapable/yukihookapi/hook/core/finder/base/BaseFinder.kt @@ -27,7 +27,6 @@ */ package com.highcapable.yukihookapi.hook.core.finder.base -import android.os.SystemClock import com.highcapable.yukihookapi.YukiHookAPI import com.highcapable.yukihookapi.annotation.YukiPrivateApi import com.highcapable.yukihookapi.hook.bean.VariousClass @@ -36,6 +35,8 @@ import com.highcapable.yukihookapi.hook.factory.classOf import com.highcapable.yukihookapi.hook.log.yLoggerE import com.highcapable.yukihookapi.hook.log.yLoggerI import com.highcapable.yukihookapi.hook.type.defined.UndefinedType +import com.highcapable.yukihookapi.hook.utils.await +import com.highcapable.yukihookapi.hook.utils.unit import com.highcapable.yukihookapi.hook.xposed.bridge.YukiHookBridge import java.lang.reflect.Constructor import java.lang.reflect.Field @@ -194,18 +195,10 @@ abstract class BaseFinder internal constructor( loggingContent = Pair(msg, throwable) } /** 判断绑定到 Hooker 时仅创建日志 */ - if (isBindToHooker) return Thread { - /** 延迟使得方法取到返回值 */ - SystemClock.sleep(1) - build() - }.start() + if (isBindToHooker) return await { build() }.unit() /** 判断始终输出日志或等待结果后输出日志 */ if (isAlwaysPrint) build().run { printLogIfExist() } - else Thread { - /** 延迟使得方法取到返回值 */ - SystemClock.sleep(1) - build().run { printLogIfExist() } - }.start() + else await { build().run { printLogIfExist() } } } /** 存在日志时输出日志 */ diff --git a/yukihookapi/src/api/kotlin/com/highcapable/yukihookapi/hook/utils/UtilsFactory.kt b/yukihookapi/src/api/kotlin/com/highcapable/yukihookapi/hook/utils/UtilsFactory.kt index 38ab9443..9d31f8b0 100644 --- a/yukihookapi/src/api/kotlin/com/highcapable/yukihookapi/hook/utils/UtilsFactory.kt +++ b/yukihookapi/src/api/kotlin/com/highcapable/yukihookapi/hook/utils/UtilsFactory.kt @@ -27,17 +27,45 @@ */ package com.highcapable.yukihookapi.hook.utils -import com.highcapable.yukihookapi.annotation.YukiPrivateApi +import java.util.concurrent.ExecutorService import java.util.concurrent.Executors import java.util.concurrent.TimeUnit +/** + * 创建当前线程池服务 + * @return [ExecutorService] + */ +private val currentThreadPool get() = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors()) + +/** + * 对 [T] 返回无返回值的 [Unit] + * @return [Unit] + */ +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 +} + /** * 进行一次并行计算的 ForEach 操作 * @param action 回调内容方法体 */ -@YukiPrivateApi -inline fun Iterable.parallelForEach(crossinline action: (T) -> Unit) { - Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors()).apply { +internal inline fun Iterable.parallelForEach(crossinline action: (T) -> Unit) { + currentThreadPool.apply { val iterator = iterator() while (iterator.hasNext()) { val item = iterator.next() @@ -52,8 +80,7 @@ inline fun Iterable.parallelForEach(crossinline action: (T) -> Unit) { * 获取数组内容依次列出的字符串表示 * @return [String] */ -@YukiPrivateApi -inline fun Array.value() = if (isNotEmpty()) { +internal inline fun Array.value() = if (isNotEmpty()) { var value = "" forEach { value += it.toString() + ", " } "[${value.trim().let { it.substring(0, it.lastIndex) }}]" @@ -64,8 +91,7 @@ inline fun Array.value() = if (isNotEmpty()) { * @param block 方法块 * @return [RunBlockResult] */ -@YukiPrivateApi -inline fun runBlocking(block: () -> R): RunBlockResult { +internal inline fun runBlocking(block: () -> R): RunBlockResult { val start = System.currentTimeMillis() block() return RunBlockResult(afterMs = System.currentTimeMillis() - start) @@ -75,13 +101,11 @@ inline fun runBlocking(block: () -> R): RunBlockResult { * 构造耗时计算结果类 * @param afterMs 耗时 */ -@YukiPrivateApi -class RunBlockResult(@PublishedApi internal val afterMs: Long) { +internal class RunBlockResult(internal val afterMs: Long) { /** * 获取耗时计算结果 * @param result 回调结果 - ([Long] 耗时) */ - @YukiPrivateApi - inline fun result(result: (Long) -> Unit) = result(afterMs) + internal inline fun result(result: (Long) -> Unit) = result(afterMs) } \ No newline at end of file