diff --git a/Assets/Mirror/Editor/Weaver/Readers.cs b/Assets/Mirror/Editor/Weaver/Readers.cs index f4959501f..8c040cc4a 100644 --- a/Assets/Mirror/Editor/Weaver/Readers.cs +++ b/Assets/Mirror/Editor/Weaver/Readers.cs @@ -27,6 +27,19 @@ public static MethodReference GetReadFunc(TypeReference variable, int recursionC return foundFunc; } + MethodDefinition newReaderFunc; + + // Arrays are special, if we resolve them, we get teh element type, + // so the following ifs might choke on it for scriptable objects + // or other objects that require a custom serializer + // thus check if it is an array and skip all the checks. + if (variable.IsArray) + { + newReaderFunc = GenerateArrayReadFunc(variable, recursionCount); + RegisterReadFunc(variable.FullName, newReaderFunc); + return newReaderFunc; + } + TypeDefinition td = variable.Resolve(); if (td == null) { @@ -60,13 +73,7 @@ public static MethodReference GetReadFunc(TypeReference variable, int recursionC return null; } - MethodDefinition newReaderFunc; - - if (variable.IsArray) - { - newReaderFunc = GenerateArrayReadFunc(variable, recursionCount); - } - else if (td.IsEnum) + if (td.IsEnum) { return GetReadFunc(td.GetEnumUnderlyingType(), recursionCount); } diff --git a/Assets/Mirror/Editor/Weaver/Writers.cs b/Assets/Mirror/Editor/Weaver/Writers.cs index 82a7328e9..57ec29547 100644 --- a/Assets/Mirror/Editor/Weaver/Writers.cs +++ b/Assets/Mirror/Editor/Weaver/Writers.cs @@ -28,6 +28,19 @@ public static MethodReference GetWriteFunc(TypeReference variable, int recursion return foundFunc; } + MethodDefinition newWriterFunc; + + // Arrays are special, if we resolve them, we get the element type, + // so the following ifs might choke on it for scriptable objects + // or other objects that require a custom serializer + // thus check if it is an array and skip all the checks. + if (variable.IsArray) + { + newWriterFunc = GenerateArrayWriteFunc(variable, recursionCount); + RegisterWriteFunc(variable.FullName, newWriterFunc); + return newWriterFunc; + } + if (variable.IsByReference) { // error?? @@ -61,13 +74,7 @@ public static MethodReference GetWriteFunc(TypeReference variable, int recursion return null; } - MethodDefinition newWriterFunc; - - if (variable.IsArray) - { - newWriterFunc = GenerateArrayWriteFunc(variable, recursionCount); - } - else if (variable.Resolve().IsEnum) + if (variable.Resolve().IsEnum) { return GetWriteFunc(variable.Resolve().GetEnumUnderlyingType(), recursionCount); } diff --git a/Assets/Mirror/Tests/Editor/WeaverTest.cs b/Assets/Mirror/Tests/Editor/WeaverTest.cs index a7220331e..bfa7ef747 100644 --- a/Assets/Mirror/Tests/Editor/WeaverTest.cs +++ b/Assets/Mirror/Tests/Editor/WeaverTest.cs @@ -728,5 +728,12 @@ public void MessageMemberInterface() Assert.That(weaverErrors, Contains.Item("Mirror.Weaver error: Cannot generate writer for interface MirrorTest.SuperCoolInterface. Use a concrete type or provide a custom writer")); } #endregion + + [Test] + public void TestingScriptableObjectArraySerialization() + { + UnityEngine.Debug.Log(string.Join("\n",weaverErrors)); + Assert.That(CompilationFinishedHook.WeaveFailed, Is.False); + } } } diff --git a/Assets/Mirror/Tests/Editor/WeaverTests~/TestingScriptableObjectArraySerialization.cs b/Assets/Mirror/Tests/Editor/WeaverTests~/TestingScriptableObjectArraySerialization.cs new file mode 100644 index 000000000..dd7f6b4ec --- /dev/null +++ b/Assets/Mirror/Tests/Editor/WeaverTests~/TestingScriptableObjectArraySerialization.cs @@ -0,0 +1,38 @@ +using Mirror; +using UnityEngine; + +namespace MirrorTest +{ + public static class CustomSerializer + { + public static void Writedata(this NetworkWriter writer, Data arg) + { + writer.WriteInt32(arg.Var1); + } + + public static Data Readdata(this NetworkReader reader) + { + return new Data + { + Var1 = reader.ReadInt32() + }; + } + } + + public class Data : ScriptableObject + { + public int Var1; + } + + public class PlayerScript : NetworkBehaviour + { + [Command] + public void + CmdwriteArraydata( + Data[] arg) //This gonna give error saying-- Mirror.Weaver error: Cannot generate writer for scriptable object Data[]. Use a supported type or provide a custom writer + { + + //some code + } + } +}