-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathSerialization.cs
More file actions
90 lines (80 loc) · 3.16 KB
/
Serialization.cs
File metadata and controls
90 lines (80 loc) · 3.16 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
using System;
using UnityEngine;
namespace LightNet
{
/// <summary>
/// A neat helper for easier unity data serialization
/// </summary>
public static class SerializationHelper
{
public static void FromBytes(byte[] data, ref int offset, out bool value)
{
Debug.Assert(offset + sizeof(byte) <= data.Length);
value = data[offset] != 0;
offset += sizeof(byte);
}
public static void FromBytes(byte[] data, ref int offset, out float value)
{
Debug.Assert(offset + sizeof(float) <= data.Length);
value = BitConverter.ToSingle(data, offset);
offset += sizeof(float);
}
public static void FromBytes(byte[] data, ref int offset, out Vector3 vector)
{
FromBytes(data, ref offset, out vector.x);
FromBytes(data, ref offset, out vector.y);
FromBytes(data, ref offset, out vector.z);
}
public static void FromBytes(byte[] data, ref int offset, out Quaternion quat)
{
FromBytes(data, ref offset, out quat.x);
FromBytes(data, ref offset, out quat.y);
FromBytes(data, ref offset, out quat.z);
FromBytes(data, ref offset, out quat.w);
}
public static void ToBytes(ulong value, byte[] data, ref int offset)
{
Debug.Assert(offset + sizeof(float) < data.Length);
byte[] buffer = BitConverter.GetBytes(value);
Array.Copy(buffer, 0, data, offset, buffer.Length);
offset += buffer.Length;
}
public static void ToBytes(bool value, byte[] data, ref int offset)
{
Debug.Assert(offset + sizeof(bool) <= data.Length);
data[offset] = value ? (byte)1 : (byte)0;
offset += sizeof(byte);
}
public static void ToBytes(float value, byte[] data, ref int offset)
{
Debug.Assert(offset + sizeof(float) <= data.Length);
byte[] buffer = BitConverter.GetBytes(value);
Array.Copy(buffer, 0, data, offset, buffer.Length);
offset += buffer.Length;
}
public static void ToBytes(in Vector3 vector, byte[] data, ref int offset)
{
ToBytes(vector.x, data, ref offset);
ToBytes(vector.y, data, ref offset);
ToBytes(vector.z, data, ref offset);
}
public static void ToBytes(in Quaternion quat, byte[] data, ref int offset)
{
ToBytes(quat.x, data, ref offset);
ToBytes(quat.y, data, ref offset);
ToBytes(quat.z, data, ref offset);
ToBytes(quat.w, data, ref offset);
}
}
/// <summary>
/// Interface that needs to be implemented for each of your custom
/// network data types / package types you wish to send over the network.<br/><br/>
/// IMPORTANT NOTE: The first byte ALWAYS corresponds to the custom package type and should be handled as such!<br/>
/// See: <see cref="ReceivedNetworkDataEventArgs.Type"/>
/// </summary>
public interface NetworkData
{
byte[] Serialize();
void Deserialize(byte[] data);
}
}