diff --git a/Assets/YooAsset/Runtime/AssetSystem/Loader/AssetBundleLoader.cs b/Assets/YooAsset/Runtime/AssetSystem/Loader/AssetBundleLoader.cs index 25572a3..bead011 100644 --- a/Assets/YooAsset/Runtime/AssetSystem/Loader/AssetBundleLoader.cs +++ b/Assets/YooAsset/Runtime/AssetSystem/Loader/AssetBundleLoader.cs @@ -41,7 +41,7 @@ namespace YooAsset private readonly List _providers = new List(100); private bool _isWaitForAsyncComplete = false; private bool _isShowWaitForAsyncError = false; - private FileDownloader _fileDownloader; + private DownloaderBase _downloader; private AssetBundleCreateRequest _cacheRequest; internal AssetBundle CacheBundle { private set; get; } @@ -106,19 +106,19 @@ namespace YooAsset if (Status == EStatus.Download) { int failedTryAgain = int.MaxValue; - _fileDownloader = DownloadSystem.BeginDownload(BundleFileInfo, failedTryAgain); + _downloader = DownloadSystem.BeginDownload(BundleFileInfo, failedTryAgain); Status = EStatus.CheckDownload; } // 2. 检测服务器下载结果 if (Status == EStatus.CheckDownload) { - if (_fileDownloader.IsDone() == false) + if (_downloader.IsDone() == false) return; - if (_fileDownloader.HasError()) + if (_downloader.HasError()) { - _fileDownloader.ReportError(); + _downloader.ReportError(); Status = EStatus.Fail; } else diff --git a/Assets/YooAsset/Runtime/AssetSystem/Operations/RawFileOperation.cs b/Assets/YooAsset/Runtime/AssetSystem/Operations/RawFileOperation.cs index 29b7369..e5f11f0 100644 --- a/Assets/YooAsset/Runtime/AssetSystem/Operations/RawFileOperation.cs +++ b/Assets/YooAsset/Runtime/AssetSystem/Operations/RawFileOperation.cs @@ -19,7 +19,7 @@ namespace YooAsset private readonly BundleInfo _bundleInfo; private readonly string _savePath; private ESteps _steps = ESteps.None; - private FileDownloader _fileDownloader; + private DownloaderBase _downloader; private UnityWebFileRequester _fileRequester; /// @@ -67,21 +67,21 @@ namespace YooAsset if (_steps == ESteps.DownloadFromWeb) { int failedTryAgain = int.MaxValue; - _fileDownloader = DownloadSystem.BeginDownload(_bundleInfo, failedTryAgain); + _downloader = DownloadSystem.BeginDownload(_bundleInfo, failedTryAgain); _steps = ESteps.CheckDownloadFromWeb; } // 3. 检测服务器下载结果 if (_steps == ESteps.CheckDownloadFromWeb) { - if (_fileDownloader.IsDone() == false) + if (_downloader.IsDone() == false) return; - if (_fileDownloader.HasError()) + if (_downloader.HasError()) { _steps = ESteps.Done; Status = EOperationStatus.Failed; - Error = _fileDownloader.GetLastError(); + Error = _downloader.GetLastError(); } else { diff --git a/Assets/YooAsset/Runtime/DownloadSystem/DownloadSystem.cs b/Assets/YooAsset/Runtime/DownloadSystem/DownloadSystem.cs index 238254e..b6b8745 100644 --- a/Assets/YooAsset/Runtime/DownloadSystem/DownloadSystem.cs +++ b/Assets/YooAsset/Runtime/DownloadSystem/DownloadSystem.cs @@ -12,7 +12,7 @@ namespace YooAsset /// internal static class DownloadSystem { - private static readonly Dictionary _downloaderDic = new Dictionary(); + private static readonly Dictionary _downloaderDic = new Dictionary(); private static readonly List _removeList = new List(100); private static readonly Dictionary _cachedHashList = new Dictionary(1000); @@ -43,7 +43,7 @@ namespace YooAsset /// 开始下载资源文件 /// 注意:只有第一次请求的参数才是有效的 /// - public static FileDownloader BeginDownload(BundleInfo bundleInfo, int failedTryAgain, int timeout = 60) + public static DownloaderBase BeginDownload(BundleInfo bundleInfo, int failedTryAgain, int timeout = 60) { // 查询存在的下载器 if (_downloaderDic.TryGetValue(bundleInfo.Hash, out var downloader)) @@ -63,7 +63,7 @@ namespace YooAsset { YooLogger.Log($"Beginning to download file : {bundleInfo.BundleName} URL : {bundleInfo.RemoteMainURL}"); FileUtility.CreateFileDirectory(bundleInfo.LocalPath); - var newDownloader = new FileDownloader(bundleInfo); + var newDownloader = new HttpDownloader(bundleInfo); newDownloader.SendRequest(failedTryAgain, timeout); _downloaderDic.Add(bundleInfo.Hash, newDownloader); return newDownloader; diff --git a/Assets/YooAsset/Runtime/DownloadSystem/HttpDownloader.cs b/Assets/YooAsset/Runtime/DownloadSystem/HttpDownloader.cs index 4e71828..652c152 100644 --- a/Assets/YooAsset/Runtime/DownloadSystem/HttpDownloader.cs +++ b/Assets/YooAsset/Runtime/DownloadSystem/HttpDownloader.cs @@ -33,7 +33,7 @@ namespace YooAsset /// /// 下载结果(成功或失败) /// - public bool Result = false; + public bool Result = true; /// /// 错误日志 @@ -93,19 +93,18 @@ namespace YooAsset { // 创建文件流 fileStream = new FileStream(_savePath, FileMode.OpenOrCreate, FileAccess.Write); - long fileLength = fileStream.Length; + long fileLength = fileStream.Length - 1; // 创建HTTP下载请求 HttpWebRequest fileRequest = WebRequest.Create(_url) as HttpWebRequest; - fileRequest.Timeout = _timeout; - fileRequest.ReadWriteTimeout = _timeout; + fileRequest.Timeout = _timeout * 1000; fileRequest.ProtocolVersion = HttpVersion.Version10; if (fileLength > 0) { // 注意:设置远端请求文件的起始位置 fileRequest.AddRange(fileLength); // 注意:设置本地文件流的起始位置 - fileStream.Seek(fileLength, SeekOrigin.Begin); + fileStream.Seek(-1, SeekOrigin.End); } // 读取下载数据并保存到文件 @@ -127,18 +126,6 @@ namespace YooAsset DownloadProgress = progress; DownloadedBytes = (ulong)fileLength; } - - // 验证下载文件完整性 - bool verfiyResult = DownloadSystem.CheckContentIntegrity(_savePath, _fileSize, _fileCRC); - if (verfiyResult) - { - Result = true; - } - else - { - Result = false; - Error = $"Verify file content failed : {_fileHash}"; - } } catch (Exception e) { @@ -160,8 +147,21 @@ namespace YooAsset if (fileStream != null) { + fileStream.Flush(); fileStream.Close(); - fileStream.Dispose(); + } + + // 验证下载文件完整性 + if (Result) + { + bool verfiyResult = DownloadSystem.CheckContentIntegrity(_savePath, _fileSize, _fileCRC); + if (verfiyResult == false) + { + Result = false; + Error = $"Verify file content failed : {_fileHash}"; + if (File.Exists(_savePath)) + File.Delete(_savePath); + } } IsDone = true; @@ -213,9 +213,6 @@ namespace YooAsset _lastError = _threadDownloader.Error; ReportError(); - if (File.Exists(_bundleInfo.LocalPath)) - File.Delete(_bundleInfo.LocalPath); - // 失败后重新尝试 if (_failedTryAgain > 0) _steps = ESteps.TryAgain; diff --git a/Assets/YooAsset/Runtime/PatchSystem/Operations/DownloaderOperation.cs b/Assets/YooAsset/Runtime/PatchSystem/Operations/DownloaderOperation.cs index 7552982..5bb89ad 100644 --- a/Assets/YooAsset/Runtime/PatchSystem/Operations/DownloaderOperation.cs +++ b/Assets/YooAsset/Runtime/PatchSystem/Operations/DownloaderOperation.cs @@ -22,8 +22,8 @@ namespace YooAsset private readonly int _failedTryAgain; private readonly List _downloadList; private readonly List _loadFailedList = new List(); - private readonly List _downloaders = new List(); - private readonly List _removeList = new List(MAX_LOADER_COUNT); + private readonly List _downloaders = new List(); + private readonly List _removeList = new List(MAX_LOADER_COUNT); // 数据相关 private ESteps _steps = ESteps.None;