Files
GhostEngine/Ghost.Engine/Utilities/Utf8JsonWriterExtension.cs
Misaki 2881fda112 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.
2025-12-21 22:18:25 +09:00

78 lines
2.0 KiB
C#

using System.Text.Json;
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 void WriteArray<T>(this Utf8JsonWriter writer, ReadOnlySpan<char> name, IEnumerable<T> source, Action<T> writeAction)
{
writer.WriteStartArray(name);
foreach (var item in source)
{
writeAction(item);
}
writer.WriteEndArray();
}
public static void WriteArray<T>(this Utf8JsonWriter writer, ReadOnlySpan<char> name, ReadOnlySpan<T> source, Action<T> writeAction)
{
writer.WriteStartArray(name);
foreach (var item in source)
{
writeAction(item);
}
writer.WriteEndArray();
}
public static void WriteObject(this Utf8JsonWriter writer, Action writeAction)
{
writer.WriteStartObject();
writeAction();
writer.WriteEndObject();
}
public static void WriteObject(this Utf8JsonWriter writer, ReadOnlySpan<char> name, Action writeAction)
{
writer.WriteStartObject(name);
writeAction();
writer.WriteEndObject();
}
public static Utf8JsonObjectScope StartObjectScope(this Utf8JsonWriter writer)
{
return new Utf8JsonObjectScope(writer);
}
public static Utf8JsonArrayScope StartArrayScope(this Utf8JsonWriter writer)
{
return new Utf8JsonArrayScope(writer);
}
}