forked from jessegrosjean/blocks
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Blocks.m
177 lines (141 loc) · 6.48 KB
/
Blocks.m
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
//
// Blocks.m
// Blocks
//
// Created by Jesse Grosjean on 2/21/08.
// Copyright 2008 Blocks. All rights reserved.
//
#import "Blocks.h"
#import <objc/objc-class.h>
@implementation NSObject (BlocksMethodSwizzle)
// derived from http://rentzsch.com/trac/wiki/JRSwizzle
+ (BOOL)replaceMethod:(SEL)originalSelelector withMethod:(SEL)replacementSelector {
Method originalMethod = class_getInstanceMethod(self, originalSelelector);
if (originalMethod == NULL) {
BLogError(@"original method %@ not found for class %@", NSStringFromSelector(originalSelelector), [self className]);
return NO;
}
Method replacementMethod = class_getInstanceMethod(self, replacementSelector);
if (replacementMethod == NULL) {
BLogError(@"original method %@ not found for class %@", NSStringFromSelector(replacementSelector), [self className]);
return NO;
}
class_addMethod(self, originalSelelector, class_getMethodImplementation(self, originalSelelector), method_getTypeEncoding(originalMethod));
class_addMethod(self, replacementSelector, class_getMethodImplementation(self, replacementSelector), method_getTypeEncoding(replacementMethod));
method_exchangeImplementations(class_getInstanceMethod(self, originalSelelector), class_getInstanceMethod(self, replacementSelector));
return YES;
}
@end
// derived from http://www.mikeash.com/svn/MAKVONotificationCenter
@interface BlocksKVONotificationHelper : NSObject {
id observer;
SEL selector;
id userInfo;
id target;
NSString *keyPath;
}
- (id)initWithObserver:(id)observer object:(id)target keyPath:(NSString *)keyPath selector:(SEL)selector userInfo:(id)userInfo options: (NSKeyValueObservingOptions)options;
- (void)deregister;
@end
@implementation BlocksKVONotificationHelper
static char BlocksKVONotificationHelperMagicContext;
- (id)initWithObserver:(id)anObserver object:(id)aTarget keyPath:(NSString *)aKeyPath selector:(SEL)aSelector userInfo:(id)aUserInfo options:(NSKeyValueObservingOptions)options {
if(self = [self init]) {
observer = anObserver;
selector = aSelector;
userInfo = aUserInfo;
target = aTarget;
keyPath = aKeyPath;
[target addObserver:self forKeyPath:aKeyPath options:options context:&BlocksKVONotificationHelperMagicContext];
}
return self;
}
- (void)observeValueForKeyPath:(NSString *)aKeyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
if (context == &BlocksKVONotificationHelperMagicContext) {
// we only ever sign up for one notification per object, so if we got here
// then we *know* that the key path and object are what we want
((void (*)(id, SEL, NSString *, id, NSDictionary *, id))objc_msgSend)(observer, selector, aKeyPath, object, change, userInfo);
} else {
[super observeValueForKeyPath:aKeyPath ofObject:object change:change context:context];
}
}
- (void)deregister {
[target removeObserver:self forKeyPath:keyPath];
}
@end
@interface BlocksKVONotificationCenter : NSObject {
NSMutableDictionary *observerHelpers;
}
@end
@implementation BlocksKVONotificationCenter
+ (id)defaultCenter {
static BlocksKVONotificationCenter *defaultCenter = nil;
if (!defaultCenter) {
defaultCenter = [[self alloc] init];
// BlocksKVONotificationCenter *newCenter = [[self alloc] init];
// OSAtomicCompareAndSwapPtrBarrier(nil, newCenter, (void *)&defaultCenter);
}
return defaultCenter;
}
- (id)init {
if (self = [super init]) {
observerHelpers = [[NSMutableDictionary alloc] init];
}
return self;
}
- (id)dictionaryKeyForObserver:(id)observer object:(id)target keyPath:(NSString *)keyPath selector:(SEL)selector {
return [NSString stringWithFormat:@"%p:%p:%@:%p", observer, target, keyPath, selector];
}
- (void)addObserver:(id)observer object:(id)target keyPath:(NSString *)keyPath selector:(SEL)selector userInfo:(id)userInfo options: (NSKeyValueObservingOptions)options {
BlocksKVONotificationHelper *helper = [[BlocksKVONotificationHelper alloc] initWithObserver:observer object:target keyPath:keyPath selector:selector userInfo:userInfo options:options];
id key = [self dictionaryKeyForObserver:observer object:target keyPath:keyPath selector:selector];
@synchronized(self) {
[observerHelpers setObject:helper forKey:key];
}
}
- (void)removeObserver:(id)observer object:(id)target keyPath:(NSString *)keyPath selector:(SEL)selector {
id key = [self dictionaryKeyForObserver:observer object:target keyPath:keyPath selector:selector];
BlocksKVONotificationHelper *helper = nil;
@synchronized(self) {
helper = [observerHelpers objectForKey:key];
[observerHelpers removeObjectForKey:key];
}
[helper deregister];
}
@end
@implementation NSObject (BlocksKVONotification)
- (void)addObserver:(id)observer forKeyPath:(NSString *)keyPath selector:(SEL)selector userInfo:(id)userInfo options:(NSKeyValueObservingOptions)options {
[[BlocksKVONotificationCenter defaultCenter] addObserver:observer object:self keyPath:keyPath selector:selector userInfo:userInfo options:options];
}
- (void)removeObserver:(id)observer keyPath:(NSString *)keyPath selector:(SEL)selector {
[[BlocksKVONotificationCenter defaultCenter] removeObserver:observer object:self keyPath:keyPath selector:selector];
}
@end
@implementation NSBundle (BlocksMethodReplacements)
+ (void)load {
if (self == [NSBundle class]) {
[[NSUserDefaults standardUserDefaults] registerDefaults:[NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithBool:YES], BDisableColorPickersAndInputManagersDefaultsKey, nil]];
[NSBundle replaceMethod:@selector(initWithPath:) withMethod:@selector(Blocks_initWithPath:)];
}
}
- (id)Blocks_initWithPath:(NSString *)fullPath {
if ([[NSUserDefaults standardUserDefaults] boolForKey:BDisableColorPickersAndInputManagersDefaultsKey]) {
if ([fullPath rangeOfString:@"InputManagers"].location != NSNotFound || [fullPath rangeOfString:@"ColorPickers"].location != NSNotFound) {
static NSArray *allowedColorPickers = nil;
if (!allowedColorPickers) {
allowedColorPickers = [NSArray arrayWithObjects:@"NSColorPickerWheel", @"NSColorPickerUser", @"NSColorPickerSliders", @"NSColorPickerPageableNameList", @"NSColorPickerCrayon", nil];
}
for (NSString *each in allowedColorPickers) {
if ([fullPath rangeOfString:each].location != NSNotFound) {
return [self Blocks_initWithPath:fullPath];
}
}
BLogInfo([NSString stringWithFormat:@"Skipping loading of unknown bundles %@", fullPath]);
return nil;
}
}
return [self Blocks_initWithPath:fullPath];
}
@end
NSString *BDisableColorPickersAndInputManagersDefaultsKey = @"BDisableColorPickersAndInputManagersDefaultsKey";