diff --git a/Assets/Mirage/Weaver/SerializeFunctionBase.cs b/Assets/Mirage/Weaver/SerializeFunctionBase.cs index 0f87210308..71a73ff358 100644 --- a/Assets/Mirage/Weaver/SerializeFunctionBase.cs +++ b/Assets/Mirage/Weaver/SerializeFunctionBase.cs @@ -162,7 +162,15 @@ private MethodReference GenerateFunction(TypeReference typeReference) { throw new SerializeFunctionException($"{typeReference.Name} is an unsupported type. Multidimensional arrays are not supported", typeReference); } - var elementType = typeReference.GetElementType(); + + TypeReference elementType; + if (typeReference is ArrayType arrayType) + // need to use ArrayType to support jagged arrays + elementType = arrayType.ElementType; + else + // fallback to GetElementType just incase + elementType = typeReference.GetElementType(); + var arrayMethod = module.ImportReference(ArrayExpression); return GenerateCollectionFunction(typeReference, new List { elementType }, arrayMethod); } diff --git a/Assets/Tests/Runtime/Serialization/NetworkWriterTest.cs b/Assets/Tests/Runtime/Serialization/NetworkWriterTest.cs index b5161f449d..b3ef0c852c 100644 --- a/Assets/Tests/Runtime/Serialization/NetworkWriterTest.cs +++ b/Assets/Tests/Runtime/Serialization/NetworkWriterTest.cs @@ -1173,6 +1173,38 @@ public void Dictionary() Assert.That(unpacked["left"], Is.EqualTo(Vector3.left)); } + [Test] + [TestCase(2, 2)] + [TestCase(2, 3)] + [TestCase(2, 5)] + [TestCase(4, 5)] + public void CanWriteJaggedArray(int size1, int size2) + { + var array = new int[size1][]; + for (var i = 0; i < array.Length; i++) + { + array[i] = new int[size2]; + for (var j = 0; j < array[i].Length; j++) + array[i][j] = (i * 10) + j; + } + + writer.Write(array); + reader.Reset(writer.ToArraySegment()); + var unpacked = reader.Read(); + + Assert.That(unpacked, Is.Not.Null); + Assert.That(unpacked.Length == array.Length); + for (var i = 0; i < array.Length; i++) + { + Assert.That(unpacked[i].Length == array[i].Length); + for (var j = 0; j < array[i].Length; j++) + { + Debug.Log(unpacked[i][j]); + Assert.That(unpacked[i][j] == array[i][j]); + } + } + } + [Test] public void SByteLength() { @@ -1334,26 +1366,31 @@ public void DoesNotWriteProtectedField() Assert.That(outValue.Field3, Is.EqualTo(default(int))); Assert.That(outValue.Field4, Is.EqualTo(default(int))); } - } - [NetworkMessage] - public class _ClassWithProtected - { - // should serialize - public int Field1; + [NetworkMessage] + public class _ClassWithProtected + { + // should serialize + public int Field1; - // should NOT serialize - protected int _field2; - private int _field3; - internal int Field4; + // should NOT serialize + protected int _field2; + private int _field3; + internal int Field4; - // accessors for test - public int Field2 { get => _field2; set => _field2 = value; } - public int Field3 { get => _field3; set => _field3 = value; } - } + // accessors for test + public int Field2 { get => _field2; set => _field2 = value; } + public int Field3 { get => _field3; set => _field3 = value; } + } - public struct _MessageWithProtected - { - public _ClassWithProtected Field; + public struct _MessageWithProtected + { + public _ClassWithProtected Field; + } + + public struct _MessageWithJaggedArray + { + public int[][] array; + } } }