refactor: Yaml serialization moved from Core to own project
This commit is contained in:
@@ -7,8 +7,4 @@
|
||||
<RootNamespace>Syntriax.Engine.Core</RootNamespace>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\YamlDotNet\YamlDotNet\YamlDotNet.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
@@ -1,35 +1,3 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace Syntriax.Engine.Core.Serialization;
|
||||
|
||||
public record class EntityReference(string? Id = null);
|
||||
|
||||
public class EntityRegistry
|
||||
{
|
||||
public event EntityRegisteredEventHandler? OnEntityRegistered = null!;
|
||||
|
||||
private readonly Dictionary<string, Action<IEntity>?> assignCallbacks = [];
|
||||
private readonly Dictionary<string, IEntity> registeredEntities = [];
|
||||
public IReadOnlyDictionary<string, IEntity> RegisteredEntities => registeredEntities;
|
||||
|
||||
public void Add(IEntity entity)
|
||||
{
|
||||
if (registeredEntities.TryAdd(entity.Id, entity))
|
||||
OnEntityRegistered?.InvokeSafe(this, entity);
|
||||
}
|
||||
|
||||
public void QueueAssign(string id, Action<IEntity> setMethod)
|
||||
{
|
||||
assignCallbacks.TryAdd(id, null);
|
||||
assignCallbacks[id] = assignCallbacks[id] + setMethod;
|
||||
}
|
||||
|
||||
public void AssignAll()
|
||||
{
|
||||
foreach ((string id, Action<IEntity>? action) in assignCallbacks)
|
||||
action?.InvokeSafe(registeredEntities[id]);
|
||||
}
|
||||
|
||||
public delegate void EntityRegisteredEventHandler(EntityRegistry sender, IEntity entity);
|
||||
}
|
||||
|
39
Engine.Core/Serialization/EntityRegistry.cs
Normal file
39
Engine.Core/Serialization/EntityRegistry.cs
Normal file
@@ -0,0 +1,39 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace Syntriax.Engine.Core.Serialization;
|
||||
|
||||
public class EntityRegistry
|
||||
{
|
||||
public event EntityRegisteredEventHandler? OnEntityRegistered = null!;
|
||||
|
||||
private readonly Dictionary<string, Action<IEntity>?> assignCallbacks = [];
|
||||
private readonly Dictionary<string, IEntity> registeredEntities = [];
|
||||
public IReadOnlyDictionary<string, IEntity> RegisteredEntities => registeredEntities;
|
||||
|
||||
public void Add(IEntity entity)
|
||||
{
|
||||
if (registeredEntities.TryAdd(entity.Id, entity))
|
||||
OnEntityRegistered?.InvokeSafe(this, entity);
|
||||
}
|
||||
|
||||
public void QueueAssign(string id, Action<IEntity> setMethod)
|
||||
{
|
||||
assignCallbacks.TryAdd(id, null);
|
||||
assignCallbacks[id] = assignCallbacks[id] + setMethod;
|
||||
}
|
||||
|
||||
public void AssignAll()
|
||||
{
|
||||
foreach ((string id, Action<IEntity>? action) in assignCallbacks)
|
||||
action?.InvokeSafe(registeredEntities[id]);
|
||||
}
|
||||
|
||||
public void Reset()
|
||||
{
|
||||
assignCallbacks.Clear();
|
||||
registeredEntities.Clear();
|
||||
}
|
||||
|
||||
public delegate void EntityRegisteredEventHandler(EntityRegistry sender, IEntity entity);
|
||||
}
|
12
Engine.Core/Serialization/ISerializer.cs
Normal file
12
Engine.Core/Serialization/ISerializer.cs
Normal file
@@ -0,0 +1,12 @@
|
||||
using System;
|
||||
|
||||
namespace Syntriax.Engine.Core.Serialization;
|
||||
|
||||
public interface ISerializer
|
||||
{
|
||||
object Deserialize(string configuration);
|
||||
object Deserialize(string configuration, Type type);
|
||||
T Deserialize<T>(string configuration);
|
||||
|
||||
string Serialize(object instance);
|
||||
}
|
@@ -26,7 +26,7 @@ public class SerializedClass
|
||||
Type type = @class.GetType();
|
||||
Type = type.FullName ?? type.Name;
|
||||
|
||||
bool isFullySerializable = type.HasAttribute<SerializeAllAttribute>();
|
||||
bool shouldSerializeAll = type.HasAttribute<SerializeAllAttribute>();
|
||||
|
||||
Public.Clear();
|
||||
Private.Clear();
|
||||
@@ -39,7 +39,7 @@ public class SerializedClass
|
||||
if (privatePropertyInfo.SetMethod is null)
|
||||
continue;
|
||||
|
||||
if (!isFullySerializable && !privatePropertyInfo.HasAttribute<SerializeAttribute>())
|
||||
if (!shouldSerializeAll && !privatePropertyInfo.HasAttribute<SerializeAttribute>())
|
||||
continue;
|
||||
|
||||
object? value = privatePropertyInfo.GetValue(@class);
|
||||
@@ -57,7 +57,7 @@ public class SerializedClass
|
||||
if (publicPropertyInfo.SetMethod is null)
|
||||
continue;
|
||||
|
||||
if (!isFullySerializable && !publicPropertyInfo.HasAttribute<SerializeAttribute>())
|
||||
if (!shouldSerializeAll && !publicPropertyInfo.HasAttribute<SerializeAttribute>())
|
||||
continue;
|
||||
|
||||
object? value = publicPropertyInfo.GetValue(@class);
|
||||
@@ -72,7 +72,7 @@ public class SerializedClass
|
||||
if (privateFieldInfo.HasAttribute<System.Runtime.CompilerServices.CompilerGeneratedAttribute>())
|
||||
continue;
|
||||
|
||||
if (!isFullySerializable && !privateFieldInfo.HasAttribute<SerializeAttribute>())
|
||||
if (!shouldSerializeAll && !privateFieldInfo.HasAttribute<SerializeAttribute>())
|
||||
continue;
|
||||
|
||||
object? value = privateFieldInfo.GetValue(@class);
|
||||
@@ -87,7 +87,7 @@ public class SerializedClass
|
||||
if (publicFieldInfo.HasAttribute<System.Runtime.CompilerServices.CompilerGeneratedAttribute>())
|
||||
continue;
|
||||
|
||||
if (!isFullySerializable && !publicFieldInfo.HasAttribute<SerializeAttribute>())
|
||||
if (!shouldSerializeAll && !publicFieldInfo.HasAttribute<SerializeAttribute>())
|
||||
continue;
|
||||
|
||||
object? value = publicFieldInfo.GetValue(@class);
|
||||
|
@@ -1,74 +0,0 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
|
||||
using YamlDotNet.Serialization;
|
||||
using YamlDotNet.Serialization.NamingConventions;
|
||||
|
||||
namespace Syntriax.Engine.Core.Serialization;
|
||||
|
||||
public static class Serializer
|
||||
{
|
||||
private static readonly ISerializer serializer = GetSerializer();
|
||||
private static ISerializer GetSerializer()
|
||||
{
|
||||
SerializerBuilder serializerBuilder = new SerializerBuilder()
|
||||
.WithNamingConvention(PascalCaseNamingConvention.Instance)
|
||||
.DisableAliases();
|
||||
|
||||
foreach (IEngineTypeYamlSerializer typeConverter in GetEngineYamlTypeConverters())
|
||||
serializerBuilder = serializerBuilder.WithTypeConverter(typeConverter);
|
||||
|
||||
return serializerBuilder.Build();
|
||||
}
|
||||
|
||||
private static IDeserializer GetDeserializer(EntityRegistry entityRegistry)
|
||||
{
|
||||
DeserializerBuilder serializerBuilder = new DeserializerBuilder()
|
||||
.WithNamingConvention(PascalCaseNamingConvention.Instance);
|
||||
|
||||
foreach (IEngineTypeYamlSerializer typeConverter in GetEngineYamlTypeConverters())
|
||||
{
|
||||
typeConverter.EntityRegistry = entityRegistry;
|
||||
serializerBuilder = serializerBuilder.WithTypeConverter(typeConverter);
|
||||
}
|
||||
|
||||
return serializerBuilder.Build();
|
||||
}
|
||||
|
||||
private static IEnumerable<IEngineTypeYamlSerializer> GetEngineYamlTypeConverters()
|
||||
{
|
||||
foreach (Type type in Assembly.GetExecutingAssembly().GetTypes().Where(t => typeof(IEngineTypeYamlSerializer).IsAssignableFrom(t) && t.IsClass && !t.IsAbstract))
|
||||
yield return (Activator.CreateInstance(type) as IEngineTypeYamlSerializer)!;
|
||||
}
|
||||
|
||||
public static string Serialize(object instance)
|
||||
{
|
||||
return serializer.Serialize(instance);
|
||||
}
|
||||
|
||||
public static object Deserialize(string yaml)
|
||||
{
|
||||
EntityRegistry entityRegistry = new();
|
||||
object result = GetDeserializer(entityRegistry).Deserialize(yaml)!;
|
||||
entityRegistry.AssignAll();
|
||||
return result;
|
||||
}
|
||||
|
||||
public static object Deserialize(string yaml, Type type)
|
||||
{
|
||||
EntityRegistry entityRegistry = new();
|
||||
object result = GetDeserializer(entityRegistry).Deserialize(yaml, type)!;
|
||||
entityRegistry.AssignAll();
|
||||
return result;
|
||||
}
|
||||
|
||||
public static T Deserialize<T>(string yaml)
|
||||
{
|
||||
EntityRegistry entityRegistry = new();
|
||||
T result = GetDeserializer(entityRegistry).Deserialize<T>(yaml);
|
||||
entityRegistry.AssignAll();
|
||||
return result;
|
||||
}
|
||||
}
|
@@ -1,8 +0,0 @@
|
||||
using YamlDotNet.Serialization;
|
||||
|
||||
namespace Syntriax.Engine.Core.Serialization;
|
||||
|
||||
public interface IEngineTypeYamlSerializer : IYamlTypeConverter
|
||||
{
|
||||
EntityRegistry EntityRegistry { get; set; }
|
||||
}
|
@@ -1,76 +0,0 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
|
||||
using YamlDotNet.Core;
|
||||
using YamlDotNet.Core.Events;
|
||||
using YamlDotNet.Serialization;
|
||||
|
||||
namespace Syntriax.Engine.Core.Serialization;
|
||||
|
||||
public class BehaviourControllerSerializer : EngineTypeYamlSerializerBase<IBehaviourController>
|
||||
{
|
||||
private const string BEHAVIOURS_SCALAR_NAME = "Behaviours";
|
||||
|
||||
public override IBehaviourController? Read(IParser parser, Type type, ObjectDeserializer rootDeserializer)
|
||||
{
|
||||
string id;
|
||||
|
||||
IBehaviourController behaviourController;
|
||||
IStateEnable stateEnable;
|
||||
List<IBehaviour> behaviours;
|
||||
|
||||
parser.Consume<MappingStart>();
|
||||
|
||||
if (parser.Consume<Scalar>().Value.CompareTo(nameof(IBehaviourController.Id)) != 0)
|
||||
throw new();
|
||||
id = parser.Consume<Scalar>().Value;
|
||||
|
||||
if (parser.Consume<Scalar>().Value.CompareTo(SERIALIZED_SCALAR_NAME) != 0)
|
||||
throw new();
|
||||
SerializedClass instanceSerializedClass = (SerializedClass)rootDeserializer(typeof(SerializedClass))!;
|
||||
behaviourController = (IBehaviourController)instanceSerializedClass.CreateInstance(EntityRegistry);
|
||||
|
||||
string value = parser.Consume<Scalar>().Value;
|
||||
if (value.CompareTo(nameof(IBehaviourController.StateEnable)) != 0)
|
||||
throw new();
|
||||
stateEnable = (IStateEnable)rootDeserializer(typeof(IStateEnable))!;
|
||||
|
||||
if (parser.Consume<Scalar>().Value.CompareTo(BEHAVIOURS_SCALAR_NAME) != 0)
|
||||
throw new();
|
||||
behaviours = (List<IBehaviour>)rootDeserializer(typeof(List<IBehaviour>))!;
|
||||
|
||||
parser.Consume<MappingEnd>();
|
||||
|
||||
behaviourController.Id = id;
|
||||
|
||||
stateEnable.Assign(behaviourController);
|
||||
behaviourController.Assign(stateEnable);
|
||||
|
||||
foreach (IBehaviour behaviour in behaviours)
|
||||
behaviourController.AddBehaviour(behaviour);
|
||||
|
||||
return behaviourController;
|
||||
}
|
||||
|
||||
public override void WriteYaml(IEmitter emitter, object? value, Type type, ObjectSerializer serializer)
|
||||
{
|
||||
IBehaviourController behaviourController = (IBehaviourController)value!;
|
||||
|
||||
emitter.Emit(new MappingStart());
|
||||
|
||||
emitter.Emit(new Scalar(nameof(IBehaviourController.Id)));
|
||||
emitter.Emit(new Scalar(behaviourController.Id));
|
||||
|
||||
emitter.Emit(new Scalar(SERIALIZED_SCALAR_NAME));
|
||||
serializer(new SerializedClass(behaviourController));
|
||||
|
||||
emitter.Emit(new Scalar(nameof(IBehaviourController.StateEnable)));
|
||||
serializer(behaviourController.StateEnable);
|
||||
|
||||
emitter.Emit(new Scalar(BEHAVIOURS_SCALAR_NAME));
|
||||
serializer(behaviourController.GetBehaviours<IBehaviour>().Where(b => !b.GetType().HasAttribute<IgnoreSerializationAttribute>()));
|
||||
|
||||
emitter.Emit(new MappingEnd());
|
||||
}
|
||||
}
|
@@ -1,69 +0,0 @@
|
||||
using System;
|
||||
|
||||
using YamlDotNet.Core;
|
||||
using YamlDotNet.Core.Events;
|
||||
using YamlDotNet.Serialization;
|
||||
|
||||
namespace Syntriax.Engine.Core.Serialization;
|
||||
|
||||
public class BehaviourSerializer : EngineTypeYamlSerializerBase<IBehaviour>
|
||||
{
|
||||
public override IBehaviour? Read(IParser parser, Type type, ObjectDeserializer rootDeserializer)
|
||||
{
|
||||
string id;
|
||||
int priority;
|
||||
|
||||
IBehaviour behaviour;
|
||||
IStateEnable stateEnable;
|
||||
|
||||
parser.Consume<MappingStart>();
|
||||
|
||||
if (parser.Consume<Scalar>().Value.CompareTo(nameof(IBehaviour.Id)) != 0)
|
||||
throw new();
|
||||
id = parser.Consume<Scalar>().Value;
|
||||
|
||||
if (parser.Consume<Scalar>().Value.CompareTo(nameof(IBehaviour.Priority)) != 0)
|
||||
throw new();
|
||||
priority = int.Parse(parser.Consume<Scalar>().Value);
|
||||
|
||||
if (parser.Consume<Scalar>().Value.CompareTo(SERIALIZED_SCALAR_NAME) != 0)
|
||||
throw new();
|
||||
SerializedClass instanceSerializedClass = (SerializedClass)rootDeserializer(typeof(SerializedClass))!;
|
||||
behaviour = (IBehaviour)instanceSerializedClass.CreateInstance(EntityRegistry);
|
||||
|
||||
if (parser.Consume<Scalar>().Value.CompareTo(nameof(IBehaviour.StateEnable)) != 0)
|
||||
throw new();
|
||||
stateEnable = (IStateEnable)rootDeserializer(typeof(IStateEnable))!;
|
||||
|
||||
parser.Consume<MappingEnd>();
|
||||
|
||||
behaviour.Id = id;
|
||||
behaviour.Priority = priority;
|
||||
|
||||
stateEnable.Assign(behaviour);
|
||||
behaviour.Assign(stateEnable);
|
||||
|
||||
return behaviour;
|
||||
}
|
||||
|
||||
public override void WriteYaml(IEmitter emitter, object? value, Type type, ObjectSerializer serializer)
|
||||
{
|
||||
IBehaviour behaviour = (IBehaviour)value!;
|
||||
|
||||
emitter.Emit(new MappingStart());
|
||||
|
||||
emitter.Emit(new Scalar(nameof(IBehaviour.Id)));
|
||||
emitter.Emit(new Scalar(behaviour.Id));
|
||||
|
||||
emitter.Emit(new Scalar(nameof(IBehaviour.Priority)));
|
||||
emitter.Emit(new Scalar(behaviour.Priority.ToString()));
|
||||
|
||||
emitter.Emit(new Scalar(SERIALIZED_SCALAR_NAME));
|
||||
serializer(new SerializedClass(behaviour));
|
||||
|
||||
emitter.Emit(new Scalar(nameof(IBehaviour.StateEnable)));
|
||||
serializer(behaviour.StateEnable);
|
||||
|
||||
emitter.Emit(new MappingEnd());
|
||||
}
|
||||
}
|
@@ -1,28 +0,0 @@
|
||||
using System;
|
||||
|
||||
using YamlDotNet.Core;
|
||||
using YamlDotNet.Serialization;
|
||||
|
||||
namespace Syntriax.Engine.Core.Serialization;
|
||||
|
||||
public abstract class EngineTypeYamlSerializerBase<T> : IEngineTypeYamlSerializer
|
||||
{
|
||||
protected const string SERIALIZED_SCALAR_NAME = "Properties";
|
||||
|
||||
public EntityRegistry EntityRegistry { get; set; } = null!;
|
||||
|
||||
public bool Accepts(Type type) => typeof(T).IsAssignableFrom(type);
|
||||
|
||||
public abstract T? Read(IParser parser, Type type, ObjectDeserializer rootDeserializer);
|
||||
public object? ReadYaml(IParser parser, Type type, ObjectDeserializer rootDeserializer)
|
||||
{
|
||||
T? result = Read(parser, type, rootDeserializer);
|
||||
|
||||
if (result is IEntity entity)
|
||||
EntityRegistry.Add(entity);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
public abstract void WriteYaml(IEmitter emitter, object? value, Type type, ObjectSerializer serializer);
|
||||
}
|
@@ -1,40 +0,0 @@
|
||||
using System;
|
||||
|
||||
using YamlDotNet.Core;
|
||||
using YamlDotNet.Core.Events;
|
||||
using YamlDotNet.Serialization;
|
||||
|
||||
namespace Syntriax.Engine.Core.Serialization;
|
||||
|
||||
public class AABBSerializer : EngineTypeYamlSerializerBase<AABB>
|
||||
{
|
||||
public override AABB Read(IParser parser, Type type, ObjectDeserializer rootDeserializer)
|
||||
{
|
||||
parser.Consume<MappingStart>();
|
||||
|
||||
if (parser.Consume<Scalar>().Value.CompareTo(nameof(AABB.LowerBoundary)) != 0)
|
||||
throw new ArgumentException($"{nameof(AABB)} mapping must start with {nameof(AABB.LowerBoundary)}");
|
||||
Vector2D lowerBoundary = (Vector2D)rootDeserializer(typeof(Vector2D))!;
|
||||
|
||||
if (parser.Consume<Scalar>().Value.CompareTo(nameof(AABB.UpperBoundary)) != 0)
|
||||
throw new ArgumentException($"{nameof(AABB)} mapping must end with {nameof(AABB.UpperBoundary)}");
|
||||
Vector2D upperBoundary = (Vector2D)rootDeserializer(typeof(Vector2D))!;
|
||||
|
||||
parser.Consume<MappingEnd>();
|
||||
|
||||
return new AABB(lowerBoundary, upperBoundary);
|
||||
}
|
||||
|
||||
public override void WriteYaml(IEmitter emitter, object? value, Type type, ObjectSerializer serializer)
|
||||
{
|
||||
AABB aabb = (AABB)value!;
|
||||
|
||||
emitter.Emit(new MappingStart());
|
||||
emitter.Emit(new Scalar(nameof(AABB.LowerBoundary)));
|
||||
serializer(aabb.LowerBoundary, typeof(Vector2D));
|
||||
emitter.Emit(new Scalar(nameof(AABB.UpperBoundary)));
|
||||
serializer(aabb.UpperBoundary, typeof(Vector2D));
|
||||
emitter.Emit(new MappingEnd());
|
||||
}
|
||||
|
||||
}
|
@@ -1,39 +0,0 @@
|
||||
using System;
|
||||
|
||||
using YamlDotNet.Core;
|
||||
using YamlDotNet.Core.Events;
|
||||
using YamlDotNet.Serialization;
|
||||
|
||||
namespace Syntriax.Engine.Core.Serialization;
|
||||
|
||||
public class CircleSerializer : EngineTypeYamlSerializerBase<Circle>
|
||||
{
|
||||
public override Circle Read(IParser parser, Type type, ObjectDeserializer rootDeserializer)
|
||||
{
|
||||
parser.Consume<MappingStart>();
|
||||
|
||||
if (parser.Consume<Scalar>().Value.CompareTo(nameof(Circle.Center)) != 0)
|
||||
throw new ArgumentException($"{nameof(Circle)} mapping must start with {nameof(Circle.Center)}");
|
||||
Vector2D lowerBoundary = (Vector2D)rootDeserializer(typeof(Vector2D))!;
|
||||
|
||||
if (parser.Consume<Scalar>().Value.CompareTo(nameof(Circle.Radius)) != 0)
|
||||
throw new ArgumentException($"{nameof(Circle)} mapping must end with {nameof(Circle.Radius)}");
|
||||
float radius = (float)rootDeserializer(typeof(float))!;
|
||||
|
||||
parser.Consume<MappingEnd>();
|
||||
|
||||
return new Circle(lowerBoundary, radius);
|
||||
}
|
||||
|
||||
public override void WriteYaml(IEmitter emitter, object? value, Type type, ObjectSerializer serializer)
|
||||
{
|
||||
Circle circle = (Circle)value!;
|
||||
|
||||
emitter.Emit(new MappingStart());
|
||||
emitter.Emit(new Scalar(nameof(Circle.Center)));
|
||||
serializer(circle.Center, typeof(Vector2D));
|
||||
emitter.Emit(new Scalar(nameof(Circle.Radius)));
|
||||
serializer(circle.Radius, typeof(float));
|
||||
emitter.Emit(new MappingEnd());
|
||||
}
|
||||
}
|
@@ -1,39 +0,0 @@
|
||||
using System;
|
||||
|
||||
using YamlDotNet.Core;
|
||||
using YamlDotNet.Core.Events;
|
||||
using YamlDotNet.Serialization;
|
||||
|
||||
namespace Syntriax.Engine.Core.Serialization;
|
||||
|
||||
public class Line2DEquationSerializer : EngineTypeYamlSerializerBase<Line2DEquation>
|
||||
{
|
||||
public override Line2DEquation Read(IParser parser, Type type, ObjectDeserializer rootDeserializer)
|
||||
{
|
||||
parser.Consume<MappingStart>();
|
||||
|
||||
if (parser.Consume<Scalar>().Value.CompareTo(nameof(Line2DEquation.Slope)) != 0)
|
||||
throw new ArgumentException($"{nameof(Line2DEquation)} mapping must start with {nameof(Line2DEquation.Slope)}");
|
||||
float slope = (float)rootDeserializer(typeof(float))!;
|
||||
|
||||
if (parser.Consume<Scalar>().Value.CompareTo(nameof(Line2DEquation.OffsetY)) != 0)
|
||||
throw new ArgumentException($"{nameof(Line2DEquation)} mapping must end with {nameof(Line2DEquation.OffsetY)}");
|
||||
float offset = (float)rootDeserializer(typeof(float))!;
|
||||
|
||||
parser.Consume<MappingEnd>();
|
||||
|
||||
return new Line2DEquation(slope, offset);
|
||||
}
|
||||
|
||||
public override void WriteYaml(IEmitter emitter, object? value, Type type, ObjectSerializer serializer)
|
||||
{
|
||||
Line2DEquation line2DEquation = (Line2DEquation)value!;
|
||||
|
||||
emitter.Emit(new MappingStart());
|
||||
emitter.Emit(new Scalar(nameof(Line2DEquation.Slope)));
|
||||
serializer(line2DEquation.Slope, typeof(float));
|
||||
emitter.Emit(new Scalar(nameof(Line2DEquation.OffsetY)));
|
||||
serializer(line2DEquation.OffsetY, typeof(float));
|
||||
emitter.Emit(new MappingEnd());
|
||||
}
|
||||
}
|
@@ -1,39 +0,0 @@
|
||||
using System;
|
||||
|
||||
using YamlDotNet.Core;
|
||||
using YamlDotNet.Core.Events;
|
||||
using YamlDotNet.Serialization;
|
||||
|
||||
namespace Syntriax.Engine.Core.Serialization;
|
||||
|
||||
public class Line2DSerializer : EngineTypeYamlSerializerBase<Line2D>
|
||||
{
|
||||
public override Line2D Read(IParser parser, Type type, ObjectDeserializer rootDeserializer)
|
||||
{
|
||||
parser.Consume<MappingStart>();
|
||||
|
||||
if (parser.Consume<Scalar>().Value.CompareTo(nameof(Line2D.From)) != 0)
|
||||
throw new ArgumentException($"{nameof(Line2D)} mapping must start with {nameof(Line2D.From)}");
|
||||
Vector2D from = (Vector2D)rootDeserializer(typeof(Vector2D))!;
|
||||
|
||||
if (parser.Consume<Scalar>().Value.CompareTo(nameof(Line2D.To)) != 0)
|
||||
throw new ArgumentException($"{nameof(Line2D)} mapping must end with {nameof(Line2D.To)}");
|
||||
Vector2D to = (Vector2D)rootDeserializer(typeof(Vector2D))!;
|
||||
|
||||
parser.Consume<MappingEnd>();
|
||||
|
||||
return new Line2D(from, to);
|
||||
}
|
||||
|
||||
public override void WriteYaml(IEmitter emitter, object? value, Type type, ObjectSerializer serializer)
|
||||
{
|
||||
Line2D line2D = (Line2D)value!;
|
||||
|
||||
emitter.Emit(new MappingStart());
|
||||
emitter.Emit(new Scalar(nameof(Line2D.From)));
|
||||
serializer(line2D.From, typeof(Vector2D));
|
||||
emitter.Emit(new Scalar(nameof(Line2D.To)));
|
||||
serializer(line2D.To, typeof(Vector2D));
|
||||
emitter.Emit(new MappingEnd());
|
||||
}
|
||||
}
|
@@ -1,39 +0,0 @@
|
||||
using System;
|
||||
|
||||
using YamlDotNet.Core;
|
||||
using YamlDotNet.Core.Events;
|
||||
using YamlDotNet.Serialization;
|
||||
|
||||
namespace Syntriax.Engine.Core.Serialization;
|
||||
|
||||
public class Projection1DSerializer : EngineTypeYamlSerializerBase<Projection1D>
|
||||
{
|
||||
public override Projection1D Read(IParser parser, Type type, ObjectDeserializer rootDeserializer)
|
||||
{
|
||||
parser.Consume<MappingStart>();
|
||||
|
||||
if (parser.Consume<Scalar>().Value.CompareTo(nameof(Projection1D.Min)) != 0)
|
||||
throw new ArgumentException($"{nameof(Projection1D)} mapping must start with {nameof(Projection1D.Min)}");
|
||||
float min = (float)rootDeserializer(typeof(float))!;
|
||||
|
||||
if (parser.Consume<Scalar>().Value.CompareTo(nameof(Projection1D.Max)) != 0)
|
||||
throw new ArgumentException($"{nameof(Projection1D)} mapping must end with {nameof(Projection1D.Max)}");
|
||||
float max = (float)rootDeserializer(typeof(float))!;
|
||||
|
||||
parser.Consume<MappingEnd>();
|
||||
|
||||
return new Projection1D(min, max);
|
||||
}
|
||||
|
||||
public override void WriteYaml(IEmitter emitter, object? value, Type type, ObjectSerializer serializer)
|
||||
{
|
||||
Projection1D projection1D = (Projection1D)value!;
|
||||
|
||||
emitter.Emit(new MappingStart());
|
||||
emitter.Emit(new Scalar(nameof(Projection1D.Min)));
|
||||
serializer(projection1D.Min, typeof(float));
|
||||
emitter.Emit(new Scalar(nameof(Projection1D.Max)));
|
||||
serializer(projection1D.Max, typeof(float));
|
||||
emitter.Emit(new MappingEnd());
|
||||
}
|
||||
}
|
@@ -1,26 +0,0 @@
|
||||
using System;
|
||||
|
||||
using YamlDotNet.Core;
|
||||
using YamlDotNet.Core.Events;
|
||||
using YamlDotNet.Serialization;
|
||||
|
||||
namespace Syntriax.Engine.Core.Serialization;
|
||||
|
||||
public class QuaternionSerializer : EngineTypeYamlSerializerBase<Quaternion>
|
||||
{
|
||||
private static readonly int SUBSTRING_START_LENGTH = nameof(Quaternion).Length + 1;
|
||||
|
||||
public override Quaternion Read(IParser parser, Type type, ObjectDeserializer rootDeserializer)
|
||||
{
|
||||
string value = parser.Consume<Scalar>().Value;
|
||||
string insideParenthesis = value[SUBSTRING_START_LENGTH..^1];
|
||||
string[] values = insideParenthesis.Split(", ");
|
||||
return new Quaternion(float.Parse(values[0]), float.Parse(values[1]), float.Parse(values[2]), float.Parse(values[3]));
|
||||
}
|
||||
|
||||
public override void WriteYaml(IEmitter emitter, object? value, Type type, ObjectSerializer serializer)
|
||||
{
|
||||
Quaternion quaternion = (Quaternion)value!;
|
||||
emitter.Emit(new Scalar($"{nameof(Quaternion)}({quaternion.X}, {quaternion.Y}, {quaternion.Z}, {quaternion.W})"));
|
||||
}
|
||||
}
|
@@ -1,46 +0,0 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
using YamlDotNet.Core;
|
||||
using YamlDotNet.Core.Events;
|
||||
using YamlDotNet.Serialization;
|
||||
|
||||
namespace Syntriax.Engine.Core.Serialization;
|
||||
|
||||
public class Shape2DSerializer : EngineTypeYamlSerializerBase<Shape2D>
|
||||
{
|
||||
public override Shape2D Read(IParser parser, Type type, ObjectDeserializer rootDeserializer)
|
||||
{
|
||||
parser.Consume<MappingStart>();
|
||||
|
||||
if (parser.Consume<Scalar>().Value.CompareTo(nameof(Shape2D.Vertices)) != 0)
|
||||
throw new ArgumentException($"{nameof(Shape2D)} mapping have a {nameof(Shape2D.Vertices)}");
|
||||
|
||||
parser.Consume<SequenceStart>();
|
||||
|
||||
List<Vector2D> vertices = [];
|
||||
|
||||
while (!parser.TryConsume<SequenceEnd>(out _))
|
||||
{
|
||||
Vector2D vertex = (Vector2D)rootDeserializer(typeof(Vector2D))!;
|
||||
vertices.Add(vertex);
|
||||
}
|
||||
|
||||
parser.Consume<MappingEnd>();
|
||||
|
||||
return new Shape2D(vertices);
|
||||
}
|
||||
|
||||
public override void WriteYaml(IEmitter emitter, object? value, Type type, ObjectSerializer serializer)
|
||||
{
|
||||
Shape2D shape2D = (Shape2D)value!;
|
||||
|
||||
emitter.Emit(new MappingStart());
|
||||
emitter.Emit(new Scalar(nameof(Shape2D.Vertices)));
|
||||
emitter.Emit(new SequenceStart(anchor: null, tag: null, isImplicit: false, style: SequenceStyle.Block));
|
||||
foreach (Vector2D vertex in shape2D.Vertices)
|
||||
serializer(vertex, typeof(Vector2D));
|
||||
emitter.Emit(new SequenceEnd());
|
||||
emitter.Emit(new MappingEnd());
|
||||
}
|
||||
}
|
@@ -1,45 +0,0 @@
|
||||
using System;
|
||||
|
||||
using YamlDotNet.Core;
|
||||
using YamlDotNet.Core.Events;
|
||||
using YamlDotNet.Serialization;
|
||||
|
||||
namespace Syntriax.Engine.Core.Serialization;
|
||||
|
||||
public class TriangleSerializer : EngineTypeYamlSerializerBase<Triangle>
|
||||
{
|
||||
public override Triangle Read(IParser parser, Type type, ObjectDeserializer rootDeserializer)
|
||||
{
|
||||
parser.Consume<MappingStart>();
|
||||
|
||||
if (parser.Consume<Scalar>().Value.CompareTo(nameof(Triangle.A)) != 0)
|
||||
throw new ArgumentException($"{nameof(Triangle)} mapping must start with {nameof(Triangle.A)}");
|
||||
Vector2D a = (Vector2D)rootDeserializer(typeof(Vector2D))!;
|
||||
|
||||
if (parser.Consume<Scalar>().Value.CompareTo(nameof(Triangle.B)) != 0)
|
||||
throw new ArgumentException($"{nameof(Triangle)} mapping must have {nameof(Triangle.B)} after {nameof(Triangle.A)}");
|
||||
Vector2D b = (Vector2D)rootDeserializer(typeof(Vector2D))!;
|
||||
|
||||
if (parser.Consume<Scalar>().Value.CompareTo(nameof(Triangle.C)) != 0)
|
||||
throw new ArgumentException($"{nameof(Triangle)} mapping must end with {nameof(Triangle.C)}");
|
||||
Vector2D c = (Vector2D)rootDeserializer(typeof(Vector2D))!;
|
||||
|
||||
parser.Consume<MappingEnd>();
|
||||
|
||||
return new Triangle(a, b, c);
|
||||
}
|
||||
|
||||
public override void WriteYaml(IEmitter emitter, object? value, Type type, ObjectSerializer serializer)
|
||||
{
|
||||
Triangle aabb = (Triangle)value!;
|
||||
|
||||
emitter.Emit(new MappingStart());
|
||||
emitter.Emit(new Scalar(nameof(Triangle.A)));
|
||||
serializer(aabb.A, typeof(Vector2D));
|
||||
emitter.Emit(new Scalar(nameof(Triangle.B)));
|
||||
serializer(aabb.B, typeof(Vector2D));
|
||||
emitter.Emit(new Scalar(nameof(Triangle.C)));
|
||||
serializer(aabb.C, typeof(Vector2D));
|
||||
emitter.Emit(new MappingEnd());
|
||||
}
|
||||
}
|
@@ -1,26 +0,0 @@
|
||||
using System;
|
||||
|
||||
using YamlDotNet.Core;
|
||||
using YamlDotNet.Core.Events;
|
||||
using YamlDotNet.Serialization;
|
||||
|
||||
namespace Syntriax.Engine.Core.Serialization;
|
||||
|
||||
public class Vector2DSerializer : EngineTypeYamlSerializerBase<Vector2D>
|
||||
{
|
||||
private static readonly int SUBSTRING_START_LENGTH = nameof(Vector2D).Length + 1;
|
||||
|
||||
public override Vector2D Read(IParser parser, Type type, ObjectDeserializer rootDeserializer)
|
||||
{
|
||||
string value = parser.Consume<Scalar>().Value;
|
||||
string insideParenthesis = value[SUBSTRING_START_LENGTH..^1];
|
||||
string[] values = insideParenthesis.Split(", ");
|
||||
return new Vector2D(float.Parse(values[0]), float.Parse(values[1]));
|
||||
}
|
||||
|
||||
public override void WriteYaml(IEmitter emitter, object? value, Type type, ObjectSerializer serializer)
|
||||
{
|
||||
Vector2D vector2D = (Vector2D)value!;
|
||||
emitter.Emit(new Scalar($"{nameof(Vector2D)}({vector2D.X}, {vector2D.Y})"));
|
||||
}
|
||||
}
|
@@ -1,26 +0,0 @@
|
||||
using System;
|
||||
|
||||
using YamlDotNet.Core;
|
||||
using YamlDotNet.Core.Events;
|
||||
using YamlDotNet.Serialization;
|
||||
|
||||
namespace Syntriax.Engine.Core.Serialization;
|
||||
|
||||
public class Vector3DSerializer : EngineTypeYamlSerializerBase<Vector3D>
|
||||
{
|
||||
private static readonly int SUBSTRING_START_LENGTH = nameof(Vector3D).Length + 1;
|
||||
|
||||
public override Vector3D Read(IParser parser, Type type, ObjectDeserializer rootDeserializer)
|
||||
{
|
||||
string value = parser.Consume<Scalar>().Value;
|
||||
string insideParenthesis = value[SUBSTRING_START_LENGTH..^1];
|
||||
string[] values = insideParenthesis.Split(", ");
|
||||
return new Vector3D(float.Parse(values[0]), float.Parse(values[1]), float.Parse(values[2]));
|
||||
}
|
||||
|
||||
public override void WriteYaml(IEmitter emitter, object? value, Type type, ObjectSerializer serializer)
|
||||
{
|
||||
Vector3D vector3D = (Vector3D)value!;
|
||||
emitter.Emit(new Scalar($"{nameof(Vector3D)}({vector3D.X}, {vector3D.Y}, {vector3D.Z})"));
|
||||
}
|
||||
}
|
@@ -1,76 +0,0 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
|
||||
using Syntriax.Engine.Core.Factory;
|
||||
|
||||
using YamlDotNet.Core;
|
||||
using YamlDotNet.Core.Events;
|
||||
using YamlDotNet.Serialization;
|
||||
|
||||
namespace Syntriax.Engine.Core.Serialization;
|
||||
|
||||
public class SerializedClassSerializer : EngineTypeYamlSerializerBase<SerializedClass>
|
||||
{
|
||||
public override SerializedClass? Read(IParser parser, Type type, ObjectDeserializer rootDeserializer)
|
||||
{
|
||||
SerializedClass serializedClass = new();
|
||||
Dictionary<string, TypeContainer> publicDictionary = [];
|
||||
Dictionary<string, TypeContainer> privateDictionary = [];
|
||||
|
||||
parser.Consume<MappingStart>();
|
||||
|
||||
while (!parser.TryConsume<MappingEnd>(out _))
|
||||
{
|
||||
string key = parser.Consume<Scalar>().Value;
|
||||
|
||||
switch (key)
|
||||
{
|
||||
case nameof(SerializedClass.Type): serializedClass.Type = parser.Consume<Scalar>().Value; break;
|
||||
case nameof(SerializedClass.Public): publicDictionary = (Dictionary<string, TypeContainer>)rootDeserializer(typeof(Dictionary<string, TypeContainer>))!; break;
|
||||
case nameof(SerializedClass.Private): privateDictionary = (Dictionary<string, TypeContainer>)rootDeserializer(typeof(Dictionary<string, TypeContainer>))!; break;
|
||||
}
|
||||
}
|
||||
|
||||
foreach ((string key, TypeContainer typeContainer) in publicDictionary)
|
||||
serializedClass.Public.Add(key, Serializer.Deserialize(typeContainer.Value!.ToString()!, TypeFactory.GetType(typeContainer.Type)));
|
||||
|
||||
foreach ((string key, TypeContainer typeContainer) in privateDictionary)
|
||||
serializedClass.Private.Add(key, Serializer.Deserialize(typeContainer.Value!.ToString()!, TypeFactory.GetType(typeContainer.Type)));
|
||||
|
||||
return serializedClass;
|
||||
}
|
||||
|
||||
public override void WriteYaml(IEmitter emitter, object? value, Type type, ObjectSerializer serializer)
|
||||
{
|
||||
SerializedClass serializedClass = (SerializedClass)value!;
|
||||
|
||||
Dictionary<string, TypeContainer> publics = [];
|
||||
Dictionary<string, TypeContainer> privates = [];
|
||||
|
||||
foreach ((string key, object? @object) in serializedClass.Public.Where(v => !v.GetType().HasAttribute<IgnoreSerializationAttribute>()))
|
||||
publics.Add(key, new TypeContainer(@object));
|
||||
|
||||
foreach ((string key, object? @object) in serializedClass.Private.Where(v => !v.GetType().HasAttribute<IgnoreSerializationAttribute>()))
|
||||
privates.Add(key, new TypeContainer(@object));
|
||||
|
||||
emitter.Emit(new MappingStart());
|
||||
|
||||
emitter.Emit(new Scalar(nameof(SerializedClass.Type)));
|
||||
emitter.Emit(new Scalar(serializedClass.Type));
|
||||
|
||||
if (publics.Count > 0)
|
||||
{
|
||||
emitter.Emit(new Scalar(nameof(SerializedClass.Public)));
|
||||
serializer(publics);
|
||||
}
|
||||
|
||||
if (privates.Count > 0)
|
||||
{
|
||||
emitter.Emit(new Scalar(nameof(SerializedClass.Private)));
|
||||
serializer(privates);
|
||||
}
|
||||
|
||||
emitter.Emit(new MappingEnd());
|
||||
}
|
||||
}
|
@@ -1,49 +0,0 @@
|
||||
using System;
|
||||
|
||||
using YamlDotNet.Core;
|
||||
using YamlDotNet.Core.Events;
|
||||
using YamlDotNet.Serialization;
|
||||
|
||||
namespace Syntriax.Engine.Core.Serialization;
|
||||
|
||||
public class StateEnableSerializer : EngineTypeYamlSerializerBase<IStateEnable>
|
||||
{
|
||||
public override IStateEnable? Read(IParser parser, Type type, ObjectDeserializer rootDeserializer)
|
||||
{
|
||||
bool enabled;
|
||||
|
||||
IStateEnable stateEnable;
|
||||
|
||||
parser.Consume<MappingStart>();
|
||||
|
||||
if (parser.Consume<Scalar>().Value.CompareTo(nameof(IStateEnable.Enabled)) != 0)
|
||||
throw new();
|
||||
enabled = bool.Parse(parser.Consume<Scalar>().Value);
|
||||
|
||||
if (parser.Consume<Scalar>().Value.CompareTo(SERIALIZED_SCALAR_NAME) != 0)
|
||||
throw new();
|
||||
SerializedClass instanceSerializedClass = (SerializedClass)rootDeserializer(typeof(SerializedClass))!;
|
||||
stateEnable = (IStateEnable)instanceSerializedClass.CreateInstance(EntityRegistry);
|
||||
|
||||
parser.Consume<MappingEnd>();
|
||||
|
||||
stateEnable.Enabled = enabled;
|
||||
|
||||
return stateEnable;
|
||||
}
|
||||
|
||||
public override void WriteYaml(IEmitter emitter, object? value, Type type, ObjectSerializer serializer)
|
||||
{
|
||||
IStateEnable stateEnable = (IStateEnable)value!;
|
||||
|
||||
emitter.Emit(new MappingStart());
|
||||
|
||||
emitter.Emit(new Scalar(nameof(IStateEnable.Enabled)));
|
||||
emitter.Emit(new Scalar(stateEnable.Enabled.ToString()));
|
||||
|
||||
emitter.Emit(new Scalar(SERIALIZED_SCALAR_NAME));
|
||||
serializer(new SerializedClass(stateEnable));
|
||||
|
||||
emitter.Emit(new MappingEnd());
|
||||
}
|
||||
}
|
@@ -1,45 +0,0 @@
|
||||
using System;
|
||||
|
||||
using Syntriax.Engine.Core.Factory;
|
||||
|
||||
using YamlDotNet.Core;
|
||||
using YamlDotNet.Core.Events;
|
||||
using YamlDotNet.Serialization;
|
||||
|
||||
namespace Syntriax.Engine.Core.Serialization;
|
||||
|
||||
public class TypeContainerSerializer : EngineTypeYamlSerializerBase<TypeContainer>
|
||||
{
|
||||
public override TypeContainer Read(IParser parser, Type type, ObjectDeserializer rootDeserializer)
|
||||
{
|
||||
parser.Consume<MappingStart>();
|
||||
|
||||
if (parser.Consume<Scalar>().Value.CompareTo(nameof(TypeContainer.Type)) != 0)
|
||||
throw new ArgumentException($"{nameof(TypeContainer)} mapping must start with {nameof(TypeContainer.Type)}");
|
||||
string typeFullName = parser.Consume<Scalar>().Value;
|
||||
|
||||
if (parser.Consume<Scalar>().Value.CompareTo(nameof(TypeContainer.Value)) != 0)
|
||||
throw new ArgumentException($"{nameof(TypeContainer)} mapping must end with {nameof(TypeContainer.Type)}");
|
||||
|
||||
object? value = rootDeserializer(TypeFactory.GetType(typeFullName));
|
||||
|
||||
parser.Consume<MappingEnd>();
|
||||
|
||||
return new TypeContainer() { Type = typeFullName, Value = value };
|
||||
}
|
||||
|
||||
public override void WriteYaml(IEmitter emitter, object? value, Type type, ObjectSerializer serializer)
|
||||
{
|
||||
TypeContainer? typeContainer = (TypeContainer)value!;
|
||||
|
||||
emitter.Emit(new MappingStart());
|
||||
|
||||
emitter.Emit(new Scalar(nameof(TypeContainer.Type)));
|
||||
emitter.Emit(new Scalar(typeContainer.Type));
|
||||
|
||||
emitter.Emit(new Scalar(nameof(TypeContainer.Value)));
|
||||
serializer(typeContainer.Value, TypeFactory.GetType(typeContainer.Type));
|
||||
|
||||
emitter.Emit(new MappingEnd());
|
||||
}
|
||||
}
|
@@ -1,93 +0,0 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
|
||||
using YamlDotNet.Core;
|
||||
using YamlDotNet.Core.Events;
|
||||
using YamlDotNet.Serialization;
|
||||
|
||||
namespace Syntriax.Engine.Core.Serialization;
|
||||
|
||||
public class UniverseObjectSerializer : EngineTypeYamlSerializerBase<IUniverseObject>
|
||||
{
|
||||
public override IUniverseObject? Read(IParser parser, Type type, ObjectDeserializer rootDeserializer)
|
||||
{
|
||||
string name;
|
||||
string id;
|
||||
|
||||
IUniverseObject universeObject;
|
||||
IStateEnable stateEnable;
|
||||
IBehaviourController behaviourController;
|
||||
List<IUniverseObject> children;
|
||||
|
||||
parser.Consume<MappingStart>();
|
||||
|
||||
if (parser.Consume<Scalar>().Value.CompareTo(nameof(IUniverseObject.Name)) != 0)
|
||||
throw new();
|
||||
name = parser.Consume<Scalar>().Value;
|
||||
|
||||
if (parser.Consume<Scalar>().Value.CompareTo(nameof(IUniverseObject.Id)) != 0)
|
||||
throw new();
|
||||
id = parser.Consume<Scalar>().Value;
|
||||
|
||||
if (parser.Consume<Scalar>().Value.CompareTo(SERIALIZED_SCALAR_NAME) != 0)
|
||||
throw new();
|
||||
SerializedClass instanceSerializedClass = (SerializedClass)rootDeserializer(typeof(SerializedClass))!;
|
||||
universeObject = (IUniverseObject)instanceSerializedClass.CreateInstance(EntityRegistry);
|
||||
|
||||
if (parser.Consume<Scalar>().Value.CompareTo(nameof(IUniverseObject.StateEnable)) != 0)
|
||||
throw new();
|
||||
stateEnable = (IStateEnable)rootDeserializer(typeof(IStateEnable))!;
|
||||
|
||||
if (parser.Consume<Scalar>().Value.CompareTo(nameof(IUniverseObject.BehaviourController)) != 0)
|
||||
throw new();
|
||||
behaviourController = (IBehaviourController)rootDeserializer(typeof(IBehaviourController))!;
|
||||
|
||||
if (parser.Consume<Scalar>().Value.CompareTo(nameof(IUniverseObject.Children)) != 0)
|
||||
throw new();
|
||||
children = (List<IUniverseObject>)rootDeserializer(typeof(List<IUniverseObject>))!;
|
||||
|
||||
parser.Consume<MappingEnd>();
|
||||
|
||||
universeObject.Id = id;
|
||||
universeObject.Name = name;
|
||||
|
||||
stateEnable.Assign(universeObject);
|
||||
universeObject.Assign(stateEnable);
|
||||
|
||||
behaviourController.Assign(universeObject);
|
||||
universeObject.Assign(behaviourController);
|
||||
|
||||
foreach (IUniverseObject child in children)
|
||||
universeObject.AddChild(child);
|
||||
|
||||
return universeObject;
|
||||
}
|
||||
|
||||
public override void WriteYaml(IEmitter emitter, object? value, Type type, ObjectSerializer serializer)
|
||||
{
|
||||
IUniverseObject universeObject = (IUniverseObject)value!;
|
||||
|
||||
emitter.Emit(new MappingStart());
|
||||
|
||||
emitter.Emit(new Scalar(nameof(IUniverseObject.Name)));
|
||||
emitter.Emit(new Scalar(universeObject.Name));
|
||||
|
||||
emitter.Emit(new Scalar(nameof(IUniverseObject.Id)));
|
||||
emitter.Emit(new Scalar(universeObject.Id));
|
||||
|
||||
emitter.Emit(new Scalar(SERIALIZED_SCALAR_NAME));
|
||||
serializer(new SerializedClass(universeObject));
|
||||
|
||||
emitter.Emit(new Scalar(nameof(IUniverseObject.StateEnable)));
|
||||
serializer(universeObject.StateEnable);
|
||||
|
||||
emitter.Emit(new Scalar(nameof(IUniverseObject.BehaviourController)));
|
||||
serializer(universeObject.BehaviourController);
|
||||
|
||||
emitter.Emit(new Scalar(nameof(IUniverseObject.Children)));
|
||||
serializer(universeObject.Children.Where(c => !c.GetType().HasAttribute<IgnoreSerializationAttribute>()));
|
||||
|
||||
emitter.Emit(new MappingEnd());
|
||||
}
|
||||
}
|
@@ -1,84 +0,0 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
|
||||
using YamlDotNet.Core;
|
||||
using YamlDotNet.Core.Events;
|
||||
using YamlDotNet.Serialization;
|
||||
|
||||
namespace Syntriax.Engine.Core.Serialization;
|
||||
|
||||
public class UniverseSerializer : EngineTypeYamlSerializerBase<IUniverse>
|
||||
{
|
||||
public override IUniverse? Read(IParser parser, Type type, ObjectDeserializer rootDeserializer)
|
||||
{
|
||||
string id;
|
||||
|
||||
IUniverse universe;
|
||||
IStateEnable stateEnable;
|
||||
List<IUniverseObject> universeObjects;
|
||||
|
||||
parser.Consume<MappingStart>();
|
||||
|
||||
if (parser.Consume<Scalar>().Value.CompareTo(nameof(IUniverse.Id)) != 0)
|
||||
throw new();
|
||||
id = parser.Consume<Scalar>().Value;
|
||||
|
||||
if (parser.Consume<Scalar>().Value.CompareTo(SERIALIZED_SCALAR_NAME) != 0)
|
||||
throw new();
|
||||
SerializedClass instanceSerializedClass = (SerializedClass)rootDeserializer(typeof(SerializedClass))!;
|
||||
universe = (IUniverse)instanceSerializedClass.CreateInstance(EntityRegistry);
|
||||
|
||||
if (parser.Consume<Scalar>().Value.CompareTo(nameof(IUniverse.StateEnable)) != 0)
|
||||
throw new();
|
||||
stateEnable = (IStateEnable)rootDeserializer(typeof(IStateEnable))!;
|
||||
|
||||
if (parser.Consume<Scalar>().Value.CompareTo(nameof(IUniverse.UniverseObjects)) != 0)
|
||||
throw new();
|
||||
universeObjects = (List<IUniverseObject>)rootDeserializer(typeof(List<IUniverseObject>))!;
|
||||
|
||||
parser.Consume<MappingEnd>();
|
||||
|
||||
universe.Id = id;
|
||||
|
||||
stateEnable.Assign(universe);
|
||||
universe.Assign(stateEnable);
|
||||
|
||||
foreach (IUniverseObject uo in universeObjects)
|
||||
universe.Register(uo);
|
||||
|
||||
return universe;
|
||||
}
|
||||
|
||||
public override void WriteYaml(IEmitter emitter, object? value, Type type, ObjectSerializer serializer)
|
||||
{
|
||||
IUniverse universe = (IUniverse)value!;
|
||||
|
||||
IEnumerable<IUniverseObject> rootUniverseObjects = universe.UniverseObjects
|
||||
.Select(uo =>
|
||||
{
|
||||
IUniverseObject root = uo;
|
||||
while (root.Parent is IUniverseObject parent)
|
||||
root = parent;
|
||||
return root;
|
||||
})
|
||||
.Where(uo => !uo.GetType().HasAttribute<IgnoreSerializationAttribute>())
|
||||
.Distinct();
|
||||
|
||||
emitter.Emit(new MappingStart());
|
||||
|
||||
emitter.Emit(new Scalar(nameof(IUniverse.Id)));
|
||||
emitter.Emit(new Scalar(universe.Id));
|
||||
|
||||
emitter.Emit(new Scalar(SERIALIZED_SCALAR_NAME));
|
||||
serializer(new SerializedClass(universe));
|
||||
|
||||
emitter.Emit(new Scalar(nameof(IUniverse.StateEnable)));
|
||||
serializer(universe.StateEnable);
|
||||
|
||||
emitter.Emit(new Scalar(nameof(IUniverse.UniverseObjects)));
|
||||
serializer(rootUniverseObjects);
|
||||
|
||||
emitter.Emit(new MappingEnd());
|
||||
}
|
||||
}
|
@@ -5,12 +5,12 @@ using System.Reflection;
|
||||
|
||||
namespace Syntriax.Engine.Core.Serialization;
|
||||
|
||||
internal static class Utils
|
||||
public static class Utils
|
||||
{
|
||||
internal static bool HasAttribute<T>(this MemberInfo memberInfo) where T : Attribute => memberInfo.GetCustomAttribute<T>() is not null;
|
||||
internal static bool IsEnumerable(this Type type) => typeof(System.Collections.IEnumerable).IsAssignableFrom(type) && type != typeof(string);
|
||||
public static bool HasAttribute<T>(this MemberInfo memberInfo) where T : Attribute => memberInfo.GetCustomAttribute<T>() is not null;
|
||||
public static bool IsEnumerable(this Type type) => typeof(System.Collections.IEnumerable).IsAssignableFrom(type) && type != typeof(string);
|
||||
|
||||
internal static TypeData GetTypeData(this Type objectType)
|
||||
public static TypeData GetTypeData(this Type objectType)
|
||||
{
|
||||
List<EventInfo> eventInfos = objectType.GetEvents(BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public)
|
||||
.OrderBy(ei => ei.Name)
|
||||
@@ -26,7 +26,7 @@ internal static class Utils
|
||||
return new TypeData(fieldInfos, propertyInfos);
|
||||
}
|
||||
|
||||
internal static List<FieldInfo> GetFieldInfosIncludingBaseClasses(Type type, BindingFlags bindingFlags)
|
||||
public static List<FieldInfo> GetFieldInfosIncludingBaseClasses(Type type, BindingFlags bindingFlags)
|
||||
{
|
||||
if (type.BaseType is null)
|
||||
return [.. type.GetFields(bindingFlags)];
|
||||
@@ -44,7 +44,7 @@ internal static class Utils
|
||||
return [.. fieldInfoList.OrderBy(fi => fi.Name)];
|
||||
}
|
||||
|
||||
internal static List<PropertyInfo> GetPropertyInfosIncludingBaseClasses(Type type, BindingFlags bindingFlags)
|
||||
public static List<PropertyInfo> GetPropertyInfosIncludingBaseClasses(Type type, BindingFlags bindingFlags)
|
||||
{
|
||||
if (type.BaseType is null)
|
||||
return [.. type.GetProperties(bindingFlags)];
|
||||
@@ -75,7 +75,7 @@ internal static class Utils
|
||||
}
|
||||
}
|
||||
|
||||
internal record struct TypeData(IEnumerable<FieldInfo> Fields, IEnumerable<PropertyInfo> Properties)
|
||||
public record struct TypeData(IEnumerable<FieldInfo> Fields, IEnumerable<PropertyInfo> Properties)
|
||||
{
|
||||
public static implicit operator (IEnumerable<FieldInfo> fields, IEnumerable<PropertyInfo> properties)(TypeData value) => (value.Fields, value.Properties);
|
||||
public static implicit operator TypeData((IEnumerable<FieldInfo> fields, IEnumerable<PropertyInfo> properties) value) => new(value.fields, value.properties);
|
||||
|
Reference in New Issue
Block a user