2024-02-09 17:50:39 +03:00
|
|
|
using System;
|
|
|
|
using System.Linq;
|
|
|
|
|
|
|
|
using Engine.Serialization.DTOs;
|
|
|
|
|
|
|
|
using YamlDotNet.Core;
|
|
|
|
using YamlDotNet.Core.Events;
|
|
|
|
using YamlDotNet.Serialization;
|
|
|
|
|
|
|
|
namespace Engine.Serialization;
|
|
|
|
|
|
|
|
internal class StateEnableYamlConverter : IYamlTypeConverter
|
|
|
|
{
|
|
|
|
public bool Accepts(Type type) => type == typeof(StateEnableDTO);
|
|
|
|
|
|
|
|
public object ReadYaml(IParser parser, Type type)
|
|
|
|
{
|
|
|
|
if (parser.Current is not MappingStart)
|
|
|
|
throw new InvalidOperationException("Expected MappingStart");
|
|
|
|
|
|
|
|
parser.MoveNext();
|
|
|
|
var stateEnable = new StateEnableDTO();
|
|
|
|
while (parser.Current != null && parser.Current is not MappingEnd)
|
|
|
|
{
|
|
|
|
var propertyName = ((Scalar)parser.Current).Value;
|
|
|
|
parser.MoveNext();
|
|
|
|
switch (propertyName)
|
|
|
|
{
|
2024-02-10 17:10:02 +03:00
|
|
|
case nameof(StateEnableDTO.ClassType):
|
|
|
|
stateEnable.ClassType = ((Scalar)parser.Current).Value;
|
2024-02-09 17:50:39 +03:00
|
|
|
break;
|
|
|
|
case nameof(StateEnableDTO.Enabled):
|
|
|
|
stateEnable.Enabled = bool.Parse(((Scalar)parser.Current).Value);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
parser.MoveNext();
|
|
|
|
}
|
|
|
|
return stateEnable;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void WriteYaml(IEmitter emitter, object? value, Type type)
|
|
|
|
{
|
|
|
|
var stateEnable = (StateEnableDTO)(value ?? throw new Exception());
|
|
|
|
|
|
|
|
emitter.Emit(new MappingStart());
|
2024-02-10 17:10:02 +03:00
|
|
|
emitter.Emit(new Scalar(nameof(StateEnableDTO.ClassType)));
|
|
|
|
emitter.Emit(new Scalar(stateEnable.ClassType));
|
2024-02-09 17:50:39 +03:00
|
|
|
emitter.Emit(new Scalar(nameof(StateEnableDTO.Enabled)));
|
|
|
|
emitter.Emit(new Scalar(stateEnable.Enabled.ToString()));
|
|
|
|
emitter.Emit(new MappingEnd());
|
|
|
|
}
|
|
|
|
}
|