Update CacheSystem

pull/35/head
hevinci 2022-08-06 16:23:38 +08:00
parent dcd4475617
commit a1ad7acb3d
16 changed files with 202 additions and 131 deletions

View File

@ -169,7 +169,7 @@ namespace YooAsset
if (MainBundleInfo.LoadMode == BundleInfo.ELoadMode.LoadFromCache)
{
string cacheLoadPath = MainBundleInfo.GetCacheLoadPath();
if (DownloadSystem.CheckContentIntegrity(EVerifyLevel.High, cacheLoadPath, MainBundleInfo.FileSize, MainBundleInfo.FileCRC) == false)
if (CacheSystem.CheckContentIntegrity(EVerifyLevel.High, cacheLoadPath, MainBundleInfo.FileSize, MainBundleInfo.FileCRC) == false)
{
if (File.Exists(cacheLoadPath))
{

View File

@ -203,7 +203,7 @@ namespace YooAsset
}
else if (_bundleInfo.LoadMode == BundleInfo.ELoadMode.LoadFromStreaming)
{
if (DownloadSystem.ContainsVerifyFile(_bundleInfo.FileHash))
if (CacheSystem.ContainsVerifyFile(_bundleInfo.FileHash))
_steps = ESteps.CheckAndCopyFile;
else
_steps = ESteps.DownloadFromApk;
@ -238,9 +238,9 @@ namespace YooAsset
}
else
{
if (DownloadSystem.CheckContentIntegrity(GetCachePath(), _bundleInfo.FileSize, _bundleInfo.FileCRC))
if (CacheSystem.CheckContentIntegrity(GetCachePath(), _bundleInfo.FileSize, _bundleInfo.FileCRC))
{
DownloadSystem.CacheVerifyFile(_bundleInfo.FileHash, _bundleInfo.FileName);
CacheSystem.CacheVerifyFile(_bundleInfo.FileHash, _bundleInfo.FileName);
_steps = ESteps.CheckAndCopyFile;
}
else
@ -267,7 +267,7 @@ namespace YooAsset
// 如果原生文件已经存在,则验证其完整性
if (File.Exists(CopyPath))
{
bool result = DownloadSystem.CheckContentIntegrity(CopyPath, _bundleInfo.FileSize, _bundleInfo.FileCRC);
bool result = CacheSystem.CheckContentIntegrity(CopyPath, _bundleInfo.FileSize, _bundleInfo.FileCRC);
if (result)
{
_steps = ESteps.Done;
@ -355,7 +355,7 @@ namespace YooAsset
}
else if (_bundleInfo.LoadMode == BundleInfo.ELoadMode.LoadFromStreaming)
{
if (DownloadSystem.ContainsVerifyFile(_bundleInfo.FileHash))
if (CacheSystem.ContainsVerifyFile(_bundleInfo.FileHash))
_steps = ESteps.CheckAndCopyFile;
else
_steps = ESteps.DownloadFromApk;
@ -421,9 +421,9 @@ namespace YooAsset
}
else
{
if (DownloadSystem.CheckContentIntegrity(GetCachePath(), _bundleInfo.FileSize, _bundleInfo.FileCRC))
if (CacheSystem.CheckContentIntegrity(GetCachePath(), _bundleInfo.FileSize, _bundleInfo.FileCRC))
{
DownloadSystem.CacheVerifyFile(_bundleInfo.FileHash, _bundleInfo.FileName);
CacheSystem.CacheVerifyFile(_bundleInfo.FileHash, _bundleInfo.FileName);
_steps = ESteps.CheckAndCopyFile;
}
else
@ -450,7 +450,7 @@ namespace YooAsset
// 如果原生文件已经存在,则验证其完整性
if (File.Exists(CopyPath))
{
bool result = DownloadSystem.CheckContentIntegrity(CopyPath, _bundleInfo.FileSize, _bundleInfo.FileCRC);
bool result = CacheSystem.CheckContentIntegrity(CopyPath, _bundleInfo.FileSize, _bundleInfo.FileCRC);
if (result)
{
_steps = ESteps.Done;

View File

@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: 96a75a20111d6124696665e7aac3564c
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -0,0 +1,133 @@
using System;
using System.IO;
using System.Collections;
using System.Collections.Generic;
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;
public static void Initialize(EVerifyLevel verifyLevel)
{
_verifyLevel = verifyLevel;
}
public static void DestroyAll()
{
_cacheBundles.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(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;
}
}
}
}

View File

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

View File

@ -48,7 +48,7 @@ namespace YooAsset
foreach (var patchBundle in localPatchManifest.BundleList)
{
// 忽略缓存文件
if (DownloadSystem.ContainsVerifyFile(patchBundle.FileHash))
if (CacheSystem.ContainsVerifyFile(patchBundle.FileHash))
continue;
// 忽略APP资源
@ -131,7 +131,7 @@ namespace YooAsset
private void VerifyInThread(object infoObj)
{
ThreadInfo info = (ThreadInfo)infoObj;
info.Result = DownloadSystem.CheckContentIntegrity(info.FilePath, info.Bundle.FileSize, info.Bundle.FileCRC);
info.Result = CacheSystem.CheckContentIntegrity(info.FilePath, info.Bundle.FileSize, info.Bundle.FileCRC);
_syncContext.Post(VerifyCallback, info);
}
private void VerifyCallback(object obj)
@ -140,7 +140,7 @@ namespace YooAsset
if (info.Result)
{
VerifySuccessCount++;
DownloadSystem.CacheVerifyFile(info.Bundle.FileHash, info.Bundle.FileName);
CacheSystem.CacheVerifyFile(info.Bundle.FileHash, info.Bundle.FileName);
}
else
{
@ -173,7 +173,7 @@ namespace YooAsset
foreach (var patchBundle in localPatchManifest.BundleList)
{
// 忽略缓存文件
if (DownloadSystem.ContainsVerifyFile(patchBundle.FileHash))
if (CacheSystem.ContainsVerifyFile(patchBundle.FileHash))
continue;
// 忽略APP资源
@ -235,11 +235,11 @@ namespace YooAsset
private void VerifyFile(PatchBundle patchBundle)
{
string filePath = SandboxHelper.MakeCacheFilePath(patchBundle.FileName);
bool result = DownloadSystem.CheckContentIntegrity(filePath, patchBundle.FileSize, patchBundle.FileCRC);
bool result = CacheSystem.CheckContentIntegrity(filePath, patchBundle.FileSize, patchBundle.FileCRC);
if (result)
{
VerifySuccessCount++;
DownloadSystem.CacheVerifyFile(patchBundle.FileHash, patchBundle.FileName);
CacheSystem.CacheVerifyFile(patchBundle.FileHash, patchBundle.FileName);
}
else
{

View File

@ -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;
}
}
}
}

View File

@ -81,7 +81,7 @@ namespace YooAsset
if (hasError == false)
{
// 注意:如果文件验证失败需要删除文件
if (DownloadSystem.CheckContentIntegrity(_bundleInfo.GetCacheLoadPath(), _bundleInfo.FileSize, _bundleInfo.FileCRC) == false)
if (CacheSystem.CheckContentIntegrity(_bundleInfo.GetCacheLoadPath(), _bundleInfo.FileSize, _bundleInfo.FileCRC) == false)
{
hasError = true;
_lastError = $"Verification failed";
@ -91,7 +91,7 @@ namespace YooAsset
if (hasError == false)
{
_steps = ESteps.Succeed;
DownloadSystem.CacheVerifyFile(_bundleInfo.FileHash, _bundleInfo.FileName);
CacheSystem.CacheVerifyFile(_bundleInfo.FileHash, _bundleInfo.FileName);
}
else
{

View File

@ -156,7 +156,7 @@ namespace YooAsset
// 验证下载文件完整性
if (DownloadedBytes == (ulong)_fileSize)
{
bool verfiyResult = DownloadSystem.CheckContentIntegrity(_savePath, _fileSize, _fileCRC);
bool verfiyResult = CacheSystem.CheckContentIntegrity(_savePath, _fileSize, _fileCRC);
if (verfiyResult == false)
{
Error = $"Verify download content failed : {_fileName}";
@ -226,7 +226,7 @@ namespace YooAsset
}
else
{
DownloadSystem.CacheVerifyFile(_bundleInfo.FileHash, _bundleInfo.FileName);
CacheSystem.CacheVerifyFile(_bundleInfo.FileHash, _bundleInfo.FileName);
_steps = ESteps.Succeed;
}
}

View File

@ -12,7 +12,7 @@ namespace YooAsset
LoadFromEditor,
}
private readonly PatchBundle _patchBundle;
public readonly PatchBundle LoadBundle;
public readonly ELoadMode LoadMode;
private string _streamingPath;
@ -50,10 +50,10 @@ namespace YooAsset
{
get
{
if (_patchBundle == null)
if (LoadBundle == null)
return string.Empty;
else
return _patchBundle.FileHash;
return LoadBundle.FileHash;
}
}
@ -64,10 +64,10 @@ namespace YooAsset
{
get
{
if (_patchBundle == null)
if (LoadBundle == null)
return string.Empty;
else
return _patchBundle.FileCRC;
return LoadBundle.FileCRC;
}
}
@ -78,10 +78,10 @@ namespace YooAsset
{
get
{
if (_patchBundle == null)
if (LoadBundle == null)
return 0;
else
return _patchBundle.FileSize;
return LoadBundle.FileSize;
}
}
@ -92,10 +92,10 @@ namespace YooAsset
{
get
{
if (_patchBundle == null)
if (LoadBundle == null)
return false;
else
return _patchBundle.IsEncrypted;
return LoadBundle.IsEncrypted;
}
}
@ -106,10 +106,10 @@ namespace YooAsset
{
get
{
if (_patchBundle == null)
if (LoadBundle == null)
return false;
else
return _patchBundle.IsRawFile;
return LoadBundle.IsRawFile;
}
}
@ -120,7 +120,7 @@ namespace YooAsset
{
get
{
return _patchBundle == null;
return LoadBundle == null;
}
}
@ -130,7 +130,7 @@ namespace YooAsset
}
public BundleInfo(PatchBundle patchBundle, ELoadMode loadMode, string mainURL, string fallbackURL)
{
_patchBundle = patchBundle;
LoadBundle = patchBundle;
LoadMode = loadMode;
BundleName = patchBundle.BundleName;
FileName = patchBundle.FileName;
@ -140,7 +140,7 @@ namespace YooAsset
}
public BundleInfo(PatchBundle patchBundle, ELoadMode loadMode, string editorAssetPath)
{
_patchBundle = patchBundle;
LoadBundle = patchBundle;
LoadMode = loadMode;
BundleName = patchBundle.BundleName;
FileName = patchBundle.FileName;
@ -150,7 +150,7 @@ namespace YooAsset
}
public BundleInfo(PatchBundle patchBundle, ELoadMode loadMode)
{
_patchBundle = patchBundle;
LoadBundle = patchBundle;
LoadMode = loadMode;
BundleName = patchBundle.BundleName;
FileName = patchBundle.FileName;
@ -164,11 +164,11 @@ namespace YooAsset
/// </summary>
public string GetStreamingLoadPath()
{
if (_patchBundle == null)
if (LoadBundle == null)
return string.Empty;
if (string.IsNullOrEmpty(_streamingPath))
_streamingPath = PathHelper.MakeStreamingLoadPath(_patchBundle.FileName);
_streamingPath = PathHelper.MakeStreamingLoadPath(LoadBundle.FileName);
return _streamingPath;
}
@ -177,11 +177,11 @@ namespace YooAsset
/// </summary>
public string GetCacheLoadPath()
{
if (_patchBundle == null)
if (LoadBundle == null)
return string.Empty;
if (string.IsNullOrEmpty(_cachePath))
_cachePath = SandboxHelper.MakeCacheFilePath(_patchBundle.FileName);
_cachePath = SandboxHelper.MakeCacheFilePath(LoadBundle.FileName);
return _cachePath;
}

View File

@ -198,7 +198,7 @@ namespace YooAsset
foreach (var patchBundle in _remotePatchManifest.BundleList)
{
// 忽略缓存文件
if (DownloadSystem.ContainsVerifyFile(patchBundle.FileHash))
if (CacheSystem.ContainsVerifyFile(patchBundle.FileHash))
continue;
// 忽略APP资源

View File

@ -131,7 +131,7 @@ namespace YooAsset
foreach (var patchBundle in LocalPatchManifest.BundleList)
{
// 忽略缓存文件
if (DownloadSystem.ContainsVerifyFile(patchBundle.FileHash))
if (CacheSystem.ContainsVerifyFile(patchBundle.FileHash))
continue;
// 忽略APP资源
@ -163,7 +163,7 @@ namespace YooAsset
foreach (var patchBundle in LocalPatchManifest.BundleList)
{
// 忽略缓存文件
if (DownloadSystem.ContainsVerifyFile(patchBundle.FileHash))
if (CacheSystem.ContainsVerifyFile(patchBundle.FileHash))
continue;
// 忽略APP资源
@ -233,7 +233,7 @@ namespace YooAsset
foreach (var patchBundle in checkList)
{
// 忽略缓存文件
if (DownloadSystem.ContainsVerifyFile(patchBundle.FileHash))
if (CacheSystem.ContainsVerifyFile(patchBundle.FileHash))
continue;
// 忽略APP资源
@ -269,7 +269,7 @@ namespace YooAsset
continue;
// 忽略缓存文件
if (DownloadSystem.ContainsVerifyFile(patchBundle.FileHash))
if (CacheSystem.ContainsVerifyFile(patchBundle.FileHash))
continue;
// 查询DLC资源
@ -301,7 +301,7 @@ namespace YooAsset
continue;
// 忽略缓存文件
if (DownloadSystem.ContainsVerifyFile(patchBundle.FileHash))
if (CacheSystem.ContainsVerifyFile(patchBundle.FileHash))
continue;
downloadList.Add(patchBundle);
@ -377,7 +377,7 @@ namespace YooAsset
throw new Exception("Should never get here !");
// 查询沙盒资源
if (DownloadSystem.ContainsVerifyFile(patchBundle.FileHash))
if (CacheSystem.ContainsVerifyFile(patchBundle.FileHash))
{
BundleInfo bundleInfo = new BundleInfo(patchBundle, BundleInfo.ELoadMode.LoadFromCache);
return bundleInfo;

View File

@ -202,7 +202,8 @@ namespace YooAsset
if (_playMode == EPlayMode.HostPlayMode)
{
var hostPlayModeParameters = parameters as HostPlayModeParameters;
DownloadSystem.Initialize(hostPlayModeParameters.BreakpointResumeFileSize, hostPlayModeParameters.VerifyLevel);
CacheSystem.Initialize(hostPlayModeParameters.VerifyLevel);
DownloadSystem.Initialize(hostPlayModeParameters.BreakpointResumeFileSize);
}
// 初始化资源系统
@ -1029,6 +1030,7 @@ namespace YooAsset
OperationSystem.DestroyAll();
DownloadSystem.DestroyAll();
CacheSystem.DestroyAll();
AssetSystem.DestroyAll();
YooLogger.Log("YooAssets destroy all !");
}