From b1338a9ffda5dea80b882dd13e454e467c03753a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=BD=95=E5=86=A0=E5=B3=B0?= Date: Thu, 12 Dec 2024 15:00:08 +0800 Subject: [PATCH] refactor : cache file system --- .../Runtime/FileSystem/CacheSystem.meta | 8 + .../CacheSystem/CacheFileElement.cs | 38 +++ .../CacheSystem/CacheFileElement.cs.meta | 11 + .../FileSystem/CacheSystem/CacheWrapper.cs | 19 ++ .../CacheSystem/CacheWrapper.cs.meta | 11 + .../FileSystem/CacheSystem/Interface.meta | 8 + .../CacheSystem/Interface/ICacheSystem.cs | 52 ++++ .../Interface/ICacheSystem.cs.meta | 11 + .../FileSystem/CacheSystem/Operation.meta | 8 + .../Operation/ClearAllCacheFilesOperation.cs} | 12 +- .../ClearAllCacheFilesOperation.cs.meta} | 0 .../ClearUnusedCacheFilesOperation.cs} | 14 +- .../ClearUnusedCacheFilesOperation.cs.meta} | 0 .../Operation}/SearchCacheFilesOperation.cs | 18 +- .../SearchCacheFilesOperation.cs.meta | 0 .../Operation}/VerifyCacheFilesOperation.cs | 52 +--- .../VerifyCacheFilesOperation.cs.meta | 0 .../Operation}/VerifyTempFileOperation.cs | 23 +- .../VerifyTempFileOperation.cs.meta | 0 .../FileSystem/CacheSystem/TempFileElement.cs | 22 ++ .../CacheSystem/TempFileElement.cs.meta | 11 + .../DefaultCacheFileSystem.cs | 235 ++++++++---------- .../Operation/DCFSInitializeOperation.cs | 4 +- 23 files changed, 336 insertions(+), 221 deletions(-) create mode 100644 Assets/YooAsset/Runtime/FileSystem/CacheSystem.meta create mode 100644 Assets/YooAsset/Runtime/FileSystem/CacheSystem/CacheFileElement.cs create mode 100644 Assets/YooAsset/Runtime/FileSystem/CacheSystem/CacheFileElement.cs.meta create mode 100644 Assets/YooAsset/Runtime/FileSystem/CacheSystem/CacheWrapper.cs create mode 100644 Assets/YooAsset/Runtime/FileSystem/CacheSystem/CacheWrapper.cs.meta create mode 100644 Assets/YooAsset/Runtime/FileSystem/CacheSystem/Interface.meta create mode 100644 Assets/YooAsset/Runtime/FileSystem/CacheSystem/Interface/ICacheSystem.cs create mode 100644 Assets/YooAsset/Runtime/FileSystem/CacheSystem/Interface/ICacheSystem.cs.meta create mode 100644 Assets/YooAsset/Runtime/FileSystem/CacheSystem/Operation.meta rename Assets/YooAsset/Runtime/FileSystem/{DefaultCacheFileSystem/Operation/DCFSClearAllBundleFilesOperation.cs => CacheSystem/Operation/ClearAllCacheFilesOperation.cs} (80%) rename Assets/YooAsset/Runtime/FileSystem/{DefaultCacheFileSystem/Operation/DCFSClearAllBundleFilesOperation.cs.meta => CacheSystem/Operation/ClearAllCacheFilesOperation.cs.meta} (100%) rename Assets/YooAsset/Runtime/FileSystem/{DefaultCacheFileSystem/Operation/DCFSClearUnusedBundleFilesOperation.cs => CacheSystem/Operation/ClearUnusedCacheFilesOperation.cs} (83%) rename Assets/YooAsset/Runtime/FileSystem/{DefaultCacheFileSystem/Operation/DCFSClearUnusedBundleFilesOperation.cs.meta => CacheSystem/Operation/ClearUnusedCacheFilesOperation.cs.meta} (100%) rename Assets/YooAsset/Runtime/FileSystem/{DefaultCacheFileSystem/Operation/internal => CacheSystem/Operation}/SearchCacheFilesOperation.cs (85%) rename Assets/YooAsset/Runtime/FileSystem/{DefaultCacheFileSystem/Operation/internal => CacheSystem/Operation}/SearchCacheFilesOperation.cs.meta (100%) rename Assets/YooAsset/Runtime/FileSystem/{DefaultCacheFileSystem/Operation/internal => CacheSystem/Operation}/VerifyCacheFilesOperation.cs (74%) rename Assets/YooAsset/Runtime/FileSystem/{DefaultCacheFileSystem/Operation/internal => CacheSystem/Operation}/VerifyCacheFilesOperation.cs.meta (100%) rename Assets/YooAsset/Runtime/FileSystem/{DefaultCacheFileSystem/Operation/internal => CacheSystem/Operation}/VerifyTempFileOperation.cs (78%) rename Assets/YooAsset/Runtime/FileSystem/{DefaultCacheFileSystem/Operation/internal => CacheSystem/Operation}/VerifyTempFileOperation.cs.meta (100%) create mode 100644 Assets/YooAsset/Runtime/FileSystem/CacheSystem/TempFileElement.cs create mode 100644 Assets/YooAsset/Runtime/FileSystem/CacheSystem/TempFileElement.cs.meta diff --git a/Assets/YooAsset/Runtime/FileSystem/CacheSystem.meta b/Assets/YooAsset/Runtime/FileSystem/CacheSystem.meta new file mode 100644 index 0000000..de5fe42 --- /dev/null +++ b/Assets/YooAsset/Runtime/FileSystem/CacheSystem.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: da6402a4b93d31943b26fb99cebc0dfd +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/YooAsset/Runtime/FileSystem/CacheSystem/CacheFileElement.cs b/Assets/YooAsset/Runtime/FileSystem/CacheSystem/CacheFileElement.cs new file mode 100644 index 0000000..0b4d57c --- /dev/null +++ b/Assets/YooAsset/Runtime/FileSystem/CacheSystem/CacheFileElement.cs @@ -0,0 +1,38 @@ +using System.IO; + +namespace YooAsset +{ + internal class CacheFileElement + { + public string PackageName { private set; get; } + public string BundleGUID { private set; get; } + public string FileRootPath { private set; get; } + public string DataFilePath { private set; get; } + public string InfoFilePath { private set; get; } + + public EFileVerifyResult Result; + public string DataFileCRC; + public long DataFileSize; + + public CacheFileElement(string packageName, string bundleGUID, string fileRootPath, string dataFilePath, string infoFilePath) + { + PackageName = packageName; + BundleGUID = bundleGUID; + FileRootPath = fileRootPath; + DataFilePath = dataFilePath; + InfoFilePath = infoFilePath; + } + + public void DeleteFiles() + { + try + { + Directory.Delete(FileRootPath, true); + } + catch (System.Exception e) + { + YooLogger.Warning($"Failed to delete cache bundle folder : {e}"); + } + } + } +} \ No newline at end of file diff --git a/Assets/YooAsset/Runtime/FileSystem/CacheSystem/CacheFileElement.cs.meta b/Assets/YooAsset/Runtime/FileSystem/CacheSystem/CacheFileElement.cs.meta new file mode 100644 index 0000000..bcfdde2 --- /dev/null +++ b/Assets/YooAsset/Runtime/FileSystem/CacheSystem/CacheFileElement.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 0c361449cdcbd8746ba3fb948798ae1b +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/YooAsset/Runtime/FileSystem/CacheSystem/CacheWrapper.cs b/Assets/YooAsset/Runtime/FileSystem/CacheSystem/CacheWrapper.cs new file mode 100644 index 0000000..7660813 --- /dev/null +++ b/Assets/YooAsset/Runtime/FileSystem/CacheSystem/CacheWrapper.cs @@ -0,0 +1,19 @@ + +namespace YooAsset +{ + internal class CacheWrapper + { + public string InfoFilePath { private set; get; } + public string DataFilePath { private set; get; } + public string DataFileCRC { private set; get; } + public long DataFileSize { private set; get; } + + public CacheWrapper(string infoFilePath, string dataFilePath, string dataFileCRC, long dataFileSize) + { + InfoFilePath = infoFilePath; + DataFilePath = dataFilePath; + DataFileCRC = dataFileCRC; + DataFileSize = dataFileSize; + } + } +} \ No newline at end of file diff --git a/Assets/YooAsset/Runtime/FileSystem/CacheSystem/CacheWrapper.cs.meta b/Assets/YooAsset/Runtime/FileSystem/CacheSystem/CacheWrapper.cs.meta new file mode 100644 index 0000000..47d9b68 --- /dev/null +++ b/Assets/YooAsset/Runtime/FileSystem/CacheSystem/CacheWrapper.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: ae2176c4ea6fbb3478bf8757def34cd7 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/YooAsset/Runtime/FileSystem/CacheSystem/Interface.meta b/Assets/YooAsset/Runtime/FileSystem/CacheSystem/Interface.meta new file mode 100644 index 0000000..4b0f190 --- /dev/null +++ b/Assets/YooAsset/Runtime/FileSystem/CacheSystem/Interface.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: ce78de82d3ce1a841a1e6bad099e0bab +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/YooAsset/Runtime/FileSystem/CacheSystem/Interface/ICacheSystem.cs b/Assets/YooAsset/Runtime/FileSystem/CacheSystem/Interface/ICacheSystem.cs new file mode 100644 index 0000000..d5e6489 --- /dev/null +++ b/Assets/YooAsset/Runtime/FileSystem/CacheSystem/Interface/ICacheSystem.cs @@ -0,0 +1,52 @@ +using System.Collections.Generic; + +namespace YooAsset +{ + internal interface ICacheSystem + { + /// + /// 获取缓存文件的根目录 + /// + string GetCacheFileRoot(); + + /// + /// 获取所有缓存文件的GUID + /// + List GetAllCachedBundleGUIDs(); + + /// + /// 是否记录了文件 + /// + bool IsRecordFile(string bundleGUID); + + /// + /// 记录指定文件 + /// + bool RecordFile(string bundleGUID, CacheWrapper wrapper); + + /// + /// 验证缓存文件 + /// + EFileVerifyResult VerifyCacheFile(PackageBundle bundle); + + /// + /// 写入缓存文件 + /// + bool WriteCacheFile(PackageBundle bundle, string copyPath); + + /// + /// 删除缓存文件 + /// + bool DeleteCacheFile(string bundleGUID); + + /// + /// 写入文件信息 + /// + void WriteInfoFile(string filePath, string dataFileCRC, long dataFileSize); + + /// + /// 读取文件信息 + /// + void ReadInfoFile(string filePath, out string dataFileCRC, out long dataFileSize); + } +} \ No newline at end of file diff --git a/Assets/YooAsset/Runtime/FileSystem/CacheSystem/Interface/ICacheSystem.cs.meta b/Assets/YooAsset/Runtime/FileSystem/CacheSystem/Interface/ICacheSystem.cs.meta new file mode 100644 index 0000000..e9a6cf3 --- /dev/null +++ b/Assets/YooAsset/Runtime/FileSystem/CacheSystem/Interface/ICacheSystem.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: f878bc9a12f5d2d40aee754ddfaada86 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/YooAsset/Runtime/FileSystem/CacheSystem/Operation.meta b/Assets/YooAsset/Runtime/FileSystem/CacheSystem/Operation.meta new file mode 100644 index 0000000..a4bf857 --- /dev/null +++ b/Assets/YooAsset/Runtime/FileSystem/CacheSystem/Operation.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: 8cae5a51fced527429445b140b8a0843 +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/YooAsset/Runtime/FileSystem/DefaultCacheFileSystem/Operation/DCFSClearAllBundleFilesOperation.cs b/Assets/YooAsset/Runtime/FileSystem/CacheSystem/Operation/ClearAllCacheFilesOperation.cs similarity index 80% rename from Assets/YooAsset/Runtime/FileSystem/DefaultCacheFileSystem/Operation/DCFSClearAllBundleFilesOperation.cs rename to Assets/YooAsset/Runtime/FileSystem/CacheSystem/Operation/ClearAllCacheFilesOperation.cs index de0f39f..24b96fe 100644 --- a/Assets/YooAsset/Runtime/FileSystem/DefaultCacheFileSystem/Operation/DCFSClearAllBundleFilesOperation.cs +++ b/Assets/YooAsset/Runtime/FileSystem/CacheSystem/Operation/ClearAllCacheFilesOperation.cs @@ -3,7 +3,7 @@ using System.Collections.Generic; namespace YooAsset { - internal sealed class DCFSClearAllBundleFilesOperation : FSClearAllBundleFilesOperation + internal sealed class ClearAllCacheFilesOperation : FSClearAllBundleFilesOperation { private enum ESteps { @@ -13,15 +13,15 @@ namespace YooAsset Done, } - private readonly DefaultCacheFileSystem _fileSystem; + private readonly ICacheSystem _cacheSystem; private List _allBundleGUIDs; private int _fileTotalCount = 0; private ESteps _steps = ESteps.None; - internal DCFSClearAllBundleFilesOperation(DefaultCacheFileSystem fileSystem) + internal ClearAllCacheFilesOperation(ICacheSystem cacheSystem) { - _fileSystem = fileSystem; + _cacheSystem = cacheSystem; } internal override void InternalOnStart() { @@ -34,7 +34,7 @@ namespace YooAsset if (_steps == ESteps.GetAllCacheFiles) { - _allBundleGUIDs = _fileSystem.GetAllCachedBundleGUIDs(); + _allBundleGUIDs = _cacheSystem.GetAllCachedBundleGUIDs(); _fileTotalCount = _allBundleGUIDs.Count; _steps = ESteps.ClearAllCacheFiles; YooLogger.Log($"Found all cache files count : {_fileTotalCount}"); @@ -45,7 +45,7 @@ namespace YooAsset for (int i = _allBundleGUIDs.Count - 1; i >= 0; i--) { string bundleGUID = _allBundleGUIDs[i]; - _fileSystem.DeleteCacheFile(bundleGUID); + _cacheSystem.DeleteCacheFile(bundleGUID); _allBundleGUIDs.RemoveAt(i); if (OperationSystem.IsBusy) break; diff --git a/Assets/YooAsset/Runtime/FileSystem/DefaultCacheFileSystem/Operation/DCFSClearAllBundleFilesOperation.cs.meta b/Assets/YooAsset/Runtime/FileSystem/CacheSystem/Operation/ClearAllCacheFilesOperation.cs.meta similarity index 100% rename from Assets/YooAsset/Runtime/FileSystem/DefaultCacheFileSystem/Operation/DCFSClearAllBundleFilesOperation.cs.meta rename to Assets/YooAsset/Runtime/FileSystem/CacheSystem/Operation/ClearAllCacheFilesOperation.cs.meta diff --git a/Assets/YooAsset/Runtime/FileSystem/DefaultCacheFileSystem/Operation/DCFSClearUnusedBundleFilesOperation.cs b/Assets/YooAsset/Runtime/FileSystem/CacheSystem/Operation/ClearUnusedCacheFilesOperation.cs similarity index 83% rename from Assets/YooAsset/Runtime/FileSystem/DefaultCacheFileSystem/Operation/DCFSClearUnusedBundleFilesOperation.cs rename to Assets/YooAsset/Runtime/FileSystem/CacheSystem/Operation/ClearUnusedCacheFilesOperation.cs index c4201c9..cf1939d 100644 --- a/Assets/YooAsset/Runtime/FileSystem/DefaultCacheFileSystem/Operation/DCFSClearUnusedBundleFilesOperation.cs +++ b/Assets/YooAsset/Runtime/FileSystem/CacheSystem/Operation/ClearUnusedCacheFilesOperation.cs @@ -3,7 +3,7 @@ using System.Collections.Generic; namespace YooAsset { - internal sealed class DCFSClearUnusedBundleFilesOperation : FSClearUnusedBundleFilesOperation + internal sealed class ClearUnusedCacheFilesOperation : FSClearUnusedBundleFilesOperation { private enum ESteps { @@ -13,16 +13,16 @@ namespace YooAsset Done, } - private readonly DefaultCacheFileSystem _fileSystem; + private readonly ICacheSystem _cacheSystem; private readonly PackageManifest _manifest; private List _unusedBundleGUIDs; private int _unusedFileTotalCount = 0; private ESteps _steps = ESteps.None; - - internal DCFSClearUnusedBundleFilesOperation(DefaultCacheFileSystem fileSystem, PackageManifest manifest) + + internal ClearUnusedCacheFilesOperation(ICacheSystem cacheSystem, PackageManifest manifest) { - _fileSystem = fileSystem; + _cacheSystem = cacheSystem; _manifest = manifest; } internal override void InternalOnStart() @@ -47,7 +47,7 @@ namespace YooAsset for (int i = _unusedBundleGUIDs.Count - 1; i >= 0; i--) { string bundleGUID = _unusedBundleGUIDs[i]; - _fileSystem.DeleteCacheFile(bundleGUID); + _cacheSystem.DeleteCacheFile(bundleGUID); _unusedBundleGUIDs.RemoveAt(i); if (OperationSystem.IsBusy) break; @@ -68,7 +68,7 @@ namespace YooAsset private List GetUnusedBundleGUIDs() { - var allBundleGUIDs = _fileSystem.GetAllCachedBundleGUIDs(); + var allBundleGUIDs = _cacheSystem.GetAllCachedBundleGUIDs(); List result = new List(allBundleGUIDs.Count); foreach (var bundleGUID in allBundleGUIDs) { diff --git a/Assets/YooAsset/Runtime/FileSystem/DefaultCacheFileSystem/Operation/DCFSClearUnusedBundleFilesOperation.cs.meta b/Assets/YooAsset/Runtime/FileSystem/CacheSystem/Operation/ClearUnusedCacheFilesOperation.cs.meta similarity index 100% rename from Assets/YooAsset/Runtime/FileSystem/DefaultCacheFileSystem/Operation/DCFSClearUnusedBundleFilesOperation.cs.meta rename to Assets/YooAsset/Runtime/FileSystem/CacheSystem/Operation/ClearUnusedCacheFilesOperation.cs.meta diff --git a/Assets/YooAsset/Runtime/FileSystem/DefaultCacheFileSystem/Operation/internal/SearchCacheFilesOperation.cs b/Assets/YooAsset/Runtime/FileSystem/CacheSystem/Operation/SearchCacheFilesOperation.cs similarity index 85% rename from Assets/YooAsset/Runtime/FileSystem/DefaultCacheFileSystem/Operation/internal/SearchCacheFilesOperation.cs rename to Assets/YooAsset/Runtime/FileSystem/CacheSystem/Operation/SearchCacheFilesOperation.cs index bdb79fd..6e32ea2 100644 --- a/Assets/YooAsset/Runtime/FileSystem/DefaultCacheFileSystem/Operation/internal/SearchCacheFilesOperation.cs +++ b/Assets/YooAsset/Runtime/FileSystem/CacheSystem/Operation/SearchCacheFilesOperation.cs @@ -15,7 +15,9 @@ namespace YooAsset Done, } - private readonly DefaultCacheFileSystem _fileSystem; + private readonly ICacheSystem _cacheSystem; + private readonly string _packageName; + private readonly bool _appendFileExtension; private IEnumerator _filesEnumerator = null; private float _verifyStartTime; private ESteps _steps = ESteps.None; @@ -26,9 +28,11 @@ namespace YooAsset public readonly List Result = new List(5000); - internal SearchCacheFilesOperation(DefaultCacheFileSystem fileSystem) + internal SearchCacheFilesOperation(ICacheSystem cacheSystem, string packageName, bool appendFileExtension) { - _fileSystem = fileSystem; + _cacheSystem = cacheSystem; + _packageName = packageName; + _appendFileExtension = appendFileExtension; } internal override void InternalOnStart() { @@ -42,7 +46,7 @@ namespace YooAsset if (_steps == ESteps.Prepare) { - DirectoryInfo rootDirectory = new DirectoryInfo(_fileSystem.GetCacheFilesRoot()); + DirectoryInfo rootDirectory = new DirectoryInfo(_cacheSystem.GetCacheFileRoot()); if (rootDirectory.Exists) { var directorieInfos = rootDirectory.EnumerateDirectories(); @@ -80,7 +84,7 @@ namespace YooAsset foreach (var chidDirectory in childDirectories) { string bundleGUID = chidDirectory.Name; - if (_fileSystem.IsRecordFile(bundleGUID)) + if (_cacheSystem.IsRecordFile(bundleGUID)) continue; // 创建验证元素类 @@ -89,14 +93,14 @@ namespace YooAsset string infoFilePath = $"{fileRootPath}/{ DefaultCacheFileSystemDefine.SaveBundleInfoFileName}"; // 存储的数据文件追加文件格式 - if (_fileSystem.AppendFileExtension) + if (_appendFileExtension) { string dataFileExtension = FindDataFileExtension(chidDirectory); if (string.IsNullOrEmpty(dataFileExtension) == false) dataFilePath += dataFileExtension; } - var element = new CacheFileElement(_fileSystem.PackageName, bundleGUID, fileRootPath, dataFilePath, infoFilePath); + var element = new CacheFileElement(_packageName, bundleGUID, fileRootPath, dataFilePath, infoFilePath); Result.Add(element); } diff --git a/Assets/YooAsset/Runtime/FileSystem/DefaultCacheFileSystem/Operation/internal/SearchCacheFilesOperation.cs.meta b/Assets/YooAsset/Runtime/FileSystem/CacheSystem/Operation/SearchCacheFilesOperation.cs.meta similarity index 100% rename from Assets/YooAsset/Runtime/FileSystem/DefaultCacheFileSystem/Operation/internal/SearchCacheFilesOperation.cs.meta rename to Assets/YooAsset/Runtime/FileSystem/CacheSystem/Operation/SearchCacheFilesOperation.cs.meta diff --git a/Assets/YooAsset/Runtime/FileSystem/DefaultCacheFileSystem/Operation/internal/VerifyCacheFilesOperation.cs b/Assets/YooAsset/Runtime/FileSystem/CacheSystem/Operation/VerifyCacheFilesOperation.cs similarity index 74% rename from Assets/YooAsset/Runtime/FileSystem/DefaultCacheFileSystem/Operation/internal/VerifyCacheFilesOperation.cs rename to Assets/YooAsset/Runtime/FileSystem/CacheSystem/Operation/VerifyCacheFilesOperation.cs index 943cee9..53d2761 100644 --- a/Assets/YooAsset/Runtime/FileSystem/DefaultCacheFileSystem/Operation/internal/VerifyCacheFilesOperation.cs +++ b/Assets/YooAsset/Runtime/FileSystem/CacheSystem/Operation/VerifyCacheFilesOperation.cs @@ -6,44 +6,10 @@ using System.Threading; namespace YooAsset { - internal class CacheFileElement - { - public string PackageName { private set; get; } - public string BundleGUID { private set; get; } - public string FileRootPath { private set; get; } - public string DataFilePath { private set; get; } - public string InfoFilePath { private set; get; } - - public EFileVerifyResult Result; - public string DataFileCRC; - public long DataFileSize; - - public CacheFileElement(string packageName, string bundleGUID, string fileRootPath, string dataFilePath, string infoFilePath) - { - PackageName = packageName; - BundleGUID = bundleGUID; - FileRootPath = fileRootPath; - DataFilePath = dataFilePath; - InfoFilePath = infoFilePath; - } - - public void DeleteFiles() - { - try - { - Directory.Delete(FileRootPath, true); - } - catch (System.Exception e) - { - YooLogger.Warning($"Failed to delete cache bundle folder : {e}"); - } - } - } - /// /// 缓存文件验证(线程版) /// - internal class VerifyCacheFilesOperation : AsyncOperationBase + internal sealed class VerifyCacheFilesOperation : AsyncOperationBase { private enum ESteps { @@ -54,10 +20,10 @@ namespace YooAsset } private readonly ThreadSyncContext _syncContext = new ThreadSyncContext(); - private readonly DefaultCacheFileSystem _fileSystem; + private readonly ICacheSystem _cacheSystem; + private readonly EFileVerifyLevel _verifyLevel; private List _waitingList; private List _verifyingList; - private EFileVerifyLevel _verifyLevel = EFileVerifyLevel.Middle; private int _verifyMaxNum; private int _verifyTotalCount; private float _verifyStartTime; @@ -66,11 +32,11 @@ namespace YooAsset private ESteps _steps = ESteps.None; - internal VerifyCacheFilesOperation(DefaultCacheFileSystem fileSystem, List elements) + internal VerifyCacheFilesOperation(ICacheSystem cacheSystem, EFileVerifyLevel verifyLevel, List elements) { - _fileSystem = fileSystem; + _cacheSystem = cacheSystem; + _verifyLevel = verifyLevel; _waitingList = elements; - _verifyLevel = _fileSystem.FileVerifyLevel; } internal override void InternalOnStart() { @@ -158,8 +124,8 @@ namespace YooAsset if (element.Result == EFileVerifyResult.Succeed) { _succeedCount++; - var fileWrapper = new DefaultCacheFileSystem.FileWrapper(element.InfoFilePath, element.DataFilePath, element.DataFileCRC, element.DataFileSize); - _fileSystem.RecordFile(element.BundleGUID, fileWrapper); + var fileWrapper = new CacheWrapper(element.InfoFilePath, element.DataFilePath, element.DataFileCRC, element.DataFileSize); + _cacheSystem.RecordFile(element.BundleGUID, fileWrapper); } else { @@ -191,7 +157,7 @@ namespace YooAsset return EFileVerifyResult.InfoFileNotExisted; // 解析信息文件获取验证数据 - _fileSystem.ReadInfoFile(element.InfoFilePath, out element.DataFileCRC, out element.DataFileSize); + _cacheSystem.ReadInfoFile(element.InfoFilePath, out element.DataFileCRC, out element.DataFileSize); } } catch (Exception) diff --git a/Assets/YooAsset/Runtime/FileSystem/DefaultCacheFileSystem/Operation/internal/VerifyCacheFilesOperation.cs.meta b/Assets/YooAsset/Runtime/FileSystem/CacheSystem/Operation/VerifyCacheFilesOperation.cs.meta similarity index 100% rename from Assets/YooAsset/Runtime/FileSystem/DefaultCacheFileSystem/Operation/internal/VerifyCacheFilesOperation.cs.meta rename to Assets/YooAsset/Runtime/FileSystem/CacheSystem/Operation/VerifyCacheFilesOperation.cs.meta diff --git a/Assets/YooAsset/Runtime/FileSystem/DefaultCacheFileSystem/Operation/internal/VerifyTempFileOperation.cs b/Assets/YooAsset/Runtime/FileSystem/CacheSystem/Operation/VerifyTempFileOperation.cs similarity index 78% rename from Assets/YooAsset/Runtime/FileSystem/DefaultCacheFileSystem/Operation/internal/VerifyTempFileOperation.cs rename to Assets/YooAsset/Runtime/FileSystem/CacheSystem/Operation/VerifyTempFileOperation.cs index cd4a101..f246e00 100644 --- a/Assets/YooAsset/Runtime/FileSystem/DefaultCacheFileSystem/Operation/internal/VerifyTempFileOperation.cs +++ b/Assets/YooAsset/Runtime/FileSystem/CacheSystem/Operation/VerifyTempFileOperation.cs @@ -3,29 +3,10 @@ using System.Threading; namespace YooAsset { - internal class TempFileElement - { - public string TempFilePath { private set; get; } - public string TempFileCRC { private set; get; } - public long TempFileSize { private set; get; } - - /// - /// 注意:原子操作对象 - /// - public int Result = 0; - - public TempFileElement(string filePath, string fileCRC, long fileSize) - { - TempFilePath = filePath; - TempFileCRC = fileCRC; - TempFileSize = fileSize; - } - } - /// /// 下载文件验证(线程版) /// - internal class VerifyTempFileOperation : AsyncOperationBase + internal sealed class VerifyTempFileOperation : AsyncOperationBase { private enum ESteps { @@ -41,7 +22,7 @@ namespace YooAsset /// /// 验证结果 /// - public EFileVerifyResult VerifyResult { protected set; get; } + public EFileVerifyResult VerifyResult { private set; get; } internal VerifyTempFileOperation(TempFileElement element) diff --git a/Assets/YooAsset/Runtime/FileSystem/DefaultCacheFileSystem/Operation/internal/VerifyTempFileOperation.cs.meta b/Assets/YooAsset/Runtime/FileSystem/CacheSystem/Operation/VerifyTempFileOperation.cs.meta similarity index 100% rename from Assets/YooAsset/Runtime/FileSystem/DefaultCacheFileSystem/Operation/internal/VerifyTempFileOperation.cs.meta rename to Assets/YooAsset/Runtime/FileSystem/CacheSystem/Operation/VerifyTempFileOperation.cs.meta diff --git a/Assets/YooAsset/Runtime/FileSystem/CacheSystem/TempFileElement.cs b/Assets/YooAsset/Runtime/FileSystem/CacheSystem/TempFileElement.cs new file mode 100644 index 0000000..d786ab1 --- /dev/null +++ b/Assets/YooAsset/Runtime/FileSystem/CacheSystem/TempFileElement.cs @@ -0,0 +1,22 @@ + +namespace YooAsset +{ + internal class TempFileElement + { + public string TempFilePath { private set; get; } + public string TempFileCRC { private set; get; } + public long TempFileSize { private set; get; } + + /// + /// 注意:原子操作对象 + /// + public int Result = 0; + + public TempFileElement(string filePath, string fileCRC, long fileSize) + { + TempFilePath = filePath; + TempFileCRC = fileCRC; + TempFileSize = fileSize; + } + } +} \ No newline at end of file diff --git a/Assets/YooAsset/Runtime/FileSystem/CacheSystem/TempFileElement.cs.meta b/Assets/YooAsset/Runtime/FileSystem/CacheSystem/TempFileElement.cs.meta new file mode 100644 index 0000000..f38ac5c --- /dev/null +++ b/Assets/YooAsset/Runtime/FileSystem/CacheSystem/TempFileElement.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 220136fdc4a13d84e97b1249cfc65f3b +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/YooAsset/Runtime/FileSystem/DefaultCacheFileSystem/DefaultCacheFileSystem.cs b/Assets/YooAsset/Runtime/FileSystem/DefaultCacheFileSystem/DefaultCacheFileSystem.cs index 9bc520f..eee98e2 100644 --- a/Assets/YooAsset/Runtime/FileSystem/DefaultCacheFileSystem/DefaultCacheFileSystem.cs +++ b/Assets/YooAsset/Runtime/FileSystem/DefaultCacheFileSystem/DefaultCacheFileSystem.cs @@ -10,26 +10,10 @@ namespace YooAsset /// 缓存文件系统 /// 说明:正在进行的下载器会在ResourcePackage销毁的时候执行Abort操作! /// - internal class DefaultCacheFileSystem : IFileSystem + internal class DefaultCacheFileSystem : IFileSystem, ICacheSystem { - public class FileWrapper - { - public string InfoFilePath { private set; get; } - public string DataFilePath { private set; get; } - public string DataFileCRC { private set; get; } - public long DataFileSize { private set; get; } - - public FileWrapper(string infoFilePath, string dataFilePath, string dataFileCRC, long dataFileSize) - { - InfoFilePath = infoFilePath; - DataFilePath = dataFilePath; - DataFileCRC = dataFileCRC; - DataFileSize = dataFileSize; - } - } - protected readonly Dictionary _downloaders = new Dictionary(1000); - protected readonly Dictionary _wrappers = new Dictionary(10000); + protected readonly Dictionary _wrappers = new Dictionary(10000); protected readonly Dictionary _loadedStream = new Dictionary(10000); protected readonly Dictionary _dataFilePaths = new Dictionary(10000); protected readonly Dictionary _infoFilePaths = new Dictionary(10000); @@ -128,13 +112,13 @@ namespace YooAsset } public virtual FSClearAllBundleFilesOperation ClearAllBundleFilesAsync() { - var operation = new DCFSClearAllBundleFilesOperation(this); + var operation = new ClearAllCacheFilesOperation(this); OperationSystem.StartOperation(PackageName, operation); return operation; } public virtual FSClearUnusedBundleFilesOperation ClearUnusedBundleFilesAsync(PackageManifest manifest) { - var operation = new DCFSClearUnusedBundleFilesOperation(this, manifest); + var operation = new ClearUnusedCacheFilesOperation(this, manifest); OperationSystem.StartOperation(PackageName, operation); return operation; } @@ -372,7 +356,100 @@ namespace YooAsset } } - #region 内部方法 + #region 缓存系统 + public string GetCacheFileRoot() + { + return _saveFileRoot; + } + public List GetAllCachedBundleGUIDs() + { + return _wrappers.Keys.ToList(); + } + + public bool IsRecordFile(string bundleGUID) + { + return _wrappers.ContainsKey(bundleGUID); + } + public bool RecordFile(string bundleGUID, CacheWrapper wrapper) + { + if (_wrappers.ContainsKey(bundleGUID)) + { + YooLogger.Error($"{nameof(DefaultCacheFileSystem)} has element : {bundleGUID}"); + return false; + } + + _wrappers.Add(bundleGUID, wrapper); + return true; + } + + public EFileVerifyResult VerifyCacheFile(PackageBundle bundle) + { + if (_wrappers.TryGetValue(bundle.BundleGUID, out CacheWrapper wrapper) == false) + return EFileVerifyResult.CacheNotFound; + + EFileVerifyResult result = FileSystemHelper.FileVerify(wrapper.DataFilePath, wrapper.DataFileSize, wrapper.DataFileCRC, EFileVerifyLevel.High); + return result; + } + public bool WriteCacheFile(PackageBundle bundle, string copyPath) + { + if (_wrappers.ContainsKey(bundle.BundleGUID)) + { + throw new Exception("Should never get here !"); + } + + string infoFilePath = GetInfoFilePath(bundle); + string dataFilePath = GetDataFilePath(bundle); + + try + { + if (File.Exists(infoFilePath)) + File.Delete(infoFilePath); + if (File.Exists(dataFilePath)) + File.Delete(dataFilePath); + + FileUtility.CreateFileDirectory(dataFilePath); + + // 拷贝数据文件 + FileInfo fileInfo = new FileInfo(copyPath); + fileInfo.CopyTo(dataFilePath); + + // 写入文件信息 + WriteInfoFile(infoFilePath, bundle.FileCRC, bundle.FileSize); + } + catch (Exception e) + { + YooLogger.Error($"Failed to write cache file ! {e.Message}"); + return false; + } + + var wrapper = new CacheWrapper(infoFilePath, dataFilePath, bundle.FileCRC, bundle.FileSize); + return RecordFile(bundle.BundleGUID, wrapper); + } + public bool DeleteCacheFile(string bundleGUID) + { + if (_wrappers.TryGetValue(bundleGUID, out CacheWrapper wrapper)) + { + try + { + string dataFilePath = wrapper.DataFilePath; + FileInfo fileInfo = new FileInfo(dataFilePath); + if (fileInfo.Exists) + fileInfo.Directory.Delete(true); + _wrappers.Remove(bundleGUID); + return true; + } + catch (Exception e) + { + YooLogger.Error($"Failed to delete cache file ! {e.Message}"); + return false; + } + } + else + { + return false; + } + } + private readonly BufferWriter _sharedBuffer = new BufferWriter(1024); public void WriteInfoFile(string filePath, string dataFileCRC, long dataFileSize) { @@ -392,7 +469,9 @@ namespace YooAsset dataFileCRC = buffer.ReadUTF8(); dataFileSize = buffer.ReadInt64(); } + #endregion + #region 内部方法 protected string GetDefaultRoot() { #if UNITY_EDITOR @@ -441,10 +520,6 @@ namespace YooAsset { return GetDataFilePath(bundle); } - public string GetCacheFilesRoot() - { - return _saveFileRoot; - } public string GetCachePackageHashFilePath(string packageVersion) { string fileName = YooAssetSettingsData.GetPackageHashFileName(PackageName, packageVersion); @@ -460,108 +535,6 @@ namespace YooAsset return PathUtility.Combine(_manifestFileRoot, DefaultCacheFileSystemDefine.AppFootPrintFileName); } - /// - /// 是否已经记录了文件 - /// - public bool IsRecordFile(string bundleGUID) - { - return _wrappers.ContainsKey(bundleGUID); - } - - /// - /// 记录文件信息 - /// - public bool RecordFile(string bundleGUID, FileWrapper wrapper) - { - if (_wrappers.ContainsKey(bundleGUID)) - { - YooLogger.Error($"{nameof(DefaultCacheFileSystem)} has element : {bundleGUID}"); - return false; - } - - _wrappers.Add(bundleGUID, wrapper); - return true; - } - - /// - /// 验证缓存文件 - /// - public EFileVerifyResult VerifyCacheFile(PackageBundle bundle) - { - if (_wrappers.TryGetValue(bundle.BundleGUID, out FileWrapper wrapper) == false) - return EFileVerifyResult.CacheNotFound; - - EFileVerifyResult result = FileSystemHelper.FileVerify(wrapper.DataFilePath, wrapper.DataFileSize, wrapper.DataFileCRC, EFileVerifyLevel.High); - return result; - } - - /// - /// 写入缓存文件 - /// - public bool WriteCacheFile(PackageBundle bundle, string copyPath) - { - if (_wrappers.ContainsKey(bundle.BundleGUID)) - { - throw new Exception("Should never get here !"); - } - - string infoFilePath = GetInfoFilePath(bundle); - string dataFilePath = GetDataFilePath(bundle); - - try - { - if (File.Exists(infoFilePath)) - File.Delete(infoFilePath); - if (File.Exists(dataFilePath)) - File.Delete(dataFilePath); - - FileUtility.CreateFileDirectory(dataFilePath); - - // 拷贝数据文件 - FileInfo fileInfo = new FileInfo(copyPath); - fileInfo.CopyTo(dataFilePath); - - // 写入文件信息 - WriteInfoFile(infoFilePath, bundle.FileCRC, bundle.FileSize); - } - catch (Exception e) - { - YooLogger.Error($"Failed to write cache file ! {e.Message}"); - return false; - } - - FileWrapper wrapper = new FileWrapper(infoFilePath, dataFilePath, bundle.FileCRC, bundle.FileSize); - return RecordFile(bundle.BundleGUID, wrapper); - } - - /// - /// 删除缓存文件 - /// - public bool DeleteCacheFile(string bundleGUID) - { - if (_wrappers.TryGetValue(bundleGUID, out FileWrapper wrapper)) - { - try - { - string dataFilePath = wrapper.DataFilePath; - FileInfo fileInfo = new FileInfo(dataFilePath); - if (fileInfo.Exists) - fileInfo.Directory.Delete(true); - _wrappers.Remove(bundleGUID); - return true; - } - catch (Exception e) - { - YooLogger.Error($"Failed to delete cache file ! {e.Message}"); - return false; - } - } - else - { - return false; - } - } - /// /// 删除所有清单文件 /// @@ -573,14 +546,6 @@ namespace YooAsset } } - /// - /// 获取所有缓存文件GUID - /// - public List GetAllCachedBundleGUIDs() - { - return _wrappers.Keys.ToList(); - } - /// /// 加载加密资源文件 /// diff --git a/Assets/YooAsset/Runtime/FileSystem/DefaultCacheFileSystem/Operation/DCFSInitializeOperation.cs b/Assets/YooAsset/Runtime/FileSystem/DefaultCacheFileSystem/Operation/DCFSInitializeOperation.cs index 07aed4d..e4ba18b 100644 --- a/Assets/YooAsset/Runtime/FileSystem/DefaultCacheFileSystem/Operation/DCFSInitializeOperation.cs +++ b/Assets/YooAsset/Runtime/FileSystem/DefaultCacheFileSystem/Operation/DCFSInitializeOperation.cs @@ -57,7 +57,7 @@ namespace YooAsset { if (_searchCacheFilesOp == null) { - _searchCacheFilesOp = new SearchCacheFilesOperation(_fileSytem); + _searchCacheFilesOp = new SearchCacheFilesOperation(_fileSytem, _fileSytem.PackageName, _fileSytem.AppendFileExtension); OperationSystem.StartOperation(_fileSytem.PackageName, _searchCacheFilesOp); } @@ -72,7 +72,7 @@ namespace YooAsset { if (_verifyCacheFilesOp == null) { - _verifyCacheFilesOp = new VerifyCacheFilesOperation(_fileSytem, _searchCacheFilesOp.Result); + _verifyCacheFilesOp = new VerifyCacheFilesOperation(_fileSytem, _fileSytem.FileVerifyLevel, _searchCacheFilesOp.Result); OperationSystem.StartOperation(_fileSytem.PackageName, _verifyCacheFilesOp); }