BREAKING CHANGE: moved yaml serialization from Engine.Serialization to Engine.Integration

This commit is contained in:
2025-08-05 19:48:49 +03:00
parent 3d183b21cd
commit 65dcb0c564
29 changed files with 8 additions and 11 deletions

View File

@@ -0,0 +1,41 @@
using System;
using Engine.Core;
using YamlDotNet.Core;
using YamlDotNet.Core.Events;
using YamlDotNet.Serialization;
namespace Engine.Serializers.Yaml;
public class AABBConverter : EngineTypeYamlSerializerBase<AABB>
{
public override AABB Read(IParser parser, Type type, ObjectDeserializer rootDeserializer)
{
parser.Consume<MappingStart>();
if (parser.Consume<Scalar>().Value.CompareTo(nameof(AABB.LowerBoundary)) != 0)
throw new ArgumentException($"{nameof(AABB)} mapping must start with {nameof(AABB.LowerBoundary)}");
Vector2D lowerBoundary = (Vector2D)rootDeserializer(typeof(Vector2D))!;
if (parser.Consume<Scalar>().Value.CompareTo(nameof(AABB.UpperBoundary)) != 0)
throw new ArgumentException($"{nameof(AABB)} mapping must end with {nameof(AABB.UpperBoundary)}");
Vector2D upperBoundary = (Vector2D)rootDeserializer(typeof(Vector2D))!;
parser.Consume<MappingEnd>();
return new AABB(lowerBoundary, upperBoundary);
}
public override void WriteYaml(IEmitter emitter, object? value, Type type, ObjectSerializer serializer)
{
AABB aabb = (AABB)value!;
emitter.Emit(new MappingStart());
emitter.Emit(new Scalar(nameof(AABB.LowerBoundary)));
serializer(aabb.LowerBoundary, typeof(Vector2D));
emitter.Emit(new Scalar(nameof(AABB.UpperBoundary)));
serializer(aabb.UpperBoundary, typeof(Vector2D));
emitter.Emit(new MappingEnd());
}
}

View File

@@ -0,0 +1,41 @@
using System;
using Engine.Core;
using YamlDotNet.Core;
using YamlDotNet.Core.Events;
using YamlDotNet.Serialization;
namespace Engine.Serializers.Yaml;
public class CircleConverter : EngineTypeYamlSerializerBase<Circle>
{
public override Circle Read(IParser parser, Type type, ObjectDeserializer rootDeserializer)
{
parser.Consume<MappingStart>();
if (parser.Consume<Scalar>().Value.CompareTo(nameof(Circle.Center)) != 0)
throw new ArgumentException($"{nameof(Circle)} mapping must start with {nameof(Circle.Center)}");
Vector2D lowerBoundary = (Vector2D)rootDeserializer(typeof(Vector2D))!;
if (parser.Consume<Scalar>().Value.CompareTo(nameof(Circle.Radius)) != 0)
throw new ArgumentException($"{nameof(Circle)} mapping must end with {nameof(Circle.Radius)}");
float radius = (float)rootDeserializer(typeof(float))!;
parser.Consume<MappingEnd>();
return new Circle(lowerBoundary, radius);
}
public override void WriteYaml(IEmitter emitter, object? value, Type type, ObjectSerializer serializer)
{
Circle circle = (Circle)value!;
emitter.Emit(new MappingStart());
emitter.Emit(new Scalar(nameof(Circle.Center)));
serializer(circle.Center, typeof(Vector2D));
emitter.Emit(new Scalar(nameof(Circle.Radius)));
serializer(circle.Radius, typeof(float));
emitter.Emit(new MappingEnd());
}
}

View File

@@ -0,0 +1,28 @@
using System;
using Engine.Core;
using YamlDotNet.Core;
using YamlDotNet.Core.Events;
using YamlDotNet.Serialization;
namespace Engine.Serializers.Yaml;
public class ColorHSVConverter : EngineTypeYamlSerializerBase<ColorHSV>
{
private static readonly int SUBSTRING_START_LENGTH = nameof(ColorHSV).Length + 1;
public override ColorHSV Read(IParser parser, Type type, ObjectDeserializer rootDeserializer)
{
string value = parser.Consume<Scalar>().Value;
string insideParenthesis = value[SUBSTRING_START_LENGTH..^1];
string[] values = insideParenthesis.Split(", ");
return new ColorHSV(float.Parse(values[0]), float.Parse(values[1]), float.Parse(values[2]));
}
public override void WriteYaml(IEmitter emitter, object? value, Type type, ObjectSerializer serializer)
{
ColorHSV hsv = (ColorHSV)value!;
emitter.Emit(new Scalar($"{nameof(ColorHSV)}({hsv.Hue}, {hsv.Saturation}, {hsv.Value})"));
}
}

View File

