Ejemplo n.º 1
0
        /// <summary>
        /// 更新下载器
        /// </summary>
        public void Update()
        {
            if (DownloadStates != EDownloaderStates.Loading)
            {
                return;
            }

            // 检测下载器结果
            for (int i = _loaders.Count - 1; i >= 0; i--)
            {
                var loader = _loaders[i];
                if (loader.IsDone() == false)
                {
                    continue;
                }

                PatchBundle element = loader.UserData as PatchBundle;

                // 检测是否下载失败
                if (loader.HasError())
                {
                    loader.ReportError();
                    loader.Dispose();
                    _loaders.RemoveAt(i);
                    _loadFailedList.Add(element);
                    continue;
                }

                // 验证下载文件完整性
                if (_patcher.CheckContentIntegrity(element) == false)
                {
                    MotionLog.Error($"Check download content integrity is failed : {element.BundleName}");
                    loader.Dispose();
                    _loaders.RemoveAt(i);
                    _checkFailedList.Add(element);
                    continue;
                }

                // 下载成功
                loader.Dispose();
                _loaders.RemoveAt(i);
                _succeedList.Add(element);
                CurrentDownloadCount++;
                CurrentDownloadBytes += element.SizeBytes;
                OnPatchFileDownloadSucceedCallback?.Invoke(TotalDownloadCount, CurrentDownloadCount, TotalDownloadBytes, CurrentDownloadBytes);
            }

            // 动态创建新的下载器到最大数量限制
            // 注意:如果期间有下载失败的文件,暂停动态创建下载器
            if (_downloadList.Count > 0 && _loadFailedList.Count == 0 && _checkFailedList.Count == 0)
            {
                if (_loaders.Count < _maxNumberOnLoad)
                {
                    int            index      = _downloadList.Count - 1;
                    WebFileRequest downloader = CreateDownloader(_downloadList[index]);
                    _loaders.Add(downloader);
                    _downloadList.RemoveAt(index);
                }
            }

            // 下载结算
            if (_loaders.Count == 0)
            {
                // 更新缓存并保存
                if (_succeedList.Count > 0)
                {
                    _patcher.CacheDownloadPatchFiles(_succeedList);
                }

                if (_loadFailedList.Count > 0)
                {
                    DownloadStates = EDownloaderStates.Failed;
                    OnPatchFileDownloadFailedCallback?.Invoke(_loadFailedList[0].BundleName);
                }
                else if (_checkFailedList.Count > 0)
                {
                    DownloadStates = EDownloaderStates.Failed;
                    OnPatchFileCheckFailedCallback?.Invoke(_checkFailedList[0].BundleName);
                }
                else
                {
                    // 结算成功
                    DownloadStates = EDownloaderStates.Succeed;
                }
            }
        }
Ejemplo n.º 2
0
        /// <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);
                }
            }
        }