From b5c82438a0378eec47cff7db7b608952765fc295 Mon Sep 17 00:00:00 2001
From: hevinci <hevinci@hotmail.com>
Date: Tue, 23 Aug 2022 15:27:20 +0800
Subject: [PATCH] Update download system
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

使用新的断点续传下载器
---
 .../Runtime/DownloadSystem/DownloadSystem.cs  |  7 +-
 .../Downloader/FileDownloader.cs              | 66 ++++++++++++++-----
 .../Downloader/HttpDownloader.cs              |  4 +-
 3 files changed, 53 insertions(+), 24 deletions(-)

diff --git a/Assets/YooAsset/Runtime/DownloadSystem/DownloadSystem.cs b/Assets/YooAsset/Runtime/DownloadSystem/DownloadSystem.cs
index dcef8cf..d767451 100644
--- a/Assets/YooAsset/Runtime/DownloadSystem/DownloadSystem.cs
+++ b/Assets/YooAsset/Runtime/DownloadSystem/DownloadSystem.cs
@@ -86,11 +86,8 @@ namespace YooAsset
 			{
 				YooLogger.Log($"Beginning to download file : {bundleInfo.Bundle.FileName} URL : {bundleInfo.RemoteMainURL}");
 				FileUtility.CreateFileDirectory(bundleInfo.Bundle.CachedFilePath);
-				DownloaderBase newDownloader;
-				if (bundleInfo.Bundle.FileSize >= _breakpointResumeFileSize)
-					newDownloader = new HttpDownloader(bundleInfo);
-				else
-					newDownloader = new FileDownloader(bundleInfo);
+				bool breakDownload = bundleInfo.Bundle.FileSize >= _breakpointResumeFileSize;
+				DownloaderBase newDownloader = new FileDownloader(bundleInfo, breakDownload);
 				newDownloader.SendRequest(failedTryAgain, timeout);
 				_downloaderDic.Add(bundleInfo.Bundle.CachedFilePath, newDownloader);
 				return newDownloader;
diff --git a/Assets/YooAsset/Runtime/DownloadSystem/Downloader/FileDownloader.cs b/Assets/YooAsset/Runtime/DownloadSystem/Downloader/FileDownloader.cs
index b973ce3..142ccd5 100644
--- a/Assets/YooAsset/Runtime/DownloadSystem/Downloader/FileDownloader.cs
+++ b/Assets/YooAsset/Runtime/DownloadSystem/Downloader/FileDownloader.cs
@@ -10,7 +10,7 @@ namespace YooAsset
 	internal sealed class FileDownloader : DownloaderBase
 	{
 		private UnityWebRequest _webRequest;
-		private UnityWebRequestAsyncOperation _operationHandle;
+		private bool _breakDownload;
 
 		// 重置变量
 		private bool _isAbort = false;
@@ -19,8 +19,9 @@ namespace YooAsset
 		private float _tryAgainTimer;
 
 
-		public FileDownloader(BundleInfo bundleInfo) : base(bundleInfo)
+		public FileDownloader(BundleInfo bundleInfo, bool breakDownload) : base(bundleInfo)
 		{
+			_breakDownload = breakDownload;
 		}
 		public override void Update()
 		{
@@ -45,6 +46,8 @@ namespace YooAsset
 			// 创建下载器
 			if (_steps == ESteps.CreateDownload)
 			{
+				string fileSavePath = _bundleInfo.Bundle.CachedFilePath;
+
 				// 重置变量
 				_downloadProgress = 0f;
 				_downloadedBytes = 0;
@@ -53,14 +56,38 @@ namespace YooAsset
 				_latestDownloadRealtime = Time.realtimeSinceStartup;
 				_tryAgainTimer = 0f;
 
-				_requestURL = GetRequestURL();
-				_webRequest = new UnityWebRequest(_requestURL, UnityWebRequest.kHttpVerbGET);
-				DownloadHandlerFile handler = new DownloadHandlerFile(_bundleInfo.Bundle.CachedFilePath);
-				handler.removeFileOnAbort = true;
-				_webRequest.downloadHandler = handler;
-				_webRequest.disposeDownloadHandlerOnDispose = true;
-				_operationHandle = _webRequest.SendWebRequest();
-				_steps = ESteps.CheckDownload;
+				// 是否开启断点续传下载	
+				if (_breakDownload)
+				{
+					long fileLength = -1;
+					if (File.Exists(fileSavePath))
+					{
+						FileInfo fileInfo = new FileInfo(fileSavePath);
+						fileLength = fileInfo.Length;
+					}
+
+					_requestURL = GetRequestURL();
+					_webRequest = UnityWebRequest.Get(_requestURL);
+					DownloadHandlerFile handler = new DownloadHandlerFile(fileSavePath, true);
+					handler.removeFileOnAbort = false;
+					_webRequest.downloadHandler = handler;
+					_webRequest.disposeDownloadHandlerOnDispose = true;
+					if (fileLength > 0)
+						_webRequest.SetRequestHeader("Range", $"bytes={fileLength}-");
+					_webRequest.SendWebRequest();
+					_steps = ESteps.CheckDownload;
+				}
+				else
+				{
+					_requestURL = GetRequestURL();
+					_webRequest = new UnityWebRequest(_requestURL, UnityWebRequest.kHttpVerbGET);
+					DownloadHandlerFile handler = new DownloadHandlerFile(fileSavePath);
+					handler.removeFileOnAbort = true;
+					_webRequest.downloadHandler = handler;
+					_webRequest.disposeDownloadHandlerOnDispose = true;
+					_webRequest.SendWebRequest();
+					_steps = ESteps.CheckDownload;
+				}
 			}
 
 			// 检测下载结果
@@ -68,7 +95,7 @@ namespace YooAsset
 			{
 				_downloadProgress = _webRequest.downloadProgress;
 				_downloadedBytes = _webRequest.downloadedBytes;
-				if (_operationHandle.isDone == false)
+				if (_webRequest.isDone == false)
 				{
 					CheckTimeout();
 					return;
@@ -98,16 +125,24 @@ namespace YooAsset
 					{
 						hasError = true;
 						_lastError = $"Verify bundle content failed : {_bundleInfo.Bundle.FileName}";
+
+						// 验证失败后删除文件
+						string cacheFilePath = _bundleInfo.Bundle.CachedFilePath;
+						if (File.Exists(cacheFilePath))
+							File.Delete(cacheFilePath);
 					}
 				}
 
 				// 如果下载失败
 				if (hasError)
 				{
-					// 下载失败后删除文件
-					string cacheFilePath = _bundleInfo.Bundle.CachedFilePath;
-					if (File.Exists(cacheFilePath))
-						File.Delete(cacheFilePath);
+					// 注意:非断点续传下载失败后删除文件
+					if (_breakDownload == false)
+					{
+						string cacheFilePath = _bundleInfo.Bundle.CachedFilePath;
+						if (File.Exists(cacheFilePath))
+							File.Delete(cacheFilePath);
+					}
 
 					// 失败后重新尝试
 					if (_failedTryAgain > 0)
@@ -179,7 +214,6 @@ namespace YooAsset
 			{
 				_webRequest.Dispose();
 				_webRequest = null;
-				_operationHandle = null;
 			}
 		}
 	}
diff --git a/Assets/YooAsset/Runtime/DownloadSystem/Downloader/HttpDownloader.cs b/Assets/YooAsset/Runtime/DownloadSystem/Downloader/HttpDownloader.cs
index 5019b06..f4ee145 100644
--- a/Assets/YooAsset/Runtime/DownloadSystem/Downloader/HttpDownloader.cs
+++ b/Assets/YooAsset/Runtime/DownloadSystem/Downloader/HttpDownloader.cs
@@ -219,9 +219,7 @@ namespace YooAsset
 					{
 						hasError = true;
 						_lastError = $"Verify bundle content failed : {_bundleInfo.Bundle.FileName}";
-					}
-					else
-					{
+
 						// 验证失败后删除文件
 						string cacheFilePath = _bundleInfo.Bundle.CachedFilePath;
 						if (File.Exists(cacheFilePath))