mirror of
https://github.com/GeWuYou/GFramework.git
synced 2026-03-22 10:34:30 +08:00
- 新增 IVersioned 接口用于定义版本信息 - 实现设置数据自动迁移机制,支持版本升级转换 - 添加 SettingsMigration 接口用于处理不同版本间的数据转换 - 在 SettingsModel 中集成迁移功能,加载时自动执行版本迁移 - 优化设置数据管理,新增 AllData 方法获取所有设置数据 - 重构代码结构,添加清晰的方法分组注释块
188 lines
6.1 KiB
C#
188 lines
6.1 KiB
C#
using GFramework.Core.Abstractions.versioning;
|
||
using GFramework.Core.extensions;
|
||
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();
|
||
private readonly Dictionary<(Type type, int from), ISettingsMigration> _migrations = new();
|
||
|
||
private ISettingsPersistence? _persistence;
|
||
|
||
// -----------------------------
|
||
// Data
|
||
// -----------------------------
|
||
|
||
/// <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 data))
|
||
return (T)data;
|
||
|
||
var created = new T();
|
||
_dataSettings[type] = created;
|
||
return created;
|
||
}
|
||
|
||
/// <summary>
|
||
/// 获取所有设置数据的枚举集合
|
||
/// </summary>
|
||
/// <returns>所有设置数据的枚举集合</returns>
|
||
public IEnumerable<ISettingsData> AllData()
|
||
=> _dataSettings.Values;
|
||
|
||
// -----------------------------
|
||
// Applicator
|
||
// -----------------------------
|
||
|
||
/// <summary>
|
||
/// 获取所有设置应用器的枚举集合
|
||
/// </summary>
|
||
/// <returns>所有设置应用器的枚举集合</returns>
|
||
public IEnumerable<IApplyAbleSettings> AllApplicators()
|
||
=> _applicators.Values;
|
||
|
||
/// <summary>
|
||
/// 注册设置应用器到模型中
|
||
/// </summary>
|
||
/// <typeparam name="T">设置应用器类型,必须实现IApplyAbleSettings接口</typeparam>
|
||
/// <param name="applicator">要注册的设置应用器实例</param>
|
||
/// <returns>当前设置模型实例,支持链式调用</returns>
|
||
public ISettingsModel RegisterApplicator<T>(T applicator)
|
||
where T : class, IApplyAbleSettings
|
||
{
|
||
_applicators[typeof(T)] = applicator;
|
||
return this;
|
||
}
|
||
|
||
/// <summary>
|
||
/// 获取指定类型的设置应用器实例
|
||
/// </summary>
|
||
/// <typeparam name="T">设置应用器类型,必须实现IApplyAbleSettings接口</typeparam>
|
||
/// <returns>指定类型的设置应用器实例,如果不存在则返回null</returns>
|
||
public T? GetApplicator<T>() where T : class, IApplyAbleSettings
|
||
{
|
||
return _applicators.TryGetValue(typeof(T), out var app)
|
||
? (T)app
|
||
: null;
|
||
}
|
||
|
||
// -----------------------------
|
||
// Section lookup
|
||
// -----------------------------
|
||
|
||
/// <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;
|
||
}
|
||
|
||
// -----------------------------
|
||
// Migration
|
||
// -----------------------------
|
||
|
||
/// <summary>
|
||
/// 注册设置迁移器到模型中
|
||
/// </summary>
|
||
/// <param name="migration">要注册的设置迁移器实例</param>
|
||
/// <returns>当前设置模型实例,支持链式调用</returns>
|
||
public ISettingsModel RegisterMigration(ISettingsMigration migration)
|
||
{
|
||
_migrations[(migration.SettingsType, migration.FromVersion)] = migration;
|
||
return this;
|
||
}
|
||
|
||
/// <summary>
|
||
/// 如果需要的话,对设置节进行版本迁移
|
||
/// </summary>
|
||
/// <param name="section">待检查和迁移的设置节</param>
|
||
/// <returns>迁移后的设置节</returns>
|
||
private ISettingsSection MigrateIfNeeded(ISettingsSection section)
|
||
{
|
||
if (section is not IVersioned versioned)
|
||
return section;
|
||
|
||
var type = section.GetType();
|
||
var current = section;
|
||
|
||
while (_migrations.TryGetValue((type, versioned.Version), out var migration))
|
||
{
|
||
current = migration.Migrate(current);
|
||
versioned = (IVersioned)current;
|
||
}
|
||
|
||
return current;
|
||
}
|
||
|
||
// -----------------------------
|
||
// Load / Init
|
||
// -----------------------------
|
||
|
||
/// <summary>
|
||
/// 异步初始化设置模型,加载指定类型的设置数据
|
||
/// </summary>
|
||
/// <param name="settingTypes">要初始化的设置类型数组</param>
|
||
public async Task InitializeAsync(params Type[] settingTypes)
|
||
{
|
||
foreach (var type in settingTypes)
|
||
{
|
||
if (!typeof(ISettingsData).IsAssignableFrom(type) ||
|
||
!type.IsClass ||
|
||
type.GetConstructor(Type.EmptyTypes) == null)
|
||
continue;
|
||
|
||
// Load<T>()
|
||
var method = typeof(ISettingsPersistence)
|
||
.GetMethod(nameof(ISettingsPersistence.LoadAsync))!
|
||
.MakeGenericMethod(type);
|
||
|
||
var task = (Task)method.Invoke(_persistence, null)!;
|
||
await task;
|
||
|
||
var loaded = (ISettingsSection)((dynamic)task).Result;
|
||
|
||
// ★ 关键:迁移
|
||
var migrated = MigrateIfNeeded(loaded);
|
||
|
||
_dataSettings[type] = (ISettingsData)migrated;
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// 初始化方法,用于获取设置持久化服务
|
||
/// </summary>
|
||
protected override void OnInit()
|
||
{
|
||
_persistence = this.GetUtility<ISettingsPersistence>();
|
||
}
|
||
} |