Skip to content

Commit 44600d2

Browse files
committed
Support for default resolution added.
Default resolution is determined by CCFileUtil's searchResolutionsOrder array and is mapped to a sprite builder resolution. CCPackageManager now offers a simple method to download a package only by providing a name. CCPackage can now be initialized with a name only. Some tests added for these cases, although CCPackageHelper tests are disabled at the moment due to a bug in CCFileUtils and handling of searchResolutionsOrder.
1 parent a962c58 commit 44600d2

File tree

9 files changed

+209
-8
lines changed

9 files changed

+209
-8
lines changed

UnitTests/CCPackageHelperTests.m

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
//
2+
// CCPackageHelperTests.m
3+
// cocos2d-tests-ios
4+
//
5+
// Created by Nicky Weber on 29.09.14.
6+
// Copyright (c) 2014 Cocos2d. All rights reserved.
7+
//
8+
9+
#import <XCTest/XCTest.h>
10+
#import "CCFileUtils.h"
11+
#import "CCPackageHelper.h"
12+
#import "CCUnitTestAssertions.h"
13+
14+
@interface CCPackageHelperTests : XCTestCase
15+
16+
@property (nonatomic, strong) NSMutableArray *searchResolutionsOrderBackup;
17+
18+
@end
19+
20+
21+
@implementation CCPackageHelperTests
22+
23+
/*
24+
- (void)testDefaultResolution
25+
{
26+
// Standard test
27+
[CCFileUtils sharedFileUtils].searchResolutionsOrder = @[CCFileUtilsSuffixiPhoneHD, CCFileUtilsSuffixiPadHD];
28+
NSString *mappedResolution = [CCPackageHelper ccFileUtilsSuffixToResolution:CCFileUtilsSuffixiPhoneHD];
29+
NSString *defaultResolution = [CCPackageHelper defaultResolution];
30+
CCAssertEqualStrings(defaultResolution, mappedResolution);
31+
32+
// Ignore non mappable entriy and pick next
33+
[CCFileUtils sharedFileUtils].searchResolutionsOrder = @[@"weird_nonsense", CCFileUtilsSuffixiPadHD];
34+
NSString *mappedResolution2 = [CCPackageHelper ccFileUtilsSuffixToResolution:CCFileUtilsSuffixiPadHD];
35+
NSString *defaultResolution2 = [CCPackageHelper defaultResolution];
36+
CCAssertEqualStrings(defaultResolution2, mappedResolution2);
37+
38+
// Return default since nothing can be mapped
39+
[CCFileUtils sharedFileUtils].searchResolutionsOrder = @[@"nothing_to_be_mapped"];
40+
NSString *defaultResolution3 = [CCPackageHelper defaultResolution];
41+
CCAssertEqualStrings(defaultResolution3, @"phonehd");
42+
43+
// Empty array
44+
[CCFileUtils sharedFileUtils].searchResolutionsOrder = @[];
45+
NSString *defaultResolution4 = [CCPackageHelper defaultResolution];
46+
CCAssertEqualStrings(defaultResolution4, @"phonehd");
47+
}
48+
*/
49+
50+
@end

UnitTests/CCPackageManagerTests.m

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,28 +7,41 @@
77
//
88

99
#import <XCTest/XCTest.h>
10+
#import "CCPackageManager.h"
11+
#import "CCPackage.h"
12+
#import "CCFileUtils.h"
1013

1114
@interface CCPackageManagerTests : XCTestCase
1215

16+
@property (nonatomic, strong) CCPackageManager *packageManager;
17+
1318
@end
1419

1520
@implementation CCPackageManagerTests
1621

1722
- (void)setUp
1823
{
1924
[super setUp];
20-
// Put setup code here. This method is called before the invocation of each test method in the class.
25+
self.packageManager = [[CCPackageManager alloc] init];
2126
}
2227

23-
- (void)tearDown
28+
- (void)testPackageWithName
2429
{
25-
// Put teardown code here. This method is called after the invocation of each test method in the class.
26-
[super tearDown];
27-
}
30+
// NOTE: This will kill many other tests due to a bug in CCFileUtils
31+
// So we assume until the bug's fixed that phonehd is the default resolution
32+
// [CCFileUtils sharedFileUtils].searchResolutionsOrder = @[CCFileUtilsSuffixiPadHD];
2833

29-
- (void)testExample
30-
{
31-
XCTFail(@"No implementation for \"%s\"", __PRETTY_FUNCTION__);
34+
CCPackage *aPackage = [[CCPackage alloc] initWithName:@"foo"
35+
// resolution:@"tablethd" // See note above
36+
resolution:@"phonehd"
37+
os:@"iOS"
38+
remoteURL:[NSURL URLWithString:@"http://foo.fake"]];
39+
40+
[_packageManager addPackage:aPackage];
41+
42+
CCPackage *result = [_packageManager packageWithName:@"foo"];
43+
44+
XCTAssertEqual(aPackage, result);
3245
}
3346

