mirror of
https://github.com/GeWuYou/GFramework.git
synced 2026-03-22 10:34:30 +08:00
refactor(setting): 重构设置事件系统
- 移除旧的设置相关事件类包括 SettingsAllLoadedEvent、SettingsBatchChangedEvent、 SettingsBatchSavedEvent、SettingsChangedEvent、SettingsDeletedEvent 和 SettingsLoadedEvent - 添加新的设置事件类包括 SettingsAppliedAllEvent、SettingsInitializedEvent、 SettingsSavedAllEvent 和 SettingsResetEvent - 在 SettingsModel 中集成新的事件发送机制 - 修改 Initialize 方法以发送 SettingsInitializedEvent 事件 - 更新 Save 方法以发送 SettingsSavedAllEvent 事件 - 修改 Apply 方法以发送 SettingsAppliedAllEvent 事件 - 添加 Reset 功能以发送 SettingsResetEvent 事件 - 添加 ResetAll 功能以发送 SettingsResetAllEvent 事件 - [release ci]
This commit is contained in:
parent
829c7a3b06
commit
af64c4ab3e
@ -4,6 +4,7 @@ using GFramework.Core.logging;
|
||||
using GFramework.Core.model;
|
||||
using GFramework.Game.Abstractions.data;
|
||||
using GFramework.Game.Abstractions.setting;
|
||||
using GFramework.Game.setting.events;
|
||||
|
||||
namespace GFramework.Game.setting;
|
||||
|
||||
@ -135,6 +136,8 @@ public class SettingsModel<TRepository>(IDataLocationProvider? locationProvider,
|
||||
Log.Error($"Failed to initialize settings data: {data.GetType().Name}", ex);
|
||||
}
|
||||
}
|
||||
|
||||
this.SendEvent(new SettingsInitializedEvent());
|
||||
}
|
||||
|
||||
|
||||
@ -155,6 +158,8 @@ public class SettingsModel<TRepository>(IDataLocationProvider? locationProvider,
|
||||
Log.Error($"Failed to save settings data: {data.GetType().Name}", ex);
|
||||
}
|
||||
}
|
||||
|
||||
this.SendEvent(new SettingsSavedAllEvent());
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@ -173,6 +178,8 @@ public class SettingsModel<TRepository>(IDataLocationProvider? locationProvider,
|
||||
Log.Error($"Failed to apply settings: {applicator.GetType().Name}", ex);
|
||||
}
|
||||
}
|
||||
|
||||
this.SendEvent(new SettingsAppliedAllEvent());
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@ -183,6 +190,7 @@ public class SettingsModel<TRepository>(IDataLocationProvider? locationProvider,
|
||||
{
|
||||
var data = GetData<T>();
|
||||
data.Reset();
|
||||
this.SendEvent(new SettingsResetEvent<T>(data));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@ -195,6 +203,7 @@ public class SettingsModel<TRepository>(IDataLocationProvider? locationProvider,
|
||||
|
||||
foreach (var applicator in _applicators)
|
||||
applicator.Value.Reset();
|
||||
this.SendEvent(new SettingsResetAllEvent(_data.Values));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
||||
@ -1,30 +0,0 @@
|
||||
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;
|
||||
}
|
||||
16
GFramework.Game/setting/events/SettingsAppliedAllEvent.cs
Normal file
16
GFramework.Game/setting/events/SettingsAppliedAllEvent.cs
Normal file
@ -0,0 +1,16 @@
|
||||
// Copyright (c) 2026 GeWuYou
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
namespace GFramework.Game.setting.events;
|
||||
|
||||
public sealed class SettingsAppliedAllEvent;
|
||||
@ -1,31 +0,0 @@
|
||||
using GFramework.Game.Abstractions.setting;
|
||||
|
||||
namespace GFramework.Game.setting.events;
|
||||
|
||||
/// <summary>
|
||||
/// 批量设置变更事件
|
||||
/// 表示多个设置项同时发生变更的事件
|
||||
/// </summary>
|
||||
/// <param name="settings">发生变更的设置数据集合</param>
|
||||
public class SettingsBatchChangedEvent(IEnumerable<IResettable> settings) : ISettingsChangedEvent
|
||||
{
|
||||
/// <summary>
|
||||
/// 获取发生变更的具体设置数据列表
|
||||
/// </summary>
|
||||
public IEnumerable<IResettable> 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;
|
||||
}
|
||||
@ -1,30 +0,0 @@
|
||||
using GFramework.Game.Abstractions.setting;
|
||||
|
||||
namespace GFramework.Game.setting.events;
|
||||
|
||||
/// <summary>
|
||||
/// 表示设置批量保存事件
|
||||
/// </summary>
|
||||
/// <param name="settings">要保存的设置数据集合</param>
|
||||
public class SettingsBatchSavedEvent(IEnumerable<IResettable> settings) : ISettingsChangedEvent
|
||||
{
|
||||
/// <summary>
|
||||
/// 获取已保存的设置数据只读集合
|
||||
/// </summary>
|
||||
public IReadOnlyCollection<IResettable> 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;
|
||||
}
|
||||
@ -1,32 +0,0 @@
|
||||
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;
|
||||
}
|
||||
@ -1,24 +0,0 @@
|
||||
using GFramework.Game.Abstractions.setting;
|
||||
|
||||
namespace GFramework.Game.setting.events;
|
||||
|
||||
/// <summary>
|
||||
/// 表示设置删除事件
|
||||
/// </summary>
|
||||
public class SettingsDeletedEvent(Type settingsType) : ISettingsChangedEvent
|
||||
{
|
||||
/// <summary>
|
||||
/// 获取被删除的设置类型
|
||||
/// </summary>
|
||||
public Type SettingsType { get; } = settingsType;
|
||||
|
||||
/// <summary>
|
||||
/// 获取设置实例,删除事件中返回 null
|
||||
/// </summary>
|
||||
public ISettingsSection Settings => null!;
|
||||
|
||||
/// <summary>
|
||||
/// 获取删除时间
|
||||
/// </summary>
|
||||
public DateTime ChangedAt { get; } = DateTime.UtcNow;
|
||||
}
|
||||
16
GFramework.Game/setting/events/SettingsInitializedEvent.cs
Normal file
16
GFramework.Game/setting/events/SettingsInitializedEvent.cs
Normal file
@ -0,0 +1,16 @@
|
||||
// Copyright (c) 2026 GeWuYou
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
namespace GFramework.Game.setting.events;
|
||||
|
||||
public sealed class SettingsInitializedEvent;
|
||||
@ -1,31 +0,0 @@
|
||||
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;
|
||||
}
|
||||
16
GFramework.Game/setting/events/SettingsSavedAllEvent.cs
Normal file
16
GFramework.Game/setting/events/SettingsSavedAllEvent.cs
Normal file
@ -0,0 +1,16 @@
|
||||
// Copyright (c) 2026 GeWuYou
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
namespace GFramework.Game.setting.events;
|
||||
|
||||
public sealed class SettingsSavedAllEvent;
|
||||
@ -1,31 +0,0 @@
|
||||
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