-
Notifications
You must be signed in to change notification settings - Fork 4
docs(config): 添加游戏内容配置系统文档和集成测试 #186
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
GeWuYou
merged 2 commits into
main
from
feat/add-game-content-config-with-source-generator
Apr 6, 2026
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,20 +1,20 @@ | ||
| // IsExternalInit.cs | ||
| // This type is required to support init-only setters and record types | ||
| // when targeting netstandard2.0 or older frameworks. | ||
| #if !NET5_0_OR_GREATER | ||
| using System.ComponentModel; | ||
| // ReSharper disable CheckNamespace | ||
| namespace System.Runtime.CompilerServices; | ||
| /// <summary> | ||
| /// 提供一个占位符类型,用于支持 C# 9.0 的 init 访问器功能。 | ||
| /// 该类型在 .NET 5.0 及更高版本中已内置,因此仅在较低版本的 .NET 中定义。 | ||
| /// </summary> | ||
| [EditorBrowsable(EditorBrowsableState.Never)] | ||
| internal static class IsExternalInit | ||
| { | ||
| } | ||
| // IsExternalInit.cs | ||
| // This type is required to support init-only setters and record types | ||
| // when targeting netstandard2.0 or older frameworks. | ||
|
|
||
| #if !NET5_0_OR_GREATER | ||
| using System.ComponentModel; | ||
|
|
||
| // ReSharper disable CheckNamespace | ||
|
|
||
| namespace System.Runtime.CompilerServices; | ||
|
|
||
| /// <summary> | ||
| /// 提供一个占位符类型,用于支持 C# 9.0 的 init 访问器功能。 | ||
| /// 该类型在 .NET 5.0 及更高版本中已内置,因此仅在较低版本的 .NET 中定义。 | ||
| /// </summary> | ||
| [EditorBrowsable(EditorBrowsableState.Never)] | ||
| internal static class IsExternalInit | ||
| { | ||
| } | ||
| #endif |
155 changes: 155 additions & 0 deletions
155
GFramework.Game.Tests/Config/ArchitectureConfigIntegrationTests.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,155 @@ | ||
| using System; | ||
| using System.IO; | ||
| using System.Linq; | ||
| using System.Threading.Tasks; | ||
| using GFramework.Core.Architectures; | ||
| using GFramework.Game.Config; | ||
| using GFramework.Game.Config.Generated; | ||
| using NUnit.Framework; | ||
|
|
||
| namespace GFramework.Game.Tests.Config; | ||
|
|
||
| /// <summary> | ||
| /// 验证在 <see cref="Architecture" /> 初始化流程中可以注册配置注册表、执行加载并通过生成的表访问器读取数据。 | ||
| /// </summary> | ||
| [TestFixture] | ||
| public class ArchitectureConfigIntegrationTests | ||
| { | ||
| /// <summary> | ||
| /// 架构初始化期间,通过 <see cref="YamlConfigLoader" /> 注册生成表, | ||
| /// 并将 <see cref="ConfigRegistry" /> 作为 utility 暴露给架构上下文读取。 | ||
| /// </summary> | ||
| [Test] | ||
| public async Task ConfigLoaderCanRunDuringArchitectureInitialization() | ||
| { | ||
| var rootPath = CreateTempConfigRoot(); | ||
| ConsumerArchitecture? architecture = null; | ||
| var initialized = false; | ||
| try | ||
| { | ||
| architecture = new ConsumerArchitecture(rootPath); | ||
| await architecture.InitializeAsync(); | ||
| initialized = true; | ||
|
|
||
| var table = architecture.MonsterTable; | ||
|
|
||
| Assert.Multiple(() => | ||
| { | ||
| Assert.That(table.Get(1).Name, Is.EqualTo("Slime")); | ||
| Assert.That(table.Get(2).Hp, Is.EqualTo(30)); | ||
| Assert.That(table.FindByFaction("dungeon").Select(static config => config.Name), | ||
| Is.EquivalentTo(new[] { "Slime", "Goblin" })); | ||
| Assert.That(architecture.Registry.TryGetMonsterTable(out var retrieved), Is.True); | ||
| Assert.That(retrieved, Is.Not.Null); | ||
| Assert.That(retrieved!.Get(1).Name, Is.EqualTo("Slime")); | ||
| Assert.That(architecture.Context.GetUtility<ConfigRegistry>(), Is.SameAs(architecture.Registry)); | ||
| }); | ||
| } | ||
| finally | ||
| { | ||
| if (architecture is not null && initialized) | ||
| { | ||
| await architecture.DestroyAsync(); | ||
| } | ||
|
|
||
| DeleteDirectoryIfExists(rootPath); | ||
| } | ||
| } | ||
|
|
||
| private static string CreateTempConfigRoot() | ||
| { | ||
| var rootPath = Path.Combine(Path.GetTempPath(), "GFramework.ConfigArchitecture", Guid.NewGuid().ToString("N")); | ||
| Directory.CreateDirectory(rootPath); | ||
| Directory.CreateDirectory(Path.Combine(rootPath, "schemas")); | ||
| Directory.CreateDirectory(Path.Combine(rootPath, "monster")); | ||
| File.WriteAllText(Path.Combine(rootPath, "schemas", "monster.schema.json"), MonsterSchemaJson); | ||
| File.WriteAllText(Path.Combine(rootPath, "monster", "slime.yaml"), MonsterSlimeYaml); | ||
| File.WriteAllText(Path.Combine(rootPath, "monster", "goblin.yaml"), MonsterGoblinYaml); | ||
| return rootPath; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// 最佳努力尝试删除临时目录。 | ||
| /// </summary> | ||
| private static void DeleteDirectoryIfExists(string path) | ||
| { | ||
| if (!Directory.Exists(path)) | ||
| { | ||
| return; | ||
| } | ||
|
|
||
| try | ||
| { | ||
| Directory.Delete(path, true); | ||
| } | ||
| catch (IOException) | ||
| { | ||
| // Ignored: cleanup is best effort and should not fail the test. | ||
| } | ||
| catch (UnauthorizedAccessException) | ||
| { | ||
| // Ignored: cleanup is best effort and can transiently fail when files are still being released. | ||
| } | ||
| } | ||
|
|
||
| private const string MonsterSchemaJson = @"{ | ||
| ""title"": ""Monster Config"", | ||
| ""description"": ""Defines one monster entry for the generated consumer integration test."", | ||
| ""type"": ""object"", | ||
| ""required"": [ | ||
| ""id"", | ||
| ""name"", | ||
| ""hp"", | ||
| ""faction"" | ||
| ], | ||
| ""properties"": { | ||
| ""id"": { | ||
| ""type"": ""integer"", | ||
| ""description"": ""Monster identifier."" | ||
| }, | ||
| ""name"": { | ||
| ""type"": ""string"", | ||
| ""description"": ""Monster display name."" | ||
| }, | ||
| ""hp"": { | ||
| ""type"": ""integer"", | ||
| ""description"": ""Monster base health."" | ||
| }, | ||
| ""faction"": { | ||
| ""type"": ""string"", | ||
| ""description"": ""Used by the integration test to validate generated non-unique queries."" | ||
| } | ||
| } | ||
| }"; | ||
|
|
||
| private const string MonsterSlimeYaml = | ||
| "id: 1\nname: Slime\nhp: 10\nfaction: dungeon\n"; | ||
|
|
||
| private const string MonsterGoblinYaml = | ||
| "id: 2\nname: Goblin\nhp: 30\nfaction: dungeon\n"; | ||
|
|
||
| private sealed class ConsumerArchitecture : Architecture | ||
| { | ||
| private readonly string _configRoot; | ||
|
|
||
| public ConfigRegistry Registry { get; } | ||
|
|
||
| public MonsterTable MonsterTable { get; private set; } = null!; | ||
|
|
||
| public ConsumerArchitecture(string configRoot) | ||
| { | ||
| _configRoot = configRoot ?? throw new ArgumentNullException(nameof(configRoot)); | ||
| Registry = new ConfigRegistry(); | ||
| } | ||
|
|
||
| protected override void OnInitialize() | ||
| { | ||
| RegisterUtility(Registry); | ||
|
|
||
| var loader = new YamlConfigLoader(_configRoot) | ||
| .RegisterMonsterTable(); | ||
| loader.LoadAsync(Registry).GetAwaiter().GetResult(); | ||
| MonsterTable = Registry.GetMonsterTable(); | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.