/// <summary> /// 更新下载器 /// </summary> public void Update() { if (DownloadStates != EDownloaderStates.Loading) { return; } // 检测下载器结果 _removeList.Clear(); long downloadBytes = CurrentDownloadBytes; foreach (var loader in _downloaders) { downloadBytes += (long)loader.DownloadedBytes; if (loader.IsDone() == false) { continue; } PatchBundle patchBundle = loader.UserData as PatchBundle; // 检测是否下载失败 if (loader.HasError()) { loader.ReportError(); loader.Dispose(); _removeList.Add(loader); _loadFailedList.Add(patchBundle); continue; } // 验证下载文件完整性 if (_patcherMgr.CheckContentIntegrity(patchBundle) == false) { MotionLog.Error($"Check download content integrity is failed : {patchBundle.BundleName}"); loader.Dispose(); _removeList.Add(loader); _checkFailedList.Add(patchBundle); continue; } // 下载成功 loader.Dispose(); _removeList.Add(loader); _succeedList.Add(patchBundle); CurrentDownloadCount++; CurrentDownloadBytes += patchBundle.SizeBytes; } // 移除已经完成的下载器(无论成功或失败) foreach (var loader in _removeList) { _downloaders.Remove(loader); } // 如果下载进度发生变化 if (_lastDownloadBytes != downloadBytes || _lastDownloadCount != CurrentDownloadCount) { _lastDownloadBytes = downloadBytes; _lastDownloadCount = CurrentDownloadCount; OnDownloadProgressCallback?.Invoke(TotalDownloadCount, _lastDownloadCount, TotalDownloadBytes, _lastDownloadBytes); } // 动态创建新的下载器到最大数量限制 // 注意:如果期间有下载失败的文件,暂停动态创建下载器 if (_downloadList.Count > 0 && _loadFailedList.Count == 0 && _checkFailedList.Count == 0) { if (_downloaders.Count < _maxNumberOnLoad) { int index = _downloadList.Count - 1; WebFileRequest downloader = CreateDownloader(_downloadList[index]); _downloaders.Add(downloader); _downloadList.RemoveAt(index); } } // 下载结算 if (_downloaders.Count == 0) { // 更新缓存并保存 if (_succeedList.Count > 0) { _patcherMgr.CacheDownloadPatchFiles(_succeedList); } if (_loadFailedList.Count > 0) { DownloadStates = EDownloaderStates.Failed; OnPatchFileDownloadFailedCallback?.Invoke(_loadFailedList[0].BundleName); OnDownloadOverCallback?.Invoke(false); } else if (_checkFailedList.Count > 0) { DownloadStates = EDownloaderStates.Failed; OnPatchFileCheckFailedCallback?.Invoke(_checkFailedList[0].BundleName); OnDownloadOverCallback?.Invoke(false); } else { // 结算成功 DownloadStates = EDownloaderStates.Succeed; OnDownloadOverCallback?.Invoke(true); } } }
internal override void Update() { if (_steps == ESteps.None || _steps == ESteps.Done) { return; } if (_steps == ESteps.Check) { if (_downloadList == null) { _steps = ESteps.Done; Status = EOperationStatus.Failed; Error = "Download list is null."; } else { _steps = ESteps.Loading; } } if (_steps == ESteps.Loading) { // 检测下载器结果 _removeList.Clear(); long downloadBytes = CurrentDownloadBytes; foreach (var downloader in _downloaders) { downloadBytes += (long)downloader.DownloadedBytes; if (downloader.IsDone() == false) { continue; } BundleInfo bundleInfo = downloader.GetBundleInfo(); // 检测是否下载失败 if (downloader.HasError()) { _removeList.Add(downloader); _loadFailedList.Add(bundleInfo); continue; } // 下载成功 _removeList.Add(downloader); CurrentDownloadCount++; CurrentDownloadBytes += bundleInfo.SizeBytes; } // 移除已经完成的下载器(无论成功或失败) foreach (var loader in _removeList) { _downloaders.Remove(loader); } // 如果下载进度发生变化 if (_lastDownloadBytes != downloadBytes || _lastDownloadCount != CurrentDownloadCount) { _lastDownloadBytes = downloadBytes; _lastDownloadCount = CurrentDownloadCount; Progress = (float)_lastDownloadBytes / TotalDownloadBytes; OnDownloadProgressCallback?.Invoke(TotalDownloadCount, _lastDownloadCount, TotalDownloadBytes, _lastDownloadBytes); } // 动态创建新的下载器到最大数量限制 // 注意:如果期间有下载失败的文件,暂停动态创建下载器 if (_downloadList.Count > 0 && _loadFailedList.Count == 0) { if (_isPause) { return; } if (_downloaders.Count < _downloadingMaxNumber) { int index = _downloadList.Count - 1; var operation = DownloadSystem.BeginDownload(_downloadList[index], _failedTryAgain); _downloaders.Add(operation); _downloadList.RemoveAt(index); } } // 下载结算 if (_downloaders.Count == 0) { if (_loadFailedList.Count > 0) { string fileName = _loadFailedList[0].BundleName; Error = $"Failed to download file : {fileName}"; _steps = ESteps.Done; Status = EOperationStatus.Failed; OnDownloadFileFailedCallback?.Invoke(fileName); OnDownloadOverCallback?.Invoke(false); } else { // 结算成功 _steps = ESteps.Done; Status = EOperationStatus.Succeed; OnDownloadOverCallback?.Invoke(true); } } } }