From e6397559ff86d9991ae6a83b94f324bff16e4099 Mon Sep 17 00:00:00 2001 From: hevinci Date: Fri, 12 May 2023 14:30:08 +0800 Subject: [PATCH] update cache system MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 新增方法ResoucePackage.ClearAllCacheFilesAsync() --- .../Handles/SubAssetsOperationHandle.cs | 2 - .../Runtime/CacheSystem/CacheSystem.cs | 8 +++ .../Operations/ClearAllCacheFilesOperation.cs | 71 +++++++++++++++++++ .../ClearAllCacheFilesOperation.cs.meta | 11 +++ .../Runtime/PackageSystem/ResourcePackage.cs | 11 +++ 5 files changed, 101 insertions(+), 2 deletions(-) create mode 100644 Assets/YooAsset/Runtime/CacheSystem/Operations/ClearAllCacheFilesOperation.cs create mode 100644 Assets/YooAsset/Runtime/CacheSystem/Operations/ClearAllCacheFilesOperation.cs.meta diff --git a/Assets/YooAsset/Runtime/AssetSystem/Handles/SubAssetsOperationHandle.cs b/Assets/YooAsset/Runtime/AssetSystem/Handles/SubAssetsOperationHandle.cs index 9c050a4..0ad7afd 100644 --- a/Assets/YooAsset/Runtime/AssetSystem/Handles/SubAssetsOperationHandle.cs +++ b/Assets/YooAsset/Runtime/AssetSystem/Handles/SubAssetsOperationHandle.cs @@ -112,8 +112,6 @@ namespace YooAsset var retObject = assetObject as TObject; if (retObject != null) ret.Add(retObject); - else - YooLogger.Warning($"The type conversion failed : {assetObject.name}"); } return ret.ToArray(); } diff --git a/Assets/YooAsset/Runtime/CacheSystem/CacheSystem.cs b/Assets/YooAsset/Runtime/CacheSystem/CacheSystem.cs index e4f7848..65c3094 100644 --- a/Assets/YooAsset/Runtime/CacheSystem/CacheSystem.cs +++ b/Assets/YooAsset/Runtime/CacheSystem/CacheSystem.cs @@ -157,6 +157,14 @@ namespace YooAsset return result; } + /// + /// 获取所有的缓存文件 + /// + public static List GetAllCacheGUIDs(ResourcePackage package) + { + var cache = GetOrCreateCache(package.PackageName); + return cache.GetAllKeys(); + } private static EVerifyResult VerifyingInternal(string filePath, long fileSize, string fileCRC, EVerifyLevel verifyLevel) { diff --git a/Assets/YooAsset/Runtime/CacheSystem/Operations/ClearAllCacheFilesOperation.cs b/Assets/YooAsset/Runtime/CacheSystem/Operations/ClearAllCacheFilesOperation.cs new file mode 100644 index 0000000..adb77d3 --- /dev/null +++ b/Assets/YooAsset/Runtime/CacheSystem/Operations/ClearAllCacheFilesOperation.cs @@ -0,0 +1,71 @@ +using System.Collections; +using System.Collections.Generic; +using System.IO; + +namespace YooAsset +{ + /// + /// 清理本地包裹所有的缓存文件 + /// + public sealed class ClearAllCacheFilesOperation : AsyncOperationBase + { + private enum ESteps + { + None, + GetAllCacheFiles, + ClearAllCacheFiles, + Done, + } + + private readonly ResourcePackage _package; + private List _allCacheGUIDs; + private int _fileTotalCount = 0; + private ESteps _steps = ESteps.None; + + internal ClearAllCacheFilesOperation(ResourcePackage package) + { + _package = package; + } + internal override void Start() + { + _steps = ESteps.GetAllCacheFiles; + } + internal override void Update() + { + if (_steps == ESteps.None || _steps == ESteps.Done) + return; + + if (_steps == ESteps.GetAllCacheFiles) + { + _allCacheGUIDs = CacheSystem.GetAllCacheGUIDs(_package); + _fileTotalCount = _allCacheGUIDs.Count; + YooLogger.Log($"Found all cache file count : {_fileTotalCount}"); + _steps = ESteps.ClearAllCacheFiles; + } + + if (_steps == ESteps.ClearAllCacheFiles) + { + for (int i = _allCacheGUIDs.Count - 1; i >= 0; i--) + { + string cacheGUID = _allCacheGUIDs[i]; + CacheSystem.DiscardFile(_package.PackageName, cacheGUID); + _allCacheGUIDs.RemoveAt(i); + + if (OperationSystem.IsBusy) + break; + } + + if (_fileTotalCount == 0) + Progress = 1.0f; + else + Progress = 1.0f - (_allCacheGUIDs.Count / _fileTotalCount); + + if (_allCacheGUIDs.Count == 0) + { + _steps = ESteps.Done; + Status = EOperationStatus.Succeed; + } + } + } + } +} \ No newline at end of file diff --git a/Assets/YooAsset/Runtime/CacheSystem/Operations/ClearAllCacheFilesOperation.cs.meta b/Assets/YooAsset/Runtime/CacheSystem/Operations/ClearAllCacheFilesOperation.cs.meta new file mode 100644 index 0000000..a16ec01 --- /dev/null +++ b/Assets/YooAsset/Runtime/CacheSystem/Operations/ClearAllCacheFilesOperation.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 4eb0b0eafee709d478ab6d81faacb304 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/YooAsset/Runtime/PackageSystem/ResourcePackage.cs b/Assets/YooAsset/Runtime/PackageSystem/ResourcePackage.cs index 4bbb5f8..10171e3 100644 --- a/Assets/YooAsset/Runtime/PackageSystem/ResourcePackage.cs +++ b/Assets/YooAsset/Runtime/PackageSystem/ResourcePackage.cs @@ -251,6 +251,17 @@ namespace YooAsset return operation; } + /// + /// 清理包裹本地所有的缓存文件 + /// + public ClearAllCacheFilesOperation ClearAllCacheFilesAsync() + { + DebugCheckInitialize(); + var operation = new ClearAllCacheFilesOperation(this); + OperationSystem.StartOperation(operation); + return operation; + } + /// /// 获取本地包裹的版本信息 ///