From 43248408fc637af0e1917bf27416644d0b7f9bfd Mon Sep 17 00:00:00 2001 From: hevinci Date: Tue, 21 Jun 2022 10:40:16 +0800 Subject: [PATCH] Update DownloaderOperation.cs Add OnStartDownloadFileCallback filed. Change OnDownloadFileFailedCallback to OnDownloadErrorCallback. --- .../Operations/DownloaderOperation.cs | 35 ++++++++++++------- 1 file changed, 22 insertions(+), 13 deletions(-) diff --git a/Assets/YooAsset/Runtime/PatchSystem/Operations/DownloaderOperation.cs b/Assets/YooAsset/Runtime/PatchSystem/Operations/DownloaderOperation.cs index 4e57fed..501ec18 100644 --- a/Assets/YooAsset/Runtime/PatchSystem/Operations/DownloaderOperation.cs +++ b/Assets/YooAsset/Runtime/PatchSystem/Operations/DownloaderOperation.cs @@ -17,14 +17,15 @@ namespace YooAsset public delegate void OnDownloadOver(bool isSucceed); public delegate void OnDownloadProgress(int totalDownloadCount, int currentDownloadCount, long totalDownloadBytes, long currentDownloadBytes); - public delegate void OnDownloadFileFailed(string fileName); - + public delegate void OnDownloadError(string fileName, string error); + public delegate void OnStartDownloadFile(string fileName, long sizeBytes); + private readonly int _downloadingMaxNumber; private readonly int _failedTryAgain; private readonly List _downloadList; - private readonly List _loadFailedList = new List(); - private readonly List _downloaders = new List(); + private readonly List _downloaders = new List(MAX_LOADER_COUNT); private readonly List _removeList = new List(MAX_LOADER_COUNT); + private readonly List _failedList = new List(MAX_LOADER_COUNT); // 数据相关 private ESteps _steps = ESteps.None; @@ -59,15 +60,20 @@ namespace YooAsset public OnDownloadOver OnDownloadOverCallback { set; get; } /// - /// 当下载进度变化 + /// 当下载进度发生变化 /// public OnDownloadProgress OnDownloadProgressCallback { set; get; } /// - /// 当文件下载失败 + /// 当某个文件下载失败 /// - public OnDownloadFileFailed OnDownloadFileFailedCallback { set; get; } + public OnDownloadError OnDownloadErrorCallback { set; get; } + /// + /// 当开始下载某个文件 + /// + public OnStartDownloadFile OnStartDownloadFileCallback { set; get; } + internal DownloaderOperation(List downloadList, int downloadingMaxNumber, int failedTryAgain) { @@ -125,7 +131,7 @@ namespace YooAsset if (downloader.HasError()) { _removeList.Add(downloader); - _loadFailedList.Add(bundleInfo); + _failedList.Add(downloader); continue; } @@ -152,7 +158,7 @@ namespace YooAsset // 动态创建新的下载器到最大数量限制 // 注意:如果期间有下载失败的文件,暂停动态创建下载器 - if (_downloadList.Count > 0 && _loadFailedList.Count == 0) + if (_downloadList.Count > 0 && _failedList.Count == 0) { if (_isPause) return; @@ -160,22 +166,25 @@ namespace YooAsset if (_downloaders.Count < _downloadingMaxNumber) { int index = _downloadList.Count - 1; - var operation = DownloadSystem.BeginDownload(_downloadList[index], _failedTryAgain); + var bundleInfo = _downloadList[index]; + var operation = DownloadSystem.BeginDownload(bundleInfo, _failedTryAgain); _downloaders.Add(operation); _downloadList.RemoveAt(index); + OnStartDownloadFileCallback?.Invoke(bundleInfo.BundleName, bundleInfo.SizeBytes); } } // 下载结算 if (_downloaders.Count == 0) { - if (_loadFailedList.Count > 0) + if (_failedList.Count > 0) { - string fileName = _loadFailedList[0].BundleName; + var failedDownloader = _failedList[0]; + string fileName = failedDownloader.GetBundleInfo().BundleName; Error = $"Failed to download file : {fileName}"; _steps = ESteps.Done; Status = EOperationStatus.Failed; - OnDownloadFileFailedCallback?.Invoke(fileName); + OnDownloadErrorCallback?.Invoke(fileName, failedDownloader.GetLastError()); OnDownloadOverCallback?.Invoke(false); } else