mirror of
https://github.com/GeWuYou/GFramework.git
synced 2026-03-22 10:34:30 +08:00
refactor(setting): 重构设置系统异步操作实现
- 移除原有的重置设置命令相关文件和方法 - 添加异步命令发送接口的默认实现 - 实现设置系统的异步应用功能 - 优化设置应用逻辑并添加泛型支持 - 统一任务完成状态返回方式
This commit is contained in:
parent
e29b6da9e1
commit
3137c85449
@ -250,6 +250,17 @@ public class TestArchitectureContextV3 : IArchitectureContext
|
||||
}
|
||||
|
||||
public TResult SendCommand<TResult>(ICommand<TResult> command) => default!;
|
||||
|
||||
public Task SendCommandAsync(IAsyncCommand command)
|
||||
{
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
public Task<TResult> SendCommandAsync<TResult>(IAsyncCommand<TResult> command)
|
||||
{
|
||||
return (Task<TResult>)Task.CompletedTask;
|
||||
}
|
||||
|
||||
public TResult SendQuery<TResult>(IQuery<TResult> query) => default!;
|
||||
public IEnvironment GetEnvironment() => _environment;
|
||||
}
|
||||
|
||||
@ -327,6 +327,16 @@ public class TestArchitectureContext : IArchitectureContext
|
||||
/// <returns>命令执行结果</returns>
|
||||
public TResult SendCommand<TResult>(ICommand<TResult> command) => default!;
|
||||
|
||||
public Task SendCommandAsync(IAsyncCommand command)
|
||||
{
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
public Task<TResult> SendCommandAsync<TResult>(IAsyncCommand<TResult> command)
|
||||
{
|
||||
return (Task<TResult>)Task.CompletedTask;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 发送查询请求
|
||||
/// </summary>
|
||||
|
||||
@ -28,23 +28,5 @@ public interface ISettingsSystem : ISystem
|
||||
/// <summary>
|
||||
/// 批量应用多个设置类型
|
||||
/// </summary>
|
||||
/// <param name="settingsTypes">设置配置类型集合</param>
|
||||
Task Apply(IEnumerable<Type> settingsTypes);
|
||||
|
||||
/// <summary>
|
||||
/// 重置指定类型的设置
|
||||
/// </summary>
|
||||
/// <param name="settingsType">设置类型</param>
|
||||
Task ResetAsync(Type settingsType);
|
||||
|
||||
/// <summary>
|
||||
/// 重置指定类型的设置(泛型版本)
|
||||
/// </summary>
|
||||
/// <typeparam name="T">设置类型</typeparam>
|
||||
Task ResetAsync<T>() where T : class, ISettingsData, new();
|
||||
|
||||
/// <summary>
|
||||
/// 重置所有设置
|
||||
/// </summary>
|
||||
Task ResetAllAsync();
|
||||
}
|
||||
@ -1,7 +1,6 @@
|
||||
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;
|
||||
@ -28,43 +27,73 @@ public class SettingsSystem : AbstractSystem, ISettingsSystem
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
public Task ResetAsync(Type settingsType)
|
||||
/// <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)
|
||||
{
|
||||
return this.SendCommandAsync(new ResetSettingsCommand(new ResetSettingsInput
|
||||
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())
|
||||
{
|
||||
SettingsType = settingsType
|
||||
}));
|
||||
if (_model.TryGet(type, out var section))
|
||||
{
|
||||
TryApply(section);
|
||||
}
|
||||
}
|
||||
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
public Task ResetAsync<T>() where T : class, ISettingsData, new()
|
||||
/// <summary>
|
||||
/// 初始化设置系统,获取设置模型实例
|
||||
/// </summary>
|
||||
protected override void OnInit()
|
||||
{
|
||||
return ResetAsync(typeof(T));
|
||||
}
|
||||
|
||||
public Task ResetAllAsync()
|
||||
{
|
||||
return this.SendCommandAsync(new ResetSettingsCommand(new ResetSettingsInput
|
||||
{
|
||||
SettingsType = null
|
||||
}));
|
||||
_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));
|
||||
if (section is not IApplyAbleSettings applyAbleSettings) return;
|
||||
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;
|
||||
}
|
||||
try
|
||||
{
|
||||
applyAbleSettings.Apply();
|
||||
this.SendEvent(new SettingsAppliedEvent<ISettingsSection>(section, true));
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
this.SendEvent(new SettingsAppliedEvent<ISettingsSection>(section, false, ex));
|
||||
throw;
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -1,54 +0,0 @@
|
||||
using GFramework.Core.command;
|
||||
using GFramework.Game.Abstractions.setting;
|
||||
using GFramework.Game.setting.events;
|
||||
|
||||
namespace GFramework.Game.setting.commands;
|
||||
|
||||
public sealed class ResetSettingsCommand : AbstractAsyncCommand<ResetSettingsInput>
|
||||
{
|
||||
private ISettingsModel _model = null!;
|
||||
private ISettingsPersistence _persistence = null!;
|
||||
|
||||
protected override void OnContextReady()
|
||||
{
|
||||
base.OnContextReady();
|
||||
_model = this.GetModel<ISettingsModel>()!;
|
||||
_persistence = this.GetUtility<ISettingsPersistence>()!;
|
||||
}
|
||||
|
||||
protected override async Task OnExecuteAsync(ResetSettingsInput input)
|
||||
{
|
||||
if (input.SettingsType == null)
|
||||
await ResetAll();
|
||||
else
|
||||
await ResetSingle(input.SettingsType);
|
||||
}
|
||||
|
||||
private async Task ResetSingle(Type settingsType)
|
||||
{
|
||||
if (!_model.TryGet(settingsType, out var section))
|
||||
throw new InvalidOperationException($"Settings {settingsType.Name} not found");
|
||||
|
||||
if (section is not ISettingsData settingsData)
|
||||
throw new InvalidOperationException($"Settings {settingsType.Name} is not ISettingsData");
|
||||
|
||||
settingsData.Reset();
|
||||
await _persistence.SaveAsync(settingsData);
|
||||
this.SendEvent(new SettingsResetEvent<ISettingsSection>(settingsData));
|
||||
}
|
||||
|
||||
private async Task ResetAll()
|
||||
{
|
||||
var allSettings = _model.All()
|
||||
.OfType<ISettingsData>()
|
||||
.ToList();
|
||||
|
||||
foreach (var settings in allSettings)
|
||||
{
|
||||
settings.Reset();
|
||||
await _persistence.SaveAsync(settings);
|
||||
}
|
||||
|
||||
this.SendEvent(new SettingsResetAllEvent(allSettings));
|
||||
}
|
||||
}
|
||||
@ -1,8 +0,0 @@
|
||||
using GFramework.Core.Abstractions.command;
|
||||
|
||||
namespace GFramework.Game.setting.commands;
|
||||
|
||||
public sealed class ResetSettingsInput : ICommandInput
|
||||
{
|
||||
public Type? SettingsType { get; init; }
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user