GFramework/GFramework.Game/setting/SettingsSystem.cs
GeWuYou 07f5d27ab3 refactor(setting): 重构设置系统的接口设计
- 移除 SettingsModel 中的 All() 方法,避免同时实现两个接口的设置被重复返回
- 添加 AllApplicators() 方法用于获取所有可应用设置
- 添加 AllData() 方法用于获取所有设置数据
- 修改 SettingsSystem.ApplyAll() 方法,直接遍历可应用设置而非设置节
- 更新 ISettingsModel 接口定义,将 All() 方法拆分为 AllData() 和 AllApplicators()
- 移除 SettingsSystem 中的 Apply(Type) 和 Apply(IEnumerable<Type>) 重载方法
- 更新 Apply<T>() 泛型约束从 ISettingsSection 改为 IApplyAbleSettings
- 移除注册时对 ISettingsData 的自动注册逻辑,保持职责分离
2026-01-27 22:11:01 +08:00

69 lines
2.0 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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 async Task ApplyAll()
{
foreach (var applicator in _model.AllApplicators())
{
await TryApplyAsync(applicator);
}
}
/// <summary>
/// 应用指定类型的设置配置
/// </summary>
/// <typeparam name="T">设置配置类型必须是类且实现ISettingsSection接口</typeparam>
/// <returns>完成的任务</returns>
public Task Apply<T>() where T : class, IApplyAbleSettings
{
var applicator = _model.GetApplicator<T>();
return applicator != null
? TryApplyAsync(applicator)
: Task.CompletedTask;
}
/// <summary>
/// 初始化设置系统,获取设置模型实例
/// </summary>
protected override void OnInit()
{
_model = this.GetModel<ISettingsModel>()!;
}
/// <summary>
/// 尝试应用可应用的设置配置
/// </summary>
/// <param name="section">设置配置对象</param>
private async Task TryApplyAsync(ISettingsSection section)
{
if (section is not IApplyAbleSettings applyAbleSettings) return;
this.SendEvent(new SettingsApplyingEvent<ISettingsSection>(section));
try
{
await applyAbleSettings.Apply();
this.SendEvent(new SettingsAppliedEvent<ISettingsSection>(section, true));
}
catch (Exception ex)
{
this.SendEvent(new SettingsAppliedEvent<ISettingsSection>(section, false, ex));
}
}
}