mirror of
https://github.com/HighCapable/YukiHookAPI.git
synced 2025-09-04 09:45:19 +08:00
Fix bug in YukiMemberHookCreater.kt and added packageName in global log
This commit is contained in:
@@ -35,6 +35,7 @@ import com.highcapable.yukihookapi.hook.bean.HookClass
|
||||
import com.highcapable.yukihookapi.hook.core.finder.ConstructorFinder
|
||||
import com.highcapable.yukihookapi.hook.core.finder.FieldFinder
|
||||
import com.highcapable.yukihookapi.hook.core.finder.MethodFinder
|
||||
import com.highcapable.yukihookapi.hook.core.finder.base.BaseFinder
|
||||
import com.highcapable.yukihookapi.hook.log.yLoggerE
|
||||
import com.highcapable.yukihookapi.hook.log.yLoggerI
|
||||
import com.highcapable.yukihookapi.hook.param.HookParam
|
||||
@@ -52,7 +53,7 @@ import java.lang.reflect.Member
|
||||
* @param packageParam 需要传入 [PackageParam] 实现方法调用
|
||||
* @param hookClass 要 Hook 的 [HookClass] 实例
|
||||
*/
|
||||
class YukiMemberHookCreater(private val packageParam: PackageParam, @PublishedApi internal val hookClass: HookClass) {
|
||||
class YukiMemberHookCreater(@PublishedApi internal val packageParam: PackageParam, @PublishedApi internal val hookClass: HookClass) {
|
||||
|
||||
/** 默认 Hook 回调优先级 */
|
||||
val PRIORITY_DEFAULT = 50
|
||||
@@ -98,7 +99,7 @@ class YukiMemberHookCreater(private val packageParam: PackageParam, @PublishedAp
|
||||
* @return [MemberHookCreater.Result]
|
||||
*/
|
||||
inline fun injectMember(priority: Int = PRIORITY_DEFAULT, tag: String = "Default", initiate: MemberHookCreater.() -> Unit) =
|
||||
MemberHookCreater(priority, tag).apply(initiate).apply { preHookMembers.add(this) }.build()
|
||||
MemberHookCreater(priority, tag, packageParam.packageName).apply(initiate).apply { preHookMembers.add(this) }.build()
|
||||
|
||||
/**
|
||||
* Hook 执行入口
|
||||
@@ -122,7 +123,7 @@ class YukiMemberHookCreater(private val packageParam: PackageParam, @PublishedAp
|
||||
}
|
||||
isDisableCreaterRunHook.not() && hookClass.instance == null ->
|
||||
if (onHookClassNotFoundFailureCallback == null)
|
||||
yLoggerE(msg = "HookClass [${hookClass.name}] not found", e = hookClass.throwable)
|
||||
yLoggerE(msg = "[${packageParam.packageName}] HookClass [${hookClass.name}] not found", e = hookClass.throwable)
|
||||
else onHookClassNotFoundFailureCallback?.invoke(hookClass.throwable ?: Throwable("[${hookClass.name}] not found"))
|
||||
}
|
||||
}.start()
|
||||
@@ -135,8 +136,9 @@ class YukiMemberHookCreater(private val packageParam: PackageParam, @PublishedAp
|
||||
* 查找和处理需要 Hook 的方法、构造类
|
||||
* @param priority Hook 优先级
|
||||
* @param tag 当前设置的标签
|
||||
* @param packageName 当前 Hook 的 APP 包名
|
||||
*/
|
||||
inner class MemberHookCreater(private val priority: Int, internal val tag: String) {
|
||||
inner class MemberHookCreater(private val priority: Int, internal val tag: String, internal val packageName: String) {
|
||||
|
||||
/** [beforeHook] 回调 */
|
||||
private var beforeHookCallback: (HookParam.() -> Unit)? = null
|
||||
@@ -187,6 +189,10 @@ class YukiMemberHookCreater(private val packageParam: PackageParam, @PublishedAp
|
||||
@PublishedApi
|
||||
internal var hookMemberMode = HookMemberMode.HOOK_CONVENTIONAL
|
||||
|
||||
/** 当前的查找实例 */
|
||||
@PublishedApi
|
||||
internal var finder: BaseFinder? = null
|
||||
|
||||
/** 全部方法的名称 */
|
||||
private var allMethodsName = ""
|
||||
|
||||
@@ -244,7 +250,7 @@ class YukiMemberHookCreater(private val packageParam: PackageParam, @PublishedAp
|
||||
inline fun method(initiate: MethodFinder.() -> Unit) = try {
|
||||
hookMemberMode = HookMemberMode.HOOK_CONVENTIONAL
|
||||
isHookMemberSetup = true
|
||||
MethodFinder(hookInstance = this, hookClass.instance).apply(initiate).build(isBind = true)
|
||||
MethodFinder(hookInstance = this, hookClass.instance).apply(initiate).apply { finder = this }.build(isBind = true)
|
||||
} catch (e: Throwable) {
|
||||
findingThrowable = e
|
||||
MethodFinder(hookInstance = this).failure(e)
|
||||
@@ -262,7 +268,7 @@ class YukiMemberHookCreater(private val packageParam: PackageParam, @PublishedAp
|
||||
inline fun constructor(initiate: ConstructorFinder.() -> Unit = {}) = try {
|
||||
hookMemberMode = HookMemberMode.HOOK_CONVENTIONAL
|
||||
isHookMemberSetup = true
|
||||
ConstructorFinder(hookInstance = this, hookClass.instance).apply(initiate).build(isBind = true)
|
||||
ConstructorFinder(hookInstance = this, hookClass.instance).apply(initiate).apply { finder = this }.build(isBind = true)
|
||||
} catch (e: Throwable) {
|
||||
findingThrowable = e
|
||||
ConstructorFinder(hookInstance = this).failure(e)
|
||||
@@ -403,6 +409,7 @@ class YukiMemberHookCreater(private val packageParam: PackageParam, @PublishedAp
|
||||
@PublishedApi
|
||||
internal fun hook() {
|
||||
if (YukiHookBridge.hasXposedBridge.not() || isDisableMemberRunHook) return
|
||||
finder?.printLogIfExist()
|
||||
if (hookClass.instance == null) {
|
||||
(hookClass.throwable ?: Throwable("HookClass [${hookClass.name}] not found")).also {
|
||||
onHookingFailureCallback?.invoke(it)
|
||||
@@ -487,13 +494,12 @@ class YukiMemberHookCreater(private val packageParam: PackageParam, @PublishedAp
|
||||
onNoSuchMemberFailureCallback?.invoke(it)
|
||||
onHookingFailureCallback?.invoke(it)
|
||||
onAllFailureCallback?.invoke(it)
|
||||
if (isNotIgnoredNoSuchMemberFailure)
|
||||
yLoggerE(
|
||||
msg = if (isHookMemberSetup)
|
||||
"Hooked Member with a finding error by $hookClass [$tag]"
|
||||
else "Hooked Member cannot be non-null by $hookClass [$tag]",
|
||||
e = findingThrowable ?: it
|
||||
)
|
||||
if (isNotIgnoredNoSuchMemberFailure) yLoggerE(
|
||||
msg = "[$packageName] " + (if (isHookMemberSetup)
|
||||
"Hooked Member with a finding error by $hookClass [$tag]"
|
||||
else "Hooked Member cannot be non-null by $hookClass [$tag]"),
|
||||
e = findingThrowable ?: it
|
||||
)
|
||||
}
|
||||
else runCatching {
|
||||
when (hookMemberMode) {
|
||||
@@ -525,7 +531,7 @@ class YukiMemberHookCreater(private val packageParam: PackageParam, @PublishedAp
|
||||
if (isMemberNotFound) onNoSuchMemberFailureCallback?.invoke(it)
|
||||
onAllFailureCallback?.invoke(it)
|
||||
if ((isNotIgnoredHookingFailure && isMemberNotFound.not()) || (isNotIgnoredNoSuchMemberFailure && isMemberNotFound))
|
||||
yLoggerE(msg = "Hooked All Members with an error in Class [$hookClass] [$tag]", e = it)
|
||||
yLoggerE(msg = "[$packageName] Hooked All Members with an error in Class [$hookClass] [$tag]", e = it)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -534,7 +540,7 @@ class YukiMemberHookCreater(private val packageParam: PackageParam, @PublishedAp
|
||||
* @param msg 调试日志内容
|
||||
*/
|
||||
private fun onHookLogMsg(msg: String) {
|
||||
if (YukiHookAPI.Configs.isDebug) yLoggerI(msg = "[${packageParam.packageName}] $msg")
|
||||
if (YukiHookAPI.Configs.isDebug) yLoggerI(msg = "[$packageName] $msg")
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -542,7 +548,7 @@ class YukiMemberHookCreater(private val packageParam: PackageParam, @PublishedAp
|
||||
* @param throwable 异常信息
|
||||
*/
|
||||
private fun onHookFailureMsg(throwable: Throwable) =
|
||||
yLoggerE(msg = "Try to hook ${hookClass.instance ?: hookClass.name}[$member] got an Exception [$tag]", e = throwable)
|
||||
yLoggerE(msg = "[$packageName] Try to hook ${hookClass.instance ?: hookClass.name}[$member] got an Exception [$tag]", e = throwable)
|
||||
|
||||
/**
|
||||
* 判断是否没有设置 Hook 过程中的任何异常拦截
|
||||
|
@@ -61,9 +61,6 @@ class ConstructorFinder(
|
||||
/** 是否在未找到后继续在当前 [classSet] 的父类中查找 */
|
||||
private var isFindInSuperClass = false
|
||||
|
||||
/** 是否将结果设置到目标 [YukiMemberHookCreater.MemberHookCreater] */
|
||||
private var isBindToHooker = false
|
||||
|
||||
/** 当前重查找结果回调 */
|
||||
private var remedyPlansCallback: (() -> Unit)? = null
|
||||
|
||||
|
@@ -61,9 +61,6 @@ class MethodFinder(
|
||||
/** 是否在未找到后继续在当前 [classSet] 的父类中查找 */
|
||||
private var isFindInSuperClass = false
|
||||
|
||||
/** 是否将结果设置到目标 [YukiMemberHookCreater.MemberHookCreater] */
|
||||
private var isBindToHooker = false
|
||||
|
||||
/** 当前重查找结果回调 */
|
||||
private var remedyPlansCallback: (() -> Unit)? = null
|
||||
|
||||
|
@@ -120,6 +120,9 @@ abstract class BaseFinder(
|
||||
@PublishedApi
|
||||
internal var isUsingRemedyPlan = false
|
||||
|
||||
/** 是否将结果设置到目标 [YukiMemberHookCreater.MemberHookCreater] */
|
||||
internal var isBindToHooker = false
|
||||
|
||||
/** 是否开启忽略错误警告功能 */
|
||||
internal var isShutErrorPrinting = false
|
||||
|
||||
@@ -138,6 +141,9 @@ abstract class BaseFinder(
|
||||
*/
|
||||
internal val isNotIgnoredNoSuchMemberFailure get() = hookInstance?.isNotIgnoredNoSuchMemberFailure ?: true
|
||||
|
||||
/** 需要输出的日志内容 */
|
||||
private var loggingContent: Pair<String, Throwable?>? = null
|
||||
|
||||
/**
|
||||
* 将目标类型转换为可识别的兼容类型
|
||||
* @return [Class] or null
|
||||
@@ -157,21 +163,41 @@ abstract class BaseFinder(
|
||||
* @param isAlwaysPrint 忽略条件每次都打印错误
|
||||
*/
|
||||
internal fun onFailureMsg(msg: String = "", throwable: Throwable? = null, isAlwaysPrint: Boolean = false) {
|
||||
fun print() = yLoggerE(msg = "NoSuch$tag happend in [$classSet] $msg [${hookTag}]", e = throwable)
|
||||
if (isAlwaysPrint) print()
|
||||
/** 创建日志 */
|
||||
fun build() {
|
||||
if (isNotIgnoredNoSuchMemberFailure && isUsingRemedyPlan.not() && isShutErrorPrinting.not())
|
||||
loggingContent = Pair(msg, throwable)
|
||||
}
|
||||
/** 判断绑定到 Hooker 时仅创建日志 */
|
||||
if (isBindToHooker) return Thread {
|
||||
/** 延迟使得方法取到返回值 */
|
||||
SystemClock.sleep(1)
|
||||
build()
|
||||
}.start()
|
||||
/** 判断始终输出日志或等待结果后输出日志 */
|
||||
if (isAlwaysPrint) build().run { printLogIfExist() }
|
||||
else Thread {
|
||||
/** 延迟使得方法取到返回值 */
|
||||
SystemClock.sleep(1)
|
||||
if (isNotIgnoredNoSuchMemberFailure && isUsingRemedyPlan.not() && isShutErrorPrinting.not()) print()
|
||||
build().run { printLogIfExist() }
|
||||
}.start()
|
||||
}
|
||||
|
||||
/** 存在日志时输出日志 */
|
||||
internal fun printLogIfExist() {
|
||||
if (loggingContent == null) return
|
||||
yLoggerE(msg = "NoSuch$tag happend in [$classSet] ${loggingContent?.first} [${hookTag}]", e = loggingContent?.second)
|
||||
/** 仅输出一次 - 然后清掉日志 */
|
||||
loggingContent = null
|
||||
}
|
||||
|
||||
/**
|
||||
* Hook 过程中开启了 [YukiHookAPI.Configs.isDebug] 输出调试信息
|
||||
* @param msg 调试日志内容
|
||||
*/
|
||||
internal fun onHookLogMsg(msg: String) {
|
||||
if (YukiHookAPI.Configs.isDebug && YukiHookBridge.hasXposedBridge) yLoggerI(msg = msg)
|
||||
if (YukiHookAPI.Configs.isDebug && YukiHookBridge.hasXposedBridge)
|
||||
hookInstance?.also { yLoggerI(msg = "[${it.packageName}] $msg") } ?: yLoggerI(msg = msg)
|
||||
}
|
||||
|
||||
/**
|
||||
|
Reference in New Issue
Block a user