forked from insthync/LiteNetLibManager
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNetDataReaderExtension.cs
More file actions
266 lines (217 loc) · 8.43 KB
/
NetDataReaderExtension.cs
File metadata and controls
266 lines (217 loc) · 8.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
using System;
using System.Collections.Generic;
using UnityEngine;
using LiteNetLibManager;
namespace LiteNetLib.Utils
{
public static class NetDataReaderExtension
{
public static TType GetValue<TType>(this NetDataReader reader)
{
return (TType)GetValue(reader, typeof(TType));
}
public static object GetValue(this NetDataReader reader, Type type)
{
#region Generic Values
if (type.IsEnum)
type = type.GetEnumUnderlyingType();
if (type == typeof(bool))
return reader.GetBool();
if (type == typeof(byte))
return reader.GetByte();
if (type == typeof(char))
return reader.GetChar();
if (type == typeof(double))
return reader.GetDouble();
if (type == typeof(float))
return reader.GetFloat();
if (type == typeof(int))
return reader.GetPackedInt();
if (type == typeof(long))
return reader.GetPackedLong();
if (type == typeof(sbyte))
return reader.GetSByte();
if (type == typeof(short))
return reader.GetPackedShort();
if (type == typeof(string))
return reader.GetString();
if (type == typeof(uint))
return reader.GetPackedUInt();
if (type == typeof(ulong))
return reader.GetPackedULong();
if (type == typeof(ushort))
return reader.GetPackedUShort();
#endregion
#region Unity Values
if (type == typeof(Color))
return reader.GetColor();
if (type == typeof(Quaternion))
return reader.GetQuaternion();
if (type == typeof(Vector2))
return reader.GetVector2();
if (type == typeof(Vector2Int))
return reader.GetVector2Int();
if (type == typeof(Vector3))
return reader.GetVector3();
if (type == typeof(Vector3Int))
return reader.GetVector3Int();
if (type == typeof(Vector4))
return reader.GetVector4();
#endregion
if (typeof(INetSerializable).IsAssignableFrom(type))
{
object instance = Activator.CreateInstance(type);
(instance as INetSerializable).Deserialize(reader);
return instance;
}
throw new ArgumentException("NetDataReader cannot read type " + type.Name);
}
public static Color GetColor(this NetDataReader reader)
{
float r = reader.GetByte() * 0.01f;
float g = reader.GetByte() * 0.01f;
float b = reader.GetByte() * 0.01f;
float a = reader.GetByte() * 0.01f;
return new Color(r, g, b, a);
}
public static Quaternion GetQuaternion(this NetDataReader reader)
{
Vector3 vector3 = new Vector3(reader.GetFloat(), reader.GetFloat(), reader.GetFloat());
return vector3.sqrMagnitude <= 0 ? Quaternion.identity : Quaternion.Euler(vector3);
}
public static Vector2 GetVector2(this NetDataReader reader)
{
return new Vector2(reader.GetFloat(), reader.GetFloat());
}
public static Vector2Int GetVector2Int(this NetDataReader reader)
{
return new Vector2Int(reader.GetInt(), reader.GetInt());
}
public static Vector3 GetVector3(this NetDataReader reader)
{
return new Vector3(reader.GetFloat(), reader.GetFloat(), reader.GetFloat());
}
public static Vector3Int GetVector3Int(this NetDataReader reader)
{
return new Vector3Int(reader.GetInt(), reader.GetInt(), reader.GetInt());
}
public static Vector4 GetVector4(this NetDataReader reader)
{
return new Vector4(reader.GetFloat(), reader.GetFloat(), reader.GetFloat(), reader.GetFloat());
}
public static TValue[] GetArray<TValue>(this NetDataReader reader)
{
int count = reader.GetInt();
TValue[] result = new TValue[count];
for (int i = 0; i < count; ++i)
{
result[i] = reader.GetValue<TValue>();
}
return result;
}
public static object GetArray(this NetDataReader reader, Type type)
{
int count = reader.GetInt();
Array array = Array.CreateInstance(type, count);
for (int i = 0; i < count; ++i)
{
array.SetValue(reader.GetValue(type), i);
}
return array;
}
public static List<TValue> GetList<TValue>(this NetDataReader reader)
{
int count = reader.GetInt();
List<TValue> result = new List<TValue>();
for (int i = 0; i < count; ++i)
{
result.Add(reader.GetValue<TValue>());
}
return result;
}
public static Dictionary<TKey, TValue> GetDictionary<TKey, TValue>(this NetDataReader reader)
{
int count = reader.GetInt();
Dictionary<TKey, TValue> result = new Dictionary<TKey, TValue>();
for (int i = 0; i < count; ++i)
{
result.Add(reader.GetValue<TKey>(), reader.GetValue<TValue>());
}
return result;
}
#region Packed Signed Int (Ref: https://developers.google.com/protocol-buffers/docs/encoding#signed-integers)
public static short GetPackedShort(this NetDataReader reader)
{
return (short)GetPackedInt(reader);
}
public static int GetPackedInt(this NetDataReader reader)
{
uint value = GetPackedUInt(reader);
return (int)((value >> 1) ^ (-(int)(value & 1)));
}
public static long GetPackedLong(this NetDataReader reader)
{
return ((long)GetPackedInt(reader)) << 32 | ((uint)GetPackedInt(reader));
}
#endregion
#region Packed Unsigned Int (Ref: https://sqlite.org/src4/doc/trunk/www/varint.wiki)
public static ushort GetPackedUShort(this NetDataReader reader)
{
return (ushort)GetPackedULong(reader);
}
public static uint GetPackedUInt(this NetDataReader reader)
{
return (uint)GetPackedULong(reader);
}
public static ulong GetPackedULong(this NetDataReader reader)
{
byte a0 = reader.GetByte();
if (a0 < 241)
{
return a0;
}
byte a1 = reader.GetByte();
if (a0 >= 241 && a0 <= 248)
{
return 240 + 256 * (a0 - ((ulong)241)) + a1;
}
byte a2 = reader.GetByte();
if (a0 == 249)
{
return 2288 + (((ulong)256) * a1) + a2;
}
byte a3 = reader.GetByte();
if (a0 == 250)
{
return a1 + (((ulong)a2) << 8) + (((ulong)a3) << 16);
}
byte a4 = reader.GetByte();
if (a0 == 251)
{
return a1 + (((ulong)a2) << 8) + (((ulong)a3) << 16) + (((ulong)a4) << 24);
}
byte a5 = reader.GetByte();
if (a0 == 252)
{
return a1 + (((ulong)a2) << 8) + (((ulong)a3) << 16) + (((ulong)a4) << 24) + (((ulong)a5) << 32);
}
byte a6 = reader.GetByte();
if (a0 == 253)
{
return a1 + (((ulong)a2) << 8) + (((ulong)a3) << 16) + (((ulong)a4) << 24) + (((ulong)a5) << 32) + (((ulong)a6) << 40);
}
byte a7 = reader.GetByte();
if (a0 == 254)
{
return a1 + (((ulong)a2) << 8) + (((ulong)a3) << 16) + (((ulong)a4) << 24) + (((ulong)a5) << 32) + (((ulong)a6) << 40) + (((ulong)a7) << 48);
}
byte a8 = reader.GetByte();
if (a0 == 255)
{
return a1 + (((ulong)a2) << 8) + (((ulong)a3) << 16) + (((ulong)a4) << 24) + (((ulong)a5) << 32) + (((ulong)a6) << 40) + (((ulong)a7) << 48) + (((ulong)a8) << 56);
}
throw new System.IndexOutOfRangeException("ReadPackedULong() failure: " + a0);
}
#endregion
}
}