From f8f2dd8faf2a99420a96601203e11b2df5e517df Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=BD=95=E5=86=A0=E5=B3=B0?= <33317070@qq.com> Date: Mon, 18 May 2026 18:58:56 +0800 Subject: [PATCH] feat : add editor bundle cache markers --- .../EditorBundleCache/EditorBundleCache.cs | 37 +++++- .../EditorBundleCacheConsts.cs | 24 ++++ .../EditorBundleCacheConsts.cs.meta | 11 ++ .../EditorBundleCacheEntry.cs | 40 +++++- .../Operations/EBCClearCacheOperation.cs | 1 - .../Operations/EBCInitializeOperation.cs | 61 +++++++++- .../Operations/EBCWriteCacheOperation.cs | 48 +++++++- .../Operations/Internal.meta | 8 ++ .../Internal/ScanMarkerFilesOperation.cs | 114 ++++++++++++++++++ .../Internal/ScanMarkerFilesOperation.cs.meta | 11 ++ .../EditorBundleCache/ScanFileInfo.cs | 30 +++++ .../EditorBundleCache/ScanFileInfo.cs.meta | 11 ++ .../Internal/SearchCacheFilesOperation.cs | 28 ++--- .../SandboxBundleCache/SandboxBundleCache.cs | 41 ++++--- .../SandboxBundleCacheConsts.cs | 5 + .../EditorFileSystem/EditorFileSystem.cs | 12 +- .../EditorFileSystemConsts.cs | 14 +++ .../EditorFileSystemConsts.cs.meta | 11 ++ 18 files changed, 466 insertions(+), 41 deletions(-) create mode 100644 Assets/YooAsset/Runtime/BundleCache/Services/EditorBundleCache/EditorBundleCacheConsts.cs create mode 100644 Assets/YooAsset/Runtime/BundleCache/Services/EditorBundleCache/EditorBundleCacheConsts.cs.meta create mode 100644 Assets/YooAsset/Runtime/BundleCache/Services/EditorBundleCache/Operations/Internal.meta create mode 100644 Assets/YooAsset/Runtime/BundleCache/Services/EditorBundleCache/Operations/Internal/ScanMarkerFilesOperation.cs create mode 100644 Assets/YooAsset/Runtime/BundleCache/Services/EditorBundleCache/Operations/Internal/ScanMarkerFilesOperation.cs.meta create mode 100644 Assets/YooAsset/Runtime/BundleCache/Services/EditorBundleCache/ScanFileInfo.cs create mode 100644 Assets/YooAsset/Runtime/BundleCache/Services/EditorBundleCache/ScanFileInfo.cs.meta create mode 100644 Assets/YooAsset/Runtime/FileSystem/Services/EditorFileSystem/EditorFileSystemConsts.cs create mode 100644 Assets/YooAsset/Runtime/FileSystem/Services/EditorFileSystem/EditorFileSystemConsts.cs.meta diff --git a/Assets/YooAsset/Runtime/BundleCache/Services/EditorBundleCache/EditorBundleCache.cs b/Assets/YooAsset/Runtime/BundleCache/Services/EditorBundleCache/EditorBundleCache.cs index 97696844..636ac358 100644 --- a/Assets/YooAsset/Runtime/BundleCache/Services/EditorBundleCache/EditorBundleCache.cs +++ b/Assets/YooAsset/Runtime/BundleCache/Services/EditorBundleCache/EditorBundleCache.cs @@ -80,7 +80,7 @@ namespace YooAsset PackageName = packageName; RootPath = rootPath; Config = config; - IsReadOnly = true; + IsReadOnly = config.VirtualDownloadMode == false; } /// @@ -208,8 +208,43 @@ namespace YooAsset if (_cacheEntries.TryGetValue(bundleGuid, out EditorBundleCacheEntry entry)) { _cacheEntries.Remove(bundleGuid); + entry.Delete(); } } + + /// + /// 获取 marker 文件路径 + /// + /// 资源包描述 + /// marker 文件的完整路径 + internal string GetMarkerFilePath(PackageBundle bundle) + { + string bundleGuid = bundle.BundleGuid; + string hashFolder = GetHashFolderName(bundleGuid); + return PathUtility.Combine(RootPath, hashFolder, bundleGuid, EditorBundleCacheConsts.MarkerFileName); + } + + /// + /// 获取 marker 临时文件路径 + /// + /// 资源包描述 + /// marker 临时文件的完整路径 + internal string GetMarkerTempFilePath(PackageBundle bundle) + { + string bundleGuid = bundle.BundleGuid; + string hashFolder = GetHashFolderName(bundleGuid); + return PathUtility.Combine(RootPath, hashFolder, bundleGuid, EditorBundleCacheConsts.MarkerTempFileName); + } + + private string GetHashFolderName(string bundleGuid) + { + if (string.IsNullOrEmpty(bundleGuid)) + throw new YooInternalException("Bundle GUID is null or empty."); + + if (bundleGuid.Length <= EditorBundleCacheConsts.HashFolderNameLength) + return bundleGuid; + return bundleGuid.Substring(0, EditorBundleCacheConsts.HashFolderNameLength); + } #endregion } } diff --git a/Assets/YooAsset/Runtime/BundleCache/Services/EditorBundleCache/EditorBundleCacheConsts.cs b/Assets/YooAsset/Runtime/BundleCache/Services/EditorBundleCache/EditorBundleCacheConsts.cs new file mode 100644 index 00000000..08eee050 --- /dev/null +++ b/Assets/YooAsset/Runtime/BundleCache/Services/EditorBundleCache/EditorBundleCacheConsts.cs @@ -0,0 +1,24 @@ + +namespace YooAsset +{ + /// + /// 编辑器文件缓存常量定义 + /// + internal static class EditorBundleCacheConsts + { + /// + /// 标记文件名称 + /// + public const string MarkerFileName = "__marker"; + + /// + /// 标记临时文件名称 + /// + public const string MarkerTempFileName = "__marker.tmp"; + + /// + /// 哈希分片目录前缀长度 + /// + public const int HashFolderNameLength = 2; + } +} diff --git a/Assets/YooAsset/Runtime/BundleCache/Services/EditorBundleCache/EditorBundleCacheConsts.cs.meta b/Assets/YooAsset/Runtime/BundleCache/Services/EditorBundleCache/EditorBundleCacheConsts.cs.meta new file mode 100644 index 00000000..3174031b --- /dev/null +++ b/Assets/YooAsset/Runtime/BundleCache/Services/EditorBundleCache/EditorBundleCacheConsts.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: dc5ebd7b9a6b77145b004dadfe7ed587 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/YooAsset/Runtime/BundleCache/Services/EditorBundleCache/EditorBundleCacheEntry.cs b/Assets/YooAsset/Runtime/BundleCache/Services/EditorBundleCache/EditorBundleCacheEntry.cs index 11ca1f2b..ff1eff51 100644 --- a/Assets/YooAsset/Runtime/BundleCache/Services/EditorBundleCache/EditorBundleCacheEntry.cs +++ b/Assets/YooAsset/Runtime/BundleCache/Services/EditorBundleCache/EditorBundleCacheEntry.cs @@ -1,3 +1,5 @@ +using System; +using System.IO; namespace YooAsset { @@ -11,13 +13,47 @@ namespace YooAsset /// public string BundleGuid { get; private set; } + /// + /// 标记文件路径 + /// + public string MarkerFilePath { get; private set; } + /// /// 创建编辑器文件缓存条目实例 /// /// 资源包唯一标识 - public EditorBundleCacheEntry(string bundleGuid) + /// 标记文件路径 + public EditorBundleCacheEntry(string bundleGuid, string markerFilePath) { BundleGuid = bundleGuid; + MarkerFilePath = markerFilePath; + } + + /// + /// 删除缓存文件夹及其所有内容 + /// + /// 删除是否成功 + public bool Delete() + { + try + { + string directory = Path.GetDirectoryName(MarkerFilePath); + var directoryInfo = new DirectoryInfo(directory); + if (directoryInfo.Exists) + { + directoryInfo.Delete(true); + return true; + } + else + { + return false; + } + } + catch (Exception ex) + { + YooLogger.LogError($"Failed to delete editor cache file: {ex.Message}."); + return false; + } } } -} \ No newline at end of file +} diff --git a/Assets/YooAsset/Runtime/BundleCache/Services/EditorBundleCache/Operations/EBCClearCacheOperation.cs b/Assets/YooAsset/Runtime/BundleCache/Services/EditorBundleCache/Operations/EBCClearCacheOperation.cs index 2d2c20dc..d9573149 100644 --- a/Assets/YooAsset/Runtime/BundleCache/Services/EditorBundleCache/Operations/EBCClearCacheOperation.cs +++ b/Assets/YooAsset/Runtime/BundleCache/Services/EditorBundleCache/Operations/EBCClearCacheOperation.cs @@ -46,7 +46,6 @@ namespace YooAsset { var cacheEntries = _fileCache.GetAllEntries(); EvictionResult clearResult = _policy.SelectEvictionTargets(cacheEntries, _options); - if (clearResult.Succeeded == false) { _steps = ESteps.Done; diff --git a/Assets/YooAsset/Runtime/BundleCache/Services/EditorBundleCache/Operations/EBCInitializeOperation.cs b/Assets/YooAsset/Runtime/BundleCache/Services/EditorBundleCache/Operations/EBCInitializeOperation.cs index 4e9cfe35..0f148ff5 100644 --- a/Assets/YooAsset/Runtime/BundleCache/Services/EditorBundleCache/Operations/EBCInitializeOperation.cs +++ b/Assets/YooAsset/Runtime/BundleCache/Services/EditorBundleCache/Operations/EBCInitializeOperation.cs @@ -1,4 +1,3 @@ - namespace YooAsset { /// @@ -6,7 +5,17 @@ namespace YooAsset /// internal sealed class EBCInitializeOperation : BCInitializeOperation { + private enum ESteps + { + None, + ScanMarkerFiles, + AddCacheEntries, + Done, + } + private readonly EditorBundleCache _fileCache; + private ScanMarkerFilesOperation _scanMarkerFilesOp; + private ESteps _steps = ESteps.None; public EBCInitializeOperation(EditorBundleCache cache) { @@ -14,10 +23,58 @@ namespace YooAsset } protected override void InternalStart() { - SetResult(); + if (_fileCache.Config.VirtualDownloadMode == false) + { + SetResult(); + return; + } + + _steps = ESteps.ScanMarkerFiles; } protected override void InternalUpdate() { + if (_steps == ESteps.None || _steps == ESteps.Done) + return; + + if (_steps == ESteps.ScanMarkerFiles) + { + if (_scanMarkerFilesOp == null) + { + _scanMarkerFilesOp = new ScanMarkerFilesOperation(_fileCache); + _scanMarkerFilesOp.StartOperation(); + AddChildOperation(_scanMarkerFilesOp); + } + + _scanMarkerFilesOp.UpdateOperation(); + Progress = _scanMarkerFilesOp.Progress; + if (_scanMarkerFilesOp.IsDone == false) + return; + + if (_scanMarkerFilesOp.Status == EOperationStatus.Succeeded) + { + _steps = ESteps.AddCacheEntries; + } + else + { + _steps = ESteps.Done; + SetError(_scanMarkerFilesOp.Error); + } + } + + if (_steps == ESteps.AddCacheEntries) + { + foreach (var fileInfo in _scanMarkerFilesOp.Result) + { + if (_fileCache.IsCached(fileInfo.BundleGuid) == false) + { + var cacheEntry = new EditorBundleCacheEntry(fileInfo.BundleGuid, fileInfo.MarkerFilePath); + _fileCache.AddEntry(fileInfo.BundleGuid, cacheEntry); + } + } + + _steps = ESteps.Done; + SetResult(); + } } } } diff --git a/Assets/YooAsset/Runtime/BundleCache/Services/EditorBundleCache/Operations/EBCWriteCacheOperation.cs b/Assets/YooAsset/Runtime/BundleCache/Services/EditorBundleCache/Operations/EBCWriteCacheOperation.cs index 35889e97..55a5f353 100644 --- a/Assets/YooAsset/Runtime/BundleCache/Services/EditorBundleCache/Operations/EBCWriteCacheOperation.cs +++ b/Assets/YooAsset/Runtime/BundleCache/Services/EditorBundleCache/Operations/EBCWriteCacheOperation.cs @@ -1,3 +1,5 @@ +using System; +using System.IO; namespace YooAsset { @@ -52,7 +54,38 @@ namespace YooAsset if (_steps == ESteps.CacheFile) { - var cacheEntry = new EditorBundleCacheEntry(_options.Bundle.BundleGuid); + string markerFilePath = _fileCache.GetMarkerFilePath(_options.Bundle); + string markerTempPath = _fileCache.GetMarkerTempFilePath(_options.Bundle); + + try + { + // 阶段A:准备目标目录,清理可能存在的残留临时文件 + FileUtility.EnsureParentDirectoryExists(markerFilePath); + DeleteFileSafely(markerTempPath); + + // 阶段B:写入临时文件,内容仅用于人工调试 + string debugContent = $"BundleName={_options.Bundle.BundleName}\n" + + $"BundleGuid={_options.Bundle.BundleGuid}\n"; + File.WriteAllText(markerTempPath, debugContent); + + // 阶段C:原子提交 + if (File.Exists(markerFilePath)) + File.Delete(markerFilePath); + File.Move(markerTempPath, markerFilePath); + } + catch (Exception ex) + { + _steps = ESteps.Done; + SetError($"Failed to write marker file: {ex.Message}."); + YooLogger.LogError(Error); + + // 回滚:清理临时文件,正式文件不受影响 + DeleteFileSafely(markerTempPath); + return; + } + + // 阶段D:注册内存缓存条目 + var cacheEntry = new EditorBundleCacheEntry(_options.Bundle.BundleGuid, markerFilePath); _fileCache.AddEntry(_options.Bundle.BundleGuid, cacheEntry); _steps = ESteps.Done; SetResult(); @@ -62,5 +95,18 @@ namespace YooAsset { ExecuteBatch(); } + + private static void DeleteFileSafely(string filePath) + { + try + { + if (File.Exists(filePath)) + File.Delete(filePath); + } + catch (Exception ex) + { + YooLogger.LogWarning($"Failed to delete file '{filePath}': {ex.Message}."); + } + } } } diff --git a/Assets/YooAsset/Runtime/BundleCache/Services/EditorBundleCache/Operations/Internal.meta b/Assets/YooAsset/Runtime/BundleCache/Services/EditorBundleCache/Operations/Internal.meta new file mode 100644 index 00000000..d92b8d56 --- /dev/null +++ b/Assets/YooAsset/Runtime/BundleCache/Services/EditorBundleCache/Operations/Internal.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: 2e9a70495d8ca514caa7647851844101 +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/YooAsset/Runtime/BundleCache/Services/EditorBundleCache/Operations/Internal/ScanMarkerFilesOperation.cs b/Assets/YooAsset/Runtime/BundleCache/Services/EditorBundleCache/Operations/Internal/ScanMarkerFilesOperation.cs new file mode 100644 index 00000000..eff0a303 --- /dev/null +++ b/Assets/YooAsset/Runtime/BundleCache/Services/EditorBundleCache/Operations/Internal/ScanMarkerFilesOperation.cs @@ -0,0 +1,114 @@ +using System.IO; +using System.Collections.Generic; + +namespace YooAsset +{ + /// + /// 扫描标记文件操作 + /// + internal sealed class ScanMarkerFilesOperation : AsyncOperationBase + { + private enum ESteps + { + None, + Prepare, + ScanFiles, + Done, + } + + private readonly EditorBundleCache _fileCache; + private IEnumerator _shardEnumerator = null; + private double _scanStartTime; + private ESteps _steps = ESteps.None; + + /// + /// 扫描到的标记件信息 + /// + public readonly List Result = new List(5000); + + /// + /// 创建操作实例 + /// + /// 编辑器文件缓存系统 + internal ScanMarkerFilesOperation(EditorBundleCache fileCache) + { + _fileCache = fileCache; + } + protected override void InternalStart() + { + _steps = ESteps.Prepare; + } + protected override void InternalUpdate() + { + if (_steps == ESteps.None || _steps == ESteps.Done) + return; + + if (_steps == ESteps.Prepare) + { + if (Directory.Exists(_fileCache.RootPath)) + { + var directories = Directory.EnumerateDirectories(_fileCache.RootPath); + _shardEnumerator = directories.GetEnumerator(); + _scanStartTime = TimeUtility.RealtimeSinceStartup; + _steps = ESteps.ScanFiles; + } + else + { + _steps = ESteps.Done; + SetResult(); + } + } + + if (_steps == ESteps.ScanFiles) + { + if (ScanFiles()) + return; + + _shardEnumerator.Dispose(); + _shardEnumerator = null; + + _steps = ESteps.Done; + SetResult(); + double costTime = TimeUtility.RealtimeSinceStartup - _scanStartTime; + YooLogger.Log($"Marker file scan completed in {costTime:f1} seconds. Found {Result.Count} marker files."); + } + } + protected override void InternalDispose() + { + if (_shardEnumerator != null) + { + _shardEnumerator.Dispose(); + _shardEnumerator = null; + } + } + + private bool ScanFiles() + { + bool hasMore; + while (true) + { + hasMore = _shardEnumerator.MoveNext(); + if (hasMore == false) + break; + + string shardFolder = _shardEnumerator.Current; + var childDirectories = Directory.EnumerateDirectories(shardFolder); + foreach (string childDirectory in childDirectories) + { + string bundleGuid = Path.GetFileName(childDirectory); + string markerFilePath = PathUtility.Combine(childDirectory, EditorBundleCacheConsts.MarkerFileName); + if (File.Exists(markerFilePath)) + { + var fileInfo = new ScanFileInfo(bundleGuid, markerFilePath); + Result.Add(fileInfo); + } + } + + if (IsBusy) + break; + } + + return hasMore; + } + } +} diff --git a/Assets/YooAsset/Runtime/BundleCache/Services/EditorBundleCache/Operations/Internal/ScanMarkerFilesOperation.cs.meta b/Assets/YooAsset/Runtime/BundleCache/Services/EditorBundleCache/Operations/Internal/ScanMarkerFilesOperation.cs.meta new file mode 100644 index 00000000..b9fcaa3e --- /dev/null +++ b/Assets/YooAsset/Runtime/BundleCache/Services/EditorBundleCache/Operations/Internal/ScanMarkerFilesOperation.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: dc7b3752dfb973447bdae44ea8722995 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/YooAsset/Runtime/BundleCache/Services/EditorBundleCache/ScanFileInfo.cs b/Assets/YooAsset/Runtime/BundleCache/Services/EditorBundleCache/ScanFileInfo.cs new file mode 100644 index 00000000..3cf99197 --- /dev/null +++ b/Assets/YooAsset/Runtime/BundleCache/Services/EditorBundleCache/ScanFileInfo.cs @@ -0,0 +1,30 @@ + +namespace YooAsset +{ + /// + /// 扫描到的标记文件信息 + /// + internal class ScanFileInfo + { + /// + /// 资源包唯一标识 + /// + public string BundleGuid { get; private set; } + + /// + /// 标记文件路径 + /// + public string MarkerFilePath { get; private set; } + + /// + /// 创建扫描文件信息实例 + /// + /// 资源包唯一标识 + /// 标记文件路径 + public ScanFileInfo(string bundleGuid, string markerFilePath) + { + BundleGuid = bundleGuid; + MarkerFilePath = markerFilePath; + } + } +} diff --git a/Assets/YooAsset/Runtime/BundleCache/Services/EditorBundleCache/ScanFileInfo.cs.meta b/Assets/YooAsset/Runtime/BundleCache/Services/EditorBundleCache/ScanFileInfo.cs.meta new file mode 100644 index 00000000..19e702c1 --- /dev/null +++ b/Assets/YooAsset/Runtime/BundleCache/Services/EditorBundleCache/ScanFileInfo.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: f4a3717e25e79a14485f85e83794dd6a +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/YooAsset/Runtime/BundleCache/Services/SandboxBundleCache/Operations/Internal/SearchCacheFilesOperation.cs b/Assets/YooAsset/Runtime/BundleCache/Services/SandboxBundleCache/Operations/Internal/SearchCacheFilesOperation.cs index 54613a6c..9180729e 100644 --- a/Assets/YooAsset/Runtime/BundleCache/Services/SandboxBundleCache/Operations/Internal/SearchCacheFilesOperation.cs +++ b/Assets/YooAsset/Runtime/BundleCache/Services/SandboxBundleCache/Operations/Internal/SearchCacheFilesOperation.cs @@ -19,7 +19,7 @@ namespace YooAsset } private readonly SandboxBundleCache _fileCache; - private IEnumerator _filesEnumerator = null; + private IEnumerator _shardEnumerator = null; private double _verifyStartTime; private ESteps _steps = ESteps.None; @@ -50,7 +50,7 @@ namespace YooAsset if (Directory.Exists(_fileCache.RootPath)) { var directories = Directory.EnumerateDirectories(_fileCache.RootPath); - _filesEnumerator = directories.GetEnumerator(); + _shardEnumerator = directories.GetEnumerator(); _verifyStartTime = TimeUtility.RealtimeSinceStartup; _steps = ESteps.SearchFiles; } @@ -66,35 +66,35 @@ namespace YooAsset if (SearchFiles()) return; - _filesEnumerator.Dispose(); - _filesEnumerator = null; + _shardEnumerator.Dispose(); + _shardEnumerator = null; _steps = ESteps.Done; SetResult(); double costTime = TimeUtility.RealtimeSinceStartup - _verifyStartTime; - YooLogger.Log($"Cache file search completed in {costTime:f1} seconds."); + YooLogger.Log($"Cache file search completed in {costTime:f1} seconds. Found {Result.Count} cache files."); } } protected override void InternalDispose() { - if (_filesEnumerator != null) + if (_shardEnumerator != null) { - _filesEnumerator.Dispose(); - _filesEnumerator = null; + _shardEnumerator.Dispose(); + _shardEnumerator = null; } } private bool SearchFiles() { - bool isFindItem; + bool hasMore; while (true) { - isFindItem = _filesEnumerator.MoveNext(); - if (isFindItem == false) + hasMore = _shardEnumerator.MoveNext(); + if (hasMore == false) break; - var rootFolder = _filesEnumerator.Current; - var childDirectories = Directory.EnumerateDirectories(rootFolder); + var shardFolder = _shardEnumerator.Current; + var childDirectories = Directory.EnumerateDirectories(shardFolder); foreach (var childDirectory in childDirectories) { string bundleGuid = Path.GetFileName(childDirectory); @@ -113,7 +113,7 @@ namespace YooAsset break; } - return isFindItem; + return hasMore; } } } diff --git a/Assets/YooAsset/Runtime/BundleCache/Services/SandboxBundleCache/SandboxBundleCache.cs b/Assets/YooAsset/Runtime/BundleCache/Services/SandboxBundleCache/SandboxBundleCache.cs index 706e4a32..eaba5ea1 100644 --- a/Assets/YooAsset/Runtime/BundleCache/Services/SandboxBundleCache/SandboxBundleCache.cs +++ b/Assets/YooAsset/Runtime/BundleCache/Services/SandboxBundleCache/SandboxBundleCache.cs @@ -46,7 +46,6 @@ namespace YooAsset } } - private const int HashFolderNameLength = 2; private readonly Dictionary _cacheEntries = new Dictionary(10000); private readonly Dictionary _dataFilePathMapping = new Dictionary(10000); private readonly Dictionary _infoFilePathMapping = new Dictionary(10000); @@ -196,11 +195,12 @@ namespace YooAsset /// 数据文件的完整路径 internal string GetDataFilePath(PackageBundle bundle) { - if (_dataFilePathMapping.TryGetValue(bundle.BundleGuid, out string filePath) == false) + string bundleGuid = bundle.BundleGuid; + if (_dataFilePathMapping.TryGetValue(bundleGuid, out string filePath) == false) { - string folderName = GetHashFolderName(bundle.FileHash); - filePath = PathUtility.Combine(RootPath, folderName, bundle.BundleGuid, SandboxBundleCacheConsts.BundleDataFileName); - _dataFilePathMapping.Add(bundle.BundleGuid, filePath); + string folderName = GetHashFolderName(bundleGuid); + filePath = PathUtility.Combine(RootPath, folderName, bundleGuid, SandboxBundleCacheConsts.BundleDataFileName); + _dataFilePathMapping.Add(bundleGuid, filePath); } return filePath; } @@ -212,11 +212,12 @@ namespace YooAsset /// 信息文件的完整路径 internal string GetInfoFilePath(PackageBundle bundle) { - if (_infoFilePathMapping.TryGetValue(bundle.BundleGuid, out string filePath) == false) + string bundleGuid = bundle.BundleGuid; + if (_infoFilePathMapping.TryGetValue(bundleGuid, out string filePath) == false) { - string folderName = GetHashFolderName(bundle.FileHash); - filePath = PathUtility.Combine(RootPath, folderName, bundle.BundleGuid, SandboxBundleCacheConsts.BundleInfoFileName); - _infoFilePathMapping.Add(bundle.BundleGuid, filePath); + string folderName = GetHashFolderName(bundleGuid); + filePath = PathUtility.Combine(RootPath, folderName, bundleGuid, SandboxBundleCacheConsts.BundleInfoFileName); + _infoFilePathMapping.Add(bundleGuid, filePath); } return filePath; } @@ -228,8 +229,9 @@ namespace YooAsset /// 数据临时文件的完整路径 internal string GetDataTempFilePath(PackageBundle bundle) { - string folderName = GetHashFolderName(bundle.FileHash); - return PathUtility.Combine(RootPath, folderName, bundle.BundleGuid, SandboxBundleCacheConsts.BundleDataTempFileName); + string bundleGuid = bundle.BundleGuid; + string folderName = GetHashFolderName(bundleGuid); + return PathUtility.Combine(RootPath, folderName, bundleGuid, SandboxBundleCacheConsts.BundleDataTempFileName); } /// @@ -239,8 +241,9 @@ namespace YooAsset /// 信息临时文件的完整路径 internal string GetInfoTempFilePath(PackageBundle bundle) { - string folderName = GetHashFolderName(bundle.FileHash); - return PathUtility.Combine(RootPath, folderName, bundle.BundleGuid, SandboxBundleCacheConsts.BundleInfoTempFileName); + string bundleGuid = bundle.BundleGuid; + string folderName = GetHashFolderName(bundleGuid); + return PathUtility.Combine(RootPath, folderName, bundleGuid, SandboxBundleCacheConsts.BundleInfoTempFileName); } /// @@ -297,14 +300,14 @@ namespace YooAsset } } - private string GetHashFolderName(string fileHash) + private string GetHashFolderName(string bundleGuid) { - if (string.IsNullOrEmpty(fileHash)) - throw new YooInternalException("File hash is null or empty."); + if (string.IsNullOrEmpty(bundleGuid)) + throw new YooInternalException("Bundle GUID is null or empty."); - if (fileHash.Length <= HashFolderNameLength) - return fileHash; - return fileHash.Substring(0, HashFolderNameLength); + if (bundleGuid.Length <= SandboxBundleCacheConsts.HashFolderNameLength) + return bundleGuid; + return bundleGuid.Substring(0, SandboxBundleCacheConsts.HashFolderNameLength); } #endregion } diff --git a/Assets/YooAsset/Runtime/BundleCache/Services/SandboxBundleCache/SandboxBundleCacheConsts.cs b/Assets/YooAsset/Runtime/BundleCache/Services/SandboxBundleCache/SandboxBundleCacheConsts.cs index ea71fe8a..b370c493 100644 --- a/Assets/YooAsset/Runtime/BundleCache/Services/SandboxBundleCache/SandboxBundleCacheConsts.cs +++ b/Assets/YooAsset/Runtime/BundleCache/Services/SandboxBundleCache/SandboxBundleCacheConsts.cs @@ -40,5 +40,10 @@ namespace YooAsset /// 信息文件预期大小(字节) /// public const int InfoFileExpectedSize = 36; + + /// + /// 哈希分片目录前缀长度 + /// + public const int HashFolderNameLength = 2; } } \ No newline at end of file diff --git a/Assets/YooAsset/Runtime/FileSystem/Services/EditorFileSystem/EditorFileSystem.cs b/Assets/YooAsset/Runtime/FileSystem/Services/EditorFileSystem/EditorFileSystem.cs index 3cc109cc..7d71dbc1 100644 --- a/Assets/YooAsset/Runtime/FileSystem/Services/EditorFileSystem/EditorFileSystem.cs +++ b/Assets/YooAsset/Runtime/FileSystem/Services/EditorFileSystem/EditorFileSystem.cs @@ -233,7 +233,8 @@ namespace YooAsset virtualWebGLMode: VirtualWebGLMode, asyncSimulateMinFrame: AsyncSimulateMinFrame, asyncSimulateMaxFrame: AsyncSimulateMaxFrame); - BundleCache = new EditorBundleCache(packageName, _packageRoot, cacheConfig); + string cacheRoot = GetEditorBundleCacheRoot(); + BundleCache = new EditorBundleCache(packageName, cacheRoot, cacheConfig); } /// public void OnDestroy() @@ -279,6 +280,15 @@ namespace YooAsset } #region 内部方法 + /// + /// 获取编辑器缓存根目录路径 + /// + private string GetEditorBundleCacheRoot() + { + string root = YooAssetConfiguration.GetEditorCacheRoot(); + return PathUtility.Combine(root, PackageName, EditorFileSystemConsts.CacheFolderName); + } + /// /// 获取编辑器包裹版本文件路径 /// diff --git a/Assets/YooAsset/Runtime/FileSystem/Services/EditorFileSystem/EditorFileSystemConsts.cs b/Assets/YooAsset/Runtime/FileSystem/Services/EditorFileSystem/EditorFileSystemConsts.cs new file mode 100644 index 00000000..fb550082 --- /dev/null +++ b/Assets/YooAsset/Runtime/FileSystem/Services/EditorFileSystem/EditorFileSystemConsts.cs @@ -0,0 +1,14 @@ + +namespace YooAsset +{ + /// + /// 编辑器文件系统常量定义 + /// + internal static class EditorFileSystemConsts + { + /// + /// 编辑器缓存文件的文件夹名称 + /// + public const string CacheFolderName = "SimulateCache"; + } +} diff --git a/Assets/YooAsset/Runtime/FileSystem/Services/EditorFileSystem/EditorFileSystemConsts.cs.meta b/Assets/YooAsset/Runtime/FileSystem/Services/EditorFileSystem/EditorFileSystemConsts.cs.meta new file mode 100644 index 00000000..ae3091b9 --- /dev/null +++ b/Assets/YooAsset/Runtime/FileSystem/Services/EditorFileSystem/EditorFileSystemConsts.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 983d63c1d4cc2a24bb2d048e6c2feb2b +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: