diff --git a/Assets/YooAsset/Runtime/AssetsPackage.cs b/Assets/YooAsset/Runtime/AssetsPackage.cs index 94567ee..468da44 100644 --- a/Assets/YooAsset/Runtime/AssetsPackage.cs +++ b/Assets/YooAsset/Runtime/AssetsPackage.cs @@ -947,6 +947,14 @@ namespace YooAsset { return _bundleServices.MappingToAssetPath(location); } + + /// + /// 是否包含资源文件 + /// + internal bool IsIncludeBundleFile(string fileName) + { + return _bundleServices.IsIncludeBundleFile(fileName); + } #endregion #region 调试方法 diff --git a/Assets/YooAsset/Runtime/PatchSystem/Operations/ClearUnusedCacheFilesOperation.cs b/Assets/YooAsset/Runtime/PatchSystem/Operations/ClearUnusedCacheFilesOperation.cs new file mode 100644 index 0000000..95ff48e --- /dev/null +++ b/Assets/YooAsset/Runtime/PatchSystem/Operations/ClearUnusedCacheFilesOperation.cs @@ -0,0 +1,112 @@ +using System.Collections; +using System.Collections.Generic; +using System.IO; + +namespace YooAsset +{ + /// + /// 清理未使用的缓存资源操作类 + /// + public sealed class ClearUnusedCacheFilesOperation : AsyncOperationBase + { + private enum ESteps + { + None, + GetUnusedCacheFiles, + ClearUnusedCacheFiles, + Done, + } + + private readonly List _packages; + private ESteps _steps = ESteps.None; + private List _unusedCacheFilePaths; + private int _unusedFileTotalCount = 0; + + internal ClearUnusedCacheFilesOperation(List packages) + { + _packages = packages; + } + internal override void Start() + { + _steps = ESteps.GetUnusedCacheFiles; + } + internal override void Update() + { + if (_steps == ESteps.None || _steps == ESteps.Done) + return; + + if (_steps == ESteps.GetUnusedCacheFiles) + { + _unusedCacheFilePaths = GetUnusedCacheFilePaths(); + _unusedFileTotalCount = _unusedCacheFilePaths.Count; + YooLogger.Log($"Found unused cache file count : {_unusedFileTotalCount}"); + _steps = ESteps.ClearUnusedCacheFiles; + } + + if (_steps == ESteps.ClearUnusedCacheFiles) + { + for (int i = _unusedCacheFilePaths.Count - 1; i >= 0; i--) + { + string filePath = _unusedCacheFilePaths[i]; + if (File.Exists(filePath)) + { + try + { + File.Delete(filePath); + YooLogger.Log($"Delete unused cache file : {filePath}"); + } + catch (System.Exception e) + { + YooLogger.Warning($"Failed delete cache file : {filePath} Exception : {e}"); + } + } + _unusedCacheFilePaths.RemoveAt(i); + + if (OperationSystem.IsBusy) + break; + } + + if (_unusedFileTotalCount == 0) + Progress = 1.0f; + else + Progress = 1.0f - (_unusedCacheFilePaths.Count / _unusedFileTotalCount); + + if (_unusedCacheFilePaths.Count == 0) + { + _steps = ESteps.Done; + Status = EOperationStatus.Succeed; + } + } + } + + /// + /// 获取未被使用的缓存文件路径集合 + /// + private List GetUnusedCacheFilePaths() + { + string cacheFolderPath = SandboxHelper.GetCacheFolderPath(); + if (Directory.Exists(cacheFolderPath) == false) + return new List(); + + // 获取所有缓存文件 + DirectoryInfo directoryInfo = new DirectoryInfo(cacheFolderPath); + FileInfo[] fileInfos = directoryInfo.GetFiles(); + List result = new List(fileInfos.Length); + foreach (FileInfo fileInfo in fileInfos) + { + bool used = false; + foreach (var package in _packages) + { + if (package.IsIncludeBundleFile(fileInfo.Name)) + { + used = true; + break; + } + } + if (used == false) + result.Add(fileInfo.FullName); + } + return result; + } + } +} \ No newline at end of file diff --git a/Assets/YooAsset/Runtime/PatchSystem/Operations/ClearUnusedCacheFilesOperation.cs.meta b/Assets/YooAsset/Runtime/PatchSystem/Operations/ClearUnusedCacheFilesOperation.cs.meta new file mode 100644 index 0000000..41bfef3 --- /dev/null +++ b/Assets/YooAsset/Runtime/PatchSystem/Operations/ClearUnusedCacheFilesOperation.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 5d188c50fd00bf941b2eeebb374dc0d1 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/YooAsset/Runtime/PatchSystem/PatchManifest.cs b/Assets/YooAsset/Runtime/PatchSystem/PatchManifest.cs index fe6e4d6..49cfbfd 100644 --- a/Assets/YooAsset/Runtime/PatchSystem/PatchManifest.cs +++ b/Assets/YooAsset/Runtime/PatchSystem/PatchManifest.cs @@ -36,7 +36,7 @@ namespace YooAsset /// 人类可读的版本信息 /// public string HumanReadableVersion; - + /// /// 资源列表(主动收集的资源列表) /// @@ -217,6 +217,19 @@ namespace YooAsset return BundleDic.TryGetValue(bundleName, out result); } + /// + /// 是否包含资源文件 + /// + public bool IsIncludeBundleFile(string fileName) + { + foreach (var patchBundle in BundleList) + { + if (patchBundle.FileName == fileName) + return true; + } + return false; + } + /// /// 序列化 diff --git a/Assets/YooAsset/Runtime/PatchSystem/PlayMode/EditorSimulateModeImpl.cs b/Assets/YooAsset/Runtime/PatchSystem/PlayMode/EditorSimulateModeImpl.cs index 88296fa..3dca035 100644 --- a/Assets/YooAsset/Runtime/PatchSystem/PlayMode/EditorSimulateModeImpl.cs +++ b/Assets/YooAsset/Runtime/PatchSystem/PlayMode/EditorSimulateModeImpl.cs @@ -30,7 +30,6 @@ namespace YooAsset return _simulatePatchManifest.HumanReadableVersion; } - // 设置资源清单 internal void SetSimulatePatchManifest(PatchManifest patchManifest) { _simulatePatchManifest = patchManifest; @@ -71,6 +70,10 @@ namespace YooAsset { return _simulatePatchManifest.PackageName; } + bool IBundleServices.IsIncludeBundleFile(string fileName) + { + return _simulatePatchManifest.IsIncludeBundleFile(fileName); + } #endregion } } \ No newline at end of file diff --git a/Assets/YooAsset/Runtime/PatchSystem/PlayMode/HostPlayModeImpl.cs b/Assets/YooAsset/Runtime/PatchSystem/PlayMode/HostPlayModeImpl.cs index edead9a..7a2137f 100644 --- a/Assets/YooAsset/Runtime/PatchSystem/PlayMode/HostPlayModeImpl.cs +++ b/Assets/YooAsset/Runtime/PatchSystem/PlayMode/HostPlayModeImpl.cs @@ -396,6 +396,10 @@ namespace YooAsset { return LocalPatchManifest.PackageName; } + bool IBundleServices.IsIncludeBundleFile(string fileName) + { + return LocalPatchManifest.IsIncludeBundleFile(fileName); + } #endregion } } \ No newline at end of file diff --git a/Assets/YooAsset/Runtime/PatchSystem/PlayMode/OfflinePlayModeImpl.cs b/Assets/YooAsset/Runtime/PatchSystem/PlayMode/OfflinePlayModeImpl.cs index 2a0d8ff..bd41a0f 100644 --- a/Assets/YooAsset/Runtime/PatchSystem/PlayMode/OfflinePlayModeImpl.cs +++ b/Assets/YooAsset/Runtime/PatchSystem/PlayMode/OfflinePlayModeImpl.cs @@ -121,6 +121,10 @@ namespace YooAsset { return _appPatchManifest.PackageName; } + bool IBundleServices.IsIncludeBundleFile(string fileName) + { + return _appPatchManifest.IsIncludeBundleFile(fileName); + } #endregion } } \ No newline at end of file diff --git a/Assets/YooAsset/Runtime/Services/IBundleServices.cs b/Assets/YooAsset/Runtime/Services/IBundleServices.cs index f9cd655..a7513c2 100644 --- a/Assets/YooAsset/Runtime/Services/IBundleServices.cs +++ b/Assets/YooAsset/Runtime/Services/IBundleServices.cs @@ -32,5 +32,10 @@ namespace YooAsset /// 获取所属的包裹名 /// string GetPackageName(); + + /// + /// 是否包含资源文件 + /// + bool IsIncludeBundleFile(string fileName); } } \ No newline at end of file diff --git a/Assets/YooAsset/Runtime/YooAssets.cs b/Assets/YooAsset/Runtime/YooAssets.cs index 2ecfe8d..0966995 100644 --- a/Assets/YooAsset/Runtime/YooAssets.cs +++ b/Assets/YooAsset/Runtime/YooAssets.cs @@ -177,6 +177,16 @@ namespace YooAsset #endregion #region 沙盒相关 + /// + /// 清理未使用的缓存文件 + /// + public static ClearUnusedCacheFilesOperation ClearUnusedCacheFiles() + { + ClearUnusedCacheFilesOperation operation = new ClearUnusedCacheFilesOperation(_packages); + OperationSystem.StartOperation(operation); + return operation; + } + /// /// 获取内置文件夹名称 /// @@ -200,14 +210,6 @@ namespace YooAsset { SandboxHelper.DeleteSandbox(); } - - /// - /// 清空所有的缓存文件 - /// - public static void ClearAllCacheFiles() - { - SandboxHelper.DeleteCacheFolder(); - } #endregion #region 调试信息