Update CacheSystem

This commit is contained in:
hevinci
2022-08-06 16:23:38 +08:00
parent dcd4475617
commit a1ad7acb3d
16 changed files with 202 additions and 131 deletions
@@ -14,18 +14,15 @@ namespace YooAsset
{
private static readonly Dictionary<string, DownloaderBase> _downloaderDic = new Dictionary<string, DownloaderBase>();
private static readonly List<string> _removeList = new List<string>(100);
private static readonly Dictionary<string, string> _cachedHashList = new Dictionary<string, string>(1000);
private static int _breakpointResumeFileSize = int.MaxValue;
private static EVerifyLevel _verifyLevel = EVerifyLevel.High;
/// <summary>
/// 初始化
/// </summary>
public static void Initialize(int breakpointResumeFileSize, EVerifyLevel verifyLevel)
public static void Initialize(int breakpointResumeFileSize)
{
_breakpointResumeFileSize = breakpointResumeFileSize;
_verifyLevel = verifyLevel;
}
/// <summary>
@@ -62,7 +59,6 @@ namespace YooAsset
}
_downloaderDic.Clear();
_removeList.Clear();
_cachedHashList.Clear();
_breakpointResumeFileSize = int.MaxValue;
}
@@ -80,7 +76,7 @@ namespace YooAsset
}
// 如果资源已经缓存
if (ContainsVerifyFile(bundleInfo.FileHash))
if (CacheSystem.ContainsVerifyFile(bundleInfo.FileHash))
{
var tempDownloader = new TempDownloader(bundleInfo);
return tempDownloader;
@@ -108,84 +104,5 @@ namespace YooAsset
{
return _downloaderDic.Count;
}
/// <summary>
/// 查询是否为验证文件
/// 注意:被收录的文件完整性是绝对有效的
/// </summary>
public static bool ContainsVerifyFile(string fileHash)
{
if (_cachedHashList.ContainsKey(fileHash))
{
string fileName = _cachedHashList[fileHash];
string filePath = SandboxHelper.MakeCacheFilePath(fileName);
if (File.Exists(filePath))
{
return true;
}
else
{
_cachedHashList.Remove(fileHash);
YooLogger.Error($"Cache file is missing : {fileName}");
return false;
}
}
else
{
return false;
}
}
/// <summary>
/// 缓存验证过的文件
/// </summary>
public static void CacheVerifyFile(string fileHash, string fileName)
{
if (_cachedHashList.ContainsKey(fileHash) == false)
{
YooLogger.Log($"Cache verify file : {fileName}");
_cachedHashList.Add(fileHash, fileName);
}
}
/// <summary>
/// 验证文件完整性
/// </summary>
public static bool CheckContentIntegrity(string filePath, long fileSize, string fileCRC)
{
return CheckContentIntegrity(_verifyLevel, filePath, fileSize, fileCRC);
}
/// <summary>
/// 验证文件完整性
/// </summary>
public static bool CheckContentIntegrity(EVerifyLevel verifyLevel, string filePath, long fileSize, string fileCRC)
{
try
{
if (File.Exists(filePath) == false)
return false;
// 先验证文件大小
long size = FileUtility.GetFileSize(filePath);
if (size != fileSize)
return false;
// 再验证文件CRC
if (verifyLevel == EVerifyLevel.High)
{
string crc = HashUtility.FileCRC32(filePath);
return crc == fileCRC;
}
else
{
return true;
}
}
catch (Exception)
{
return false;
}
}
}
}