From 873d8731944d526851a57ddb30aa09803ad41cbb Mon Sep 17 00:00:00 2001 From: hevinci Date: Mon, 2 May 2022 15:03:36 +0800 Subject: [PATCH] Add clear unused cache files method. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 增加清空缓存文件的方法。 --- .../Runtime/DownloadSystem/DownloadSystem.cs | 4 +- .../Runtime/PatchSystem/BundleInfo.cs | 90 +++++++++---------- .../YooAsset/Runtime/PatchSystem/CacheData.cs | 60 +++++++++++++ .../{PatchCache.cs.meta => CacheData.cs.meta} | 0 .../Operations/InitializationOperation.cs | 16 ++-- .../Operations/UpdateManifestOperation.cs | 4 +- .../Operations/UpdatePackageOperation.cs | 2 +- .../Runtime/PatchSystem/PatchCache.cs | 55 ------------ .../PatchSystem/PlayMode/HostPlayModeImpl.cs | 30 +++++++ .../LocationServicesHelper.cs | 12 +-- Assets/YooAsset/Runtime/Utility/YooHelper.cs | 43 +++------ Assets/YooAsset/Runtime/YooAssets.cs | 37 +++++--- 12 files changed, 191 insertions(+), 162 deletions(-) create mode 100644 Assets/YooAsset/Runtime/PatchSystem/CacheData.cs rename Assets/YooAsset/Runtime/PatchSystem/{PatchCache.cs.meta => CacheData.cs.meta} (100%) delete mode 100644 Assets/YooAsset/Runtime/PatchSystem/PatchCache.cs diff --git a/Assets/YooAsset/Runtime/DownloadSystem/DownloadSystem.cs b/Assets/YooAsset/Runtime/DownloadSystem/DownloadSystem.cs index 2b10ad7..3c8eb70 100644 --- a/Assets/YooAsset/Runtime/DownloadSystem/DownloadSystem.cs +++ b/Assets/YooAsset/Runtime/DownloadSystem/DownloadSystem.cs @@ -112,7 +112,7 @@ namespace YooAsset { if (_cachedHashList.ContainsKey(hash)) { - string filePath = SandboxHelper.MakeSandboxCacheFilePath(hash); + string filePath = SandboxHelper.MakeCacheFilePath(hash); if (File.Exists(filePath)) { return true; @@ -150,7 +150,7 @@ namespace YooAsset } public static bool CheckContentIntegrity(PatchBundle patchBundle) { - string filePath = SandboxHelper.MakeSandboxCacheFilePath(patchBundle.Hash); + string filePath = SandboxHelper.MakeCacheFilePath(patchBundle.Hash); return CheckContentIntegrity(filePath, patchBundle.SizeBytes, patchBundle.CRC); } public static bool CheckContentIntegrity(string filePath, long size, string crc) diff --git a/Assets/YooAsset/Runtime/PatchSystem/BundleInfo.cs b/Assets/YooAsset/Runtime/PatchSystem/BundleInfo.cs index 82de44c..7597fcd 100644 --- a/Assets/YooAsset/Runtime/PatchSystem/BundleInfo.cs +++ b/Assets/YooAsset/Runtime/PatchSystem/BundleInfo.cs @@ -25,12 +25,12 @@ namespace YooAsset /// /// 远端下载地址 /// - public string RemoteMainURL { private set; get; } + internal string RemoteMainURL { private set; get; } /// /// 远端下载备用地址 /// - public string RemoteFallbackURL { private set; get; } + internal string RemoteFallbackURL { private set; get; } /// /// 文件哈希值 @@ -103,49 +103,6 @@ namespace YooAsset } - /// - /// 资源包是否有效 - /// - public bool IsValid() - { - return _patchBundle != null; - } - - /// - /// 资源包文件是否在云端 - /// - public bool InCloud() - { - return LoadMode == ELoadMode.LoadFromRemote; - } - - /// - /// 获取流文件夹的加载路径 - /// - public string GetStreamingLoadPath() - { - if (_patchBundle == null) - return string.Empty; - - if (string.IsNullOrEmpty(_streamingPath)) - _streamingPath = PathHelper.MakeStreamingLoadPath(_patchBundle.Hash); - return _streamingPath; - } - - /// - /// 获取缓存文件夹的加载路径 - /// - public string GetCacheLoadPath() - { - if (_patchBundle == null) - return string.Empty; - - if (string.IsNullOrEmpty(_cachePath)) - _cachePath = SandboxHelper.MakeSandboxCacheFilePath(_patchBundle.Hash); - return _cachePath; - } - - private BundleInfo() { } @@ -174,6 +131,49 @@ namespace YooAsset RemoteFallbackURL = string.Empty; } + /// + /// 资源包是否有效 + /// + public bool IsValid() + { + return _patchBundle != null; + } + + /// + /// 资源包文件是否在云端 + /// + public bool InCloud() + { + return LoadMode == ELoadMode.LoadFromRemote; + } + + /// + /// 获取流文件夹的加载路径 + /// + internal string GetStreamingLoadPath() + { + if (_patchBundle == null) + return string.Empty; + + if (string.IsNullOrEmpty(_streamingPath)) + _streamingPath = PathHelper.MakeStreamingLoadPath(_patchBundle.Hash); + return _streamingPath; + } + + /// + /// 获取缓存文件夹的加载路径 + /// + internal string GetCacheLoadPath() + { + if (_patchBundle == null) + return string.Empty; + + if (string.IsNullOrEmpty(_cachePath)) + _cachePath = SandboxHelper.MakeCacheFilePath(_patchBundle.Hash); + return _cachePath; + } + + /// /// 是否为JAR包内文件 /// diff --git a/Assets/YooAsset/Runtime/PatchSystem/CacheData.cs b/Assets/YooAsset/Runtime/PatchSystem/CacheData.cs new file mode 100644 index 0000000..8cfdd20 --- /dev/null +++ b/Assets/YooAsset/Runtime/PatchSystem/CacheData.cs @@ -0,0 +1,60 @@ +using System; +using System.Collections; +using System.Collections.Generic; +using System.IO; +using UnityEngine; + +namespace YooAsset +{ + [Serializable] + internal sealed class CacheData + { + /// + /// 缓存的APP内置版本 + /// + public string CacheAppVersion = string.Empty; + + /// + /// 读取缓存文件 + /// 注意:如果文件不存在则创建新的缓存文件 + /// + public static CacheData LoadCache() + { + string filePath = GetCacheDataFilePath(); + if (File.Exists(filePath)) + { + string jsonData = FileUtility.ReadFile(filePath); + var cacheData = JsonUtility.FromJson(jsonData); + YooLogger.Log($"Load cache data : {cacheData.CacheAppVersion}"); + return cacheData; + } + else + { + YooLogger.Log($"Create cache data : {Application.version}"); + CacheData cacheData = new CacheData(); + cacheData.CacheAppVersion = Application.version; + string jsonData = JsonUtility.ToJson(cacheData); + FileUtility.CreateFile(filePath, jsonData); + return cacheData; + } + } + + /// + /// 更新缓存文件 + /// + public static void UpdateCache() + { + YooLogger.Log($"Update cache data to disk : {Application.version}"); + CacheData cacheData = new CacheData(); + cacheData.CacheAppVersion = Application.version; + string filePath = GetCacheDataFilePath(); + string jsonData = JsonUtility.ToJson(cacheData); + FileUtility.CreateFile(filePath, jsonData); + } + + private static string GetCacheDataFilePath() + { + return PathHelper.MakePersistentLoadPath("CacheData.bytes"); + } + } +} \ No newline at end of file diff --git a/Assets/YooAsset/Runtime/PatchSystem/PatchCache.cs.meta b/Assets/YooAsset/Runtime/PatchSystem/CacheData.cs.meta similarity index 100% rename from Assets/YooAsset/Runtime/PatchSystem/PatchCache.cs.meta rename to Assets/YooAsset/Runtime/PatchSystem/CacheData.cs.meta diff --git a/Assets/YooAsset/Runtime/PatchSystem/Operations/InitializationOperation.cs b/Assets/YooAsset/Runtime/PatchSystem/Operations/InitializationOperation.cs index 9c223dc..801c0b1 100644 --- a/Assets/YooAsset/Runtime/PatchSystem/Operations/InitializationOperation.cs +++ b/Assets/YooAsset/Runtime/PatchSystem/Operations/InitializationOperation.cs @@ -53,7 +53,7 @@ namespace YooAsset { if (_steps == ESteps.None || _steps == ESteps.Done) return; - + if (_steps == ESteps.Update) { _appManifestLoader.Update(); @@ -61,7 +61,7 @@ namespace YooAsset return; if (_appManifestLoader.Result == null) - { + { _steps = ESteps.Done; Status = EOperationStatus.Failed; Error = _appManifestLoader.Error; @@ -110,20 +110,20 @@ namespace YooAsset if (_steps == ESteps.InitCache) { // 每次启动时比对APP版本号是否一致 - PatchCache cache = PatchCache.LoadCache(); - if (cache.CacheAppVersion != Application.version) + CacheData cacheData = CacheData.LoadCache(); + if (cacheData.CacheAppVersion != Application.version) { - YooLogger.Warning($"Cache is dirty ! Cache app version is {cache.CacheAppVersion}, Current app version is {Application.version}"); + YooLogger.Warning($"Cache is dirty ! Cache application version is {cacheData.CacheAppVersion}, Current application version is {Application.version}"); // 注意:在覆盖安装的时候,会保留APP沙盒目录,可以选择清空缓存目录 if (_impl.ClearCacheWhenDirty) { YooLogger.Warning("Clear cache files."); - SandboxHelper.DeleteSandboxCacheFolder(); + SandboxHelper.DeleteCacheFolder(); } // 更新缓存文件 - PatchCache.UpdateCache(); + CacheData.UpdateCache(); } _steps = ESteps.Update; } @@ -243,7 +243,7 @@ namespace YooAsset if (_downloader2.HasError()) { - Error = _downloader2.GetError(); + Error = _downloader2.GetError(); _steps = ESteps.Failed; } else diff --git a/Assets/YooAsset/Runtime/PatchSystem/Operations/UpdateManifestOperation.cs b/Assets/YooAsset/Runtime/PatchSystem/Operations/UpdateManifestOperation.cs index a560849..d5c63a4 100644 --- a/Assets/YooAsset/Runtime/PatchSystem/Operations/UpdateManifestOperation.cs +++ b/Assets/YooAsset/Runtime/PatchSystem/Operations/UpdateManifestOperation.cs @@ -277,7 +277,7 @@ namespace YooAsset } // 查看文件是否存在 - string filePath = SandboxHelper.MakeSandboxCacheFilePath(patchBundle.Hash); + string filePath = SandboxHelper.MakeCacheFilePath(patchBundle.Hash); if (File.Exists(filePath) == false) continue; @@ -321,7 +321,7 @@ namespace YooAsset } private bool RunThread(PatchBundle patchBundle) { - string filePath = SandboxHelper.MakeSandboxCacheFilePath(patchBundle.Hash); + string filePath = SandboxHelper.MakeCacheFilePath(patchBundle.Hash); ThreadInfo info = new ThreadInfo(filePath, patchBundle); return ThreadPool.QueueUserWorkItem(new WaitCallback(VerifyInThread), info); } diff --git a/Assets/YooAsset/Runtime/PatchSystem/Operations/UpdatePackageOperation.cs b/Assets/YooAsset/Runtime/PatchSystem/Operations/UpdatePackageOperation.cs index b69fa57..89dd3c5 100644 --- a/Assets/YooAsset/Runtime/PatchSystem/Operations/UpdatePackageOperation.cs +++ b/Assets/YooAsset/Runtime/PatchSystem/Operations/UpdatePackageOperation.cs @@ -210,7 +210,7 @@ namespace YooAsset // 注意:下载系统只会验证当前游戏版本的资源文件,对于其它游戏版本的差异文件不会在初始化的时候去做校验。 // 注意:通过比对文件大小做实时的文件校验方式! - string filePath = SandboxHelper.MakeSandboxCacheFilePath(patchBundle.Hash); + string filePath = SandboxHelper.MakeCacheFilePath(patchBundle.Hash); if (File.Exists(filePath)) { long fileSize = FileUtility.GetFileSize(filePath); diff --git a/Assets/YooAsset/Runtime/PatchSystem/PatchCache.cs b/Assets/YooAsset/Runtime/PatchSystem/PatchCache.cs deleted file mode 100644 index 9414727..0000000 --- a/Assets/YooAsset/Runtime/PatchSystem/PatchCache.cs +++ /dev/null @@ -1,55 +0,0 @@ -using System; -using System.Collections; -using System.Collections.Generic; -using UnityEngine; - -namespace YooAsset -{ - [Serializable] - internal sealed class PatchCache - { - /// - /// 缓存的APP内置版本 - /// - public string CacheAppVersion = string.Empty; - - /// - /// 读取缓存文件 - /// 注意:如果文件不存在则创建新的缓存文件 - /// - public static PatchCache LoadCache() - { - if (SandboxHelper.CheckSandboxCacheFileExist()) - { - string filePath = SandboxHelper.GetSandboxCacheFilePath(); - string jsonData = FileUtility.ReadFile(filePath); - var patchCache = JsonUtility.FromJson(jsonData); - YooLogger.Log($"Load cache file : {patchCache.CacheAppVersion}"); - return patchCache; - } - else - { - YooLogger.Log($"Create cache file : {Application.version}"); - PatchCache cache = new PatchCache(); - cache.CacheAppVersion = Application.version; - string filePath = SandboxHelper.GetSandboxCacheFilePath(); - string jsonData = JsonUtility.ToJson(cache); - FileUtility.CreateFile(filePath, jsonData); - return cache; - } - } - - /// - /// 更新缓存文件 - /// - public static void UpdateCache() - { - YooLogger.Log($"Update patch cache to disk : {Application.version}"); - PatchCache cache = new PatchCache(); - cache.CacheAppVersion = Application.version; - string filePath = SandboxHelper.GetSandboxCacheFilePath(); - string jsonData = JsonUtility.ToJson(cache); - FileUtility.CreateFile(filePath, jsonData); - } - } -} \ 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 1d8b763..21397d2 100644 --- a/Assets/YooAsset/Runtime/PatchSystem/PlayMode/HostPlayModeImpl.cs +++ b/Assets/YooAsset/Runtime/PatchSystem/PlayMode/HostPlayModeImpl.cs @@ -1,6 +1,7 @@ using System; using System.Collections; using System.Collections.Generic; +using System.IO; namespace YooAsset { @@ -69,6 +70,35 @@ namespace YooAsset return LocalPatchManifest.ResourceVersion; } + /// + /// 清空未被使用的缓存文件 + /// + public void ClearUnusedCacheFiles() + { + string cacheFolderPath = SandboxHelper.GetCacheFolderPath(); + if (Directory.Exists(cacheFolderPath) == false) + return; + + DirectoryInfo directoryInfo = new DirectoryInfo(cacheFolderPath); + foreach (FileInfo fileInfo in directoryInfo.GetFiles()) + { + bool used = false; + foreach (var patchBundle in LocalPatchManifest.BundleList) + { + if (fileInfo.Name == patchBundle.Hash) + { + used = true; + break; + } + } + if(used == false) + { + YooLogger.Log($"Delete unused cache file : {fileInfo.Name}"); + File.Delete(fileInfo.FullName); + } + } + } + /// /// 创建下载器 /// diff --git a/Assets/YooAsset/Runtime/Services/LocationServices/LocationServicesHelper.cs b/Assets/YooAsset/Runtime/Services/LocationServices/LocationServicesHelper.cs index f144ade..d16f5f9 100644 --- a/Assets/YooAsset/Runtime/Services/LocationServices/LocationServicesHelper.cs +++ b/Assets/YooAsset/Runtime/Services/LocationServices/LocationServicesHelper.cs @@ -5,17 +5,17 @@ namespace YooAsset { internal static class LocationServicesHelper { - private static System.Type AssetBundleGrouperSettingHelperClassType; - + private static System.Type _classType; + public static void InitEditorPlayMode(bool enableAddressable) { - AssetBundleGrouperSettingHelperClassType = Assembly.Load("YooAsset.Editor").GetType("YooAsset.Editor.AssetBundleGrouperRuntimeSupport"); - InvokePublicStaticMethod(AssetBundleGrouperSettingHelperClassType, "InitEditorPlayMode", enableAddressable); + _classType = Assembly.Load("YooAsset.Editor").GetType("YooAsset.Editor.AssetBundleGrouperRuntimeSupport"); + InvokePublicStaticMethod(_classType, "InitEditorPlayMode", enableAddressable); } public static string ConvertLocationToAssetPath(string location) { - return (string)InvokePublicStaticMethod(AssetBundleGrouperSettingHelperClassType, "ConvertLocationToAssetPath", location); - } + return (string)InvokePublicStaticMethod(_classType, "ConvertLocationToAssetPath", location); + } private static object InvokePublicStaticMethod(System.Type type, string method, params object[] parameters) { var methodInfo = type.GetMethod(method, BindingFlags.Public | BindingFlags.Static); diff --git a/Assets/YooAsset/Runtime/Utility/YooHelper.cs b/Assets/YooAsset/Runtime/Utility/YooHelper.cs index 630979c..f9cfdb4 100644 --- a/Assets/YooAsset/Runtime/Utility/YooHelper.cs +++ b/Assets/YooAsset/Runtime/Utility/YooHelper.cs @@ -80,63 +80,42 @@ namespace YooAsset /// internal static class SandboxHelper { - private const string StrCacheFileName = "Cache.bytes"; - private const string StrCacheFolderName = "CacheFiles"; + private const string CacheFolderName = "CacheFiles"; /// - /// 清空沙盒目录 + /// 删除沙盒总目录 /// - public static void ClearSandbox() + public static void DeleteSandbox() { string directoryPath = PathHelper.MakePersistentLoadPath(string.Empty); if (Directory.Exists(directoryPath)) Directory.Delete(directoryPath, true); } - /// - /// 删除沙盒内的缓存文件 - /// - public static void DeleteSandboxCacheFile() - { - string filePath = GetSandboxCacheFilePath(); - if (File.Exists(filePath)) - File.Delete(filePath); - } - /// /// 删除沙盒内的缓存文件夹 /// - public static void DeleteSandboxCacheFolder() + public static void DeleteCacheFolder() { - string directoryPath = PathHelper.MakePersistentLoadPath(StrCacheFolderName); + string directoryPath = GetCacheFolderPath(); if (Directory.Exists(directoryPath)) Directory.Delete(directoryPath, true); } - /// - /// 获取沙盒内缓存文件的路径 + /// 获取缓存文件夹路径 /// - public static string GetSandboxCacheFilePath() + public static string GetCacheFolderPath() { - return PathHelper.MakePersistentLoadPath(StrCacheFileName); - } - - /// - /// 检测沙盒内缓存文件是否存在 - /// - public static bool CheckSandboxCacheFileExist() - { - string filePath = GetSandboxCacheFilePath(); - return File.Exists(filePath); + return PathHelper.MakePersistentLoadPath(CacheFolderName); } /// /// 获取缓存文件的存储路径 /// - public static string MakeSandboxCacheFilePath(string fileName) + public static string MakeCacheFilePath(string fileName) { - return PathHelper.MakePersistentLoadPath($"{StrCacheFolderName}/{fileName}"); + return PathHelper.MakePersistentLoadPath($"{CacheFolderName}/{fileName}"); } } @@ -155,7 +134,7 @@ namespace YooAsset foreach (var patchBundle in appPatchManifest.BundleList) { // 如果已经在沙盒内 - string filePath = SandboxHelper.MakeSandboxCacheFilePath(patchBundle.Hash); + string filePath = SandboxHelper.MakeCacheFilePath(patchBundle.Hash); if (System.IO.File.Exists(filePath)) continue; diff --git a/Assets/YooAsset/Runtime/YooAssets.cs b/Assets/YooAsset/Runtime/YooAssets.cs index 251ae27..04c15ca 100644 --- a/Assets/YooAsset/Runtime/YooAssets.cs +++ b/Assets/YooAsset/Runtime/YooAssets.cs @@ -675,22 +675,37 @@ namespace YooAsset #region 沙盒相关 /// - /// 清空沙盒目录 - /// 注意:可以使用该方法修复我们本地的客户端 - /// - public static void ClearSandbox() - { - YooLogger.Warning("Clear sandbox."); - SandboxHelper.ClearSandbox(); - } - - /// - /// 获取沙盒文件夹的路径 + /// 获取沙盒的根路径 /// public static string GetSandboxRoot() { return PathHelper.MakePersistentRootPath(); } + + /// + /// 清空沙盒目录 + /// + public static void ClearSandbox() + { + SandboxHelper.DeleteSandbox(); + } + + /// + /// 清空所有的缓存文件 + /// + public static void ClearAllCacheFiles() + { + SandboxHelper.DeleteCacheFolder(); + } + + /// + /// 清空未被使用的缓存文件 + /// + public static void ClearUnusedCacheFiles() + { + if (_playMode == EPlayMode.HostPlayMode) + _hostPlayModeImpl.ClearUnusedCacheFiles(); + } #endregion #region 内部方法