1
This commit is contained in:
@@ -1,6 +1,8 @@
|
||||
using System;
|
||||
using Newtonsoft.Json;
|
||||
using Unity.Mathematics;
|
||||
using YamlDotNet.Core;
|
||||
using YamlDotNet.Serialization;
|
||||
|
||||
#if NET5_0_OR_GREATER
|
||||
namespace BITKit
|
||||
@@ -190,4 +192,4 @@ namespace BITKit
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
||||
#endif
|
||||
|
3
Src/NetSupport/Net.BITKit.NetSupport.asmref
Normal file
3
Src/NetSupport/Net.BITKit.NetSupport.asmref
Normal file
@@ -0,0 +1,3 @@
|
||||
{
|
||||
"reference": "GUID:14fe60d984bf9f84eac55c6ea033a8f4"
|
||||
}
|
7
Src/NetSupport/Net.BITKit.NetSupport.asmref.meta
Normal file
7
Src/NetSupport/Net.BITKit.NetSupport.asmref.meta
Normal file
@@ -0,0 +1,7 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 28ed10548f6aa6944ab6ac01ce4f9faf
|
||||
AssemblyDefinitionReferenceImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
109
Src/NetSupport/UnityMathematicsYamlConverters.cs
Normal file
109
Src/NetSupport/UnityMathematicsYamlConverters.cs
Normal file
@@ -0,0 +1,109 @@
|
||||
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));
|
||||
}
|
||||
}
|
||||
}
|
11
Src/NetSupport/UnityMathematicsYamlConverters.cs.meta
Normal file
11
Src/NetSupport/UnityMathematicsYamlConverters.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 16ca25bd5fef10847aec8081266207e1
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
Reference in New Issue
Block a user