mirror of
https://github.com/GeWuYou/GFramework.git
synced 2026-03-23 03:04:29 +08:00
- 将SettingsModel内部存储分离为_dataSettings和_applicators两个字典 - 添加IDataSettings接口用于标识纯数据设置 - 修改Get方法为GetData以明确区分数据获取 - 添加RegisterApplicator和GetApplicator方法管理可应用设置 - 更新TryGet方法支持从数据和应用器中查找设置 - 扩展SettingsPersistence支持批量保存和加载所有设置数据 - 将AudioBusMap重命名为AudioBusMapSettings并实现ISettingsData接口 - 修改Godot音频和图形设置适配新的接口变更 - [skip ci]
101 lines
3.3 KiB
C#
101 lines
3.3 KiB
C#
using GFramework.Core.model;
|
||
using GFramework.Game.Abstractions.setting;
|
||
|
||
namespace GFramework.Game.setting;
|
||
|
||
/// <summary>
|
||
/// 设置模型类,用于管理不同类型的应用程序设置部分
|
||
/// </summary>
|
||
public class SettingsModel : AbstractModel, ISettingsModel
|
||
{
|
||
private readonly Dictionary<Type, IApplyAbleSettings> _applicators = new();
|
||
private readonly Dictionary<Type, ISettingsData> _dataSettings = new();
|
||
|
||
/// <summary>
|
||
/// 获取或创建数据设置
|
||
/// </summary>
|
||
/// <typeparam name="T">设置数据类型,必须实现ISettingsData接口并具有无参构造函数</typeparam>
|
||
/// <returns>指定类型的设置数据实例</returns>
|
||
public T GetData<T>() where T : class, ISettingsData, new()
|
||
{
|
||
var type = typeof(T);
|
||
|
||
// 尝试从现有字典中获取已存在的设置数据
|
||
if (_dataSettings.TryGetValue(type, out var existing))
|
||
return (T)existing;
|
||
|
||
// 创建新的设置数据实例并存储到字典中
|
||
var created = new T();
|
||
_dataSettings[type] = created;
|
||
return created;
|
||
}
|
||
|
||
/// <summary>
|
||
/// 注册可应用设置
|
||
/// </summary>
|
||
/// <typeparam name="T">可应用设置类型,必须实现IApplyAbleSettings接口</typeparam>
|
||
/// <param name="applicator">要注册的可应用设置实例</param>
|
||
public void RegisterApplicator<T>(T applicator) where T : class, IApplyAbleSettings
|
||
{
|
||
var type = typeof(T);
|
||
_applicators[type] = applicator;
|
||
}
|
||
|
||
/// <summary>
|
||
/// 获取已注册的可应用设置
|
||
/// </summary>
|
||
/// <typeparam name="T">可应用设置类型,必须实现IApplyAbleSettings接口</typeparam>
|
||
/// <returns>找到的可应用设置实例,如果未找到则返回null</returns>
|
||
public T? GetApplicator<T>() where T : class, IApplyAbleSettings
|
||
{
|
||
var type = typeof(T);
|
||
return _applicators.TryGetValue(type, out var applicator)
|
||
? (T)applicator
|
||
: null;
|
||
}
|
||
|
||
/// <summary>
|
||
/// 尝试获取指定类型的设置节
|
||
/// </summary>
|
||
/// <param name="type">要查找的设置类型</param>
|
||
/// <param name="section">输出参数,找到的设置节实例</param>
|
||
/// <returns>如果找到设置节则返回true,否则返回false</returns>
|
||
public bool TryGet(Type type, out ISettingsSection section)
|
||
{
|
||
// 首先在数据设置字典中查找
|
||
if (_dataSettings.TryGetValue(type, out var data))
|
||
{
|
||
section = data;
|
||
return true;
|
||
}
|
||
|
||
// 然后在应用器字典中查找
|
||
if (_applicators.TryGetValue(type, out var applicator))
|
||
{
|
||
section = applicator;
|
||
return true;
|
||
}
|
||
|
||
section = null!;
|
||
return false;
|
||
}
|
||
|
||
/// <summary>
|
||
/// 获取所有设置节的集合
|
||
/// </summary>
|
||
/// <returns>包含所有设置节的可枚举集合</returns>
|
||
public IEnumerable<ISettingsSection> All()
|
||
{
|
||
// 合并数据设置和应用器设置的所有值
|
||
return _dataSettings.Values
|
||
.Concat(_applicators.Values.Cast<ISettingsSection>());
|
||
}
|
||
|
||
|
||
/// <summary>
|
||
/// 初始化方法,用于执行模型的初始化逻辑
|
||
/// </summary>
|
||
protected override void OnInit()
|
||
{
|
||
}
|
||
} |