3737#import " CCDirector_Private.h"
3838#import " CCPhysics+ObjectiveChipmunk.h"
3939#import " CCAnimationManager_Private.h"
40+ #import " CCEffectStack.h"
4041
4142#ifdef CCB_ENABLE_UNZIP
4243#import " SSZipArchive.h"
5152@interface CCBFile : CCNode
5253{
5354 CCNode* ccbFile;
55+
5456}
5557@property (nonatomic ,strong ) CCNode* ccbFile;
5658@end
5759
5860
5961@interface CCBReader ()
62+ {
63+
64+ }
6065
6166@property (nonatomic , copy ) NSString *currentCCBFile;
6267
68+
6369@end
6470
6571
@@ -122,7 +128,7 @@ - (id) init
122128 animationManager.rootContainerSize = [CCDirector sharedDirector ].designSize ;
123129
124130 nodeMapping = [NSMutableDictionary dictionary ];
125-
131+ postDeserializationUUIDFixup = [ NSMutableArray array ];
126132 return self;
127133}
128134
@@ -912,10 +918,12 @@ - (void) readPropertyForNode:(CCNode*) node parent:(CCNode*)parent isExtraProp:(
912918 else if (type == kCCBPropTypeNodeReference )
913919 {
914920 int uuid = readIntWithSign (self, NO );
915- CCNode * mappedNode = nodeMapping[@(uuid)];
916- NSAssert (mappedNode != nil , @" CCBReader: Failed to find node UUID:%i " , uuid);
917- [node setValue: mappedNode forKey: name];
918- }
921+
922+ // Node references are fixed after the whole node graph is deserialized (since since the node ref may not be deserialized yet)
923+ NSArray * queuedFixupTask = @[node, name, @(uuid)];
924+ [postDeserializationUUIDFixup addObject: queuedFixupTask];
925+
926+ }
919927 else if (type == kCCBPropTypeFloatCheck )
920928 {
921929 float f = readFloat (self);
@@ -927,12 +935,71 @@ - (void) readPropertyForNode:(CCNode*) node parent:(CCNode*)parent isExtraProp:(
927935 [node setValue: @(f) forKey: name];
928936 }
929937 }
938+ else if (type == kCCBPropTypeEffects )
939+ {
940+ CCEffect * effect = [self readEffects ];
941+
942+ if (effect)
943+ {
944+ // Hmmm..... Force it to write to @"effect" property.
945+ [node setValue: effect forKey: @" effect" ];
946+ }
947+
948+ }
930949 else
931950 {
932951 NSAssert (false , @" [PROPERTY] %@ - Failed to read property type %d , node class name: \" %@ \" , name: \" %@ \" , in ccb file: \" %@ \" " , name, type, [node class ], [node name ], _currentCCBFile);
933952 }
934953}
935954
955+
956+
957+ // Either returns a CCStackEffect or the one single effect.
958+ -(CCEffect*)readEffects
959+ {
960+
961+ int numberOfEffects = readIntWithSign (self, NO );
962+
963+ if (numberOfEffects == 0 )
964+ {
965+ return nil ;
966+ }
967+
968+ NSMutableArray * effectsStack = [NSMutableArray array ];
969+
970+ for (int i = 0 ; i < numberOfEffects; i++) {
971+ NSString * className = [self readCachedString ];
972+
973+ Class nodeClass = NSClassFromString (className);
974+ if (nodeClass == nil )
975+ {
976+ NSAssert (nil , @" CCBReader: Could not create class named: %@ " , className);
977+ return nil ;
978+ }
979+
980+ CCEffect* effect = [[nodeClass alloc ] init ];
981+
982+ int propCount = readIntWithSign (self,NO );
983+
984+ for (int propIndex = 0 ; propIndex < propCount; propIndex++)
985+ {
986+ // Just lie and let the property reader do its work.
987+ [self readPropertyForNode: (CCNode*)effect parent: nil isExtraProp: NO ];
988+
989+ }
990+
991+ if (numberOfEffects == 1 )
992+ {
993+ return effect;
994+ }
995+ [effectsStack addObject: effect];
996+
997+ }
998+
999+ return [[CCEffectStack alloc ] initWithArray: effectsStack];
1000+
1001+ }
1002+
9361003- (BOOL )isPropertyKeySettable : (NSString *)key onInstance : (id )instance
9371004{
9381005 if (!key || !instance || ([key length ] == 0 ))
@@ -1071,6 +1138,21 @@ - (void) didLoadFromCCB
10711138{
10721139}
10731140
1141+ -(void )postDeserialization
1142+ {
1143+ // Post deserialization fixup.
1144+ for (NSArray * task in postDeserializationUUIDFixup) {
1145+ id node = task[0 ];
1146+ NSString * name = task[1 ];
1147+ int uuid = (int )[task[2 ] integerValue ];
1148+
1149+ CCNode * mappedNode = nodeMapping[@(uuid)];
1150+ NSAssert (mappedNode != nil , @" CCBReader: Failed to find node UUID:%i " , uuid);
1151+ [node setValue: mappedNode forKey: name];
1152+
1153+ }
1154+ }
1155+
10741156-(void )readJoints
10751157{
10761158 int numJoints = readIntWithSign (self, NO );
@@ -1675,6 +1757,7 @@ - (CCNode*) readFileWithCleanUp:(BOOL)cleanUp actionManagers:(NSMutableDictionar
16751757
16761758 CCNode* node = [self readNodeGraphParent: NULL ];
16771759 [self readJoints ];
1760+ [self postDeserialization ];
16781761
16791762 [actionManagers setObject: self .animationManager forKey: [NSValue valueWithPointer: (__bridge const void *)(node)]];
16801763
0 commit comments