Skip to content

Commit

Permalink
feat: Exclude fields from weaver's automatic Read/Write using System.…
Browse files Browse the repository at this point in the history
…NonSerialized attribute (#1727)

* adding test class for NonSerialized

* checking for IsNotSerialized
  • Loading branch information
James-Frowen committed Apr 23, 2020
1 parent e6881ef commit 7f8733c
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 1 deletion.
3 changes: 3 additions & 0 deletions Assets/Mirror/Editor/Weaver/Readers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -381,6 +381,9 @@ private static void DeserializeFields(TypeReference variable, int recursionCount
if (field.IsStatic || field.IsPrivate)
continue;

if (field.IsNotSerialized)
continue;

// mismatched ldloca/ldloc for struct/class combinations is invalid IL, which causes crash at runtime
OpCode opcode = variable.IsValueType ? OpCodes.Ldloca : OpCodes.Ldloc;
worker.Append(worker.Create(opcode, 0));
Expand Down
3 changes: 3 additions & 0 deletions Assets/Mirror/Editor/Weaver/Writers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,9 @@ static MethodDefinition GenerateClassOrStructWriterFunction(TypeReference variab
if (field.IsStatic || field.IsPrivate)
continue;

if (field.IsNotSerialized)
continue;

MethodReference writeFunc = GetWriteFunc(field.FieldType, recursionCount + 1);
if (writeFunc != null)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using Mirror;
using Mirror.Weaver.Tests.Extra;

namespace MirrorTest
{
public class ExcludesNonSerializedFields : NetworkBehaviour
{
[ClientRpc]
public void RpcDoSomething(SomeDataUsingNonSerialized data)
{
// empty
}
}

public struct SomeDataUsingNonSerialized
{
public int usefulNumber;
// Object is a not allowed type
[System.NonSerialized] public UnityEngine.Object obj;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,6 @@ public void GivesErrorWhenUsingTypeInheritedFromMonoBehaviour()
}

[Test]
[Ignore("Not Implemented")]
public void ExcludesNonSerializedFields()
{
// we test this by having a not allowed type in the class, but mark it with NonSerialized
Expand Down

0 comments on commit 7f8733c

Please sign in to comment.