@@ -0,0 +1,28 @@
using System;
using Engine.Core;
using YamlDotNet.Core;
using YamlDotNet.Core.Events;
using YamlDotNet.Serialization;
namespace Engine.Serializers.Yaml;
public class ColorRGBAConverter : EngineTypeYamlSerializerBase<ColorRGBA>
{
private static readonly int SUBSTRING_START_LENGTH = nameof(ColorRGBA).Length + 1;
public override ColorRGBA Read(IParser parser, Type type, ObjectDeserializer rootDeserializer)
{
string value = parser.Consume<Scalar>().Value;
string insideParenthesis = value[SUBSTRING_START_LENGTH..^1];
string[] values = insideParenthesis.Split(", ");
return new ColorRGBA(byte.Parse(values[0]), byte.Parse(values[1]), byte.Parse(values[2]), byte.Parse(values[3]));
}
public override void WriteYaml(IEmitter emitter, object? value, Type type, ObjectSerializer serializer)
{
ColorRGBA rgb = (ColorRGBA)value!;
emitter.Emit(new Scalar($"{nameof(ColorRGBA)}({rgb.R}, {rgb.G}, {rgb.B}, {rgb.A})"));
}
}

View File

@@ -0,0 +1,28 @@
using System;
using Engine.Core;
using YamlDotNet.Core;
using YamlDotNet.Core.Events;
using YamlDotNet.Serialization;
namespace Engine.Serializers.Yaml;
public class ColorRGBConverter : EngineTypeYamlSerializerBase<ColorRGB>
{
private static readonly int SUBSTRING_START_LENGTH = nameof(ColorRGB).Length + 1;
public override ColorRGB Read(IParser parser, Type type, ObjectDeserializer rootDeserializer)
{
string value = parser.Consume<Scalar>().Value;
string insideParenthesis = value[SUBSTRING_START_LENGTH..^1];
string[] values = insideParenthesis.Split(", ");
return new ColorRGB(byte.Parse(values[0]), byte.Parse(values[1]), byte.Parse(values[2]));
}
public override void WriteYaml(IEmitter emitter, object? value, Type type, ObjectSerializer serializer)
{
ColorRGB rgb = (ColorRGB)value!;
emitter.Emit(new Scalar($"{nameof(ColorRGB)}({rgb.R}, {rgb.G}, {rgb.B})"));
}
}

View File

@@ -0,0 +1,41 @@
using System;
using Engine.Core;
using YamlDotNet.Core;
using YamlDotNet.Core.Events;
using YamlDotNet.Serialization;
namespace Engine.Serializers.Yaml;
public class Line2DConverter : EngineTypeYamlSerializerBase<Line2D>
{
public override Line2D Read(IParser parser, Type type, ObjectDeserializer rootDeserializer)
{
parser.Consume<MappingStart>();
if (parser.Consume<Scalar>().Value.CompareTo(nameof(Line2D.From)) != 0)
throw new ArgumentException($"{nameof(Line2D)} mapping must start with {nameof(Line2D.From)}");
Vector2D from = (Vector2D)rootDeserializer(typeof(Vector2D))!;
if (parser.Consume<Scalar>().Value.CompareTo(nameof(Line2D.To)) != 0)
throw new ArgumentException($"{nameof(Line2D)} mapping must end with {nameof(Line2D.To)}");
Vector2D to = (Vector2D)rootDeserializer(typeof(Vector2D))!;
parser.Consume<MappingEnd>();
return new Line2D(from, to);
}
public override void WriteYaml(IEmitter emitter, object? value, Type type, ObjectSerializer serializer)
{
Line2D line2D = (Line2D)value!;
emitter.Emit(new MappingStart());
emitter.Emit(new Scalar(nameof(Line2D.From)));
serializer(line2D.From, typeof(Vector2D));
emitter.Emit(new Scalar(nameof(Line2D.To)));
serializer(line2D.To, typeof(Vector2D));
emitter.Emit(new MappingEnd());
}
}

View File

