Update download system

使用新的断点续传下载器
pull/40/head
hevinci 2022-08-23 15:27:20 +08:00
parent 16f8592497
commit b5c82438a0
3 changed files with 53 additions and 24 deletions

View File

@ -86,11 +86,8 @@ namespace YooAsset
{ {
YooLogger.Log($"Beginning to download file : {bundleInfo.Bundle.FileName} URL : {bundleInfo.RemoteMainURL}"); YooLogger.Log($"Beginning to download file : {bundleInfo.Bundle.FileName} URL : {bundleInfo.RemoteMainURL}");
FileUtility.CreateFileDirectory(bundleInfo.Bundle.CachedFilePath); FileUtility.CreateFileDirectory(bundleInfo.Bundle.CachedFilePath);
DownloaderBase newDownloader; bool breakDownload = bundleInfo.Bundle.FileSize >= _breakpointResumeFileSize;
if (bundleInfo.Bundle.FileSize >= _breakpointResumeFileSize) DownloaderBase newDownloader = new FileDownloader(bundleInfo, breakDownload);
newDownloader = new HttpDownloader(bundleInfo);
else
newDownloader = new FileDownloader(bundleInfo);
newDownloader.SendRequest(failedTryAgain, timeout); newDownloader.SendRequest(failedTryAgain, timeout);
_downloaderDic.Add(bundleInfo.Bundle.CachedFilePath, newDownloader); _downloaderDic.Add(bundleInfo.Bundle.CachedFilePath, newDownloader);
return newDownloader; return newDownloader;

View File

@ -10,7 +10,7 @@ namespace YooAsset
internal sealed class FileDownloader : DownloaderBase internal sealed class FileDownloader : DownloaderBase
{ {
private UnityWebRequest _webRequest; private UnityWebRequest _webRequest;
private UnityWebRequestAsyncOperation _operationHandle; private bool _breakDownload;
// 重置变量 // 重置变量
private bool _isAbort = false; private bool _isAbort = false;
@ -19,8 +19,9 @@ namespace YooAsset
private float _tryAgainTimer; private float _tryAgainTimer;
public FileDownloader(BundleInfo bundleInfo) : base(bundleInfo) public FileDownloader(BundleInfo bundleInfo, bool breakDownload) : base(bundleInfo)
{ {
_breakDownload = breakDownload;
} }
public override void Update() public override void Update()
{ {
@ -45,6 +46,8 @@ namespace YooAsset
// 创建下载器 // 创建下载器
if (_steps == ESteps.CreateDownload) if (_steps == ESteps.CreateDownload)
{ {
string fileSavePath = _bundleInfo.Bundle.CachedFilePath;
// 重置变量 // 重置变量
_downloadProgress = 0f; _downloadProgress = 0f;
_downloadedBytes = 0; _downloadedBytes = 0;
@ -53,22 +56,46 @@ namespace YooAsset
_latestDownloadRealtime = Time.realtimeSinceStartup; _latestDownloadRealtime = Time.realtimeSinceStartup;
_tryAgainTimer = 0f; _tryAgainTimer = 0f;
// 是否开启断点续传下载
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(); _requestURL = GetRequestURL();
_webRequest = new UnityWebRequest(_requestURL, UnityWebRequest.kHttpVerbGET); _webRequest = new UnityWebRequest(_requestURL, UnityWebRequest.kHttpVerbGET);
DownloadHandlerFile handler = new DownloadHandlerFile(_bundleInfo.Bundle.CachedFilePath); DownloadHandlerFile handler = new DownloadHandlerFile(fileSavePath);
handler.removeFileOnAbort = true; handler.removeFileOnAbort = true;
_webRequest.downloadHandler = handler; _webRequest.downloadHandler = handler;
_webRequest.disposeDownloadHandlerOnDispose = true; _webRequest.disposeDownloadHandlerOnDispose = true;
_operationHandle = _webRequest.SendWebRequest(); _webRequest.SendWebRequest();
_steps = ESteps.CheckDownload; _steps = ESteps.CheckDownload;
} }
}
// 检测下载结果 // 检测下载结果
if (_steps == ESteps.CheckDownload) if (_steps == ESteps.CheckDownload)
{ {
_downloadProgress = _webRequest.downloadProgress; _downloadProgress = _webRequest.downloadProgress;
_downloadedBytes = _webRequest.downloadedBytes; _downloadedBytes = _webRequest.downloadedBytes;
if (_operationHandle.isDone == false) if (_webRequest.isDone == false)
{ {
CheckTimeout(); CheckTimeout();
return; return;
@ -98,16 +125,24 @@ namespace YooAsset
{ {
hasError = true; hasError = true;
_lastError = $"Verify bundle content failed : {_bundleInfo.Bundle.FileName}"; _lastError = $"Verify bundle content failed : {_bundleInfo.Bundle.FileName}";
// 验证失败后删除文件
string cacheFilePath = _bundleInfo.Bundle.CachedFilePath;
if (File.Exists(cacheFilePath))
File.Delete(cacheFilePath);
} }
} }
// 如果下载失败 // 如果下载失败
if (hasError) if (hasError)
{ {
// 下载失败后删除文件 // 注意:非断点续传下载失败后删除文件
if (_breakDownload == false)
{
string cacheFilePath = _bundleInfo.Bundle.CachedFilePath; string cacheFilePath = _bundleInfo.Bundle.CachedFilePath;
if (File.Exists(cacheFilePath)) if (File.Exists(cacheFilePath))
File.Delete(cacheFilePath); File.Delete(cacheFilePath);
}
// 失败后重新尝试 // 失败后重新尝试
if (_failedTryAgain > 0) if (_failedTryAgain > 0)
@ -179,7 +214,6 @@ namespace YooAsset
{ {
_webRequest.Dispose(); _webRequest.Dispose();
_webRequest = null; _webRequest = null;
_operationHandle = null;
} }
} }
} }

View File

@ -219,9 +219,7 @@ namespace YooAsset
{ {
hasError = true; hasError = true;
_lastError = $"Verify bundle content failed : {_bundleInfo.Bundle.FileName}"; _lastError = $"Verify bundle content failed : {_bundleInfo.Bundle.FileName}";
}
else
{
// 验证失败后删除文件 // 验证失败后删除文件
string cacheFilePath = _bundleInfo.Bundle.CachedFilePath; string cacheFilePath = _bundleInfo.Bundle.CachedFilePath;
if (File.Exists(cacheFilePath)) if (File.Exists(cacheFilePath))