Syntriax.Engine/Engine.Serialization/GameObjectDTOListConverter.cs

42 lines
1.3 KiB
C#
Raw Normal View History

2024-02-10 20:02:38 +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 GameObjectDTOListConverter : IYamlTypeConverter
{
public bool Accepts(Type type)
{
return type == typeof(List<GameObjectDTO>);
}
public object ReadYaml(IParser parser, Type type)
{
var gameObjects = new List<GameObjectDTO>();
if (parser.Current is SequenceStart)
{
parser.MoveNext();
while (parser.Current != null && !(parser.Current is SequenceEnd))
{
gameObjects.Add((GameObjectDTO)new GameObjectYamlConverter().ReadYaml(parser, typeof(GameObjectDTO)));
parser.MoveNext();
}
}
return gameObjects;
}
public void WriteYaml(IEmitter emitter, object? value, Type type)
{
var gameObjects = (List<GameObjectDTO>)(value ?? throw new Exception());
emitter.Emit(new SequenceStart(null, null, false, SequenceStyle.Block));
foreach (var gameObject in gameObjects)
new GameObjectYamlConverter().WriteYaml(emitter, gameObject, typeof(BehaviourDTO));
emitter.Emit(new SequenceEnd());
}
}