Update CacheSystem

优化缓存系统代码结构
This commit is contained in:
hevinci
2022-08-15 11:55:13 +08:00
parent aba68f859f
commit fbbf762e70
26 changed files with 306 additions and 446 deletions
@@ -2,71 +2,46 @@
using System.IO;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace YooAsset
{
internal static class CacheSystem
{
private readonly static HashSet<PatchBundle> _cacheBundles = new HashSet<PatchBundle>();
private readonly static Dictionary<string, string> _cachedHashList = new Dictionary<string, string>(1000);
private static EVerifyLevel _verifyLevel = EVerifyLevel.High;
private readonly static Dictionary<string, PatchBundle> _cachedDic = new Dictionary<string, PatchBundle>(1000);
public static void Initialize(EVerifyLevel verifyLevel)
/// <summary>
/// 初始化时的验证级别
/// </summary>
public static EVerifyLevel InitVerifyLevel { private set; get; }
public static void Initialize(EVerifyLevel initVerifyLevel)
{
_verifyLevel = verifyLevel;
InitVerifyLevel = initVerifyLevel;
}
public static void DestroyAll()
{
_cacheBundles.Clear();
_cachedDic.Clear();
}
public static void WriteInfoFileForCachedFile()
{
}
public static void ReadInfoFileForCachedFile()
{
}
public static void GetCachingDiskSpaceUsed()
{
}
public static void GetCachingDiskSpaceFree()
{
}
public static bool IsCached(PatchBundle patchBundle)
{
return false;
}
public static void ClearCache()
{
}
/// <summary>
/// 查询是否为验证文件
/// 注意:被收录的文件完整性是绝对有效的
/// </summary>
public static bool ContainsVerifyFile(PatchBundle patchBundle)
public static bool IsCached(PatchBundle patchBundle)
{
string fileHash = patchBundle.FileHash;
if (_cachedHashList.ContainsKey(fileHash))
if (_cachedDic.ContainsKey(fileHash))
{
string fileName = _cachedHashList[fileHash];
string filePath = SandboxHelper.MakeCacheFilePath(fileName);
string filePath = patchBundle.CachedFilePath;
if (File.Exists(filePath))
{
return true;
}
else
{
_cachedHashList.Remove(fileHash);
YooLogger.Error($"Cache file is missing : {fileName}");
_cachedDic.Remove(fileHash);
YooLogger.Error($"Cache file is missing : {filePath}");
return false;
}
}
@@ -77,31 +52,47 @@ namespace YooAsset
}
/// <summary>
/// 缓存验证过的文件
/// 缓存补丁包文件
/// </summary>
public static void CacheVerifyFile(PatchBundle patchBundle)
public static void CacheBundle(PatchBundle patchBundle)
{
string fileHash = patchBundle.FileHash;
string fileName = patchBundle.FileName;
if (_cachedHashList.ContainsKey(fileHash) == false)
if (_cachedDic.ContainsKey(fileHash) == false)
{
YooLogger.Log($"Cache verify file : {fileName}");
_cachedHashList.Add(fileHash, fileName);
string filePath = patchBundle.CachedFilePath;
YooLogger.Log($"Cache verify file : {filePath}");
_cachedDic.Add(fileHash, patchBundle);
}
}
/// <summary>
/// 验证补丁包文件
/// </summary>
public static bool VerifyBundle(PatchBundle patchBundle, EVerifyLevel verifyLevel)
{
return VerifyContentInternal(patchBundle.CachedFilePath, patchBundle.FileSize, patchBundle.FileCRC, verifyLevel);
}
/// <summary>
/// 验证并缓存补丁包文件
/// </summary>
public static bool VerifyAndCacheBundle(PatchBundle patchBundle, EVerifyLevel verifyLevel)
{
if (VerifyContentInternal(patchBundle.CachedFilePath, patchBundle.FileSize, patchBundle.FileCRC, verifyLevel))
{
CacheBundle(patchBundle);
return true;
}
else
{
return false;
}
}
/// <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)
public static bool VerifyContentInternal(string filePath, long fileSize, string fileCRC, EVerifyLevel verifyLevel)
{
try
{
@@ -48,7 +48,7 @@ namespace YooAsset
foreach (var patchBundle in localPatchManifest.BundleList)
{
// 忽略缓存文件
if (CacheSystem.ContainsVerifyFile(patchBundle))
if (CacheSystem.IsCached(patchBundle))
continue;
// 忽略APP资源
@@ -62,7 +62,7 @@ namespace YooAsset
// 注意:在弱联网模式下,我们需要验证指定资源版本的所有资源完整性
if (weaklyUpdate)
{
string filePath = SandboxHelper.MakeCacheFilePath(patchBundle.FileName);
string filePath = patchBundle.CachedFilePath;
if (File.Exists(filePath))
_waitingList.Add(patchBundle);
else
@@ -70,7 +70,7 @@ namespace YooAsset
}
else
{
string filePath = SandboxHelper.MakeCacheFilePath(patchBundle.FileName);
string filePath = patchBundle.CachedFilePath;
if (File.Exists(filePath))
_waitingList.Add(patchBundle);
}
@@ -124,14 +124,14 @@ namespace YooAsset
private bool VerifyFile(PatchBundle patchBundle)
{
string filePath = SandboxHelper.MakeCacheFilePath(patchBundle.FileName);
string filePath = patchBundle.CachedFilePath;
ThreadInfo info = new ThreadInfo(filePath, patchBundle);
return ThreadPool.QueueUserWorkItem(new WaitCallback(VerifyInThread), info);
}
private void VerifyInThread(object infoObj)
{
ThreadInfo info = (ThreadInfo)infoObj;
info.Result = CacheSystem.CheckContentIntegrity(info.FilePath, info.Bundle.FileSize, info.Bundle.FileCRC);
info.Result = CacheSystem.VerifyBundle(info.Bundle, CacheSystem.InitVerifyLevel);
_syncContext.Post(VerifyCallback, info);
}
private void VerifyCallback(object obj)
@@ -140,17 +140,17 @@ namespace YooAsset
if (info.Result)
{
VerifySuccessCount++;
CacheSystem.CacheVerifyFile(info.Bundle);
CacheSystem.CacheBundle(info.Bundle);
}
else
{
VerifyFailCount++;
YooLogger.Warning($"Failed to verify file : {info.Bundle.CachedFilePath}");
// NOTE:不期望删除断点续传的资源文件
/*
YooLogger.Warning($"Failed to verify file : {info.FilePath}");
if (File.Exists(info.FilePath))
File.Delete(info.FilePath);
if (File.Exists(patchBundle.CachedBundleFilePath))
File.Delete(patchBundle.CachedBundleFilePath);
*/
}
_verifyingList.Remove(info.Bundle);
@@ -173,7 +173,7 @@ namespace YooAsset
foreach (var patchBundle in localPatchManifest.BundleList)
{
// 忽略缓存文件
if (CacheSystem.ContainsVerifyFile(patchBundle))
if (CacheSystem.IsCached(patchBundle))
continue;
// 忽略APP资源
@@ -187,7 +187,7 @@ namespace YooAsset
// 注意:在弱联网模式下,我们需要验证指定资源版本的所有资源完整性
if (weaklyUpdate)
{
string filePath = SandboxHelper.MakeCacheFilePath(patchBundle.FileName);
string filePath = patchBundle.CachedFilePath;
if (File.Exists(filePath))
_waitingList.Add(patchBundle);
else
@@ -195,7 +195,7 @@ namespace YooAsset
}
else
{
string filePath = SandboxHelper.MakeCacheFilePath(patchBundle.FileName);
string filePath = patchBundle.CachedFilePath;
if (File.Exists(filePath))
_waitingList.Add(patchBundle);
}
@@ -234,22 +234,19 @@ namespace YooAsset
private void VerifyFile(PatchBundle patchBundle)
{
string filePath = SandboxHelper.MakeCacheFilePath(patchBundle.FileName);
bool result = CacheSystem.CheckContentIntegrity(filePath, patchBundle.FileSize, patchBundle.FileCRC);
if (result)
if (CacheSystem.VerifyAndCacheBundle(patchBundle, CacheSystem.InitVerifyLevel))
{
VerifySuccessCount++;
CacheSystem.CacheVerifyFile(patchBundle);
}
else
{
VerifyFailCount++;
YooLogger.Warning($"Failed to verify file : {patchBundle.CachedFilePath}");
// NOTE:不期望删除断点续传的资源文件
/*
YooLogger.Warning($"Failed to verify file : {info.FilePath}");
if (File.Exists(info.FilePath))
File.Delete(info.FilePath);
/*
if (File.Exists(patchBundle.CachedBundleFilePath))
File.Delete(patchBundle.CachedBundleFilePath);
*/
}
}