GFramework/GFramework.Godot/Config/GodotYamlConfigTableSource.cs
GeWuYou 411d4cb14a docs(config): 添加游戏内容配置系统完整文档
- 新增配置系统概述和核心能力介绍
- 添加Schema和YAML配置文件格式示例
- 提供推荐目录结构和接入模板
- 详细说明Generator集成和运行时加载流程
- 介绍VS Code工具和热重载功能
- 添加Godot引擎桥接适配器文档
- 说明运行时校验行为和错误处理机制
- 提供Architecture模块集成模板
- 记录当前限制和未来规划评估
2026-04-10 23:25:53 +08:00

115 lines
4.2 KiB
C#

using System.IO;
namespace GFramework.Godot.Config;
/// <summary>
/// 描述一个 Godot YAML 配置表在资源目录中的来源信息。
/// </summary>
public sealed class GodotYamlConfigTableSource
{
/// <summary>
/// 初始化一个配置表来源描述。
/// </summary>
/// <param name="tableName">运行时表名称。</param>
/// <param name="configRelativePath">
/// 相对配置根目录的 YAML 目录。
/// 该路径必须保持为无根相对路径,且不能包含 <c>.</c>、<c>..</c>、<c>res://</c>、<c>user://</c> 或磁盘根路径前缀。
/// </param>
/// <param name="schemaRelativePath">
/// 相对配置根目录的 schema 文件路径;未启用 schema 时为空。
/// 如果提供,同样必须保持为无根相对路径,且不能包含 <c>.</c>、<c>..</c> 或任何绝对路径前缀。
/// </param>
/// <exception cref="ArgumentException">
/// <paramref name="tableName" />、<paramref name="configRelativePath" /> 或 <paramref name="schemaRelativePath" />
/// 不满足非空白且安全相对路径的约束时抛出。
/// </exception>
public GodotYamlConfigTableSource(
string tableName,
string configRelativePath,
string? schemaRelativePath = null)
{
if (string.IsNullOrWhiteSpace(tableName))
{
throw new ArgumentException("Table name cannot be null or whitespace.", nameof(tableName));
}
if (string.IsNullOrWhiteSpace(configRelativePath))
{
throw new ArgumentException("Config relative path cannot be null or whitespace.",
nameof(configRelativePath));
}
if (!IsSafeRelativePath(configRelativePath))
{
throw new ArgumentException(
"Config relative path must be a safe relative path without root segments or traversal markers.",
nameof(configRelativePath));
}
if (schemaRelativePath != null && string.IsNullOrWhiteSpace(schemaRelativePath))
{
throw new ArgumentException(
"Schema relative path cannot be empty or whitespace when provided.",
nameof(schemaRelativePath));
}
if (schemaRelativePath != null && !IsSafeRelativePath(schemaRelativePath))
{
throw new ArgumentException(
"Schema relative path must be a safe relative path without root segments or traversal markers.",
nameof(schemaRelativePath));
}
TableName = tableName;
ConfigRelativePath = configRelativePath;
SchemaRelativePath = schemaRelativePath;
}
/// <summary>
/// 获取运行时表名称。
/// </summary>
public string TableName { get; }
/// <summary>
/// 获取相对配置根目录的 YAML 目录路径。
/// 该值始终保持为无根相对路径,不会包含 <c>.</c> 或 <c>..</c> 段。
/// </summary>
public string ConfigRelativePath { get; }
/// <summary>
/// 获取相对配置根目录的 schema 文件路径;未启用 schema 校验时为空。
/// 该值在非空时始终保持为无根相对路径,不会包含 <c>.</c> 或 <c>..</c> 段。
/// </summary>
public string? SchemaRelativePath { get; }
private static bool IsSafeRelativePath(string path)
{
var normalizedPath = path.Replace('\\', '/');
if (normalizedPath.StartsWith("/", StringComparison.Ordinal) ||
normalizedPath.StartsWith("res://", StringComparison.Ordinal) ||
normalizedPath.StartsWith("user://", StringComparison.Ordinal) ||
Path.IsPathRooted(path) ||
HasWindowsDrivePrefix(normalizedPath))
{
return false;
}
foreach (var segment in normalizedPath.Split('/', StringSplitOptions.RemoveEmptyEntries))
{
if (segment is "." or "..")
{
return false;
}
}
return true;
}
private static bool HasWindowsDrivePrefix(string path)
{
return path.Length >= 2 &&
char.IsLetter(path[0]) &&
path[1] == ':';
}
}