-
Notifications
You must be signed in to change notification settings - Fork 0
/
UnitySerializer.cs
321 lines (281 loc) · 9.93 KB
/
UnitySerializer.cs
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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
using UnityEngine;
using System.Collections.Generic;
using System;
namespace Gosh.Utilities
{
public class UnitySerializer
{
private List<byte> byteStream = new List<byte>();
private byte[] byteArray;
private int index = 0;
/// <summary>
/// Returns the stream as a Byte Array
/// </summary>
public byte[] ByteArray
{
get
{
if (byteArray == null || byteStream.Count != byteArray.Length)
byteArray = byteStream.ToArray();
return byteArray;
}
}
/// <summary>
/// Create a new empty stream
/// </summary>
public UnitySerializer()
{
}
/// <summary>
/// Initialiaze a stream from a byte array.
/// Used for deserilaizing a byte array
/// </summary>
/// <param name="ByteArray"></param>
public UnitySerializer(byte[] ByteArray)
{
byteArray = ByteArray;
byteStream = new List<byte>(ByteArray);
}
// GG
public void Init(byte[] _byteArray)
{
byteStream = new List<byte>(_byteArray);
}
public void Reset()
{
if(byteStream != null)
byteStream.Clear();
byteArray = null;
}
// --- double ---
public void Serialize(double d)
{
byteStream.AddRange(BitConverter.GetBytes(d));
}
public double DeserializeDouble()
{
double d = BitConverter.ToDouble(ByteArray, index); index += 8;
return d;
}
//
// --- bool ---
public void Serialize(bool b)
{
byteStream.AddRange(BitConverter.GetBytes(b));
}
public bool DeserializeBool()
{
bool b = BitConverter.ToBoolean(ByteArray, index); index += 1;
return b;
}
//
// --- Vector2 ---
public void Serialize(Vector2 v)
{
byteStream.AddRange(GetBytes(v));
}
public Vector2 DeserializeVector2()
{
Vector2 vector2 = new Vector2();
vector2.x = BitConverter.ToSingle(ByteArray, index); index += 4;
vector2.y = BitConverter.ToSingle(ByteArray, index); index += 4;
return vector2;
}
//
// --- Vector3 ---
public void Serialize(Vector3 v)
{
byteStream.AddRange(GetBytes(v));
}
public Vector3 DeserializeVector3()
{
Vector3 vector3 = new Vector3();
vector3.x = BitConverter.ToSingle(ByteArray, index); index += 4;
vector3.y = BitConverter.ToSingle(ByteArray, index); index += 4;
vector3.z = BitConverter.ToSingle(ByteArray, index); index += 4;
return vector3;
}
//
// --- Type ---
public void Serialize(System.Type t)
{
// serialize type as string
string typeStr = t.ToString();
Serialize(typeStr);
}
public Type DeserializeType()
{
// type stored as string
string typeStr = DeserializeString();
return Type.GetType(typeStr); ;
}
//
// --- String ---
public void Serialize(string s)
{
// add the length as a header
byteStream.AddRange(BitConverter.GetBytes(s.Length));
foreach (char c in s)
byteStream.Add((byte)c);
}
public string DeserializeString()
{
int length = BitConverter.ToInt32(ByteArray, index); index += 4;
string s = "";
for (int i = 0; i < length; i++)
{
s += (char)ByteArray[index];
index++;
}
return s;
}
//
// --- byte[] ---
public void Serialize(byte[] b)
{
// add the length as a header
byteStream.AddRange(BitConverter.GetBytes(b.Length));
byteStream.AddRange(b);
}
public byte[] DeserializeByteArray()
{
int length = BitConverter.ToInt32(ByteArray, index); index += 4;
byte[] bytes = new byte[length];
for (int i = 0; i < length; i++)
{
bytes[i] = ByteArray[index];
index++;
}
return bytes;
}
//
// --- Quaternion ---
public void Serialize(Quaternion q)
{
byteStream.AddRange(GetBytes(q));
}
public Quaternion DeserializeQuaternion()
{
Quaternion quat = new Quaternion();
quat.x = BitConverter.ToSingle(ByteArray, index); index += 4;
quat.y = BitConverter.ToSingle(ByteArray, index); index += 4;
quat.z = BitConverter.ToSingle(ByteArray, index); index += 4;
quat.w = BitConverter.ToSingle(ByteArray, index); index += 4;
return quat;
}
//
// --- float ---
public void Serialize(float f)
{
byteStream.AddRange(BitConverter.GetBytes(f));
}
public float DeserializeFloat()
{
float f = BitConverter.ToSingle(ByteArray, index); index += 4;
return f;
}
//
// --- int ---
public void Serialize(int i)
{
byteStream.AddRange(BitConverter.GetBytes(i));
}
public int DeserializeInt()
{
int i = BitConverter.ToInt32(ByteArray, index); index += 4;
return i;
}
//
// --- internal ----
Vector3 DeserializeVector3(byte[] bytes, ref int index)
{
Vector3 vector3 = new Vector3();
vector3.x = BitConverter.ToSingle(bytes, index); index += 4;
vector3.y = BitConverter.ToSingle(bytes, index); index += 4;
vector3.z = BitConverter.ToSingle(bytes, index); index += 4;
return vector3;
}
Quaternion DeserializeQuaternion(byte[] bytes, ref int index)
{
Quaternion quat = new Quaternion();
quat.x = BitConverter.ToSingle(bytes, index); index += 4;
quat.y = BitConverter.ToSingle(bytes, index); index += 4;
quat.z = BitConverter.ToSingle(bytes, index); index += 4;
quat.w = BitConverter.ToSingle(bytes, index); index += 4;
return quat;
}
byte[] GetBytes(Vector2 v)
{
List<byte> bytes = new List<byte>(8);
bytes.AddRange(BitConverter.GetBytes(v.x));
bytes.AddRange(BitConverter.GetBytes(v.y));
return bytes.ToArray();
}
byte[] GetBytes(Vector3 v)
{
List<byte> bytes = new List<byte>(12);
bytes.AddRange(BitConverter.GetBytes(v.x));
bytes.AddRange(BitConverter.GetBytes(v.y));
bytes.AddRange(BitConverter.GetBytes(v.z));
return bytes.ToArray();
}
byte[] GetBytes(Quaternion q)
{
List<byte> bytes = new List<byte>(16);
bytes.AddRange(BitConverter.GetBytes(q.x));
bytes.AddRange(BitConverter.GetBytes(q.y));
bytes.AddRange(BitConverter.GetBytes(q.z));
bytes.AddRange(BitConverter.GetBytes(q.w));
return bytes.ToArray();
}
// public static void Example()
// {
// //
// Debug.Log("--- UnitySerializer Example ---");
// Vector2 point = UnityEngine.Random.insideUnitCircle;
// Vector3 position = UnityEngine.Random.onUnitSphere;
// Quaternion quaternion = UnityEngine.Random.rotation;
// float f = UnityEngine.Random.value;
// int i = UnityEngine.Random.Range(0, 10000);
// double d = (double)UnityEngine.Random.Range(0, 10000);
// string s = "Brundle Fly";
// bool b = UnityEngine.Random.value < 0.5f ? true : false;
// System.Type type = typeof(UnitySerializer);
//
// //
// Debug.Log("--- Before ---");
// Debug.Log(point + " " + position + " " + quaternion + " " + f + " " + d + " " + s + " " + b + " " + type);
//
// //
// Debug.Log("--- Serialize ---");
// UnitySerializer us = new UnitySerializer();
// us.Serialize(point);
// us.Serialize(position);
// us.Serialize(quaternion);
// us.Serialize(f);
// us.Serialize(i);
// us.Serialize(d);
// us.Serialize(s);
// us.Serialize(b);
// us.Serialize(type);
// byte[] byteArray = us.ByteArray;
//
// // the array must be deserialized in the same order as it was serialized
// Debug.Log("--- Deserialize ---");
// UnitySerializer uds = new UnitySerializer(byteArray);
// Vector2 point2 = uds.DeserializeVector2();
// Vector3 position2 = uds.DeserializeVector3();
// Quaternion quaternion2 = uds.DeserializeQuaternion();
// float f2 = uds.DeserializeFloat();
// int i2 = uds.DeserializeInt();
// double d2 = uds.DeserializeDouble();
// string s2 = uds.DeserializeString();
// bool b2 = uds.DeserializeBool();
// System.Type type2 = uds.DeserializeType();
//
// //
// Debug.Log("--- After ---");
// Debug.Log(point2 + " " + position2 + " " + quaternion2 + " " + f2 + " " + d2 + " " + s2 + " " + b2 + " " + type2);
// }
}
}