refactor: Yaml serialization moved from Core to own project
This commit is contained in:
@@ -0,0 +1,52 @@
|
||||
using System;
|
||||
|
||||
using Syntriax.Engine.Core;
|
||||
using Syntriax.Engine.Core.Serialization;
|
||||
|
||||
using YamlDotNet.Core;
|
||||
using YamlDotNet.Core.Events;
|
||||
using YamlDotNet.Serialization;
|
||||
|
||||
namespace Syntriax.Engine.Serializers.Yaml;
|
||||
|
||||
public class StateEnableConverter : 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());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user