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)