110 lines
4.3 KiB
C#
110 lines
4.3 KiB
C#
using System;
|
|
using YamlDotNet.Core;
|
|
using YamlDotNet.Core.Events;
|
|
using YamlDotNet.Serialization;
|
|
using Unity.Mathematics;
|
|
|
|
namespace BITKit
|
|
{
|
|
public class UnityMathematicsYamlConverters : IYamlTypeConverter
|
|
{
|
|
public bool Accepts(Type type)
|
|
{
|
|
return type == typeof(int2) || type == typeof(int3) || type == typeof(int4) ||
|
|
type == typeof(float2) || type == typeof(float3) || type == typeof(float4) ||
|
|
type == typeof(quaternion);
|
|
}
|
|
|
|
public object ReadYaml(IParser parser, Type type, ObjectDeserializer rootDeserializer)
|
|
{
|
|
var scalar = parser.Consume<Scalar>();
|
|
var parts = scalar.Value.Split(',');
|
|
|
|
if (type == typeof(int2))
|
|
{
|
|
if (parts.Length != 2) throw new YamlException(scalar.Start, scalar.End, "Expected 2 int values for int2.");
|
|
return new int2(int.Parse(parts[0]), int.Parse(parts[1]));
|
|
}
|
|
else if (type == typeof(int3))
|
|
{
|
|
if (parts.Length != 3) throw new YamlException(scalar.Start, scalar.End, "Expected 3 int values for int3.");
|
|
return new int3(int.Parse(parts[0]), int.Parse(parts[1]), int.Parse(parts[2]));
|
|
}
|
|
else if (type == typeof(int4))
|
|
{
|
|
if (parts.Length != 4) throw new YamlException(scalar.Start, scalar.End, "Expected 4 int values for int4.");
|
|
return new int4(int.Parse(parts[0]), int.Parse(parts[1]), int.Parse(parts[2]), int.Parse(parts[3]));
|
|
}
|
|
else if (type == typeof(float2))
|
|
{
|
|
if (parts.Length != 2) throw new YamlException(scalar.Start, scalar.End, "Expected 2 float values for float2.");
|
|
return new float2(float.Parse(parts[0]), float.Parse(parts[1]));
|
|
}
|
|
else if (type == typeof(float3))
|
|
{
|
|
if (parts.Length != 3) throw new YamlException(scalar.Start, scalar.End, "Expected 3 float values for float3.");
|
|
return new float3(float.Parse(parts[0]), float.Parse(parts[1]), float.Parse(parts[2]));
|
|
}
|
|
else if (type == typeof(float4))
|
|
{
|
|
if (parts.Length != 4) throw new YamlException(scalar.Start, scalar.End, "Expected 4 float values for float4.");
|
|
return new float4(float.Parse(parts[0]), float.Parse(parts[1]), float.Parse(parts[2]), float.Parse(parts[3]));
|
|
}
|
|
else if (type == typeof(quaternion))
|
|
{
|
|
if (parts.Length != 4) throw new YamlException(scalar.Start, scalar.End, "Expected 4 float values for quaternion.");
|
|
return new quaternion(float.Parse(parts[0]), float.Parse(parts[1]), float.Parse(parts[2]), float.Parse(parts[3]));
|
|
}
|
|
|
|
throw new YamlException(scalar.Start, scalar.End, $"Unsupported type: {type}");
|
|
}
|
|
|
|
public void WriteYaml(IEmitter emitter, object value, Type type, ObjectSerializer serializer)
|
|
{
|
|
string scalarValue = "";
|
|
|
|
if (type == typeof(int2))
|
|
{
|
|
var int2Value = (int2)value;
|
|
scalarValue = $"{int2Value.x},{int2Value.y}";
|
|
}
|
|
else if (type == typeof(int3))
|
|
{
|
|
var int3Value = (int3)value;
|
|
scalarValue = $"{int3Value.x},{int3Value.y},{int3Value.z}";
|
|
}
|
|
else if (type == typeof(int4))
|
|
{
|
|
var int4Value = (int4)value;
|
|
scalarValue = $"{int4Value.x},{int4Value.y},{int4Value.z},{int4Value.w}";
|
|
}
|
|
else if (type == typeof(float2))
|
|
{
|
|
var float2Value = (float2)value;
|
|
scalarValue = $"{float2Value.x},{float2Value.y}";
|
|
}
|
|
else if (type == typeof(float3))
|
|
{
|
|
var float3Value = (float3)value;
|
|
scalarValue = $"{float3Value.x},{float3Value.y},{float3Value.z}";
|
|
}
|
|
else if (type == typeof(float4))
|
|
{
|
|
var float4Value = (float4)value;
|
|
scalarValue = $"{float4Value.x},{float4Value.y},{float4Value.z},{float4Value.w}";
|
|
}
|
|
else if (type == typeof(quaternion))
|
|
{
|
|
var quaternionValue = (quaternion)value;
|
|
scalarValue = $"{quaternionValue.value.x},{quaternionValue.value.y},{quaternionValue.value.z},{quaternionValue.value.w}";
|
|
}
|
|
else
|
|
{
|
|
throw new YamlException($"Unsupported type: {type}");
|
|
}
|
|
|
|
emitter.Emit(new Scalar(scalarValue));
|
|
}
|
|
}
|
|
}
|