feat(Reader/Writer): Added LayerMask Support

- includes normal and nullable
- includes unit tests
This commit is contained in:
MrGadget1024 2023-11-21 02:26:22 -05:00
parent fd049fc67d
commit eb66d81c77
3 changed files with 49 additions and 0 deletions

View File

@ -150,6 +150,18 @@ public static ArraySegment<byte> ReadArraySegmentAndSize(this NetworkReader read
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(Ray?);
// LayerMask is a struct with properties instead of fields
public static LayerMask ReadLayerMask(this NetworkReader reader)
{
// LayerMask doesn't have a constructor that takes an initial value.
// 32 layers as a flags enum, max value of 496, we only need a UShort.
LayerMask layerMask = default;
layerMask.value = reader.ReadUShort();
return layerMask;
}
public static LayerMask? ReadLayerMaskNullable(this NetworkReader reader) => reader.ReadBool() ? ReadLayerMask(reader) : default(LayerMask?);
public static Matrix4x4 ReadMatrix4x4(this NetworkReader reader) => reader.ReadBlittable<Matrix4x4>();
public static Matrix4x4? ReadMatrix4x4Nullable(this NetworkReader reader) => reader.ReadBlittableNullable<Matrix4x4>();

View File

@ -186,6 +186,19 @@ public static void WriteRayNullable(this NetworkWriter writer, Ray? value)
writer.WriteRay(value.Value);
}
// LayerMask is a struct with properties instead of fields
public static void WriteLayerMask(this NetworkWriter writer, LayerMask layerMask)
{
// 32 layers as a flags enum, max value of 496, we only need a UShort.
writer.WriteUShort((ushort)layerMask.value);
}
public static void WriteLayerMaskNullable(this NetworkWriter writer, LayerMask? layerMask)
{
writer.WriteBool(layerMask.HasValue);
if (layerMask.HasValue)
writer.WriteLayerMask(layerMask.Value);
}
public static void WriteMatrix4x4(this NetworkWriter writer, Matrix4x4 value) => writer.WriteBlittable(value);
public static void WriteMatrix4x4Nullable(this NetworkWriter writer, Matrix4x4? value) => writer.WriteBlittableNullable(value);

View File

@ -704,6 +704,30 @@ public void TestRayNullable()
Assert.That(output, Is.EqualTo(input));
}
[Test]
public void TestLayerMask()
{
LayerMask originalLayerMask = new LayerMask();
originalLayerMask.value = 42;
NetworkWriter writer = new NetworkWriter();
writer.WriteLayerMask(originalLayerMask);
NetworkReader reader = new NetworkReader(writer.ToArray());
LayerMask readLayerMask = reader.ReadLayerMask();
Assert.That(readLayerMask, Is.EqualTo(originalLayerMask));
}
[Test]
public void TestLayerMaskNullable()
{
LayerMask? input = null;
NetworkWriter writer = new NetworkWriter();
writer.WriteLayerMaskNullable(input);
NetworkReader reader = new NetworkReader(writer.ToArray());
LayerMask? output = reader.ReadLayerMaskNullable();
Assert.That(output, Is.EqualTo(input));
}
[Test]
public void TestMatrix4x4()
{