Skip to content

Commit c48cef3

Browse files
committed
Manual Objective-C method hooks
1 parent e6e5335 commit c48cef3

File tree

1 file changed

+24
-22
lines changed

1 file changed

+24
-22
lines changed

loader/src/hooks/AddExtraKeys.mm

+24-22
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include <Geode/DefaultInclude.hpp>
66
#include <Geode/cocos/robtop/keyboard_dispatcher/CCKeyboardDispatcher.h>
77
#include <Geode/cocos/robtop/keyboard_dispatcher/CCKeyboardDelegate.h>
8+
#import <Geode/cocos/platform/mac/EAGLView.h>
89
#include <Geode/cocos/text_input_node/CCIMEDispatcher.h>
910
#include <Geode/modify/Modify.hpp>
1011
#include <Geode/modify/CCKeyboardDispatcher.hpp>
@@ -126,15 +127,16 @@ bool isKeyNumpad(NSEvent* event) {
126127
}
127128

128129

129-
static void(*s_keyDownExecOrig)(void*, SEL, NSEvent*);
130-
void keyDownExecHook(void* self, SEL sel, NSEvent* event) {
130+
void keyDownExecHook(EAGLView* self, SEL sel, NSEvent* event) {
131131
bool extraKey = isExtraKey(event);
132132
bool numpad = isKeyNumpad(event);
133133
if (!extraKey && !numpad) {
134-
return s_keyDownExecOrig(self, sel, event);
134+
[self performSelector:sel withObject:event];
135+
return;
135136
}
136137
if (CCIMEDispatcher::sharedDispatcher()->hasDelegate()) {
137-
return s_keyDownExecOrig(self, sel, event);
138+
[self performSelector:sel withObject:event];
139+
return;
138140
}
139141

140142
enumKeyCodes keyCode = enumKeyCodes::KEY_Unknown;
@@ -147,15 +149,16 @@ void keyDownExecHook(void* self, SEL sel, NSEvent* event) {
147149
CCKeyboardDispatcher::get()->dispatchKeyboardMSG(keyCode, true, [event isARepeat]);
148150
}
149151

150-
static void(*s_keyUpExecOrig)(void*, SEL, NSEvent*);
151-
void keyUpExecHook(void* self, SEL sel, NSEvent* event) {
152+
void keyUpExecHook(EAGLView* self, SEL sel, NSEvent* event) {
152153
bool extraKey = isExtraKey(event);
153154
bool numpad = isKeyNumpad(event);
154155
if (!extraKey && !numpad) {
155-
return s_keyUpExecOrig(self, sel, event);
156+
[self performSelector:sel withObject:event];
157+
return;
156158
}
157159
if (CCIMEDispatcher::sharedDispatcher()->hasDelegate()) {
158-
return s_keyUpExecOrig(self, sel, event);
160+
[self performSelector:sel withObject:event];
161+
return;
159162
}
160163

161164
enumKeyCodes keyCode = enumKeyCodes::KEY_Unknown;
@@ -168,20 +171,20 @@ void keyUpExecHook(void* self, SEL sel, NSEvent* event) {
168171
CCKeyboardDispatcher::get()->dispatchKeyboardMSG(keyCode, false, [event isARepeat]);
169172
}
170173

171-
static void(*s_mouseDownExecOrig)(void*, SEL, NSEvent*);
172-
void mouseDownExecHook(void* self, SEL sel, NSEvent* event) {
174+
void mouseDownExecHook(EAGLView* self, SEL sel, NSEvent* event) {
173175
if (!isExtraMouseButton(event)) {
174-
return s_mouseDownExecOrig(self, sel, event);
176+
[self performSelector:sel withObject:event];
177+
return;
175178
}
176179

177180
enumKeyCodes keyCode = mouseButtonToKeyCode(event);
178181
CCKeyboardDispatcher::get()->dispatchKeyboardMSG(keyCode, true, false);
179182
}
180183

181-
static void(*s_mouseUpExecOrig)(void*, SEL, NSEvent*);
182-
void mouseUpExecHook(void* self, SEL sel, NSEvent* event) {
184+
void mouseUpExecHook(EAGLView* self, SEL sel, NSEvent* event) {
183185
if (!isExtraMouseButton(event)) {
184-
return s_mouseUpExecOrig(self, sel, event);
186+
[self performSelector:sel withObject:event];
187+
return;
185188
}
186189

187190
enumKeyCodes keyCode = mouseButtonToKeyCode(event);
@@ -237,17 +240,16 @@ void mouseUpExecHook(void* self, SEL sel, NSEvent* event) {
237240
};
238241

239242

240-
#define OBJC_SWIZZLE(klass, methodName) \
241-
auto methodName##Method = class_getInstanceMethod(klass, @selector(methodName:)); \
242-
s_##methodName##Orig = reinterpret_cast<decltype(s_##methodName##Orig)>(method_getImplementation(methodName##Method)); \
243-
method_setImplementation(methodName##Method, reinterpret_cast<IMP>(&methodName##Hook));
243+
#define HOOK_OBJC_METHOD(klass, methodName) \
244+
auto methodName##Addr = class_getInstanceMethod(klass, @selector(methodName:)); \
245+
static_cast<void>(Mod::get()->hook(reinterpret_cast<void*>(method_getImplementation(methodName##Addr)), &methodName##Hook, #klass " " #methodName));
244246

245247
__attribute__((constructor)) void initialize_newKeyboardMSGKeysHooks() {
246248
auto eaglView = objc_getClass("EAGLView");
247249

248-
OBJC_SWIZZLE(eaglView, keyDownExec);
249-
OBJC_SWIZZLE(eaglView, keyUpExec);
250+
HOOK_OBJC_METHOD(eaglView, keyDownExec);
251+
HOOK_OBJC_METHOD(eaglView, keyUpExec);
250252

251-
OBJC_SWIZZLE(eaglView, mouseDownExec);
252-
OBJC_SWIZZLE(eaglView, mouseUpExec);
253+
HOOK_OBJC_METHOD(eaglView, mouseDownExec);
254+
HOOK_OBJC_METHOD(eaglView, mouseUpExec);
253255
}

0 commit comments

Comments
 (0)