42 lines
1.4 KiB
C#
42 lines
1.4 KiB
C#
using System;
|
|
|
|
using Engine.Core;
|
|
|
|
using YamlDotNet.Core;
|
|
using YamlDotNet.Core.Events;
|
|
using YamlDotNet.Serialization;
|
|
|
|
namespace Engine.Serializers.Yaml;
|
|
|
|
public class Ray3DConverter : EngineTypeYamlSerializerBase<Ray3D>
|
|
{
|
|
public override Ray3D Read(IParser parser, Type type, ObjectDeserializer rootDeserializer)
|
|
{
|
|
parser.Consume<MappingStart>();
|
|
|
|
if (parser.Consume<Scalar>().Value.CompareTo(nameof(Ray3D.Origin)) != 0)
|
|
throw new ArgumentException($"{nameof(Ray3D)} mapping must start with {nameof(Ray3D.Origin)}");
|
|
Vector3D origin = (Vector3D)rootDeserializer(typeof(Vector3D))!;
|
|
|
|
if (parser.Consume<Scalar>().Value.CompareTo(nameof(Ray3D.Direction)) != 0)
|
|
throw new ArgumentException($"{nameof(Ray3D)} mapping must end with {nameof(Ray3D.Direction)}");
|
|
Vector3D direction = (Vector3D)rootDeserializer(typeof(Vector3D))!;
|
|
|
|
parser.Consume<MappingEnd>();
|
|
|
|
return new Ray3D(origin, direction);
|
|
}
|
|
|
|
public override void WriteYaml(IEmitter emitter, object? value, Type type, ObjectSerializer serializer)
|
|
{
|
|
Ray3D ray3D = (Ray3D)value!;
|
|
|
|
emitter.Emit(new MappingStart());
|
|
emitter.Emit(new Scalar(nameof(ray3D.Origin)));
|
|
serializer(ray3D.Origin, typeof(Vector3D));
|
|
emitter.Emit(new Scalar(nameof(ray3D.Direction)));
|
|
serializer(ray3D.Direction, typeof(Vector3D));
|
|
emitter.Emit(new MappingEnd());
|
|
}
|
|
}
|