From 3137c85449b76a74a46f4f4988a0480e169c72c7 Mon Sep 17 00:00:00 2001 From: GeWuYou <95328647+GeWuYou@users.noreply.github.com> Date: Sat, 17 Jan 2026 19:26:38 +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=E5=BC=82=E6=AD=A5=E6=93=8D?= =?UTF-8?q?=E4=BD=9C=E5=AE=9E=E7=8E=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 移除原有的重置设置命令相关文件和方法 - 添加异步命令发送接口的默认实现 - 实现设置系统的异步应用功能 - 优化设置应用逻辑并添加泛型支持 - 统一任务完成状态返回方式 --- .../architecture/ArchitectureServicesTests.cs | 11 +++ .../architecture/GameContextTests.cs | 10 +++ .../setting/ISettingsSystem.cs | 18 ---- GFramework.Game/setting/SettingsSystem.cs | 85 +++++++++++++------ .../setting/commands/ResetSettingsCommand.cs | 54 ------------ .../setting/commands/ResetSettingsInput.cs | 8 -- 6 files changed, 78 insertions(+), 108 deletions(-) delete mode 100644 GFramework.Game/setting/commands/ResetSettingsCommand.cs delete mode 100644 GFramework.Game/setting/commands/ResetSettingsInput.cs diff --git a/GFramework.Core.Tests/architecture/ArchitectureServicesTests.cs b/GFramework.Core.Tests/architecture/ArchitectureServicesTests.cs index 3995f71..2e20a1d 100644 --- a/GFramework.Core.Tests/architecture/ArchitectureServicesTests.cs +++ b/GFramework.Core.Tests/architecture/ArchitectureServicesTests.cs @@ -250,6 +250,17 @@ public class TestArchitectureContextV3 : IArchitectureContext } public TResult SendCommand(ICommand command) => default!; + + public Task SendCommandAsync(IAsyncCommand command) + { + return Task.CompletedTask; + } + + public Task SendCommandAsync(IAsyncCommand command) + { + return (Task)Task.CompletedTask; + } + public TResult SendQuery(IQuery query) => default!; public IEnvironment GetEnvironment() => _environment; } diff --git a/GFramework.Core.Tests/architecture/GameContextTests.cs b/GFramework.Core.Tests/architecture/GameContextTests.cs index 2dc63a5..8818f05 100644 --- a/GFramework.Core.Tests/architecture/GameContextTests.cs +++ b/GFramework.Core.Tests/architecture/GameContextTests.cs @@ -327,6 +327,16 @@ public class TestArchitectureContext : IArchitectureContext /// 命令执行结果 public TResult SendCommand(ICommand command) => default!; + public Task SendCommandAsync(IAsyncCommand command) + { + return Task.CompletedTask; + } + + public Task SendCommandAsync(IAsyncCommand command) + { + return (Task)Task.CompletedTask; + } + /// /// 发送查询请求 /// diff --git a/GFramework.Game.Abstractions/setting/ISettingsSystem.cs b/GFramework.Game.Abstractions/setting/ISettingsSystem.cs index 2afb5b3..aeccb4e 100644 --- a/GFramework.Game.Abstractions/setting/ISettingsSystem.cs +++ b/GFramework.Game.Abstractions/setting/ISettingsSystem.cs @@ -28,23 +28,5 @@ public interface ISettingsSystem : ISystem /// /// 批量应用多个设置类型 /// - /// 设置配置类型集合 Task Apply(IEnumerable settingsTypes); - - /// - /// 重置指定类型的设置 - /// - /// 设置类型 - Task ResetAsync(Type settingsType); - - /// - /// 重置指定类型的设置(泛型版本) - /// - /// 设置类型 - Task ResetAsync() where T : class, ISettingsData, new(); - - /// - /// 重置所有设置 - /// - Task ResetAllAsync(); } \ No newline at end of file diff --git a/GFramework.Game/setting/SettingsSystem.cs b/GFramework.Game/setting/SettingsSystem.cs index 7f37ea7..0623360 100644 --- a/GFramework.Game/setting/SettingsSystem.cs +++ b/GFramework.Game/setting/SettingsSystem.cs @@ -1,7 +1,6 @@ using GFramework.Core.extensions; using GFramework.Core.system; using GFramework.Game.Abstractions.setting; -using GFramework.Game.setting.commands; using GFramework.Game.setting.events; namespace GFramework.Game.setting; @@ -28,43 +27,73 @@ public class SettingsSystem : AbstractSystem, ISettingsSystem return Task.CompletedTask; } - public Task ResetAsync(Type settingsType) + /// + /// 应用指定类型的设置配置 + /// + /// 设置配置类型,必须是类且实现ISettingsSection接口 + /// 完成的任务 + public Task Apply() where T : class, ISettingsSection + => Apply(typeof(T)); + + /// + /// 应用指定类型的设置配置 + /// + /// 设置配置类型 + /// 完成的任务 + public Task Apply(Type settingsType) { - return this.SendCommandAsync(new ResetSettingsCommand(new ResetSettingsInput + if (!_model.TryGet(settingsType, out var section)) + return Task.CompletedTask; + + TryApply(section); + return Task.CompletedTask; + } + + /// + /// 应用指定类型集合的设置配置 + /// + /// 设置配置类型集合 + /// 完成的任务 + public Task Apply(IEnumerable settingsTypes) + { + // 去重后遍历设置类型,获取并应用对应的设置配置 + foreach (var type in settingsTypes.Distinct()) { - SettingsType = settingsType - })); + if (_model.TryGet(type, out var section)) + { + TryApply(section); + } + } + + return Task.CompletedTask; } - public Task ResetAsync() where T : class, ISettingsData, new() + /// + /// 初始化设置系统,获取设置模型实例 + /// + protected override void OnInit() { - return ResetAsync(typeof(T)); - } - - public Task ResetAllAsync() - { - return this.SendCommandAsync(new ResetSettingsCommand(new ResetSettingsInput - { - SettingsType = null - })); + _model = this.GetModel()!; } + /// + /// 尝试应用可应用的设置配置 + /// + /// 设置配置对象 private void TryApply(ISettingsSection section) { - if (section is IApplyAbleSettings applyable) - { - this.SendEvent(new SettingsApplyingEvent(section)); + if (section is not IApplyAbleSettings applyAbleSettings) return; + this.SendEvent(new SettingsApplyingEvent(section)); - try - { - applyable.Apply(); - this.SendEvent(new SettingsAppliedEvent(section, true)); - } - catch (Exception ex) - { - this.SendEvent(new SettingsAppliedEvent(section, false, ex)); - throw; - } + try + { + applyAbleSettings.Apply(); + this.SendEvent(new SettingsAppliedEvent(section, true)); + } + catch (Exception ex) + { + this.SendEvent(new SettingsAppliedEvent(section, false, ex)); + throw; } } } \ No newline at end of file diff --git a/GFramework.Game/setting/commands/ResetSettingsCommand.cs b/GFramework.Game/setting/commands/ResetSettingsCommand.cs deleted file mode 100644 index fa1c835..0000000 --- a/GFramework.Game/setting/commands/ResetSettingsCommand.cs +++ /dev/null @@ -1,54 +0,0 @@ -using GFramework.Core.command; -using GFramework.Game.Abstractions.setting; -using GFramework.Game.setting.events; - -namespace GFramework.Game.setting.commands; - -public sealed class ResetSettingsCommand : AbstractAsyncCommand -{ - private ISettingsModel _model = null!; - private ISettingsPersistence _persistence = null!; - - protected override void OnContextReady() - { - base.OnContextReady(); - _model = this.GetModel()!; - _persistence = this.GetUtility()!; - } - - protected override async Task OnExecuteAsync(ResetSettingsInput input) - { - if (input.SettingsType == null) - await ResetAll(); - else - await ResetSingle(input.SettingsType); - } - - private async Task ResetSingle(Type settingsType) - { - if (!_model.TryGet(settingsType, out var section)) - throw new InvalidOperationException($"Settings {settingsType.Name} not found"); - - if (section is not ISettingsData settingsData) - throw new InvalidOperationException($"Settings {settingsType.Name} is not ISettingsData"); - - settingsData.Reset(); - await _persistence.SaveAsync(settingsData); - this.SendEvent(new SettingsResetEvent(settingsData)); - } - - private async Task ResetAll() - { - var allSettings = _model.All() - .OfType() - .ToList(); - - foreach (var settings in allSettings) - { - settings.Reset(); - await _persistence.SaveAsync(settings); - } - - this.SendEvent(new SettingsResetAllEvent(allSettings)); - } -} \ No newline at end of file diff --git a/GFramework.Game/setting/commands/ResetSettingsInput.cs b/GFramework.Game/setting/commands/ResetSettingsInput.cs deleted file mode 100644 index 7200737..0000000 --- a/GFramework.Game/setting/commands/ResetSettingsInput.cs +++ /dev/null @@ -1,8 +0,0 @@ -using GFramework.Core.Abstractions.command; - -namespace GFramework.Game.setting.commands; - -public sealed class ResetSettingsInput : ICommandInput -{ - public Type? SettingsType { get; init; } -} \ No newline at end of file