29 lines
851 B
C#
29 lines
851 B
C#
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));
|
|
}
|
|
}
|