3447
@end

cocos2d-tests-ios.xcodeproj/project.pbxproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,7 @@
143143
D4AFE94D1977100000261299 /* west_england-64.png in Resources */ = {isa = PBXBuildFile; fileRef = D4AFE9121977100000261299 /* west_england-64.png */; };
144144
E525F033EECC363CA8EAF2A6 /* CCPackageDownloadManagerTests.m in Sources */ = {isa = PBXBuildFile; fileRef = E525F2B770C5914D0810E4C2 /* CCPackageDownloadManagerTests.m */; };
145145
E525F23949076BAB5337F7AD /* CCPackage+InstallDataTests.m in Sources */ = {isa = PBXBuildFile; fileRef = E525F935C2ECADF41CA1B1F2 /* CCPackage+InstallDataTests.m */; };
146+
E525F4D0568A8A4176684936 /* CCPackageHelperTests.m in Sources */ = {isa = PBXBuildFile; fileRef = E525F99055258F35DFF00ECD /* CCPackageHelperTests.m */; };
146147
E525F5DB1E3086DDE0296CEC /* CCPackageDownloadTests.m in Sources */ = {isa = PBXBuildFile; fileRef = E525F5FACFF098F0ACBA6419 /* CCPackageDownloadTests.m */; };
147148
E525F7C977339D17470FC149 /* CCPackageInstallerTests.m in Sources */ = {isa = PBXBuildFile; fileRef = E525FBF58DFECF27272F5D6F /* CCPackageInstallerTests.m */; };
148149
E525F7FD04A53FA8700B4890 /* CCPackageInstallDataTests.m in Sources */ = {isa = PBXBuildFile; fileRef = E525FA40B9446D6820F622B4 /* CCPackageInstallDataTests.m */; };
@@ -363,6 +364,7 @@
363364
E525F771C583AAF68EB42E15 /* CCPackageTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = CCPackageTests.m; sourceTree = "<group>"; };
364365
E525F832B569117922DA339E /* CCUnitTestAssertions.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = CCUnitTestAssertions.h; sourceTree = "<group>"; };
365366
E525F935C2ECADF41CA1B1F2 /* CCPackage+InstallDataTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "CCPackage+InstallDataTests.m"; sourceTree = "<group>"; };
367+
E525F99055258F35DFF00ECD /* CCPackageHelperTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = CCPackageHelperTests.m; sourceTree = "<group>"; };
366368
E525FA40B9446D6820F622B4 /* CCPackageInstallDataTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = CCPackageInstallDataTests.m; sourceTree = "<group>"; };
367369
E525FB8A3CC89D071A8377B1 /* CCPackageCocos2dEnablerTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = CCPackageCocos2dEnablerTests.m; sourceTree = "<group>"; };
368370
E525FBF58DFECF27272F5D6F /* CCPackageInstallerTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = CCPackageInstallerTests.m; sourceTree = "<group>"; };
@@ -763,6 +765,7 @@
763765
E525F544D63123C8E948AA05 /* Unzip */,
764766
E525F771C583AAF68EB42E15 /* CCPackageTests.m */,
765767
E525F395FE85323D86232416 /* CCPackageManagerTests.m */,
768+
E525F99055258F35DFF00ECD /* CCPackageHelperTests.m */,
766769
);
767770
name = Packages;
768771
sourceTree = "<group>";
@@ -1009,6 +1012,7 @@
10091012
E525FF2E8351476CDD7DEAC4 /* CCPackageManagerTests.m in Sources */,
10101013
E525F033EECC363CA8EAF2A6 /* CCPackageDownloadManagerTests.m in Sources */,
10111014
E525F7FD04A53FA8700B4890 /* CCPackageInstallDataTests.m in Sources */,
1015+
E525F4D0568A8A4176684936 /* CCPackageHelperTests.m in Sources */,
10121016
);
10131017
runOnlyForDeploymentPostprocessing = 0;
10141018
};

cocos2d/CCPackage.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,21 @@
3535
*/
3636
@property (nonatomic, readonly) CCPackageStatus status;
3737

38+
/**
39+
* Creates a new instance of a package.
40+
* OS and resolution are determined implicitly. Resolution is derived from CCFileUtils' searchResolutionsOrder first entry.
41+
*
42+
* @param name Name of the package
43+
* @param remoteURL Remote URL of the package
44+
*
45+
* @return New instance of CCPackage
46+
*/
47+
- (instancetype)initWithName:(NSString *)name
48+
remoteURL:(NSURL *)remoteURL;
3849

