Remove NetworkMessage class support

This commit is contained in:
vis2k 2020-09-29 10:12:52 +02:00
parent 4d4e507847
commit a565c7c33c
20 changed files with 130 additions and 527 deletions

View File

@ -14,9 +14,13 @@ public void SetHostname(string hostname)
public ChatWindow chatWindow;
public class CreatePlayerMessage : MessageBase
public class CreatePlayerMessage : IMessageBase
{
public string name;
// Weaver will generate serialization
public void Serialize(NetworkWriter writer) {}
public void Deserialize(NetworkReader reader) {}
}
public override void OnStartServer()

View File

@ -10,15 +10,6 @@ public interface IMessageBase
void Serialize(NetworkWriter writer);
}
public abstract class MessageBase : IMessageBase
{
// De-serialize the contents of the reader into this message
public virtual void Deserialize(NetworkReader reader) { }
// Serialize the contents of this message into the writer
public virtual void Serialize(NetworkWriter writer) { }
}
#region Public System Messages
public struct ErrorMessage : IMessageBase
{

View File

@ -1,68 +0,0 @@
using NUnit.Framework;
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()
{
// try setting value with constructor
AuthRequestMessage message = new AuthRequestMessage
{
authUsername = "abc",
authPassword = "123"
};
Assert.That(message.authUsername, Is.EqualTo("abc"));
Assert.That(message.authPassword, Is.EqualTo("123"));
// serialize
NetworkWriter writer = new NetworkWriter();
message.Serialize(writer);
byte[] writerData = writer.ToArray();
// try deserialize
AuthRequestMessage fresh = new AuthRequestMessage();
fresh.Deserialize(new NetworkReader(writerData));
Assert.That(fresh.authUsername, Is.EqualTo("abc"));
Assert.That(fresh.authPassword, Is.EqualTo("123"));
}
[Test]
public void AuthResponseMessageTest()
{
// try setting value with constructor
AuthResponseMessage message = new AuthResponseMessage
{
code = 123,
message = "abc"
};
Assert.That(message.code, Is.EqualTo(123));
Assert.That(message.message, Is.EqualTo("abc"));
// serialize
NetworkWriter writer = new NetworkWriter();
message.Serialize(writer);
byte[] writerData = writer.ToArray();
// try deserialize
AuthResponseMessage fresh = new AuthResponseMessage();
fresh.Deserialize(new NetworkReader(writerData));
Assert.That(fresh.code, Is.EqualTo(123));
Assert.That(fresh.message, Is.EqualTo("abc"));
}
}
}

View File

@ -1,11 +0,0 @@
fileFormatVersion: 2
guid: 14742e780e150894b983a54d36a3cdb6
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -23,7 +23,7 @@ public static void WriteQuest(this NetworkWriter writer, MockQuest quest)
{
writer.WriteInt32(quest.Id);
}
public static MockQuest WriteQuest(this NetworkReader reader)
public static MockQuest ReadQuest(this NetworkReader reader)
{
return new MockQuest(reader.ReadInt32());
}
@ -33,9 +33,13 @@ public static MockQuest WriteQuest(this NetworkReader reader)
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]

View File

