forked from Misaki/GhostEngine
ECS refactor: new ComponentSet, serialization, generators
Major ECS API overhaul: added ComponentSet, refactored ComponentRegistry, and updated all entity/component creation methods. Introduced robust custom serialization infrastructure and per-component source generators for registration and (de)serialization. Updated editor, engine, and test code to use new APIs. Improved code quality, naming, and performance throughout. Removed obsolete code and updated dependencies.
This commit is contained in:
32
Ghost.Engine/IO/CustomSerializer.cs
Normal file
32
Ghost.Engine/IO/CustomSerializer.cs
Normal file
@@ -0,0 +1,32 @@
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.Text.Json;
|
||||
using System.Text.Json.Serialization;
|
||||
|
||||
namespace Ghost.Engine.IO;
|
||||
|
||||
public class CustomSerializerAttribute : JsonConverterAttribute
|
||||
{
|
||||
public CustomSerializerAttribute([DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicParameterlessConstructor)] Type serializerType)
|
||||
: base(serializerType)
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
public abstract class CustomSerializer<T> : JsonConverter<T>
|
||||
{
|
||||
public sealed override T? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
|
||||
{
|
||||
return DeserializeJson(ref reader, options);
|
||||
}
|
||||
|
||||
public sealed override void Write(Utf8JsonWriter writer, T value, JsonSerializerOptions options)
|
||||
{
|
||||
SerializeJson(writer, value, options);
|
||||
}
|
||||
|
||||
public abstract void SerializeJson(Utf8JsonWriter writer, T value, JsonSerializerOptions options);
|
||||
public abstract T? DeserializeJson(ref Utf8JsonReader reader, JsonSerializerOptions options);
|
||||
|
||||
public abstract void SerializeBinary(BinaryWriter writer, T value);
|
||||
public abstract T? DeserializeBinary(BinaryReader reader);
|
||||
}
|
||||
52
Ghost.Engine/IO/SerializerRegistry.cs
Normal file
52
Ghost.Engine/IO/SerializerRegistry.cs
Normal file
@@ -0,0 +1,52 @@
|
||||
using Ghost.Core;
|
||||
using Ghost.Entities;
|
||||
using System.Text.Json;
|
||||
|
||||
namespace Ghost.Engine.IO;
|
||||
|
||||
public static unsafe class ComponentSerializerRegistry
|
||||
{
|
||||
public delegate void BinaryWriteDelegate(BinaryWriter writer, void* ptr);
|
||||
public delegate void JsonWriteDelegate(Utf8JsonWriter writer, void* ptr, JsonSerializerOptions options);
|
||||
|
||||
private static BinaryWriteDelegate[] s_binWriters = new BinaryWriteDelegate[64];
|
||||
private static JsonWriteDelegate[] s_jsonWriters = new JsonWriteDelegate[64];
|
||||
|
||||
public static void Register(int typeID, BinaryWriteDelegate binWriter, JsonWriteDelegate jsonWriter)
|
||||
{
|
||||
if (typeID < 0)
|
||||
{
|
||||
throw new Exception($"Type ID cannot be negative: {typeID}");
|
||||
}
|
||||
|
||||
if (typeID >= s_binWriters.Length)
|
||||
{
|
||||
Array.Resize(ref s_binWriters, typeID + 16);
|
||||
Array.Resize(ref s_jsonWriters, typeID + 16);
|
||||
}
|
||||
|
||||
s_binWriters[typeID] = binWriter;
|
||||
s_jsonWriters[typeID] = jsonWriter;
|
||||
}
|
||||
|
||||
public static void SerializeBinary(Identifier<IComponent> typeID, BinaryWriter writer, void* ptr)
|
||||
{
|
||||
if (s_binWriters[typeID] == null)
|
||||
{
|
||||
throw new Exception($"No serializer for ID {typeID}");
|
||||
}
|
||||
|
||||
s_binWriters[typeID](writer, ptr);
|
||||
}
|
||||
|
||||
public static void SerializeJson(Identifier<IComponent> typeID, Utf8JsonWriter writer, void* ptr, JsonSerializerOptions options)
|
||||
{
|
||||
if (s_jsonWriters[typeID] == null)
|
||||
{
|
||||
// TODO: Fallback to reflection?
|
||||
return;
|
||||
}
|
||||
|
||||
s_jsonWriters[typeID](writer, ptr, options);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user