2
2
using System . Globalization ;
3
3
using Hazel ;
4
4
using Hazel . Udp ;
5
+ using Reactor . Networking . Serialization ;
5
6
using UnityEngine ;
6
7
7
8
namespace Reactor . Networking . Extensions ;
@@ -38,30 +39,7 @@ public static void Write(this MessageWriter writer, Vector2 value)
38
39
/// </summary>
39
40
/// <param name="writer">The <see cref="MessageWriter"/> to write to.</param>
40
41
/// <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 ) ) ;
65
43
66
44
/// <summary>
67
45
/// Writes a generic Enum value to the <paramref name="writer"/>.
@@ -71,6 +49,14 @@ public static void Write(this MessageWriter writer, Enum value)
71
49
/// <typeparam name="T">Enum type to write.</typeparam>
72
50
public static void Write < T > ( this MessageWriter writer , T value ) where T : struct , Enum => writer . Write ( ( Enum ) value ) ;
73
51
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
+
74
60
/// <summary>
75
61
/// Reads a <see cref="Vector2"/> from the <paramref name="reader"/>.
76
62
/// </summary>
@@ -90,74 +76,22 @@ public static Vector2 ReadVector2(this MessageReader reader)
90
76
/// <param name="reader">The <see cref="MessageReader"/> to read from.</param>
91
77
/// <typeparam name="T">The <see cref="Enum"/> type to convert to.</typeparam>
92
78
/// <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 ) ) ;
124
80
125
81
/// <summary>
126
82
/// Reads an enum value from a network message.
127
83
/// </summary>
128
84
/// <param name="reader">The <see cref="MessageReader"/> to read from.</param>
129
85
/// <param name="enumType">The type of the enum.</param>
130
86
/// <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 ) ) ;
149
88
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 ) ;
161
95
162
96
/// <summary>
163
97
/// Sends a message on the <paramref name="connection"/> with an <paramref name="ackCallback"/>.
0 commit comments