From 686163c164bc7ead852aa3f092e7b57572a376ac Mon Sep 17 00:00:00 2001
From: GeWuYou <95328647+GeWuYou@users.noreply.github.com>
Date: Mon, 23 Feb 2026 21:54:49 +0800
Subject: [PATCH 1/2] =?UTF-8?q?feat(godot):=20=E6=B7=BB=E5=8A=A0=E8=B5=84?=
=?UTF-8?q?=E6=BA=90=E4=BB=93=E5=82=A8=E5=8A=9F=E8=83=BD=E6=94=AF=E6=8C=81?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
- 新增 IHasKey 接口定义键值访问契约
- 新增 IRepository 接口提供通用数据仓储功能
- 实现 GodotResourceRepository 类支持资源的存储和加载
- 添加 IResourceRepository 接口扩展通用仓储功能
- 实现从路径批量加载 Godot 资源的功能
- 支持递归加载子目录中的资源文件
- 提供 .tres 和 .res 文件的自动识别和加载
---
GFramework.Core.Abstractions/bases/IHasKey.cs | 26 +++
GFramework.Core.Tests/ecs/EcsAdvancedTests.cs | 2 -
.../data/IRepository.cs | 68 +++++++
.../data/GodotResourceRepository.cs | 184 ++++++++++++++++++
GFramework.Godot/data/IResourceRepository.cs | 41 ++++
5 files changed, 319 insertions(+), 2 deletions(-)
create mode 100644 GFramework.Core.Abstractions/bases/IHasKey.cs
create mode 100644 GFramework.Game.Abstractions/data/IRepository.cs
create mode 100644 GFramework.Godot/data/GodotResourceRepository.cs
create mode 100644 GFramework.Godot/data/IResourceRepository.cs
diff --git a/GFramework.Core.Abstractions/bases/IHasKey.cs b/GFramework.Core.Abstractions/bases/IHasKey.cs
new file mode 100644
index 00000000..a2b09fa1
--- /dev/null
+++ b/GFramework.Core.Abstractions/bases/IHasKey.cs
@@ -0,0 +1,26 @@
+// Copyright (c) 2026 GeWuYou
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+namespace GFramework.Core.Abstractions.bases;
+
+///
+/// 定义具有键值访问能力的接口契约
+///
+/// 键的类型
+public interface IHasKey
+{
+ ///
+ /// 获取对象的键值
+ ///
+ TKey Key { get; }
+}
\ No newline at end of file
diff --git a/GFramework.Core.Tests/ecs/EcsAdvancedTests.cs b/GFramework.Core.Tests/ecs/EcsAdvancedTests.cs
index 577f7e11..53582efa 100644
--- a/GFramework.Core.Tests/ecs/EcsAdvancedTests.cs
+++ b/GFramework.Core.Tests/ecs/EcsAdvancedTests.cs
@@ -1,4 +1,3 @@
-using System.Diagnostics.CodeAnalysis;
using System.Reflection;
using Arch.Core;
using GFramework.Core.Abstractions.ecs;
@@ -14,7 +13,6 @@
namespace GFramework.Core.Tests.ecs;
[TestFixture]
-[Experimental("GFrameworkECS")]
public class EcsAdvancedTests
{
[SetUp]
diff --git a/GFramework.Game.Abstractions/data/IRepository.cs b/GFramework.Game.Abstractions/data/IRepository.cs
new file mode 100644
index 00000000..3e7724ee
--- /dev/null
+++ b/GFramework.Game.Abstractions/data/IRepository.cs
@@ -0,0 +1,68 @@
+// Copyright (c) 2026 GeWuYou
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+namespace GFramework.Game.Abstractions.data;
+
+///
+/// 定义数据仓储接口,提供键值对数据的基本操作功能
+///
+/// 键的类型
+/// 值的类型
+public interface IRepository
+{
+ ///
+ /// 添加键值对到仓储中
+ ///
+ /// 要添加的键
+ /// 要添加的值
+ void Add(TKey key, TValue value);
+
+ ///
+ /// 根据键获取对应的值
+ ///
+ /// 要查找的键
+ /// 与指定键关联的值
+ TValue Get(TKey key);
+
+ ///
+ /// 尝试根据键获取对应的值
+ ///
+ /// 要查找的键
+ /// 输出参数,如果找到则返回对应的值,否则返回默认值
+ /// 如果找到键则返回true,否则返回false
+ bool TryGet(TKey key, out TValue value);
+
+ ///
+ /// 获取仓储中的所有值
+ ///
+ /// 包含所有值的只读集合
+ IReadOnlyCollection GetAll();
+
+ ///
+ /// 检查仓储中是否包含指定的键
+ ///
+ /// 要检查的键
+ /// 如果包含该键则返回true,否则返回false
+ bool Contains(TKey key);
+
+ ///
+ /// 从仓储中移除指定键的项
+ ///
+ /// 要移除的键
+ void Remove(TKey key);
+
+ ///
+ /// 清空仓储中的所有数据
+ ///
+ void Clear();
+}
\ No newline at end of file
diff --git a/GFramework.Godot/data/GodotResourceRepository.cs b/GFramework.Godot/data/GodotResourceRepository.cs
new file mode 100644
index 00000000..5119f71e
--- /dev/null
+++ b/GFramework.Godot/data/GodotResourceRepository.cs
@@ -0,0 +1,184 @@
+// Copyright (c) 2026 GeWuYou
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+using GFramework.Core.Abstractions.bases;
+using Godot;
+
+namespace GFramework.Godot.data;
+
+///
+/// Godot资源仓储实现类,用于管理Godot资源的存储和加载。
+/// 实现了IResourceRepository接口,提供基于键的资源存取功能。
+///
+/// 资源键的类型
+/// 资源类型,必须继承自Godot.Resource并实现IHasKey接口
+public class GodotResourceRepository
+ : IResourceRepository
+ where TResource : Resource, IHasKey
+ where TKey : notnull
+{
+ ///
+ /// 内部存储字典,用于保存键值对形式的资源
+ ///
+ private readonly Dictionary _storage = new();
+
+ ///
+ /// 向仓储中添加资源
+ ///
+ /// 资源的键
+ /// 要添加的资源对象
+ /// 当键已存在时抛出异常
+ public void Add(TKey key, TResource value)
+ {
+ if (!_storage.TryAdd(key, value))
+ throw new InvalidOperationException($"Duplicate key detected: {key}");
+ }
+
+ ///
+ /// 根据键获取资源
+ ///
+ /// 资源的键
+ /// 对应的资源对象
+ /// 当键不存在时抛出异常
+ public TResource Get(TKey key)
+ {
+ if (!_storage.TryGetValue(key, out var value))
+ throw new KeyNotFoundException($"Resource with key '{key}' not found.");
+
+ return value;
+ }
+
+ ///
+ /// 尝试根据键获取资源
+ ///
+ /// 资源的键
+ /// 输出参数,返回找到的资源对象
+ /// 如果找到资源返回true,否则返回false
+ public bool TryGet(TKey key, out TResource value)
+ => _storage.TryGetValue(key, out value!);
+
+ ///
+ /// 获取所有资源的只读集合
+ ///
+ /// 包含所有资源的只读集合
+ public IReadOnlyCollection GetAll()
+ => _storage.Values;
+
+ ///
+ /// 检查是否包含指定键的资源
+ ///
+ /// 要检查的键
+ /// 如果包含该键返回true,否则返回false
+ public bool Contains(TKey key)
+ => _storage.ContainsKey(key);
+
+ ///
+ /// 从仓储中移除指定键的资源
+ ///
+ /// 要移除的资源键
+ /// 当键不存在时抛出异常
+ public void Remove(TKey key)
+ {
+ if (!_storage.Remove(key))
+ throw new KeyNotFoundException($"Resource with key '{key}' not found.");
+ }
+
+ ///
+ /// 清空仓储中的所有资源
+ ///
+ public void Clear()
+ => _storage.Clear();
+
+ ///
+ /// 从指定路径集合加载资源到仓储中
+ ///
+ /// 资源文件路径的集合
+ /// 是否递归加载子目录中的资源
+ public void LoadFromPath(IEnumerable paths, bool recursive = false)
+ {
+ foreach (var path in paths)
+ {
+ LoadSinglePath(path, recursive);
+ }
+ }
+
+ ///
+ /// 从指定路径数组加载资源到仓储中
+ /// 提供便捷的参数数组重载方法
+ ///
+ /// 是否递归加载子目录中的资源
+ /// 资源文件路径的参数数组
+ public void LoadFromPath(bool recursive = false, params string[] paths)
+ {
+ LoadFromPath(paths, recursive);
+ }
+
+ ///
+ /// 从单个路径加载资源
+ /// 遍历目录中的所有.tres和.res文件并加载为资源
+ ///
+ /// 要加载资源的目录路径
+ /// 是否递归加载子目录中的资源
+ private void LoadSinglePath(string path, bool recursive)
+ {
+ // 打开目录访问对象
+ var dir = DirAccess.Open(path);
+ if (dir == null)
+ {
+ GD.PushWarning($"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);
+ }
+
+ // 结束目录遍历
+ dir.ListDirEnd();
+ }
+}
\ No newline at end of file
diff --git a/GFramework.Godot/data/IResourceRepository.cs b/GFramework.Godot/data/IResourceRepository.cs
new file mode 100644
index 00000000..c510e675
--- /dev/null
+++ b/GFramework.Godot/data/IResourceRepository.cs
@@ -0,0 +1,41 @@
+// Copyright (c) 2026 GeWuYou
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+using GFramework.Game.Abstractions.data;
+using Godot;
+
+namespace GFramework.Godot.data;
+
+///
+/// 定义资源仓储接口,专门用于管理Godot资源的加载和存储
+/// 继承自通用仓储接口,添加了从路径加载资源的功能
+///
+/// 资源键的类型
+/// 资源类型,必须继承自Godot.Resource
+public interface IResourceRepository : IRepository where TResource : Resource
+{
+ ///
+ /// 从指定路径加载资源到仓储中
+ ///
+ /// 资源文件的路径集合
+ /// 是否递归加载子目录中的资源
+ void LoadFromPath(IEnumerable paths, bool recursive = false);
+
+ ///
+ /// 从指定路径数组加载资源到仓储中
+ /// 提供便捷的参数数组重载方法
+ ///
+ /// 是否递归加载子目录中的资源
+ /// 资源文件路径的参数数组
+ void LoadFromPath(bool recursive = false, params string[] paths);
+}
\ No newline at end of file
From b1d3192fc816446ca4a0eaf09b4776f0e30f80bb Mon Sep 17 00:00:00 2001
From: GeWuYou <95328647+GeWuYou@users.noreply.github.com>
Date: Mon, 23 Feb 2026 22:25:29 +0800
Subject: [PATCH 2/2] =?UTF-8?q?refactor(GodotResourceRepository):=20?=
=?UTF-8?q?=E9=87=8D=E6=9E=84=E8=B5=84=E6=BA=90=E4=BB=93=E5=BA=93=E8=B7=AF?=
=?UTF-8?q?=E5=BE=84=E5=8A=A0=E8=BD=BD=E5=8A=9F=E8=83=BD?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
- 添加ILogger用于日志记录替换GD.PushWarning
- 修改GetAll方法返回ToArray()副本而非直接Values引用
- 分离路径加载方法为非递归和递归两个独立接口
- 新增LoadFromPath和LoadFromPathRecursive的重载方法
- 提取内部处理逻辑到ProcessEntry私有方法
- 优化目录遍历逻辑并改进错误处理机制
- 添加重复键检测和资源加载失败的日志记录
---
.../data/GodotResourceRepository.cs | 129 ++++++++++++------
GFramework.Godot/data/IResourceRepository.cs | 27 ++--
2 files changed, 104 insertions(+), 52 deletions(-)
diff --git a/GFramework.Godot/data/GodotResourceRepository.cs b/GFramework.Godot/data/GodotResourceRepository.cs
index 5119f71e..e4801d6a 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 bool TryGet(TKey key, out TResource value)
///
/// 包含所有资源的只读集合
public IReadOnlyCollection GetAll()
- => _storage.Values;
+ => _storage.Values.ToArray();
///
/// 检查是否包含指定键的资源
@@ -100,27 +105,52 @@ public void Clear()
=> _storage.Clear();
///
- /// 从指定路径集合加载资源到仓储中
+ /// 从指定路径集合加载资源(非递归)
///
- /// 资源文件路径的集合
- /// 是否递归加载子目录中的资源
- public void LoadFromPath(IEnumerable paths, bool recursive = false)
+ /// 资源文件路径集合
+ public void LoadFromPath(IEnumerable paths)
{
- foreach (var path in paths)
- {
- LoadSinglePath(path, recursive);
- }
+ 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(bool recursive = false, params string[] paths)
+ private void LoadFromPathInternal(IEnumerable paths, bool recursive)
{
- LoadFromPath(paths, recursive);
+ foreach (var path in paths)
+ {
+ LoadSinglePath(path, recursive);
+ }
}
///
@@ -131,54 +161,67 @@ public void LoadFromPath(bool recursive = false, params string[] paths)
/// 是否递归加载子目录中的资源
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;
+ // 跳过当前目录和父目录的特殊条目
+ if (entry is not ("." or ".."))
+ ProcessEntry(path, entry, dir.CurrentIsDir(), recursive);
+ }
- var fullPath = $"{path}/{entry}";
-
- // 处理目录项
- if (dir.CurrentIsDir())
- {
- // 递归处理子目录(排除.和..目录)
- if (recursive && entry != "." && entry != "..")
- {
- LoadSinglePath(fullPath, true);
- }
-
- continue;
- }
+ // 结束目录遍历
+ dir.ListDirEnd();
+ }
- // 只处理.tres和.res文件
- if (!entry.EndsWith(".tres") && !entry.EndsWith(".res"))
- continue;
+ ///
+ /// 处理目录中的单个条目
+ /// 如果是目录且启用递归则继续深入处理,如果是资源文件则加载到存储中
+ ///
+ /// 基础路径
+ /// 当前处理的条目名称
+ /// 当前条目是否为目录
+ /// 是否递归处理子目录
+ private void ProcessEntry(string basePath, string entry, bool isDir, bool recursive)
+ {
+ // 构建完整的文件路径
+ var fullPath = $"{basePath}/{entry}";
- // 加载资源文件
- var resource = GD.Load(fullPath);
+ // 处理目录条目
+ if (isDir)
+ {
+ // 如果启用递归,则递归处理子目录
+ if (recursive)
+ LoadSinglePath(fullPath, true);
+ return;
+ }
- if (resource == null)
- {
- GD.PushWarning($"Failed to load resource: {fullPath}");
- continue;
- }
+ // 只处理.tres和.res扩展名的资源文件
+ if (!entry.EndsWith(".tres") && !entry.EndsWith(".res"))
+ return;
- Add(resource.Key, resource);
+ // 加载资源文件
+ var resource = GD.Load(fullPath);
+ if (resource == null)
+ {
+ Log.Warn($"Failed to load resource: {fullPath}");
+ return;
}
- // 结束目录遍历
- dir.ListDirEnd();
+ // 将资源添加到存储中,如果键已存在则记录警告
+ 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 c510e675..99b7227b 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