From c93d32c495c5018a95590feb2ffd055d3f00c670 Mon Sep 17 00:00:00 2001 From: GeWuYou <95328647+GeWuYou@users.noreply.github.com> Date: Sat, 31 Jan 2026 19:44:59 +0800 Subject: [PATCH] =?UTF-8?q?feat(data):=20=E5=AE=9E=E7=8E=B0=E8=AE=BE?= =?UTF-8?q?=E7=BD=AE=E6=95=B0=E6=8D=AE=E7=B1=BB=E5=9E=8B=E7=9A=84=E5=8A=A8?= =?UTF-8?q?=E6=80=81=E6=B3=A8=E5=86=8C=E5=92=8C=E5=8F=8D=E5=BA=8F=E5=88=97?= =?UTF-8?q?=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 添加类型注册表支持动态类型映射 - 修改LoadAllAsync方法使用注册类型进行反序列化 - 在SettingsModel初始化时自动注册数据类型到仓库 - 添加RegisterDataType接口方法支持类型注册功能 - 移除原有的无类型约束反序列化逻辑 - 增强数据加载的安全性和准确性 - [release ci] --- .../data/ISettingsDataRepository.cs | 7 +++++ .../data/UnifiedSettingsDataRepository.cs | 28 ++++++++++++++++--- GFramework.Game/setting/SettingsModel.cs | 6 ++++ 3 files changed, 37 insertions(+), 4 deletions(-) diff --git a/GFramework.Game.Abstractions/data/ISettingsDataRepository.cs b/GFramework.Game.Abstractions/data/ISettingsDataRepository.cs index d1615d5..6b9959f 100644 --- a/GFramework.Game.Abstractions/data/ISettingsDataRepository.cs +++ b/GFramework.Game.Abstractions/data/ISettingsDataRepository.cs @@ -31,4 +31,11 @@ public interface ISettingsDataRepository : IDataRepository /// 此方法将从数据源中异步读取所有可用的设置项,并将其组织成字典格式返回 /// Task> LoadAllAsync(); + + /// + /// 注册数据类型到类型注册表中 + /// + /// 数据位置信息,用于获取键值 + /// 数据类型 + void RegisterDataType(IDataLocation location, Type type); } \ No newline at end of file diff --git a/GFramework.Game/data/UnifiedSettingsDataRepository.cs b/GFramework.Game/data/UnifiedSettingsDataRepository.cs index d2c020d..bbb4b88 100644 --- a/GFramework.Game/data/UnifiedSettingsDataRepository.cs +++ b/GFramework.Game/data/UnifiedSettingsDataRepository.cs @@ -32,6 +32,7 @@ public class UnifiedSettingsDataRepository( { private readonly SemaphoreSlim _lock = new(1, 1); private readonly DataRepositoryOptions _options = options ?? new DataRepositoryOptions(); + private readonly Dictionary _typeRegistry = new(); private UnifiedSettingsFile? _file; private bool _loaded; private IRuntimeTypeSerializer? _serializer = serializer; @@ -158,10 +159,29 @@ public class UnifiedSettingsDataRepository( public async Task> LoadAllAsync() { await EnsureLoadedAsync(); - return File.Sections.ToDictionary( - kv => kv.Key, - kv => Serializer.Deserialize(kv.Value) - ); + + var result = new Dictionary(); + + foreach (var (key, raw) in File.Sections) + { + if (!_typeRegistry.TryGetValue(key, out var type)) + continue; + + var data = (IData)Serializer.Deserialize(raw, type); + result[key] = data; + } + + return result; + } + + /// + /// 注册数据类型到类型注册表中 + /// + /// 数据位置信息,用于获取键值 + /// 数据类型 + public void RegisterDataType(IDataLocation location, Type type) + { + _typeRegistry[location.Key] = type; } protected override void OnInit() diff --git a/GFramework.Game/setting/SettingsModel.cs b/GFramework.Game/setting/SettingsModel.cs index 3185dce..6a0a324 100644 --- a/GFramework.Game/setting/SettingsModel.cs +++ b/GFramework.Game/setting/SettingsModel.cs @@ -29,6 +29,7 @@ public class SettingsModel(IDataLocationProvider? locationProvider, private readonly ConcurrentDictionary _data = new(); private readonly ConcurrentDictionary> _migrationCache = new(); private readonly ConcurrentDictionary<(Type type, int from), ISettingsMigration> _migrations = new(); + private IDataLocationProvider? _locationProvider = locationProvider; private ISettingsDataRepository? _repository = repository; @@ -215,6 +216,11 @@ public class SettingsModel(IDataLocationProvider? locationProvider, { _repository ??= this.GetUtility()!; _locationProvider ??= this.GetUtility()!; + foreach (var type in _data.Keys) + { + var location = _locationProvider.GetLocation(type); + DataRepository.RegisterDataType(location, type); + } } private ISettingsData MigrateIfNeeded(ISettingsData data)