3950
/**
4051
* Creates a new instance of a package.
52+
* OS is determined implicitly.
4153
*
4254
* @param name Name of the package
4355
* @param resolution Resolution of the package

cocos2d/CCPackage.m

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#import <MacTypes.h>
12
#import "CCPackage.h"
23
#import "CCPackageHelper.h"
34

@@ -54,6 +55,14 @@ - (instancetype)initWithName:(NSString *)name resolution:(NSString *)resolution
5455
remoteURL:remoteURL];
5556
}
5657

58+
- (instancetype)initWithName:(NSString *)name remoteURL:(NSURL *)remoteURL
59+
{
60+
return [[CCPackage alloc] initWithName:name
61+
resolution:[CCPackageHelper defaultResolution]
62+
os:[CCPackageHelper currentOS]
63+
remoteURL:remoteURL];
64+
}
65+
5766
- (instancetype)initWithDictionary:(NSDictionary *)dictionary
5867
{
5968
CCPackage *package = [[CCPackage alloc] initWithName:dictionary[PACKAGE_SERIALIZATION_KEY_NAME]

cocos2d/CCPackageHelper.h

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,23 @@
33

44
@interface CCPackageHelper : NSObject
55

6+
/**
7+
* Returns the current OS, possible values: iOS, Android and Mac
8+
*/
69
+ (NSString *)currentOS;
710

11+
/**
12+
* Returns a spritebuilder resolution(phone, phonehd, tablet, tablethd) for a CCFileUtil's device/resolution suffix like CCFileUtilsSuffixiPadHD
13+
* If none given or not found it'll return nil.
14+
*
15+
* @param suffix
16+
*/
17+
+ (NSString *)ccFileUtilsSuffixToResolution:(NSString *)suffix;
18+
19+
/**
20+
* Returns the preferred Sprite Builder resolution(phone, phonehd, tablet, tablethd) for the current cocos2d setup by looking at the entries
21+
* in CCFileUtils searchResolutionsOrder array. If none can be found or mapped phonehd will be returned as default.
22+
*/
23+
+ (NSString *)defaultResolution;
24+
825
@end

cocos2d/CCPackageHelper.m

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1+
#import <MacTypes.h>
12
#import "CCPackageHelper.h"
3+
#import "CCFileUtils.h"
24

35

46
@implementation CCPackageHelper
@@ -18,4 +20,48 @@ + (NSString *)currentOS
1820
return @"iOS";
1921
}
2022

23+
+ (NSString *)ccFileUtilsSuffixToResolution:(NSString *)suffix
24+
{
25+
if ([suffix isEqualToString:CCFileUtilsSuffixiPhone5HD]
26+
|| [suffix isEqualToString:CCFileUtilsSuffixiPhone5]
27+
|| [suffix isEqualToString:CCFileUtilsSuffixiPhoneHD]
28+
|| [suffix isEqualToString:CCFileUtilsSuffixDefault])
29+
{
30+
return @"phonehd";
31+
}
32+
33+
if ([suffix isEqualToString:CCFileUtilsSuffixiPhone])
34+
{
35+
return @"phone";
36+
}
37+
38+
if ([suffix isEqualToString:CCFileUtilsSuffixiPadHD]
39+
|| [suffix isEqualToString:CCFileUtilsSuffixMacHD])
40+
{
41+
return @"tablethd";
42+
}
43+
44+
if ([suffix isEqualToString:CCFileUtilsSuffixMac]
45+
|| [suffix isEqualToString:CCFileUtilsSuffixiPad])
46+
{
47+
return @"tablet";
48+
}
49+
50+
return nil;
51+
}
52+
53+
+ (NSString *)defaultResolution
54+
{
55+
for (NSString *resolution in [CCFileUtils sharedFileUtils].searchResolutionsOrder)
56+
{
57+
NSString *result = [CCPackageHelper ccFileUtilsSuffixToResolution:resolution];
58+
if (result)
59+
{
60+
return result;
61+
}
62+
}
63+
64+
return @"phonehd";
65+
}
66+
2167
@end

cocos2d/CCPackageManager.h

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,17 @@
5555
*/
5656
- (void)savePackages;
5757

58+
/**
59+
* Returns a package identified by name. Resolution and OS are determined implicitly.
60+
*
61+
* @param name Name of the package
62+
*/
63+
- (CCPackage *)packageWithName:(NSString *)name;
5864

