Skip to content

Commit b95f558

Browse files
[Serialization] WIP;
1 parent 78594a0 commit b95f558

File tree

4 files changed

+519
-24
lines changed

4 files changed

+519
-24
lines changed

Engine/Core/Serialization/Material/MaterialMetadata.cs

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,28 @@ public class Vector2Holder
3838
[Key(1)]
3939
public float y;
4040

41+
public Vector2Holder()
42+
{
43+
}
44+
45+
public Vector2Holder(float x, float y)
46+
{
47+
this.x = x;
48+
this.y = y;
49+
}
50+
51+
public Vector2Holder(Vector2 v)
52+
{
53+
x = v.X;
54+
y = v.Y;
55+
}
56+
57+
public Vector2Holder(Vector2Int v)
58+
{
59+
x = v.X;
60+
y = v.Y;
61+
}
62+
4163
public Vector2 ToVector2()
4264
{
4365
return new Vector2(x, y);
@@ -137,6 +159,13 @@ public Vector3Holder(Vector3 v)
137159
z = v.Z;
138160
}
139161

162+
public Vector3Holder(Vector3Int v)
163+
{
164+
x = v.X;
165+
y = v.Y;
166+
z = v.Z;
167+
}
168+
140169
public Vector3Holder(Quaternion q) : this(q.ToEulerAngles())
141170
{
142171
}
@@ -253,11 +282,32 @@ public Vector4Holder(Vector4 v)
253282
w = v.W;
254283
}
255284

285+
public Vector4Holder(Vector4Int v)
286+
{
287+
x = v.X;
288+
y = v.Y;
289+
z = v.Z;
290+
w = v.W;
291+
}
292+
293+
public Vector4Holder(Quaternion q)
294+
{
295+
x = q.X;
296+
y = q.Y;
297+
z = q.Z;
298+
w = q.W;
299+
}
300+
256301
public Vector4 ToVector4()
257302
{
258303
return new Vector4(x, y, z, w);
259304
}
260305

306+
public Quaternion ToQuaternion()
307+
{
308+
return new Quaternion(x, y, z, w);
309+
}
310+
261311
public static bool operator ==(Vector4Holder lhs, Vector4Holder rhs)
262312
{
263313
if (lhs is null && rhs is null)
Lines changed: 139 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Numerics;
23
using System.Reflection;
34
using System.Text.Json;
45

@@ -20,41 +21,163 @@ public bool HandlesType(Type type)
2021
Type t when t == typeof(float) => true,
2122
Type t when t == typeof(double) => true,
2223
Type t when t == typeof(bool) => true,
24+
Type t when t == typeof(Vector2) => true,
25+
Type t when t == typeof(Vector3) => true,
26+
Type t when t == typeof(Vector4) => true,
27+
Type t when t == typeof(Quaternion) => true,
2328
_ => false
2429
};
2530
}
2631

2732
public object DeserializeField(Type type, FieldInfo field, Type fieldType, StapleSerializerField fieldInfo)
2833
{
29-
return fieldInfo.value;
34+
return fieldType switch
35+
{
36+
Type t when t == typeof(Vector2) => ((Vector2Holder)fieldInfo.value).ToVector2(),
37+
Type t when t == typeof(Vector3) => ((Vector3Holder)fieldInfo.value).ToVector3(),
38+
Type t when t == typeof(Vector4) => ((Vector4Holder)fieldInfo.value).ToVector4(),
39+
Type t when t == typeof(Quaternion) => ((Vector4Holder)fieldInfo.value).ToQuaternion(),
40+
_ => fieldInfo.value,
41+
};
3042
}
3143

