Skip to content

Commit a663438

Browse files
committed
Support QAngle in KeyValues2
1 parent ff1bc8d commit a663438

4 files changed

Lines changed: 35 additions & 5 deletions

File tree

Codecs/Binary.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,11 @@ class Binary : IDeferredAttributeCodec, IDisposable
2626

2727
static Binary()
2828
{
29-
SupportedAttributes[1] = SupportedAttributes[2] = new Type[] { typeof(Element), typeof(int), typeof(float), typeof(bool), typeof(string), typeof(byte[]), null /* ObjectID */, typeof(System.Drawing.Color), typeof(Vector2), typeof(Vector3), typeof(Vector4), typeof(Vector3) /* angle*/, typeof(Quaternion), typeof(Matrix4x4) };
30-
SupportedAttributes[3] = SupportedAttributes[4] = SupportedAttributes[5] = new Type[] { typeof(Element), typeof(int), typeof(float), typeof(bool), typeof(string), typeof(byte[]), typeof(TimeSpan), typeof(System.Drawing.Color), typeof(Vector2), typeof(Vector3), typeof(Vector4), typeof(Vector3) /* angle*/, typeof(Quaternion), typeof(Matrix4x4) };
29+
SupportedAttributes[1] =
30+
SupportedAttributes[2] = new Type[] { typeof(Element), typeof(int), typeof(float), typeof(bool), typeof(string), typeof(byte[]), null /* ObjectID */, typeof(System.Drawing.Color), typeof(Vector2), typeof(Vector3), typeof(Vector4), typeof(Vector3) /* angle*/, typeof(Quaternion), typeof(Matrix4x4) };
31+
SupportedAttributes[3] =
32+
SupportedAttributes[4] =
33+
SupportedAttributes[5] = new Type[] { typeof(Element), typeof(int), typeof(float), typeof(bool), typeof(string), typeof(byte[]), typeof(TimeSpan), typeof(System.Drawing.Color), typeof(Vector2), typeof(Vector3), typeof(Vector4), typeof(Vector3) /* angle*/, typeof(Quaternion), typeof(Matrix4x4) };
3134
SupportedAttributes[9] = new Type[] { typeof(Element), typeof(int), typeof(float), typeof(bool), typeof(string), typeof(byte[]), typeof(TimeSpan), typeof(System.Drawing.Color), typeof(Vector2), typeof(Vector3), typeof(Vector4), typeof(Vector3) /* angle*/, typeof(Quaternion), typeof(Matrix4x4), typeof(UInt64), typeof(byte) };
3235
}
3336

Codecs/KeyValues2.cs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ static KeyValues2()
3939

4040
TypeNames[typeof(byte)] = "uint8";
4141
TypeNames[typeof(ulong)] = "uint64";
42+
TypeNames[typeof(QAngle)] = "qangle";
4243

4344
ValidAttributes[4] = TypeNames.Select(kv => kv.Key).ToArray();
4445
}
@@ -244,8 +245,8 @@ void WriteAttribute(string name, Type type, object value, bool in_array)
244245
var c = (System.Drawing.Color)value;
245246
value = String.Join(" ", new int[] { c.R, c.G, c.B, c.A });
246247
}
247-
else if (type == typeof(UInt64))
248-
value = ((UInt64)value).ToString("X");
248+
else if (value is ulong ulong_value)
249+
value = $"0x{ulong_value:X}";
249250
else if (type == typeof(Vector2))
250251
{
251252
var arr = new float[2];
@@ -276,6 +277,10 @@ void WriteAttribute(string name, Type type, object value, bool in_array)
276277
var m = (Matrix4x4)value;
277278
value = string.Join(" ", m.M11, m.M12, m.M13, m.M14, m.M21, m.M22, m.M23, m.M24, m.M31, m.M32, m.M33, m.M34, m.M41, m.M42, m.M43, m.M44);
278279
}
280+
else if (value is QAngle qangle_value)
281+
{
282+
value = string.Join(" ", (int)qangle_value.Pitch, (int)qangle_value.Yaw, (int)qangle_value.Roll);
283+
}
279284

280285
if (in_array)
281286
Writer.Write(String.Format(" \"{0}\",", value.ToString()));
@@ -327,6 +332,7 @@ public void Encode(Datamodel dm, int encoding_version, Stream stream)
327332
Writer.WriteTokens("$prefix_element$");
328333
Writer.WriteLine("{");
329334
Writer.Indent++;
335+
Writer.WriteTokenLine("id", "elementid", Guid.NewGuid().ToString());
330336
foreach (var attr in dm.PrefixAttributes)
331337
WriteAttribute(attr.Key, attr.Value.GetType(), attr.Value, false);
332338
Writer.Indent--;

Datamodel.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,8 @@ public DebugView(Datamodel dm)
5252
typeof(Quaternion),
5353
typeof(Matrix4x4),
5454
typeof(byte),
55-
typeof(ulong)
55+
typeof(ulong),
56+
typeof(QAngle),
5657
};
5758

5859
public static Type[] AttributeTypes => attributeTypes;

Types/QAngle.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
using System.Numerics;
2+
3+
namespace Datamodel;
4+
5+
public struct QAngle
6+
{
7+
public float Pitch;
8+
public float Yaw;
9+
public float Roll;
10+
11+
public QAngle(float pitch, float yaw, float roll)
12+
{
13+
Pitch = pitch;
14+
Yaw = yaw;
15+
Roll = roll;
16+
}
17+
18+
public static implicit operator Vector3(QAngle q) => new(q.Pitch, q.Yaw, q.Roll);
19+
public static implicit operator QAngle(Vector3 v) => new(v.X, v.Y, v.Z);
20+
}

0 commit comments

Comments
 (0)