diff --git a/GFramework.Godot/data/GodotResourceRepository.cs b/GFramework.Godot/data/GodotResourceRepository.cs index 5119f71..e4801d6 100644 --- a/GFramework.Godot/data/GodotResourceRepository.cs +++ b/GFramework.Godot/data/GodotResourceRepository.cs @@ -12,6 +12,8 @@ // limitations under the License. using GFramework.Core.Abstractions.bases; +using GFramework.Core.Abstractions.logging; +using GFramework.Core.logging; using Godot; namespace GFramework.Godot.data; @@ -27,6 +29,9 @@ public class GodotResourceRepository where TResource : Resource, IHasKey where TKey : notnull { + private static readonly ILogger Log = + LoggerFactoryResolver.Provider.CreateLogger(nameof(GodotResourceRepository)); + /// /// 内部存储字典,用于保存键值对形式的资源 /// @@ -72,7 +77,7 @@ public class GodotResourceRepository /// /// 包含所有资源的只读集合 public IReadOnlyCollection GetAll() - => _storage.Values; + => _storage.Values.ToArray(); /// /// 检查是否包含指定键的资源 @@ -100,11 +105,47 @@ public class GodotResourceRepository => _storage.Clear(); /// - /// 从指定路径集合加载资源到仓储中 + /// 从指定路径集合加载资源(非递归) /// - /// 资源文件路径的集合 + /// 资源文件路径集合 + public void LoadFromPath(IEnumerable paths) + { + LoadFromPathInternal(paths, recursive: false); + } + + /// + /// 从指定路径数组加载资源(非递归) + /// + /// 资源文件路径数组 + public void LoadFromPath(params string[] paths) + { + LoadFromPathInternal(paths, recursive: false); + } + + /// + /// 递归从指定路径集合加载资源 + /// + /// 资源文件路径集合 + public void LoadFromPathRecursive(IEnumerable paths) + { + LoadFromPathInternal(paths, recursive: true); + } + + /// + /// 递归从指定路径数组加载资源 + /// + /// 资源文件路径数组 + public void LoadFromPathRecursive(params string[] paths) + { + LoadFromPathInternal(paths, recursive: true); + } + + /// + /// 内部方法,根据路径集合和递归标志加载资源 + /// + /// 资源文件路径集合 /// 是否递归加载子目录中的资源 - public void LoadFromPath(IEnumerable paths, bool recursive = false) + private void LoadFromPathInternal(IEnumerable paths, bool recursive) { foreach (var path in paths) { @@ -112,17 +153,6 @@ public class GodotResourceRepository } } - /// - /// 从指定路径数组加载资源到仓储中 - /// 提供便捷的参数数组重载方法 - /// - /// 是否递归加载子目录中的资源 - /// 资源文件路径的参数数组 - public void LoadFromPath(bool recursive = false, params string[] paths) - { - LoadFromPath(paths, recursive); - } - /// /// 从单个路径加载资源 /// 遍历目录中的所有.tres和.res文件并加载为资源 @@ -131,54 +161,67 @@ public class GodotResourceRepository /// 是否递归加载子目录中的资源 private void LoadSinglePath(string path, bool recursive) { - // 打开目录访问对象 + // 尝试打开指定路径的目录 var dir = DirAccess.Open(path); if (dir == null) { - GD.PushWarning($"Path not found: {path}"); + Log.Warn($"Path not found: {path}"); return; } // 开始遍历目录 dir.ListDirBegin(); - + // 循环读取目录中的每个条目 while (true) { var entry = dir.GetNext(); if (string.IsNullOrEmpty(entry)) break; - - var fullPath = $"{path}/{entry}"; - - // 处理目录项 - if (dir.CurrentIsDir()) - { - // 递归处理子目录(排除.和..目录) - if (recursive && entry != "." && entry != "..") - { - LoadSinglePath(fullPath, true); - } - - continue; - } - - // 只处理.tres和.res文件 - if (!entry.EndsWith(".tres") && !entry.EndsWith(".res")) - continue; - - // 加载资源文件 - var resource = GD.Load(fullPath); - - if (resource == null) - { - GD.PushWarning($"Failed to load resource: {fullPath}"); - continue; - } - - Add(resource.Key, resource); + // 跳过当前目录和父目录的特殊条目 + if (entry is not ("." or "..")) + ProcessEntry(path, entry, dir.CurrentIsDir(), recursive); } // 结束目录遍历 dir.ListDirEnd(); } + + /// + /// 处理目录中的单个条目 + /// 如果是目录且启用递归则继续深入处理,如果是资源文件则加载到存储中 + /// + /// 基础路径 + /// 当前处理的条目名称 + /// 当前条目是否为目录 + /// 是否递归处理子目录 + private void ProcessEntry(string basePath, string entry, bool isDir, bool recursive) + { + // 构建完整的文件路径 + var fullPath = $"{basePath}/{entry}"; + + // 处理目录条目 + if (isDir) + { + // 如果启用递归,则递归处理子目录 + if (recursive) + LoadSinglePath(fullPath, true); + return; + } + + // 只处理.tres和.res扩展名的资源文件 + if (!entry.EndsWith(".tres") && !entry.EndsWith(".res")) + return; + + // 加载资源文件 + var resource = GD.Load(fullPath); + if (resource == null) + { + Log.Warn($"Failed to load resource: {fullPath}"); + return; + } + + // 将资源添加到存储中,如果键已存在则记录警告 + if (!_storage.TryAdd(resource.Key, resource)) + Log.Warn($"Duplicate key detected: {resource.Key}"); + } } \ No newline at end of file diff --git a/GFramework.Godot/data/IResourceRepository.cs b/GFramework.Godot/data/IResourceRepository.cs index c510e67..99b7227 100644 --- a/GFramework.Godot/data/IResourceRepository.cs +++ b/GFramework.Godot/data/IResourceRepository.cs @@ -25,17 +25,26 @@ namespace GFramework.Godot.data; public interface IResourceRepository : IRepository where TResource : Resource { /// - /// 从指定路径加载资源到仓储中 + /// 从指定路径集合加载资源 /// - /// 资源文件的路径集合 - /// 是否递归加载子目录中的资源 - void LoadFromPath(IEnumerable paths, bool recursive = false); + /// 资源文件路径集合 + void LoadFromPath(IEnumerable paths); /// - /// 从指定路径数组加载资源到仓储中 - /// 提供便捷的参数数组重载方法 + /// 从指定路径数组加载资源 /// - /// 是否递归加载子目录中的资源 - /// 资源文件路径的参数数组 - void LoadFromPath(bool recursive = false, params string[] paths); + /// 资源文件路径数组 + void LoadFromPath(params string[] paths); + + /// + /// 递归从指定路径集合加载资源 + /// + /// 资源文件路径集合 + void LoadFromPathRecursive(IEnumerable paths); + + /// + /// 递归从指定路径数组加载资源 + /// + /// 资源文件路径数组 + void LoadFromPathRecursive(params string[] paths); } \ No newline at end of file