Added YukiHookAPI itself internal checking in ReflectionFactory and remove exception when hooker is empty

This commit is contained in:
2022-08-12 02:51:04 +08:00
parent 925f3df68c
commit b5531d2879
6 changed files with 32 additions and 6 deletions

View File

@@ -36,6 +36,7 @@ import com.highcapable.yukihookapi.hook.core.finder.FieldFinder
import com.highcapable.yukihookapi.hook.core.finder.MethodFinder import com.highcapable.yukihookapi.hook.core.finder.MethodFinder
import com.highcapable.yukihookapi.hook.core.finder.base.BaseFinder import com.highcapable.yukihookapi.hook.core.finder.base.BaseFinder
import com.highcapable.yukihookapi.hook.factory.* import com.highcapable.yukihookapi.hook.factory.*
import com.highcapable.yukihookapi.hook.log.loggerW
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.log.yLoggerW import com.highcapable.yukihookapi.hook.log.yLoggerW
@@ -113,13 +114,18 @@ class YukiMemberHookCreater(@PublishedApi internal val packageParam: PackagePara
YukiHookBridge.hasXposedBridge.not() -> Result() YukiHookBridge.hasXposedBridge.not() -> Result()
/** 过滤 [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() -> Result().also { loggerW(msg = "Hook Members is empty in [${hookClass.name}], hook aborted") }
else -> Result().await { else -> Result().await {
warnTerribleHookClass() warnTerribleHookClass()
when { when {
isDisableCreaterRunHook.not() && hookClass.instance != null -> { isDisableCreaterRunHook.not() && hookClass.instance != null -> runCatching {
hookClass.instance?.checkingInternal()
it.onPrepareHook?.invoke() it.onPrepareHook?.invoke()
preHookMembers.forEach { (_, m) -> m.hook() } preHookMembers.forEach { (_, m) -> m.hook() }
}.onFailure {
if (onHookClassNotFoundFailureCallback == null)
yLoggerE(msg = "Hook initialization failed because got an Exception", e = it)
else onHookClassNotFoundFailureCallback?.invoke(it)
} }
isDisableCreaterRunHook.not() && hookClass.instance == null -> isDisableCreaterRunHook.not() && hookClass.instance == null ->
if (onHookClassNotFoundFailureCallback == null) if (onHookClassNotFoundFailureCallback == null)

View File

@@ -32,6 +32,7 @@ package com.highcapable.yukihookapi.hook.core
import android.content.res.Resources import android.content.res.Resources
import com.highcapable.yukihookapi.YukiHookAPI import com.highcapable.yukihookapi.YukiHookAPI
import com.highcapable.yukihookapi.hook.bean.HookResources import com.highcapable.yukihookapi.hook.bean.HookResources
import com.highcapable.yukihookapi.hook.log.loggerW
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.param.PackageParam import com.highcapable.yukihookapi.hook.param.PackageParam
@@ -69,7 +70,7 @@ class YukiResourcesHookCreater(@PublishedApi internal val packageParam: PackageP
if (YukiHookBridge.hasXposedBridge.not()) return if (YukiHookBridge.hasXposedBridge.not()) return
/** 过滤 [HookEntryType.ZYGOTE] 与 [HookEntryType.RESOURCES] */ /** 过滤 [HookEntryType.ZYGOTE] 与 [HookEntryType.RESOURCES] */
if (packageParam.wrapper?.type == HookEntryType.PACKAGE) return if (packageParam.wrapper?.type == HookEntryType.PACKAGE) return
if (preHookResources.isEmpty()) error("Hook Resources is empty, hook aborted") if (preHookResources.isEmpty()) return loggerW(msg = "Hook Resources is empty, hook aborted")
preHookResources.forEach { (_, r) -> r.hook() } preHookResources.forEach { (_, r) -> r.hook() }
} }

View File

