forked from LukeStampfli/DarkriftSerializationExtensions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSerializationExtensions.cs
212 lines (187 loc) · 6.33 KB
/
SerializationExtensions.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
using System;
using DarkRift;
using UnityEngine;
namespace DarkriftSerializationExtensions
{
public static class SerializationExtensions
{
/// <summary>
/// Writes a Vector3 (12 bytes)
/// </summary>
public static void WriteVector3(this DarkRiftWriter writer, Vector3 v)
{
writer.Write(v.x);
writer.Write(v.y);
writer.Write(v.z);
}
/// <summary>
/// Reads a Vector3 (12 bytes)
/// </summary>
public static Vector3 ReadVector3(this DarkRiftReader reader)
{
return new Vector3(reader.ReadSingle(),reader.ReadSingle(),reader.ReadSingle());
}
/// <summary>
/// Writes a Vector2 (8 bytes)
/// </summary>
public static void WriteVector2(this DarkRiftWriter writer, Vector2 v)
{
writer.Write(v.x);
writer.Write(v.y);
}
/// <summary>
/// Reads a Vector2 (8 bytes)
/// </summary>
public static Vector2 ReadVector2(this DarkRiftReader reader)
{
return new Vector2(reader.ReadSingle(), reader.ReadSingle());
}
/// <summary>
/// Writes a Quaternion (12 bytes)
/// </summary>
public static void WriteQuaternion(this DarkRiftWriter writer, Quaternion q)
{
// x*x+y*y+z*z+w*w = 1 => We don't have to send w.
writer.Write(q.x);
writer.Write(q.y);
writer.Write(q.z);
}
/// <summary>
/// Reads a Quaternion (12 bytes)
/// </summary>
public static Quaternion ReadQuaternion(this DarkRiftReader reader)
{
float x = reader.ReadSingle();
float y = reader.ReadSingle();
float z = reader.ReadSingle();
float w = Mathf.Sqrt(1f - (x * x + y * y + z * z));
return new Quaternion(x,y,z,w);
}
/// <summary>
/// Writes a quaternion compressed as 50 bits (6 bytes + 2 bits)
/// </summary>
public static void WriteQuaternionCompressed(this DarkRiftWriter writer, Quaternion q)
{
byte maxIndex = 0;
float maxValue = float.MinValue;
float sign = 1f;
for (int i = 0; i < 4; i++)
{
float f = q[i];
float abs = Mathf.Abs(f);
if (abs > maxValue )
{
maxValue = abs;
maxIndex = (byte)i;
sign = f > 0 ? 1 : -1;
}
}
short a = 0, b = 0, c = 0;
switch (maxIndex)
{
case 0:
a = (short) (q.y * sign * 32767f);
b = (short)(q.z * sign * 32767f);
c = (short)(q.w * sign * 32767f);
break;
case 1:
a = (short)(q.x * sign * 32767f);
b = (short)(q.z * sign * 32767f);
c = (short)(q.w * sign * 32767f);
break;
case 2:
a = (short)(q.x * sign * 32767f);
b = (short)(q.y * sign * 32767f);
c = (short)(q.w * sign * 32767f);
break;
case 3:
a = (short)(q.x * sign * 32767f);
b = (short)(q.y * sign * 32767f);
c = (short)(q.z * sign * 32767f);
break;
}
writer.Write(maxIndex);
writer.Write(a);
writer.Write(b);
writer.Write(c);
}
/// <summary>
/// Reads a quaternion that was written by WriteQuaternionCompressed
/// </summary>
public static Quaternion ReadQuaternionCompressed(this DarkRiftReader reader)
{
byte maxIndex = reader.ReadByte();
float a = reader.ReadInt16() / 32767f;
float b = reader.ReadInt16() / 32767f;
float c = reader.ReadInt16() / 32767f;
float d = Mathf.Sqrt(1f - (a * a + b * b + c * c));
switch (maxIndex)
{
case 0:
return new Quaternion(d, a, b, c);
case 1:
return new Quaternion(a, d, b, c);
case 2:
return new Quaternion(a, b, d, c);
default:
return new Quaternion(a, b, c, d);
}
}
private static bool GetBit(this byte b, int bitIndex)
{
return ((b >> bitIndex) & 1) != 0;
}
/// <summary>
/// Writes an angle in radiant as 2 bytes
/// </summary>
public static void WriteAngle(this DarkRiftWriter writer, float angle)
{
ushort a = (ushort) (angle * 10430);
writer.Write(a);
}
/// <summary>
/// Reads an angle written by WriteAngle
/// </summary>
public static float ReadAngle(this DarkRiftReader reader)
{
ushort a = reader.ReadUInt16();
return a / 10430f;
}
/// <summary>
/// Writes an array of bools as bytes
/// </summary>
public static void WriteBoolsAsBytes(this DarkRiftWriter writer, bool[] bools)
{
for (int i = 0; i < bools.Length; i+= 8)
{
int temp = 0;
for (int j = 0; j < 8; j++)
{
if(bools[i+j])
{
temp += (bools[i + j] ? 1: 0) << j;
}
}
writer.Write((byte)temp);
}
}
/// <summary>
/// Reads an array of bools written by WriteBoolsAsBytes
/// </summary>
/// <param name="amount"> The amount of bools to read</param>
/// <returns></returns>
public static bool[] ReadBytesAsBools(this DarkRiftReader reader, int amount)
{
bool[] bools = new bool[amount];
for (int i = 0; i < amount; i+= 8)
{
int temp = reader.ReadByte();
for (int j = 0; j < 8; j++)
{
bools[i + j] = ((temp >> j) & 1) == 1;
}
}
return bools;
}
}
}