feat: added entity converter
This commit is contained in:
parent
5bcc256777
commit
5fa7420c04
28
Engine.Serialization/Converters/EntityConverter.cs
Normal file
28
Engine.Serialization/Converters/EntityConverter.cs
Normal file
@ -0,0 +1,28 @@
|
||||
using Syntriax.Engine.Core;
|
||||
using Syntriax.Engine.Core.Factory;
|
||||
|
||||
using YamlDotNet.Core;
|
||||
using YamlDotNet.Serialization;
|
||||
|
||||
namespace Syntriax.Engine.Serialization;
|
||||
|
||||
public class EntityConverter : IEngineTypeYamlConverter
|
||||
{
|
||||
public bool Accepts(Type type) => typeof(IEntity).IsAssignableFrom(type);
|
||||
|
||||
public object? ReadYaml(IParser parser, Type type, ObjectDeserializer rootDeserializer)
|
||||
{
|
||||
EntityReference entityReference = (EntityReference)rootDeserializer(typeof(EntityReference))!;
|
||||
|
||||
IEntity entity = (IEntity)TypeFactory.Get(entityReference.TypeContainer.Type);
|
||||
|
||||
return entity;
|
||||
}
|
||||
|
||||
public void WriteYaml(IEmitter emitter, object? value, Type type, ObjectSerializer serializer)
|
||||
{
|
||||
IEntity? entity = (IEntity)value!;
|
||||
|
||||
serializer(new EntityReference(entity), typeof(EntityReference));
|
||||
}
|
||||
}
|
12
Engine.Serialization/Converters/EntityReference.cs
Normal file
12
Engine.Serialization/Converters/EntityReference.cs
Normal file
@ -0,0 +1,12 @@
|
||||
using Syntriax.Engine.Core;
|
||||
|
||||
namespace Syntriax.Engine.Serialization;
|
||||
|
||||
public class EntityReference
|
||||
{
|
||||
public string Id { get; set; } = string.Empty;
|
||||
public TypeContainer TypeContainer { get; set; } = new();
|
||||
|
||||
public EntityReference() { }
|
||||
public EntityReference(IEntity entity) { TypeContainer = new(entity); Id = entity.Id; }
|
||||
}
|
36
Engine.Serialization/Converters/EntityReferenceConverter.cs
Normal file
36
Engine.Serialization/Converters/EntityReferenceConverter.cs
Normal file
@ -0,0 +1,36 @@
|
||||
using YamlDotNet.Core;
|
||||
using YamlDotNet.Core.Events;
|
||||
using YamlDotNet.Serialization;
|
||||
|
||||
namespace Syntriax.Engine.Serialization;
|
||||
|
||||
public class EntityReferenceConverter : IEngineTypeYamlConverter
|
||||
{
|
||||
public bool Accepts(Type type) => type == typeof(EntityReference);
|
||||
|
||||
public object? ReadYaml(IParser parser, Type type, ObjectDeserializer rootDeserializer)
|
||||
{
|
||||
parser.Consume<MappingStart>();
|
||||
|
||||
TypeContainer typeContainer = (TypeContainer)rootDeserializer(typeof(TypeContainer))!;
|
||||
|
||||
if (parser.Consume<Scalar>().Value.CompareTo(nameof(EntityReference.Id)) != 0)
|
||||
throw new ArgumentException($"{nameof(EntityReference)} mapping must have an {nameof(EntityReference.Id)}");
|
||||
string id = parser.Consume<Scalar>().Value;
|
||||
|
||||
parser.Consume<MappingEnd>();
|
||||
|
||||
return new EntityReference() { Id = id, TypeContainer = typeContainer };
|
||||
}
|
||||
|
||||
public void WriteYaml(IEmitter emitter, object? value, Type type, ObjectSerializer serializer)
|
||||
{
|
||||
EntityReference? entityReference = (EntityReference)value!;
|
||||
|
||||
emitter.Emit(new MappingStart());
|
||||
serializer(entityReference.TypeContainer, typeof(TypeContainer));
|
||||
emitter.Emit(new Scalar(nameof(EntityReference.Id)));
|
||||
emitter.Emit(new Scalar(entityReference.Id));
|
||||
emitter.Emit(new MappingEnd());
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user