forked from Unity-Technologies/com.unity.netcode.gameobjects
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAutoBitWritable.cs
More file actions
46 lines (42 loc) · 1.51 KB
/
Copy pathAutoBitWritable.cs
File metadata and controls
46 lines (42 loc) · 1.51 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
using System.IO;
using System.Reflection;
using MLAPI.Serialization.Pooled;
namespace MLAPI.Serialization
{
/// <summary>
/// AutoBitWritable implements IBitWritable and automatically serializes fields using reflection
/// </summary>
public abstract class AutoBitWritable : IBitWritable
{
/// <summary>
/// Writes the contents of the type instance to the stream
/// </summary>
/// <param name="stream">The stream to write to</param>
public virtual void Write(Stream stream)
{
FieldInfo[] fields = SerializationManager.GetFieldsForType(GetType());
using (PooledBitWriter writer = PooledBitWriter.Get(stream))
{
for (int i = 0; i < fields.Length; i++)
{
writer.WriteObjectPacked(fields[i].GetValue(this));
}
}
}
/// <summary>
/// Reads the contents from the stream and applies it to the type instance
/// </summary>
/// <param name="stream">The stream to read from</param>
public virtual void Read(Stream stream)
{
FieldInfo[] fields = SerializationManager.GetFieldsForType(GetType());
using (PooledBitReader reader = PooledBitReader.Get(stream))
{
for (int i = 0; i < fields.Length; i++)
{
fields[i].SetValue(this, reader.ReadObjectPacked(fields[i].FieldType));
}
}
}
}
}