Skip to content

Commit

Permalink
fix: weaver support array of custom types (#1470)
Browse files Browse the repository at this point in the history
* WIP

* Proposed Rearrange

* Rearrange Code

* Unit test for PR

* unit test

* unit test

* Update TestingScriptableObjectArraySerialization.cs

Improving code

* Update TestingScriptableObjectArraySerialization.cs

* Remove blank lines

* remove blank space

* Remove leftover log

Co-authored-by: MrGadget <chris@clevertech.net>
Co-authored-by: Uchiha I_A_H_I <jkaran.sharma101@gmail.com>
  • Loading branch information
3 people committed Feb 3, 2020
1 parent 1736bb0 commit d0b0bc9
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 14 deletions.
21 changes: 14 additions & 7 deletions Assets/Mirror/Editor/Weaver/Readers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down Expand Up @@ -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);
}
Expand Down
21 changes: 14 additions & 7 deletions Assets/Mirror/Editor/Weaver/Writers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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??
Expand Down Expand Up @@ -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);
}
Expand Down
7 changes: 7 additions & 0 deletions Assets/Mirror/Tests/Editor/WeaverTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}
Original file line number Diff line number Diff line change
@@ -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
}
}
}

0 comments on commit d0b0bc9

Please sign in to comment.