@@ -0,0 +1,41 @@
using System;
using Engine.Core;
using YamlDotNet.Core;
using YamlDotNet.Core.Events;
using YamlDotNet.Serialization;
namespace Engine.Serializers.Yaml;
public class Line2DEquationConverter : EngineTypeYamlSerializerBase<Line2DEquation>
{
public override Line2DEquation Read(IParser parser, Type type, ObjectDeserializer rootDeserializer)
{
parser.Consume<MappingStart>();
if (parser.Consume<Scalar>().Value.CompareTo(nameof(Line2DEquation.Slope)) != 0)
throw new ArgumentException($"{nameof(Line2DEquation)} mapping must start with {nameof(Line2DEquation.Slope)}");
float slope = (float)rootDeserializer(typeof(float))!;
if (parser.Consume<Scalar>().Value.CompareTo(nameof(Line2DEquation.OffsetY)) != 0)
throw new ArgumentException($"{nameof(Line2DEquation)} mapping must end with {nameof(Line2DEquation.OffsetY)}");
float offset = (float)rootDeserializer(typeof(float))!;
parser.Consume<MappingEnd>();
return new Line2DEquation(slope, offset);
}
public override void WriteYaml(IEmitter emitter, object? value, Type type, ObjectSerializer serializer)
{
Line2DEquation line2DEquation = (Line2DEquation)value!;
emitter.Emit(new MappingStart());
emitter.Emit(new Scalar(nameof(Line2DEquation.Slope)));
serializer(line2DEquation.Slope, typeof(float));
emitter.Emit(new Scalar(nameof(Line2DEquation.OffsetY)));
serializer(line2DEquation.OffsetY, typeof(float));
emitter.Emit(new MappingEnd());
}
}

View File

@@ -0,0 +1,41 @@
using System;
using Engine.Core;
using YamlDotNet.Core;
using YamlDotNet.Core.Events;
using YamlDotNet.Serialization;
namespace Engine.Serializers.Yaml;
public class Projection1DConverter : EngineTypeYamlSerializerBase<Projection1D>
{
public override Projection1D Read(IParser parser, Type type, ObjectDeserializer rootDeserializer)
{
parser.Consume<MappingStart>();
if (parser.Consume<Scalar>().Value.CompareTo(nameof(Projection1D.Min)) != 0)
throw new ArgumentException($"{nameof(Projection1D)} mapping must start with {nameof(Projection1D.Min)}");
float min = (float)rootDeserializer(typeof(float))!;
if (parser.Consume<Scalar>().Value.CompareTo(nameof(Projection1D.Max)) != 0)
throw new ArgumentException($"{nameof(Projection1D)} mapping must end with {nameof(Projection1D.Max)}");
float max = (float)rootDeserializer(typeof(float))!;
parser.Consume<MappingEnd>();
return new Projection1D(min, max);
}
public override void WriteYaml(IEmitter emitter, object? value, Type type, ObjectSerializer serializer)
{
Projection1D projection1D = (Projection1D)value!;
emitter.Emit(new MappingStart());
emitter.Emit(new Scalar(nameof(Projection1D.Min)));
serializer(projection1D.Min, typeof(float));
emitter.Emit(new Scalar(nameof(Projection1D.Max)));
serializer(projection1D.Max, typeof(float));
emitter.Emit(new MappingEnd());
}
}

View File

@@ -0,0 +1,28 @@
using System;
using Engine.Core;
using YamlDotNet.Core;
using YamlDotNet.Core.Events;
using YamlDotNet.Serialization;
namespace Engine.Serializers.Yaml;
public class QuaternionConverter : EngineTypeYamlSerializerBase<Quaternion>
{
private static readonly int SUBSTRING_START_LENGTH = nameof(Quaternion).Length + 1;
public override Quaternion Read(IParser parser, Type type, ObjectDeserializer rootDeserializer)
{
string value = parser.Consume<Scalar>().Value;
string insideParenthesis = value[SUBSTRING_START_LENGTH..^1];
string[] values = insideParenthesis.Split(", ");
return new Quaternion(float.Parse(values[0]), float.Parse(values[1]), float.Parse(values[2]), float.Parse(values[3]));
}
public override void WriteYaml(IEmitter emitter, object? value, Type type, ObjectSerializer serializer)
{
Quaternion quaternion = (Quaternion)value!;
emitter.Emit(new Scalar($"{nameof(Quaternion)}({quaternion.X}, {quaternion.Y}, {quaternion.Z}, {quaternion.W})"));
}
}

View File

@@ -0,0 +1,48 @@
using System;
using System.Collections.Generic;
using Engine.Core;
using YamlDotNet.Core;
using YamlDotNet.Core.Events;
using YamlDotNet.Serialization;
namespace Engine.Serializers.Yaml;
public class Shape2DConverter : EngineTypeYamlSerializerBase<Shape2D>
{
public override Shape2D Read(IParser parser, Type type, ObjectDeserializer rootDeserializer)
{
parser.Consume<MappingStart>();
if (parser.Consume<Scalar>().Value.CompareTo(nameof(Shape2D.Vertices)) != 0)
throw new ArgumentException($"{nameof(Shape2D)} mapping have a {nameof(Shape2D.Vertices)}");
parser.Consume<SequenceStart>();
List<Vector2D> vertices = [];
while (!parser.TryConsume<SequenceEnd>(out _))
{
Vector2D vertex = (Vector2D)rootDeserializer(typeof(Vector2D))!;
vertices.Add(vertex);
}
parser.Consume<MappingEnd>();
return new Shape2D(vertices);
}
public override void WriteYaml(IEmitter emitter, object? value, Type type, ObjectSerializer serializer)
{
Shape2D shape2D = (Shape2D)value!;
emitter.Emit(new MappingStart());
emitter.Emit(new Scalar(nameof(Shape2D.Vertices)));
emitter.Emit(new SequenceStart(anchor: null, tag: null, isImplicit: false, style: SequenceStyle.Block));
foreach (Vector2D vertex in shape2D.Vertices)
serializer(vertex, typeof(Vector2D));
emitter.Emit(new SequenceEnd());
emitter.Emit(new MappingEnd());
}
}

