From 609cd21603a0ec4e594d0faf4afcbc225e3231fe Mon Sep 17 00:00:00 2001 From: hevinci Date: Tue, 18 Oct 2022 14:49:56 +0800 Subject: [PATCH] Update DownloadSystem MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 修复断点续传统计不准确的问题。 --- .../Downloader/DownloadHandlerFileRange.cs | 2 +- .../Downloader/FileDownloader.cs | 8 +- .../Downloader/HttpDownloader.cs | 288 ------------------ .../Downloader/HttpDownloader.cs.meta | 11 - 4 files changed, 7 insertions(+), 302 deletions(-) delete mode 100644 Assets/YooAsset/Runtime/DownloadSystem/Downloader/HttpDownloader.cs delete mode 100644 Assets/YooAsset/Runtime/DownloadSystem/Downloader/HttpDownloader.cs.meta diff --git a/Assets/YooAsset/Runtime/DownloadSystem/Downloader/DownloadHandlerFileRange.cs b/Assets/YooAsset/Runtime/DownloadSystem/Downloader/DownloadHandlerFileRange.cs index d8ee959..f439e47 100644 --- a/Assets/YooAsset/Runtime/DownloadSystem/Downloader/DownloadHandlerFileRange.cs +++ b/Assets/YooAsset/Runtime/DownloadSystem/Downloader/DownloadHandlerFileRange.cs @@ -8,7 +8,7 @@ namespace YooAsset /// /// 支持Unity2018版本的断点续传下载器 /// - public class DownloadHandlerFileRange : DownloadHandlerScript + internal class DownloadHandlerFileRange : DownloadHandlerScript { private string _fileSavePath; private long _fileTotalSize; diff --git a/Assets/YooAsset/Runtime/DownloadSystem/Downloader/FileDownloader.cs b/Assets/YooAsset/Runtime/DownloadSystem/Downloader/FileDownloader.cs index b8253cd..b32a6eb 100644 --- a/Assets/YooAsset/Runtime/DownloadSystem/Downloader/FileDownloader.cs +++ b/Assets/YooAsset/Runtime/DownloadSystem/Downloader/FileDownloader.cs @@ -15,6 +15,7 @@ namespace YooAsset // 重置变量 private bool _isAbort = false; + private ulong _fileOriginLength; private ulong _latestDownloadBytes; private float _latestDownloadRealtime; private float _tryAgainTimer; @@ -60,6 +61,7 @@ namespace YooAsset _downloadProgress = 0f; _downloadedBytes = 0; _isAbort = false; + _fileOriginLength = 0; _latestDownloadBytes = 0; _latestDownloadRealtime = Time.realtimeSinceStartup; _tryAgainTimer = 0f; @@ -72,6 +74,8 @@ namespace YooAsset { FileInfo fileInfo = new FileInfo(fileSavePath); fileLength = fileInfo.Length; + _fileOriginLength = (ulong)fileLength; + _downloadedBytes = _fileOriginLength; } _requestURL = GetRequestURL(); @@ -109,7 +113,7 @@ namespace YooAsset if (_steps == ESteps.CheckDownload) { _downloadProgress = _webRequest.downloadProgress; - _downloadedBytes = _webRequest.downloadedBytes; + _downloadedBytes = _fileOriginLength + _webRequest.downloadedBytes; if (_webRequest.isDone == false) { CheckTimeout(); @@ -226,7 +230,7 @@ namespace YooAsset } private void DisposeWebRequest() { - if(_downloadHandle != null) + if (_downloadHandle != null) { _downloadHandle.Cleanup(); _downloadHandle = null; diff --git a/Assets/YooAsset/Runtime/DownloadSystem/Downloader/HttpDownloader.cs b/Assets/YooAsset/Runtime/DownloadSystem/Downloader/HttpDownloader.cs deleted file mode 100644 index fa33471..0000000 --- a/Assets/YooAsset/Runtime/DownloadSystem/Downloader/HttpDownloader.cs +++ /dev/null @@ -1,288 +0,0 @@ -using System; -using System.Collections; -using System.Collections.Generic; -using System.IO; -using System.Net; -using System.Net.Security; -using System.Threading; - -namespace YooAsset -{ - /* - internal sealed class HttpDownloader : DownloaderBase - { - /// - /// 多线程下载器 - /// - private class ThreadDownloader - { - private const int BufferSize = 1042 * 4; - - private Thread _thread; - private bool _running = true; - private string _url; - private string _savePath; - private long _fileSize; - private int _timeout; - - /// - /// 下载是否结束 - /// - public bool IsDone = false; - - /// - /// 错误日志 - /// - public string Error = string.Empty; - - /// - /// 下载进度 - /// - public float DownloadProgress = 0f; - - /// - /// 已经下载的总字节数 - /// - public ulong DownloadedBytes = 0; - - - /// - /// 开始下载 - /// - public void Run(string url, string savePath, long fileSize, int timeout) - { - _url = url; - _savePath = savePath; - _fileSize = fileSize; - _timeout = timeout; - - _thread = new Thread(ThreadRun); - _thread.IsBackground = true; - _thread.Start(); - } - - /// - /// 中断下载线程 - /// - public void Abort() - { - _running = false; - } - - /// - /// 下载结果 - /// - public bool HasError() - { - if (string.IsNullOrEmpty(Error)) - return false; - else - return true; - } - - private void ThreadRun() - { - long fileTotalSize = _fileSize; - - FileStream fileStream = null; - HttpWebResponse webResponse = null; - Stream responseStream = null; - - try - { - // 创建文件流 - fileStream = new FileStream(_savePath, FileMode.OpenOrCreate, FileAccess.Write); - long fileLength = fileStream.Length; - - // 创建HTTP下载请求 - HttpWebRequest webRequest = WebRequest.Create(_url) as HttpWebRequest; - webRequest.Timeout = _timeout * 1000; - webRequest.ProtocolVersion = HttpVersion.Version10; - if (fileLength > 0) - { - // 注意:设置远端请求文件的起始位置 - webRequest.AddRange(fileLength); - // 注意:设置本地文件流的起始位置 - fileStream.Seek(fileLength, SeekOrigin.Begin); - } - - // 读取下载数据并保存到文件 - webResponse = webRequest.GetResponse() as HttpWebResponse; - responseStream = webResponse.GetResponseStream(); - byte[] buffer = new byte[BufferSize]; - while (_running) - { - int length = responseStream.Read(buffer, 0, buffer.Length); - if (length <= 0) - break; - - fileStream.Write(buffer, 0, length); - - // 计算下载进度 - // 注意:原子操作保证数据安全 - fileLength += length; - float progress = fileLength / fileTotalSize; - DownloadProgress = progress; - DownloadedBytes = (ulong)fileLength; - } - } - catch (Exception e) - { - Error = e.Message; - } - finally - { - if (responseStream != null) - { - responseStream.Close(); - responseStream.Dispose(); - } - - if (webResponse != null) - { - webResponse.Close(); - webResponse.Dispose(); - } - - if (fileStream != null) - { - fileStream.Flush(); - fileStream.Close(); - } - - IsDone = true; - } - } - } - - - private ThreadDownloader _threadDownloader; - private float _tryAgainTimer; - - public HttpDownloader(BundleInfo bundleInfo) : base(bundleInfo) - { - } - public override void Update() - { - if (_steps == ESteps.None) - return; - if (IsDone()) - return; - - // 检测本地文件 - if (_steps == ESteps.CheckLocalFile) - { - var verifyResult = CacheSystem.VerifyAndCacheBundle(_bundleInfo.Bundle, EVerifyLevel.High); - if (verifyResult == EVerifyResult.Succeed) - { - _steps = ESteps.Succeed; - } - else - { - if (verifyResult == EVerifyResult.FileOverflow) - { - string cacheFilePath = _bundleInfo.Bundle.CachedFilePath; - if (File.Exists(cacheFilePath)) - File.Delete(cacheFilePath); - } - _steps = ESteps.CreateDownload; - } - } - - // 创建下载器 - if (_steps == ESteps.CreateDownload) - { - // 重置变量 - _downloadProgress = 0f; - _downloadedBytes = 0; - _tryAgainTimer = 0f; - - _requestURL = GetRequestURL(); - _threadDownloader = new ThreadDownloader(); - _threadDownloader.Run(_requestURL, _bundleInfo.Bundle.CachedFilePath, _bundleInfo.Bundle.FileSize, _timeout); - _steps = ESteps.CheckDownload; - } - - // 检测下载结果 - if (_steps == ESteps.CheckDownload) - { - _downloadProgress = _threadDownloader.DownloadProgress; - _downloadedBytes = _threadDownloader.DownloadedBytes; - if (_threadDownloader.IsDone == false) - return; - - bool hasError = false; - - // 检查下载错误 - if (_threadDownloader.HasError()) - { - hasError = true; - _lastError = _threadDownloader.Error; - } - - // 检查文件完整性 - if (hasError == false) - { - var verifyResult = CacheSystem.VerifyAndCacheBundle(_bundleInfo.Bundle, EVerifyLevel.High); - if (verifyResult != EVerifyResult.Succeed) - { - hasError = true; - _lastError = $"Verify bundle content failed : {_bundleInfo.Bundle.FileName}"; - - // 验证失败后删除文件 - string cacheFilePath = _bundleInfo.Bundle.CachedFilePath; - if (File.Exists(cacheFilePath)) - File.Delete(cacheFilePath); - } - } - - // 如果下载失败 - if (hasError) - { - // 失败后重新尝试 - if (_failedTryAgain > 0) - { - ReportWarning(); - _steps = ESteps.TryAgain; - } - else - { - ReportError(); - _steps = ESteps.Failed; - } - } - else - { - _lastError = string.Empty; - _steps = ESteps.Succeed; - } - } - - // 重新尝试下载 - if (_steps == ESteps.TryAgain) - { - _tryAgainTimer += UnityEngine.Time.unscaledDeltaTime; - if (_tryAgainTimer > 1f) - { - _failedTryAgain--; - _steps = ESteps.CreateDownload; - YooLogger.Warning($"Try again download : {_requestURL}"); - } - } - } - public override void Abort() - { - if (IsDone() == false) - { - _steps = ESteps.Failed; - _lastError = "user abort"; - if (_threadDownloader != null) - { - _threadDownloader.Abort(); - _threadDownloader = null; - } - } - } - } - */ -} \ No newline at end of file diff --git a/Assets/YooAsset/Runtime/DownloadSystem/Downloader/HttpDownloader.cs.meta b/Assets/YooAsset/Runtime/DownloadSystem/Downloader/HttpDownloader.cs.meta deleted file mode 100644 index b2fdeed..0000000 --- a/Assets/YooAsset/Runtime/DownloadSystem/Downloader/HttpDownloader.cs.meta +++ /dev/null @@ -1,11 +0,0 @@ -fileFormatVersion: 2 -guid: 057a358d2d0c92b47add59dac8bef783 -MonoImporter: - externalObjects: {} - serializedVersion: 2 - defaultReferences: [] - executionOrder: 0 - icon: {instanceID: 0} - userData: - assetBundleName: - assetBundleVariant: