Skip to content

Commit 019272a

Browse files
committed
Bugfix for CCPackageDownloadManager and resuming downloads.
In case of resuming a download of package that was not stored initially in the download manager by enqueuing a resume would fail. This was the case when an app loads persisted packages with the status CCPackageStatusDownloadPaused. The download object is created and added to the manager if there is none.
1 parent 4d03ca2 commit 019272a

File tree

3 files changed

+33
-1
lines changed

3 files changed

+33
-1
lines changed

UnitTests/CCPackageDownloadManagerTests.m

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#import "CCDirector.h"
1414
#import "AppDelegate.h"
1515
#import "CCUnitTestAssertions.h"
16+
#import "CCPackage_private.h"
1617

1718
@interface CCPackageDownloadManagerTestURLProtocol : NSURLProtocol @end
1819

@@ -175,6 +176,22 @@ - (void)testEnqueuePausedPackage
175176
[self assertPackagesDownloadedAndContentsAreAsExpected:@[package1]];
176177
}
177178

179+
- (void)testResumeDownloadAfterLoadingPackages
180+
{
181+
// This test aims at the situation when coming back from persistency and
182+
// the package manager resume downloads.
183+
184+
CCPackage *package = [self completePackageWithName:@"package"];
185+
package.status = CCPackageStatusDownloadPaused;
186+
package.localDownloadURL = [NSURL fileURLWithPath:[_downloadManager.downloadPath stringByAppendingPathComponent:@"foo.zip"]];
187+
188+
[_downloadManager enqueuePackageForDownload:package];
189+
190+
[self waitUntilDelegateReturns];
191+
192+
[self assertPackagesDownloadedAndContentsAreAsExpected:@[package]];
193+
}
194+
178195
- (void)waitUntilDelegateReturns
179196
{
180197
while (!_allDownloadsReturned)

cocos2d/CCPackageDownloadManager.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,8 @@
4545
- (void)enqueuePackageForDownload:(CCPackage *)package;
4646

4747
/**
48-
* Cancels a download of a given package. Downloaded file will be deleted.
48+
* Cancels a download of a given package. Downloaded data will be deleted.
49+
* Status of package is reset to CCPackageStatusInitial.
4950
*
5051
* @param package The package that should be cancelled
5152
*/

cocos2d/CCPackageDownloadManager.m

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,11 +126,25 @@ - (void)pauseDownloadOfPackage:(CCPackage *)package
126126

127127
- (void)resumeDownloadOfPackage:(CCPackage *)package
128128
{
129+
[self createDownloadIfNotExistForPackage:package];
130+
129131
CCLOGINFO(@"[PACKAGE/DOWNLOADS][INFO] Resuming download of package %@.", package);
130132
CCPackageDownload *packageDownload = [self packageDownloadForPackage:package];
131133
[packageDownload resume];
132134
}
133135

136+
- (void)createDownloadIfNotExistForPackage:(CCPackage *)package
137+
{
138+
if (![self packageDownloadForPackage:package])
139+
{
140+
CCPackageDownload *packageDownload = [[CCPackageDownload alloc] initWithPackage:package
141+
localURL:package.localDownloadURL];
142+
packageDownload.delegate = self;
143+
144+
[_downloads addObject:packageDownload];
145+
}
146+
}
147+
134148
- (void)pauseAllDownloads
135149
{
136150
CCLOGINFO(@"[PACKAGE/DOWNLOADS][INFO] Pausing all downloads.");

0 commit comments

Comments
 (0)