Skip to content

Commit

Permalink
feat: Added array serialization support
Browse files Browse the repository at this point in the history
  • Loading branch information
TwoTenPvP committed May 26, 2019
1 parent 2e09de3 commit bd322df
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 11 deletions.
13 changes: 13 additions & 0 deletions MLAPI/Serialization/BitReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,19 @@ public object ReadObjectPacked(Type type)

if (SerializationManager.TryDeserialize(source, type, out object obj))
return obj;
if (type.IsArray && type.HasElementType)
{
int size = ReadInt32Packed();

Array array = Array.CreateInstance(type.GetElementType(), size);

for (int i = 0; i < size; i++)
{
array.SetValue(ReadObjectPacked(type.GetElementType()), i);
}

return array;
}
if (type == typeof(byte))
return ReadByteDirect();
if (type == typeof(sbyte))
Expand Down
34 changes: 25 additions & 9 deletions MLAPI/Serialization/BitWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,22 @@ public void WriteObjectPacked(object value)
{
return;
}
else if (value is Array array)
{
Type elementType = value.GetType().GetElementType();

if (SerializationManager.IsTypeSupported(elementType))
{
WriteInt32Packed(array.Length);

for (int i = 0; i < array.Length; i++)
{
WriteObjectPacked(array.GetValue(i));
}

return;
}
}
else if (value is byte)
{
WriteByte((byte)value);
Expand Down Expand Up @@ -161,19 +177,19 @@ public void WriteObjectPacked(object value)
WriteCharPacked((char)value);
return;
}
else if (value.GetType().IsEnum)
else if (value.GetType().IsEnum)
{
WriteInt32Packed((int)value);
return;
}
else if (value is GameObject)
else if (value is GameObject)
{
NetworkedObject networkedObject = ((GameObject)value).GetComponent<NetworkedObject>();
if(networkedObject == null)
if (networkedObject == null)
{
throw new ArgumentException("BitWriter cannot write GameObject types that does not has a NetworkedObject component attached. GameObject: " + ((GameObject)value).name);
}
else
}
else
{
WriteUInt64Packed(networkedObject.NetworkId);
}
Expand All @@ -183,19 +199,19 @@ public void WriteObjectPacked(object value)
{
WriteUInt64Packed(((NetworkedObject)value).NetworkId);
return;
}
}
else if (value is NetworkedBehaviour)
{
WriteUInt64Packed(((NetworkedBehaviour)value).NetworkId);
WriteUInt16Packed(((NetworkedBehaviour)value).GetBehaviourId());
return;
}
}
else if (value is IBitWritable)
{
((IBitWritable)value).Write(this.sink);
return;
}
}


throw new ArgumentException("BitWriter cannot write type " + value.GetType().Name);
}
Expand Down
5 changes: 3 additions & 2 deletions MLAPI/Serialization/SerializationManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -141,8 +141,9 @@ internal static FieldInfo[] GetFieldsForType(Type type)
/// <returns>Whether or not the type is supported</returns>
public static bool IsTypeSupported(Type type)
{
return type.IsEnum || SupportedTypes.Contains(type) || type.HasInterface(typeof(IBitWritable)) ||
(cachedExternalSerializers.ContainsKey(type) && cachedExternalDeserializers.ContainsKey(type));
return type.IsEnum || SupportedTypes.Contains(type) || type.HasInterface(typeof(IBitWritable)) ||
(cachedExternalSerializers.ContainsKey(type) && cachedExternalDeserializers.ContainsKey(type)) ||
(type.IsArray && type.HasElementType && IsTypeSupported(type.GetElementType()));
}
}
}

0 comments on commit bd322df

Please sign in to comment.