From 07f5d27ab3b57dd2a89e5b1bbf154b51850f65ee Mon Sep 17 00:00:00 2001 From: GeWuYou <95328647+GeWuYou@users.noreply.github.com> Date: Tue, 27 Jan 2026 22:11:01 +0800 Subject: [PATCH] =?UTF-8?q?refactor(setting):=20=E9=87=8D=E6=9E=84?= =?UTF-8?q?=E8=AE=BE=E7=BD=AE=E7=B3=BB=E7=BB=9F=E7=9A=84=E6=8E=A5=E5=8F=A3?= =?UTF-8?q?=E8=AE=BE=E8=AE=A1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 移除 SettingsModel 中的 All() 方法,避免同时实现两个接口的设置被重复返回 - 添加 AllApplicators() 方法用于获取所有可应用设置 - 添加 AllData() 方法用于获取所有设置数据 - 修改 SettingsSystem.ApplyAll() 方法,直接遍历可应用设置而非设置节 - 更新 ISettingsModel 接口定义,将 All() 方法拆分为 AllData() 和 AllApplicators() - 移除 SettingsSystem 中的 Apply(Type) 和 Apply(IEnumerable) 重载方法 - 更新 Apply() 泛型约束从 ISettingsSection 改为 IApplyAbleSettings - 移除注册时对 ISettingsData 的自动注册逻辑,保持职责分离 --- .../setting/ISettingsModel.cs | 13 ++++-- .../setting/ISettingsSystem.cs | 12 +----- GFramework.Game/setting/SettingsModel.cs | 43 ++++++++----------- GFramework.Game/setting/SettingsSystem.cs | 42 ++++-------------- 4 files changed, 39 insertions(+), 71 deletions(-) diff --git a/GFramework.Game.Abstractions/setting/ISettingsModel.cs b/GFramework.Game.Abstractions/setting/ISettingsModel.cs index 273753d..fb3e2b4 100644 --- a/GFramework.Game.Abstractions/setting/ISettingsModel.cs +++ b/GFramework.Game.Abstractions/setting/ISettingsModel.cs @@ -30,10 +30,17 @@ public interface ISettingsModel : IModel T? GetApplicator() where T : class, IApplyAbleSettings; /// - /// 获取所有设置节的集合 + /// 获取所有设置数据的集合 /// - /// 包含所有设置节的可枚举集合 - IEnumerable All(); + /// 包含所有设置数据的可枚举集合 + IEnumerable AllData(); + + /// + /// 获取所有可应用设置的集合 + /// + /// 包含所有可应用设置的可枚举集合 + IEnumerable AllApplicators(); + /// /// 注册可应用设置(必须手动注册) diff --git a/GFramework.Game.Abstractions/setting/ISettingsSystem.cs b/GFramework.Game.Abstractions/setting/ISettingsSystem.cs index f41aca0..34ed148 100644 --- a/GFramework.Game.Abstractions/setting/ISettingsSystem.cs +++ b/GFramework.Game.Abstractions/setting/ISettingsSystem.cs @@ -12,18 +12,8 @@ public interface ISettingsSystem : ISystem /// Task ApplyAll(); - /// - /// 应用指定类型的设置 - /// - Task Apply(Type settingsType); - /// /// 应用指定类型的设置(泛型版本) /// - Task Apply() where T : class, ISettingsSection; - - /// - /// 批量应用多个设置类型 - /// - Task Apply(IEnumerable settingsTypes); + Task Apply() where T : class, IApplyAbleSettings; } \ No newline at end of file diff --git a/GFramework.Game/setting/SettingsModel.cs b/GFramework.Game/setting/SettingsModel.cs index ce63df4..6608f89 100644 --- a/GFramework.Game/setting/SettingsModel.cs +++ b/GFramework.Game/setting/SettingsModel.cs @@ -32,6 +32,15 @@ public class SettingsModel : AbstractModel, ISettingsModel return created; } + /// + /// 获取所有已注册的可应用设置 + /// + /// 所有可应用设置的枚举集合 + public IEnumerable AllApplicators() + { + return _applicators.Values; + } + /// /// 注册可应用设置(必须手动注册) /// @@ -42,13 +51,6 @@ public class SettingsModel : AbstractModel, ISettingsModel { var type = typeof(T); _applicators[type] = applicator; - - // 如果这个应用设置同时也是数据设置,也注册到数据字典中 - if (applicator is ISettingsData data) - { - _dataSettings[type] = data; - } - return this; } @@ -65,6 +67,15 @@ public class SettingsModel : AbstractModel, ISettingsModel : null; } + /// + /// 获取所有设置数据 + /// + /// 所有设置数据的枚举集合 + public IEnumerable AllData() + { + return _dataSettings.Values; + } + /// /// 尝试获取指定类型的设置节 /// @@ -91,27 +102,11 @@ public class SettingsModel : AbstractModel, ISettingsModel return false; } - /// - /// 获取所有设置节的集合 - /// - /// 包含所有设置节的可枚举集合 - public IEnumerable All() - { - // 使用 HashSet 去重(避免同时实现两个接口的设置被重复返回) - var sections = new HashSet(); - - foreach (var applicator in _applicators.Values) - sections.Add(applicator); - - foreach (var data in _dataSettings.Values) - sections.Add(data); - - return sections; - } /// /// 初始化并加载指定类型的设置数据 /// + /// 要初始化的设置类型数组 public async Task InitializeAsync(params Type[] settingTypes) { foreach (var type in settingTypes) diff --git a/GFramework.Game/setting/SettingsSystem.cs b/GFramework.Game/setting/SettingsSystem.cs index a132a08..37ebe28 100644 --- a/GFramework.Game/setting/SettingsSystem.cs +++ b/GFramework.Game/setting/SettingsSystem.cs @@ -18,8 +18,10 @@ public class SettingsSystem : AbstractSystem, ISettingsSystem /// 完成的任务 public async Task ApplyAll() { - // 遍历所有设置配置并尝试应用 - foreach (var section in _model.All()) await TryApplyAsync(section); + foreach (var applicator in _model.AllApplicators()) + { + await TryApplyAsync(applicator); + } } /// @@ -27,40 +29,14 @@ public class SettingsSystem : AbstractSystem, ISettingsSystem /// /// 设置配置类型,必须是类且实现ISettingsSection接口 /// 完成的任务 - public Task Apply() where T : class, ISettingsSection + public Task Apply() where T : class, IApplyAbleSettings { - return Apply(typeof(T)); + var applicator = _model.GetApplicator(); + return applicator != null + ? TryApplyAsync(applicator) + : Task.CompletedTask; } - /// - /// 应用指定类型的设置配置 - /// - /// 设置配置类型 - /// 完成的任务 - public async Task Apply(Type settingsType) - { - if (_model.TryGet(settingsType, out var section)) - { - await TryApplyAsync(section); - } - } - - /// - /// 应用指定类型集合的设置配置 - /// - /// 设置配置类型集合 - /// 完成的任务 - public async Task Apply(IEnumerable settingsTypes) - { - // 去重后遍历设置类型,获取并应用对应的设置配置 - foreach (var type in settingsTypes.Distinct()) - { - if (_model.TryGet(type, out var section)) - { - await TryApplyAsync(section); - } - } - } /// /// 初始化设置系统,获取设置模型实例