From 9ae0f633245002347ed2775c0d48743c5eac3fd9 Mon Sep 17 00:00:00 2001 From: GeWuYou <95328647+GeWuYou@users.noreply.github.com> Date: Sat, 17 Jan 2026 13:16:50 +0800 Subject: [PATCH] =?UTF-8?q?feat(setting):=20=E6=B7=BB=E5=8A=A0=E8=AE=BE?= =?UTF-8?q?=E7=BD=AE=E4=BA=8B=E4=BB=B6=E7=B3=BB=E7=BB=9F=E5=92=8C=E6=94=B9?= =?UTF-8?q?=E8=BF=9B=E8=AE=BE=E7=BD=AE=E6=A8=A1=E5=9E=8B=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 新增 SettingsAllLoadedEvent 事件类,用于表示所有设置加载完成 - 新增 SettingsAppliedEvent 事件类,用于表示设置应用完成状态 - 新增 SettingsApplyingEvent 事件类,用于表示设置正在应用过程 - 新增 SettingsBatchChangedEvent 事件类,用于表示批量设置变更 - 新增 SettingsBatchSavedEvent 事件类,用于表示批量设置保存 - 新增 SettingsChangedEvent 通用设置变更事件类 - 新增 SettingsLoadedEvent 和 SettingsSavedEvent 事件类 - 在 SettingsModel 中添加对 GFramework.Core.extensions 的引用 - 更新 RegisterApplicator 方法支持链式调用并改进注释说明 - 新增 ISettingsChangedEvent 接口定义设置变更事件基础结构 - 修改 ISettingsModel 接口使 RegisterApplicator 方法支持链式调用 - 修改 ISettingsPersistence 接口继承 IContextUtility 接口 --- .../setting/ISettingsChangedEvent.cs | 25 +++++++++++ .../setting/ISettingsModel.cs | 3 +- .../setting/ISettingsPersistence.cs | 3 +- GFramework.Game/setting/SettingsModel.cs | 8 ++-- .../setting/events/SettingsAllLoadedEvent.cs | 30 ++++++++++++++ .../setting/events/SettingsAppliedEvent.cs | 41 +++++++++++++++++++ .../setting/events/SettingsApplyingEvent.cs | 31 ++++++++++++++ .../events/SettingsBatchChangedEvent.cs | 31 ++++++++++++++ .../setting/events/SettingsBatchSavedEvent.cs | 30 ++++++++++++++ .../setting/events/SettingsChangedEvent.cs | 32 +++++++++++++++ .../setting/events/SettingsLoadedEvent.cs | 31 ++++++++++++++ .../setting/events/SettingsSavedEvent.cs | 31 ++++++++++++++ 12 files changed, 291 insertions(+), 5 deletions(-) create mode 100644 GFramework.Game.Abstractions/setting/ISettingsChangedEvent.cs create mode 100644 GFramework.Game/setting/events/SettingsAllLoadedEvent.cs create mode 100644 GFramework.Game/setting/events/SettingsAppliedEvent.cs create mode 100644 GFramework.Game/setting/events/SettingsApplyingEvent.cs create mode 100644 GFramework.Game/setting/events/SettingsBatchChangedEvent.cs create mode 100644 GFramework.Game/setting/events/SettingsBatchSavedEvent.cs create mode 100644 GFramework.Game/setting/events/SettingsChangedEvent.cs create mode 100644 GFramework.Game/setting/events/SettingsLoadedEvent.cs create mode 100644 GFramework.Game/setting/events/SettingsSavedEvent.cs diff --git a/GFramework.Game.Abstractions/setting/ISettingsChangedEvent.cs b/GFramework.Game.Abstractions/setting/ISettingsChangedEvent.cs new file mode 100644 index 0000000..d965298 --- /dev/null +++ b/GFramework.Game.Abstractions/setting/ISettingsChangedEvent.cs @@ -0,0 +1,25 @@ +using System; + +namespace GFramework.Game.Abstractions.setting; + +/// +/// 设置变更事件基类 +/// 定义了设置变更事件的基本结构和属性 +/// +public interface ISettingsChangedEvent +{ + /// + /// 获取变更的设置类型 + /// + Type SettingsType { get; } + + /// + /// 获取变更的设置实例 + /// + ISettingsSection Settings { get; } + + /// + /// 获取变更发生的时间 + /// + DateTime ChangedAt { get; } +} \ No newline at end of file diff --git a/GFramework.Game.Abstractions/setting/ISettingsModel.cs b/GFramework.Game.Abstractions/setting/ISettingsModel.cs index 0b8256a..8637733 100644 --- a/GFramework.Game.Abstractions/setting/ISettingsModel.cs +++ b/GFramework.Game.Abstractions/setting/ISettingsModel.cs @@ -42,5 +42,6 @@ public interface ISettingsModel : IModel /// /// 可应用设置的类型,必须继承自class和IApplyAbleSettings /// 要注册的可应用设置实例 - void RegisterApplicator(T applicator) where T : class, IApplyAbleSettings; + /// 返回当前设置模型实例,支持链式调用 + ISettingsModel RegisterApplicator(T applicator) where T : class, IApplyAbleSettings; } \ No newline at end of file diff --git a/GFramework.Game.Abstractions/setting/ISettingsPersistence.cs b/GFramework.Game.Abstractions/setting/ISettingsPersistence.cs index 47f66cf..37531e7 100644 --- a/GFramework.Game.Abstractions/setting/ISettingsPersistence.cs +++ b/GFramework.Game.Abstractions/setting/ISettingsPersistence.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.Threading.Tasks; +using GFramework.Core.Abstractions.utility; namespace GFramework.Game.Abstractions.setting; @@ -8,7 +9,7 @@ namespace GFramework.Game.Abstractions.setting; /// 设置持久化接口 /// 定义了设置数据的异步加载、保存、检查存在性和删除操作 /// -public interface ISettingsPersistence +public interface ISettingsPersistence : IContextUtility { /// /// 异步加载指定类型的设置数据 diff --git a/GFramework.Game/setting/SettingsModel.cs b/GFramework.Game/setting/SettingsModel.cs index 611ab16..b531ad8 100644 --- a/GFramework.Game/setting/SettingsModel.cs +++ b/GFramework.Game/setting/SettingsModel.cs @@ -31,14 +31,16 @@ public class SettingsModel : AbstractModel, ISettingsModel } /// - /// 注册可应用设置 + /// 注册可应用设置(必须手动注册) /// - /// 可应用设置类型,必须实现IApplyAbleSettings接口 + /// 可应用设置的类型,必须继承自class和IApplyAbleSettings /// 要注册的可应用设置实例 - public void RegisterApplicator(T applicator) where T : class, IApplyAbleSettings + /// 返回当前设置模型实例,支持链式调用 + public ISettingsModel RegisterApplicator(T applicator) where T : class, IApplyAbleSettings { var type = typeof(T); _applicators[type] = applicator; + return this; } /// diff --git a/GFramework.Game/setting/events/SettingsAllLoadedEvent.cs b/GFramework.Game/setting/events/SettingsAllLoadedEvent.cs new file mode 100644 index 0000000..6d81f35 --- /dev/null +++ b/GFramework.Game/setting/events/SettingsAllLoadedEvent.cs @@ -0,0 +1,30 @@ +using GFramework.Game.Abstractions.setting; + +namespace GFramework.Game.setting.events; + +/// +/// 表示所有设置已加载完成的事件 +/// +/// 包含所有设置节的可枚举集合 +public class SettingsAllLoadedEvent(IEnumerable all) : ISettingsChangedEvent +{ + /// + /// 获取所有设置节的只读集合 + /// + public IReadOnlyCollection AllSettings { get; } = all.ToList(); + + /// + /// 获取设置类型,始终返回 ISettingsSection 类型 + /// + public Type SettingsType => typeof(ISettingsSection); + + /// + /// 获取具体的设置节,此事件中始终为 null + /// + public ISettingsSection Settings => null!; + + /// + /// 获取事件发生的时间(UTC时间) + /// + public DateTime ChangedAt { get; } = DateTime.UtcNow; +} \ No newline at end of file diff --git a/GFramework.Game/setting/events/SettingsAppliedEvent.cs b/GFramework.Game/setting/events/SettingsAppliedEvent.cs new file mode 100644 index 0000000..59b2b8d --- /dev/null +++ b/GFramework.Game/setting/events/SettingsAppliedEvent.cs @@ -0,0 +1,41 @@ +using GFramework.Game.Abstractions.setting; + +namespace GFramework.Game.setting.events; + +/// +/// 表示设置应用完成事件 +/// +/// 设置节类型,必须实现ISettingsSection接口 +public class SettingsAppliedEvent(T settings, bool success, Exception? error = null) : ISettingsChangedEvent + where T : ISettingsSection +{ + /// + /// 获取类型化的设置节实例 + /// + public T TypedSettings => (T)Settings; + + /// + /// 获取设置应用是否成功的状态 + /// + public bool Success { get; } = success; + + /// + /// 获取设置应用过程中发生的错误异常(如果有的话) + /// + public Exception? Error { get; } = error; + + /// + /// 获取设置类型的Type信息 + /// + public Type SettingsType => typeof(T); + + /// + /// 获取应用的设置节实例 + /// + public ISettingsSection Settings { get; } = settings; + + /// + /// 获取设置变更的时间戳(UTC时间) + /// + public DateTime ChangedAt { get; } = DateTime.UtcNow; +} \ No newline at end of file diff --git a/GFramework.Game/setting/events/SettingsApplyingEvent.cs b/GFramework.Game/setting/events/SettingsApplyingEvent.cs new file mode 100644 index 0000000..a8745d2 --- /dev/null +++ b/GFramework.Game/setting/events/SettingsApplyingEvent.cs @@ -0,0 +1,31 @@ +using GFramework.Game.Abstractions.setting; + +namespace GFramework.Game.setting.events; + +/// +/// 表示设置应用事件的泛型类 +/// +/// 设置节类型,必须实现ISettingsSection接口 +public class SettingsApplyingEvent(T settings) : ISettingsChangedEvent + where T : ISettingsSection +{ + /// + /// 获取类型化的设置对象 + /// + public T TypedSettings { get; } = settings; + + /// + /// 获取设置类型的Type信息 + /// + public Type SettingsType => typeof(T); + + /// + /// 获取设置节基接口实例 + /// + public ISettingsSection Settings => TypedSettings; + + /// + /// 获取设置变更的时间戳 + /// + public DateTime ChangedAt { get; } = DateTime.UtcNow; +} \ No newline at end of file diff --git a/GFramework.Game/setting/events/SettingsBatchChangedEvent.cs b/GFramework.Game/setting/events/SettingsBatchChangedEvent.cs new file mode 100644 index 0000000..f1dc043 --- /dev/null +++ b/GFramework.Game/setting/events/SettingsBatchChangedEvent.cs @@ -0,0 +1,31 @@ +using GFramework.Game.Abstractions.setting; + +namespace GFramework.Game.setting.events; + +/// +/// 批量设置变更事件 +/// 表示多个设置项同时发生变更的事件 +/// +/// 发生变更的设置数据集合 +public class SettingsBatchChangedEvent(IEnumerable settings) : ISettingsChangedEvent +{ + /// + /// 获取发生变更的具体设置数据列表 + /// + public IEnumerable ChangedSettings { get; } = settings.ToList(); + + /// + /// 获取设置类型,对于批量变更事件,固定返回ISettingsSection类型 + /// + public Type SettingsType => typeof(ISettingsSection); + + /// + /// 获取设置实例,批量变更事件中此属性返回null + /// + public ISettingsSection Settings => null!; + + /// + /// 获取变更发生的时间戳(UTC时间) + /// + public DateTime ChangedAt { get; } = DateTime.UtcNow; +} \ No newline at end of file diff --git a/GFramework.Game/setting/events/SettingsBatchSavedEvent.cs b/GFramework.Game/setting/events/SettingsBatchSavedEvent.cs new file mode 100644 index 0000000..922782a --- /dev/null +++ b/GFramework.Game/setting/events/SettingsBatchSavedEvent.cs @@ -0,0 +1,30 @@ +using GFramework.Game.Abstractions.setting; + +namespace GFramework.Game.setting.events; + +/// +/// 表示设置批量保存事件 +/// +/// 要保存的设置数据集合 +public class SettingsBatchSavedEvent(IEnumerable settings) : ISettingsChangedEvent +{ + /// + /// 获取已保存的设置数据只读集合 + /// + public IReadOnlyCollection SavedSettings { get; } = settings.ToList(); + + /// + /// 获取设置类型(始终返回ISettingsSection类型) + /// + public Type SettingsType => typeof(ISettingsSection); + + /// + /// 获取设置节(在此事件中始终为null) + /// + public ISettingsSection Settings => null!; + + /// + /// 获取更改发生的时间(UTC时间) + /// + public DateTime ChangedAt { get; } = DateTime.UtcNow; +} \ No newline at end of file diff --git a/GFramework.Game/setting/events/SettingsChangedEvent.cs b/GFramework.Game/setting/events/SettingsChangedEvent.cs new file mode 100644 index 0000000..4eeaba1 --- /dev/null +++ b/GFramework.Game/setting/events/SettingsChangedEvent.cs @@ -0,0 +1,32 @@ +using GFramework.Game.Abstractions.setting; + +namespace GFramework.Game.setting.events; + +/// +/// 泛型设置变更事件 +/// +/// 设置节类型,必须实现ISettingsSection接口 +/// 设置实例 +public class SettingsChangedEvent(T settings) : ISettingsChangedEvent + where T : ISettingsSection +{ + /// + /// 获取类型化的设置实例 + /// + public T TypedSettings => (T)Settings; + + /// + /// 获取设置类型 + /// + public Type SettingsType => typeof(T); + + /// + /// 获取设置实例 + /// + public ISettingsSection Settings { get; } = settings; + + /// + /// 获取变更时间 + /// + public DateTime ChangedAt { get; } = DateTime.UtcNow; +} \ No newline at end of file diff --git a/GFramework.Game/setting/events/SettingsLoadedEvent.cs b/GFramework.Game/setting/events/SettingsLoadedEvent.cs new file mode 100644 index 0000000..349188a --- /dev/null +++ b/GFramework.Game/setting/events/SettingsLoadedEvent.cs @@ -0,0 +1,31 @@ +using GFramework.Game.Abstractions.setting; + +namespace GFramework.Game.setting.events; + +/// +/// 表示设置加载完成事件的泛型类 +/// +/// 设置节类型,必须实现ISettingsSection接口 +public class SettingsLoadedEvent(T settings) : ISettingsChangedEvent + where T : ISettingsSection +{ + /// + /// 获取类型化的设置对象 + /// + public T TypedSettings { get; } = settings; + + /// + /// 获取设置类型的Type信息 + /// + public Type SettingsType => typeof(T); + + /// + /// 获取设置节基接口对象 + /// + public ISettingsSection Settings => TypedSettings; + + /// + /// 获取事件发生的时间戳 + /// + public DateTime ChangedAt { get; } = DateTime.UtcNow; +} \ No newline at end of file diff --git a/GFramework.Game/setting/events/SettingsSavedEvent.cs b/GFramework.Game/setting/events/SettingsSavedEvent.cs new file mode 100644 index 0000000..516bb02 --- /dev/null +++ b/GFramework.Game/setting/events/SettingsSavedEvent.cs @@ -0,0 +1,31 @@ +using GFramework.Game.Abstractions.setting; + +namespace GFramework.Game.setting.events; + +/// +/// 表示设置保存事件的泛型类 +/// +/// 设置节类型,必须实现ISettingsSection接口 +public class SettingsSavedEvent(T settings) : ISettingsChangedEvent + where T : ISettingsSection +{ + /// + /// 获取类型化的设置对象 + /// + public T TypedSettings { get; } = settings; + + /// + /// 获取设置类型的Type信息 + /// + public Type SettingsType => typeof(T); + + /// + /// 获取设置节对象 + /// + public ISettingsSection Settings => TypedSettings; + + /// + /// 获取设置更改的时间戳 + /// + public DateTime ChangedAt { get; } = DateTime.UtcNow; +} \ No newline at end of file