Skip to content

Commit c1d27e5

Browse files
committed
Package classes added.
1 parent 5c3e259 commit c1d27e5

26 files changed

+2463
-1
lines changed

cocos2d-ios.xcodeproj/project.pbxproj

Lines changed: 133 additions & 1 deletion
Large diffs are not rendered by default.

cocos2d/CCPackage+InstallData.h

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#import <Foundation/Foundation.h>
2+
#import "CCPackage.h"
3+
4+
@class CCPackageInstallData;
5+
6+
7+
@interface CCPackage (InstallData)
8+
9+
- (CCPackageInstallData *)installData;
10+
11+
// Attaches install data to the package as an associated object
12+
- (void)setInstallData:(CCPackageInstallData *)installData;
13+
14+
// Removes any install data attached to the package if there is any
15+
- (void)removeInstallData;
16+
17+
// Reads values of dictionary and sets them on the install data
18+
// InstallData has to be attached to the package already
19+
- (void)populateInstallDataWithDictionary:(NSDictionary *)dictionary;
20+
21+
// Returns a dictionary with the install data's values
22+
- (void)writeInstallDataToDictionary:(NSMutableDictionary *)dictionary;
23+
24+
@end

cocos2d/CCPackage+InstallData.m

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
#import <objc/runtime.h>
2+
#import "CCPackage+InstallData.h"
3+
#import "CCPackageInstallData.h"
4+
5+
NSString *const PACKAGE_SERIALIZATION_KEY_LOCAL_DOWNLOAD_URL = @"localDownloadURL";
6+
NSString *const PACKAGE_SERIALIZATION_KEY_LOCAL_UNZIP_URL = @"localUnzipURL";
7+
NSString *const PACKAGE_SERIALIZATION_KEY_FOLDER_NAME = @"folderName";
8+
NSString *const PACKAGE_SERIALIZATION_KEY_ENABLE_ON_DOWNLOAD = @"enableOnDownload";
9+
10+
char *const KEY_INSTALL_DATA = "installData";
11+
12+
13+
@implementation CCPackage (InstallData)
14+
15+
- (CCPackageInstallData *)installData
16+
{
17+
CCPackageInstallData *result = objc_getAssociatedObject(self, KEY_INSTALL_DATA);
18+
19+
return result;
20+
}
21+
22+
- (void)populateInstallDataWithDictionary:(NSDictionary *)dictionary
23+
{
24+
CCPackageInstallData *installData = [self installData];
25+
26+
if (dictionary[PACKAGE_SERIALIZATION_KEY_LOCAL_DOWNLOAD_URL])
27+
{
28+
installData.localDownloadURL = [NSURL URLWithString:dictionary[PACKAGE_SERIALIZATION_KEY_LOCAL_DOWNLOAD_URL]];
29+
}
30+
31+
if (dictionary[PACKAGE_SERIALIZATION_KEY_LOCAL_UNZIP_URL])
32+
{
33+
installData.unzipURL = [NSURL URLWithString:dictionary[PACKAGE_SERIALIZATION_KEY_LOCAL_UNZIP_URL]];
34+
}
35+
36+
if (dictionary[PACKAGE_SERIALIZATION_KEY_FOLDER_NAME])
37+
{
38+
installData.folderName = dictionary[PACKAGE_SERIALIZATION_KEY_FOLDER_NAME];
39+
}
40+
41+
if (dictionary[PACKAGE_SERIALIZATION_KEY_FOLDER_NAME])
42+
{
43+
installData.enableOnDownload = [dictionary[PACKAGE_SERIALIZATION_KEY_ENABLE_ON_DOWNLOAD] boolValue];
44+
}
45+
}
46+
47+
- (void)setInstallData:(CCPackageInstallData *)installData
48+
{
49+
if (!installData)
50+
{
51+
return;
52+
}
53+
54+
objc_setAssociatedObject(self, KEY_INSTALL_DATA, installData, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
55+
}
56+
57+
- (void)removeInstallData
58+
{
59+
objc_setAssociatedObject(self, KEY_INSTALL_DATA, nil, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
60+
}
61+
62+
- (void)writeInstallDataToDictionary:(NSMutableDictionary *)dictionary
63+
{
64+
CCPackageInstallData *installData = [self installData];
65+
66+
if (installData.localDownloadURL)
67+
{
68+
dictionary[PACKAGE_SERIALIZATION_KEY_LOCAL_DOWNLOAD_URL] = [installData.localDownloadURL absoluteString];
69+
}
70+
71+
if (installData.unzipURL)
72+
{
73+
dictionary[PACKAGE_SERIALIZATION_KEY_LOCAL_UNZIP_URL] = [installData.unzipURL absoluteString];
74+
}
75+
76+
if (installData.folderName)
77+
{
78+
dictionary[PACKAGE_SERIALIZATION_KEY_FOLDER_NAME] = installData.folderName;
79+
}
80+
81+
dictionary[PACKAGE_SERIALIZATION_KEY_ENABLE_ON_DOWNLOAD] = @(installData.enableOnDownload);
82+
}
83+
84+
@end

cocos2d/CCPackage.h

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#import <Foundation/Foundation.h>
2+
#import "CCPackageTypes.h"
3+
4+
@class CCPackageManager;
5+
6+
@interface CCPackage : NSObject
7+
8+
@property (nonatomic, copy, readonly) NSString *name;
9+
@property (nonatomic, copy, readonly) NSString *resolution;
10+
@property (nonatomic, copy, readonly) NSString *os;
11+
@property (nonatomic, copy, readonly) NSURL *remoteURL;
12+
@property (nonatomic, copy, readonly) NSURL *installURL;
13+
@property (nonatomic, readonly) CCPackageStatus status;
14+
15+
- (instancetype)initWithName:(NSString *)name
16+
resolution:(NSString *)resolution
17+
os:(NSString *)os
18+
remoteURL:(NSURL *)remoteURL;
19+
20+
// Returns a new instance of a package populated with the contents of the dictionary
21+
- (instancetype)initWithDictionary:(NSDictionary *)dictionary;
22+
23+
// Returns a dictionary containing the values of the package's properties.
24+
- (NSDictionary *)toDictionary;
25+
26+
// Returns an identifier of the package: The pattern is <NAME>-<OS>-<RESOLUTION>. Example: DLC-iOS-phonehd.
27+
// This name can vary though and can be determined by delegation.
28+
- (NSString *)standardIdentifier;
29+
30+
// Debug
31+
- (NSString *)statusToString;
32+
33+
@end

cocos2d/CCPackage.m

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
#import "CCPackage.h"
2+
#import "CCPackageInstallData.h"
3+
#import "CCPackage+InstallData.h"
4+
5+
6+
NSUInteger PACKAGE_SERIALIZATION_VERSION = 1;
7+
NSString *const PACKAGE_SERIALIZATION_KEY_NAME = @"name";
8+
NSString *const PACKAGE_SERIALIZATION_KEY_RESOLUTION = @"resolution";
9+
NSString *const PACKAGE_SERIALIZATION_KEY_OS = @"os";
10+
NSString *const PACKAGE_SERIALIZATION_KEY_REMOTE_URL = @"remoteURL";
11+
NSString *const PACKAGE_SERIALIZATION_KEY_INSTALL_URL = @"installURL";
12+
NSString *const PACKAGE_SERIALIZATION_KEY_VERSION = @"version";
13+
NSString *const PACKAGE_SERIALIZATION_KEY_STATUS = @"status";
14+
15+
16+
@interface CCPackage()
17+
18+
@property (nonatomic, copy, readwrite) NSString *name;
19+
@property (nonatomic, copy, readwrite) NSString *resolution;
20+
@property (nonatomic, copy, readwrite) NSString *os;
21+
@property (nonatomic, copy, readwrite) NSURL *remoteURL;
22+
@property (nonatomic, copy, readwrite) NSString *folderName;
23+
@property (nonatomic, copy, readwrite) NSURL *installURL;
24+
@property (nonatomic, readwrite) CCPackageStatus status;
25+
26+
@end
27+
28+
29+
@implementation CCPackage
30+
31+
- (instancetype)initWithName:(NSString *)name resolution:(NSString *)resolution os:(NSString *)os remoteURL:(NSURL *)remoteURL
32+
{
33+
NSAssert(name != nil, @"name must not be nil");
34+
NSAssert(resolution != nil, @"resolution must not be nil");
35+
NSAssert(os != nil, @"os must not be nil");
36+
NSAssert(remoteURL != nil, @"remoteURL must not be nil");
37+
38+
self = [super init];
39+
if (self)
40+
{
41+
self.name = name;
42+
self.resolution = resolution;
43+
self.os = os;
44+
self.remoteURL = remoteURL;
45+
self.status = CCPackageStatusInitial;
46+
}
47+
48+
return self;
49+
}
50+
51+
- (instancetype)initWithDictionary:(NSDictionary *)dictionary
52+
{
53+
CCPackage *package = [[CCPackage alloc] initWithName:dictionary[PACKAGE_SERIALIZATION_KEY_NAME]
54+
resolution:dictionary[PACKAGE_SERIALIZATION_KEY_RESOLUTION]
55+
os:dictionary[PACKAGE_SERIALIZATION_KEY_OS]
56+
remoteURL:[NSURL URLWithString:dictionary[PACKAGE_SERIALIZATION_KEY_REMOTE_URL]]];
57+
58+
package.installURL = [NSURL URLWithString:dictionary[PACKAGE_SERIALIZATION_KEY_INSTALL_URL]];
59+
package.status = (CCPackageStatus) [dictionary[PACKAGE_SERIALIZATION_KEY_STATUS] unsignedIntegerValue];
60+
61+
CCPackageInstallData *installData = [[CCPackageInstallData alloc] initWithPackage:package];
62+
[package setInstallData:installData];
63+
[package populateInstallDataWithDictionary:dictionary];
64+
65+
return package;
66+
}
67+
68+
- (NSString *)standardIdentifier
69+
{
70+
return [NSString stringWithFormat:@"%@-%@-%@", _name, _os, _resolution];
71+
}
72+
73+
- (NSDictionary *)toDictionary
74+
{
75+
NSMutableDictionary *dictionary = [NSMutableDictionary dictionary];
76+
77+
dictionary[PACKAGE_SERIALIZATION_KEY_STATUS] = @(_status);
78+
dictionary[PACKAGE_SERIALIZATION_KEY_NAME] = _name;
79+
dictionary[PACKAGE_SERIALIZATION_KEY_RESOLUTION] = _resolution;
80+
dictionary[PACKAGE_SERIALIZATION_KEY_OS] = _os;
81+
dictionary[PACKAGE_SERIALIZATION_KEY_REMOTE_URL] = [_remoteURL absoluteString];
82+
dictionary[PACKAGE_SERIALIZATION_KEY_VERSION] = @(PACKAGE_SERIALIZATION_VERSION);
83+
if (_installURL)
84+
{
85+
dictionary[PACKAGE_SERIALIZATION_KEY_INSTALL_URL] = [_installURL absoluteString];
86+
}
87+
88+
[self writeInstallDataToDictionary:dictionary];
89+
90+
return dictionary;
91+
}
92+
93+
- (NSString *)description
94+
{
95+
CCPackageInstallData *installData = [self installData];
96+
97+
return [NSString stringWithFormat:@"Name: %@, resolution: %@, os: %@, status: %d, folder name: %@\nremoteURL: %@\ninstallURL: %@\nunzipURL: %@\ndownloadURL: %@\n",
98+
_name, _resolution, _os, _status, installData.folderName, _remoteURL, _installURL, installData.unzipURL, installData.localDownloadURL];
99+
}
100+
101+
- (NSString *)statusToString
102+
{
103+
switch (_status)
104+
{
105+
case CCPackageStatusInitial :
106+
return @"Initial";
107+
case CCPackageStatusDownloading :
108+
return @"Downloading";
109+
case CCPackageStatusDownloadPaused :
110+
return @"Download Paused";
111+
case CCPackageStatusDownloaded :
112+
return @"Downloaded";
113+
case CCPackageStatusUnzipping :
114+
return @"Unzipping";
115+
case CCPackageStatusUnzipped :
116+
return @"Unzipped";
117+
case CCPackageStatusInstalledEnabled :
118+
return @"Installed/Enabled";
119+
case CCPackageStatusInstalledDisabled :
120+
return @"Installed/Disabled";
121+
122+
default : return @"Unknown";
123+
}
124+
}
125+
126+
@end

cocos2d/CCPackageCocos2dEnabler.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#import <Foundation/Foundation.h>
2+
3+
@interface CCPackageCocos2dEnabler : NSObject
4+
5+
// Enables packages by adding to cocos2d's search path and loading sprite sheets and filename lookups
6+
- (void)enablePackages:(NSArray *)packages;
7+
8+
// Disables packages by removing them fromcocos2d's search path after that reloading sprite sheets and filename lookups of remaining search paths.
9+
- (void)disablePackages:(NSArray *)array;
10+
11+
@end

cocos2d/CCPackageCocos2dEnabler.m

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
#import "CCPackageCocos2dEnabler.h"
2+
#import "CCTextureCache.h"
3+
#import "CCPackage.h"
4+
#import "CCFileUtils.h"
5+
#import "CCSpriteFrameCache.h"
6+
7+
8+
@implementation CCPackageCocos2dEnabler
9+
10+
- (void)enablePackages:(NSArray *)packages
11+
{
12+
[self addPackagestoSearchPath:packages];
13+
14+
CCLOGINFO(@"[PACKAGE/INSTALL][INFO] Enable packages - Search path: %@", [CCFileUtils sharedFileUtils].searchPath);
15+
16+
[self reloadCocos2dFiles];
17+
}
18+
19+
- (void)disablePackages:(NSArray *)array
20+
{
21+
[self removePackagesFromSearchPath:array];
22+
23+
CCLOGINFO(@"[PACKAGE/INSTALL][INFO] Disable packages - Search path: %@", [CCFileUtils sharedFileUtils].searchPath);
24+
25+
[self reloadCocos2dFiles];
26+
}
27+
28+
- (void)addPackagestoSearchPath:(NSArray *)packages
29+
{
30+
for (CCPackage *aPackage in packages)
31+
{
32+
NSMutableArray *newSearchPath = [[CCFileUtils sharedFileUtils].searchPath mutableCopy];
33+
NSString *newPackagePath = aPackage.installURL.path;
34+
35+
if (![newSearchPath containsObject:newPackagePath])
36+
{
37+
[newSearchPath insertObject:newPackagePath atIndex:0];
38+
}
39+
40+
[CCFileUtils sharedFileUtils].searchPath = newSearchPath;
41+
}
42+
}
43+
44+
- (void)reloadCocos2dFiles
45+
{
46+
[[CCFileUtils sharedFileUtils] purgeCachedEntries];
47+
[CCSpriteFrameCache purgeSharedSpriteFrameCache];
48+
[CCTextureCache purgeSharedTextureCache];
49+
50+
[[CCFileUtils sharedFileUtils] loadFileNameLookupsInAllSearchPathsWithName:@"fileLookup.plist"];
51+
52+
[[CCSpriteFrameCache sharedSpriteFrameCache] loadSpriteFrameLookupsInAllSearchPathsWithName:@"spriteFrameFileList.plist"];
53+
}
54+
55+
- (void)removePackagesFromSearchPath:(NSArray *)packages
56+
{
57+
for (CCPackage *aPackage in packages)
58+
{
59+
NSMutableArray *newSearchPath = [[CCFileUtils sharedFileUtils].searchPath mutableCopy];
60+
NSString *packagePathToRemove = aPackage.installURL.path;
61+
62+
if ([newSearchPath containsObject:packagePathToRemove])
63+
{
64+
[newSearchPath removeObject:packagePathToRemove];
65+
}
66+
67+
[CCFileUtils sharedFileUtils].searchPath = newSearchPath;
68+
}
69+
}
70+
71+
@end

cocos2d/CCPackageConstants.h

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
extern NSUInteger const PACKAGE_ERROR_DOWNLOAD_SERVER_RESPONSE_NOT_OK;
2+
3+
extern NSUInteger const PACKAGE_ERROR_INSTALL_PACKAGE_NOT_FOUND;
4+
5+
extern NSUInteger const PACKAGE_ERROR_INSTALL_COULD_NOT_MOVE_PACKAGE;
6+
7+
extern NSUInteger const PACKAGE_ERROR_INSTALL_PACKAGE_EMPTY;
8+
9+
extern NSUInteger const PACKAGE_ERROR_INSTALL_PACKAGE_FOLDER_NAME_NOT_FOUND;
10+
11+
extern NSUInteger const PACKAGE_ERROR_MANAGER_CANNOT_ENABLE_NON_DISABLED_PACKAGE;
12+
13+
extern NSUInteger const PACKAGE_ERROR_MANAGER_CANNOT_DISABLE_NON_ENABLED_PACKAGE;
14+
15+
extern NSString *const PACKAGE_REL_DOWNLOAD_FOLDER;
16+
17+
extern NSString *const PACKAGE_REL_UNZIP_FOLDER;
18+
19+
extern NSString *const PACKAGE_STORAGE_USERDEFAULTS_KEY;

0 commit comments

Comments
 (0)