3244
public object DeserializeJsonField(Type type, FieldInfo field, Type fieldType, StapleSerializerField fieldInfo, JsonElement element)
3345
{
34-
return fieldType switch
46+
switch (fieldType)
3547
{
36-
Type t when t == typeof(string) && element.ValueKind == JsonValueKind.String => element.GetString(),
37-
Type t when t == typeof(byte) && element.ValueKind == JsonValueKind.Number => element.GetByte(),
38-
Type t when t == typeof(sbyte) && element.ValueKind == JsonValueKind.Number => element.GetSByte(),
39-
Type t when t == typeof(ushort) && element.ValueKind == JsonValueKind.Number => element.GetUInt16(),
40-
Type t when t == typeof(short) && element.ValueKind == JsonValueKind.Number => element.GetInt16(),
41-
Type t when t == typeof(uint) && element.ValueKind == JsonValueKind.Number => element.GetUInt32(),
42-
Type t when t == typeof(int) && element.ValueKind == JsonValueKind.Number => element.GetInt32(),
43-
Type t when t == typeof(float) && element.ValueKind == JsonValueKind.Number => element.GetSingle(),
44-
Type t when t == typeof(double) && element.ValueKind == JsonValueKind.Number => element.GetDouble(),
45-
Type t when t == typeof(bool) && (element.ValueKind == JsonValueKind.True ||
46-
element.ValueKind == JsonValueKind.False) => element.GetBoolean(),
47-
_ => null,
48+
case Type t when t == typeof(string) && element.ValueKind == JsonValueKind.String:
49+
50+
return element.GetString();
51+
52+
case Type t when t == typeof(byte) && element.ValueKind == JsonValueKind.Number:
53+
54+
return element.GetByte();
55+
56+
case Type t when t == typeof(sbyte) && element.ValueKind == JsonValueKind.Number:
57+
58+
return element.GetSByte();
59+
60+
case Type t when t == typeof(ushort) && element.ValueKind == JsonValueKind.Number:
61+
62+
return element.GetUInt16();
63+
64+
case Type t when t == typeof(short) && element.ValueKind == JsonValueKind.Number:
65+
66+
return element.GetInt16();
67+
68+
case Type t when t == typeof(uint) && element.ValueKind == JsonValueKind.Number:
69+
70+
return element.GetUInt32();
71+
72+
case Type t when t == typeof(int) && element.ValueKind == JsonValueKind.Number:
73+
74+
return element.GetInt32();
75+
76+
case Type t when t == typeof(float) && element.ValueKind == JsonValueKind.Number:
77+
78+
return element.GetSingle();
79+
80+
case Type t when t == typeof(double) && element.ValueKind == JsonValueKind.Number:
81+
82+
return element.GetDouble();
83+
84+
case Type t when t == typeof(bool) && (element.ValueKind == JsonValueKind.True ||
85+
element.ValueKind == JsonValueKind.False):
86+
87+
return element.GetBoolean();
88+
89+
90+
case Type t when t == typeof(Vector2) && element.ValueKind == JsonValueKind.Object:
91+
92+
{
93+
if (element.TryGetProperty(nameof(Vector2Holder.x), out var xProp) &&
94+
element.TryGetProperty(nameof(Vector2Holder.y), out var yProp) &&
95+
xProp.ValueKind == JsonValueKind.Number &&
96+
yProp.ValueKind == JsonValueKind.Number)
97+
{
98+
return new Vector2(xProp.GetSingle(), yProp.GetSingle());
99+
}
100+
}
101+
102+
break;
103+
104+
case Type t when t == typeof(Vector3) && element.ValueKind == JsonValueKind.Object:
105+
106+
{
107+
if (element.TryGetProperty(nameof(Vector3Holder.x), out var xProp) &&
108+
element.TryGetProperty(nameof(Vector3Holder.y), out var yProp) &&
109+
element.TryGetProperty(nameof(Vector3Holder.z), out var zProp) &&
110+
xProp.ValueKind == JsonValueKind.Number &&
111+
yProp.ValueKind == JsonValueKind.Number &&
112+
zProp.ValueKind == JsonValueKind.Number)
113+
{
114+
return new Vector3(xProp.GetSingle(), yProp.GetSingle(), zProp.GetSingle());
115+
}
116+
}
117+
118+
break;
119+
120+
case Type t when t == typeof(Vector4) && element.ValueKind == JsonValueKind.Object:
121+
122+
{
123+
if (element.TryGetProperty(nameof(Vector4Holder.x), out var xProp) &&
124+
element.TryGetProperty(nameof(Vector4Holder.y), out var yProp) &&
125+
element.TryGetProperty(nameof(Vector4Holder.z), out var zProp) &&
126+
element.TryGetProperty(nameof(Vector4Holder.w), out var wProp) &&
127+
xProp.ValueKind == JsonValueKind.Number &&
128+
yProp.ValueKind == JsonValueKind.Number &&
129+
zProp.ValueKind == JsonValueKind.Number &&
130+
wProp.ValueKind == JsonValueKind.Number)
131+
{
132+
return new Vector4(xProp.GetSingle(), yProp.GetSingle(), zProp.GetSingle(), wProp.GetSingle());
133+
}
134+
}
135+
136+
break;
137+
138+
case Type t when t == typeof(Quaternion) && element.ValueKind == JsonValueKind.Object:
139+
140+
{
141+
if (element.TryGetProperty(nameof(Vector4Holder.x), out var xProp) &&
142+
element.TryGetProperty(nameof(Vector4Holder.y), out var yProp) &&
143+
element.TryGetProperty(nameof(Vector4Holder.z), out var zProp) &&
144+
element.TryGetProperty(nameof(Vector4Holder.w), out var wProp) &&
145+
xProp.ValueKind == JsonValueKind.Number &&
146+
yProp.ValueKind == JsonValueKind.Number &&
147+
zProp.ValueKind == JsonValueKind.Number &&
148+
wProp.ValueKind == JsonValueKind.Number)
149+
{
150+
return new Quaternion(xProp.GetSingle(), yProp.GetSingle(), zProp.GetSingle(), wProp.GetSingle());
151+
}
152+
}
153+
154+
break;
48155
};
156+
157+
return null;
49158
}
50159

51160
public object SerializeField(object instance, Type type, FieldInfo field, Type fieldType)
52161
{
53-
return instance;
162+
return fieldType switch
163+
{
164+
Type t when t == typeof(Vector2) => new Vector2Holder((Vector2)instance),
165+
Type t when t == typeof(Vector3) => new Vector3Holder((Vector3)instance),
166+
Type t when t == typeof(Vector4) => new Vector4Holder((Vector4)instance),
167+
Type t when t == typeof(Quaternion) => new Vector4Holder((Quaternion)instance),
168+
_ => instance,
169+
};
54170
}
55171

56172
public object SerializeJsonField(object instance, Type type, FieldInfo field, Type fieldType)
57173
{
58-
return instance;
174+
return fieldType switch
175+
{
176+
Type t when t == typeof(Vector2) => new Vector2Holder((Vector2)instance),
177+
Type t when t == typeof(Vector3) => new Vector3Holder((Vector3)instance),
178+
Type t when t == typeof(Vector4) => new Vector4Holder((Vector4)instance),
179+
Type t when t == typeof(Quaternion) => new Vector4Holder((Quaternion)instance),
180+
_ => instance,
181+
};
59182
}
60183
}

0 commit comments

Comments
 (0)