Refactor component registration, update deps, improve JSON

- Updated Misaki.HighPerformance package versions in Core and Graphics projects.
- Added IsTrimmable to Ghost.Engine.csproj for trimming support.
- Renamed GetOrRegisterComponent to GetOrRegisterComponentID and updated all usages.
- Component registration codegen now uses a static class with [ModuleInitializer], no longer requires [EngineEntry].
- Improved JSON serialization: added string support, introduced Utf8JsonObjectScope/ArrayScope, and new extension methods for cleaner JSON writing.
- Removed [SkipLocalsInit] from Hierarchy and LocalToWorld.
- Fixed Entity.Invalid to use INVALID_ID for both fields.
- Minor cleanup: clarified comments, reorganized Ghost.Generator in solution, and disabled component serialization generator.
This commit is contained in:
2025-12-21 22:18:25 +09:00
parent 840cf7dd5a
commit 2881fda112
15 changed files with 90 additions and 137 deletions

View File

@@ -20,10 +20,10 @@
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="Misaki.HighPerformance" Version="1.0.2" /> <PackageReference Include="Misaki.HighPerformance" Version="1.0.3" />
<PackageReference Include="Misaki.HighPerformance.Jobs" Version="1.2.1" /> <PackageReference Include="Misaki.HighPerformance.Jobs" Version="1.2.2" />
<PackageReference Include="Misaki.HighPerformance.LowLevel" Version="1.3.2" /> <PackageReference Include="Misaki.HighPerformance.LowLevel" Version="1.3.3" />
<PackageReference Include="Misaki.HighPerformance.Mathematics" Version="1.3.0" /> <PackageReference Include="Misaki.HighPerformance.Mathematics" Version="1.3.1" />
<PackageReference Include="System.IO.Hashing" Version="10.0.1" /> <PackageReference Include="System.IO.Hashing" Version="10.0.1" />
<PackageReference Include="TerraFX.Interop.Windows" Version="10.0.26100.6" /> <PackageReference Include="TerraFX.Interop.Windows" Version="10.0.26100.6" />
</ItemGroup> </ItemGroup>

View File

