using GFramework.Core.model; using GFramework.Game.Abstractions.setting; namespace GFramework.Game.setting; /// /// 设置模型类,用于管理不同类型的应用程序设置部分 /// public class SettingsModel : AbstractModel, ISettingsModel { private readonly Dictionary _applicators = new(); private readonly Dictionary _dataSettings = new(); /// /// 获取或创建数据设置 /// /// 设置数据类型,必须实现ISettingsData接口并具有无参构造函数 /// 指定类型的设置数据实例 public T GetData() 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; } /// /// 注册可应用设置 /// /// 可应用设置类型,必须实现IApplyAbleSettings接口 /// 要注册的可应用设置实例 public void RegisterApplicator(T applicator) where T : class, IApplyAbleSettings { var type = typeof(T); _applicators[type] = applicator; } /// /// 获取已注册的可应用设置 /// /// 可应用设置类型,必须实现IApplyAbleSettings接口 /// 找到的可应用设置实例,如果未找到则返回null public T? GetApplicator() where T : class, IApplyAbleSettings { var type = typeof(T); return _applicators.TryGetValue(type, out var applicator) ? (T)applicator : null; } /// /// 尝试获取指定类型的设置节 /// /// 要查找的设置类型 /// 输出参数,找到的设置节实例 /// 如果找到设置节则返回true,否则返回false 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; } /// /// 获取所有设置节的集合 /// /// 包含所有设置节的可枚举集合 public IEnumerable All() { // 合并数据设置和应用器设置的所有值 return _dataSettings.Values .Concat(_applicators.Values.Cast()); } /// /// 初始化方法,用于执行模型的初始化逻辑 /// protected override void OnInit() { } }