View File

@@ -0,0 +1,47 @@
using System;
using Engine.Core;
using YamlDotNet.Core;
using YamlDotNet.Core.Events;
using YamlDotNet.Serialization;
namespace Engine.Serializers.Yaml;
public class TriangleConverter : EngineTypeYamlSerializerBase<Triangle>
{
public override Triangle Read(IParser parser, Type type, ObjectDeserializer rootDeserializer)
{
parser.Consume<MappingStart>();
if (parser.Consume<Scalar>().Value.CompareTo(nameof(Triangle.A)) != 0)
throw new ArgumentException($"{nameof(Triangle)} mapping must start with {nameof(Triangle.A)}");
Vector2D a = (Vector2D)rootDeserializer(typeof(Vector2D))!;
if (parser.Consume<Scalar>().Value.CompareTo(nameof(Triangle.B)) != 0)
throw new ArgumentException($"{nameof(Triangle)} mapping must have {nameof(Triangle.B)} after {nameof(Triangle.A)}");
Vector2D b = (Vector2D)rootDeserializer(typeof(Vector2D))!;
if (parser.Consume<Scalar>().Value.CompareTo(nameof(Triangle.C)) != 0)
throw new ArgumentException($"{nameof(Triangle)} mapping must end with {nameof(Triangle.C)}");
Vector2D c = (Vector2D)rootDeserializer(typeof(Vector2D))!;
parser.Consume<MappingEnd>();
return new Triangle(a, b, c);
}
public override void WriteYaml(IEmitter emitter, object? value, Type type, ObjectSerializer serializer)
{
Triangle aabb = (Triangle)value!;
emitter.Emit(new MappingStart());
emitter.Emit(new Scalar(nameof(Triangle.A)));
serializer(aabb.A, typeof(Vector2D));
emitter.Emit(new Scalar(nameof(Triangle.B)));
serializer(aabb.B, typeof(Vector2D));
emitter.Emit(new Scalar(nameof(Triangle.C)));
serializer(aabb.C, typeof(Vector2D));
emitter.Emit(new MappingEnd());
}
}

View File

@@ -0,0 +1,28 @@
using System;
using Engine.Core;
using YamlDotNet.Core;
using YamlDotNet.Core.Events;
using YamlDotNet.Serialization;
namespace Engine.Serializers.Yaml;
public class Vector2DConverter : EngineTypeYamlSerializerBase<Vector2D>
{
private static readonly int SUBSTRING_START_LENGTH = nameof(Vector2D).Length + 1;
public override Vector2D Read(IParser parser, Type type, ObjectDeserializer rootDeserializer)
{
string value = parser.Consume<Scalar>().Value;
string insideParenthesis = value[SUBSTRING_START_LENGTH..^1];
string[] values = insideParenthesis.Split(", ");
return new Vector2D(float.Parse(values[0]), float.Parse(values[1]));
}
public override void WriteYaml(IEmitter emitter, object? value, Type type, ObjectSerializer serializer)
{
Vector2D vector2D = (Vector2D)value!;
emitter.Emit(new Scalar($"{nameof(Vector2D)}({vector2D.X}, {vector2D.Y})"));
}
}

View File

@@ -0,0 +1,28 @@
using System;
using Engine.Core;
using YamlDotNet.Core;
using YamlDotNet.Core.Events;
using YamlDotNet.Serialization;
namespace Engine.Serializers.Yaml;
public class Vector3DConverter : EngineTypeYamlSerializerBase<Vector3D>
{
private static readonly int SUBSTRING_START_LENGTH = nameof(Vector3D).Length + 1;
public override Vector3D Read(IParser parser, Type type, ObjectDeserializer rootDeserializer)
{
string value = parser.Consume<Scalar>().Value;
string insideParenthesis = value[SUBSTRING_START_LENGTH..^1];
string[] values = insideParenthesis.Split(", ");
return new Vector3D(float.Parse(values[0]), float.Parse(values[1]), float.Parse(values[2]));
}
public override void WriteYaml(IEmitter emitter, object? value, Type type, ObjectSerializer serializer)
{
Vector3D vector3D = (Vector3D)value!;
emitter.Emit(new Scalar($"{nameof(Vector3D)}({vector3D.X}, {vector3D.Y}, {vector3D.Z})"));
}
}