From 566c41733d57ee6aa9fcf8af4e2e4dc783af5e26 Mon Sep 17 00:00:00 2001 From: vis2k Date: Thu, 30 Jul 2020 22:38:30 +0200 Subject: [PATCH] so far --- .../Authenticators/BasicAuthenticator.cs | 36 +++++++++++-------- .../Components/Discovery/ServerRequest.cs | 7 +++- .../Components/Discovery/ServerResponse.cs | 8 +++-- .../Chat/Scripts/ChatNetworkManager.cs | 6 +++- Assets/Mirror/Runtime/Messages.cs | 1 + .../Tests/Editor/ArraySegmentWriterTest.cs | 13 +++++-- Assets/Mirror/Tests/Editor/ArrayWriterTest.cs | 12 +++++-- .../Tests/Editor/BasicAuthenticatorTest.cs | 13 +------ Assets/Mirror/Tests/Editor/CustomRWTest.cs | 9 +++-- .../Mirror/Tests/Editor/Mirror.Tests.asmdef | 1 + .../Tests/Editor/StructMessagesTests.cs | 2 +- 11 files changed, 69 insertions(+), 39 deletions(-) diff --git a/Assets/Mirror/Authenticators/BasicAuthenticator.cs b/Assets/Mirror/Authenticators/BasicAuthenticator.cs index 1f0a4ca77..f8e45c688 100644 --- a/Assets/Mirror/Authenticators/BasicAuthenticator.cs +++ b/Assets/Mirror/Authenticators/BasicAuthenticator.cs @@ -3,6 +3,28 @@ namespace Mirror.Authenticators { + public struct AuthRequestMessage : IMessageBase + { + // use whatever credentials make sense for your game + // for example, you might want to pass the accessToken if using oauth + public string authUsername; + public string authPassword; + + // Weaver will generate serialization + public void Serialize(NetworkWriter writer) {} + public void Deserialize(NetworkReader reader) {} + } + + public struct AuthResponseMessage : IMessageBase + { + public byte code; + public string message; + + // Weaver will generate serialization + public void Serialize(NetworkWriter writer) {} + public void Deserialize(NetworkReader reader) {} + } + [AddComponentMenu("Network/Authenticators/BasicAuthenticator")] public class BasicAuthenticator : NetworkAuthenticator { @@ -14,20 +36,6 @@ public class BasicAuthenticator : NetworkAuthenticator public string username; public string password; - public class AuthRequestMessage : MessageBase - { - // use whatever credentials make sense for your game - // for example, you might want to pass the accessToken if using oauth - public string authUsername; - public string authPassword; - } - - public class AuthResponseMessage : MessageBase - { - public byte code; - public string message; - } - public override void OnStartServer() { // register a handler for the authentication request we expect from client diff --git a/Assets/Mirror/Components/Discovery/ServerRequest.cs b/Assets/Mirror/Components/Discovery/ServerRequest.cs index 3ac59cfa4..a70a603b2 100644 --- a/Assets/Mirror/Components/Discovery/ServerRequest.cs +++ b/Assets/Mirror/Components/Discovery/ServerRequest.cs @@ -1,4 +1,9 @@ namespace Mirror.Discovery { - public class ServerRequest : MessageBase { } + public struct ServerRequest : IMessageBase + { + // Weaver will generate serialization + public void Serialize(NetworkWriter writer) {} + public void Deserialize(NetworkReader reader) {} + } } diff --git a/Assets/Mirror/Components/Discovery/ServerResponse.cs b/Assets/Mirror/Components/Discovery/ServerResponse.cs index 34173e532..0cac4d6a4 100644 --- a/Assets/Mirror/Components/Discovery/ServerResponse.cs +++ b/Assets/Mirror/Components/Discovery/ServerResponse.cs @@ -3,7 +3,7 @@ namespace Mirror.Discovery { - public class ServerResponse : MessageBase + public struct ServerResponse : IMessageBase { // The server that sent this // this is a property so that it is not serialized, but the @@ -14,5 +14,9 @@ public class ServerResponse : MessageBase // Prevent duplicate server appearance when a connection can be made via LAN on multiple NICs public long serverId; + + // Weaver will generate serialization + public void Serialize(NetworkWriter writer) {} + public void Deserialize(NetworkReader reader) {} } -} \ No newline at end of file +} diff --git a/Assets/Mirror/Examples/Chat/Scripts/ChatNetworkManager.cs b/Assets/Mirror/Examples/Chat/Scripts/ChatNetworkManager.cs index d4a836125..eae046236 100644 --- a/Assets/Mirror/Examples/Chat/Scripts/ChatNetworkManager.cs +++ b/Assets/Mirror/Examples/Chat/Scripts/ChatNetworkManager.cs @@ -14,9 +14,13 @@ public void SetHostname(string hostname) public ChatWindow chatWindow; - public class CreatePlayerMessage : MessageBase + public struct CreatePlayerMessage : IMessageBase { public string name; + + // Weaver will generate serialization + public void Serialize(NetworkWriter writer) {} + public void Deserialize(NetworkReader reader) {} } public override void OnStartServer() diff --git a/Assets/Mirror/Runtime/Messages.cs b/Assets/Mirror/Runtime/Messages.cs index 57fef86e4..b8557e4e4 100644 --- a/Assets/Mirror/Runtime/Messages.cs +++ b/Assets/Mirror/Runtime/Messages.cs @@ -10,6 +10,7 @@ public interface IMessageBase void Serialize(NetworkWriter writer); } + [Obsolete("Please make your message a struct, implement IMessageBase and add empty OnSerialize/OnDeserialize methods. MessageBase was for classes, and we should never send classes to avoid runtime allocations and redundant code. We only use structs in the future. OnSerialize/OnDeserialize content is still auto generated.")] public abstract class MessageBase : IMessageBase { // De-serialize the contents of the reader into this message diff --git a/Assets/Mirror/Tests/Editor/ArraySegmentWriterTest.cs b/Assets/Mirror/Tests/Editor/ArraySegmentWriterTest.cs index cd5ac212d..0681949d0 100644 --- a/Assets/Mirror/Tests/Editor/ArraySegmentWriterTest.cs +++ b/Assets/Mirror/Tests/Editor/ArraySegmentWriterTest.cs @@ -11,9 +11,13 @@ public class ArraySegmentWriterTest // ArraySegment is a special case, optimized for no copy and no allocation // other types are generated by the weaver - class ByteArraySegmentMessage : MessageBase + struct ByteArraySegmentMessage : IMessageBase { public ArraySegment array; + + // Weaver will generate serialization + public void Serialize(NetworkWriter writer) {} + public void Deserialize(NetworkReader reader) {} } [Test] @@ -84,9 +88,13 @@ public void TestSegmentByteArray() #region ArraySegment - class IntArraySegmentMessage : MessageBase + struct IntArraySegmentMessage : IMessageBase { public ArraySegment array; + + // Weaver will generate serialization + public void Serialize(NetworkWriter writer) {} + public void Deserialize(NetworkReader reader) {} } [Test] @@ -140,6 +148,5 @@ public void TestSegmentIntArray() Assert.That(unpacked.array, Is.EquivalentTo(new int[] { 3, 4 })); } #endregion - } } diff --git a/Assets/Mirror/Tests/Editor/ArrayWriterTest.cs b/Assets/Mirror/Tests/Editor/ArrayWriterTest.cs index 4f409ffff..fffbe22da 100644 --- a/Assets/Mirror/Tests/Editor/ArrayWriterTest.cs +++ b/Assets/Mirror/Tests/Editor/ArrayWriterTest.cs @@ -4,9 +4,13 @@ namespace Mirror.Tests [TestFixture] public class ArrayWriterTest { - class ArrayByteMessage : MessageBase + struct ArrayByteMessage : IMessageBase { public byte[] array; + + // Weaver will generate serialization + public void Serialize(NetworkWriter writer) {} + public void Deserialize(NetworkReader reader) {} } [Test] @@ -58,9 +62,13 @@ public void TestDataByteArray() Assert.That(unpacked.array, Is.EquivalentTo(new byte[] { 3, 4, 5 })); } - class ArrayIntMessage : MessageBase + struct ArrayIntMessage : IMessageBase { public int[] array; + + // Weaver will generate serialization + public void Serialize(NetworkWriter writer) {} + public void Deserialize(NetworkReader reader) {} } [Test] diff --git a/Assets/Mirror/Tests/Editor/BasicAuthenticatorTest.cs b/Assets/Mirror/Tests/Editor/BasicAuthenticatorTest.cs index d31ceb21a..cc56054f3 100644 --- a/Assets/Mirror/Tests/Editor/BasicAuthenticatorTest.cs +++ b/Assets/Mirror/Tests/Editor/BasicAuthenticatorTest.cs @@ -1,22 +1,11 @@ using NUnit.Framework; +using Mirror.Authenticators; namespace Mirror.Tests { [TestFixture] public class BasicAuthenticatorTest { - public class AuthRequestMessage : MessageBase - { - public string authUsername; - public string authPassword; - } - - public class AuthResponseMessage : MessageBase - { - public byte code; - public string message; - } - [Test] public void AuthRequestMessageTest() { diff --git a/Assets/Mirror/Tests/Editor/CustomRWTest.cs b/Assets/Mirror/Tests/Editor/CustomRWTest.cs index 83464592b..d7363278b 100644 --- a/Assets/Mirror/Tests/Editor/CustomRWTest.cs +++ b/Assets/Mirror/Tests/Editor/CustomRWTest.cs @@ -23,7 +23,7 @@ public static void WriteQuest(this NetworkWriter writer, MockQuest quest) { writer.WritePackedInt32(quest.Id); } - public static MockQuest WriteQuest(this NetworkReader reader) + public static MockQuest ReadQuest(this NetworkReader reader) { return new MockQuest(reader.ReadPackedInt32()); } @@ -32,10 +32,13 @@ public static MockQuest WriteQuest(this NetworkReader reader) [TestFixture] public class CustomRWTest { - - class QuestMessage : MessageBase + struct QuestMessage : IMessageBase { public MockQuest quest; + + // Weaver will generate serialization + public void Serialize(NetworkWriter writer) {} + public void Deserialize(NetworkReader reader) {} } [Test] diff --git a/Assets/Mirror/Tests/Editor/Mirror.Tests.asmdef b/Assets/Mirror/Tests/Editor/Mirror.Tests.asmdef index 88d6fa65a..48d26303c 100644 --- a/Assets/Mirror/Tests/Editor/Mirror.Tests.asmdef +++ b/Assets/Mirror/Tests/Editor/Mirror.Tests.asmdef @@ -4,6 +4,7 @@ "Mirror", "Mirror.Weaver", "Mirror.Components", + "Mirror.Authenticators", "Mirror.Tests.Common" ], "optionalUnityReferences": [ diff --git a/Assets/Mirror/Tests/Editor/StructMessagesTests.cs b/Assets/Mirror/Tests/Editor/StructMessagesTests.cs index 51f475a2e..25d5e7898 100644 --- a/Assets/Mirror/Tests/Editor/StructMessagesTests.cs +++ b/Assets/Mirror/Tests/Editor/StructMessagesTests.cs @@ -6,7 +6,7 @@ public struct SomeStructMessage : IMessageBase { public int someValue; - // Mirror will automatically implement message that are empty + // Weaver will generate serialization public void Serialize(NetworkWriter writer) { } public void Deserialize(NetworkReader reader) { } }