Skip to content

Commit

Permalink
feat: Support more nullable types (#3005)
Browse files Browse the repository at this point in the history
* feat: Support more nullable types
- NetworkWriter was also slightly rearraged to match NetworkReader

* fixed code smells

* fixed WriteVector4Nullable

* Got some of them working

* Couple more

* Couple more

* Added the rest

* reverted accidental change
  • Loading branch information
MrGadget1024 authored Nov 27, 2021
1 parent 8b363d1 commit 52111d1
Show file tree
Hide file tree
Showing 2 changed files with 241 additions and 55 deletions.
84 changes: 60 additions & 24 deletions Assets/Mirror/Runtime/NetworkReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -127,10 +127,19 @@ public static class NetworkReaderExtensions
static readonly UTF8Encoding encoding = new UTF8Encoding(false, true);

public static byte ReadByte(this NetworkReader reader) => reader.ReadByte();
public static byte? ReadByteNullable(this NetworkReader reader) => reader.ReadBool() ? ReadByte(reader) : default;

public static sbyte ReadSByte(this NetworkReader reader) => (sbyte)reader.ReadByte();
public static sbyte? ReadSByteNullable(this NetworkReader reader) => reader.ReadBool() ? ReadSByte(reader) : default;

public static char ReadChar(this NetworkReader reader) => (char)reader.ReadUShort();
public static char? ReadCharNullable(this NetworkReader reader) => reader.ReadBool() ? ReadChar(reader) : default;

public static bool ReadBool(this NetworkReader reader) => reader.ReadByte() != 0;
public static bool? ReadBoolNullable(this NetworkReader reader) => reader.ReadBool() ? ReadBool(reader) : default;

public static short ReadShort(this NetworkReader reader) => (short)reader.ReadUShort();
public static short? ReadShortNullable(this NetworkReader reader) => reader.ReadBool() ? ReadShort(reader) : default;

public static ushort ReadUShort(this NetworkReader reader)
{
Expand All @@ -139,8 +148,10 @@ public static ushort ReadUShort(this NetworkReader reader)
value |= (ushort)(reader.ReadByte() << 8);
return value;
}
public static ushort? ReadUShortNullable(this NetworkReader reader) => reader.ReadBool() ? ReadUShort(reader) : default;

public static int ReadInt(this NetworkReader reader) => (int)reader.ReadUInt();
public static int? ReadIntNullable(this NetworkReader reader) => reader.ReadBool() ? ReadInt(reader) : default;

public static uint ReadUInt(this NetworkReader reader)
{
Expand All @@ -151,8 +162,10 @@ public static uint ReadUInt(this NetworkReader reader)
value |= (uint)(reader.ReadByte() << 24);
return value;
}
public static uint? ReadUIntNullable(this NetworkReader reader) => reader.ReadBool() ? ReadUInt(reader) : default;

public static long ReadLong(this NetworkReader reader) => (long)reader.ReadULong();
public static long? ReadLongNullable(this NetworkReader reader) => reader.ReadBool() ? ReadLong(reader) : default;

public static ulong ReadULong(this NetworkReader reader)
{
Expand All @@ -167,27 +180,32 @@ public static ulong ReadULong(this NetworkReader reader)
value |= ((ulong)reader.ReadByte()) << 56;
return value;
}
public static ulong? ReadULongNullable(this NetworkReader reader) => reader.ReadBool() ? ReadULong(reader) : default;

public static float ReadFloat(this NetworkReader reader)
{
UIntFloat converter = new UIntFloat();
converter.intValue = reader.ReadUInt();
return converter.floatValue;
}
public static float? ReadFloatNullable(this NetworkReader reader) => reader.ReadBool() ? ReadFloat(reader) : default;

public static double ReadDouble(this NetworkReader reader)
{
UIntDouble converter = new UIntDouble();
converter.longValue = reader.ReadULong();
return converter.doubleValue;
}
public static double? ReadDoubleNullable(this NetworkReader reader) => reader.ReadBool() ? ReadDouble(reader) : default;

public static decimal ReadDecimal(this NetworkReader reader)
{
UIntDecimal converter = new UIntDecimal();
converter.longValue1 = reader.ReadULong();
converter.longValue2 = reader.ReadULong();
return converter.decimalValue;
}
public static decimal? ReadDecimalNullable(this NetworkReader reader) => reader.ReadBool() ? ReadDecimal(reader) : default;

/// <exception cref="T:System.ArgumentException">if an invalid utf8 string is sent</exception>
public static string ReadString(this NetworkReader reader)
Expand Down Expand Up @@ -223,6 +241,13 @@ public static byte[] ReadBytesAndSize(this NetworkReader reader)
return count == 0 ? null : reader.ReadBytes(checked((int)(count - 1u)));
}

public static byte[] ReadBytes(this NetworkReader reader, int count)
{
byte[] bytes = new byte[count];
reader.ReadBytes(bytes, count);
return bytes;
}

