Add VerifyLevel filed.

增加了下载文件校验等级初始化参数。
pull/15/head
hevinci 2022-06-21 16:04:59 +08:00
parent c0099cb90c
commit 00688a65af
6 changed files with 80 additions and 17 deletions

View File

@ -1,4 +1,5 @@
using System; using System;
using System.IO;
using System.Collections; using System.Collections;
using System.Collections.Generic; using System.Collections.Generic;
using UnityEngine; using UnityEngine;
@ -39,7 +40,7 @@ namespace YooAsset
if (_steps == ESteps.None) if (_steps == ESteps.None)
{ {
if(MainBundleInfo.IsInvalid) if (MainBundleInfo.IsInvalid)
{ {
_steps = ESteps.Done; _steps = ESteps.Done;
Status = EStatus.Failed; Status = EStatus.Failed;
@ -158,6 +159,21 @@ namespace YooAsset
Status = EStatus.Failed; Status = EStatus.Failed;
LastError = $"Failed to load assetBundle : {MainBundleInfo.BundleName}"; LastError = $"Failed to load assetBundle : {MainBundleInfo.BundleName}";
YooLogger.Error(LastError); 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 else
{ {

View File

@ -16,13 +16,16 @@ namespace YooAsset
private static readonly List<string> _removeList = new List<string>(100); private static readonly List<string> _removeList = new List<string>(100);
private static readonly Dictionary<string, string> _cachedHashList = new Dictionary<string, string>(1000); private static readonly Dictionary<string, string> _cachedHashList = new Dictionary<string, string>(1000);
private static int _breakpointResumeFileSize = int.MaxValue; private static int _breakpointResumeFileSize = int.MaxValue;
private static EVerifyLevel _verifyLevel = EVerifyLevel.High;
/// <summary> /// <summary>
/// 初始化 /// 初始化
/// </summary> /// </summary>
public static void Initialize(int breakpointResumeFileSize) public static void Initialize(int breakpointResumeFileSize, EVerifyLevel verifyLevel)
{ {
_breakpointResumeFileSize = breakpointResumeFileSize; _breakpointResumeFileSize = breakpointResumeFileSize;
_verifyLevel = verifyLevel;
} }
/// <summary> /// <summary>
@ -145,17 +148,18 @@ namespace YooAsset
} }
} }
// 验证文件完整性 /// <summary>
public static bool CheckContentIntegrity(BundleInfo bundleInfo) /// 验证文件完整性
{ /// </summary>
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) public static bool CheckContentIntegrity(string filePath, long size, string crc)
{
return CheckContentIntegrity(_verifyLevel, filePath, size, crc);
}
/// <summary>
/// 验证文件完整性
/// </summary>
public static bool CheckContentIntegrity(EVerifyLevel verifyLevel, string filePath, long size, string crc)
{ {
try try
{ {
@ -168,10 +172,17 @@ namespace YooAsset
return false; return false;
// 再验证文件CRC // 再验证文件CRC
if (verifyLevel == EVerifyLevel.High)
{
string fileCRC = HashUtility.FileCRC32(filePath); string fileCRC = HashUtility.FileCRC32(filePath);
return fileCRC == crc; return fileCRC == crc;
} }
catch(Exception) else
{
return true;
}
}
catch (Exception)
{ {
return false; return false;
} }

View File

@ -81,7 +81,8 @@ namespace YooAsset
if (hasError == false) if (hasError == false)
{ {
// 注意:如果文件验证失败需要删除文件 // 注意:如果文件验证失败需要删除文件
if (DownloadSystem.CheckContentIntegrity(_bundleInfo) == false)
if (DownloadSystem.CheckContentIntegrity(_bundleInfo.GetCacheLoadPath(), _bundleInfo.SizeBytes, _bundleInfo.CRC) == false)
{ {
hasError = true; hasError = true;
_lastError = $"Verification failed"; _lastError = $"Verification failed";

View File

@ -0,0 +1,19 @@

namespace YooAsset
{
/// <summary>
/// 下载文件校验等级
/// </summary>
public enum EVerifyLevel
{
/// <summary>
/// 验证文件大小
/// </summary>
Low,
/// <summary>
/// 验证文件大小和CRC
/// </summary>
High,
}
}

View File

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

View File

@ -104,6 +104,11 @@ namespace YooAsset
/// 启用断点续传功能的文件大小 /// 启用断点续传功能的文件大小
/// </summary> /// </summary>
public int BreakpointResumeFileSize = int.MaxValue; public int BreakpointResumeFileSize = int.MaxValue;
/// <summary>
/// 下载文件校验等级
/// </summary>
public EVerifyLevel VerifyLevel = EVerifyLevel.High;
} }
@ -193,7 +198,7 @@ namespace YooAsset
throw new Exception($"{EPlayMode.HostPlayMode} not supports WebGL platform !"); throw new Exception($"{EPlayMode.HostPlayMode} not supports WebGL platform !");
#else #else
var hostPlayModeParameters = parameters as HostPlayModeParameters; var hostPlayModeParameters = parameters as HostPlayModeParameters;
DownloadSystem.Initialize(hostPlayModeParameters.BreakpointResumeFileSize); DownloadSystem.Initialize(hostPlayModeParameters.BreakpointResumeFileSize, hostPlayModeParameters.VerifyLevel);
#endif #endif
} }