Skip to content

Commit 5bfc94c

Browse files
committed
CCPackageManager's API changed.
An instance of a package can now be added to the manager to actually get managed without starting the whole installation life cycle. Leading to a new method to start the download of a CCPackage instance which has already been added before. The unzip delegate methods will return on the main thread now. Some refactoring to attach installData with a method instead of code duplication.
1 parent 0a57376 commit 5bfc94c

File tree

3 files changed

+106
-34
lines changed

3 files changed

+106
-34
lines changed

cocos2d/CCPackageManager.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,17 @@
8282
*/
8383
- (CCPackage *)downloadPackageWithName:(NSString *)name resolution:(NSString *)resolution remoteURL:(NSURL *)remoteURL enableAfterDownload:(BOOL)enableAfterDownload;
8484

85+
/**
86+
* Downloads a package. This is supposed to work in conjunction with addPackage where a package is created without the package manager
87+
* and should become managed.
88+
* A download will only start if the status is CCPackageStatusInitial, CCPackageStatusDownloadFailed.
89+
* A package with status CCPackageStatusDownloadPaused will be resumed if possible.
90+
*
91+
* @param name The package to be manager by the package manager
92+
* @param enableAfterDownload If the package should be enabled in cocos2d after download. You can enable it with the enablePackage: method later on.
93+
*/
94+
- (BOOL)downloadPackage:(CCPackage *)package enableAfterDownload:(BOOL)enableAfterDownload;
95+
8596
/**
8697
* Disables a package. Only packages with state CCPackageStatusInstalledEnabled can be disabled.
8798
* The package is removed from cocos2d's search, sprite sheets and filename lookups are reloaded.
@@ -105,6 +116,13 @@
105116
*/
106117
- (BOOL)enablePackage:(CCPackage *)package error:(NSError **)error;
107118

