refactor : cache system

pull/332/merge
何冠峰 2024-12-12 15:16:34 +08:00
parent b1338a9ffd
commit 9607d7135b
7 changed files with 46 additions and 23 deletions

View File

@ -0,0 +1,25 @@
using System;
using System.IO;
namespace YooAsset
{
internal class CacheHelper
{
/// <summary>
/// 获取默认的缓存根目录
/// </summary>
public static string GetDefaultCacheRoot()
{
#if UNITY_EDITOR
// 注意:为了方便调试查看,编辑器下把存储目录放到项目里。
string projectPath = Path.GetDirectoryName(UnityEngine.Application.dataPath);
projectPath = PathUtility.RegularPath(projectPath);
return PathUtility.Combine(projectPath, YooAssetSettingsData.Setting.DefaultYooFolderName);
#elif UNITY_STANDALONE
return PathUtility.Combine(UnityEngine.Application.dataPath, YooAssetSettingsData.Setting.DefaultYooFolderName);
#else
return PathUtility.Combine(UnityEngine.Application.persistentDataPath, YooAssetSettingsData.Setting.DefaultYooFolderName);
#endif
}
}
}

View File

@ -3,7 +3,7 @@ using System.IO;
namespace YooAsset namespace YooAsset
{ {
internal class FileSystemHelper internal class FileVerifyHelper
{ {
/// <summary> /// <summary>
/// 文件校验 /// 文件校验

View File

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

View File

@ -165,7 +165,7 @@ namespace YooAsset
return EFileVerifyResult.Exception; return EFileVerifyResult.Exception;
} }
return FileSystemHelper.FileVerify(element.DataFilePath, element.DataFileSize, element.DataFileCRC, verifyLevel); return FileVerifyHelper.FileVerify(element.DataFilePath, element.DataFileSize, element.DataFileCRC, verifyLevel);
} }
} }
} }

View File

@ -84,7 +84,7 @@ namespace YooAsset
private void VerifyInThread(object obj) private void VerifyInThread(object obj)
{ {
TempFileElement element = (TempFileElement)obj; TempFileElement element = (TempFileElement)obj;
int result = (int)FileSystemHelper.FileVerify(element.TempFilePath, element.TempFileSize, element.TempFileCRC, EFileVerifyLevel.High); int result = (int)FileVerifyHelper.FileVerify(element.TempFilePath, element.TempFileSize, element.TempFileCRC, EFileVerifyLevel.High);
element.Result = result; element.Result = result;
} }
} }

View File

