File: CustomDelegate.m

package info (click to toggle)
vimix 0.8.5c%2Bgit20251123%2Bds-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 88,340 kB
  • sloc: cpp: 94,323; ansic: 34,427; makefile: 373; objc: 97; xml: 83; sh: 45
file content (49 lines) | stat: -rw-r--r-- 1,949 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#import "CustomDelegate.h"
#import <objc/runtime.h>

// part of your application
extern void forward_load_message(const char * filename);

@implementation GLFWCustomDelegate

+ (void)load{
        static dispatch_once_t onceToken;
        dispatch_once(&onceToken, ^{
                Class class = objc_getClass("GLFWApplicationDelegate");

                [GLFWCustomDelegate swizzle:class src:@selector(application:openFile:) tgt:@selector(swz_application:openFile:)];
                [GLFWCustomDelegate swizzle:class src:@selector(application:openFiles:) tgt:@selector(swz_application:openFiles:)];
        });
}

+ (void) swizzle:(Class) original_c src:(SEL)original_s tgt:(SEL)target_s{
        Class target_c = [GLFWCustomDelegate class];
        Method originalMethod = class_getInstanceMethod(original_c, original_s);
        Method swizzledMethod = class_getInstanceMethod(target_c, target_s);

        BOOL didAddMethod =
        class_addMethod(original_c,
                                        original_s,
                                        method_getImplementation(swizzledMethod),
                                        method_getTypeEncoding(swizzledMethod));

        if (didAddMethod) {
                class_replaceMethod(original_c,
                                                        target_s,
                                                        method_getImplementation(originalMethod),
                                                        method_getTypeEncoding(originalMethod));
        } else {
                method_exchangeImplementations(originalMethod, swizzledMethod);
        }
}

- (BOOL)swz_application:(NSApplication *)sender openFile:(NSString *)filename{
        forward_load_message(filename.UTF8String);
        return YES;
}

- (void)swz_application:(NSApplication *)sender openFiles:(NSArray<NSString *> *)filenames{
        forward_load_message(filenames.firstObject.UTF8String);
}

@end