@@ -10,6 +10,7 @@ using Microsoft.UI.Xaml.Controls;
namespace Ghost.Editor.Core.SceneGraph; namespace Ghost.Editor.Core.SceneGraph;
// FIX: This should be scene node, not world node
[CustomSerializer(typeof(WorldNodeSerializer))] [CustomSerializer(typeof(WorldNodeSerializer))]
public partial class WorldNode : SceneGraphNode, IEquatable<WorldNode> public partial class WorldNode : SceneGraphNode, IEquatable<WorldNode>
{ {

View File

@@ -4,7 +4,6 @@ using System.Runtime.CompilerServices;
namespace Ghost.Engine.Components; namespace Ghost.Engine.Components;
[SkipLocalsInit]
[HideEditor] [HideEditor]
public struct Hierarchy : IComponent public struct Hierarchy : IComponent
{ {

View File

@@ -1,10 +1,8 @@
using Ghost.Entities; using Ghost.Entities;
using Misaki.HighPerformance.Mathematics; using Misaki.HighPerformance.Mathematics;
using System.Runtime.CompilerServices;
namespace Ghost.Engine.Components; namespace Ghost.Engine.Components;
[SkipLocalsInit]
public struct LocalToWorld : IComponent public struct LocalToWorld : IComponent
{ {
public float4x4 matrix; public float4x4 matrix;

View File

@@ -40,8 +40,7 @@ public static partial class EngineCore
{ {
s_impl = new EngineCoreImpl(); s_impl = new EngineCoreImpl();
ComponentRegistry.GetOrRegisterComponent<ManagedEntityRef>(); ComponentRegistry.GetOrRegisterComponentID<ManagedEntityRef>();
RegisterIComponentTypes();
} }
internal static void Init() internal static void Init()

View File

@@ -9,10 +9,12 @@
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'"> <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
<IsAotCompatible>True</IsAotCompatible> <IsAotCompatible>True</IsAotCompatible>
<IsTrimmable>True</IsTrimmable>
</PropertyGroup> </PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'"> <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
<IsAotCompatible>True</IsAotCompatible> <IsAotCompatible>True</IsAotCompatible>
<IsTrimmable>True</IsTrimmable>
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>

View File

@@ -2,6 +2,34 @@ using System.Text.Json;
namespace Ghost.Engine.Utilities; namespace Ghost.Engine.Utilities;
public readonly ref struct Utf8JsonObjectScope : IDisposable
{
private readonly Utf8JsonWriter _writer;
public Utf8JsonObjectScope(Utf8JsonWriter writer)
{
_writer = writer;
_writer.WriteStartObject();
}
public void Dispose()
{
_writer.WriteEndObject();
}
}
public readonly ref struct Utf8JsonArrayScope : IDisposable
{
private readonly Utf8JsonWriter _writer;
public Utf8JsonArrayScope(Utf8JsonWriter writer)
{
_writer = writer;
_writer.WriteStartArray();
}
public void Dispose()
{
_writer.WriteEndArray();
}
}
public static class Utf8JsonWriterExtension public static class Utf8JsonWriterExtension
{ {
public static void WriteArray<T>(this Utf8JsonWriter writer, ReadOnlySpan<char> name, IEnumerable<T> source, Action<T> writeAction) public static void WriteArray<T>(this Utf8JsonWriter writer, ReadOnlySpan<char> name, IEnumerable<T> source, Action<T> writeAction)
@@ -37,4 +65,14 @@ public static class Utf8JsonWriterExtension
writeAction(); writeAction();
writer.WriteEndObject(); writer.WriteEndObject();
} }
public static Utf8JsonObjectScope StartObjectScope(this Utf8JsonWriter writer)
{
return new Utf8JsonObjectScope(writer);
}
public static Utf8JsonArrayScope StartArrayScope(this Utf8JsonWriter writer)
{
return new Utf8JsonArrayScope(writer);
}
} }

View File

@@ -3,7 +3,6 @@ using Misaki.HighPerformance.LowLevel.Buffer;
using Misaki.HighPerformance.Mathematics; using Misaki.HighPerformance.Mathematics;
using System.Runtime.InteropServices; using System.Runtime.InteropServices;
using System.Text.Json; using System.Text.Json;
using System.Text.Json.Serialization.Metadata;
namespace Ghost.Entities.Test; namespace Ghost.Entities.Test;
@@ -32,23 +31,7 @@ public class SerializationTest : ITest
using var stream = new MemoryStream(); using var stream = new MemoryStream();
var serializeOptions = new JsonSerializerOptions var serializeOptions = new JsonSerializerOptions
{ {
IncludeFields = true, IncludeFields = true
IgnoreReadOnlyProperties = true,
TypeInfoResolver = new DefaultJsonTypeInfoResolver
{
Modifiers = { typeInfo =>
{
// Remove everything from the serialization list that is not a field
foreach (var property in typeInfo.Properties)
{
if (property.AttributeProvider is not System.Reflection.FieldInfo)
{
property.ShouldSerialize = (_, _) => false;
}
}
}
}
}
}; };
using var writer = new Utf8JsonWriter(stream, new JsonWriterOptions { Indented = true }); using var writer = new Utf8JsonWriter(stream, new JsonWriterOptions { Indented = true });

View File

@@ -89,7 +89,7 @@ public struct ComponentSet : IDisposable, IEquatable<ComponentSet>
public static class ComponentTypeID<T> public static class ComponentTypeID<T>
where T : unmanaged, IComponent where T : unmanaged, IComponent
{ {
public static readonly Identifier<IComponent> Value = ComponentRegistry.GetOrRegisterComponent<T>(); public static readonly Identifier<IComponent> Value = ComponentRegistry.GetOrRegisterComponentID<T>();
} }
internal static class ComponentRegistry internal static class ComponentRegistry
@@ -100,7 +100,7 @@ internal static class ComponentRegistry
internal static readonly Dictionary<int, Type> s_runtimeIDToType = new(); internal static readonly Dictionary<int, Type> s_runtimeIDToType = new();
public static unsafe Identifier<IComponent> GetOrRegisterComponent<T>() public static unsafe Identifier<IComponent> GetOrRegisterComponentID<T>()
where T : unmanaged, IComponent where T : unmanaged, IComponent
{ {
var type = typeof(T); var type = typeof(T);

View File

@@ -32,7 +32,7 @@ public readonly record struct Entity
public static Entity Invalid public static Entity Invalid
{ {
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
get => new(INVALID_ID, GenerationID.MaxValue); get => new(INVALID_ID, INVALID_ID);
} }
internal Entity(EntityID id, GenerationID generation) internal Entity(EntityID id, GenerationID generation)

View File

@@ -8,6 +8,7 @@ using System.Threading;
namespace Ghost.Generator namespace Ghost.Generator
{ {
// TODO: this should be per assembly, not global
[Generator] [Generator]
public class ComponentRegistrationGenerator : IIncrementalGenerator public class ComponentRegistrationGenerator : IIncrementalGenerator
{ {
@@ -21,20 +22,8 @@ namespace Ghost.Generator
.Where(symbol => symbol != null) .Where(symbol => symbol != null)
.Collect(); .Collect();
// 2. Pipeline: Find the ONE class with [EngineEntry]
var engineEntryClass = context.SyntaxProvider
.CreateSyntaxProvider(
predicate: (s, _) => s is ClassDeclarationSyntax,
transform: GetEngineEntrySymbol)
.Where(symbol => symbol != null)
.Collect(); // Returns ImmutableArray<INamedTypeSymbol>
// 3. COMBINE: Pair the list of components with the list of entry classes
// The result 'source' in the callback will be a tuple: (ImmutableArray<Info>, ImmutableArray<Entry>)
var combinedProvider = componentCandidates.Combine(engineEntryClass);
// 4. Output: Generate the source using both pieces of data at once // 4. Output: Generate the source using both pieces of data at once
context.RegisterSourceOutput(combinedProvider, GenerateRegistrationCode); context.RegisterSourceOutput(componentCandidates, GenerateRegistrationCode);
} }
// Extraction Logic for Components // Extraction Logic for Components
@@ -63,60 +52,23 @@ namespace Ghost.Generator
return null; return null;
} }
// Extraction Logic for Engine Entry
private static INamedTypeSymbol GetEngineEntrySymbol(GeneratorSyntaxContext ctx, CancellationToken _)
{
var classSyntax = (ClassDeclarationSyntax)ctx.Node;
if (!(ctx.SemanticModel.GetDeclaredSymbol(classSyntax) is INamedTypeSymbol symbol))
{
return null;
}
// Check attributes
foreach (var attribute in symbol.GetAttributes())
{
if (attribute.AttributeClass?.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat) == "global::Ghost.Engine.EngineEntryAttribute")
{
return symbol;
}
}
return null;
}
// The Generation Logic (Stateless) // The Generation Logic (Stateless)
private static void GenerateRegistrationCode( private static void GenerateRegistrationCode(SourceProductionContext context, ImmutableArray<INamedTypeSymbol> components)
SourceProductionContext context,
(ImmutableArray<INamedTypeSymbol> Components, ImmutableArray<INamedTypeSymbol> Entries) source)
{ {
var components = source.Components;
var entries = source.Entries;
// 1. Validation: Ensure we found exactly one [EngineEntry] class
if (entries.IsDefaultOrEmpty)
{
return;
}
// Pick the first one (if multiple exist, you might want to report a diagnostic error)
var targetClass = entries[0];
if (components.IsDefaultOrEmpty) if (components.IsDefaultOrEmpty)
{ {
return; return;
} }
// 2. Extract Namespace and Class Name directly from the symbol found in the pipeline var name = $"g_component_registration";
var targetNamespace = targetClass.ContainingNamespace.ToDisplayString();
var targetClassName = targetClass.Name;
var sb = new StringBuilder(); var sb = new StringBuilder();
sb.Append($@" sb.Append($@"
namespace {targetNamespace} [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)]
internal static class {name}
{{ {{
public partial class {targetClassName} [global::System.Runtime.CompilerServices.ModuleInitializer]
{{ public static void RegisterIComponentTypes()
private static void RegisterIComponentTypes()
{{"); {{");
foreach (var symbol in components.Distinct(SymbolEqualityComparer.Default)) foreach (var symbol in components.Distinct(SymbolEqualityComparer.Default))
@@ -124,15 +76,14 @@ namespace {targetNamespace}
if (symbol is null) continue; if (symbol is null) continue;
sb.Append($@" sb.Append($@"
global::Ghost.Entities.ComponentRegistry.GetOrRegisterComponent<{symbol.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat)}>();"); global::Ghost.Entities.ComponentRegistry.GetOrRegisterComponentID<{symbol.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat)}>();");
} }
sb.Append(@" sb.Append(@"
} }
}
}"); }");
context.AddSource($"{targetClassName}.ComponentReg.gen.cs", SourceText.From(sb.ToString(), Encoding.UTF8)); context.AddSource($"{name}.gen.cs", SourceText.From(sb.ToString(), Encoding.UTF8));
} }
} }
} }

View File

@@ -11,7 +11,6 @@ namespace Ghost.Generator
{ {
private string GetJsonWriteCall(ITypeSymbol type, string fieldName) private string GetJsonWriteCall(ITypeSymbol type, string fieldName)
{ {
// 1. PRIMITIVES (Fastest)
switch (type.SpecialType) switch (type.SpecialType)
{ {
case SpecialType.System_Byte: case SpecialType.System_Byte:
@@ -32,26 +31,10 @@ namespace Ghost.Generator
return $@"writer.WriteBoolean(""{fieldName}"", value.{fieldName});"; return $@"writer.WriteBoolean(""{fieldName}"", value.{fieldName});";
case SpecialType.System_Char: case SpecialType.System_Char:
return $@"writer.WriteString(""{fieldName}"", [value.{fieldName}]);"; return $@"writer.WriteString(""{fieldName}"", [value.{fieldName}]);";
case SpecialType.System_String:
return $@"writer.WriteString(""{fieldName}"", value.{fieldName});";
} }
// TODO: Add well-known types like float3, Vector3, etc.
#if false
// 2. KNOWN MATH TYPES (Optimized Arrays)
// Checking by name is simple and effective for standard math libs
if (type.Name == "float3" || type.Name == "Vector3")
{
return $@"writer.WritePropertyName(""{fieldName}"");
writer.WriteStartArray();
writer.WriteNumberValue(value.{fieldName}.x);
writer.WriteNumberValue(value.{fieldName}.y);
writer.WriteNumberValue(value.{fieldName}.z);
writer.WriteEndArray();";
}
#endif
// 3. FALLBACK: System.Text.Json (Reflection)
// If we don't know what it is, let the standard serializer handle it.
// This handles float4x4, List<T>, and other nested structs automatically.
return $@"writer.WritePropertyName(""{fieldName}""); global::System.Text.Json.JsonSerializer.Serialize(writer, value.{fieldName}, options);"; return $@"writer.WritePropertyName(""{fieldName}""); global::System.Text.Json.JsonSerializer.Serialize(writer, value.{fieldName}, options);";
} }
@@ -70,17 +53,14 @@ namespace Ghost.Generator
case SpecialType.System_UInt16: return "reader.GetUInt16()"; case SpecialType.System_UInt16: return "reader.GetUInt16()";
case SpecialType.System_UInt32: return "reader.GetUInt32()"; case SpecialType.System_UInt32: return "reader.GetUInt32()";
case SpecialType.System_UInt64: return "reader.GetUInt64()"; case SpecialType.System_UInt64: return "reader.GetUInt64()";
case SpecialType.System_IntPtr: return "reader.GetInt64()"; // Note: IntPtr size varies by platform // Note: the size of IntPtr and UIntPtr varies by platform, we use Int64/UInt64 to ensure compatibility
case SpecialType.System_UIntPtr: return "reader.GetUInt64()"; // Note: UIntPtr size varies by platform case SpecialType.System_IntPtr: return "reader.GetInt64()";
case SpecialType.System_UIntPtr: return "reader.GetUInt64()";
case SpecialType.System_Boolean: return "reader.GetBoolean()"; case SpecialType.System_Boolean: return "reader.GetBoolean()";
case SpecialType.System_Char: return "reader.GetString()[0]"; case SpecialType.System_Char: return "reader.GetString()[0]";
case SpecialType.System_String: return "reader.GetString()";
} }
#if false
// For custom math types, you'd need to generate code to read the array back
if (type.Name == "float3") return "new float3(reader.GetSingle(), reader.GetSingle(), reader.GetSingle())"; // Simplified
#endif
return $"global::System.Text.Json.JsonSerializer.Deserialize<{type.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat)}>(ref reader, options)"; return $"global::System.Text.Json.JsonSerializer.Deserialize<{type.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat)}>(ref reader, options)";
} }
@@ -125,6 +105,7 @@ namespace Ghost.Generator
// 2. The Main Template using $@ // 2. The Main Template using $@
// Watch the double braces {{ }} and double quotes "" "" // Watch the double braces {{ }} and double quotes "" ""
var sourceCode = $@"// <auto-generated/> var sourceCode = $@"// <auto-generated/>
#nullable enable
namespace {namespaceName} namespace {namespaceName}
{{ {{
@@ -207,6 +188,8 @@ namespace {namespaceName}
public void Initialize(IncrementalGeneratorInitializationContext context) public void Initialize(IncrementalGeneratorInitializationContext context)
{ {
return;
var componentCandidates = context.SyntaxProvider var componentCandidates = context.SyntaxProvider
.CreateSyntaxProvider( .CreateSyntaxProvider(
predicate: (syntaxNode, _) => syntaxNode is StructDeclarationSyntax, predicate: (syntaxNode, _) => syntaxNode is StructDeclarationSyntax,

View File

@@ -41,7 +41,7 @@
<PrivateAssets>all</PrivateAssets> <PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference> </PackageReference>
<PackageReference Include="Misaki.HighPerformance.Image" Version="1.0.0" /> <PackageReference Include="Misaki.HighPerformance.Image" Version="1.1.0" />
<PackageReference Include="TerraFX.Interop.D3D12MemoryAllocator" Version="3.0.1" /> <PackageReference Include="TerraFX.Interop.D3D12MemoryAllocator" Version="3.0.1" />
</ItemGroup> </ItemGroup>

View File

@@ -1,4 +1,3 @@
using Ghost.Graphics.D3D12;
using Ghost.Graphics.RHI; using Ghost.Graphics.RHI;
namespace Ghost.Graphics; namespace Ghost.Graphics;

View File

@@ -13,7 +13,6 @@
<Platform Solution="*|x86" Project="x86" /> <Platform Solution="*|x86" Project="x86" />
<Deploy /> <Deploy />
</Project> </Project>
<Project Path="Ghost.Generator/Ghost.Generator.csproj" />
</Folder> </Folder>
<Folder Name="/Library/"> <Folder Name="/Library/">
<Project Path="Ghost.FMOD/Ghost.FMOD.csproj" /> <Project Path="Ghost.FMOD/Ghost.FMOD.csproj" />
@@ -23,6 +22,7 @@
<Project Path="Ghost.Core/Ghost.Core.csproj" /> <Project Path="Ghost.Core/Ghost.Core.csproj" />
<Project Path="Ghost.Engine/Ghost.Engine.csproj" /> <Project Path="Ghost.Engine/Ghost.Engine.csproj" />
<Project Path="Ghost.Entities/Ghost.Entities.csproj" /> <Project Path="Ghost.Entities/Ghost.Entities.csproj" />
<Project Path="Ghost.Generator/Ghost.Generator.csproj" />
<Project Path="Ghost.Graphics/Ghost.Graphics.csproj" /> <Project Path="Ghost.Graphics/Ghost.Graphics.csproj" />
</Folder> </Folder>
<Folder Name="/Test/"> <Folder Name="/Test/">