update new cache system

重新设计了资源缓存系统
This commit is contained in:
hevinci
2023-02-07 18:39:08 +08:00
parent 33a1b9d4bf
commit 8ce8e81792
38 changed files with 858 additions and 841 deletions
@@ -77,13 +77,13 @@ namespace YooAsset
public static DownloaderBase BeginDownload(BundleInfo bundleInfo, int failedTryAgain, int timeout = 60)
{
// 查询存在的下载器
if (_downloaderDic.TryGetValue(bundleInfo.Bundle.CachedFilePath, out var downloader))
if (_downloaderDic.TryGetValue(bundleInfo.Bundle.CachedDataFilePath, out var downloader))
{
return downloader;
}
// 如果资源已经缓存
if (CacheSystem.IsCached(bundleInfo.Bundle))
if (CacheSystem.IsCached(bundleInfo.Bundle.PackageName, bundleInfo.Bundle.CacheGUID))
{
var tempDownloader = new TempDownloader(bundleInfo);
return tempDownloader;
@@ -92,11 +92,11 @@ namespace YooAsset
// 创建新的下载器
{
YooLogger.Log($"Beginning to download file : {bundleInfo.Bundle.FileName} URL : {bundleInfo.RemoteMainURL}");
FileUtility.CreateFileDirectory(bundleInfo.Bundle.CachedFilePath);
FileUtility.CreateFileDirectory(bundleInfo.Bundle.CachedDataFilePath);
bool breakDownload = bundleInfo.Bundle.FileSize >= BreakpointResumeFileSize;
DownloaderBase newDownloader = new FileDownloader(bundleInfo, breakDownload);
newDownloader.SendRequest(failedTryAgain, timeout);
_downloaderDic.Add(bundleInfo.Bundle.CachedFilePath, newDownloader);
_downloaderDic.Add(bundleInfo.Bundle.CachedDataFilePath, newDownloader);
return newDownloader;
}
}
@@ -6,13 +6,13 @@ namespace YooAsset
protected enum ESteps
{
None,
CheckLocalFile,
CheckTempFile,
PrepareDownload,
CreateResumeDownloader,
CreateGeneralDownloader,
CheckDownload,
VerifyDownload,
VerifyingFile,
CachingFile,
TryAgain,
Succeed,
Failed,
@@ -60,7 +60,7 @@ namespace YooAsset
{
_failedTryAgain = failedTryAgain;
_timeout = timeout;
_steps = ESteps.CheckLocalFile;
_steps = ESteps.CheckTempFile;
}
}
public abstract void Update();
@@ -25,7 +25,7 @@ namespace YooAsset
public FileDownloader(BundleInfo bundleInfo, bool breakResume) : base(bundleInfo)
{
_breakResume = breakResume;
_tempFilePath = bundleInfo.Bundle.CachedFilePath + ".temp";
_tempFilePath = bundleInfo.Bundle.TempDataFilePath;
}
public override void Update()
{
@@ -34,29 +34,13 @@ namespace YooAsset
if (IsDone())
return;
// 检测本地正式文件
if (_steps == ESteps.CheckLocalFile)
{
var verifyResult = CacheSystem.VerifyAndCacheLocalBundleFile(_bundleInfo.Bundle, CacheSystem.InitVerifyLevel);
if (verifyResult == EVerifyResult.Succeed)
{
_steps = ESteps.Succeed;
}
else
{
if (File.Exists(_bundleInfo.Bundle.CachedFilePath))
File.Delete(_bundleInfo.Bundle.CachedFilePath);
_steps = ESteps.CheckTempFile;
}
}
// 检测本地临时文件
if (_steps == ESteps.CheckTempFile)
{
var verifyResult = CacheSystem.VerifyAndCacheDownloadBundleFile(_tempFilePath, _bundleInfo.Bundle, EVerifyLevel.High);
var verifyResult = CacheSystem.VerifyingTempFile(_bundleInfo.Bundle, EVerifyLevel.High);
if (verifyResult == EVerifyResult.Succeed)
{
_steps = ESteps.Succeed;
_steps = ESteps.CachingFile;
}
else
{
@@ -94,9 +78,7 @@ namespace YooAsset
if (_steps == ESteps.CreateGeneralDownloader)
{
if (File.Exists(_tempFilePath))
{
File.Delete(_tempFilePath);
}
_webRequest = new UnityWebRequest(_requestURL, UnityWebRequest.kHttpVerbGET);
DownloadHandlerFile handler = new DownloadHandlerFile(_tempFilePath);
@@ -206,7 +188,7 @@ namespace YooAsset
}
else
{
_steps = ESteps.VerifyDownload;
_steps = ESteps.VerifyingFile;
}
// 释放下载器
@@ -214,23 +196,55 @@ namespace YooAsset
}
// 验证下载文件
if (_steps == ESteps.VerifyDownload)
if (_steps == ESteps.VerifyingFile)
{
var verifyResult = CacheSystem.VerifyAndCacheDownloadBundleFile(_tempFilePath, _bundleInfo.Bundle, EVerifyLevel.High);
var verifyResult = CacheSystem.VerifyingTempFile(_bundleInfo.Bundle, EVerifyLevel.High);
if (verifyResult == EVerifyResult.Succeed)
{
_steps = ESteps.CachingFile;
}
else
{
_lastError = $"Failed to verifying file : {_bundleInfo.Bundle.FileName}, ErrorCode : {verifyResult}";
// 注意:验证失败后删除文件
if (File.Exists(_tempFilePath))
File.Delete(_tempFilePath);
_steps = ESteps.TryAgain;
}
}
// 缓存下载文件
if (_steps == ESteps.CachingFile)
{
try
{
string destFilePath = _bundleInfo.Bundle.CachedDataFilePath;
if (File.Exists(destFilePath))
File.Delete(destFilePath);
FileInfo fileInfo = new FileInfo(_tempFilePath);
fileInfo.MoveTo(destFilePath);
// 写入信息文件记录验证数据
CacheFileInfo cacheInfo = new CacheFileInfo();
cacheInfo.FileCRC = _bundleInfo.Bundle.FileCRC;
cacheInfo.FileSize = _bundleInfo.Bundle.FileSize;
string jsonContent = UnityEngine.JsonUtility.ToJson(cacheInfo);
FileUtility.CreateFile(_bundleInfo.Bundle.CachedInfoFilePath, jsonContent);
// 记录缓存文件
var wrapper = new PackageCache.RecordWrapper(_bundleInfo.Bundle.CachedInfoFilePath, _bundleInfo.Bundle.CachedDataFilePath, _bundleInfo.Bundle.FileCRC, _bundleInfo.Bundle.FileSize);
CacheSystem.RecordFile(_bundleInfo.Bundle.PackageName, _bundleInfo.Bundle.CacheGUID, wrapper);
_lastError = string.Empty;
_lastCode = 0;
_steps = ESteps.Succeed;
}
else
catch (Exception e)
{
_lastError = $"Verify bundle content failed : {_bundleInfo.Bundle.FileName}";
// 验证失败后删除文件
if (File.Exists(_tempFilePath))
File.Delete(_tempFilePath);
_lastError = e.Message;
_steps = ESteps.TryAgain;
}
}