mirror of
https://github.com/GeWuYou/GFramework.git
synced 2026-03-22 19:03:29 +08:00
- 在SettingsModel中添加事件相关依赖引用 - 在SettingsPersistence中实现设置加载、保存、删除的事件发送机制 - 添加SettingsDeletedEvent用于通知设置删除操作 - 添加SettingsResetEvent和SettingsResetAllEvent支持设置重置功能 - 在SettingsPersistence中新增ResetAsync和ResetAllAsync方法 - 修改TryApply方法为实例方法并添加设置应用过程的事件通知 - 添加SettingsApplyingEvent和SettingsAppliedEvent跟踪设置应用状态 - [skip ci]
101 lines
2.8 KiB
C#
101 lines
2.8 KiB
C#
using GFramework.Core.extensions;
|
||
using GFramework.Core.system;
|
||
using GFramework.Game.Abstractions.setting;
|
||
using GFramework.Game.setting.events;
|
||
|
||
namespace GFramework.Game.setting;
|
||
|
||
/// <summary>
|
||
/// 设置系统,负责管理和应用各种设置配置
|
||
/// </summary>
|
||
public class SettingsSystem : AbstractSystem, ISettingsSystem
|
||
{
|
||
private ISettingsModel _model = null!;
|
||
|
||
/// <summary>
|
||
/// 应用所有设置配置
|
||
/// </summary>
|
||
/// <returns>完成的任务</returns>
|
||
public Task ApplyAll()
|
||
{
|
||
// 遍历所有设置配置并尝试应用
|
||
foreach (var section in _model.All())
|
||
{
|
||
TryApply(section);
|
||
}
|
||
|
||
return Task.CompletedTask;
|
||
}
|
||
|
||
/// <summary>
|
||
/// 应用指定类型的设置配置
|
||
/// </summary>
|
||
/// <typeparam name="T">设置配置类型,必须是类且实现ISettingsSection接口</typeparam>
|
||
/// <returns>完成的任务</returns>
|
||
public Task Apply<T>() where T : class, ISettingsSection
|
||
=> Apply(typeof(T));
|
||
|
||
/// <summary>
|
||
/// 应用指定类型的设置配置
|
||
/// </summary>
|
||
/// <param name="settingsType">设置配置类型</param>
|
||
/// <returns>完成的任务</returns>
|
||
public Task Apply(Type settingsType)
|
||
{
|
||
if (!_model.TryGet(settingsType, out var section))
|
||
return Task.CompletedTask;
|
||
|
||
TryApply(section);
|
||
return Task.CompletedTask;
|
||
}
|
||
|
||
/// <summary>
|
||
/// 应用指定类型集合的设置配置
|
||
/// </summary>
|
||
/// <param name="settingsTypes">设置配置类型集合</param>
|
||
/// <returns>完成的任务</returns>
|
||
public Task Apply(IEnumerable<Type> settingsTypes)
|
||
{
|
||
// 去重后遍历设置类型,获取并应用对应的设置配置
|
||
foreach (var type in settingsTypes.Distinct())
|
||
{
|
||
if (_model.TryGet(type, out var section))
|
||
{
|
||
TryApply(section);
|
||
}
|
||
}
|
||
|
||
return Task.CompletedTask;
|
||
}
|
||
|
||
/// <summary>
|
||
/// 初始化设置系统,获取设置模型实例
|
||
/// </summary>
|
||
protected override void OnInit()
|
||
{
|
||
_model = this.GetModel<ISettingsModel>()!;
|
||
}
|
||
|
||
/// <summary>
|
||
/// 尝试应用可应用的设置配置
|
||
/// </summary>
|
||
/// <param name="section">设置配置对象</param>
|
||
private void TryApply(ISettingsSection section)
|
||
{
|
||
if (section is IApplyAbleSettings applyable)
|
||
{
|
||
this.SendEvent(new SettingsApplyingEvent<ISettingsSection>(section));
|
||
|
||
try
|
||
{
|
||
applyable.Apply();
|
||
this.SendEvent(new SettingsAppliedEvent<ISettingsSection>(section, true));
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
this.SendEvent(new SettingsAppliedEvent<ISettingsSection>(section, false, ex));
|
||
throw;
|
||
}
|
||
}
|
||
}
|
||
} |