From ebcb05cc55cc24715e3b35dea331c7f73301a8a3 Mon Sep 17 00:00:00 2001 From: hevinci Date: Tue, 28 Feb 2023 18:25:27 +0800 Subject: [PATCH] update cache system MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 优化了缓存系统初始化逻辑,支持分帧获取所有缓存文件。 --- .../CacheSystem/Operations/Internal.meta | 8 + .../Internal/FindCacheFilesOperation.cs | 168 ++++++++++++++++++ .../Internal/FindCacheFilesOperation.cs.meta | 11 ++ .../VerifyCacheFilesOperation.cs} | 20 +-- .../VerifyCacheFilesOperation.cs.meta} | 0 .../Operations/PackageCachingOperation.cs | 74 +++----- .../Runtime/PatchSystem/PatchBundle.cs | 26 ++- Assets/YooAsset/Runtime/Utility/YooHelper.cs | 17 +- 8 files changed, 257 insertions(+), 67 deletions(-) create mode 100644 Assets/YooAsset/Runtime/CacheSystem/Operations/Internal.meta create mode 100644 Assets/YooAsset/Runtime/CacheSystem/Operations/Internal/FindCacheFilesOperation.cs create mode 100644 Assets/YooAsset/Runtime/CacheSystem/Operations/Internal/FindCacheFilesOperation.cs.meta rename Assets/YooAsset/Runtime/CacheSystem/Operations/{PackageVerifyOperation.cs => Internal/VerifyCacheFilesOperation.cs} (89%) rename Assets/YooAsset/Runtime/CacheSystem/Operations/{PackageVerifyOperation.cs.meta => Internal/VerifyCacheFilesOperation.cs.meta} (100%) diff --git a/Assets/YooAsset/Runtime/CacheSystem/Operations/Internal.meta b/Assets/YooAsset/Runtime/CacheSystem/Operations/Internal.meta new file mode 100644 index 0000000..81927ef --- /dev/null +++ b/Assets/YooAsset/Runtime/CacheSystem/Operations/Internal.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: a93a516506b2b5c4492fdefe26eb1175 +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/YooAsset/Runtime/CacheSystem/Operations/Internal/FindCacheFilesOperation.cs b/Assets/YooAsset/Runtime/CacheSystem/Operations/Internal/FindCacheFilesOperation.cs new file mode 100644 index 0000000..6be1a7c --- /dev/null +++ b/Assets/YooAsset/Runtime/CacheSystem/Operations/Internal/FindCacheFilesOperation.cs @@ -0,0 +1,168 @@ +using System; +using System.IO; +using System.Collections; +using System.Collections.Generic; + +namespace YooAsset +{ + internal class FindCacheFilesOperation : AsyncOperationBase + { + private enum ESteps + { + None, + FindPrepare, + FindBundleFiles, + FindRawFiles, + Done, + } + + private readonly string _packageName; + private float _verifyStartTime; + private IEnumerator _bundleFilesEnumerator = null; + private IEnumerator _rawFilesEnumerator = null; + private ESteps _steps = ESteps.None; + + /// + /// 需要验证的元素 + /// + public readonly List VerifyElements = new List(5000); + + public FindCacheFilesOperation(string packageName) + { + _packageName = packageName; + } + internal override void Start() + { + _steps = ESteps.FindPrepare; + _verifyStartTime = UnityEngine.Time.realtimeSinceStartup; + } + internal override void Update() + { + if (_steps == ESteps.None || _steps == ESteps.Done) + return; + + if (_steps == ESteps.FindPrepare) + { + // BundleFiles + { + string rootPath = PersistentHelper.GetCachedBundleFileFolderPath(_packageName); + DirectoryInfo rootDirectory = new DirectoryInfo(rootPath); + if (rootDirectory.Exists) + { + var directorieInfos = rootDirectory.EnumerateDirectories(); + _bundleFilesEnumerator = directorieInfos.GetEnumerator(); + } + } + + // RawFiles + { + string rootPath = PersistentHelper.GetCachedRawFileFolderPath(_packageName); + DirectoryInfo rootDirectory = new DirectoryInfo(rootPath); + if (rootDirectory.Exists) + { + var directorieInfos = rootDirectory.EnumerateDirectories(); + _rawFilesEnumerator = directorieInfos.GetEnumerator(); + } + } + + _steps = ESteps.FindBundleFiles; + } + + if (_steps == ESteps.FindBundleFiles) + { + if (UpdateFindBundleFiles()) + return; + + _steps = ESteps.FindRawFiles; + } + + if (_steps == ESteps.FindRawFiles) + { + if (UpdateFindRawFiles()) + return; + + // 注意:总是返回成功 + _steps = ESteps.Done; + Status = EOperationStatus.Succeed; + float costTime = UnityEngine.Time.realtimeSinceStartup - _verifyStartTime; + YooLogger.Log($"Find cache files elapsed time {costTime:f1} seconds"); + } + } + + private bool UpdateFindBundleFiles() + { + if (_bundleFilesEnumerator == null) + return false; + + bool isFindItem; + while (true) + { + isFindItem = _bundleFilesEnumerator.MoveNext(); + if (isFindItem == false) + break; + + var fileFoder = _bundleFilesEnumerator.Current; + string cacheGUID = fileFoder.Name; + if (CacheSystem.IsCached(_packageName, cacheGUID)) + continue; + + // 创建验证元素类 + string fileRootPath = fileFoder.FullName; + string dataFilePath = $"{fileRootPath}/{ YooAssetSettings.CacheBundleDataFileName}"; + string infoFilePath = $"{fileRootPath}/{ YooAssetSettings.CacheBundleInfoFileName}"; + VerifyElement element = new VerifyElement(_packageName, cacheGUID, fileRootPath, dataFilePath, infoFilePath); + VerifyElements.Add(element); + + if (OperationSystem.IsBusy) + break; + } + + return isFindItem; + } + private bool UpdateFindRawFiles() + { + if (_rawFilesEnumerator == null) + return false; + + bool isFindItem; + while (true) + { + isFindItem = _rawFilesEnumerator.MoveNext(); + if (isFindItem == false) + break; + + var fileFoder = _rawFilesEnumerator.Current; + string cacheGUID = fileFoder.Name; + if (CacheSystem.IsCached(_packageName, cacheGUID)) + continue; + + // 获取数据文件的后缀名 + string dataFileExtension = string.Empty; + var fileInfos = fileFoder.GetFiles(); + foreach (var fileInfo in fileInfos) + { + if (fileInfo.Extension == ".temp") + continue; + + if (fileInfo.Name.StartsWith(YooAssetSettings.CacheBundleDataFileName)) + { + dataFileExtension = fileInfo.Extension; + break; + } + } + + // 创建验证元素类 + string fileRootPath = fileFoder.FullName; + string dataFilePath = $"{fileRootPath}/{ YooAssetSettings.CacheBundleDataFileName}{dataFileExtension}"; + string infoFilePath = $"{fileRootPath}/{ YooAssetSettings.CacheBundleInfoFileName}"; + VerifyElement element = new VerifyElement(_packageName, cacheGUID, fileRootPath, dataFilePath, infoFilePath); + VerifyElements.Add(element); + + if (OperationSystem.IsBusy) + break; + } + + return isFindItem; + } + } +} \ No newline at end of file diff --git a/Assets/YooAsset/Runtime/CacheSystem/Operations/Internal/FindCacheFilesOperation.cs.meta b/Assets/YooAsset/Runtime/CacheSystem/Operations/Internal/FindCacheFilesOperation.cs.meta new file mode 100644 index 0000000..66a2d13 --- /dev/null +++ b/Assets/YooAsset/Runtime/CacheSystem/Operations/Internal/FindCacheFilesOperation.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 00ec004354d75ac499606d6959192f9b +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/YooAsset/Runtime/CacheSystem/Operations/PackageVerifyOperation.cs b/Assets/YooAsset/Runtime/CacheSystem/Operations/Internal/VerifyCacheFilesOperation.cs similarity index 89% rename from Assets/YooAsset/Runtime/CacheSystem/Operations/PackageVerifyOperation.cs rename to Assets/YooAsset/Runtime/CacheSystem/Operations/Internal/VerifyCacheFilesOperation.cs index db2a563..3ee1bfb 100644 --- a/Assets/YooAsset/Runtime/CacheSystem/Operations/PackageVerifyOperation.cs +++ b/Assets/YooAsset/Runtime/CacheSystem/Operations/Internal/VerifyCacheFilesOperation.cs @@ -6,14 +6,14 @@ using System.Threading; namespace YooAsset { - internal abstract class PackageVerifyOperation : AsyncOperationBase + internal abstract class VerifyCacheFilesOperation : AsyncOperationBase { - public static PackageVerifyOperation CreateOperation(List elements) + public static VerifyCacheFilesOperation CreateOperation(List elements) { #if UNITY_WEBGL - var operation = new PackageVerifyWithoutThreadOperation(elements); + var operation = new VerifyCacheFilesWithoutThreadOperation(elements); #else - var operation = new PackageVerifyWithThreadOperation(elements); + var operation = new VerifyCacheFilesWithThreadOperation(elements); #endif return operation; } @@ -22,7 +22,7 @@ namespace YooAsset /// /// 本地缓存文件验证(线程版) /// - internal class PackageVerifyWithThreadOperation : PackageVerifyOperation + internal class VerifyCacheFilesWithThreadOperation : VerifyCacheFilesOperation { private enum ESteps { @@ -42,7 +42,7 @@ namespace YooAsset private int _failedCount; private ESteps _steps = ESteps.None; - public PackageVerifyWithThreadOperation(List elements) + public VerifyCacheFilesWithThreadOperation(List elements) { _waitingList = elements; } @@ -82,7 +82,7 @@ namespace YooAsset _steps = ESteps.Done; Status = EOperationStatus.Succeed; float costTime = UnityEngine.Time.realtimeSinceStartup - _verifyStartTime; - YooLogger.Log($"Package verify elapsed time {costTime:f1} seconds"); + YooLogger.Log($"Verify cache files elapsed time {costTime:f1} seconds"); } for (int i = _waitingList.Count - 1; i >= 0; i--) @@ -148,7 +148,7 @@ namespace YooAsset /// /// 本地缓存文件验证(非线程版) /// - internal class PackageVerifyWithoutThreadOperation : PackageVerifyOperation + internal class VerifyCacheFilesWithoutThreadOperation : VerifyCacheFilesOperation { private enum ESteps { @@ -166,8 +166,8 @@ namespace YooAsset private int _succeedCount; private int _failedCount; private ESteps _steps = ESteps.None; - - public PackageVerifyWithoutThreadOperation(List elements) + + public VerifyCacheFilesWithoutThreadOperation(List elements) { _waitingList = elements; } diff --git a/Assets/YooAsset/Runtime/CacheSystem/Operations/PackageVerifyOperation.cs.meta b/Assets/YooAsset/Runtime/CacheSystem/Operations/Internal/VerifyCacheFilesOperation.cs.meta similarity index 100% rename from Assets/YooAsset/Runtime/CacheSystem/Operations/PackageVerifyOperation.cs.meta rename to Assets/YooAsset/Runtime/CacheSystem/Operations/Internal/VerifyCacheFilesOperation.cs.meta diff --git a/Assets/YooAsset/Runtime/CacheSystem/Operations/PackageCachingOperation.cs b/Assets/YooAsset/Runtime/CacheSystem/Operations/PackageCachingOperation.cs index aafa1af..ae87610 100644 --- a/Assets/YooAsset/Runtime/CacheSystem/Operations/PackageCachingOperation.cs +++ b/Assets/YooAsset/Runtime/CacheSystem/Operations/PackageCachingOperation.cs @@ -10,13 +10,14 @@ namespace YooAsset private enum ESteps { None, - GetCacheFiles, + FindCacheFiles, VerifyCacheFiles, Done, } private readonly string _packageName; - private PackageVerifyOperation _packageVerifyOp; + private FindCacheFilesOperation _findCacheFilesOp; + private VerifyCacheFilesOperation _verifyCacheFilesOp; private ESteps _steps = ESteps.None; public PackageCachingOperation(string packageName) @@ -25,25 +26,40 @@ namespace YooAsset } internal override void Start() { - _steps = ESteps.GetCacheFiles; + _steps = ESteps.FindCacheFiles; } internal override void Update() { if (_steps == ESteps.None || _steps == ESteps.Done) return; - if (_steps == ESteps.GetCacheFiles) + if (_steps == ESteps.FindCacheFiles) { - var elements = GetVerifyElements(); - _packageVerifyOp = PackageVerifyOperation.CreateOperation(elements); - OperationSystem.StartOperation(_packageVerifyOp); + if (_findCacheFilesOp == null) + { + _findCacheFilesOp = new FindCacheFilesOperation(_packageName); + OperationSystem.StartOperation(_findCacheFilesOp); + _steps = ESteps.VerifyCacheFiles; + } + + Progress = _findCacheFilesOp.Progress; + if (_findCacheFilesOp.IsDone == false) + return; + _steps = ESteps.VerifyCacheFiles; } if (_steps == ESteps.VerifyCacheFiles) { - Progress = _packageVerifyOp.Progress; - if (_packageVerifyOp.IsDone == false) + if (_verifyCacheFilesOp == null) + { + _verifyCacheFilesOp = VerifyCacheFilesOperation.CreateOperation(_findCacheFilesOp.VerifyElements); + OperationSystem.StartOperation(_verifyCacheFilesOp); + _steps = ESteps.VerifyCacheFiles; + } + + Progress = _verifyCacheFilesOp.Progress; + if (_verifyCacheFilesOp.IsDone == false) return; // 注意:总是返回成功 @@ -51,45 +67,5 @@ namespace YooAsset Status = EOperationStatus.Succeed; } } - - private List GetVerifyElements() - { - string cacheFolderPath = PersistentHelper.GetCacheFolderPath(_packageName); - if (Directory.Exists(cacheFolderPath) == false) - return new List(); - - DirectoryInfo rootDirectory = new DirectoryInfo(cacheFolderPath); - DirectoryInfo[] fileFolders = rootDirectory.GetDirectories(); - List result = new List(fileFolders.Length); - foreach (var fileFoder in fileFolders) - { - string cacheGUID = fileFoder.Name; - if (CacheSystem.IsCached(_packageName, cacheGUID)) - continue; - - // 获取数据文件的后缀名 - string dataFileExtension = string.Empty; - var fileInfos = fileFoder.GetFiles(); - foreach (var fileInfo in fileInfos) - { - if (fileInfo.Extension == ".temp") - continue; - - if (fileInfo.Name.StartsWith(YooAssetSettings.CacheBundleDataFileName)) - { - dataFileExtension = fileInfo.Extension; - break; - } - } - - string fileRootPath = fileFoder.FullName; - string dataFilePath = $"{fileRootPath}/{ YooAssetSettings.CacheBundleDataFileName}{dataFileExtension}"; - string infoFilePath = $"{fileRootPath}/{ YooAssetSettings.CacheBundleInfoFileName}"; - VerifyElement element = new VerifyElement(_packageName, cacheGUID, fileRootPath, dataFilePath, infoFilePath); - result.Add(element); - } - - return result; - } } } \ No newline at end of file diff --git a/Assets/YooAsset/Runtime/PatchSystem/PatchBundle.cs b/Assets/YooAsset/Runtime/PatchSystem/PatchBundle.cs index d4a7699..7e6e041 100644 --- a/Assets/YooAsset/Runtime/PatchSystem/PatchBundle.cs +++ b/Assets/YooAsset/Runtime/PatchSystem/PatchBundle.cs @@ -46,7 +46,7 @@ namespace YooAsset /// public int[] ReferenceIDs; - + /// /// 所属的包裹名称 /// @@ -71,8 +71,16 @@ namespace YooAsset if (string.IsNullOrEmpty(_cachedDataFilePath) == false) return _cachedDataFilePath; - string cacheRoot = PersistentHelper.GetCacheFolderPath(PackageName); - _cachedDataFilePath = $"{cacheRoot}/{CacheGUID}/{YooAssetSettings.CacheBundleDataFileName}{_fileExtension}"; + if (IsRawFile) + { + string cacheRoot = PersistentHelper.GetCachedRawFileFolderPath(PackageName); + _cachedDataFilePath = $"{cacheRoot}/{CacheGUID}/{YooAssetSettings.CacheBundleDataFileName}{_fileExtension}"; + } + else + { + string cacheRoot = PersistentHelper.GetCachedBundleFileFolderPath(PackageName); + _cachedDataFilePath = $"{cacheRoot}/{CacheGUID}/{YooAssetSettings.CacheBundleDataFileName}"; + } return _cachedDataFilePath; } } @@ -88,8 +96,16 @@ namespace YooAsset if (string.IsNullOrEmpty(_cachedInfoFilePath) == false) return _cachedInfoFilePath; - string cacheRoot = PersistentHelper.GetCacheFolderPath(PackageName); - _cachedInfoFilePath = $"{cacheRoot}/{CacheGUID}/{YooAssetSettings.CacheBundleInfoFileName}"; + if (IsRawFile) + { + string cacheRoot = PersistentHelper.GetCachedRawFileFolderPath(PackageName); + _cachedInfoFilePath = $"{cacheRoot}/{CacheGUID}/{YooAssetSettings.CacheBundleInfoFileName}"; + } + else + { + string cacheRoot = PersistentHelper.GetCachedBundleFileFolderPath(PackageName); + _cachedInfoFilePath = $"{cacheRoot}/{CacheGUID}/{YooAssetSettings.CacheBundleInfoFileName}"; + } return _cachedInfoFilePath; } } diff --git a/Assets/YooAsset/Runtime/Utility/YooHelper.cs b/Assets/YooAsset/Runtime/Utility/YooHelper.cs index a8e4835..83131a5 100644 --- a/Assets/YooAsset/Runtime/Utility/YooHelper.cs +++ b/Assets/YooAsset/Runtime/Utility/YooHelper.cs @@ -84,6 +84,8 @@ namespace YooAsset internal static class PersistentHelper { private const string CacheFolderName = "CacheFiles"; + private const string CachedBundleFileFolder = "BundleFiles"; + private const string CachedRawFileFolder = "RawFiles"; private const string ManifestFolderName = "ManifestFiles"; private const string AppFootPrintFileName = "ApplicationFootPrint.bytes"; @@ -120,12 +122,21 @@ namespace YooAsset /// - /// 获取缓存文件夹路径 + /// 获取缓存的BundleFile文件夹路径 /// - public static string GetCacheFolderPath(string packageName) + public static string GetCachedBundleFileFolderPath(string packageName) { string root = PathHelper.MakePersistentLoadPath(CacheFolderName); - return $"{root}/{packageName}"; + return $"{root}/{packageName}/{CachedBundleFileFolder}"; + } + + /// + /// 获取缓存的RawFile文件夹路径 + /// + public static string GetCachedRawFileFolderPath(string packageName) + { + string root = PathHelper.MakePersistentLoadPath(CacheFolderName); + return $"{root}/{packageName}/{CachedRawFileFolder}"; } ///