-
Notifications
You must be signed in to change notification settings - Fork 3
/
SpriteKit+QuickLook.m
360 lines (323 loc) · 13.1 KB
/
SpriteKit+QuickLook.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
//
// SpriteKit+QuickLook.m
// SB+SpriteKit
//
// Created by Steffen Itterheim on 13/03/14.
// Distributed under MIT License
//
#import "SpriteKit+QuickLook.h"
#import <objc/runtime.h>
static void dumpIvarNamesForClass(Class klass)
{
NSMutableString* names = [NSMutableString stringWithFormat:@"%@ ivars:\n", NSStringFromClass(klass)];
unsigned int count = 0;
Ivar* ivars = class_copyIvarList(klass, &count);
for (unsigned int i = 0; i < count; i++)
{
[names appendFormat:@"%s (%s)\n", ivar_getName(ivars[i]), ivar_getTypeEncoding(ivars[i])];
}
NSLog(@"%@\n", names);
}
@interface QuickLookHelper ()
+(NSString*) debugDescriptionStringWithDelimiter:(NSString*)delimiter spriteKitObject:(id)object;
+(NSString*) commonDebugDescriptionForSpriteKitObject:(id)object;
@end
@implementation SKNode (QuickLook)
-(id) valueForUndefinedKey:(NSString*)key { NSLog(@"%@: KVC ignored undefined key '%@', returning nil", NSStringFromClass([self class]), key); return nil; }
-(NSString*) debugDescription { return [QuickLookHelper commonDebugDescriptionForSpriteKitObject:self]; }
-(id) debugQuickLookObject { return [QuickLookHelper debugDescriptionStringWithDelimiter:@"\n" spriteKitObject:self]; }
@end
@implementation SKTexture (QuickLook)
-(id) valueForUndefinedKey:(NSString*)key { NSLog(@"%@: KVC ignored undefined key '%@', returning nil", NSStringFromClass([self class]), key); return nil; }
-(NSString*) debugDescription { return [QuickLookHelper commonDebugDescriptionForSpriteKitObject:self]; }
-(id) debugQuickLookObject { return [QuickLookHelper debugDescriptionStringWithDelimiter:@"\n" spriteKitObject:self]; }
@end
@implementation SKTextureAtlas (QuickLook)
-(id) valueForUndefinedKey:(NSString*)key { NSLog(@"%@: KVC ignored undefined key '%@', returning nil", NSStringFromClass([self class]), key); return nil; }
-(NSString*) debugDescription { return [QuickLookHelper commonDebugDescriptionForSpriteKitObject:self]; }
-(id) debugQuickLookObject { return [QuickLookHelper debugDescriptionStringWithDelimiter:@"\n" spriteKitObject:self]; }
@end
@implementation SKAction (QuickLook)
-(id) valueForUndefinedKey:(NSString*)key { NSLog(@"%@: KVC ignored undefined key '%@', returning nil", NSStringFromClass([self class]), key); return nil; }
-(NSString*) debugDescription { return [QuickLookHelper commonDebugDescriptionForSpriteKitObject:self]; }
-(id) debugQuickLookObject { return [QuickLookHelper debugDescriptionStringWithDelimiter:@"\n" spriteKitObject:self]; }
@end
@implementation SKView (QuickLook)
-(id) valueForUndefinedKey:(NSString*)key { NSLog(@"%@: KVC ignored undefined key '%@', returning nil", NSStringFromClass([self class]), key); return nil; }
-(NSString*) debugDescription { return [QuickLookHelper commonDebugDescriptionForSpriteKitObject:self]; }
-(id) debugQuickLookObject { return [QuickLookHelper debugDescriptionStringWithDelimiter:@"\n" spriteKitObject:self]; }
@end
@implementation SKPhysicsBody (QuickLook)
-(id) valueForUndefinedKey:(NSString*)key { NSLog(@"%@: KVC ignored undefined key '%@', returning nil", NSStringFromClass([self class]), key); return nil; }
-(NSString*) debugDescription { return [QuickLookHelper commonDebugDescriptionForSpriteKitObject:self]; }
-(id) debugQuickLookObject { return [QuickLookHelper debugDescriptionStringWithDelimiter:@"\n" spriteKitObject:self]; }
@end
@implementation SKPhysicsWorld (QuickLook)
-(id) valueForUndefinedKey:(NSString*)key { NSLog(@"%@: KVC ignored undefined key '%@', returning nil", NSStringFromClass([self class]), key); return nil; }
-(NSString*) debugDescription { return [QuickLookHelper commonDebugDescriptionForSpriteKitObject:self]; }
-(id) debugQuickLookObject { return [QuickLookHelper debugDescriptionStringWithDelimiter:@"\n" spriteKitObject:self]; }
@end
@implementation SKPhysicsJoint (QuickLook)
-(id) valueForUndefinedKey:(NSString*)key { NSLog(@"%@: KVC ignored undefined key '%@', returning nil", NSStringFromClass([self class]), key); return nil; }
-(NSString*) debugDescription { return [QuickLookHelper commonDebugDescriptionForSpriteKitObject:self]; }
-(id) debugQuickLookObject { return [QuickLookHelper debugDescriptionStringWithDelimiter:@"\n" spriteKitObject:self]; }
@end
@implementation SKTransition (QuickLook)
-(id) valueForUndefinedKey:(NSString*)key { NSLog(@"%@: KVC ignored undefined key '%@', returning nil", NSStringFromClass([self class]), key); return nil; }
-(NSString*) debugDescription { return [QuickLookHelper commonDebugDescriptionForSpriteKitObject:self]; }
-(id) debugQuickLookObject { return [QuickLookHelper debugDescriptionStringWithDelimiter:@"\n" spriteKitObject:self]; }
@end
@implementation SKKeyframeSequence (QuickLook)
-(id) valueForUndefinedKey:(NSString*)key { NSLog(@"%@: KVC ignored undefined key '%@', returning nil", NSStringFromClass([self class]), key); return nil; }
-(NSString*) debugDescription { return [QuickLookHelper commonDebugDescriptionForSpriteKitObject:self]; }
-(id) debugQuickLookObject { return [QuickLookHelper debugDescriptionStringWithDelimiter:@"\n" spriteKitObject:self]; }
@end
@implementation QuickLookHelper
-(id) init
{
self = [super init];
if (self)
{
// avoid compiler warning
dumpIvarNamesForClass([SKNode class]);
/*
dumpIvarNamesForClass([SKAction class]);
dumpIvarNamesForClass([SKKeyframeSequence class]);
dumpIvarNamesForClass([SKTexture class]);
dumpIvarNamesForClass([SKTextureAtlas class]);
dumpIvarNamesForClass([SKTransition class]);
dumpIvarNamesForClass([SKView class]);
dumpIvarNamesForClass([SKPhysicsBody class]);
dumpIvarNamesForClass([SKPhysicsContact class]);
dumpIvarNamesForClass([SKPhysicsJoint class]);
dumpIvarNamesForClass([SKPhysicsJointFixed class]);
dumpIvarNamesForClass([SKPhysicsJointLimit class]);
dumpIvarNamesForClass([SKPhysicsJointPin class]);
dumpIvarNamesForClass([SKPhysicsJointSliding class]);
dumpIvarNamesForClass([SKPhysicsJointSpring class]);
dumpIvarNamesForClass([SKPhysicsWorld class]);
dumpIvarNamesForClass([SKNode class]);
dumpIvarNamesForClass([SKScene class]);
dumpIvarNamesForClass([SKCropNode class]);
dumpIvarNamesForClass([SKEffectNode class]);
dumpIvarNamesForClass([SKEmitterNode class]);
dumpIvarNamesForClass([SKLabelNode class]);
dumpIvarNamesForClass([SKShapeNode class]);
dumpIvarNamesForClass([SKSpriteNode class]);
dumpIvarNamesForClass([SKVideoNode class]);
// private classes
dumpIvarNamesForClass(NSClassFromString(@"SKTextureCache"));
dumpIvarNamesForClass(NSClassFromString(@"SKBitmapFont"));
*/
//dumpIvarNamesForClass([SKLabelNode class]);
/*
SKNode* node = [SKLabelNode node];
NSLog(@"%@", [node debugQuickLookObject]);
SKView* view = [[SKView alloc] init];
NSLog(@"%@", [view debugQuickLookObject]);
*/
}
return self;
}
+(BOOL) isSpriteKitClass:(Class)klass
{
return ([klass isSubclassOfClass:[SKNode class]] ||
[klass isSubclassOfClass:[SKTexture class]] ||
[klass isSubclassOfClass:[SKTextureAtlas class]] ||
[klass isSubclassOfClass:[SKAction class]] ||
[klass isSubclassOfClass:[SKView class]] ||
[klass isSubclassOfClass:[SKPhysicsBody class]] ||
[klass isSubclassOfClass:[SKPhysicsWorld class]] ||
[klass isSubclassOfClass:[SKPhysicsJoint class]] ||
[klass isSubclassOfClass:[SKTransition class]] ||
[klass isSubclassOfClass:[SKKeyframeSequence class]]);
}
+(NSDictionary*) debugClassVarDictionaryForSpriteKitObject:(id)object
{
NSMutableDictionary* classesDict = [NSMutableDictionary dictionary];
NSMutableArray* classOrder = [NSMutableArray array];
[classesDict setObject:classOrder forKey:@"classes"];
Class klass = [object class];
while ([QuickLookHelper isSpriteKitClass:klass])
{
NSMutableDictionary* ivarsDict = [NSMutableDictionary dictionary];
unsigned int count = 0;
objc_property_t* properties = class_copyPropertyList(klass, &count);
for (unsigned int i = 0; i < count; i++)
{
objc_property_t prop = properties[i];
NSString* propName = [NSString stringWithCString:property_getName(prop) encoding:NSUTF8StringEncoding];
id value = [object valueForKey:propName];
[ivarsDict setObject:value ? value : [NSNull null] forKey:propName];
}
count = 0;
Ivar* ivars = class_copyIvarList(klass, &count);
for (unsigned int i = 0; i < count; i++)
{
Ivar var = ivars[i];
NSString* ivarName = [NSString stringWithCString:ivar_getName(var) encoding:NSUTF8StringEncoding];
// only add ivars if there's no corresponding property of the same name
NSString* ivarNameWithoutUnderscore = [ivarName substringFromIndex:1];
if ([ivarsDict objectForKey:ivarNameWithoutUnderscore] == NO)
{
id value = [object valueForKey:ivarName];
[ivarsDict setObject:value ? value : [NSNull null] forKey:ivarName];
}
}
NSString* className = NSStringFromClass(klass);
[classesDict setObject:ivarsDict forKey:className];
[classOrder insertObject:className atIndex:0];
klass = [klass superclass];
}
return classesDict;
}
+(NSString*) formattedStringForValue:(id)value varName:(NSString*)varName
{
// TODO: physics body bitMasks
NSString* valueString = nil;
if ([varName isEqualToString:@"blendMode"] || [varName isEqualToString:@"particleBlendMode"])
{
switch ([value integerValue]) {
case SKBlendModeAdd:
valueString = @"Add";
break;
case SKBlendModeAlpha:
valueString = @"Alpha";
break;
case SKBlendModeMultiply:
valueString = @"Multiply";
break;
case SKBlendModeMultiplyX2:
valueString = @"MultiplyX2";
break;
case SKBlendModeReplace:
valueString = @"Replace";
break;
case SKBlendModeScreen:
valueString = @"Screen";
break;
case SKBlendModeSubtract:
valueString = @"Subtract";
break;
default:
valueString = [NSString stringWithFormat:@"%ld (unknown)", (unsigned long)[value integerValue]];
break;
}
}
else if ([varName isEqualToString:@"scaleMode"])
{
switch ([value integerValue]) {
case SKSceneScaleModeAspectFill:
valueString = @"AspectFill";
break;
case SKSceneScaleModeAspectFit:
valueString = @"AspectFit";
break;
case SKSceneScaleModeFill:
valueString = @"Fill";
break;
case SKSceneScaleModeResizeFill:
valueString = @"ResizeFill";
break;
default:
valueString = [NSString stringWithFormat:@"%ld (unknown)", (unsigned long)[value integerValue]];
break;
}
}
else if ([varName isEqualToString:@"verticalAlignmentMode"])
{
switch ([value integerValue]) {
case SKLabelVerticalAlignmentModeBaseline:
valueString = @"Baseline";
break;
case SKLabelVerticalAlignmentModeBottom:
valueString = @"Bottom";
break;
case SKLabelVerticalAlignmentModeCenter:
valueString = @"Center";
break;
case SKLabelVerticalAlignmentModeTop:
valueString = @"Top";
break;
default:
valueString = [NSString stringWithFormat:@"%ld (unknown)", (unsigned long)[value integerValue]];
break;
}
}
else if ([varName isEqualToString:@"horizontalAlignmentMode"])
{
switch ([value integerValue]) {
case SKLabelHorizontalAlignmentModeCenter:
valueString = @"Center";
break;
case SKLabelHorizontalAlignmentModeLeft:
valueString = @"Left";
break;
case SKLabelHorizontalAlignmentModeRight:
valueString = @"Right";
break;
default:
valueString = [NSString stringWithFormat:@"%ld (unknown)", (unsigned long)[value integerValue]];
break;
}
}
else if ([varName isEqualToString:@"filteringMode"])
{
switch ([value integerValue]) {
case SKTextureFilteringLinear:
valueString = @"Linear";
break;
case SKTextureFilteringNearest:
valueString = @"Nearest";
break;
default:
valueString = [NSString stringWithFormat:@"%ld (unknown)", (unsigned long)[value integerValue]];
break;
}
}
else if ([value isKindOfClass:[NSString class]])
{
valueString = [NSString stringWithFormat:@"'%@'", value];
}
else
{
valueString = [[value description] stringByReplacingOccurrencesOfString:@"\n" withString:@""];
}
return valueString;
}
+(NSString*) debugDescriptionStringWithDelimiter:(NSString*)delimiter spriteKitObject:(id)object
{
NSMutableString* desc = [NSMutableString string];
[desc appendFormat:@"%@%@", [object debugDescription], delimiter];
NSDictionary* classVars = [QuickLookHelper debugClassVarDictionaryForSpriteKitObject:object];
for (NSString* className in classVars[@"classes"])
{
[desc appendFormat:@"%@:%@", className, delimiter];
NSDictionary* ivars = classVars[className];
NSArray* keys = [ivars allKeys];
keys = [keys sortedArrayUsingComparator:^(id obj1, id obj2)
{
if ([(NSString*)obj1 hasPrefix:@"_"] && [(NSString*)obj2 hasPrefix:@"_"] == NO)
{
return NSOrderedDescending;
}
else if ([(NSString*)obj1 hasPrefix:@"_"] == NO && [(NSString*)obj2 hasPrefix:@"_"])
{
return NSOrderedAscending;
}
return [obj1 localizedCaseInsensitiveCompare:obj2];
}];
for (NSString* ivarName in keys)
{
id value = [ivars objectForKey:ivarName];
[desc appendFormat:@" %@ = %@%@", ivarName, [QuickLookHelper formattedStringForValue:value varName:ivarName], delimiter];
}
}
//NSLog(@"desc: \n%@ \n", desc);
return desc;
}
+(NSString*) commonDebugDescriptionForSpriteKitObject:(id)object
{
return [NSString stringWithFormat:@"<%p> %@", object, [object description]];
}
@end