diff --git a/Assets/YooAsset/Runtime/AssetSystem/Loader/AssetBundleFileLoader.cs b/Assets/YooAsset/Runtime/AssetSystem/Loader/AssetBundleFileLoader.cs index e2bbf74..da08447 100644 --- a/Assets/YooAsset/Runtime/AssetSystem/Loader/AssetBundleFileLoader.cs +++ b/Assets/YooAsset/Runtime/AssetSystem/Loader/AssetBundleFileLoader.cs @@ -1,4 +1,5 @@ using System; +using System.IO; using System.Collections; using System.Collections.Generic; using UnityEngine; @@ -39,7 +40,7 @@ namespace YooAsset if (_steps == ESteps.None) { - if(MainBundleInfo.IsInvalid) + if (MainBundleInfo.IsInvalid) { _steps = ESteps.Done; Status = EStatus.Failed; @@ -158,6 +159,21 @@ namespace YooAsset Status = EStatus.Failed; LastError = $"Failed to load assetBundle : {MainBundleInfo.BundleName}"; YooLogger.Error(LastError); + + // 注意:当缓存文件的校验等级为Low的时候,并不能保证缓存文件的完整性。 + // 在AssetBundle文件加载失败的情况下,我们需要重新验证文件的完整性! + if (MainBundleInfo.LoadMode == BundleInfo.ELoadMode.LoadFromCache) + { + string cacheLoadPath = MainBundleInfo.GetCacheLoadPath(); + if (DownloadSystem.CheckContentIntegrity(EVerifyLevel.High, cacheLoadPath, MainBundleInfo.SizeBytes, MainBundleInfo.CRC) == false) + { + if (File.Exists(cacheLoadPath)) + { + YooLogger.Error($"Delete the invalid cache file : {cacheLoadPath}"); + File.Delete(cacheLoadPath); + } + } + } } else { diff --git a/Assets/YooAsset/Runtime/DownloadSystem/DownloadSystem.cs b/Assets/YooAsset/Runtime/DownloadSystem/DownloadSystem.cs index a1062e2..17402b7 100644 --- a/Assets/YooAsset/Runtime/DownloadSystem/DownloadSystem.cs +++ b/Assets/YooAsset/Runtime/DownloadSystem/DownloadSystem.cs @@ -16,13 +16,16 @@ namespace YooAsset private static readonly List _removeList = new List(100); private static readonly Dictionary _cachedHashList = new Dictionary(1000); private static int _breakpointResumeFileSize = int.MaxValue; + private static EVerifyLevel _verifyLevel = EVerifyLevel.High; + /// /// 初始化 /// - public static void Initialize(int breakpointResumeFileSize) + public static void Initialize(int breakpointResumeFileSize, EVerifyLevel verifyLevel) { _breakpointResumeFileSize = breakpointResumeFileSize; + _verifyLevel = verifyLevel; } /// @@ -145,17 +148,18 @@ namespace YooAsset } } - // 验证文件完整性 - public static bool CheckContentIntegrity(BundleInfo bundleInfo) - { - return CheckContentIntegrity(bundleInfo.GetCacheLoadPath(), bundleInfo.SizeBytes, bundleInfo.CRC); - } - public static bool CheckContentIntegrity(PatchBundle patchBundle) - { - string filePath = SandboxHelper.MakeCacheFilePath(patchBundle.Hash); - return CheckContentIntegrity(filePath, patchBundle.SizeBytes, patchBundle.CRC); - } + /// + /// 验证文件完整性 + /// public static bool CheckContentIntegrity(string filePath, long size, string crc) + { + return CheckContentIntegrity(_verifyLevel, filePath, size, crc); + } + + /// + /// 验证文件完整性 + /// + public static bool CheckContentIntegrity(EVerifyLevel verifyLevel, string filePath, long size, string crc) { try { @@ -168,10 +172,17 @@ namespace YooAsset return false; // 再验证文件CRC - string fileCRC = HashUtility.FileCRC32(filePath); - return fileCRC == crc; + if (verifyLevel == EVerifyLevel.High) + { + string fileCRC = HashUtility.FileCRC32(filePath); + return fileCRC == crc; + } + else + { + return true; + } } - catch(Exception) + catch (Exception) { return false; } diff --git a/Assets/YooAsset/Runtime/DownloadSystem/Downloader/FileDownloader.cs b/Assets/YooAsset/Runtime/DownloadSystem/Downloader/FileDownloader.cs index 74c4fc0..60a7514 100644 --- a/Assets/YooAsset/Runtime/DownloadSystem/Downloader/FileDownloader.cs +++ b/Assets/YooAsset/Runtime/DownloadSystem/Downloader/FileDownloader.cs @@ -81,7 +81,8 @@ namespace YooAsset if (hasError == false) { // 注意:如果文件验证失败需要删除文件 - if (DownloadSystem.CheckContentIntegrity(_bundleInfo) == false) + + if (DownloadSystem.CheckContentIntegrity(_bundleInfo.GetCacheLoadPath(), _bundleInfo.SizeBytes, _bundleInfo.CRC) == false) { hasError = true; _lastError = $"Verification failed"; diff --git a/Assets/YooAsset/Runtime/DownloadSystem/EVerifyLevel.cs b/Assets/YooAsset/Runtime/DownloadSystem/EVerifyLevel.cs new file mode 100644 index 0000000..a3e7943 --- /dev/null +++ b/Assets/YooAsset/Runtime/DownloadSystem/EVerifyLevel.cs @@ -0,0 +1,19 @@ + +namespace YooAsset +{ + /// + /// 下载文件校验等级 + /// + public enum EVerifyLevel + { + /// + /// 验证文件大小 + /// + Low, + + /// + /// 验证文件大小和CRC + /// + High, + } +} \ No newline at end of file diff --git a/Assets/YooAsset/Runtime/DownloadSystem/EVerifyLevel.cs.meta b/Assets/YooAsset/Runtime/DownloadSystem/EVerifyLevel.cs.meta new file mode 100644 index 0000000..26f29bb --- /dev/null +++ b/Assets/YooAsset/Runtime/DownloadSystem/EVerifyLevel.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 02ad351eb8539da47a0c789e2f8c468f +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/YooAsset/Runtime/YooAssets.cs b/Assets/YooAsset/Runtime/YooAssets.cs index c6506b7..32dbe86 100644 --- a/Assets/YooAsset/Runtime/YooAssets.cs +++ b/Assets/YooAsset/Runtime/YooAssets.cs @@ -104,6 +104,11 @@ namespace YooAsset /// 启用断点续传功能的文件大小 /// public int BreakpointResumeFileSize = int.MaxValue; + + /// + /// 下载文件校验等级 + /// + public EVerifyLevel VerifyLevel = EVerifyLevel.High; } @@ -193,7 +198,7 @@ namespace YooAsset throw new Exception($"{EPlayMode.HostPlayMode} not supports WebGL platform !"); #else var hostPlayModeParameters = parameters as HostPlayModeParameters; - DownloadSystem.Initialize(hostPlayModeParameters.BreakpointResumeFileSize); + DownloadSystem.Initialize(hostPlayModeParameters.BreakpointResumeFileSize, hostPlayModeParameters.VerifyLevel); #endif }