using GFramework.Game.Abstractions.Config;
namespace GFramework.Game.Config;
///
/// 默认配置注册表实现。
/// 该类型负责统一管理按名称注册的配置表,并在消费端提供类型安全的解析入口。
/// 为了支持开发期热重载,注册行为采用覆盖策略而不是拒绝重复名称。
///
public sealed class ConfigRegistry : IConfigRegistry
{
private const string NameCannotBeNullOrWhiteSpaceMessage = "Table name cannot be null or whitespace.";
private readonly ConcurrentDictionary _tables = new(StringComparer.Ordinal);
///
/// 获取已注册的配置表数量。
///
public int Count => _tables.Count;
///
/// 获取所有已注册配置表的名称集合,按字典序排序。
///
/// 返回只读的配置表名称集合。
public IReadOnlyCollection GetTableNames()
{
return _tables.Keys.OrderBy(static key => key, StringComparer.Ordinal).ToArray();
}
///
/// 注册一个配置表到注册表中。
/// 如果同名的配置表已存在,则会覆盖原有注册以支持热重载。
///
/// 配置表主键的类型,必须为非空类型。
/// 配置表值的类型。
/// 配置表的注册名称,用于后续查找。
/// 要注册的配置表实例。
/// 当 为 null、空或仅包含空白字符时抛出。
/// 当 为 null 时抛出。
public void RegisterTable(string name, IConfigTable table)
where TKey : notnull
{
if (string.IsNullOrWhiteSpace(name))
{
throw new ArgumentException(NameCannotBeNullOrWhiteSpaceMessage, nameof(name));
}
ArgumentNullException.ThrowIfNull(table);
_tables[name] = table;
}
///
/// 根据名称获取已注册的配置表,并进行类型验证。
///
/// 期望的主键类型,必须为非空类型。
/// 期望的值类型。
/// 要查找的配置表名称。
/// 返回类型匹配的配置表实例。
/// 当 为 null、空或仅包含空白字符时抛出。
/// 当指定名称的配置表不存在时抛出。
///
/// 当找到的配置表类型与请求的类型不匹配时抛出。
///
public IConfigTable GetTable(string name)
where TKey : notnull
{
if (string.IsNullOrWhiteSpace(name))
{
throw new ArgumentException(NameCannotBeNullOrWhiteSpaceMessage, nameof(name));
}
if (!_tables.TryGetValue(name, out var table))
{
throw new KeyNotFoundException($"Config table '{name}' was not found.");
}
if (table is IConfigTable typedTable)
{
return typedTable;
}
throw new InvalidOperationException(
$"Config table '{name}' was registered as '{table.KeyType.Name} -> {table.ValueType.Name}', " +
$"but the caller requested '{typeof(TKey).Name} -> {typeof(TValue).Name}'.");
}
///
/// 尝试根据名称获取配置表,操作失败时不会抛出异常。
///
/// 期望的主键类型,必须为非空类型。
/// 期望的值类型。
/// 要查找的配置表名称。
///
/// 输出参数,如果查找成功则返回类型匹配的配置表实例,否则为 null。
///
/// 如果找到指定名称且类型匹配的配置表则返回 true,否则返回 false。
/// 当 为 null、空或仅包含空白字符时抛出。
public bool TryGetTable(string name, out IConfigTable? table)
where TKey : notnull
{
table = default;
if (string.IsNullOrWhiteSpace(name))
{
throw new ArgumentException(NameCannotBeNullOrWhiteSpaceMessage, nameof(name));
}
if (!_tables.TryGetValue(name, out var rawTable))
{
return false;
}
if (rawTable is not IConfigTable typedTable)
{
return false;
}
table = typedTable;
return true;
}
///
/// 检查指定名称的配置表是否已注册。
///
/// 要检查的配置表名称。
/// 如果配置表已注册则返回 true,否则返回 false。
/// 当 为 null、空或仅包含空白字符时抛出。
public bool HasTable(string name)
{
if (string.IsNullOrWhiteSpace(name))
{
throw new ArgumentException(NameCannotBeNullOrWhiteSpaceMessage, nameof(name));
}
return _tables.ContainsKey(name);
}
///
/// 从注册表中移除指定名称的配置表。
///
/// 要移除的配置表名称。
/// 如果配置表存在并被成功移除则返回 true,否则返回 false。
/// 当 为 null、空或仅包含空白字符时抛出。
public bool RemoveTable(string name)
{
if (string.IsNullOrWhiteSpace(name))
{
throw new ArgumentException(NameCannotBeNullOrWhiteSpaceMessage, nameof(name));
}
return _tables.TryRemove(name, out _);
}
///
/// 清空注册表中的所有配置表。
///
public void Clear()
{
_tables.Clear();
}
}