Skip to content

Commit

Permalink
feat: explicit declaration of network messages (#565)
Browse files Browse the repository at this point in the history
Normally MirrorNG detects what is a message and creates an id and serializer for it.
This PR allows you to explicitly declare a class or struct as a network message
This can be useful when you are making a generic class (such as Discovery)

to explicitly mark a class/struct as a network message you can do:
```cs
[NetworkMessage]
public struct MyMessage {
...
}
```
  • Loading branch information
paulpach committed Feb 3, 2021
1 parent b8fc97d commit b0610e2
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 1 deletion.
8 changes: 7 additions & 1 deletion Assets/Mirror/Weaver/Processors/ReaderWriterProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ public bool Process()
{
messages.Clear();


LoadBuiltinExtensions();
LoadBuiltinMessages();

Expand Down Expand Up @@ -118,6 +117,13 @@ void ProcessAssemblyClasses()
LoadDeclaredWriters(klass);
LoadDeclaredReaders(klass);
}

if (klass.GetCustomAttribute<NetworkMessageAttribute>() != null)
{
readers.GetReadFunc(klass, null);
writers.GetWriteFunc(klass, null);
messages.Add(klass);
}
}

// Generate readers and writers
Expand Down
6 changes: 6 additions & 0 deletions Assets/Tests/Weaver/GeneratedReaderWriterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@ public void CreatesForStructs()
IsSuccess();
}

[Test]
public void CreateForExplicitNetworkMessage()
{
IsSuccess();
}

[Test]
public void CreatesForClass()
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using Mirror;

namespace GeneratedReaderWriterTests.CreateForExplicitNetworkMessage
{
[NetworkMessage]
public class SomeOtherData
{
public int usefulNumber;
}
}

0 comments on commit b0610e2

Please sign in to comment.