From 3589a0cf2f08cbed6176c3c3e95944aff78290c6 Mon Sep 17 00:00:00 2001 From: GeWuYou <95328647+GeWuYou@users.noreply.github.com> Date: Sun, 18 Jan 2026 16:01:10 +0800 Subject: [PATCH] =?UTF-8?q?feat(registry):=20=E6=B7=BB=E5=8A=A0=E9=80=9A?= =?UTF-8?q?=E7=94=A8=E6=B3=A8=E5=86=8C=E8=A1=A8=E5=9F=BA=E7=A1=80=E8=AE=BE?= =?UTF-8?q?=E6=96=BD=E5=B9=B6=E9=87=8D=E6=9E=84UI=E6=B3=A8=E5=86=8C?= =?UTF-8?q?=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 新增 IKeyValue 接口定义通用键值对数据结构契约 - 创建 IRegistry 接口提供通用注册表功能定义 - 实现 KeyValueRegistryBase 基类提供基于字典的键值对管理 - 添加 GodotSceneRegistry 类管理Godot场景资源 - 重构 GodotUiRegistry 和 GodotUiFactory 使用新的注册表基类 - 移除废弃的 IWritableUiRegistry 接口 - 更新项目文件添加注册表相关目录结构 --- .../bases/IKeyValue.cs | 19 +++++ .../registries/IRegistry.cs | 38 ++++++++++ .../registries/KeyValueRegistryBase.cs | 69 +++++++++++++++++++ .../scene/IGameSceneRegistry.cs | 9 +++ .../ui/IUiRegistry.cs | 11 +-- .../ui/IWritableUiRegistry.cs | 16 ----- GFramework.Game/GFramework.Game.csproj | 3 + GFramework.Godot/scene/GodotSceneRegistry.cs | 13 ++++ GFramework.Godot/ui/GodotUiFactory.cs | 4 +- GFramework.Godot/ui/GodotUiRegistry.cs | 41 ++--------- 10 files changed, 162 insertions(+), 61 deletions(-) create mode 100644 GFramework.Core.Abstractions/bases/IKeyValue.cs create mode 100644 GFramework.Core.Abstractions/registries/IRegistry.cs create mode 100644 GFramework.Core.Abstractions/registries/KeyValueRegistryBase.cs create mode 100644 GFramework.Game.Abstractions/scene/IGameSceneRegistry.cs delete mode 100644 GFramework.Game.Abstractions/ui/IWritableUiRegistry.cs create mode 100644 GFramework.Godot/scene/GodotSceneRegistry.cs diff --git a/GFramework.Core.Abstractions/bases/IKeyValue.cs b/GFramework.Core.Abstractions/bases/IKeyValue.cs new file mode 100644 index 0000000..ad2fca2 --- /dev/null +++ b/GFramework.Core.Abstractions/bases/IKeyValue.cs @@ -0,0 +1,19 @@ +namespace GFramework.Core.Abstractions.bases; + +/// +/// 表示键值对的接口,定义了通用的键值对数据结构契约 +/// +/// 键的类型 +/// 值的类型 +public interface IKeyValue +{ + /// + /// 获取键值对中的键 + /// + TKey Key { get; } + + /// + /// 获取键值对中的值 + /// + TValue Value { get; } +} \ No newline at end of file diff --git a/GFramework.Core.Abstractions/registries/IRegistry.cs b/GFramework.Core.Abstractions/registries/IRegistry.cs new file mode 100644 index 0000000..8c26073 --- /dev/null +++ b/GFramework.Core.Abstractions/registries/IRegistry.cs @@ -0,0 +1,38 @@ +using GFramework.Core.Abstractions.bases; + +namespace GFramework.Game.Abstractions.registry; + +/// +/// 表示一个通用的注册表接口,用于根据键类型T获取值类型TR的对象 +/// +/// 注册表中用作键的类型 +/// 注册表中存储的值的类型 +public interface IRegistry +{ + /// + /// 根据指定的键获取对应的值 + /// + /// 用于查找值的键 + /// 与指定键关联的值 + Tr Get(T key); + + /// + /// 检查注册表是否包含指定的键 + /// + /// 要检查的键 + /// 如果注册表包含具有指定键的元素,则为true;否则为false + bool Contains(T key); + + /// + /// 添加一个键值对到注册表中 + /// + /// 要添加的键值对映射对象 + IRegistry Registry(IKeyValue mapping); + + /// + /// 添加一个键值对到注册表中 + /// + /// 要添加的键 + /// 要添加的值 + IRegistry Registry(T key, Tr value); +} \ No newline at end of file diff --git a/GFramework.Core.Abstractions/registries/KeyValueRegistryBase.cs b/GFramework.Core.Abstractions/registries/KeyValueRegistryBase.cs new file mode 100644 index 0000000..f1fb0cc --- /dev/null +++ b/GFramework.Core.Abstractions/registries/KeyValueRegistryBase.cs @@ -0,0 +1,69 @@ +using System.Collections.Generic; +using GFramework.Core.Abstractions.bases; +using GFramework.Game.Abstractions.registry; + +namespace GFramework.Core.Abstractions.registries; + +/// +/// 基于Dictionary的通用键值注册表基类 +/// 提供基于字典的键值对注册、查询和管理功能 +/// +/// 键的类型 +/// 值的类型 +public abstract class KeyValueRegistryBase + : IRegistry +{ + /// + /// 存储键值对映射关系的字典 + /// + protected readonly IDictionary Map; + + /// + /// 初始化KeyValueRegistryBase的新实例 + /// + /// 用于比较键的相等性的比较器,如果为null则使用默认比较器 + protected KeyValueRegistryBase(IEqualityComparer? comparer = null) + { + // 使用指定的比较器或默认比较器创建字典 + Map = new Dictionary(comparer ?? EqualityComparer.Default); + } + + /// + /// 根据指定的键获取对应的值 + /// + /// 要查找的键 + /// 与键关联的值 + /// 当键不存在时抛出异常 + public virtual TValue Get(TKey key) + => Map.TryGetValue(key, out var value) + ? value + : throw new KeyNotFoundException($"{GetType().Name}: key not registered: {key}"); + + /// + /// 判断是否包含指定的键 + /// + /// 要检查的键 + /// 如果包含该键返回true,否则返回false + public virtual bool Contains(TKey key) + => Map.ContainsKey(key); + + /// + /// 注册键值对到注册表中 + /// + /// 要注册的键 + /// 要注册的值 + /// 当前注册表实例,支持链式调用 + public virtual IRegistry Registry(TKey key, TValue value) + { + Map.Add(key, value); + return this; + } + + /// + /// 注册键值对映射对象到注册表中 + /// + /// 包含键值对的映射对象 + /// 当前注册表实例,支持链式调用 + public virtual IRegistry Registry(IKeyValue mapping) + => Registry(mapping.Key, mapping.Value); +} \ No newline at end of file diff --git a/GFramework.Game.Abstractions/scene/IGameSceneRegistry.cs b/GFramework.Game.Abstractions/scene/IGameSceneRegistry.cs new file mode 100644 index 0000000..a598f4b --- /dev/null +++ b/GFramework.Game.Abstractions/scene/IGameSceneRegistry.cs @@ -0,0 +1,9 @@ +using GFramework.Game.Abstractions.registry; + +namespace GFramework.Game.Abstractions.scene; + +/// +/// 游戏场景注册表接口,用于管理游戏场景的注册和查找 +/// +/// 场景类型,表示注册表中存储的具体场景对象类型 +public interface IGameSceneRegistry : IRegistry; \ No newline at end of file diff --git a/GFramework.Game.Abstractions/ui/IUiRegistry.cs b/GFramework.Game.Abstractions/ui/IUiRegistry.cs index 8d66633..bddb3e2 100644 --- a/GFramework.Game.Abstractions/ui/IUiRegistry.cs +++ b/GFramework.Game.Abstractions/ui/IUiRegistry.cs @@ -1,4 +1,5 @@ using GFramework.Core.Abstractions.utility; +using GFramework.Game.Abstractions.registry; namespace GFramework.Game.Abstractions.ui; @@ -6,12 +7,4 @@ namespace GFramework.Game.Abstractions.ui; /// UI注册表接口,用于根据UI键获取对应的UI实例 /// /// UI实例的类型参数,使用协变修饰符out -public interface IUiRegistry : IUtility -{ - /// - /// 根据指定的UI键获取对应的UI实例 - /// - /// UI的唯一标识键 - /// 与指定键关联的UI实例 - T Get(string uiKey); -} \ No newline at end of file +public interface IUiRegistry : IUtility, IRegistry; \ No newline at end of file diff --git a/GFramework.Game.Abstractions/ui/IWritableUiRegistry.cs b/GFramework.Game.Abstractions/ui/IWritableUiRegistry.cs deleted file mode 100644 index c50669f..0000000 --- a/GFramework.Game.Abstractions/ui/IWritableUiRegistry.cs +++ /dev/null @@ -1,16 +0,0 @@ -namespace GFramework.Game.Abstractions.ui; - -/// -/// 可写的UI注册表接口 -/// -/// UI实例的类型参数 -public interface IWritableUiRegistry : IUiRegistry where T : class -{ - /// - /// 注册UI实例到注册表 - /// - /// UI的唯一标识键 - /// 要注册的UI实例 - /// 当前注册表实例 - IWritableUiRegistry Register(string key, T scene); -} \ No newline at end of file diff --git a/GFramework.Game/GFramework.Game.csproj b/GFramework.Game/GFramework.Game.csproj index b81965b..77d1ec0 100644 --- a/GFramework.Game/GFramework.Game.csproj +++ b/GFramework.Game/GFramework.Game.csproj @@ -13,4 +13,7 @@ + + + diff --git a/GFramework.Godot/scene/GodotSceneRegistry.cs b/GFramework.Godot/scene/GodotSceneRegistry.cs new file mode 100644 index 0000000..17efa8e --- /dev/null +++ b/GFramework.Godot/scene/GodotSceneRegistry.cs @@ -0,0 +1,13 @@ +using GFramework.Core.Abstractions.registries; +using GFramework.Game.Abstractions.ui; +using Godot; + +namespace GFramework.Godot.scene; + +/// +/// Godot场景注册表类 +/// 继承自KeyValueRegistryBase,使用字符串作为键,PackedScene作为值进行存储 +/// 实现IUiRegistry接口,提供UI场景的注册和管理功能 +/// +public class GodotSceneRegistry() : KeyValueRegistryBase(StringComparer.Ordinal), + IUiRegistry; \ No newline at end of file diff --git a/GFramework.Godot/ui/GodotUiFactory.cs b/GFramework.Godot/ui/GodotUiFactory.cs index c763bb8..c80985c 100644 --- a/GFramework.Godot/ui/GodotUiFactory.cs +++ b/GFramework.Godot/ui/GodotUiFactory.cs @@ -14,7 +14,7 @@ public class GodotUiFactory : AbstractContextUtility, IUiFactory /// /// UI注册表,用于存储和获取PackedScene类型的UI资源 /// - private IWritableUiRegistry _registry = null!; + private IUiRegistry _registry = null!; /// /// 根据指定的UI键创建UI页面实例 @@ -44,6 +44,6 @@ public class GodotUiFactory : AbstractContextUtility, IUiFactory /// protected override void OnInit() { - _registry = this.GetUtility>()!; + _registry = this.GetUtility>()!; } } \ No newline at end of file diff --git a/GFramework.Godot/ui/GodotUiRegistry.cs b/GFramework.Godot/ui/GodotUiRegistry.cs index 36b1754..dab8072 100644 --- a/GFramework.Godot/ui/GodotUiRegistry.cs +++ b/GFramework.Godot/ui/GodotUiRegistry.cs @@ -1,40 +1,13 @@ -using GFramework.Game.Abstractions.ui; +using GFramework.Core.Abstractions.registries; +using GFramework.Game.Abstractions.ui; using Godot; namespace GFramework.Godot.ui; /// -/// Godot UI注册表实现类,用于管理PackedScene类型的UI资源注册和获取 +/// Godot UI注册表类,用于管理UI相关的PackedScene资源 +/// 继承自KeyValueRegistryBase,使用字符串作为键,PackedScene作为值进行存储 +/// 实现IUiRegistry接口,提供UI场景的注册和管理功能 /// -public class GodotUiRegistry : IWritableUiRegistry -{ - /// - /// 存储UI键值对的字典,键为UI标识符,值为对应的PackedScene对象 - /// - private readonly Dictionary _map = new(StringComparer.Ordinal); - - /// - /// 注册UI场景到注册表中 - /// - /// UI的唯一标识符 - /// 要注册的PackedScene对象 - /// 返回当前UI注册表实例,支持链式调用 - public IWritableUiRegistry Register(string key, PackedScene scene) - { - _map[key] = scene; - return this; - } - - /// - /// 根据键获取已注册的UI场景 - /// - /// UI的唯一标识符 - /// 当指定的键未找到对应的UI场景时抛出异常 - /// 对应的PackedScene对象 - public PackedScene Get(string uiKey) - { - return !_map.TryGetValue(uiKey, out var scene) - ? throw new KeyNotFoundException($"UI not registered: {uiKey}") - : scene; - } -} \ No newline at end of file +public class GodotUiRegistry() : KeyValueRegistryBase(StringComparer.Ordinal), + IUiRegistry; \ No newline at end of file