5965
/**
6066
* The all inclsuive method to add a package to your app.
6167
* Returns a new package immediately which will be downloaded, unzipped and installed asynchronously to the Packages folder in /Library/Caches (default)
68+
* OS and resolution are determined implicitly. Resolution is derived from CCFileUtils' searchResolutionsOrder first entry.
6269
*
6370
* If a package with the same name and resolution already exists it won't be rescheduled for downloading.
6471
* If you need to update a package by re-downloading it you will have to delete it first.
@@ -67,14 +74,35 @@
6774
* The URL is determined by the baseURL property and the standard identifier created by the name, os and resolution.
6875
* Example: base is http://foo, name: DLC, os: iOS (determined by manager), resolution: phonehd => http://foo/DLC-iOS-phonehd.zip
6976
*
77+
* @param name Name of the package
78+
* @param enableAfterDownload If the package should be enabled in cocos2d after download. You can enable it with the enablePackage: method later on.
79+
*/
80+
- (CCPackage *)downloadPackageWithName:(NSString *)name enableAfterDownload:(BOOL)enableAfterDownload;
81+
82+
/**
83+
* Like the method above. Instead of using the baseURL, name and resolution you can provide the URL directly.
84+
* Returns a new package immediately which will be downloaded, unzipped and installed asynchronously to the Packages folder in /Library/Caches (default)
85+
* OS is determined implicitly.
86+
*
7087
* @param name Name of the package
7188
* @param resolution Resolution of the package, e.g. phonehd, tablethd etc.
7289
* @param enableAfterDownload If the package should be enabled in cocos2d after download. You can enable it with the enablePackage: method later on.
7390
*/
7491
- (CCPackage *)downloadPackageWithName:(NSString *)name resolution:(NSString *)resolution enableAfterDownload:(BOOL)enableAfterDownload;
7592

93+
/**
94+
* Like the method above. Instead of using the baseURL and name you can provide the URL directly.
95+
* OS and resolution are determined implicitly. Resolution is derived from CCFileUtils' searchResolutionsOrder first entry.
96+
*
97+
* @param name Name of the package
98+
* @param remoteURL URL of the package to be downloaded
99+
* @param enableAfterDownload If the package should be enabled in cocos2d after download. You can enable it with the enablePackage: method later on.
100+
*/
101+
- (CCPackage *)downloadPackageWithName:(NSString *)name remoteURL:(NSURL *)remoteURL enableAfterDownload:(BOOL)enableAfterDownload;
102+
76103
/**
77104
* Like the method above. Instead of using the baseURL, name and resolution you can provide the URL directly.
105+
* OS is determined implicitly.
78106
*
79107
* @param name Name of the package
80108
* @param resolution Resolution of the package, e.g. phonehd, tablethd etc.

cocos2d/CCPackageManager.m

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,13 @@ - (void)setUnzippingQueue:(dispatch_queue_t)unzippingQueue
219219

220220
#pragma mark - download
221221

222+
- (CCPackage *)downloadPackageWithName:(NSString *)name enableAfterDownload:(BOOL)enableAfterDownload
223+
{
224+
return [self downloadPackageWithName:name
225+
resolution:[CCPackageHelper defaultResolution]
226+
enableAfterDownload:enableAfterDownload];
227+
}
228+
222229
- (CCPackage *)downloadPackageWithName:(NSString *)name resolution:(NSString *)resolution enableAfterDownload:(BOOL)enableAfterDownload
223230
{
224231
NSAssert(_baseURL != nil, @"baseURL must not be nil");
@@ -260,6 +267,14 @@ - (BOOL)downloadPackage:(CCPackage *)package enableAfterDownload:(BOOL)enableAft
260267
return YES;
261268
}
262269

270+
- (CCPackage *)downloadPackageWithName:(NSString *)name remoteURL:(NSURL *)remoteURL enableAfterDownload:(BOOL)enableAfterDownload
271+
{
272+
return [self downloadPackageWithName:name
273+
resolution:[CCPackageHelper defaultResolution]
274+
remoteURL:remoteURL
275+
enableAfterDownload:enableAfterDownload];
276+
}
277+
263278
- (CCPackage *)downloadPackageWithName:(NSString *)name resolution:(NSString *)resolution remoteURL:(NSURL *)remoteURL enableAfterDownload:(BOOL)enableAfterDownload
264279
{
265280
CCPackage *aPackage = [self packageWithName:name resolution:resolution];
@@ -287,6 +302,13 @@ - (void)attachNewInstallDataToPackage:(CCPackage *)package enableAfterDownload:(
287302
[package setInstallData:installData];
288303
}
289304

305+
- (CCPackage *)packageWithName:(NSString *)name
306+
{
307+
NSString *resolution = [CCPackageHelper defaultResolution];
308+
309+
return [self packageWithName:name resolution:resolution];
310+
}
311+
290312
- (CCPackage *)packageWithName:(NSString *)name resolution:(NSString *)resolution
291313
{
292314
for (CCPackage *aPackage in _packages)

0 commit comments

Comments
 (0)