@ -20,7 +20,7 @@ namespace YooAsset
protected readonly Dictionary<string, string> _tempFilePaths = new Dictionary<string, string>(10000); protected readonly Dictionary<string, string> _tempFilePaths = new Dictionary<string, string>(10000);
protected readonly List<string> _removeList = new List<string>(1000); protected readonly List<string> _removeList = new List<string>(1000);
protected string _packageRoot; protected string _packageRoot;
protected string _saveFileRoot; protected string _cacheFileRoot;
protected string _tempFileRoot; protected string _tempFileRoot;
protected string _manifestFileRoot; protected string _manifestFileRoot;
@ -238,10 +238,10 @@ namespace YooAsset
PackageName = packageName; PackageName = packageName;
if (string.IsNullOrEmpty(rootDirectory)) if (string.IsNullOrEmpty(rootDirectory))
rootDirectory = GetDefaultRoot(); rootDirectory = CacheHelper.GetDefaultCacheRoot();
_packageRoot = PathUtility.Combine(rootDirectory, packageName); _packageRoot = PathUtility.Combine(rootDirectory, packageName);
_saveFileRoot = PathUtility.Combine(_packageRoot, DefaultCacheFileSystemDefine.SaveFilesFolderName); _cacheFileRoot = PathUtility.Combine(_packageRoot, DefaultCacheFileSystemDefine.SaveFilesFolderName);
_tempFileRoot = PathUtility.Combine(_packageRoot, DefaultCacheFileSystemDefine.TempFilesFolderName); _tempFileRoot = PathUtility.Combine(_packageRoot, DefaultCacheFileSystemDefine.TempFilesFolderName);
_manifestFileRoot = PathUtility.Combine(_packageRoot, DefaultCacheFileSystemDefine.ManifestFilesFolderName); _manifestFileRoot = PathUtility.Combine(_packageRoot, DefaultCacheFileSystemDefine.ManifestFilesFolderName);
} }
@ -359,7 +359,7 @@ namespace YooAsset
#region 缓存系统 #region 缓存系统
public string GetCacheFileRoot() public string GetCacheFileRoot()
{ {
return _saveFileRoot; return _cacheFileRoot;
} }
public List<string> GetAllCachedBundleGUIDs() public List<string> GetAllCachedBundleGUIDs()
{ {
@ -387,7 +387,7 @@ namespace YooAsset
if (_wrappers.TryGetValue(bundle.BundleGUID, out CacheWrapper wrapper) == false) if (_wrappers.TryGetValue(bundle.BundleGUID, out CacheWrapper wrapper) == false)
return EFileVerifyResult.CacheNotFound; return EFileVerifyResult.CacheNotFound;
EFileVerifyResult result = FileSystemHelper.FileVerify(wrapper.DataFilePath, wrapper.DataFileSize, wrapper.DataFileCRC, EFileVerifyLevel.High); EFileVerifyResult result = FileVerifyHelper.FileVerify(wrapper.DataFilePath, wrapper.DataFileSize, wrapper.DataFileCRC, EFileVerifyLevel.High);
return result; return result;
} }
public bool WriteCacheFile(PackageBundle bundle, string copyPath) public bool WriteCacheFile(PackageBundle bundle, string copyPath)
@ -472,25 +472,12 @@ namespace YooAsset
#endregion #endregion
#region 内部方法 #region 内部方法
protected string GetDefaultRoot()
{
#if UNITY_EDITOR
// 注意:为了方便调试查看,编辑器下把存储目录放到项目里。
string projectPath = Path.GetDirectoryName(UnityEngine.Application.dataPath);
projectPath = PathUtility.RegularPath(projectPath);
return PathUtility.Combine(projectPath, YooAssetSettingsData.Setting.DefaultYooFolderName);
#elif UNITY_STANDALONE
return PathUtility.Combine(UnityEngine.Application.dataPath, YooAssetSettingsData.Setting.DefaultYooFolderName);
#else
return PathUtility.Combine(UnityEngine.Application.persistentDataPath, YooAssetSettingsData.Setting.DefaultYooFolderName);
#endif
}
protected string GetDataFilePath(PackageBundle bundle) protected string GetDataFilePath(PackageBundle bundle)
{ {
if (_dataFilePaths.TryGetValue(bundle.BundleGUID, out string filePath) == false) if (_dataFilePaths.TryGetValue(bundle.BundleGUID, out string filePath) == false)
{ {
string folderName = bundle.FileHash.Substring(0, 2); string folderName = bundle.FileHash.Substring(0, 2);
filePath = PathUtility.Combine(_saveFileRoot, folderName, bundle.BundleGUID, DefaultCacheFileSystemDefine.SaveBundleDataFileName); filePath = PathUtility.Combine(_cacheFileRoot, folderName, bundle.BundleGUID, DefaultCacheFileSystemDefine.SaveBundleDataFileName);
if (AppendFileExtension) if (AppendFileExtension)
filePath += bundle.FileExtension; filePath += bundle.FileExtension;
_dataFilePaths.Add(bundle.BundleGUID, filePath); _dataFilePaths.Add(bundle.BundleGUID, filePath);
@ -502,7 +489,7 @@ namespace YooAsset
if (_infoFilePaths.TryGetValue(bundle.BundleGUID, out string filePath) == false) if (_infoFilePaths.TryGetValue(bundle.BundleGUID, out string filePath) == false)
{ {
string folderName = bundle.FileHash.Substring(0, 2); string folderName = bundle.FileHash.Substring(0, 2);
filePath = PathUtility.Combine(_saveFileRoot, folderName, bundle.BundleGUID, DefaultCacheFileSystemDefine.SaveBundleInfoFileName); filePath = PathUtility.Combine(_cacheFileRoot, folderName, bundle.BundleGUID, DefaultCacheFileSystemDefine.SaveBundleInfoFileName);
_infoFilePaths.Add(bundle.BundleGUID, filePath); _infoFilePaths.Add(bundle.BundleGUID, filePath);
} }
return filePath; return filePath;