/// <exception cref="T:OverflowException">if count is invalid</exception>
public static ArraySegment<byte> ReadBytesAndSizeSegment(this NetworkReader reader)
{
Expand All @@ -234,24 +259,38 @@ public static ArraySegment<byte> ReadBytesAndSizeSegment(this NetworkReader read
}

public static Vector2 ReadVector2(this NetworkReader reader) => new Vector2(reader.ReadFloat(), reader.ReadFloat());
public static Vector2? ReadVector2Nullable(this NetworkReader reader) => reader.ReadBool() ? ReadVector2(reader) : default;

public static Vector3 ReadVector3(this NetworkReader reader) => new Vector3(reader.ReadFloat(), reader.ReadFloat(), reader.ReadFloat());
// TODO add nullable support to weaver instead
public static Vector3? ReadVector3Nullable(this NetworkReader reader) => reader.ReadBool() ? ReadVector3(reader) : default;

public static Vector4 ReadVector4(this NetworkReader reader) => new Vector4(reader.ReadFloat(), reader.ReadFloat(), reader.ReadFloat(), reader.ReadFloat());
public static Vector4? ReadVector4Nullable(this NetworkReader reader) => reader.ReadBool() ? ReadVector4(reader) : default;

public static Vector2Int ReadVector2Int(this NetworkReader reader) => new Vector2Int(reader.ReadInt(), reader.ReadInt());
public static Vector2Int? ReadVector2IntNullable(this NetworkReader reader) => reader.ReadBool() ? ReadVector2Int(reader) : default;

public static Vector3Int ReadVector3Int(this NetworkReader reader) => new Vector3Int(reader.ReadInt(), reader.ReadInt(), reader.ReadInt());
public static Vector3Int? ReadVector3IntNullable(this NetworkReader reader) => reader.ReadBool() ? ReadVector3Int(reader) : default;

public static Color ReadColor(this NetworkReader reader) => new Color(reader.ReadFloat(), reader.ReadFloat(), reader.ReadFloat(), reader.ReadFloat());
// TODO add nullable support to weaver instead
public static Color? ReadColorNullable(this NetworkReader reader) => reader.ReadBool() ? new Color(reader.ReadFloat(), reader.ReadFloat(), reader.ReadFloat(), reader.ReadFloat()) : default;

public static Color32 ReadColor32(this NetworkReader reader) => new Color32(reader.ReadByte(), reader.ReadByte(), reader.ReadByte(), reader.ReadByte());
// TODO add nullable support to weaver instead
public static Color32? ReadColor32Nullable(this NetworkReader reader) => reader.ReadBool() ? new Color32(reader.ReadByte(), reader.ReadByte(), reader.ReadByte(), reader.ReadByte()) : default;

public static Quaternion ReadQuaternion(this NetworkReader reader) => new Quaternion(reader.ReadFloat(), reader.ReadFloat(), reader.ReadFloat(), reader.ReadFloat());
// TODO add nullable support to weaver instead
public static Quaternion? ReadQuaternionNullable(this NetworkReader reader) => reader.ReadBool() ? ReadQuaternion(reader) : default;

public static Rect ReadRect(this NetworkReader reader) => new Rect(reader.ReadFloat(), reader.ReadFloat(), reader.ReadFloat(), reader.ReadFloat());
public static Rect? ReadRectNullable(this NetworkReader reader) => reader.ReadBool() ? ReadRect(reader) : default;

public static Plane ReadPlane(this NetworkReader reader) => new Plane(reader.ReadVector3(), reader.ReadFloat());
public static Plane? ReadPlaneNullable(this NetworkReader reader) => reader.ReadBool() ? ReadPlane(reader) : default;

public static Ray ReadRay(this NetworkReader reader) => new Ray(reader.ReadVector3(), reader.ReadVector3());
public static Ray? ReadRayNullable(this NetworkReader reader) => reader.ReadBool() ? ReadRay(reader) : default;

public static Matrix4x4 ReadMatrix4x4(this NetworkReader reader)
{
return new Matrix4x4
Expand All @@ -274,27 +313,10 @@ public static Matrix4x4 ReadMatrix4x4(this NetworkReader reader)
m33 = reader.ReadFloat()
};
}
public static byte[] ReadBytes(this NetworkReader reader, int count)
{
byte[] bytes = new byte[count];
reader.ReadBytes(bytes, count);
return bytes;
}
public static Guid ReadGuid(this NetworkReader reader) => new Guid(reader.ReadBytes(16));

public static Transform ReadTransform(this NetworkReader reader)
{
// Don't use null propagation here as it could lead to MissingReferenceException
NetworkIdentity networkIdentity = reader.ReadNetworkIdentity();
return networkIdentity != null ? networkIdentity.transform : null;
}
public static Matrix4x4? ReadMatrix4x4Nullable(this NetworkReader reader) => reader.ReadBool() ? ReadMatrix4x4(reader) : default;

public static GameObject ReadGameObject(this NetworkReader reader)
{
// Don't use null propagation here as it could lead to MissingReferenceException
NetworkIdentity networkIdentity = reader.ReadNetworkIdentity();
return networkIdentity != null ? networkIdentity.gameObject : null;
}
public static Guid ReadGuid(this NetworkReader reader) => new Guid(reader.ReadBytes(16));
public static Guid? ReadGuidNullable(this NetworkReader reader) => reader.ReadBool() ? ReadGuid(reader) : Guid.Empty;

public static NetworkIdentity ReadNetworkIdentity(this NetworkReader reader)
{
Expand Down Expand Up @@ -355,6 +377,20 @@ public static NetworkBehaviour.NetworkBehaviourSyncVar ReadNetworkBehaviourSyncV
return new NetworkBehaviour.NetworkBehaviourSyncVar(netId, componentIndex);
}

public static Transform ReadTransform(this NetworkReader reader)
{
// Don't use null propagation here as it could lead to MissingReferenceException
NetworkIdentity networkIdentity = reader.ReadNetworkIdentity();
return networkIdentity != null ? networkIdentity.transform : null;
}

public static GameObject ReadGameObject(this NetworkReader reader)
{
// Don't use null propagation here as it could lead to MissingReferenceException
NetworkIdentity networkIdentity = reader.ReadNetworkIdentity();
return networkIdentity != null ? networkIdentity.gameObject : null;
}

public static List<T> ReadList<T>(this NetworkReader reader)
{
int length = reader.ReadInt();
Expand Down
Loading

0 comments on commit 52111d1

Please sign in to comment.