@ -23,19 +23,40 @@ public static EnumReadWriteTests.MyCustomEnum ReadMyCustomEnum(this NetworkReade
}
public class EnumReadWriteTests
{
public class ByteMessage : MessageBase { public MyByteEnum byteEnum; }
public struct ByteMessage : IMessageBase
{
public MyByteEnum byteEnum;
// Weaver auto generates serialization
public void Deserialize(NetworkReader reader) {}
public void Serialize(NetworkWriter writer) {}
}
public enum MyByteEnum : byte
{
A, B, C, D
}
public class ShortMessage : MessageBase { public MyShortEnum shortEnum; }
public struct ShortMessage : IMessageBase
{
public MyShortEnum shortEnum;
// Weaver auto generates serialization
public void Deserialize(NetworkReader reader) {}
public void Serialize(NetworkWriter writer) {}
}
public enum MyShortEnum : short
{
E, F, G, H
}
public class CustomMessage : MessageBase { public MyCustomEnum customEnum; }
public struct CustomMessage : IMessageBase
{
public MyCustomEnum customEnum;
// Weaver auto generates serialization
public void Deserialize(NetworkReader reader) {}
public void Serialize(NetworkWriter writer) {}
}
public enum MyCustomEnum
{
@ -76,7 +97,7 @@ public void CustomWriterIsUsedForEnum()
// custom writer should write N if it sees O
Assert.That(clientMsg.customEnum, Is.EqualTo(MyCustomEnum.N));
}
T SerializeAndDeserializeMessage<T>(T msg) where T : MessageBase, new()
T SerializeAndDeserializeMessage<T>(T msg) where T : IMessageBase, new()
{
NetworkWriter writer = new NetworkWriter();

View File

@ -10,9 +10,13 @@ namespace Mirror.Tests.Generated.CollectionWriters
public class Array_int_Test
{
class Message : MessageBase
struct Message : IMessageBase
{
public int[] collection;
// Weaver will generate serialization
public void Serialize(NetworkWriter writer) {}
public void Deserialize(NetworkReader reader) {}
}
[Test]
@ -74,9 +78,13 @@ public void SendsData()
public class Array_string_Test
{
class Message : MessageBase
struct Message : IMessageBase
{
public string[] collection;
// Weaver will generate serialization
public void Serialize(NetworkWriter writer) {}
public void Deserialize(NetworkReader reader) {}
}
[Test]
@ -138,9 +146,13 @@ public void SendsData()
public class Array_Vector3_Test
{
class Message : MessageBase
struct Message : IMessageBase
{
public Vector3[] collection;
// Weaver will generate serialization
public void Serialize(NetworkWriter writer) {}
public void Deserialize(NetworkReader reader) {}
}
[Test]
@ -202,9 +214,13 @@ public void SendsData()
public class Array_FloatStringStruct_Test
{
class Message : MessageBase
struct Message : IMessageBase
{
public FloatStringStruct[] collection;
// Weaver will generate serialization
public void Serialize(NetworkWriter writer) {}
public void Deserialize(NetworkReader reader) {}
}
[Test]
@ -266,9 +282,13 @@ public void SendsData()
public class Array_ClassWithNoConstructor_Test
{
class Message : MessageBase
struct Message : IMessageBase
{
public ClassWithNoConstructor[] collection;
// Weaver will generate serialization
public void Serialize(NetworkWriter writer) {}
public void Deserialize(NetworkReader reader) {}
}
[Test]
@ -330,9 +350,13 @@ public void SendsData()
public class ArraySegment_int_Test
{
class Message : MessageBase
struct Message : IMessageBase
{
public ArraySegment<int> collection;
// Weaver will generate serialization
public void Serialize(NetworkWriter writer) {}
public void Deserialize(NetworkReader reader) {}
}
[Test]
@ -408,9 +432,13 @@ public void SendsData()
public class ArraySegment_string_Test
{
class Message : MessageBase
struct Message : IMessageBase
{
public ArraySegment<string> collection;
// Weaver will generate serialization
public void Serialize(NetworkWriter writer) {}
public void Deserialize(NetworkReader reader) {}
}
[Test]
@ -486,9 +514,13 @@ public void SendsData()
public class ArraySegment_Vector3_Test
{
class Message : MessageBase
struct Message : IMessageBase
{
public ArraySegment<Vector3> collection;
// Weaver will generate serialization
public void Serialize(NetworkWriter writer) {}
public void Deserialize(NetworkReader reader) {}
}
[Test]
@ -564,9 +596,13 @@ public void SendsData()
public class ArraySegment_FloatStringStruct_Test
{
class Message : MessageBase
struct Message : IMessageBase
{
public ArraySegment<FloatStringStruct> collection;
// Weaver will generate serialization
public void Serialize(NetworkWriter writer) {}
public void Deserialize(NetworkReader reader) {}
}
[Test]
@ -642,9 +678,13 @@ public void SendsData()
public class ArraySegment_ClassWithNoConstructor_Test
{
class Message : MessageBase
struct Message : IMessageBase
{
public ArraySegment<ClassWithNoConstructor> collection;
// Weaver will generate serialization
public void Serialize(NetworkWriter writer) {}
public void Deserialize(NetworkReader reader) {}
}
[Test]
@ -720,9 +760,13 @@ public void SendsData()
public class List_int_Test
{
class Message : MessageBase
struct Message : IMessageBase
{
public List<int> collection;
// Weaver will generate serialization
public void Serialize(NetworkWriter writer) {}
public void Deserialize(NetworkReader reader) {}
}
[Test]
@ -784,9 +828,13 @@ public void SendsData()
public class List_string_Test
{
class Message : MessageBase
struct Message : IMessageBase
{
public List<string> collection;
// Weaver will generate serialization
public void Serialize(NetworkWriter writer) {}
public void Deserialize(NetworkReader reader) {}
}
[Test]
@ -848,9 +896,13 @@ public void SendsData()
public class List_Vector3_Test
{
class Message : MessageBase
struct Message : IMessageBase
{
public List<Vector3> collection;
// Weaver will generate serialization
public void Serialize(NetworkWriter writer) {}
public void Deserialize(NetworkReader reader) {}
}
[Test]
@ -912,9 +964,13 @@ public void SendsData()
public class List_FloatStringStruct_Test
{
class Message : MessageBase
struct Message : IMessageBase
{
public List<FloatStringStruct> collection;
// Weaver will generate serialization
public void Serialize(NetworkWriter writer) {}
public void Deserialize(NetworkReader reader) {}
}
[Test]
@ -976,9 +1032,13 @@ public void SendsData()
public class List_ClassWithNoConstructor_Test
{
class Message : MessageBase
struct Message : IMessageBase
{
public List<ClassWithNoConstructor> collection;
// Weaver will generate serialization
public void Serialize(NetworkWriter writer) {}
public void Deserialize(NetworkReader reader) {}
}
[Test]

View File

@ -1,190 +1 @@
using NUnit.Framework;
namespace Mirror.Tests.MessageTests
{
struct TestMessage : IMessageBase
{
public int IntValue;
public string StringValue;
public double DoubleValue;
public TestMessage(int i, string s, double d)
{
IntValue = i;
StringValue = s;
DoubleValue = d;
}
public void Deserialize(NetworkReader reader)
{
IntValue = reader.ReadInt32();
StringValue = reader.ReadString();
DoubleValue = reader.ReadDouble();
}
public void Serialize(NetworkWriter writer)
{
writer.WriteInt32(IntValue);
writer.WriteString(StringValue);
writer.WriteDouble(DoubleValue);
}
}
struct StructWithEmptyMethodMessage : IMessageBase
{
public int IntValue;
public string StringValue;
public double DoubleValue;
// Mirror will fill out these empty methods
public void Deserialize(NetworkReader reader) { }
public void Serialize(NetworkWriter writer) { }
}
class ClassWithoutBaseMessage : IMessageBase
{
public int[] array;
// Mirror will fill out these empty methods
public void Deserialize(NetworkReader reader) { }
public void Serialize(NetworkWriter writer) { }
}
abstract class AbstractMessage : IMessageBase
{
public abstract void Deserialize(NetworkReader reader);
public abstract void Serialize(NetworkWriter writer);
}
class OverrideMessage : AbstractMessage
{
public int someValue;
// Mirror will fill out these empty methods
public override void Serialize(NetworkWriter writer) { }
public override void Deserialize(NetworkReader reader) { }
}
class Layer1Message : IMessageBase
{
public int value1;
// Mirror will fill out these empty methods
public virtual void Serialize(NetworkWriter writer) { }
public virtual void Deserialize(NetworkReader reader) { }
}
class Layer2Message : Layer1Message
{
public int value2;
}
class Layer3Message : Layer2Message
{
public int value3;
}
class NullableObject
{
public int id = 3;
}
class NullableObjectMessage : MessageBase
{
public string name;
public NullableObject nullObj;
}
[TestFixture]
public class MessageBaseTests
{
[Test]
public void StructWithMethods()
{
byte[] arr = MessagePackerTest.PackToByteArray(new TestMessage(1, "2", 3.3));
TestMessage t = MessagePacker.Unpack<TestMessage>(arr);
Assert.AreEqual(1, t.IntValue);
}
[Test]
public void StructWithEmptyMethods()
{
byte[] arr = MessagePackerTest.PackToByteArray(new StructWithEmptyMethodMessage { IntValue = 1, StringValue = "2", DoubleValue = 3.3 });
StructWithEmptyMethodMessage t = MessagePacker.Unpack<StructWithEmptyMethodMessage>(arr);
Assert.AreEqual(1, t.IntValue);
Assert.AreEqual("2", t.StringValue);
Assert.AreEqual(3.3, t.DoubleValue);
}
[Test]
public void ClassWithEmptyMethods()
{
ClassWithoutBaseMessage intMessage = new ClassWithoutBaseMessage
{
array = new[] { 3, 4, 5 }
};
byte[] data = MessagePackerTest.PackToByteArray(intMessage);
ClassWithoutBaseMessage unpacked = MessagePacker.Unpack<ClassWithoutBaseMessage>(data);
Assert.That(unpacked.array, Is.EquivalentTo(new int[] { 3, 4, 5 }));
}
[Test]
public void AbstractBaseClassWorks()
{
const int value = 10;
OverrideMessage intMessage = new OverrideMessage
{
someValue = value
};
byte[] data = MessagePackerTest.PackToByteArray(intMessage);
OverrideMessage unpacked = MessagePacker.Unpack<OverrideMessage>(data);
Assert.That(unpacked.someValue, Is.EqualTo(value));
}
[Test]
public void MessageInheirtanceWorksWithMultipleLayers()
{
const int value1 = 10;
const int value2 = 13;
const int value3 = 15;
Layer3Message intMessage = new Layer3Message
{
value1 = value1,
value2 = value2,
value3 = value3
};
byte[] data = MessagePackerTest.PackToByteArray(intMessage);
Layer3Message unpacked = MessagePacker.Unpack<Layer3Message>(data);
Assert.That(unpacked.value1, Is.EqualTo(value1));
Assert.That(unpacked.value2, Is.EqualTo(value2));
Assert.That(unpacked.value3, Is.EqualTo(value3));
}
[Test]
public void NullObjectMessageTest()
{
NullableObjectMessage nullableObjectMessage = new NullableObjectMessage
{
name = "pepe",
nullObj = null
};
byte[] data = MessagePackerTest.PackToByteArray(nullableObjectMessage);
NullableObjectMessage unpacked = MessagePacker.Unpack<NullableObjectMessage>(data);
Assert.That(unpacked.name, Is.EqualTo("pepe"));
Assert.That(unpacked.nullObj, Is.Null);
}
}
}

View File

@ -1,11 +0,0 @@
fileFormatVersion: 2
guid: ecf93fcf0386fee4e85f981d5ca9259d
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -1,127 +0,0 @@
using NUnit.Framework;
namespace Mirror.Tests.MessageTests
{
class ParentMessage : MessageBase
{
public int parentValue;
}
class ChildMessage : ParentMessage
{
public int childValue;
}
public abstract class RequestMessageBase : MessageBase
{
public int responseId = 0;
}
public class ResponseMessage : RequestMessageBase
{
public int state;
public string message = "";
public int errorCode = 0; // optional for error codes
}
//reverseOrder to test this https://github.com/vis2k/Mirror/issues/1925
public class ResponseMessageReverse : RequestMessageBaseReverse
{
public int state;
public string message = "";
public int errorCode = 0; // optional for error codes
}
public abstract class RequestMessageBaseReverse : MessageBase
{
public int responseId = 0;
}
[TestFixture]
public class MessageInheritanceTest
{
[Test]
public void SendsVauesInParentAndChildClass()
{
NetworkWriter writer = new NetworkWriter();
writer.WriteMessage(new ChildMessage
{
parentValue = 3,
childValue = 4
});
byte[] arr = writer.ToArray();
NetworkReader reader = new NetworkReader(arr);
ChildMessage received = new ChildMessage();
received.Deserialize(reader);
Assert.AreEqual(3, received.parentValue);
Assert.AreEqual(4, received.childValue);
int writeLength = writer.Length;
int readLength = reader.Position;
Assert.That(writeLength == readLength, $"OnSerializeAll and OnDeserializeAll calls write the same amount of data\n writeLength={writeLength}\n readLength={readLength}");
}
[Test]
public void SendsVauesWhenUsingAbstractClass()
{
NetworkWriter writer = new NetworkWriter();
const int state = 2;
const string message = "hello world";
const int responseId = 5;
writer.WriteMessage(new ResponseMessage
{
state = state,
message = message,
responseId = responseId,
});
byte[] arr = writer.ToArray();
NetworkReader reader = new NetworkReader(arr);
ResponseMessage received = new ResponseMessage();
received.Deserialize(reader);
Assert.AreEqual(state, received.state);
Assert.AreEqual(message, received.message);
Assert.AreEqual(responseId, received.responseId);
int writeLength = writer.Length;
int readLength = reader.Position;
Assert.That(writeLength == readLength, $"OnSerializeAll and OnDeserializeAll calls write the same amount of data\n writeLength={writeLength}\n readLength={readLength}");
}
[Test]
public void SendsVauesWhenUsingAbstractClassReverseDefineOrder()
{
NetworkWriter writer = new NetworkWriter();
const int state = 2;
const string message = "hello world";
const int responseId = 5;
writer.WriteMessage(new ResponseMessageReverse
{
state = state,
message = message,
responseId = responseId,
});
byte[] arr = writer.ToArray();
NetworkReader reader = new NetworkReader(arr);
ResponseMessageReverse received = new ResponseMessageReverse();
received.Deserialize(reader);
Assert.AreEqual(state, received.state);
Assert.AreEqual(message, received.message);
Assert.AreEqual(responseId, received.responseId);
int writeLength = writer.Length;
int readLength = reader.Position;
Assert.That(writeLength == readLength, $"OnSerializeAll and OnDeserializeAll calls write the same amount of data\n writeLength={writeLength}\n readLength={readLength}");
}
}
}

View File

@ -1,11 +0,0 @@
fileFormatVersion: 2
guid: 808855b645f9843d2b3077ab1304b2b3
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -16,9 +16,13 @@ public class ScriptableObjectWriterTest
// other types are generated by the weaver
class ScriptableObjectMessage : MessageBase
struct ScriptableObjectMessage : IMessageBase
{
public MyScriptableObject scriptableObject;
// Weaver auto generates serialization
public void Deserialize(NetworkReader reader) {}
public void Serialize(NetworkWriter writer) {}
}
[Test]

View File

@ -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) { }
}

View File

@ -10,19 +10,6 @@ public void MessageValid()
IsSuccess();
}
[Test]
public void MessageWithBaseClass()
{
IsSuccess();
}
[Test]
public void MessageSelfReferencing()
{
HasError("MessageSelfReferencing has field selfReference that references itself",
"WeaverMessageTests.MessageSelfReferencing.MessageSelfReferencing WeaverMessageTests.MessageSelfReferencing.MessageSelfReferencing::selfReference");
}
[Test]
public void MessageMemberGeneric()
{
@ -41,12 +28,6 @@ public void MessageMemberInterface()
"WeaverMessageTests.MessageMemberInterface.SuperCoolInterface WeaverMessageTests.MessageMemberInterface.MessageMemberInterface::invalidField");
}
[Test]
public void MessageNestedInheritance()
{
IsSuccess();
}
[Test]
public void AbstractMessageMethods()
{

View File

@ -6,7 +6,7 @@ namespace WeaverMessageTests.MessageMemberGeneric
{
class HasGeneric<T> { }
class MessageMemberGeneric : MessageBase
struct MessageMemberGeneric : IMessageBase
{
public uint netId;
public Guid assetId;
@ -14,5 +14,9 @@ class MessageMemberGeneric : MessageBase
public Quaternion rotation;
public HasGeneric<int> invalidField;
public byte[] payload;
// Weaver will generate serialization
public void Serialize(NetworkWriter writer) {}
public void Deserialize(NetworkReader reader) {}
}
}

View File

@ -6,7 +6,7 @@ namespace WeaverMessageTests.MessageMemberInterface
{
interface SuperCoolInterface { }
class MessageMemberInterface : MessageBase
struct MessageMemberInterface : IMessageBase
{
public uint netId;
public Guid assetId;
@ -14,5 +14,9 @@ class MessageMemberInterface : MessageBase
public Quaternion rotation;
public SuperCoolInterface invalidField;
public byte[] payload;
// Weaver will generate serialization
public void Serialize(NetworkWriter writer) {}
public void Deserialize(NetworkReader reader) {}
}
}

View File

@ -1,17 +0,0 @@
using Mirror;
namespace WeaverMessageTests.MessageNestedInheritance
{
public class Message : MessageBase
{
public class Request : Message
{
}
public class Response : Message
{
public int errorCode;
}
}
}

View File

@ -1,16 +0,0 @@
using System;
using Mirror;
using UnityEngine;
namespace WeaverMessageTests.MessageSelfReferencing
{
class MessageSelfReferencing : MessageBase
{
public uint netId;
public Guid assetId;
public Vector3 position;
public Quaternion rotation;
public MessageSelfReferencing selfReference = new MessageSelfReferencing();
public byte[] payload;
}
}

View File

@ -4,7 +4,7 @@
namespace WeaverMessageTests.MessageValid
{
class MessageValid : MessageBase
struct MessageValid : IMessageBase
{
public uint netId;
public Guid assetId;
@ -12,7 +12,7 @@ class MessageValid : MessageBase
public Quaternion rotation;
public byte[] payload;
public override void Deserialize(NetworkReader reader)
public void Deserialize(NetworkReader reader)
{
netId = reader.ReadPackedUInt32();
assetId = reader.ReadGuid();
@ -21,7 +21,7 @@ public override void Deserialize(NetworkReader reader)
payload = reader.ReadBytesAndSize();
}
public override void Serialize(NetworkWriter writer)
public void Serialize(NetworkWriter writer)
{
writer.WritePackedUInt32(netId);
writer.WriteGuid(assetId);

View File

@ -1,20 +0,0 @@
using System;
using Mirror;
using UnityEngine;
namespace WeaverMessageTests.MessageWithBaseClass
{
class MessageWithBaseClass : SomeBaseMessage
{
public uint netId;
public Guid assetId;
public Vector3 position;
public Quaternion rotation;
public byte[] payload;
}
class SomeBaseMessage : MessageBase
{
public int myExtraType;
}
}