-
Notifications
You must be signed in to change notification settings - Fork 0
/
ANSVisualSwizzler.m
404 lines (350 loc) · 15.7 KB
/
ANSVisualSwizzler.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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
//
// MPSwizzler.m
// HelloMixpanel
//
// Created by Alex Hofsteede on 1/5/14.
// Copyright (c) 2014 Mixpanel. All rights reserved.
//
#import <objc/runtime.h>
#import <UIKit/UIKit.h>
#import "ANSVisualSwizzler.h"
#import "AnalysysLogger.h"
#define MIN_ARGS 2
#define MAX_ARGS 6
@interface ANSVisualSwizzle : NSObject
@property (nonatomic, assign) Class class;
@property (nonatomic, assign) SEL selector;
@property (nonatomic, assign) IMP originalMethod;
@property (nonatomic, assign) uint numArgs;
@property (nonatomic, copy) NSMapTable *blocks;
- (instancetype)initWithBlock:(swizzleBlock)aBlock
named:(NSString *)aName
forClass:(Class)aClass
selector:(SEL)aSelector
originalMethod:(IMP)aMethod
withNumArgs:(uint)numArgs;
@end
static NSMapTable *ans_visual_swizzles;
static id cellForRowAtIndexPath(id self, SEL _cmd, id arg, id arg2)
{
Method aMethod = class_getInstanceMethod([self class], _cmd);
ANSVisualSwizzle *swizzle = (ANSVisualSwizzle *)[ans_visual_swizzles objectForKey:(__bridge id)((void *)aMethod)];
if (swizzle) {
id ret = ((id(*)(id, SEL, id, id))swizzle.originalMethod)(self, _cmd, arg, arg2);
NSEnumerator *blocks = [swizzle.blocks objectEnumerator];
swizzleBlock block;
while ((block = [blocks nextObject])) {
block(ret, self, _cmd, arg, arg2);
}
return ret;
}
return nil;
}
static id viewForHeaderInSection(id self, SEL _cmd, id arg, NSInteger arg2)
{
Method aMethod = class_getInstanceMethod([self class], _cmd);
ANSVisualSwizzle *swizzle = (ANSVisualSwizzle *)[ans_visual_swizzles objectForKey:(__bridge id)((void *)aMethod)];
if (swizzle) {
id ret = ((id(*)(id, SEL, id, NSInteger))swizzle.originalMethod)(self, _cmd, arg, arg2);
NSEnumerator *blocks = [swizzle.blocks objectEnumerator];
swizzleBlock block;
while ((block = [blocks nextObject])) {
block(ret, self, _cmd, arg, arg2);
}
return ret;
}
return nil;
}
static id viewForFooterInSection(id self, SEL _cmd, id arg, NSInteger arg2)
{
Method aMethod = class_getInstanceMethod([self class], _cmd);
ANSVisualSwizzle *swizzle = (ANSVisualSwizzle *)[ans_visual_swizzles objectForKey:(__bridge id)((void *)aMethod)];
if (swizzle) {
id ret = ((id(*)(id, SEL, id, NSInteger))swizzle.originalMethod)(self, _cmd, arg, arg2);
NSEnumerator *blocks = [swizzle.blocks objectEnumerator];
swizzleBlock block;
while ((block = [blocks nextObject])) {
block(ret, self, _cmd, arg, arg2);
}
return ret;
}
return nil;
}
static id cellForItemAtIndexPath(id self, SEL _cmd, id arg, id arg2)
{
Method aMethod = class_getInstanceMethod([self class], _cmd);
ANSVisualSwizzle *swizzle = (ANSVisualSwizzle *)[ans_visual_swizzles objectForKey:(__bridge id)((void *)aMethod)];
if (swizzle) {
id ret = ((id(*)(id, SEL, id, id))swizzle.originalMethod)(self, _cmd, arg, arg2);
NSEnumerator *blocks = [swizzle.blocks objectEnumerator];
swizzleBlock block;
while ((block = [blocks nextObject])) {
block(ret, self, _cmd, arg, arg2);
}
return ret;
}
return nil;
}
static id viewForSupplementaryElementOfKind(id self, SEL _cmd, id arg, id arg2, id arg3)
{
Method aMethod = class_getInstanceMethod([self class], _cmd);
ANSVisualSwizzle *swizzle = (ANSVisualSwizzle *)[ans_visual_swizzles objectForKey:(__bridge id)((void *)aMethod)];
if (swizzle) {
id ret = ((id(*)(id, SEL, id, id, id))swizzle.originalMethod)(self, _cmd, arg, arg2, arg3);
NSEnumerator *blocks = [swizzle.blocks objectEnumerator];
swizzleBlock block;
while ((block = [blocks nextObject])) {
block(ret, self, _cmd, arg, arg2, arg3);
}
return ret;
}
return nil;
}
static void didSelectRow(id self, SEL _cmd, id arg, id arg2)
{
Method aMethod = class_getInstanceMethod([self class], _cmd);
ANSVisualSwizzle *swizzle = (ANSVisualSwizzle *)[ans_visual_swizzles objectForKey:(__bridge id)((void *)aMethod)];
if (swizzle) {
NSEnumerator *blocks = [swizzle.blocks objectEnumerator];
swizzleBlock block;
while ((block = [blocks nextObject])) {
block(self, _cmd, arg, arg2);
}
((void(*)(id, SEL, id, id))swizzle.originalMethod)(self, _cmd, arg, arg2);
}
}
static void didDeselectRow(id self, SEL _cmd, id arg, id arg2)
{
Method aMethod = class_getInstanceMethod([self class], _cmd);
ANSVisualSwizzle *swizzle = (ANSVisualSwizzle *)[ans_visual_swizzles objectForKey:(__bridge id)((void *)aMethod)];
if (swizzle) {
NSEnumerator *blocks = [swizzle.blocks objectEnumerator];
swizzleBlock block;
while ((block = [blocks nextObject])) {
block(self, _cmd, arg, arg2);
}
((void(*)(id, SEL, id, id))swizzle.originalMethod)(self, _cmd, arg, arg2);
}
}
static void didSelectItem(id self, SEL _cmd, id arg, id arg2)
{
Method aMethod = class_getInstanceMethod([self class], _cmd);
ANSVisualSwizzle *swizzle = (ANSVisualSwizzle *)[ans_visual_swizzles objectForKey:(__bridge id)((void *)aMethod)];
if (swizzle) {
NSEnumerator *blocks = [swizzle.blocks objectEnumerator];
swizzleBlock block;
while ((block = [blocks nextObject])) {
block(self, _cmd, arg, arg2);
}
((void(*)(id, SEL, id, id))swizzle.originalMethod)(self, _cmd, arg, arg2);
}
}
static void didDeselectItem(id self, SEL _cmd, id arg, id arg2)
{
Method aMethod = class_getInstanceMethod([self class], _cmd);
ANSVisualSwizzle *swizzle = (ANSVisualSwizzle *)[ans_visual_swizzles objectForKey:(__bridge id)((void *)aMethod)];
if (swizzle) {
NSEnumerator *blocks = [swizzle.blocks objectEnumerator];
swizzleBlock block;
while ((block = [blocks nextObject])) {
block(self, _cmd, arg, arg2);
}
((void(*)(id, SEL, id, id))swizzle.originalMethod)(self, _cmd, arg, arg2);
}
}
// Ignore the warning cause we need the paramters to be dynamic and it's only being used internally
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wstrict-prototypes"
//static void (*mp_swizzledMethods[MAX_ARGS - MIN_ARGS + 1])() = {mp_swizzledMethod_2, mp_swizzledMethod_3, mp_swizzledMethod_4, mp_swizzledMethod_5};
//
//static BOOL (*mp_swizzledMethods_bool[MAX_ARGS - MIN_ARGS + 1])() = {mp_swizzledMethod_2_bool, mp_swizzledMethod_3_bool, mp_swizzledMethod_4_bool, mp_swizzledMethod_5_bool, mp_swizzledMethod_6_bool};
//
//static id (*mp_swizzledMethods_id[MAX_ARGS - MIN_ARGS + 1])() = {mp_swizzledMethod_2_id, mp_swizzledMethod_3_id, mp_swizzledMethod_4_id, mp_swizzledMethod_5_id};
static id (*ans_cellForRowAtIndexPath)() = cellForRowAtIndexPath;
static id (*ans_viewForHeaderInSection)() = viewForHeaderInSection;
static id (*ans_viewForFooterInSection)() = viewForFooterInSection;
static id (*ans_cellForItemAtIndexPath)() = cellForItemAtIndexPath;
static id (*ans_viewForSupplementaryElementOfKind)() = viewForSupplementaryElementOfKind;
static void (*ans_didSelectRow)() = didSelectRow;
static void (*ans_didDeselectRow)() = didDeselectRow;
static void (*ans_didSelectItem)() = didSelectItem;
static void (*ans_didDeselectItem)() = didDeselectItem;
#pragma clang diagnostic pop
@implementation ANSVisualSwizzler
+ (void)load
{
ans_visual_swizzles = [NSMapTable mapTableWithKeyOptions:(NSPointerFunctionsOpaqueMemory | NSPointerFunctionsOpaquePersonality)
valueOptions:(NSPointerFunctionsStrongMemory | NSPointerFunctionsObjectPointerPersonality)];
}
+ (void)printSwizzles
{
NSEnumerator *en = [ans_visual_swizzles objectEnumerator];
ANSVisualSwizzle *swizzle;
while ((swizzle = (ANSVisualSwizzle *)[en nextObject])) {
ANSVisualBriefError(@"%@", swizzle);
}
}
+ (ANSVisualSwizzle *)swizzleForMethod:(Method)aMethod
{
return (ANSVisualSwizzle *)[ans_visual_swizzles objectForKey:MAPTABLE_ID(aMethod)];
}
+ (void)removeSwizzleForMethod:(Method)aMethod
{
[ans_visual_swizzles removeObjectForKey:MAPTABLE_ID(aMethod)];
}
+ (void)setSwizzle:(ANSVisualSwizzle *)swizzle forMethod:(Method)aMethod
{
[ans_visual_swizzles setObject:swizzle forKey:MAPTABLE_ID(aMethod)];
}
+ (BOOL)isLocallyDefinedMethod:(Method)aMethod onClass:(Class)aClass
{
uint count;
BOOL isLocal = NO;
Method *methods = class_copyMethodList(aClass, &count);
for (NSUInteger i = 0; i < count; i++) {
if (aMethod == methods[i]) {
isLocal = YES;
break;
}
}
free(methods);
return isLocal;
}
+ (void)swizzleSelector:(SEL)aSelector onClass:(Class)aClass withBlock:(swizzleBlock)aBlock named:(NSString *)aName
{
Method aMethod = class_getInstanceMethod(aClass, aSelector);
if (aMethod) {
uint numArgs = method_getNumberOfArguments(aMethod);
if (numArgs >= MIN_ARGS && numArgs <= MAX_ARGS) {
BOOL isLocal = [self isLocallyDefinedMethod:aMethod onClass:aClass];
IMP swizzledMethod;
// char retType[512] = {};
// method_getReturnType(aMethod, retType, 512);
// NSString *retTypeStr = [NSString stringWithUTF8String:retType];
// if ([retTypeStr isEqualToString:@"B"]) {
// swizzledMethod = (IMP)mp_swizzledMethods_bool[numArgs - 2];
// } else if ([retTypeStr isEqualToString:@"v"]) {
// swizzledMethod = (IMP)mp_swizzledMethods[numArgs - 2];
// } else {
// swizzledMethod = (IMP)mp_swizzledMethods_id[numArgs - 2];
// }
if (aSelector == @selector(tableView:cellForRowAtIndexPath:)) {
swizzledMethod = (IMP)ans_cellForRowAtIndexPath;
} else if (aSelector == @selector(tableView:viewForHeaderInSection:)) {
swizzledMethod = (IMP)ans_viewForHeaderInSection;
} else if (aSelector == @selector(tableView:viewForFooterInSection:)) {
swizzledMethod = (IMP)ans_viewForFooterInSection;
} else if (aSelector == @selector(collectionView:cellForItemAtIndexPath:)) {
swizzledMethod = (IMP)ans_cellForItemAtIndexPath;
} else if (aSelector == @selector(collectionView:viewForSupplementaryElementOfKind:atIndexPath:)) {
swizzledMethod = (IMP)ans_viewForSupplementaryElementOfKind;
} else if (aSelector == @selector(tableView:didSelectRowAtIndexPath:)) {
swizzledMethod = (IMP)ans_didSelectRow;
} else if (aSelector == @selector(tableView:didDeselectRowAtIndexPath:)) {
swizzledMethod = (IMP)ans_didDeselectRow;
} else if (aSelector == @selector(collectionView:didSelectItemAtIndexPath:)) {
swizzledMethod = (IMP)ans_didSelectItem;
} else if (aSelector == @selector(collectionView:didDeselectItemAtIndexPath:)) {
swizzledMethod = (IMP)ans_didDeselectItem;
} else {
swizzledMethod = nil;
}
ANSVisualSwizzle *swizzle = [self swizzleForMethod:aMethod];
if (isLocal) {
if (!swizzle) {
IMP originalMethod = method_getImplementation(aMethod);
// Replace the local implementation of this method with the swizzled one
method_setImplementation(aMethod,swizzledMethod);
// Create and add the swizzle
swizzle = [[ANSVisualSwizzle alloc] initWithBlock:aBlock named:aName forClass:aClass selector:aSelector originalMethod:originalMethod withNumArgs:numArgs];
[self setSwizzle:swizzle forMethod:aMethod];
} else {
[swizzle.blocks setObject:aBlock forKey:aName];
}
} else {
IMP originalMethod = swizzle ? swizzle.originalMethod : method_getImplementation(aMethod);
// Add the swizzle as a new local method on the class.
if (!class_addMethod(aClass, aSelector, swizzledMethod, method_getTypeEncoding(aMethod))) {
ANSVisualBriefLog(@"SwizzlerAssert: Could not add swizzled for %@::%@, even though it didn't already exist locally", NSStringFromClass(aClass), NSStringFromSelector(aSelector));
return;
}
// Now re-get the Method, it should be the one we just added.
Method newMethod = class_getInstanceMethod(aClass, aSelector);
if (aMethod == newMethod) {
ANSVisualBriefLog(@"SwizzlerAssert: Newly added method for %@::%@ was the same as the old method", NSStringFromClass(aClass), NSStringFromSelector(aSelector));
return;
}
ANSVisualSwizzle *newSwizzle = [[ANSVisualSwizzle alloc] initWithBlock:aBlock named:aName forClass:aClass selector:aSelector originalMethod:originalMethod withNumArgs:numArgs];
[self setSwizzle:newSwizzle forMethod:newMethod];
}
} else {
ANSVisualBriefLog(@"SwizzlerAssert: Cannot swizzle method with %d args", numArgs);
}
} else {
ANSVisualBriefLog(@"SwizzlerAssert: Cannot find method for %@ on %@", NSStringFromSelector(aSelector), NSStringFromClass(aClass));
}
}
+ (void)unswizzleSelector:(SEL)aSelector onClass:(Class)aClass
{
Method aMethod = class_getInstanceMethod(aClass, aSelector);
ANSVisualSwizzle *swizzle = [self swizzleForMethod:aMethod];
if (swizzle) {
method_setImplementation(aMethod, swizzle.originalMethod);
[self removeSwizzleForMethod:aMethod];
}
}
/*
Remove the named swizzle from the given class/selector. If aName is nil, remove all
swizzles for this class/selector
*/
+ (void)unswizzleSelector:(SEL)aSelector onClass:(Class)aClass named:(NSString *)aName
{
Method aMethod = class_getInstanceMethod(aClass, aSelector);
ANSVisualSwizzle *swizzle = [self swizzleForMethod:aMethod];
if (swizzle) {
if (aName) {
[swizzle.blocks removeObjectForKey:aName];
}
if (!aName || swizzle.blocks.count == 0) {
method_setImplementation(aMethod, swizzle.originalMethod);
[self removeSwizzleForMethod:aMethod];
}
}
}
@end
@implementation ANSVisualSwizzle
- (instancetype)init
{
if ((self = [super init])) {
self.blocks = [NSMapTable mapTableWithKeyOptions:(NSPointerFunctionsStrongMemory | NSPointerFunctionsObjectPersonality)
valueOptions:(NSPointerFunctionsStrongMemory | NSPointerFunctionsObjectPointerPersonality)];
}
return self;
}
- (instancetype)initWithBlock:(swizzleBlock)aBlock
named:(NSString *)aName
forClass:(Class)aClass
selector:(SEL)aSelector
originalMethod:(IMP)aMethod
withNumArgs:(uint)numArgs
{
if ((self = [self init])) {
self.class = aClass;
self.selector = aSelector;
self.numArgs = numArgs;
self.originalMethod = aMethod;
[self.blocks setObject:aBlock forKey:aName];
}
return self;
}
- (NSString *)description
{
NSString *descriptors = @"";
NSString *key;
NSEnumerator *keys = [self.blocks keyEnumerator];
while ((key = [keys nextObject])) {
descriptors = [descriptors stringByAppendingFormat:@"\t%@ : %@\n", key, [self.blocks objectForKey:key]];
}
return [NSString stringWithFormat:@"Swizzle on %@::%@ [\n%@]", NSStringFromClass(self.class), NSStringFromSelector(self.selector), descriptors];
}
@end