Update cache system

pull/40/head
hevinci 2022-08-30 18:03:20 +08:00
parent d47c0bf4b6
commit a9e858bc9a
8 changed files with 101 additions and 34 deletions

View File

@ -159,7 +159,7 @@ namespace YooAsset
if (MainBundleInfo.LoadMode == BundleInfo.ELoadMode.LoadFromCache)
{
string cacheLoadPath = MainBundleInfo.Bundle.CachedFilePath;
if (CacheSystem.VerifyBundle(MainBundleInfo.Bundle, EVerifyLevel.High) == false)
if (CacheSystem.VerifyBundle(MainBundleInfo.Bundle, EVerifyLevel.High) != EVerifyResult.Succeed)
{
if (File.Exists(cacheLoadPath))
{

View File

@ -257,8 +257,8 @@ namespace YooAsset
// 如果原生文件已经存在,则验证其完整性
if (File.Exists(CopyPath))
{
bool result = CacheSystem.VerifyContentInternal(CopyPath, _bundleInfo.Bundle.FileSize, _bundleInfo.Bundle.FileCRC, EVerifyLevel.High);
if (result)
var verifyResult = CacheSystem.VerifyContentInternal(CopyPath, _bundleInfo.Bundle.FileSize, _bundleInfo.Bundle.FileCRC, EVerifyLevel.High);
if (verifyResult == EVerifyResult.Succeed)
{
_steps = ESteps.Done;
Status = EOperationStatus.Succeed;
@ -405,8 +405,8 @@ namespace YooAsset
// 如果原生文件已经存在,则验证其完整性
if (File.Exists(CopyPath))
{
bool result = CacheSystem.VerifyContentInternal(CopyPath, _bundleInfo.Bundle.FileSize, _bundleInfo.Bundle.FileCRC, EVerifyLevel.High);
if (result)
var verifyResult = CacheSystem.VerifyContentInternal(CopyPath, _bundleInfo.Bundle.FileSize, _bundleInfo.Bundle.FileCRC, EVerifyLevel.High);
if (verifyResult == EVerifyResult.Succeed)
{
_steps = ESteps.Done;
Status = EOperationStatus.Succeed;

View File

@ -68,7 +68,7 @@ namespace YooAsset
/// <summary>
/// 验证补丁包文件
/// </summary>
public static bool VerifyBundle(PatchBundle patchBundle, EVerifyLevel verifyLevel)
public static EVerifyResult VerifyBundle(PatchBundle patchBundle, EVerifyLevel verifyLevel)
{
return VerifyContentInternal(patchBundle.CachedFilePath, patchBundle.FileSize, patchBundle.FileCRC, verifyLevel);
}
@ -76,48 +76,48 @@ namespace YooAsset
/// <summary>
/// 验证并缓存补丁包文件
/// </summary>
public static bool VerifyAndCacheBundle(PatchBundle patchBundle, EVerifyLevel verifyLevel)
public static EVerifyResult VerifyAndCacheBundle(PatchBundle patchBundle, EVerifyLevel verifyLevel)
{
if (VerifyContentInternal(patchBundle.CachedFilePath, patchBundle.FileSize, patchBundle.FileCRC, verifyLevel))
{
var verifyResult = VerifyContentInternal(patchBundle.CachedFilePath, patchBundle.FileSize, patchBundle.FileCRC, verifyLevel);
if (verifyResult == EVerifyResult.Succeed)
CacheBundle(patchBundle);
return true;
}
else
{
return false;
}
return verifyResult;
}
/// <summary>
/// 验证文件完整性
/// </summary>
public static bool VerifyContentInternal(string filePath, long fileSize, string fileCRC, EVerifyLevel verifyLevel)
public static EVerifyResult VerifyContentInternal(string filePath, long fileSize, string fileCRC, EVerifyLevel verifyLevel)
{
try
{
if (File.Exists(filePath) == false)
return false;
return EVerifyResult.FileNotExisted;
// 先验证文件大小
long size = FileUtility.GetFileSize(filePath);
if (size != fileSize)
return false;
if (size < fileSize)
return EVerifyResult.FileNotComplete;
else if (size > fileSize)
return EVerifyResult.FileOverflow;
// 再验证文件CRC
if (verifyLevel == EVerifyLevel.High)
{
string crc = HashUtility.FileCRC32(filePath);
return crc == fileCRC;
if (crc == fileCRC)
return EVerifyResult.Succeed;
else
return EVerifyResult.FileCrcError;
}
else
{
return true;
return EVerifyResult.Succeed;
}
}
catch (Exception)
{
return false;
return EVerifyResult.Exception;
}
}
}

View File

@ -0,0 +1,39 @@

namespace YooAsset
{
/// <summary>
/// 下载文件校验结果
/// </summary>
public enum EVerifyResult
{
/// <summary>
/// 文件不存在
/// </summary>
FileNotExisted = -4,
/// <summary>
/// 文件内容不足(小于正常大小)
/// </summary>
FileNotComplete = -3,
/// <summary>
/// 文件内容溢出(超过正常大小)
/// </summary>
FileOverflow = -2,
/// <summary>
/// 文件内容不匹配
/// </summary>
FileCrcError = -1,
/// <summary>
/// 验证异常
/// </summary>
Exception = 0,
/// <summary>
/// 验证成功
/// </summary>
Succeed = 1,
}
}

View File

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 65584a0568d40b14582a3c4aaf947b98
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -26,7 +26,7 @@ namespace YooAsset
{
private class ThreadInfo
{
public bool Result = false;
public EVerifyResult Result;
public string FilePath { private set; get; }
public PatchBundle Bundle { private set; get; }
public ThreadInfo(string filePath, PatchBundle bundle)
@ -137,7 +137,7 @@ namespace YooAsset
private void VerifyCallback(object obj)
{
ThreadInfo info = (ThreadInfo)obj;
if (info.Result)
if (info.Result == EVerifyResult.Succeed)
{
VerifySuccessCount++;
CacheSystem.CacheBundle(info.Bundle);
@ -234,7 +234,8 @@ namespace YooAsset
private void VerifyFile(PatchBundle patchBundle)
{
if (CacheSystem.VerifyAndCacheBundle(patchBundle, CacheSystem.InitVerifyLevel))
var verifyResult = CacheSystem.VerifyAndCacheBundle(patchBundle, CacheSystem.InitVerifyLevel);
if (verifyResult == EVerifyResult.Succeed)
{
VerifySuccessCount++;
}

View File

@ -10,7 +10,7 @@ namespace YooAsset
internal sealed class FileDownloader : DownloaderBase
{
private UnityWebRequest _webRequest;
private bool _breakDownload;
private bool _breakResume;
// 重置变量
private bool _isAbort = false;
@ -19,9 +19,9 @@ namespace YooAsset
private float _tryAgainTimer;
public FileDownloader(BundleInfo bundleInfo, bool breakDownload) : base(bundleInfo)
public FileDownloader(BundleInfo bundleInfo, bool breakResume) : base(bundleInfo)
{
_breakDownload = breakDownload;
_breakResume = breakResume;
}
public override void Update()
{
@ -33,12 +33,19 @@ namespace YooAsset
// 检测本地文件
if (_steps == ESteps.CheckLocalFile)
{
if (CacheSystem.VerifyAndCacheBundle(_bundleInfo.Bundle, EVerifyLevel.High))
var verifyResult = CacheSystem.VerifyAndCacheBundle(_bundleInfo.Bundle, EVerifyLevel.High);
if (verifyResult == EVerifyResult.Succeed)
{
_steps = ESteps.Succeed;
}
else
{
if(verifyResult == EVerifyResult.FileOverflow)
{
string cacheFilePath = _bundleInfo.Bundle.CachedFilePath;
if (File.Exists(cacheFilePath))
File.Delete(cacheFilePath);
}
_steps = ESteps.CreateDownload;
}
}
@ -57,7 +64,7 @@ namespace YooAsset
_tryAgainTimer = 0f;
// 是否开启断点续传下载
if (_breakDownload)
if (_breakResume)
{
long fileLength = -1;
if (File.Exists(fileSavePath))
@ -121,7 +128,8 @@ namespace YooAsset
// 检查文件完整性
if (hasError == false)
{
if (CacheSystem.VerifyAndCacheBundle(_bundleInfo.Bundle, EVerifyLevel.High) == false)
var verifyResult = CacheSystem.VerifyAndCacheBundle(_bundleInfo.Bundle, EVerifyLevel.High);
if (verifyResult != EVerifyResult.Succeed)
{
hasError = true;
_lastError = $"Verify bundle content failed : {_bundleInfo.Bundle.FileName}";
@ -137,7 +145,7 @@ namespace YooAsset
if (hasError)
{
// 注意:非断点续传下载失败后删除文件
if (_breakDownload == false)
if (_breakResume == false)
{
string cacheFilePath = _bundleInfo.Bundle.CachedFilePath;
if (File.Exists(cacheFilePath))

View File

@ -171,12 +171,19 @@ namespace YooAsset
// 检测本地文件
if (_steps == ESteps.CheckLocalFile)
{
if (CacheSystem.VerifyAndCacheBundle(_bundleInfo.Bundle, EVerifyLevel.High))
var verifyResult = CacheSystem.VerifyAndCacheBundle(_bundleInfo.Bundle, EVerifyLevel.High);
if (verifyResult == EVerifyResult.Succeed)
{
_steps = ESteps.Succeed;
}
else
{
if (verifyResult == EVerifyResult.FileOverflow)
{
string cacheFilePath = _bundleInfo.Bundle.CachedFilePath;
if (File.Exists(cacheFilePath))
File.Delete(cacheFilePath);
}
_steps = ESteps.CreateDownload;
}
}
@ -215,7 +222,8 @@ namespace YooAsset
// 检查文件完整性
if (hasError == false)
{
if (CacheSystem.VerifyAndCacheBundle(_bundleInfo.Bundle, EVerifyLevel.High) == false)
var verifyResult = CacheSystem.VerifyAndCacheBundle(_bundleInfo.Bundle, EVerifyLevel.High);
if (verifyResult != EVerifyResult.Succeed)
{
hasError = true;
_lastError = $"Verify bundle content failed : {_bundleInfo.Bundle.FileName}";