Skip to content

Commit

Permalink
feat: support for jagged arrays
Browse files Browse the repository at this point in the history
  • Loading branch information
James-Frowen committed Mar 31, 2024
1 parent dc66a54 commit 2ec6d3a
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 18 deletions.
10 changes: 9 additions & 1 deletion Assets/Mirage/Weaver/SerializeFunctionBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<TypeReference> { elementType }, arrayMethod);
}
Expand Down
71 changes: 54 additions & 17 deletions Assets/Tests/Runtime/Serialization/NetworkWriterTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<int[][]>();

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()
{
Expand Down Expand Up @@ -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;
}
}
}

0 comments on commit 2ec6d3a

Please sign in to comment.