mirror of
https://github.com/tuyoogame/YooAsset.git
synced 2026-05-22 00:11:41 +00:00
update new cache system
重新设计了资源缓存系统
This commit is contained in:
@@ -2,19 +2,19 @@
|
||||
using System.IO;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
|
||||
namespace YooAsset
|
||||
{
|
||||
internal static class CacheSystem
|
||||
{
|
||||
private readonly static Dictionary<string, PatchBundle> _cachedDic = new Dictionary<string, PatchBundle>(1000);
|
||||
private readonly static Dictionary<string, PackageCache> _cachedDic = new Dictionary<string, PackageCache>(1000);
|
||||
|
||||
/// <summary>
|
||||
/// 初始化时的验证级别
|
||||
/// </summary>
|
||||
public static EVerifyLevel InitVerifyLevel { set; get; } = EVerifyLevel.Low;
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 清空所有数据
|
||||
/// </summary>
|
||||
@@ -25,103 +25,119 @@ namespace YooAsset
|
||||
|
||||
/// <summary>
|
||||
/// 查询是否为验证文件
|
||||
/// 注意:被收录的文件完整性是绝对有效的
|
||||
/// </summary>
|
||||
public static bool IsCached(PatchBundle patchBundle)
|
||||
public static bool IsCached(string packageName, string cacheGUID)
|
||||
{
|
||||
string cacheKey = patchBundle.CacheKey;
|
||||
if (_cachedDic.ContainsKey(cacheKey))
|
||||
var cache = GetOrCreateCache(packageName);
|
||||
return cache.IsCached(cacheGUID);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 录入验证的文件
|
||||
/// </summary>
|
||||
public static void RecordFile(string packageName, string cacheGUID, PackageCache.RecordWrapper wrapper)
|
||||
{
|
||||
var cache = GetOrCreateCache(packageName);
|
||||
cache.Record(cacheGUID, wrapper);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 丢弃验证的文件(同时删除文件)
|
||||
/// </summary>
|
||||
public static void DiscardFile(string packageName, string cacheGUID)
|
||||
{
|
||||
var cache = GetOrCreateCache(packageName);
|
||||
var wrapper = cache.TryGetWrapper(cacheGUID);
|
||||
if (wrapper == null)
|
||||
return;
|
||||
|
||||
cache.Discard(cacheGUID);
|
||||
|
||||
try
|
||||
{
|
||||
string filePath = patchBundle.CachedFilePath;
|
||||
if (File.Exists(filePath))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
else
|
||||
{
|
||||
_cachedDic.Remove(cacheKey);
|
||||
YooLogger.Error($"Cache file is missing : {filePath}");
|
||||
return false;
|
||||
}
|
||||
string dataFilePath = wrapper.DataFilePath;
|
||||
FileInfo fileInfo = new FileInfo(dataFilePath);
|
||||
if (fileInfo.Exists)
|
||||
fileInfo.Directory.Delete(true);
|
||||
}
|
||||
else
|
||||
catch (Exception e)
|
||||
{
|
||||
return false;
|
||||
YooLogger.Error($"Failed to delete cache file ! {e.Message}");
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 缓存补丁包文件
|
||||
/// 验证缓存文件(子线程内操作)
|
||||
/// </summary>
|
||||
public static void CacheBundle(PatchBundle patchBundle)
|
||||
public static EVerifyResult VerifyingCacheFile(VerifyElement element, EVerifyLevel verifyLevel)
|
||||
{
|
||||
string cacheKey = patchBundle.CacheKey;
|
||||
if (_cachedDic.ContainsKey(cacheKey) == false)
|
||||
try
|
||||
{
|
||||
string filePath = patchBundle.CachedFilePath;
|
||||
YooLogger.Log($"Cache verify file : {filePath}");
|
||||
_cachedDic.Add(cacheKey, patchBundle);
|
||||
string infoFilePath = element.InfoFilePath;
|
||||
if (File.Exists(infoFilePath) == false)
|
||||
return EVerifyResult.InfoFileNotExisted;
|
||||
|
||||
// 解析信息文件获取验证数据
|
||||
string jsonContent = FileUtility.ReadAllText(infoFilePath);
|
||||
CacheFileInfo fileInfo = UnityEngine.JsonUtility.FromJson<CacheFileInfo>(jsonContent);
|
||||
element.DataFileCRC = fileInfo.FileCRC;
|
||||
element.DataFileSize = fileInfo.FileSize;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 验证补丁包文件
|
||||
/// </summary>
|
||||
public static EVerifyResult VerifyBundle(PatchBundle patchBundle, EVerifyLevel verifyLevel)
|
||||
{
|
||||
return VerifyContentInternal(patchBundle.CachedFilePath, patchBundle.FileSize, patchBundle.FileCRC, verifyLevel);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 验证并缓存本地文件
|
||||
/// </summary>
|
||||
public static EVerifyResult VerifyAndCacheLocalBundleFile(PatchBundle patchBundle, EVerifyLevel verifyLevel)
|
||||
{
|
||||
var verifyResult = VerifyContentInternal(patchBundle.CachedFilePath, patchBundle.FileSize, patchBundle.FileCRC, verifyLevel);
|
||||
if (verifyResult == EVerifyResult.Succeed)
|
||||
CacheBundle(patchBundle);
|
||||
return verifyResult;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 验证并缓存下载文件
|
||||
/// </summary>
|
||||
public static EVerifyResult VerifyAndCacheDownloadBundleFile(string tempFilePath, PatchBundle patchBundle, EVerifyLevel verifyLevel)
|
||||
{
|
||||
var verifyResult = VerifyContentInternal(tempFilePath, patchBundle.FileSize, patchBundle.FileCRC, verifyLevel);
|
||||
if (verifyResult == EVerifyResult.Succeed)
|
||||
catch (Exception)
|
||||
{
|
||||
try
|
||||
{
|
||||
string destFilePath = patchBundle.CachedFilePath;
|
||||
if (File.Exists(destFilePath))
|
||||
File.Delete(destFilePath);
|
||||
return EVerifyResult.Exception;
|
||||
}
|
||||
|
||||
FileInfo fileInfo = new FileInfo(tempFilePath);
|
||||
fileInfo.MoveTo(destFilePath);
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
verifyResult = EVerifyResult.FileMoveFailed;
|
||||
}
|
||||
return VerifyingInternal(element.DataFilePath, element.DataFileSize, element.DataFileCRC, verifyLevel);
|
||||
}
|
||||
|
||||
if (verifyResult == EVerifyResult.Succeed)
|
||||
/// <summary>
|
||||
/// 验证下载文件
|
||||
/// </summary>
|
||||
public static EVerifyResult VerifyingTempFile(PatchBundle patchBundle, EVerifyLevel verifyLevel)
|
||||
{
|
||||
return VerifyingInternal(patchBundle.TempDataFilePath, patchBundle.FileSize, patchBundle.FileCRC, verifyLevel);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 验证记录文件
|
||||
/// </summary>
|
||||
public static EVerifyResult VerifyingRecordFile(string packageName, string cacheGUID)
|
||||
{
|
||||
var cache = GetOrCreateCache(packageName);
|
||||
var wrapper = cache.TryGetWrapper(cacheGUID);
|
||||
if (wrapper == null)
|
||||
return EVerifyResult.CacheNotFound;
|
||||
|
||||
EVerifyResult result = VerifyingInternal(wrapper.DataFilePath, wrapper.DataFileSize, wrapper.DataFileCRC, EVerifyLevel.High);
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取未被使用的缓存文件
|
||||
/// </summary>
|
||||
public static List<string> GetUnusedCacheGUIDs(AssetsPackage package)
|
||||
{
|
||||
var cache = GetOrCreateCache(package.PackageName);
|
||||
var keys = cache.GetAllKeys();
|
||||
List<string> result = new List<string>(keys.Count);
|
||||
foreach (var cacheGUID in keys)
|
||||
{
|
||||
if (package.IsIncludeBundleFile(cacheGUID) == false)
|
||||
{
|
||||
CacheBundle(patchBundle);
|
||||
result.Add(cacheGUID);
|
||||
}
|
||||
}
|
||||
return verifyResult;
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 验证文件完整性
|
||||
/// </summary>
|
||||
private static EVerifyResult VerifyContentInternal(string filePath, long fileSize, string fileCRC, EVerifyLevel verifyLevel)
|
||||
|
||||
private static EVerifyResult VerifyingInternal(string filePath, long fileSize, string fileCRC, EVerifyLevel verifyLevel)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (File.Exists(filePath) == false)
|
||||
return EVerifyResult.FileNotExisted;
|
||||
return EVerifyResult.DataFileNotExisted;
|
||||
|
||||
// 先验证文件大小
|
||||
long size = FileUtility.GetFileSize(filePath);
|
||||
@@ -149,5 +165,14 @@ namespace YooAsset
|
||||
return EVerifyResult.Exception;
|
||||
}
|
||||
}
|
||||
private static PackageCache GetOrCreateCache(string packageName)
|
||||
{
|
||||
if (_cachedDic.TryGetValue(packageName, out PackageCache cache) == false)
|
||||
{
|
||||
cache = new PackageCache(packageName);
|
||||
_cachedDic.Add(packageName, cache);
|
||||
}
|
||||
return cache;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user