GFramework/GFramework.Game/setting/SettingsSystem.cs
GeWuYou a45bf27c8b refactor(setting): 重构设置重置功能实现
- 移除 SettingsResetEvent 中的旧设置属性,改为仅保存新设置
- 删除 SettingsPersistence 中的重置方法,统一通过命令模式处理
- 在 SettingsSystem 中添加 ResetAsync 方法并集成命令模式
- 为 AudioSettings 和 GraphicsSettings 添加 Reset 方法实现
- 扩展 ISettingsData 接口添加 Reset 方法定义
- 从接口中移除重置相关方法定义
- 在 ISettingsSystem 中添加重置相关的异步方法声明
- 为 AudioBusMapSettings 添加 Reset 方法实现
- 新增 ResetSettingsCommand 和 ResetSettingsInput 实现命令模式
- 添加 SettingsData 抽象基类提供默认的 Reset 实现
- [skip ci]
2026-01-17 16:07:37 +08:00

70 lines
1.8 KiB
C#

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;
/// <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;
}
public Task ResetAsync(Type settingsType)
{
return this.SendCommandAsync(new ResetSettingsCommand(new ResetSettingsInput
{
SettingsType = settingsType
}));
}
public Task ResetAsync<T>() where T : class, ISettingsData, new()
{
return ResetAsync(typeof(T));
}
public Task ResetAllAsync()
{
return this.SendCommandAsync(new ResetSettingsCommand(new ResetSettingsInput
{
SettingsType = null
}));
}
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;
}
}
}
}