From fc49149315e36f7529e7689f1911af58d968be0b Mon Sep 17 00:00:00 2001 From: rhunk <101876869+rhunk@users.noreply.github.com> Date: Fri, 19 May 2023 18:52:18 +0200 Subject: [PATCH] refactor: hooker --- .../me/rhunk/snapenhance/hook/Hooker.kt | 26 +++++++++---------- 1 file changed, 12 insertions(+), 14 deletions(-) diff --git a/app/src/main/kotlin/me/rhunk/snapenhance/hook/Hooker.kt b/app/src/main/kotlin/me/rhunk/snapenhance/hook/Hooker.kt index 92f558e4..3c820308 100644 --- a/app/src/main/kotlin/me/rhunk/snapenhance/hook/Hooker.kt +++ b/app/src/main/kotlin/me/rhunk/snapenhance/hook/Hooker.kt @@ -9,20 +9,18 @@ object Hooker { stage: HookStage, consumer: (HookAdapter) -> Unit, filter: ((HookAdapter) -> Boolean) = { true } - ) = object : XC_MethodHook() { - override fun beforeHookedMethod(param: MethodHookParam<*>) { - if (stage != HookStage.BEFORE) return - with(HookAdapter(param)) { - if (!filter(this)) return - consumer(this) - } + ): XC_MethodHook { + val callEvent = { param: XC_MethodHook.MethodHookParam<*> -> + HookAdapter(param).takeIf(filter)?.also(consumer) } - override fun afterHookedMethod(param: MethodHookParam<*>) { - if (stage != HookStage.AFTER) return - with(HookAdapter(param)) { - if (!filter(this)) return - consumer(this) + return if (stage == HookStage.BEFORE) object : XC_MethodHook() { + override fun beforeHookedMethod(param: MethodHookParam<*>) { + callEvent(param) + } + } else object : XC_MethodHook() { + override fun afterHookedMethod(param: MethodHookParam<*>) { + callEvent(param) } } } @@ -32,7 +30,7 @@ object Hooker { methodName: String, stage: HookStage, consumer: (HookAdapter) -> Unit - ): Set = XposedBridge.hookAllMethods(clazz, methodName, newMethodHook(stage, consumer)) + ): Set = hook(clazz, methodName, stage, { true }, consumer) fun hook( clazz: Class<*>, @@ -47,7 +45,7 @@ object Hooker { stage: HookStage, consumer: (HookAdapter) -> Unit ): XC_MethodHook.Unhook { - return XposedBridge.hookMethod(member, newMethodHook(stage, consumer)) + return hook(member, stage, { true }, consumer) } fun hook(