From b1e9d0a345d52bab2caaa88854f9c3f67d896107 Mon Sep 17 00:00:00 2001 From: GeWuYou <95328647+GeWuYou@users.noreply.github.com> Date: Fri, 3 Apr 2026 12:31:58 +0800 Subject: [PATCH] =?UTF-8?q?feat(config):=20=E6=B7=BB=E5=8A=A0=E5=9F=BA?= =?UTF-8?q?=E4=BA=8EYAML=E7=9A=84=E9=85=8D=E7=BD=AE=E5=8A=A0=E8=BD=BD?= =?UTF-8?q?=E5=99=A8=E5=92=8C=E6=9E=B6=E6=9E=84=E9=AA=8C=E8=AF=81=E5=99=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 实现YamlConfigLoader类,支持从文件目录加载YAML配置 - 添加热重载功能,监听配置文件变更并自动重新加载 - 实现YAML配置架构验证器,支持JSON Schema校验 - 添加跨表引用验证机制,确保配置依赖关系正确 - 支持配置表注册和类型安全的配置项访问 - 实现防抖机制避免频繁的文件变更触发 - 提供详细的错误诊断信息和异常处理 --- GFramework.Game/Config/YamlConfigLoader.cs | 27 ++++++++++--------- .../Config/YamlConfigSchemaValidator.cs | 4 --- 2 files changed, 15 insertions(+), 16 deletions(-) diff --git a/GFramework.Game/Config/YamlConfigLoader.cs b/GFramework.Game/Config/YamlConfigLoader.cs index df99f1a6..5034f2cf 100644 --- a/GFramework.Game/Config/YamlConfigLoader.cs +++ b/GFramework.Game/Config/YamlConfigLoader.cs @@ -1,3 +1,4 @@ +using System.Diagnostics; using GFramework.Core.Abstractions.Events; using GFramework.Game.Abstractions.Config; using YamlDotNet.Serialization; @@ -299,6 +300,20 @@ public sealed class YamlConfigLoader : IConfigLoader Func keySelector, IEqualityComparer? 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; RelativePath = relativePath; SchemaRelativePath = schemaRelativePath; @@ -365,10 +380,6 @@ public sealed class YamlConfigLoader : IConfigLoader { yaml = await File.ReadAllTextAsync(file, cancellationToken); } - catch (ConfigLoadException) - { - throw; - } catch (Exception exception) { throw ConfigLoadExceptionFactory.Create( @@ -399,10 +410,6 @@ public sealed class YamlConfigLoader : IConfigLoader values.Add(value); } - catch (ConfigLoadException) - { - throw; - } catch (Exception exception) { throw ConfigLoadExceptionFactory.Create( @@ -422,10 +429,6 @@ public sealed class YamlConfigLoader : IConfigLoader var table = new InMemoryConfigTable(values, _keySelector, _comparer); return new YamlTableLoadResult(Name, table, referencedTableNames, referenceUsages); } - catch (ConfigLoadException) - { - throw; - } catch (Exception exception) { throw ConfigLoadExceptionFactory.Create( diff --git a/GFramework.Game/Config/YamlConfigSchemaValidator.cs b/GFramework.Game/Config/YamlConfigSchemaValidator.cs index c2eee56f..a4af09bc 100644 --- a/GFramework.Game/Config/YamlConfigSchemaValidator.cs +++ b/GFramework.Game/Config/YamlConfigSchemaValidator.cs @@ -77,10 +77,6 @@ internal static class YamlConfigSchemaValidator return new YamlConfigSchema(schemaPath, rootNode, referencedTableNames.ToArray()); } - catch (ConfigLoadException) - { - throw; - } catch (JsonException exception) { throw ConfigLoadExceptionFactory.Create(