From a9e858bc9a6e798d77e46bd153cd108e5c06c04d Mon Sep 17 00:00:00 2001 From: hevinci Date: Tue, 30 Aug 2022 18:03:20 +0800 Subject: [PATCH] Update cache system --- .../Loader/AssetBundleFileLoader.cs | 2 +- .../Operations/RawFileOperation.cs | 8 ++-- .../Runtime/CacheSystem/CacheSystem.cs | 34 ++++++++-------- .../Runtime/CacheSystem/EVerifyResult.cs | 39 +++++++++++++++++++ .../Runtime/CacheSystem/EVerifyResult.cs.meta | 11 ++++++ .../Runtime/CacheSystem/PatchCacheVerifier.cs | 7 ++-- .../Downloader/FileDownloader.cs | 22 +++++++---- .../Downloader/HttpDownloader.cs | 12 +++++- 8 files changed, 101 insertions(+), 34 deletions(-) create mode 100644 Assets/YooAsset/Runtime/CacheSystem/EVerifyResult.cs create mode 100644 Assets/YooAsset/Runtime/CacheSystem/EVerifyResult.cs.meta diff --git a/Assets/YooAsset/Runtime/AssetSystem/Loader/AssetBundleFileLoader.cs b/Assets/YooAsset/Runtime/AssetSystem/Loader/AssetBundleFileLoader.cs index 8079637..0a8bf7e 100644 --- a/Assets/YooAsset/Runtime/AssetSystem/Loader/AssetBundleFileLoader.cs +++ b/Assets/YooAsset/Runtime/AssetSystem/Loader/AssetBundleFileLoader.cs @@ -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)) { diff --git a/Assets/YooAsset/Runtime/AssetSystem/Operations/RawFileOperation.cs b/Assets/YooAsset/Runtime/AssetSystem/Operations/RawFileOperation.cs index 4ab868e..3adfe02 100644 --- a/Assets/YooAsset/Runtime/AssetSystem/Operations/RawFileOperation.cs +++ b/Assets/YooAsset/Runtime/AssetSystem/Operations/RawFileOperation.cs @@ -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; diff --git a/Assets/YooAsset/Runtime/CacheSystem/CacheSystem.cs b/Assets/YooAsset/Runtime/CacheSystem/CacheSystem.cs index 923764e..0a1bd17 100644 --- a/Assets/YooAsset/Runtime/CacheSystem/CacheSystem.cs +++ b/Assets/YooAsset/Runtime/CacheSystem/CacheSystem.cs @@ -68,7 +68,7 @@ namespace YooAsset /// /// 验证补丁包文件 /// - 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 /// /// 验证并缓存补丁包文件 /// - 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; } /// /// 验证文件完整性 /// - 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; } } } diff --git a/Assets/YooAsset/Runtime/CacheSystem/EVerifyResult.cs b/Assets/YooAsset/Runtime/CacheSystem/EVerifyResult.cs new file mode 100644 index 0000000..c59e8bc --- /dev/null +++ b/Assets/YooAsset/Runtime/CacheSystem/EVerifyResult.cs @@ -0,0 +1,39 @@ + +namespace YooAsset +{ + /// + /// 下载文件校验结果 + /// + public enum EVerifyResult + { + /// + /// 文件不存在 + /// + FileNotExisted = -4, + + /// + /// 文件内容不足(小于正常大小) + /// + FileNotComplete = -3, + + /// + /// 文件内容溢出(超过正常大小) + /// + FileOverflow = -2, + + /// + /// 文件内容不匹配 + /// + FileCrcError = -1, + + /// + /// 验证异常 + /// + Exception = 0, + + /// + /// 验证成功 + /// + Succeed = 1, + } +} \ No newline at end of file diff --git a/Assets/YooAsset/Runtime/CacheSystem/EVerifyResult.cs.meta b/Assets/YooAsset/Runtime/CacheSystem/EVerifyResult.cs.meta new file mode 100644 index 0000000..4060acc --- /dev/null +++ b/Assets/YooAsset/Runtime/CacheSystem/EVerifyResult.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 65584a0568d40b14582a3c4aaf947b98 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/YooAsset/Runtime/CacheSystem/PatchCacheVerifier.cs b/Assets/YooAsset/Runtime/CacheSystem/PatchCacheVerifier.cs index c550946..f82c2a4 100644 --- a/Assets/YooAsset/Runtime/CacheSystem/PatchCacheVerifier.cs +++ b/Assets/YooAsset/Runtime/CacheSystem/PatchCacheVerifier.cs @@ -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++; } diff --git a/Assets/YooAsset/Runtime/DownloadSystem/Downloader/FileDownloader.cs b/Assets/YooAsset/Runtime/DownloadSystem/Downloader/FileDownloader.cs index 142ccd5..c3c2d55 100644 --- a/Assets/YooAsset/Runtime/DownloadSystem/Downloader/FileDownloader.cs +++ b/Assets/YooAsset/Runtime/DownloadSystem/Downloader/FileDownloader.cs @@ -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)) diff --git a/Assets/YooAsset/Runtime/DownloadSystem/Downloader/HttpDownloader.cs b/Assets/YooAsset/Runtime/DownloadSystem/Downloader/HttpDownloader.cs index f4ee145..33088c0 100644 --- a/Assets/YooAsset/Runtime/DownloadSystem/Downloader/HttpDownloader.cs +++ b/Assets/YooAsset/Runtime/DownloadSystem/Downloader/HttpDownloader.cs @@ -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}";