Skip to content

Commit

Permalink
Fix[MCDL]: correct progress tracking
Browse files Browse the repository at this point in the history
  • Loading branch information
khanhduytran0 committed Jun 29, 2024
1 parent 073389e commit 1d2a19a
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 12 deletions.
6 changes: 3 additions & 3 deletions Natives/DownloadProgressViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
static void *TotalProgressObserverContext = &TotalProgressObserverContext;

@interface DownloadProgressViewController ()
@property BOOL needsReloadData;
@property NSInteger fileListCount;
@end

@implementation DownloadProgressViewController
Expand Down Expand Up @@ -61,9 +61,10 @@ - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(N
} else if (context == TotalProgressObserverContext) {
dispatch_async(dispatch_get_main_queue(), ^{
self.title = progress.localizedDescription;
if (self.needsReloadData) {
if (self.fileListCount != self.task.fileList.count) {
[self.tableView reloadData];
}
self.fileListCount = self.task.fileList.count;
});
} else {
[super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
Expand All @@ -72,7 +73,6 @@ - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(N

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
self.needsReloadData = self.task.fileList.count == 0;
return self.task.fileList.count;
}

Expand Down
29 changes: 20 additions & 9 deletions Natives/MinecraftResourceDownloadTask.m
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,14 @@ - (NSURLSessionDownloadTask *)createDownloadTask:(NSString *)url size:(NSUIntege

NSString *name = altName ?: path.lastPathComponent;
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:url]];
NSURLSessionDownloadTask *task = [self.manager downloadTaskWithRequest:request progress:nil
__block NSProgress *progress;
__block NSURLSessionDownloadTask *task = [self.manager downloadTaskWithRequest:request progress:nil
destination:^NSURL * _Nonnull(NSURL * _Nonnull targetPath, NSURLResponse * _Nonnull response) {
NSLog(@"[MCDL] Downloading %@", name);
progress = [self.manager downloadProgressForTask:task];
if (!size && task) {
[self addDownloadTaskToProgress:task size:response.expectedContentLength];
[self.fileList addObject:name];
}
[NSFileManager.defaultManager createDirectoryAtPath:path.stringByDeletingLastPathComponent withIntermediateDirectories:YES attributes:nil error:nil];
[NSFileManager.defaultManager removeItemAtPath:path error:nil];
Expand All @@ -58,12 +61,14 @@ - (NSURLSessionDownloadTask *)createDownloadTask:(NSString *)url size:(NSUIntege
} else if (![self checkSHA:sha forFile:path altName:altName]) {
[self finishDownloadWithErrorString:[NSString stringWithFormat:@"Failed to verify file %@: SHA1 mismatch", path.lastPathComponent]];
} else {
progress.totalUnitCount = progress.completedUnitCount;
if (success) success();
}
}];

if (size && task) {
[self addDownloadTaskToProgress:task size:size];
[self.fileList addObject:name];
}

return task;
Expand All @@ -73,11 +78,13 @@ - (NSURLSessionDownloadTask *)createDownloadTask:(NSString *)url size:(NSUIntege
return [self createDownloadTask:url size:size sha:sha altName:altName toPath:path success:nil];
}

- (void)addDownloadTaskToProgress:(NSURLSessionDownloadTask *)task size:(NSUInteger)size {
- (void)addDownloadTaskToProgress:(NSURLSessionDownloadTask *)task size:(NSInteger)size {
NSProgress *progress = [self.manager downloadProgressForTask:task];
NSUInteger fileSize = size ?: 1;
NSUInteger fileSize = size>0 ? size : 1;
progress.kind = NSProgressKindFile;
progress.totalUnitCount = fileSize;
if (size > 0) {
progress.totalUnitCount = fileSize;
}
[self.progressList addObject:progress];
[self.progress addChild:progress withPendingUnitCount:fileSize];
self.progress.totalUnitCount += fileSize;
Expand Down Expand Up @@ -146,11 +153,12 @@ - (void)downloadAssetMetadataWithSuccess:(void (^)())success {
success();
return;
}
NSString *path = [NSString stringWithFormat:@"%s/assets/indexes/%@.json", getenv("POJAV_GAME_DIR"), assetIndex[@"id"]];
NSString *name = [NSString stringWithFormat:@"assets/indexes/%@.json", assetIndex[@"id"]];
NSString *path = [@(getenv("POJAV_GAME_DIR")) stringByAppendingPathComponent:name];
NSString *url = assetIndex[@"url"];
NSString *sha = url.stringByDeletingLastPathComponent.lastPathComponent;
NSUInteger size = [assetIndex[@"size"] unsignedLongLongValue];
NSURLSessionDownloadTask *task = [self createDownloadTask:url size:size sha:sha altName:nil toPath:path success:^{
NSURLSessionDownloadTask *task = [self createDownloadTask:url size:size sha:sha altName:name toPath:path success:^{
self.metadata[@"assetIndexObj"] = parseJSONFromFile(path);
success();
}];
Expand Down Expand Up @@ -182,9 +190,8 @@ - (NSArray *)downloadClientLibraries {
continue;
}

NSURLSessionDownloadTask *task = [self createDownloadTask:url size:size sha:sha altName:nil toPath:path success:nil];
NSURLSessionDownloadTask *task = [self createDownloadTask:url size:size sha:sha altName:name toPath:path success:nil];
if (task) {
[self.fileList addObject:name];
[tasks addObject:task];
} else if (self.progress.cancelled) {
return nil;
Expand Down Expand Up @@ -224,7 +231,6 @@ - (NSArray *)downloadClientAssets {
NSString *url = [NSString stringWithFormat:@"https://resources.download.minecraft.net/%@", pathname];
NSURLSessionDownloadTask *task = [self createDownloadTask:url size:size sha:hash altName:name toPath:path success:nil];
if (task) {
[self.fileList addObject:name];
[tasks addObject:task];
} else if (self.progress.cancelled) {
return nil;
Expand All @@ -239,6 +245,9 @@ - (void)downloadVersion:(NSDictionary *)version {
[self downloadAssetMetadataWithSuccess:^{
NSArray *libTasks = [self downloadClientLibraries];
NSArray *assetTasks = [self downloadClientAssets];
// Drop the 1 byte we set initially
self.progress.totalUnitCount--;
self.textProgress.totalUnitCount--;
if (self.progress.totalUnitCount == 0) {
// We have nothing to download, invoke completion observer
self.progress.totalUnitCount = 1;
Expand Down Expand Up @@ -284,6 +293,8 @@ - (void)prepareForDownload {
self.textProgress.totalUnitCount = -1;

self.progress = [NSProgress new];
// Push 1 byte so it won't accidentally finish after downloading assets index
self.progress.totalUnitCount = 1;
[self.fileList removeAllObjects];
[self.progressList removeAllObjects];
}
Expand Down

0 comments on commit 1d2a19a

Please sign in to comment.