Skip to content

Commit 1431a9d

Browse files
committed
Add long serialisation + improve enum serialisation
1 parent f130f44 commit 1431a9d

File tree

2 files changed

+26
-84
lines changed

2 files changed

+26
-84
lines changed

Reactor/Networking/Extensions/ExtraMessageExtensions.cs

+18-84
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.Globalization;
33
using Hazel;
44
using Hazel.Udp;
5+
using Reactor.Networking.Serialization;
56
using UnityEngine;
67

78
namespace Reactor.Networking.Extensions;
@@ -38,30 +39,7 @@ public static void Write(this MessageWriter writer, Vector2 value)
3839
/// </summary>
3940
/// <param name="writer">The <see cref="MessageWriter"/> to write to.</param>
4041
/// <param name="value">The <see cref="Enum"/> to write.</param>
41-
public static void Write(this MessageWriter writer, Enum value)
42-
{
43-
var enumType = value.GetType();
44-
var underlyingType = enumType.GetEnumUnderlyingType();
45-
46-
if (underlyingType == typeof(byte))
47-
writer.Write(Convert.ToByte(value, NumberFormatInfo.InvariantInfo));
48-
else if (underlyingType == typeof(sbyte))
49-
writer.Write(Convert.ToSByte(value, NumberFormatInfo.InvariantInfo));
50-
else if (underlyingType == typeof(short))
51-
writer.Write(Convert.ToInt16(value, NumberFormatInfo.InvariantInfo));
52-
else if (underlyingType == typeof(ushort))
53-
writer.Write(Convert.ToUInt16(value, NumberFormatInfo.InvariantInfo));
54-
else if (underlyingType == typeof(ulong))
55-
writer.Write(Convert.ToUInt64(value, NumberFormatInfo.InvariantInfo));
56-
else if (underlyingType == typeof(uint))
57-
writer.WritePacked(Convert.ToUInt32(value, NumberFormatInfo.InvariantInfo));
58-
else if (underlyingType == typeof(int))
59-
writer.WritePacked(Convert.ToInt32(value, NumberFormatInfo.InvariantInfo));
60-
else if (underlyingType == typeof(long))
61-
throw new NotSupportedException("long enum types are not supported at the moment.");
62-
else
63-
throw new ArgumentException("Unknown underlying type for " + enumType.Name);
64-
}
42+
public static void Write(this MessageWriter writer, Enum value) => writer.Serialize(Convert.ChangeType(value, Enum.GetUnderlyingType(value.GetType()), CultureInfo.InvariantCulture));
6543

6644
/// <summary>
6745
/// Writes a generic Enum value to the <paramref name="writer"/>.
@@ -71,6 +49,14 @@ public static void Write(this MessageWriter writer, Enum value)
7149
/// <typeparam name="T">Enum type to write.</typeparam>
7250
public static void Write<T>(this MessageWriter writer, T value) where T : struct, Enum => writer.Write((Enum) value);
7351

52+
/// <summary>
53+
/// Writes a long value to the <paramref name="writer"/>.
54+
/// </summary>
55+
/// <param name="writer">The <see cref="MessageWriter"/> to write to.</param>
56+
/// <param name="value">The <see cref="long"/> to write.</param>
57+
public static void Write(this MessageWriter writer, long value) => writer.Write(unchecked(value + long.MaxValue));
58+
// Someone please give me a better way to do this that does not involve shifting values please
59+
7460
/// <summary>
7561
/// Reads a <see cref="Vector2"/> from the <paramref name="reader"/>.
7662
/// </summary>
@@ -90,74 +76,22 @@ public static Vector2 ReadVector2(this MessageReader reader)
9076
/// <param name="reader">The <see cref="MessageReader"/> to read from.</param>
9177
/// <typeparam name="T">The <see cref="Enum"/> type to convert to.</typeparam>
9278
/// <returns>An <see cref="Enum"/> value from the <paramref name="reader"/>.</returns>
93-
public static T ReadEnum<T>(this MessageReader reader) where T : struct, Enum
94-
{
95-
var enumType = typeof(T);
96-
var underlyingType = enumType.GetEnumUnderlyingType();
97-
98-
if (underlyingType == typeof(byte))
99-
return (T) (object) reader.ReadByte();
100-
101-
if (underlyingType == typeof(sbyte))
102-
return (T) (object) reader.ReadSByte();
103-
104-
if (underlyingType == typeof(short))
105-
return (T) (object) reader.ReadInt16();
106-
107-
if (underlyingType == typeof(ushort))
108-
return (T) (object) reader.ReadUInt16();
109-
110-
if (underlyingType == typeof(ulong))
111-
return (T) (object) reader.ReadUInt64();
112-
113-
if (underlyingType == typeof(uint))
114-
return (T) (object) reader.ReadPackedUInt32();
115-
116-
if (underlyingType == typeof(int))
117-
return (T) (object) reader.ReadPackedInt32();
118-
119-
if (underlyingType == typeof(long))
120-
throw new NotSupportedException("long enum types are not supported at the moment.");
121-
122-
throw new ArgumentException("Unknown underlying type for " + enumType.Name);
123-
}
79+
public static T ReadEnum<T>(this MessageReader reader) where T : struct, Enum => (T) reader.ReadEnum(typeof(T));
12480