119+
/**
120+
* Adds a package to the package manager. Only packages with status initial can be added.
121+
*
122+
* @param package The package to be added to the package manager
123+
*/
124+
- (void)addPackage:(CCPackage *)package;
125+
108126
/**
109127
* Deletes a package.
110128
* Will disable the package first and delete it from disk. Temp download and unzip files will be removed as well.

cocos2d/CCPackageManager.m

Lines changed: 88 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -188,18 +188,35 @@ - (CCPackage *)downloadPackageWithName:(NSString *)name resolution:(NSString *)r
188188
return [self downloadPackageWithName:name resolution:resolution remoteURL:remoteURL enableAfterDownload:enableAfterDownload];
189189
}
190190

191-
- (NSString *)currentOS
191+
- (BOOL)downloadPackage:(CCPackage *)package enableAfterDownload:(BOOL)enableAfterDownload
192192
{
193-
#ifdef __CC_PLATFORM_IOS
194-
return @"iOS";
193+
if (![_packages containsObject:package])
194+
{
195+
return NO;
196+
}
195197

196-
#elif defined(__CC_PLATFORM_MAC)
197-
return @"Mac";
198+
NSAssert(package, @"package must not be nil");
199+
NSAssert(package.name, @"package.name must not be nil");
200+
NSAssert(package.resolution, @"package.resolution must not be nil");
198201

199-
#elif defined(__CC_PLATFORM_ANDROID)
200-
return @"Android";
202+
if (!package.remoteURL && !_baseURL)
203+
{
204+
return NO;
205+
}
206+
else if (!package.remoteURL)
207+
{
208+
NSString *packageName = [NSString stringWithFormat:@"%@-%@-%@.zip", package.name, package.os, package.resolution];
209+
NSURL *remoteURL = [_baseURL URLByAppendingPathComponent:packageName];
210+
[package setValue:remoteURL forKey:@"remoteURL"];
211+
}
212+
213+
[self attachNewInstallDataToPackage:package enableAfterDownload:enableAfterDownload];
214+
215+
CCLOGINFO(@"[PACKAGE][INFO]: adding package to download queue: %@", package);
201216

202-
#endif
217+
[_downloadManager enqueuePackageForDownload:package];
218+
219+
return YES;
203220
}
204221

205222
- (CCPackage *)downloadPackageWithName:(NSString *)name resolution:(NSString *)resolution remoteURL:(NSURL *)remoteURL enableAfterDownload:(BOOL)enableAfterDownload
@@ -210,10 +227,8 @@ - (CCPackage *)downloadPackageWithName:(NSString *)name resolution:(NSString *)r
210227
return aPackage;
211228
}
212229

213-
CCPackageInstallData *installData = [[CCPackageInstallData alloc] initWithPackage:package];
214-
installData.enableOnDownload = enableAfterDownload;
215-
[package setInstallData:installData];
216230
CCPackage *package = [[CCPackage alloc] initWithName:name resolution:resolution remoteURL:remoteURL];
231+
[self attachNewInstallDataToPackage:package enableAfterDownload:enableAfterDownload];
217232

218233
[_packages addObject:package];
219234

@@ -224,6 +239,13 @@ - (CCPackage *)downloadPackageWithName:(NSString *)name resolution:(NSString *)r
224239
return package;
225240
}
226241

242+
- (void)attachNewInstallDataToPackage:(CCPackage *)package enableAfterDownload:(BOOL)enableAfterDownload
243+
{
244+
CCPackageInstallData *installData = [[CCPackageInstallData alloc] initWithPackage:package];
245+
installData.enableOnDownload = enableAfterDownload;
246+
[package setInstallData:installData];
247+
}
248+
227249
- (CCPackage *)packageWithName:(NSString *)name resolution:(NSString *)resolution
228250
{
229251
for (CCPackage *aPackage in _packages)
@@ -266,38 +288,47 @@ - (void)downloadProgressOfPackage:(CCPackage *)package downloadedBytes:(NSUInteg
266288

267289
- (void)unzipFinished:(CCPackageUnzipper *)packageUnzipper
268290
{
269-
[self removeDownloadFile:packageUnzipper.package];
291+
[self runOnMainQueue:^
292+
{
293+
[self removeDownloadFile:packageUnzipper.package];
270294

271-
[_unzipTasks removeObject:packageUnzipper];
295+
[_unzipTasks removeObject:packageUnzipper];
272296

273-
[packageUnzipper.package setValue:@(CCPackageStatusUnzipped) forKey:@"status"];
297+
[packageUnzipper.package setValue:@(CCPackageStatusUnzipped) forKey:@"status"];
274298

275-
if ([_delegate respondsToSelector:@selector(packageUnzippingFinished:)])
276-
{
277-
[_delegate packageUnzippingFinished:packageUnzipper.package];
278-
}
299+
if ([_delegate respondsToSelector:@selector(packageUnzippingFinished:)])
300+
{
301+
[_delegate packageUnzippingFinished:packageUnzipper.package];
302+
}
279303

280-
if (![self installPackage:packageUnzipper.package])
281-
{
282-
return;
283-
}
304+
if (![self installPackage:packageUnzipper.package])
305+
{
306+
return;
307+
}
284308

285-
[self tidyUpAfterInstallation:packageUnzipper.package];
309+
[self tidyUpAfterInstallation:packageUnzipper.package];
310+
}];
286311
}
287312

288313
- (void)unzipFailed:(CCPackageUnzipper *)packageUnzipper error:(NSError *)error
289314
{
290-
[_unzipTasks removeObject:packageUnzipper];
315+
[self runOnMainQueue:^
316+
{
317+
[_unzipTasks removeObject:packageUnzipper];
291318

292-
[_delegate packageUnzippingFailed:packageUnzipper.package error:error];
319+
[_delegate packageUnzippingFailed:packageUnzipper.package error:error];
320+
}];
293321
}
294322

295323
- (void)unzipProgress:(CCPackageUnzipper *)packageUnzipper unzippedBytes:(NSUInteger)unzippedBytes totalBytes:(NSUInteger)totalBytes
296324
{
297-
if ([_delegate respondsToSelector:@selector(packageUnzippingProgress:unzippedBytes:totalBytes:)])
325+
[self runOnMainQueue:^
298326
{
299-
[_delegate packageUnzippingProgress:packageUnzipper.package unzippedBytes:unzippedBytes totalBytes:totalBytes];
300-
}
327+
if ([_delegate respondsToSelector:@selector(packageUnzippingProgress:unzippedBytes:totalBytes:)])
328+
{
329+
[_delegate packageUnzippingProgress:packageUnzipper.package unzippedBytes:unzippedBytes totalBytes:totalBytes];
330+
}
331+
}];
301332
}
302333

303334

@@ -383,6 +414,7 @@ - (BOOL)installPackage:(CCPackage *)package
383414
{
384415
CCLOG(@"[PACKAGE][ERROR] Installation failed: %@", error);
385416

417+
[package setValue:@(CCPackageStatusInstallationFailed) forKey:NSStringFromSelector(@selector(status))];
386418
[_delegate packageInstallationFailed:package error:error];
387419
return NO;
388420
}
@@ -551,13 +583,27 @@ - (BOOL)enablePackage:(CCPackage *)package error:(NSError **)error
551583
return YES;
552584
}
553585

586+
- (void)addPackage:(CCPackage *)package;
587+
{
588+
NSAssert(package, @"package must not be nil");
589+
NSAssert(package.status == CCPackageStatusInitial, @"package status must be CCPackageStatusInitial");
590+
591+
if ([_packages containsObject:package]
592+
|| [self packageWithName:package.name resolution:package.resolution])
593+
{
594+
return;
595+
}
596+
597+
[_packages addObject:package];
598+
}
599+
554600
- (BOOL)deletePackage:(CCPackage *)package error:(NSError **)error
555601
{
556602
CCPackageCocos2dEnabler *packageCocos2dEnabler = [[CCPackageCocos2dEnabler alloc] init];
557603
[packageCocos2dEnabler disablePackages:@[package]];
558604

559605
[_packages removeObject:package];
560-
[self storePackages];
606+
[self savePackages];
561607

562608
[_downloadManager cancelDownloadOfPackage:package];
563609

@@ -599,7 +645,7 @@ - (void)cancelDownloadOfPackage:(CCPackage *)package
599645

600646
[_downloadManager cancelDownloadOfPackage:package];
601647

602-
[self storePackages];
648+
[self savePackages];
603649
}
604650

605651
- (void)pauseDownloadOfPackage:(CCPackage *)package
@@ -630,4 +676,16 @@ - (void)request:(NSMutableURLRequest *)request ofPackage:(CCPackage *)package
630676
}
631677
}
632678

679+
- (void)runOnMainQueue:(dispatch_block_t)block
680+
{
681+
if ([NSThread isMainThread])
682+
{
683+
block();
684+
}
685+
else
686+
{
687+
dispatch_sync(dispatch_get_main_queue(), block);
688+
}
689+
}
690+
633691
@end

cocos2d/CCPackageManagerDelegate.h

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,15 +53,13 @@
5353

5454
/**
5555
* Only called when the process of unzipping finished successfully
56-
* Note: This method will most likely return on a different thread than the main thread depending on the queue set for unzipping.
5756
*
5857
* @param package The package for which the unzip process finished
5958
*/
6059
- (void)packageUnzippingFinished:(CCPackage *)package;
6160

6261
/**
6362
* Only called when the process of unzipping failed
64-
* Note: This method will most likely return on a different thread than the main thread depending on the queue set for unzipping.
6563
*
6664
* @param package The package for which the unzip process failed
6765
* @param error Pointer to an error object
@@ -71,8 +69,6 @@
7169
@optional
7270
/**
7371
* Called whenever the process of unzipping reports a progress in bytes
74-
* Note: This method will most likely return on a different thread than the main thread depending on the queue set for unzipping.
75-
*
7672
*
7773
* @param package The package for which the unzip progress occured
7874
* @param unzippedBytes Unzip progress in bytes

0 commit comments

Comments
 (0)