This repository has been archived on 2025-04-22. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
com.unity.netcode.gameobjects/Tests/Editor/Messaging/MessageRegistrationTests.cs
Unity Technologies add668dfd2 com.unity.netcode.gameobjects@1.0.0-pre.8
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/) and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

Additional documentation and release notes are available at [Multiplayer Documentation](https://docs-multiplayer.unity3d.com).

## [1.0.0-pre.8] - 2022-04-27

### Changed

- `unmanaged` structs are no longer universally accepted as RPC parameters because some structs (i.e., structs with pointers in them, such as `NativeList<T>`) can't be supported by the default memcpy struct serializer. Structs that are intended to be serialized across the network must add `INetworkSerializeByMemcpy` to the interface list (i.e., `struct Foo : INetworkSerializeByMemcpy`). This interface is empty and just serves to mark the struct as compatible with memcpy serialization. For external structs you can't edit, you can pass them to RPCs by wrapping them in `ForceNetworkSerializeByMemcpy<T>`. (#1901)

### Removed
- Removed `SIPTransport` (#1870)

- Removed `ClientNetworkTransform` from the package samples and moved to Boss Room's Utilities package which can be found [here](https://github.com/Unity-Technologies/com.unity.multiplayer.samples.coop/blob/main/Packages/com.unity.multiplayer.samples.coop/Utilities/Net/ClientAuthority/ClientNetworkTransform.cs).

### Fixed

- Fixed `NetworkTransform` generating false positive rotation delta checks when rolling over between 0 and 360 degrees. (#1890)
- Fixed client throwing an exception if it has messages in the outbound queue when processing the `NetworkEvent.Disconnect` event and is using UTP. (#1884)
- Fixed issue during client synchronization if 'ValidateSceneBeforeLoading' returned false it would halt the client synchronization process resulting in a client that was approved but not synchronized or fully connected with the server. (#1883)
- Fixed an issue where UNetTransport.StartServer would return success even if the underlying transport failed to start (#854)
- Passing generic types to RPCs no longer causes a native crash (#1901)
- Fixed an issue where calling `Shutdown` on a `NetworkManager` that was already shut down would cause an immediate shutdown the next time it was started (basically the fix makes `Shutdown` idempotent). (#1877)
2022-04-27 00:00:00 +00:00

184 lines
6.7 KiB
C#

using System.Collections.Generic;
using NUnit.Framework;
namespace Unity.Netcode.EditorTests
{
public class MessageRegistrationTests
{
private struct TestMessageOne : INetworkMessage, INetworkSerializeByMemcpy
{
public int A;
public int B;
public int C;
public void Serialize(FastBufferWriter writer)
{
writer.WriteValue(this);
}
public bool Deserialize(FastBufferReader reader, ref NetworkContext context)
{
return true;
}
public void Handle(ref NetworkContext context)
{
}
}
private struct TestMessageTwo : INetworkMessage, INetworkSerializeByMemcpy
{
public int A;
public int B;
public int C;
public void Serialize(FastBufferWriter writer)
{
writer.WriteValue(this);
}
public bool Deserialize(FastBufferReader reader, ref NetworkContext context)
{
return true;
}
public void Handle(ref NetworkContext context)
{
}
}
private class TestMessageProviderOne : IMessageProvider
{
public List<MessagingSystem.MessageWithHandler> GetMessages()
{
return new List<MessagingSystem.MessageWithHandler>
{
new MessagingSystem.MessageWithHandler
{
MessageType = typeof(TestMessageOne),
Handler = MessagingSystem.ReceiveMessage<TestMessageOne>
},
new MessagingSystem.MessageWithHandler
{
MessageType = typeof(TestMessageTwo),
Handler = MessagingSystem.ReceiveMessage<TestMessageTwo>
}
};
}
}
private struct TestMessageThree : INetworkMessage, INetworkSerializeByMemcpy
{
public int A;
public int B;
public int C;
public void Serialize(FastBufferWriter writer)
{
writer.WriteValue(this);
}
public bool Deserialize(FastBufferReader reader, ref NetworkContext context)
{
return true;
}
public void Handle(ref NetworkContext context)
{
}
}
private class TestMessageProviderTwo : IMessageProvider
{
public List<MessagingSystem.MessageWithHandler> GetMessages()
{
return new List<MessagingSystem.MessageWithHandler>
{
new MessagingSystem.MessageWithHandler
{
MessageType = typeof(TestMessageThree),
Handler = MessagingSystem.ReceiveMessage<TestMessageThree>
}
};
}
}
private struct TestMessageFour : INetworkMessage, INetworkSerializeByMemcpy
{
public int A;
public int B;
public int C;
public void Serialize(FastBufferWriter writer)
{
writer.WriteValue(this);
}
public bool Deserialize(FastBufferReader reader, ref NetworkContext context)
{
return true;
}
public void Handle(ref NetworkContext context)
{
}
}
private class TestMessageProviderThree : IMessageProvider
{
public List<MessagingSystem.MessageWithHandler> GetMessages()
{
return new List<MessagingSystem.MessageWithHandler>
{
new MessagingSystem.MessageWithHandler
{
MessageType = typeof(TestMessageFour),
Handler = MessagingSystem.ReceiveMessage<TestMessageFour>
}
};
}
}
[Test]
public void WhenCreatingMessageSystem_OnlyProvidedTypesAreRegistered()
{
var sender = new NopMessageSender();
var systemOne = new MessagingSystem(sender, null, new TestMessageProviderOne());
var systemTwo = new MessagingSystem(sender, null, new TestMessageProviderTwo());
var systemThree = new MessagingSystem(sender, null, new TestMessageProviderThree());
using (systemOne)
using (systemTwo)
using (systemThree)
{
Assert.AreEqual(2, systemOne.MessageHandlerCount);
Assert.AreEqual(1, systemTwo.MessageHandlerCount);
Assert.AreEqual(1, systemThree.MessageHandlerCount);
Assert.Contains(typeof(TestMessageOne), systemOne.MessageTypes);
Assert.Contains(typeof(TestMessageTwo), systemOne.MessageTypes);
Assert.Contains(typeof(TestMessageThree), systemTwo.MessageTypes);
Assert.Contains(typeof(TestMessageFour), systemThree.MessageTypes);
}
}
[Test]
public void WhenCreatingMessageSystem_BoundTypeMessageHandlersAreRegistered()
{
var sender = new NopMessageSender();
var systemOne = new MessagingSystem(sender, null, new TestMessageProviderOne());
var systemTwo = new MessagingSystem(sender, null, new TestMessageProviderTwo());
var systemThree = new MessagingSystem(sender, null, new TestMessageProviderThree());
using (systemOne)
using (systemTwo)
using (systemThree)
{
MessagingSystem.MessageHandler handlerOne = MessagingSystem.ReceiveMessage<TestMessageOne>;
MessagingSystem.MessageHandler handlerTwo = MessagingSystem.ReceiveMessage<TestMessageTwo>;
MessagingSystem.MessageHandler handlerThree = MessagingSystem.ReceiveMessage<TestMessageThree>;
MessagingSystem.MessageHandler handlerFour = MessagingSystem.ReceiveMessage<TestMessageFour>;
Assert.AreEqual(handlerOne, systemOne.MessageHandlers[systemOne.GetMessageType(typeof(TestMessageOne))]);
Assert.AreEqual(handlerTwo, systemOne.MessageHandlers[systemOne.GetMessageType(typeof(TestMessageTwo))]);
Assert.AreEqual(handlerThree, systemTwo.MessageHandlers[systemTwo.GetMessageType(typeof(TestMessageThree))]);
Assert.AreEqual(handlerFour, systemThree.MessageHandlers[systemThree.GetMessageType(typeof(TestMessageFour))]);
}
}
}
}