Update CacheSystem
优化缓存系统代码结构
This commit is contained in:
@@ -70,13 +70,13 @@ namespace YooAsset
|
||||
public static DownloaderBase BeginDownload(BundleInfo bundleInfo, int failedTryAgain, int timeout = 60)
|
||||
{
|
||||
// 查询存在的下载器
|
||||
if (_downloaderDic.TryGetValue(bundleInfo.FileHash, out var downloader))
|
||||
if (_downloaderDic.TryGetValue(bundleInfo.Bundle.CachedFilePath, out var downloader))
|
||||
{
|
||||
return downloader;
|
||||
}
|
||||
|
||||
// 如果资源已经缓存
|
||||
if (CacheSystem.ContainsVerifyFile(bundleInfo.LoadBundle))
|
||||
if (CacheSystem.IsCached(bundleInfo.Bundle))
|
||||
{
|
||||
var tempDownloader = new TempDownloader(bundleInfo);
|
||||
return tempDownloader;
|
||||
@@ -84,15 +84,15 @@ namespace YooAsset
|
||||
|
||||
// 创建新的下载器
|
||||
{
|
||||
YooLogger.Log($"Beginning to download file : {bundleInfo.FileName} URL : {bundleInfo.RemoteMainURL}");
|
||||
FileUtility.CreateFileDirectory(bundleInfo.GetCacheLoadPath());
|
||||
YooLogger.Log($"Beginning to download file : {bundleInfo.Bundle.FileName} URL : {bundleInfo.RemoteMainURL}");
|
||||
FileUtility.CreateFileDirectory(bundleInfo.Bundle.CachedFilePath);
|
||||
DownloaderBase newDownloader;
|
||||
if (bundleInfo.FileSize >= _breakpointResumeFileSize)
|
||||
if (bundleInfo.Bundle.FileSize >= _breakpointResumeFileSize)
|
||||
newDownloader = new HttpDownloader(bundleInfo);
|
||||
else
|
||||
newDownloader = new FileDownloader(bundleInfo);
|
||||
newDownloader.SendRequest(failedTryAgain, timeout);
|
||||
_downloaderDic.Add(bundleInfo.FileHash, newDownloader);
|
||||
_downloaderDic.Add(bundleInfo.Bundle.CachedFilePath, newDownloader);
|
||||
return newDownloader;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,6 +6,7 @@ namespace YooAsset
|
||||
protected enum ESteps
|
||||
{
|
||||
None,
|
||||
CheckLocalFile,
|
||||
CreateDownload,
|
||||
CheckDownload,
|
||||
TryAgain,
|
||||
@@ -54,7 +55,7 @@ namespace YooAsset
|
||||
{
|
||||
_failedTryAgain = failedTryAgain;
|
||||
_timeout = timeout;
|
||||
_steps = ESteps.CreateDownload;
|
||||
_steps = ESteps.CheckLocalFile;
|
||||
}
|
||||
}
|
||||
public abstract void Update();
|
||||
|
||||
@@ -29,6 +29,19 @@ namespace YooAsset
|
||||
if (IsDone())
|
||||
return;
|
||||
|
||||
// 检测本地文件
|
||||
if (_steps == ESteps.CheckLocalFile)
|
||||
{
|
||||
if (CacheSystem.VerifyAndCacheBundle(_bundleInfo.Bundle, EVerifyLevel.High))
|
||||
{
|
||||
_steps = ESteps.Succeed;
|
||||
}
|
||||
else
|
||||
{
|
||||
_steps = ESteps.CreateDownload;
|
||||
}
|
||||
}
|
||||
|
||||
// 创建下载器
|
||||
if (_steps == ESteps.CreateDownload)
|
||||
{
|
||||
@@ -42,7 +55,7 @@ namespace YooAsset
|
||||
|
||||
_requestURL = GetRequestURL();
|
||||
_webRequest = new UnityWebRequest(_requestURL, UnityWebRequest.kHttpVerbGET);
|
||||
DownloadHandlerFile handler = new DownloadHandlerFile(_bundleInfo.GetCacheLoadPath());
|
||||
DownloadHandlerFile handler = new DownloadHandlerFile(_bundleInfo.Bundle.CachedFilePath);
|
||||
handler.removeFileOnAbort = true;
|
||||
_webRequest.downloadHandler = handler;
|
||||
_webRequest.disposeDownloadHandlerOnDispose = true;
|
||||
@@ -61,8 +74,9 @@ namespace YooAsset
|
||||
return;
|
||||
}
|
||||
|
||||
// 检查网络错误
|
||||
bool hasError = false;
|
||||
|
||||
// 检查网络错误
|
||||
#if UNITY_2020_3_OR_NEWER
|
||||
if (_webRequest.result != UnityWebRequest.Result.Success)
|
||||
{
|
||||
@@ -80,22 +94,18 @@ namespace YooAsset
|
||||
// 检查文件完整性
|
||||
if (hasError == false)
|
||||
{
|
||||
// 注意:如果文件验证失败需要删除文件
|
||||
if (CacheSystem.CheckContentIntegrity(_bundleInfo.GetCacheLoadPath(), _bundleInfo.FileSize, _bundleInfo.FileCRC) == false)
|
||||
if (CacheSystem.VerifyAndCacheBundle(_bundleInfo.Bundle, EVerifyLevel.High) == false)
|
||||
{
|
||||
hasError = true;
|
||||
_lastError = $"Verification failed";
|
||||
_lastError = $"Verify bundle content failed : {_bundleInfo.Bundle.FileName}";
|
||||
}
|
||||
}
|
||||
|
||||
if (hasError == false)
|
||||
// 如果下载失败
|
||||
if (hasError)
|
||||
{
|
||||
_steps = ESteps.Succeed;
|
||||
CacheSystem.CacheVerifyFile(_bundleInfo.LoadBundle);
|
||||
}
|
||||
else
|
||||
{
|
||||
string cacheFilePath = _bundleInfo.GetCacheLoadPath();
|
||||
// 下载失败后删除文件
|
||||
string cacheFilePath = _bundleInfo.Bundle.CachedFilePath;
|
||||
if (File.Exists(cacheFilePath))
|
||||
File.Delete(cacheFilePath);
|
||||
|
||||
@@ -111,6 +121,11 @@ namespace YooAsset
|
||||
_steps = ESteps.Failed;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
_lastError = string.Empty;
|
||||
_steps = ESteps.Succeed;
|
||||
}
|
||||
|
||||
// 释放下载器
|
||||
DisposeWebRequest();
|
||||
|
||||
@@ -21,8 +21,6 @@ namespace YooAsset
|
||||
private bool _running = true;
|
||||
private string _url;
|
||||
private string _savePath;
|
||||
private string _fileName;
|
||||
private string _fileCRC;
|
||||
private long _fileSize;
|
||||
private int _timeout;
|
||||
|
||||
@@ -50,12 +48,10 @@ namespace YooAsset
|
||||
/// <summary>
|
||||
/// 开始下载
|
||||
/// </summary>
|
||||
public void Run(string url, string savePath, string fileName, string fileCRC, long fileSize, int timeout)
|
||||
public void Run(string url, string savePath, long fileSize, int timeout)
|
||||
{
|
||||
_url = url;
|
||||
_savePath = savePath;
|
||||
_fileName = fileName;
|
||||
_fileCRC = fileCRC;
|
||||
_fileSize = fileSize;
|
||||
_timeout = timeout;
|
||||
|
||||
@@ -153,22 +149,6 @@ namespace YooAsset
|
||||
fileStream.Close();
|
||||
}
|
||||
|
||||
// 验证下载文件完整性
|
||||
if (DownloadedBytes == (ulong)_fileSize)
|
||||
{
|
||||
bool verfiyResult = CacheSystem.CheckContentIntegrity(_savePath, _fileSize, _fileCRC);
|
||||
if (verfiyResult == false)
|
||||
{
|
||||
Error = $"Verify download content failed : {_fileName}";
|
||||
if (File.Exists(_savePath))
|
||||
File.Delete(_savePath);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
Error = $"Download content is incomplete : {_fileName}";
|
||||
}
|
||||
|
||||
IsDone = true;
|
||||
}
|
||||
}
|
||||
@@ -188,6 +168,20 @@ namespace YooAsset
|
||||
if (IsDone())
|
||||
return;
|
||||
|
||||
// 检测本地文件
|
||||
if (_steps == ESteps.CheckLocalFile)
|
||||
{
|
||||
if (CacheSystem.VerifyAndCacheBundle(_bundleInfo.Bundle, EVerifyLevel.High))
|
||||
{
|
||||
_steps = ESteps.Succeed;
|
||||
}
|
||||
else
|
||||
{
|
||||
_steps = ESteps.CreateDownload;
|
||||
}
|
||||
}
|
||||
|
||||
// 创建下载器
|
||||
if (_steps == ESteps.CreateDownload)
|
||||
{
|
||||
// 重置变量
|
||||
@@ -197,10 +191,11 @@ namespace YooAsset
|
||||
|
||||
_requestURL = GetRequestURL();
|
||||
_threadDownloader = new ThreadDownloader();
|
||||
_threadDownloader.Run(_requestURL, _bundleInfo.GetCacheLoadPath(), _bundleInfo.FileName, _bundleInfo.FileCRC, _bundleInfo.FileSize, _timeout);
|
||||
_threadDownloader.Run(_requestURL, _bundleInfo.Bundle.CachedFilePath, _bundleInfo.Bundle.FileSize, _timeout);
|
||||
_steps = ESteps.CheckDownload;
|
||||
}
|
||||
|
||||
// 检测下载结果
|
||||
if (_steps == ESteps.CheckDownload)
|
||||
{
|
||||
_downloadProgress = _threadDownloader.DownloadProgress;
|
||||
@@ -208,10 +203,35 @@ namespace YooAsset
|
||||
if (_threadDownloader.IsDone == false)
|
||||
return;
|
||||
|
||||
bool hasError = false;
|
||||
|
||||
// 检查下载错误
|
||||
if (_threadDownloader.HasError())
|
||||
{
|
||||
hasError = true;
|
||||
_lastError = _threadDownloader.Error;
|
||||
}
|
||||
|
||||
// 检查文件完整性
|
||||
if (hasError == false)
|
||||
{
|
||||
if (CacheSystem.VerifyAndCacheBundle(_bundleInfo.Bundle, EVerifyLevel.High) == false)
|
||||
{
|
||||
hasError = true;
|
||||
_lastError = $"Verify bundle content failed : {_bundleInfo.Bundle.FileName}";
|
||||
}
|
||||
else
|
||||
{
|
||||
// 验证失败后删除文件
|
||||
string cacheFilePath = _bundleInfo.Bundle.CachedFilePath;
|
||||
if (File.Exists(cacheFilePath))
|
||||
File.Delete(cacheFilePath);
|
||||
}
|
||||
}
|
||||
|
||||
// 如果下载失败
|
||||
if (hasError)
|
||||
{
|
||||
// 失败后重新尝试
|
||||
if (_failedTryAgain > 0)
|
||||
{
|
||||
@@ -226,7 +246,7 @@ namespace YooAsset
|
||||
}
|
||||
else
|
||||
{
|
||||
CacheSystem.CacheVerifyFile(_bundleInfo.LoadBundle);
|
||||
_lastError = string.Empty;
|
||||
_steps = ESteps.Succeed;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,7 +6,7 @@ namespace YooAsset
|
||||
public TempDownloader(BundleInfo bundleInfo) : base(bundleInfo)
|
||||
{
|
||||
_downloadProgress = 1f;
|
||||
_downloadedBytes = (ulong)bundleInfo.FileSize;
|
||||
_downloadedBytes = (ulong)bundleInfo.Bundle.FileSize;
|
||||
_steps = ESteps.Succeed;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user