mirror of
https://github.com/GeWuYou/GFramework.git
synced 2026-03-22 10:34:30 +08:00
feat(setting): 添加设置事件系统和改进设置模型接口
- 新增 SettingsAllLoadedEvent 事件类,用于表示所有设置加载完成 - 新增 SettingsAppliedEvent 事件类,用于表示设置应用完成状态 - 新增 SettingsApplyingEvent 事件类,用于表示设置正在应用过程 - 新增 SettingsBatchChangedEvent 事件类,用于表示批量设置变更 - 新增 SettingsBatchSavedEvent 事件类,用于表示批量设置保存 - 新增 SettingsChangedEvent 通用设置变更事件类 - 新增 SettingsLoadedEvent 和 SettingsSavedEvent 事件类 - 在 SettingsModel 中添加对 GFramework.Core.extensions 的引用 - 更新 RegisterApplicator 方法支持链式调用并改进注释说明 - 新增 ISettingsChangedEvent 接口定义设置变更事件基础结构 - 修改 ISettingsModel 接口使 RegisterApplicator 方法支持链式调用 - 修改 ISettingsPersistence 接口继承 IContextUtility 接口
This commit is contained in:
parent
103792f178
commit
9ae0f63324
@ -0,0 +1,25 @@
|
||||
using System;
|
||||
|
||||
namespace GFramework.Game.Abstractions.setting;
|
||||
|
||||
/// <summary>
|
||||
/// 设置变更事件基类
|
||||
/// 定义了设置变更事件的基本结构和属性
|
||||
/// </summary>
|
||||
public interface ISettingsChangedEvent
|
||||
{
|
||||
/// <summary>
|
||||
/// 获取变更的设置类型
|
||||
/// </summary>
|
||||
Type SettingsType { get; }
|
||||
|
||||
/// <summary>
|
||||
/// 获取变更的设置实例
|
||||
/// </summary>
|
||||
ISettingsSection Settings { get; }
|
||||
|
||||
/// <summary>
|
||||
/// 获取变更发生的时间
|
||||
/// </summary>
|
||||
DateTime ChangedAt { get; }
|
||||
}
|
||||
@ -42,5 +42,6 @@ public interface ISettingsModel : IModel
|
||||
/// </summary>
|
||||
/// <typeparam name="T">可应用设置的类型,必须继承自class和IApplyAbleSettings</typeparam>
|
||||
/// <param name="applicator">要注册的可应用设置实例</param>
|
||||
void RegisterApplicator<T>(T applicator) where T : class, IApplyAbleSettings;
|
||||
/// <returns>返回当前设置模型实例,支持链式调用</returns>
|
||||
ISettingsModel RegisterApplicator<T>(T applicator) where T : class, IApplyAbleSettings;
|
||||
}
|
||||
@ -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;
|
||||
/// 设置持久化接口
|
||||
/// 定义了设置数据的异步加载、保存、检查存在性和删除操作
|
||||
/// </summary>
|
||||
public interface ISettingsPersistence
|
||||
public interface ISettingsPersistence : IContextUtility
|
||||
{
|
||||
/// <summary>
|
||||
/// 异步加载指定类型的设置数据
|
||||
|
||||
@ -31,14 +31,16 @@ public class SettingsModel : AbstractModel, ISettingsModel
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 注册可应用设置
|
||||
/// 注册可应用设置(必须手动注册)
|
||||
/// </summary>
|
||||
/// <typeparam name="T">可应用设置类型,必须实现IApplyAbleSettings接口</typeparam>
|
||||
/// <typeparam name="T">可应用设置的类型,必须继承自class和IApplyAbleSettings</typeparam>
|
||||
/// <param name="applicator">要注册的可应用设置实例</param>
|
||||
public void RegisterApplicator<T>(T applicator) where T : class, IApplyAbleSettings
|
||||
/// <returns>返回当前设置模型实例,支持链式调用</returns>
|
||||
public ISettingsModel RegisterApplicator<T>(T applicator) where T : class, IApplyAbleSettings
|
||||
{
|
||||
var type = typeof(T);
|
||||
_applicators[type] = applicator;
|
||||
return this;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
||||
30
GFramework.Game/setting/events/SettingsAllLoadedEvent.cs
Normal file
30
GFramework.Game/setting/events/SettingsAllLoadedEvent.cs
Normal file
@ -0,0 +1,30 @@
|
||||
using GFramework.Game.Abstractions.setting;
|
||||
|
||||
namespace GFramework.Game.setting.events;
|
||||
|
||||
/// <summary>
|
||||
/// 表示所有设置已加载完成的事件
|
||||
/// </summary>
|
||||
/// <param name="all">包含所有设置节的可枚举集合</param>
|
||||
public class SettingsAllLoadedEvent(IEnumerable<ISettingsSection> all) : ISettingsChangedEvent
|
||||
{
|
||||
/// <summary>
|
||||
/// 获取所有设置节的只读集合
|
||||
/// </summary>
|
||||
public IReadOnlyCollection<ISettingsSection> AllSettings { get; } = all.ToList();
|
||||
|
||||
/// <summary>
|
||||
/// 获取设置类型,始终返回 ISettingsSection 类型
|
||||
/// </summary>
|
||||
public Type SettingsType => typeof(ISettingsSection);
|
||||
|
||||
/// <summary>
|
||||
/// 获取具体的设置节,此事件中始终为 null
|
||||
/// </summary>
|
||||
public ISettingsSection Settings => null!;
|
||||
|
||||
/// <summary>
|
||||
/// 获取事件发生的时间(UTC时间)
|
||||
/// </summary>
|
||||
public DateTime ChangedAt { get; } = DateTime.UtcNow;
|
||||
}
|
||||
41
GFramework.Game/setting/events/SettingsAppliedEvent.cs
Normal file
41
GFramework.Game/setting/events/SettingsAppliedEvent.cs
Normal file
@ -0,0 +1,41 @@
|
||||
using GFramework.Game.Abstractions.setting;
|
||||
|
||||
namespace GFramework.Game.setting.events;
|
||||
|
||||
/// <summary>
|
||||
/// 表示设置应用完成事件
|
||||
/// </summary>
|
||||
/// <typeparam name="T">设置节类型,必须实现ISettingsSection接口</typeparam>
|
||||
public class SettingsAppliedEvent<T>(T settings, bool success, Exception? error = null) : ISettingsChangedEvent
|
||||
where T : ISettingsSection
|
||||
{
|
||||
/// <summary>
|
||||
/// 获取类型化的设置节实例
|
||||
/// </summary>
|
||||
public T TypedSettings => (T)Settings;
|
||||
|
||||
/// <summary>
|
||||
/// 获取设置应用是否成功的状态
|
||||
/// </summary>
|
||||
public bool Success { get; } = success;
|
||||
|
||||
/// <summary>
|
||||
/// 获取设置应用过程中发生的错误异常(如果有的话)
|
||||
/// </summary>
|
||||
public Exception? Error { get; } = error;
|
||||
|
||||
/// <summary>
|
||||
/// 获取设置类型的Type信息
|
||||
/// </summary>
|
||||
public Type SettingsType => typeof(T);
|
||||
|
||||
/// <summary>
|
||||
/// 获取应用的设置节实例
|
||||
/// </summary>
|
||||
public ISettingsSection Settings { get; } = settings;
|
||||
|
||||
/// <summary>
|
||||
/// 获取设置变更的时间戳(UTC时间)
|
||||
/// </summary>
|
||||
public DateTime ChangedAt { get; } = DateTime.UtcNow;
|
||||
}
|
||||
31
GFramework.Game/setting/events/SettingsApplyingEvent.cs
Normal file
31
GFramework.Game/setting/events/SettingsApplyingEvent.cs
Normal file
@ -0,0 +1,31 @@
|
||||
using GFramework.Game.Abstractions.setting;
|
||||
|
||||
namespace GFramework.Game.setting.events;
|
||||
|
||||
/// <summary>
|
||||
/// 表示设置应用事件的泛型类
|
||||
/// </summary>
|
||||
/// <typeparam name="T">设置节类型,必须实现ISettingsSection接口</typeparam>
|
||||
public class SettingsApplyingEvent<T>(T settings) : ISettingsChangedEvent
|
||||
where T : ISettingsSection
|
||||
{
|
||||
/// <summary>
|
||||
/// 获取类型化的设置对象
|
||||
/// </summary>
|
||||
public T TypedSettings { get; } = settings;
|
||||
|
||||
/// <summary>
|
||||
/// 获取设置类型的Type信息
|
||||
/// </summary>
|
||||
public Type SettingsType => typeof(T);
|
||||
|
||||
/// <summary>
|
||||
/// 获取设置节基接口实例
|
||||
/// </summary>
|
||||
public ISettingsSection Settings => TypedSettings;
|
||||
|
||||
/// <summary>
|
||||
/// 获取设置变更的时间戳
|
||||
/// </summary>
|
||||
public DateTime ChangedAt { get; } = DateTime.UtcNow;
|
||||
}
|
||||
31
GFramework.Game/setting/events/SettingsBatchChangedEvent.cs
Normal file
31
GFramework.Game/setting/events/SettingsBatchChangedEvent.cs
Normal file
@ -0,0 +1,31 @@
|
||||
using GFramework.Game.Abstractions.setting;
|
||||
|
||||
namespace GFramework.Game.setting.events;
|
||||
|
||||
/// <summary>
|
||||
/// 批量设置变更事件
|
||||
/// 表示多个设置项同时发生变更的事件
|
||||
/// </summary>
|
||||
/// <param name="settings">发生变更的设置数据集合</param>
|
||||
public class SettingsBatchChangedEvent(IEnumerable<ISettingsData> settings) : ISettingsChangedEvent
|
||||
{
|
||||
/// <summary>
|
||||
/// 获取发生变更的具体设置数据列表
|
||||
/// </summary>
|
||||
public IEnumerable<ISettingsData> ChangedSettings { get; } = settings.ToList();
|
||||
|
||||
/// <summary>
|
||||
/// 获取设置类型,对于批量变更事件,固定返回ISettingsSection类型
|
||||
/// </summary>
|
||||
public Type SettingsType => typeof(ISettingsSection);
|
||||
|
||||
/// <summary>
|
||||
/// 获取设置实例,批量变更事件中此属性返回null
|
||||
/// </summary>
|
||||
public ISettingsSection Settings => null!;
|
||||
|
||||
/// <summary>
|
||||
/// 获取变更发生的时间戳(UTC时间)
|
||||
/// </summary>
|
||||
public DateTime ChangedAt { get; } = DateTime.UtcNow;
|
||||
}
|
||||
30
GFramework.Game/setting/events/SettingsBatchSavedEvent.cs
Normal file
30
GFramework.Game/setting/events/SettingsBatchSavedEvent.cs
Normal file
@ -0,0 +1,30 @@
|
||||
using GFramework.Game.Abstractions.setting;
|
||||
|
||||
namespace GFramework.Game.setting.events;
|
||||
|
||||
/// <summary>
|
||||
/// 表示设置批量保存事件
|
||||
/// </summary>
|
||||
/// <param name="settings">要保存的设置数据集合</param>
|
||||
public class SettingsBatchSavedEvent(IEnumerable<ISettingsData> settings) : ISettingsChangedEvent
|
||||
{
|
||||
/// <summary>
|
||||
/// 获取已保存的设置数据只读集合
|
||||
/// </summary>
|
||||
public IReadOnlyCollection<ISettingsData> SavedSettings { get; } = settings.ToList();
|
||||
|
||||
/// <summary>
|
||||
/// 获取设置类型(始终返回ISettingsSection类型)
|
||||
/// </summary>
|
||||
public Type SettingsType => typeof(ISettingsSection);
|
||||
|
||||
/// <summary>
|
||||
/// 获取设置节(在此事件中始终为null)
|
||||
/// </summary>
|
||||
public ISettingsSection Settings => null!;
|
||||
|
||||
/// <summary>
|
||||
/// 获取更改发生的时间(UTC时间)
|
||||
/// </summary>
|
||||
public DateTime ChangedAt { get; } = DateTime.UtcNow;
|
||||
}
|
||||
32
GFramework.Game/setting/events/SettingsChangedEvent.cs
Normal file
32
GFramework.Game/setting/events/SettingsChangedEvent.cs
Normal file
@ -0,0 +1,32 @@
|
||||
using GFramework.Game.Abstractions.setting;
|
||||
|
||||
namespace GFramework.Game.setting.events;
|
||||
|
||||
/// <summary>
|
||||
/// 泛型设置变更事件
|
||||
/// </summary>
|
||||
/// <typeparam name="T">设置节类型,必须实现ISettingsSection接口</typeparam>
|
||||
/// <param name="settings">设置实例</param>
|
||||
public class SettingsChangedEvent<T>(T settings) : ISettingsChangedEvent
|
||||
where T : ISettingsSection
|
||||
{
|
||||
/// <summary>
|
||||
/// 获取类型化的设置实例
|
||||
/// </summary>
|
||||
public T TypedSettings => (T)Settings;
|
||||
|
||||
/// <summary>
|
||||
/// 获取设置类型
|
||||
/// </summary>
|
||||
public Type SettingsType => typeof(T);
|
||||
|
||||
/// <summary>
|
||||
/// 获取设置实例
|
||||
/// </summary>
|
||||
public ISettingsSection Settings { get; } = settings;
|
||||
|
||||
/// <summary>
|
||||
/// 获取变更时间
|
||||
/// </summary>
|
||||
public DateTime ChangedAt { get; } = DateTime.UtcNow;
|
||||
}
|
||||
31
GFramework.Game/setting/events/SettingsLoadedEvent.cs
Normal file
31
GFramework.Game/setting/events/SettingsLoadedEvent.cs
Normal file
@ -0,0 +1,31 @@
|
||||
using GFramework.Game.Abstractions.setting;
|
||||
|
||||
namespace GFramework.Game.setting.events;
|
||||
|
||||
/// <summary>
|
||||
/// 表示设置加载完成事件的泛型类
|
||||
/// </summary>
|
||||
/// <typeparam name="T">设置节类型,必须实现ISettingsSection接口</typeparam>
|
||||
public class SettingsLoadedEvent<T>(T settings) : ISettingsChangedEvent
|
||||
where T : ISettingsSection
|
||||
{
|
||||
/// <summary>
|
||||
/// 获取类型化的设置对象
|
||||
/// </summary>
|
||||
public T TypedSettings { get; } = settings;
|
||||
|
||||
/// <summary>
|
||||
/// 获取设置类型的Type信息
|
||||
/// </summary>
|
||||
public Type SettingsType => typeof(T);
|
||||
|
||||
/// <summary>
|
||||
/// 获取设置节基接口对象
|
||||
/// </summary>
|
||||
public ISettingsSection Settings => TypedSettings;
|
||||
|
||||
/// <summary>
|
||||
/// 获取事件发生的时间戳
|
||||
/// </summary>
|
||||
public DateTime ChangedAt { get; } = DateTime.UtcNow;
|
||||
}
|
||||
31
GFramework.Game/setting/events/SettingsSavedEvent.cs
Normal file
31
GFramework.Game/setting/events/SettingsSavedEvent.cs
Normal file
@ -0,0 +1,31 @@
|
||||
using GFramework.Game.Abstractions.setting;
|
||||
|
||||
namespace GFramework.Game.setting.events;
|
||||
|
||||
/// <summary>
|
||||
/// 表示设置保存事件的泛型类
|
||||
/// </summary>
|
||||
/// <typeparam name="T">设置节类型,必须实现ISettingsSection接口</typeparam>
|
||||
public class SettingsSavedEvent<T>(T settings) : ISettingsChangedEvent
|
||||
where T : ISettingsSection
|
||||
{
|
||||
/// <summary>
|
||||
/// 获取类型化的设置对象
|
||||
/// </summary>
|
||||
public T TypedSettings { get; } = settings;
|
||||
|
||||
/// <summary>
|
||||
/// 获取设置类型的Type信息
|
||||
/// </summary>
|
||||
public Type SettingsType => typeof(T);
|
||||
|
||||
/// <summary>
|
||||
/// 获取设置节对象
|
||||
/// </summary>
|
||||
public ISettingsSection Settings => TypedSettings;
|
||||
|
||||
/// <summary>
|
||||
/// 获取设置更改的时间戳
|
||||
/// </summary>
|
||||
public DateTime ChangedAt { get; } = DateTime.UtcNow;
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user