From 774b69f5608d6a007278151c129949502582d009 Mon Sep 17 00:00:00 2001
From: GeWuYou <95328647+GeWuYou@users.noreply.github.com>
Date: Sun, 12 Apr 2026 16:09:07 +0800
Subject: [PATCH] =?UTF-8?q?feat(config):=20=E6=B7=BB=E5=8A=A0YAML=E9=85=8D?=
=?UTF-8?q?=E7=BD=AE=E6=96=87=E6=9C=AC=E6=A0=A1=E9=AA=8C=E5=99=A8?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
- 实现同步和异步YAML文本校验功能
- 添加基于schema文件的配置校验支持
- 实现schema缓存机制避免重复磁盘IO
- 提供配置表名称和文件路径参数验证
- 集成取消令牌支持异步操作取消
- 添加详细的异常处理和诊断信息
---
GFramework.Game/Config/YamlConfigTextValidator.cs | 13 +++++++++----
1 file changed, 9 insertions(+), 4 deletions(-)
diff --git a/GFramework.Game/Config/YamlConfigTextValidator.cs b/GFramework.Game/Config/YamlConfigTextValidator.cs
index 40b9dc72..b558e520 100644
--- a/GFramework.Game/Config/YamlConfigTextValidator.cs
+++ b/GFramework.Game/Config/YamlConfigTextValidator.cs
@@ -79,8 +79,9 @@ public static class YamlConfigTextValidator
return cachedSchema;
}
+ var lastWriteTimeUtc = File.GetLastWriteTimeUtc(schemaPath);
var schema = YamlConfigSchemaValidator.Load(tableName, schemaPath);
- CacheSchema(cacheKey, schema);
+ CacheSchema(cacheKey, lastWriteTimeUtc, schema);
return schema;
}
@@ -104,9 +105,10 @@ public static class YamlConfigTextValidator
return cachedSchema;
}
+ var lastWriteTimeUtc = File.GetLastWriteTimeUtc(schemaPath);
var schema = await YamlConfigSchemaValidator.LoadAsync(tableName, schemaPath, cancellationToken)
.ConfigureAwait(false);
- CacheSchema(cacheKey, schema);
+ CacheSchema(cacheKey, lastWriteTimeUtc, schema);
return schema;
}
@@ -157,15 +159,18 @@ public static class YamlConfigTextValidator
}
///
- /// 使用最新的文件时间戳刷新 schema 缓存。
+ /// 使用读取前捕获的文件时间戳刷新 schema 缓存。
+ /// 这样即使 schema 在读取过程中发生变化,后续访问也会因时间戳变新而重新加载,
+ /// 避免把“旧内容 + 新时间戳”写入缓存。
///
/// 缓存键。
+ /// 本次读取开始前捕获的 schema 文件修改时间。
/// 最新加载的 schema。
private static void CacheSchema(
SchemaCacheKey cacheKey,
+ DateTime lastWriteTimeUtc,
YamlConfigSchema schema)
{
- var lastWriteTimeUtc = File.GetLastWriteTimeUtc(cacheKey.SchemaPath);
SchemaCache[cacheKey] = new SchemaCacheEntry(lastWriteTimeUtc, schema);
}