@@ -36,6 +36,7 @@ import com.highcapable.yukihookapi.hook.core.finder.base.BaseFinder
import com.highcapable.yukihookapi.hook.core.finder.type.ModifierRules import com.highcapable.yukihookapi.hook.core.finder.type.ModifierRules
import com.highcapable.yukihookapi.hook.core.reflex.tools.ReflectionTool import com.highcapable.yukihookapi.hook.core.reflex.tools.ReflectionTool
import com.highcapable.yukihookapi.hook.factory.ConstructorCondition import com.highcapable.yukihookapi.hook.factory.ConstructorCondition
import com.highcapable.yukihookapi.hook.factory.checkingInternal
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.type.defined.UndefinedType import com.highcapable.yukihookapi.hook.type.defined.UndefinedType
@@ -204,6 +205,7 @@ class ConstructorFinder @PublishedApi internal constructor(
@YukiPrivateApi @YukiPrivateApi
override fun build(isBind: Boolean) = try { override fun build(isBind: Boolean) = try {
if (classSet != null) { if (classSet != null) {
classSet.checkingInternal()
runBlocking { runBlocking {
isBindToHooker = isBind isBindToHooker = isBind
setInstance(isBind, result) setInstance(isBind, result)

View File

@@ -38,6 +38,7 @@ import com.highcapable.yukihookapi.hook.core.finder.type.ModifierRules
import com.highcapable.yukihookapi.hook.core.finder.type.NameConditions 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.FieldCondition import com.highcapable.yukihookapi.hook.factory.FieldCondition
import com.highcapable.yukihookapi.hook.factory.checkingInternal
import com.highcapable.yukihookapi.hook.factory.current import com.highcapable.yukihookapi.hook.factory.current
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
@@ -198,6 +199,7 @@ class FieldFinder @PublishedApi internal constructor(
@YukiPrivateApi @YukiPrivateApi
override fun build(isBind: Boolean) = try { override fun build(isBind: Boolean) = try {
if (classSet != null) { if (classSet != null) {
classSet.checkingInternal()
runBlocking { runBlocking {
setInstance(result) setInstance(result)
}.result { ms -> }.result { ms ->

View File

@@ -37,6 +37,7 @@ import com.highcapable.yukihookapi.hook.core.finder.type.ModifierRules
import com.highcapable.yukihookapi.hook.core.finder.type.NameConditions 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.MethodCondition import com.highcapable.yukihookapi.hook.factory.MethodCondition
import com.highcapable.yukihookapi.hook.factory.checkingInternal
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.type.defined.UndefinedType import com.highcapable.yukihookapi.hook.type.defined.UndefinedType
@@ -271,6 +272,7 @@ class MethodFinder @PublishedApi internal constructor(
@YukiPrivateApi @YukiPrivateApi
override fun build(isBind: Boolean) = try { override fun build(isBind: Boolean) = try {
if (classSet != null) { if (classSet != null) {
classSet.checkingInternal()
runBlocking { runBlocking {
isBindToHooker = isBind isBindToHooker = isBind
setInstance(isBind, result) setInstance(isBind, result)

View File

@@ -29,6 +29,7 @@
package com.highcapable.yukihookapi.hook.factory package com.highcapable.yukihookapi.hook.factory
import com.highcapable.yukihookapi.YukiHookAPI
import com.highcapable.yukihookapi.hook.bean.CurrentClass import com.highcapable.yukihookapi.hook.bean.CurrentClass
import com.highcapable.yukihookapi.hook.core.finder.ConstructorFinder import com.highcapable.yukihookapi.hook.core.finder.ConstructorFinder
import com.highcapable.yukihookapi.hook.core.finder.FieldFinder import com.highcapable.yukihookapi.hook.core.finder.FieldFinder
@@ -36,6 +37,7 @@ import com.highcapable.yukihookapi.hook.core.finder.MethodFinder
import com.highcapable.yukihookapi.hook.core.finder.type.ModifierRules import com.highcapable.yukihookapi.hook.core.finder.type.ModifierRules
import com.highcapable.yukihookapi.hook.store.MemberCacheStore import com.highcapable.yukihookapi.hook.store.MemberCacheStore
import com.highcapable.yukihookapi.hook.xposed.bridge.YukiHookBridge import com.highcapable.yukihookapi.hook.xposed.bridge.YukiHookBridge
import com.highcapable.yukihookapi.hook.xposed.bridge.status.YukiHookModuleStatus
import de.robv.android.xposed.XposedHelpers import de.robv.android.xposed.XposedHelpers
import java.lang.reflect.Constructor import java.lang.reflect.Constructor
import java.lang.reflect.Field import java.lang.reflect.Field
@@ -169,7 +171,7 @@ inline fun Class<*>.constructor(initiate: ConstructorCondition = { emptyParam()
* @return [T] * @return [T]
*/ */
inline fun <reified T : Any> T.current(initiate: CurrentClass.() -> Unit): T { inline fun <reified T : Any> T.current(initiate: CurrentClass.() -> Unit): T {
if (javaClass.name == CurrentClass::class.java.name) error("Cannot create itself within CurrentClass itself") javaClass.checkingInternal()
CurrentClass(javaClass, instance = this).apply(initiate) CurrentClass(javaClass, instance = this).apply(initiate)
return this return this
} }
@@ -179,7 +181,7 @@ inline fun <reified T : Any> T.current(initiate: CurrentClass.() -> Unit): T {
* @return [CurrentClass] * @return [CurrentClass]
*/ */
inline fun <reified T : Any> T.current(): CurrentClass { inline fun <reified T : Any> T.current(): CurrentClass {
if (javaClass.name == CurrentClass::class.java.name) error("Cannot create itself within CurrentClass itself") javaClass.checkingInternal()
return CurrentClass(javaClass, instance = this) return CurrentClass(javaClass, instance = this)
} }
@@ -220,4 +222,15 @@ inline fun Class<*>.allConstructors(result: (index: Int, constructor: Constructo
* @param result 回调 - ([Int] 下标,[Field] 实例) * @param result 回调 - ([Int] 下标,[Field] 实例)
*/ */
inline fun Class<*>.allFields(result: (index: Int, field: Field) -> Unit) = inline fun Class<*>.allFields(result: (index: Int, field: Field) -> Unit) =
declaredFields.forEachIndexed { p, it -> result(p, it.apply { isAccessible = true }) } declaredFields.forEachIndexed { p, it -> result(p, it.apply { isAccessible = true }) }
/**
* 检查内部类调用
* @throws RuntimeException 如果遇到非法调用
*/
@PublishedApi
internal fun Class<*>.checkingInternal() {
if (name == classOf<YukiHookModuleStatus>().name) return
if (name == classOf<YukiHookAPI>().name || name.startsWith(prefix = "com.highcapable.yukihookapi.hook"))
throw RuntimeException("!!!DO NOT ALLOWED!!! You cannot hook or reflection to call the internal class of the YukiHookAPI itself")
}