Skip to content

Commit

Permalink
feat: support Jagged arrays
Browse files Browse the repository at this point in the history
simplify the code for checking for multidimentional arrays
and lift the restriction on jagged arrays
  • Loading branch information
paulpach committed Sep 30, 2020
1 parent 5e2d876 commit 0267dbe
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 14 deletions.
9 changes: 2 additions & 7 deletions Assets/Mirror/Editor/Weaver/Extensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,14 +79,9 @@ public static bool ImplementsInterface<TInterface>(this TypeDefinition td)
return false;
}

public static bool IsArrayType(this TypeReference tr)
public static bool IsMultidimensionalArray(this TypeReference tr)
{
// jagged array
if ((tr.IsArray && ((ArrayType)tr).ElementType.IsArray) ||
// multidimensional array
(tr.IsArray && ((ArrayType)tr).Rank > 1))
return false;
return true;
return tr is ArrayType arrayType && arrayType.Rank > 1;
}

public static bool CanBeResolved(this TypeReference parent)
Expand Down
4 changes: 2 additions & 2 deletions Assets/Mirror/Editor/Weaver/Readers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,9 @@ static void RegisterReadFunc(TypeReference typeReference, MethodDefinition newRe

static MethodDefinition GenerateArrayReadFunc(TypeReference variable)
{
if (!variable.IsArrayType())
if (variable.IsMultidimensionalArray())
{
Weaver.Error($"{variable.Name} is an unsupported type. Jagged and multidimensional arrays are not supported", variable);
Weaver.Error($"{variable.Name} is an unsupported type. Multidimensional arrays are not supported", variable);
return null;
}

Expand Down
4 changes: 2 additions & 2 deletions Assets/Mirror/Editor/Weaver/Writers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -230,9 +230,9 @@ static bool WriteAllFields(TypeReference variable, ILProcessor worker)

static MethodDefinition GenerateArrayWriteFunc(TypeReference variable)
{
if (!variable.IsArrayType())
if (variable.IsMultidimensionalArray())
{
throw new GenerateWriterException($"{variable.Name} is an unsupported type. Jagged and multidimensional arrays are not supported", variable);
throw new GenerateWriterException($"{variable.Name} is an unsupported type. Multidimensional arrays are not supported", variable);
}
MethodDefinition writerFunc = GenerateWriterFunc(variable);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -184,14 +184,13 @@ public void CreatesForStructArraySegment()
[Test]
public void GivesErrorForJaggedArray()
{
HasError("Int32[][] is an unsupported type. Jagged and multidimensional arrays are not supported",
"System.Int32[][]");
IsSuccess();
}

[Test]
public void GivesErrorForMultidimensionalArray()
{
HasError("Int32[0...,0...] is an unsupported type. Jagged and multidimensional arrays are not supported",
HasError("Int32[0...,0...] is an unsupported type. Multidimensional arrays are not supported",
"System.Int32[0...,0...]");
}

Expand Down

0 comments on commit 0267dbe

Please sign in to comment.