mirror of https://github.com/tuyoogame/YooAsset
parent
16f8592497
commit
b5c82438a0
|
@ -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;
|
||||||
|
|
|
@ -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,14 +56,38 @@ namespace YooAsset
|
||||||
_latestDownloadRealtime = Time.realtimeSinceStartup;
|
_latestDownloadRealtime = Time.realtimeSinceStartup;
|
||||||
_tryAgainTimer = 0f;
|
_tryAgainTimer = 0f;
|
||||||
|
|
||||||
_requestURL = GetRequestURL();
|
// 是否开启断点续传下载
|
||||||
_webRequest = new UnityWebRequest(_requestURL, UnityWebRequest.kHttpVerbGET);
|
if (_breakDownload)
|
||||||
DownloadHandlerFile handler = new DownloadHandlerFile(_bundleInfo.Bundle.CachedFilePath);
|
{
|
||||||
handler.removeFileOnAbort = true;
|
long fileLength = -1;
|
||||||
_webRequest.downloadHandler = handler;
|
if (File.Exists(fileSavePath))
|
||||||
_webRequest.disposeDownloadHandlerOnDispose = true;
|
{
|
||||||
_operationHandle = _webRequest.SendWebRequest();
|
FileInfo fileInfo = new FileInfo(fileSavePath);
|
||||||
_steps = ESteps.CheckDownload;
|
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;
|
_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)
|
||||||
{
|
{
|
||||||
// 下载失败后删除文件
|
// 注意:非断点续传下载失败后删除文件
|
||||||
string cacheFilePath = _bundleInfo.Bundle.CachedFilePath;
|
if (_breakDownload == false)
|
||||||
if (File.Exists(cacheFilePath))
|
{
|
||||||
File.Delete(cacheFilePath);
|
string cacheFilePath = _bundleInfo.Bundle.CachedFilePath;
|
||||||
|
if (File.Exists(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;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -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))
|
||||||
|
|
Loading…
Reference in New Issue