mirror of
https://github.com/HighCapable/YukiHookAPI.git
synced 2025-09-04 09:45:19 +08:00
Modify change Thread to await function and change self expanded function to internal
This commit is contained in:
@@ -29,7 +29,6 @@
|
|||||||
|
|
||||||
package com.highcapable.yukihookapi.hook.core
|
package com.highcapable.yukihookapi.hook.core
|
||||||
|
|
||||||
import android.os.SystemClock
|
|
||||||
import com.highcapable.yukihookapi.YukiHookAPI
|
import com.highcapable.yukihookapi.YukiHookAPI
|
||||||
import com.highcapable.yukihookapi.hook.bean.HookClass
|
import com.highcapable.yukihookapi.hook.bean.HookClass
|
||||||
import com.highcapable.yukihookapi.hook.core.finder.ConstructorFinder
|
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.type.HookEntryType
|
||||||
import com.highcapable.yukihookapi.hook.param.wrapper.HookParamWrapper
|
import com.highcapable.yukihookapi.hook.param.wrapper.HookParamWrapper
|
||||||
import com.highcapable.yukihookapi.hook.type.java.*
|
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.YukiHookBridge
|
||||||
import com.highcapable.yukihookapi.hook.xposed.bridge.factory.YukiHookHelper
|
import com.highcapable.yukihookapi.hook.xposed.bridge.factory.YukiHookHelper
|
||||||
import com.highcapable.yukihookapi.hook.xposed.bridge.factory.YukiHookPriority
|
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] 已被执行 */
|
/** 过滤 [HookEntryType.ZYGOTE] 与 [HookEntryType.PACKAGE] 或 [HookParam.isCallbackCalled] 已被执行 */
|
||||||
packageParam.wrapper?.type == HookEntryType.RESOURCES && HookParam.isCallbackCalled.not() -> Result()
|
packageParam.wrapper?.type == HookEntryType.RESOURCES && HookParam.isCallbackCalled.not() -> Result()
|
||||||
preHookMembers.isEmpty() -> error("Hook Members is empty, hook aborted")
|
preHookMembers.isEmpty() -> error("Hook Members is empty, hook aborted")
|
||||||
else -> Result().also {
|
else -> Result().await {
|
||||||
warnTerribleHookClass()
|
warnTerribleHookClass()
|
||||||
Thread {
|
when {
|
||||||
/** 延迟使得方法取到返回值 */
|
isDisableCreaterRunHook.not() && hookClass.instance != null -> {
|
||||||
SystemClock.sleep(1)
|
it.onPrepareHook?.invoke()
|
||||||
when {
|
preHookMembers.forEach { (_, m) -> m.hook() }
|
||||||
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"))
|
|
||||||
}
|
}
|
||||||
}.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"))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -29,7 +29,6 @@
|
|||||||
|
|
||||||
package com.highcapable.yukihookapi.hook.core.finder
|
package com.highcapable.yukihookapi.hook.core.finder
|
||||||
|
|
||||||
import android.os.SystemClock
|
|
||||||
import com.highcapable.yukihookapi.annotation.YukiPrivateApi
|
import com.highcapable.yukihookapi.annotation.YukiPrivateApi
|
||||||
import com.highcapable.yukihookapi.hook.bean.VariousClass
|
import com.highcapable.yukihookapi.hook.bean.VariousClass
|
||||||
import com.highcapable.yukihookapi.hook.core.YukiMemberHookCreater
|
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.core.reflex.tools.ReflectionTool
|
||||||
import com.highcapable.yukihookapi.hook.factory.hasExtends
|
import com.highcapable.yukihookapi.hook.factory.hasExtends
|
||||||
import com.highcapable.yukihookapi.hook.log.yLoggerW
|
import com.highcapable.yukihookapi.hook.log.yLoggerW
|
||||||
|
import com.highcapable.yukihookapi.hook.utils.await
|
||||||
import com.highcapable.yukihookapi.hook.utils.runBlocking
|
import com.highcapable.yukihookapi.hook.utils.runBlocking
|
||||||
import java.lang.reflect.Field
|
import java.lang.reflect.Field
|
||||||
|
|
||||||
@@ -203,12 +203,7 @@ class FieldFinder @PublishedApi internal constructor(
|
|||||||
Result()
|
Result()
|
||||||
} else Result(isNoSuch = true, Throwable("classSet is null"))
|
} else Result(isNoSuch = true, Throwable("classSet is null"))
|
||||||
} catch (e: Throwable) {
|
} catch (e: Throwable) {
|
||||||
Thread {
|
Result(isNoSuch = true, e).await { onFailureMsg(throwable = e) }
|
||||||
/** 延迟使得方法取到返回值 */
|
|
||||||
SystemClock.sleep(1)
|
|
||||||
onFailureMsg(throwable = e)
|
|
||||||
}.start()
|
|
||||||
Result(isNoSuch = true, e)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@@ -27,7 +27,6 @@
|
|||||||
*/
|
*/
|
||||||
package com.highcapable.yukihookapi.hook.core.finder.base
|
package com.highcapable.yukihookapi.hook.core.finder.base
|
||||||
|
|
||||||
import android.os.SystemClock
|
|
||||||
import com.highcapable.yukihookapi.YukiHookAPI
|
import com.highcapable.yukihookapi.YukiHookAPI
|
||||||
import com.highcapable.yukihookapi.annotation.YukiPrivateApi
|
import com.highcapable.yukihookapi.annotation.YukiPrivateApi
|
||||||
import com.highcapable.yukihookapi.hook.bean.VariousClass
|
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.yLoggerE
|
||||||
import com.highcapable.yukihookapi.hook.log.yLoggerI
|
import com.highcapable.yukihookapi.hook.log.yLoggerI
|
||||||
import com.highcapable.yukihookapi.hook.type.defined.UndefinedType
|
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 com.highcapable.yukihookapi.hook.xposed.bridge.YukiHookBridge
|
||||||
import java.lang.reflect.Constructor
|
import java.lang.reflect.Constructor
|
||||||
import java.lang.reflect.Field
|
import java.lang.reflect.Field
|
||||||
@@ -194,18 +195,10 @@ abstract class BaseFinder internal constructor(
|
|||||||
loggingContent = Pair(msg, throwable)
|
loggingContent = Pair(msg, throwable)
|
||||||
}
|
}
|
||||||
/** 判断绑定到 Hooker 时仅创建日志 */
|
/** 判断绑定到 Hooker 时仅创建日志 */
|
||||||
if (isBindToHooker) return Thread {
|
if (isBindToHooker) return await { build() }.unit()
|
||||||
/** 延迟使得方法取到返回值 */
|
|
||||||
SystemClock.sleep(1)
|
|
||||||
build()
|
|
||||||
}.start()
|
|
||||||
/** 判断始终输出日志或等待结果后输出日志 */
|
/** 判断始终输出日志或等待结果后输出日志 */
|
||||||
if (isAlwaysPrint) build().run { printLogIfExist() }
|
if (isAlwaysPrint) build().run { printLogIfExist() }
|
||||||
else Thread {
|
else await { build().run { printLogIfExist() } }
|
||||||
/** 延迟使得方法取到返回值 */
|
|
||||||
SystemClock.sleep(1)
|
|
||||||
build().run { printLogIfExist() }
|
|
||||||
}.start()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/** 存在日志时输出日志 */
|
/** 存在日志时输出日志 */
|
||||||
|
@@ -27,17 +27,45 @@
|
|||||||
*/
|
*/
|
||||||
package com.highcapable.yukihookapi.hook.utils
|
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.Executors
|
||||||
import java.util.concurrent.TimeUnit
|
import java.util.concurrent.TimeUnit
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 创建当前线程池服务
|
||||||
|
* @return [ExecutorService]
|
||||||
|
*/
|
||||||
|
private val currentThreadPool get() = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors())
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 对 [T] 返回无返回值的 [Unit]
|
||||||
|
* @return [Unit]
|
||||||
|
*/
|
||||||
|
internal fun <T> T?.unit() = let {}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 启动 [Thread] 延迟等待 [block] 的结果 [T]
|
||||||
|
* @param delayMs 延迟毫秒 - 默认 1 ms
|
||||||
|
* @param block 方法块
|
||||||
|
* @return [T]
|
||||||
|
*/
|
||||||
|
internal inline fun <T> 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 操作
|
* 进行一次并行计算的 ForEach 操作
|
||||||
* @param action 回调内容方法体
|
* @param action 回调内容方法体
|
||||||
*/
|
*/
|
||||||
@YukiPrivateApi
|
internal inline fun <T> Iterable<T>.parallelForEach(crossinline action: (T) -> Unit) {
|
||||||
inline fun <T> Iterable<T>.parallelForEach(crossinline action: (T) -> Unit) {
|
currentThreadPool.apply {
|
||||||
Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors()).apply {
|
|
||||||
val iterator = iterator()
|
val iterator = iterator()
|
||||||
while (iterator.hasNext()) {
|
while (iterator.hasNext()) {
|
||||||
val item = iterator.next()
|
val item = iterator.next()
|
||||||
@@ -52,8 +80,7 @@ inline fun <T> Iterable<T>.parallelForEach(crossinline action: (T) -> Unit) {
|
|||||||
* 获取数组内容依次列出的字符串表示
|
* 获取数组内容依次列出的字符串表示
|
||||||
* @return [String]
|
* @return [String]
|
||||||
*/
|
*/
|
||||||
@YukiPrivateApi
|
internal inline fun <reified T> Array<out T>.value() = if (isNotEmpty()) {
|
||||||
inline fun <reified T> Array<out T>.value() = if (isNotEmpty()) {
|
|
||||||
var value = ""
|
var value = ""
|
||||||
forEach { value += it.toString() + ", " }
|
forEach { value += it.toString() + ", " }
|
||||||
"[${value.trim().let { it.substring(0, it.lastIndex) }}]"
|
"[${value.trim().let { it.substring(0, it.lastIndex) }}]"
|
||||||
@@ -64,8 +91,7 @@ inline fun <reified T> Array<out T>.value() = if (isNotEmpty()) {
|
|||||||
* @param block 方法块
|
* @param block 方法块
|
||||||
* @return [RunBlockResult]
|
* @return [RunBlockResult]
|
||||||
*/
|
*/
|
||||||
@YukiPrivateApi
|
internal inline fun <R> runBlocking(block: () -> R): RunBlockResult {
|
||||||
inline fun <R> runBlocking(block: () -> R): RunBlockResult {
|
|
||||||
val start = System.currentTimeMillis()
|
val start = System.currentTimeMillis()
|
||||||
block()
|
block()
|
||||||
return RunBlockResult(afterMs = System.currentTimeMillis() - start)
|
return RunBlockResult(afterMs = System.currentTimeMillis() - start)
|
||||||
@@ -75,13 +101,11 @@ inline fun <R> runBlocking(block: () -> R): RunBlockResult {
|
|||||||
* 构造耗时计算结果类
|
* 构造耗时计算结果类
|
||||||
* @param afterMs 耗时
|
* @param afterMs 耗时
|
||||||
*/
|
*/
|
||||||
@YukiPrivateApi
|
internal class RunBlockResult(internal val afterMs: Long) {
|
||||||
class RunBlockResult(@PublishedApi internal val afterMs: Long) {
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 获取耗时计算结果
|
* 获取耗时计算结果
|
||||||
* @param result 回调结果 - ([Long] 耗时)
|
* @param result 回调结果 - ([Long] 耗时)
|
||||||
*/
|
*/
|
||||||
@YukiPrivateApi
|
internal inline fun result(result: (Long) -> Unit) = result(afterMs)
|
||||||
inline fun result(result: (Long) -> Unit) = result(afterMs)
|
|
||||||
}
|
}
|
Reference in New Issue
Block a user