12581
/// <summary>
12682
/// Reads an enum value from a network message.
12783
/// </summary>
12884
/// <param name="reader">The <see cref="MessageReader"/> to read from.</param>
12985
/// <param name="enumType">The type of the enum.</param>
13086
/// <returns>The resulting enum value from the <paramref name="reader"/>.</returns>
131-
public static object ReadEnum(this MessageReader reader, Type enumType)
132-
{
133-
var underlyingType = enumType.GetEnumUnderlyingType();
134-
135-
if (underlyingType == typeof(byte))
136-
return Enum.Parse(enumType, $"{reader.ReadByte()}");
137-
138-
if (underlyingType == typeof(sbyte))
139-
return Enum.Parse(enumType, $"{reader.ReadSByte()}");
140-
141-
if (underlyingType == typeof(short))
142-
return Enum.Parse(enumType, $"{reader.ReadInt16()}");
143-
144-
if (underlyingType == typeof(ushort))
145-
return Enum.Parse(enumType, $"{reader.ReadUInt16()}");
146-
147-
if (underlyingType == typeof(ulong))
148-
return Enum.Parse(enumType, $"{reader.ReadUInt64()}");
87+
public static object ReadEnum(this MessageReader reader, Type enumType) => reader.Deserialize(Enum.GetUnderlyingType(enumType));
14988

150-
if (underlyingType == typeof(uint))
151-
return Enum.Parse(enumType, $"{reader.ReadPackedUInt32()}");
152-
153-
if (underlyingType == typeof(int))
154-
return Enum.Parse(enumType, $"{reader.ReadPackedInt32()}");
155-
156-
if (underlyingType == typeof(long))
157-
throw new NotSupportedException("long enum types are not supported at the moment.");
158-
159-
throw new ArgumentException("Unknown underlying type for " + enumType.Name);
160-
}
89+
/// <summary>
90+
/// Reads a long value from a network message.
91+
/// </summary>
92+
/// <param name="reader">The <see cref="MessageReader"/> to read from.</param>
93+
/// <returns>The resulting long value from the <paramref name="reader"/>.</returns>
94+
public static long ReadInt64(this MessageReader reader) => (long) unchecked(reader.ReadUInt64() - long.MaxValue);
16195

16296
/// <summary>
16397
/// Sends a message on the <paramref name="connection"/> with an <paramref name="ackCallback"/>.

Reactor/Networking/Serialization/MessageSerializer.cs

+8
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,9 @@ public static void Serialize(this MessageWriter writer, object @object)
9090
case ulong i:
9191
writer.Write(i);
9292
break;
93+
case long i:
94+
ExtraMessageExtensions.Write(writer, i); // For some reason this insists on referring the to float write method, so this is me taking precautions
95+
break;
9396
case Vector2 i:
9497
writer.Write(i);
9598
break;
@@ -169,6 +172,11 @@ public static object Deserialize(this MessageReader reader, Type objectType)
169172
return reader.ReadUInt64();
170173
}
171174

175+
if (objectType == typeof(long))
176+
{
177+
return reader.ReadInt64();
178+
}
179+
172180
if (objectType.IsEnum)
173181
{
174182
return reader.ReadEnum(objectType);

0 commit comments

Comments
 (0)