Syntriax.Engine/Engine.Serialization/BehaviourDTOListConverter.cs

42 lines
1.3 KiB
C#
Raw Normal View History

2024-02-10 17:10:02 +03:00
using System;
using System.Collections.Generic;
using Engine.Serialization.DTOs;
using YamlDotNet.Core;
using YamlDotNet.Core.Events;
using YamlDotNet.Serialization;
namespace Engine.Serialization;
public class BehaviourDTOListConverter : IYamlTypeConverter
{
public bool Accepts(Type type)
{
return type == typeof(List<BehaviourDTO>);
}
public object ReadYaml(IParser parser, Type type)
{
var behaviours = new List<BehaviourDTO>();
if (parser.Current is SequenceStart)
{
parser.MoveNext();
while (parser.Current != null && !(parser.Current is SequenceEnd))
{
behaviours.Add((BehaviourDTO)new BehaviourYamlConverter().ReadYaml(parser, typeof(BehaviourDTO)));
parser.MoveNext();
}
}
return behaviours;
}
public void WriteYaml(IEmitter emitter, object? value, Type type)
{
var behaviours = (List<BehaviourDTO>)(value ?? throw new Exception());
emitter.Emit(new SequenceStart(null, null, false, SequenceStyle.Block));
foreach (var behaviour in behaviours)
new BehaviourYamlConverter().WriteYaml(emitter, behaviour, typeof(BehaviourDTO));
emitter.Emit(new SequenceEnd());
}
}