diff --git a/Assets/Mirror/Editor/Weaver/Extensions.cs b/Assets/Mirror/Editor/Weaver/Extensions.cs index 0d211bce8..2897aaf35 100644 --- a/Assets/Mirror/Editor/Weaver/Extensions.cs +++ b/Assets/Mirror/Editor/Weaver/Extensions.cs @@ -122,10 +122,15 @@ public static bool CanBeResolved(this TypeReference parent) } - // Given a method of a generic class such as ArraySegment.get_Count, - // and a generic instance such as ArraySegment - // Creates a reference to the specialized method ArraySegment.get_Count - // Note that calling ArraySegment.get_Count directly gives an invalid IL error + /// + /// Given a method of a generic class such as ArraySegment`T.get_Count, + /// and a generic instance such as ArraySegment`int + /// Creates a reference to the specialized method ArraySegment`int`.get_Count + /// Note that calling ArraySegment`T.get_Count directly gives an invalid IL error + /// + /// + /// + /// public static MethodReference MakeHostInstanceGeneric(this MethodReference self, GenericInstanceType instanceType) { MethodReference reference = new MethodReference(self.Name, self.ReturnType, instanceType) diff --git a/Assets/Mirror/Editor/Weaver/Readers.cs b/Assets/Mirror/Editor/Weaver/Readers.cs index 6261ff286..a3de67513 100644 --- a/Assets/Mirror/Editor/Weaver/Readers.cs +++ b/Assets/Mirror/Editor/Weaver/Readers.cs @@ -36,7 +36,10 @@ public static MethodReference GetReadFunc(TypeReference variable, int recursionC if (variable.IsArray) { newReaderFunc = GenerateArrayReadFunc(variable, recursionCount); - RegisterReadFunc(variable.FullName, newReaderFunc); + if (newReaderFunc != null) + { + RegisterReadFunc(variable.FullName, newReaderFunc); + } return newReaderFunc; } @@ -121,6 +124,7 @@ static MethodDefinition GenerateArrayReadFunc(TypeReference variable, int recurs MethodReference elementReadFunc = GetReadFunc(elementType, recursionCount + 1); if (elementReadFunc == null) { + Weaver.Error($"Cannot generate reader for Array because element {elementType.Name} does not have a reader. Use a supported type or provide a custom reader", variable); return null; } @@ -214,6 +218,7 @@ static MethodDefinition GenerateArraySegmentReadFunc(TypeReference variable, int MethodReference elementReadFunc = GetReadFunc(elementType, recursionCount + 1); if (elementReadFunc == null) { + Weaver.Error($"Cannot generate reader for ArraySegment because element {elementType.Name} does not have a reader. Use a supported type or provide a custom reader", variable); return null; } diff --git a/Assets/Mirror/Editor/Weaver/Writers.cs b/Assets/Mirror/Editor/Weaver/Writers.cs index a2105bf63..ca0f2daa2 100644 --- a/Assets/Mirror/Editor/Weaver/Writers.cs +++ b/Assets/Mirror/Editor/Weaver/Writers.cs @@ -37,7 +37,10 @@ public static MethodReference GetWriteFunc(TypeReference variable, int recursion if (variable.IsArray) { newWriterFunc = GenerateArrayWriteFunc(variable, recursionCount); - RegisterWriteFunc(variable.FullName, newWriterFunc); + if (newWriterFunc != null) + { + RegisterWriteFunc(variable.FullName, newWriterFunc); + } return newWriterFunc; } @@ -196,6 +199,7 @@ static MethodDefinition GenerateArrayWriteFunc(TypeReference variable, int recur MethodReference elementWriteFunc = GetWriteFunc(elementType, recursionCount + 1); if (elementWriteFunc == null) { + Weaver.Error($"Cannot generate writer for Array because element {elementType.Name} does not have a writer. Use a supported type or provide a custom writer", variable); return null; } @@ -293,6 +297,7 @@ static MethodDefinition GenerateArraySegmentWriteFunc(TypeReference variable, in if (elementWriteFunc == null) { + Weaver.Error($"Cannot generate writer for ArraySegment because element {elementType.Name} does not have a writer. Use a supported type or provide a custom writer", variable); return null; } diff --git a/Assets/Mirror/Tests/Editor/Weaver/.WeaverTests.csproj b/Assets/Mirror/Tests/Editor/Weaver/.WeaverTests.csproj index 3d6d63445..335272168 100644 --- a/Assets/Mirror/Tests/Editor/Weaver/.WeaverTests.csproj +++ b/Assets/Mirror/Tests/Editor/Weaver/.WeaverTests.csproj @@ -1,4 +1,4 @@ - + netstandard2.0 _WeaverTests2.csproj @@ -64,6 +64,7 @@ + @@ -73,8 +74,13 @@ + + + + + diff --git a/Assets/Mirror/Tests/Editor/Weaver/GeneratedReaderWriter~/CreatesForArraySegment.cs b/Assets/Mirror/Tests/Editor/Weaver/GeneratedReaderWriter~/CreatesForArraySegment.cs new file mode 100644 index 000000000..d43da5bf7 --- /dev/null +++ b/Assets/Mirror/Tests/Editor/Weaver/GeneratedReaderWriter~/CreatesForArraySegment.cs @@ -0,0 +1,14 @@ +using System; +using Mirror; + +namespace GeneratedReaderWriter.CreatesForArraySegment +{ + public class CreatesForArraySegment : NetworkBehaviour + { + [ClientRpc] + public void RpcDoSomething(ArraySegment data) + { + // empty + } + } +} diff --git a/Assets/Mirror/Tests/Editor/Weaver/GeneratedReaderWriter~/CreatesForStructArraySegment.cs b/Assets/Mirror/Tests/Editor/Weaver/GeneratedReaderWriter~/CreatesForStructArraySegment.cs new file mode 100644 index 000000000..06e535949 --- /dev/null +++ b/Assets/Mirror/Tests/Editor/Weaver/GeneratedReaderWriter~/CreatesForStructArraySegment.cs @@ -0,0 +1,21 @@ +using System; +using Mirror; +using UnityEngine; + +namespace GeneratedReaderWriter.CreatesForStructArraySegment +{ + public class CreatesForStructArraySegment : NetworkBehaviour + { + [ClientRpc] + public void RpcDoSomething(ArraySegment data) + { + // empty + } + } + + public struct MyStruct + { + public int someValue; + public Vector3 anotherValue; + } +} diff --git a/Assets/Mirror/Tests/Editor/Weaver/GeneratedReaderWriter~/GivesErrorForInvalidArraySegmentType.cs b/Assets/Mirror/Tests/Editor/Weaver/GeneratedReaderWriter~/GivesErrorForInvalidArraySegmentType.cs new file mode 100644 index 000000000..d81c3ddbc --- /dev/null +++ b/Assets/Mirror/Tests/Editor/Weaver/GeneratedReaderWriter~/GivesErrorForInvalidArraySegmentType.cs @@ -0,0 +1,15 @@ +using System; +using Mirror; +using UnityEngine; + +namespace GeneratedReaderWriter.GivesErrorForInvalidArraySegmentType +{ + public class GivesErrorForInvalidArraySegmentType : NetworkBehaviour + { + [ClientRpc] + public void RpcDoSomething(ArraySegment data) + { + // empty + } + } +} diff --git a/Assets/Mirror/Tests/Editor/Weaver/GeneratedReaderWriter~/GivesErrorForInvalidArrayType.cs b/Assets/Mirror/Tests/Editor/Weaver/GeneratedReaderWriter~/GivesErrorForInvalidArrayType.cs new file mode 100644 index 000000000..a63124e9d --- /dev/null +++ b/Assets/Mirror/Tests/Editor/Weaver/GeneratedReaderWriter~/GivesErrorForInvalidArrayType.cs @@ -0,0 +1,14 @@ +using Mirror; +using UnityEngine; + +namespace GeneratedReaderWriter.GivesErrorForInvalidArrayType +{ + public class GivesErrorForInvalidArrayType : NetworkBehaviour + { + [ClientRpc] + public void RpcDoSomething(MonoBehaviour[] data) + { + // empty + } + } +} diff --git a/Assets/Mirror/Tests/Editor/Weaver/GeneratedReaderWriter~/GivesErrorForJaggedArray.cs b/Assets/Mirror/Tests/Editor/Weaver/GeneratedReaderWriter~/GivesErrorForJaggedArray.cs new file mode 100644 index 000000000..236c54e4e --- /dev/null +++ b/Assets/Mirror/Tests/Editor/Weaver/GeneratedReaderWriter~/GivesErrorForJaggedArray.cs @@ -0,0 +1,13 @@ +using Mirror; + +namespace GeneratedReaderWriter.GivesErrorForJaggedArray +{ + public class GivesErrorForJaggedArray : NetworkBehaviour + { + [ClientRpc] + public void RpcDoSomething(int[][] data) + { + // empty + } + } +} diff --git a/Assets/Mirror/Tests/Editor/Weaver/GeneratedReaderWriter~/GivesErrorForMultidimensionalArray.cs b/Assets/Mirror/Tests/Editor/Weaver/GeneratedReaderWriter~/GivesErrorForMultidimensionalArray.cs new file mode 100644 index 000000000..297fafa3f --- /dev/null +++ b/Assets/Mirror/Tests/Editor/Weaver/GeneratedReaderWriter~/GivesErrorForMultidimensionalArray.cs @@ -0,0 +1,13 @@ +using Mirror; + +namespace GeneratedReaderWriter.GivesErrorForMultidimensionalArray +{ + public class GivesErrorForMultidimensionalArray : NetworkBehaviour + { + [ClientRpc] + public void RpcDoSomething(int[,] data) + { + // empty + } + } +} diff --git a/Assets/Mirror/Tests/Editor/Weaver/WeaverGeneratedReaderWriterTests.cs b/Assets/Mirror/Tests/Editor/Weaver/WeaverGeneratedReaderWriterTests.cs index 8853c5fad..ecfa84d36 100644 --- a/Assets/Mirror/Tests/Editor/Weaver/WeaverGeneratedReaderWriterTests.cs +++ b/Assets/Mirror/Tests/Editor/Weaver/WeaverGeneratedReaderWriterTests.cs @@ -141,5 +141,43 @@ public void CreatesForEnums() { Assert.That(weaverErrors, Is.Empty); } + + [Test] + public void CreatesForArraySegment() + { + Assert.That(weaverErrors, Is.Empty); + } + + [Test] + public void CreatesForStructArraySegment() + { + Assert.That(weaverErrors, Is.Empty); + } + + [Test] + public void GivesErrorForJaggedArray() + { + Assert.That(weaverErrors, Contains.Item($"Int32[][] is an unsupported type. Jagged and multidimensional arrays are not supported (at System.Int32[][])")); + } + + [Test] + public void GivesErrorForMultidimensionalArray() + { + Assert.That(weaverErrors, Contains.Item($"Int32[0...,0...] is an unsupported type. Jagged and multidimensional arrays are not supported (at System.Int32[0...,0...])")); + } + + [Test] + public void GivesErrorForInvalidArrayType() + { + Assert.That(weaverErrors, Contains.Item("Cannot generate writer for Array because element MonoBehaviour does not have a writer. Use a supported type or provide a custom writer (at UnityEngine.MonoBehaviour[])")); + Assert.That(weaverErrors, Contains.Item("Cannot generate reader for Array because element MonoBehaviour does not have a reader. Use a supported type or provide a custom reader (at UnityEngine.MonoBehaviour[])")); + } + + [Test] + public void GivesErrorForInvalidArraySegmentType() + { + Assert.That(weaverErrors, Contains.Item("Cannot generate writer for ArraySegment because element MonoBehaviour does not have a writer. Use a supported type or provide a custom writer (at System.ArraySegment`1)")); + Assert.That(weaverErrors, Contains.Item("Cannot generate reader for ArraySegment because element MonoBehaviour does not have a reader. Use a supported type or provide a custom reader (at System.ArraySegment`1)")); + } } }