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 Ray2DConverter : EngineTypeYamlSerializerBase<Ray2D>
 | 
						|
{
 | 
						|
    public override Ray2D Read(IParser parser, Type type, ObjectDeserializer rootDeserializer)
 | 
						|
    {
 | 
						|
        parser.Consume<MappingStart>();
 | 
						|
 | 
						|
        if (parser.Consume<Scalar>().Value.CompareTo(nameof(Ray2D.Origin)) != 0)
 | 
						|
            throw new ArgumentException($"{nameof(Ray2D)} mapping must start with {nameof(Ray2D.Origin)}");
 | 
						|
        Vector2D origin = (Vector2D)rootDeserializer(typeof(Vector2D))!;
 | 
						|
 | 
						|
        if (parser.Consume<Scalar>().Value.CompareTo(nameof(Ray2D.Direction)) != 0)
 | 
						|
            throw new ArgumentException($"{nameof(Ray2D)} mapping must end with {nameof(Ray2D.Direction)}");
 | 
						|
        Vector2D direction = (Vector2D)rootDeserializer(typeof(Vector2D))!;
 | 
						|
 | 
						|
        parser.Consume<MappingEnd>();
 | 
						|
 | 
						|
        return new Ray2D(origin, direction);
 | 
						|
    }
 | 
						|
 | 
						|
    public override void WriteYaml(IEmitter emitter, object? value, Type type, ObjectSerializer serializer)
 | 
						|
    {
 | 
						|
        Ray2D ray2D = (Ray2D)value!;
 | 
						|
 | 
						|
        emitter.Emit(new MappingStart());
 | 
						|
        emitter.Emit(new Scalar(nameof(ray2D.Origin)));
 | 
						|
        serializer(ray2D.Origin, typeof(Vector2D));
 | 
						|
        emitter.Emit(new Scalar(nameof(ray2D.Direction)));
 | 
						|
        serializer(ray2D.Direction, typeof(Vector2D));
 | 
						|
        emitter.Emit(new MappingEnd());
 | 
						|
    }
 | 
						|
}
 |