mirror of
https://github.com/GeWuYou/GFramework.git
synced 2026-05-12 22:03:30 +08:00
feat(config): 添加基于YAML的配置加载器和架构验证器
- 实现YamlConfigLoader类,支持从文件目录加载YAML配置 - 添加热重载功能,监听配置文件变更并自动重新加载 - 实现YAML配置架构验证器,支持JSON Schema校验 - 添加跨表引用验证机制,确保配置依赖关系正确 - 支持配置表注册和类型安全的配置项访问 - 实现防抖机制避免频繁的文件变更触发 - 提供详细的错误诊断信息和异常处理
This commit is contained in:
parent
081a65f740
commit
b1e9d0a345
@ -1,3 +1,4 @@
|
|||||||
|
using System.Diagnostics;
|
||||||
using GFramework.Core.Abstractions.Events;
|
using GFramework.Core.Abstractions.Events;
|
||||||
using GFramework.Game.Abstractions.Config;
|
using GFramework.Game.Abstractions.Config;
|
||||||
using YamlDotNet.Serialization;
|
using YamlDotNet.Serialization;
|
||||||
@ -299,6 +300,20 @@ public sealed class YamlConfigLoader : IConfigLoader
|
|||||||
Func<TValue, TKey> keySelector,
|
Func<TValue, TKey> keySelector,
|
||||||
IEqualityComparer<TKey>? comparer)
|
IEqualityComparer<TKey>? comparer)
|
||||||
{
|
{
|
||||||
|
Debug.Assert(!string.IsNullOrWhiteSpace(name), "Table registrations should always have a non-empty name.");
|
||||||
|
Debug.Assert(!string.IsNullOrWhiteSpace(relativePath),
|
||||||
|
"Table registrations should always have a non-empty relative path.");
|
||||||
|
|
||||||
|
if (string.IsNullOrWhiteSpace(name))
|
||||||
|
{
|
||||||
|
throw new ArgumentException(TableNameCannotBeNullOrWhiteSpaceMessage, nameof(name));
|
||||||
|
}
|
||||||
|
|
||||||
|
if (string.IsNullOrWhiteSpace(relativePath))
|
||||||
|
{
|
||||||
|
throw new ArgumentException(RelativePathCannotBeNullOrWhiteSpaceMessage, nameof(relativePath));
|
||||||
|
}
|
||||||
|
|
||||||
Name = name;
|
Name = name;
|
||||||
RelativePath = relativePath;
|
RelativePath = relativePath;
|
||||||
SchemaRelativePath = schemaRelativePath;
|
SchemaRelativePath = schemaRelativePath;
|
||||||
@ -365,10 +380,6 @@ public sealed class YamlConfigLoader : IConfigLoader
|
|||||||
{
|
{
|
||||||
yaml = await File.ReadAllTextAsync(file, cancellationToken);
|
yaml = await File.ReadAllTextAsync(file, cancellationToken);
|
||||||
}
|
}
|
||||||
catch (ConfigLoadException)
|
|
||||||
{
|
|
||||||
throw;
|
|
||||||
}
|
|
||||||
catch (Exception exception)
|
catch (Exception exception)
|
||||||
{
|
{
|
||||||
throw ConfigLoadExceptionFactory.Create(
|
throw ConfigLoadExceptionFactory.Create(
|
||||||
@ -399,10 +410,6 @@ public sealed class YamlConfigLoader : IConfigLoader
|
|||||||
|
|
||||||
values.Add(value);
|
values.Add(value);
|
||||||
}
|
}
|
||||||
catch (ConfigLoadException)
|
|
||||||
{
|
|
||||||
throw;
|
|
||||||
}
|
|
||||||
catch (Exception exception)
|
catch (Exception exception)
|
||||||
{
|
{
|
||||||
throw ConfigLoadExceptionFactory.Create(
|
throw ConfigLoadExceptionFactory.Create(
|
||||||
@ -422,10 +429,6 @@ public sealed class YamlConfigLoader : IConfigLoader
|
|||||||
var table = new InMemoryConfigTable<TKey, TValue>(values, _keySelector, _comparer);
|
var table = new InMemoryConfigTable<TKey, TValue>(values, _keySelector, _comparer);
|
||||||
return new YamlTableLoadResult(Name, table, referencedTableNames, referenceUsages);
|
return new YamlTableLoadResult(Name, table, referencedTableNames, referenceUsages);
|
||||||
}
|
}
|
||||||
catch (ConfigLoadException)
|
|
||||||
{
|
|
||||||
throw;
|
|
||||||
}
|
|
||||||
catch (Exception exception)
|
catch (Exception exception)
|
||||||
{
|
{
|
||||||
throw ConfigLoadExceptionFactory.Create(
|
throw ConfigLoadExceptionFactory.Create(
|
||||||
|
|||||||
@ -77,10 +77,6 @@ internal static class YamlConfigSchemaValidator
|
|||||||
|
|
||||||
return new YamlConfigSchema(schemaPath, rootNode, referencedTableNames.ToArray());
|
return new YamlConfigSchema(schemaPath, rootNode, referencedTableNames.ToArray());
|
||||||
}
|
}
|
||||||
catch (ConfigLoadException)
|
|
||||||
{
|
|
||||||
throw;
|
|
||||||
}
|
|
||||||
catch (JsonException exception)
|
catch (JsonException exception)
|
||||||
{
|
{
|
||||||
throw ConfigLoadExceptionFactory.Create(
|
throw ConfigLoadExceptionFactory.Create(
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user