Skip to content

Commit 051af85

Browse files
committed
Replaced IBitWritable with BitWritable with support for automatic serialization
1 parent 9b8746d commit 051af85

File tree

6 files changed

+132
-30
lines changed

6 files changed

+132
-30
lines changed

MLAPI/MLAPI.csproj

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,8 +116,9 @@
116116
<Compile Include="NetworkingManagerComponents\Binary\BitReaderDeprecated.cs" />
117117
<Compile Include="NetworkingManagerComponents\Binary\BinaryHelpers.cs" />
118118
<Compile Include="NetworkingManagerComponents\Binary\ByteBool.cs" />
119-
<Compile Include="NetworkingManagerComponents\Binary\IBitWritable.cs" />
119+
<Compile Include="NetworkingManagerComponents\Binary\BitWritable.cs" />
120120
<Compile Include="NetworkingManagerComponents\Binary\ResourcePool.cs" />
121+
<Compile Include="NetworkingManagerComponents\Binary\SerializationHelper.cs" />
121122
<Compile Include="NetworkingManagerComponents\Binary\UIntFloat.cs" />
122123
<Compile Include="NetworkingManagerComponents\Core\LogHelper.cs" />
123124
<Compile Include="NetworkingManagerComponents\Cryptography\CryptographyHelper.cs" />

MLAPI/NetworkingManagerComponents/Binary/BitReader.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -174,10 +174,10 @@ public object ReadObjectPacked(Type type)
174174
return null;
175175
}
176176
}
177-
if (typeof(IBitWritable).IsAssignableFrom(type))
177+
if (typeof(BitWritable).IsAssignableFrom(type))
178178
{
179179
object instance = Activator.CreateInstance(type);
180-
((IBitWritable)instance).Read(this.source);
180+
((BitWritable)instance).Read(this.source);
181181
return instance;
182182
}
183183

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
using System;
2+
using System.IO;
3+
using System.Reflection;
4+
5+
namespace MLAPI.Serialization
6+
{
7+
[Obsolete("The IBitWritable interface has been replaced with the abstract BitWritable class", true)]
8+
public interface IBitWritable
9+
{
10+
[Obsolete("The IBitWritable interface has been replaced with the abstract BitWritable class", true)]
11+
void Read(Stream stream);
12+
[Obsolete("The IBitWritable interface has been replaced with the abstract BitWritable class", true)]
13+
void Write(Stream stream);
14+
}
15+
16+
/// <summary>
17+
/// BitWritable is the base class for writable types
18+
/// </summary>
19+
public abstract class BitWritable
20+
{
21+
/// <summary>
22+
/// Writes the contents of the type instance to the stream
23+
/// </summary>
24+
/// <param name="stream">The stream to write to</param>
25+
public virtual void Write(Stream stream)
26+
{
27+
FieldInfo[] fields = SerializationHelper.GetFieldsForType(GetType());
28+
29+
using (PooledBitWriter writer = PooledBitWriter.Get(stream))
30+
{
31+
for (int i = 0; i < fields.Length; i++)
32+
{
33+
writer.WriteObjectPacked(fields[i].GetValue(this));
34+
}
35+
}
36+
}
37+
38+
/// <summary>
39+
/// Reads the contents from the stream and applies it to the type instance
40+
/// </summary>
41+
/// <param name="stream">The stream to read from</param>
42+
public virtual void Read(Stream stream)
43+
{
44+
FieldInfo[] fields = SerializationHelper.GetFieldsForType(GetType());
45+
46+
using (PooledBitReader reader = PooledBitReader.Get(stream))
47+
{
48+
for (int i = 0; i < fields.Length; i++)
49+
{
50+
fields[i].SetValue(this, reader.ReadObjectPacked(fields[i].FieldType));
51+
}
52+
}
53+
}
54+
}
55+
}

MLAPI/NetworkingManagerComponents/Binary/BitWriter.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -190,13 +190,13 @@ public void WriteObjectPacked(object value)
190190
WriteUInt16Packed(((NetworkedBehaviour)value).GetBehaviourId());
191191
return;
192192
}
193-
else if (value is IBitWritable)
193+
else if (value is BitWritable)
194194
{
195195
if (value == null)
196196
{
197197
throw new ArgumentException("BitWriter cannot write IBitWritable types with a null value");
198198
}
199-
((IBitWritable)value).Write(this.sink);
199+
((BitWritable)value).Write(this.sink);
200200
return;
201201
}
202202

MLAPI/NetworkingManagerComponents/Binary/IBitWritable.cs

Lines changed: 0 additions & 25 deletions
This file was deleted.
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Reflection;
5+
using UnityEngine;
6+
7+
namespace MLAPI.Serialization
8+
{
9+
/// <summary>
10+
/// Helper class providing helper serialization methods
11+
/// </summary>
12+
public static class SerializationHelper
13+
{
14+
private static readonly Dictionary<Type, FieldInfo[]> fieldCache = new Dictionary<Type, FieldInfo[]>();
15+
16+
internal static FieldInfo[] GetFieldsForType(Type type)
17+
{
18+
if (fieldCache.ContainsKey(type))
19+
return fieldCache[type];
20+
else
21+
{
22+
FieldInfo[] fields = type
23+
.GetFields(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance)
24+
.Where(x => (x.IsPublic || x.GetCustomAttributes(typeof(SerializeField), true).Length > 0) && IsTypeSupported(x.FieldType))
25+
.OrderBy(x => x.Name).ToArray();
26+
27+
fieldCache.Add(type, fields);
28+
29+
return fields;
30+
}
31+
}
32+
33+
private static readonly HashSet<Type> SupportedTypes = new HashSet<Type>()
34+
{
35+
typeof(byte),
36+
typeof(byte),
37+
typeof(sbyte),
38+
typeof(ushort),
39+
typeof(short),
40+
typeof(int),
41+
typeof(uint),
42+
typeof(long),
43+
typeof(ulong),
44+
typeof(float),
45+
typeof(double),
46+
typeof(string),
47+
typeof(bool),
48+
typeof(Vector2),
49+
typeof(Vector3),
50+
typeof(Vector4),
51+
typeof(Color),
52+
typeof(Color32),
53+
typeof(Ray),
54+
typeof(Quaternion),
55+
typeof(char),
56+
typeof(GameObject),
57+
typeof(NetworkedObject),
58+
typeof(NetworkedBehaviour)
59+
};
60+
61+
/// <summary>
62+
/// Returns if a type is supported for serialization
63+
/// </summary>
64+
/// <param name="type">The type to check</param>
65+
/// <returns>Whether or not the type is supported</returns>
66+
public static bool IsTypeSupported(Type type)
67+
{
68+
return type.IsEnum || SupportedTypes.Contains(type) || (typeof(BitWritable).IsAssignableFrom(type));
69+
}
70+
}
71+
}

0 commit comments

Comments
 (0)