@@ -188,18 +188,35 @@ - (CCPackage *)downloadPackageWithName:(NSString *)name resolution:(NSString *)r
188
188
return [self downloadPackageWithName: name resolution: resolution remoteURL: remoteURL enableAfterDownload: enableAfterDownload];
189
189
}
190
190
191
- - (NSString *)currentOS
191
+ - (BOOL ) downloadPackage : (CCPackage *)package enableAfterDownload : ( BOOL ) enableAfterDownload
192
192
{
193
- #ifdef __CC_PLATFORM_IOS
194
- return @" iOS" ;
193
+ if (![_packages containsObject: package])
194
+ {
195
+ return NO ;
196
+ }
195
197
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" );
198
201
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);
201
216
202
- #endif
217
+ [_downloadManager enqueuePackageForDownload: package];
218
+
219
+ return YES ;
203
220
}
204
221
205
222
- (CCPackage *)downloadPackageWithName : (NSString *)name resolution : (NSString *)resolution remoteURL : (NSURL *)remoteURL enableAfterDownload : (BOOL )enableAfterDownload
@@ -210,10 +227,8 @@ - (CCPackage *)downloadPackageWithName:(NSString *)name resolution:(NSString *)r
210
227
return aPackage;
211
228
}
212
229
213
- CCPackageInstallData *installData = [[CCPackageInstallData alloc ] initWithPackage: package];
214
- installData.enableOnDownload = enableAfterDownload;
215
- [package setInstallData: installData];
216
230
CCPackage *package = [[CCPackage alloc ] initWithName: name resolution: resolution remoteURL: remoteURL];
231
+ [self attachNewInstallDataToPackage: package enableAfterDownload: enableAfterDownload];
217
232
218
233
[_packages addObject: package];
219
234
@@ -224,6 +239,13 @@ - (CCPackage *)downloadPackageWithName:(NSString *)name resolution:(NSString *)r
224
239
return package;
225
240
}
226
241
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
+
227
249
- (CCPackage *)packageWithName : (NSString *)name resolution : (NSString *)resolution
228
250
{
229
251
for (CCPackage *aPackage in _packages)
@@ -266,38 +288,47 @@ - (void)downloadProgressOfPackage:(CCPackage *)package downloadedBytes:(NSUInteg
266
288
267
289
- (void )unzipFinished : (CCPackageUnzipper *)packageUnzipper
268
290
{
269
- [self removeDownloadFile: packageUnzipper.package];
291
+ [self runOnMainQueue: ^
292
+ {
293
+ [self removeDownloadFile: packageUnzipper.package];
270
294
271
- [_unzipTasks removeObject: packageUnzipper];
295
+ [_unzipTasks removeObject: packageUnzipper];
272
296
273
- [packageUnzipper.package setValue: @(CCPackageStatusUnzipped) forKey: @" status" ];
297
+ [packageUnzipper.package setValue: @(CCPackageStatusUnzipped) forKey: @" status" ];
274
298
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
+ }
279
303
280
- if (![self installPackage: packageUnzipper.package])
281
- {
282
- return ;
283
- }
304
+ if (![self installPackage: packageUnzipper.package])
305
+ {
306
+ return ;
307
+ }
284
308
285
- [self tidyUpAfterInstallation: packageUnzipper.package];
309
+ [self tidyUpAfterInstallation: packageUnzipper.package];
310
+ }];
286
311
}
287
312
288
313
- (void )unzipFailed : (CCPackageUnzipper *)packageUnzipper error : (NSError *)error
289
314
{
290
- [_unzipTasks removeObject: packageUnzipper];
315
+ [self runOnMainQueue: ^
316
+ {
317
+ [_unzipTasks removeObject: packageUnzipper];
291
318
292
- [_delegate packageUnzippingFailed: packageUnzipper.package error: error];
319
+ [_delegate packageUnzippingFailed: packageUnzipper.package error: error];
320
+ }];
293
321
}
294
322
295
323
- (void )unzipProgress : (CCPackageUnzipper *)packageUnzipper unzippedBytes : (NSUInteger )unzippedBytes totalBytes : (NSUInteger )totalBytes
296
324
{
297
- if ([_delegate respondsToSelector: @selector ( packageUnzippingProgress:unzippedBytes:totalBytes: )])
325
+ [ self runOnMainQueue: ^
298
326
{
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
+ }];
301
332
}
302
333
303
334
@@ -383,6 +414,7 @@ - (BOOL)installPackage:(CCPackage *)package
383
414
{
384
415
CCLOG (@" [PACKAGE][ERROR] Installation failed: %@ " , error);
385
416
417
+ [package setValue: @(CCPackageStatusInstallationFailed) forKey: NSStringFromSelector (@selector (status ))];
386
418
[_delegate packageInstallationFailed: package error: error];
387
419
return NO ;
388
420
}
@@ -551,13 +583,27 @@ - (BOOL)enablePackage:(CCPackage *)package error:(NSError **)error
551
583
return YES ;
552
584
}
553
585
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
+
554
600
- (BOOL )deletePackage : (CCPackage *)package error : (NSError **)error
555
601
{
556
602
CCPackageCocos2dEnabler *packageCocos2dEnabler = [[CCPackageCocos2dEnabler alloc ] init ];
557
603
[packageCocos2dEnabler disablePackages: @[package]];
558
604
559
605
[_packages removeObject: package];
560
- [self storePackages ];
606
+ [self savePackages ];
561
607
562
608
[_downloadManager cancelDownloadOfPackage: package];
563
609
@@ -599,7 +645,7 @@ - (void)cancelDownloadOfPackage:(CCPackage *)package
599
645
600
646
[_downloadManager cancelDownloadOfPackage: package];
601
647
602
- [self storePackages ];
648
+ [self savePackages ];
603
649
}
604
650
605
651
- (void )pauseDownloadOfPackage : (CCPackage *)package
@@ -630,4 +676,16 @@ - (void)request:(NSMutableURLRequest *)request ofPackage:(CCPackage *)package
630
676
}
631
677
}
632
678
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
+
633
691
@end
0 commit comments