mirror of
https://github.com/MirrorNetworking/Mirror.git
synced 2024-11-18 02:50:32 +00:00
NetworkIdentityTests: HandleRPC
This commit is contained in:
parent
598766764c
commit
0f362ec1cf
@ -1250,6 +1250,45 @@ public void HandleCommand()
|
||||
GameObject.DestroyImmediate(gameObject);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void HandleRpc()
|
||||
{
|
||||
// create a networkidentity with an rpc component
|
||||
GameObject gameObject = new GameObject();
|
||||
NetworkIdentity identity = gameObject.AddComponent<NetworkIdentity>();
|
||||
RpcTestNetworkBehaviour comp0 = gameObject.AddComponent<RpcTestNetworkBehaviour>();
|
||||
Assert.That(comp0.called, Is.EqualTo(0));
|
||||
|
||||
// register the command delegate, otherwise it's not found
|
||||
NetworkBehaviour.RegisterRpcDelegate(typeof(RpcTestNetworkBehaviour), nameof(RpcTestNetworkBehaviour.RpcGenerated), RpcTestNetworkBehaviour.RpcGenerated);
|
||||
|
||||
// identity needs to be in spawned dict, otherwise command handler
|
||||
// won't find it
|
||||
NetworkIdentity.spawned[identity.netId] = identity;
|
||||
|
||||
// serialize an RpcMessage message into an arraysegment
|
||||
RpcMessage message = new RpcMessage {
|
||||
componentIndex = 0,
|
||||
functionHash = NetworkBehaviour.GetMethodHash(typeof(RpcTestNetworkBehaviour), nameof(RpcTestNetworkBehaviour.RpcGenerated)),
|
||||
netId = identity.netId,
|
||||
payload = new ArraySegment<byte>(new byte[0])
|
||||
};
|
||||
NetworkWriter writer = new NetworkWriter();
|
||||
MessagePacker.Pack(message, writer);
|
||||
ArraySegment<byte> segment = writer.ToArraySegment();
|
||||
|
||||
// call HandleRpc
|
||||
identity.HandleRPC(message.componentIndex, message.functionHash, new NetworkReader(segment));
|
||||
|
||||
// was the rpc called in the component?
|
||||
Assert.That(comp0.called, Is.EqualTo(1));
|
||||
|
||||
// clean up
|
||||
NetworkIdentity.spawned.Clear();
|
||||
NetworkBehaviour.ClearDelegates();
|
||||
GameObject.DestroyImmediate(gameObject);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ServerUpdate()
|
||||
{
|
||||
|
@ -19,6 +19,18 @@ public static void CommandGenerated(NetworkBehaviour comp, NetworkReader reader)
|
||||
}
|
||||
}
|
||||
|
||||
public class RpcTestNetworkBehaviour : NetworkBehaviour
|
||||
{
|
||||
// counter to make sure that it's called exactly once
|
||||
public int called;
|
||||
// weaver generates this from [Rpc]
|
||||
// but for tests we need to add it manually
|
||||
public static void RpcGenerated(NetworkBehaviour comp, NetworkReader reader)
|
||||
{
|
||||
++((RpcTestNetworkBehaviour)comp).called;
|
||||
}
|
||||
}
|
||||
|
||||
public class OnStartClientTestNetworkBehaviour : NetworkBehaviour
|
||||
{
|
||||
// counter to make sure that it's called exactly once
|
||||
|
Loading…
Reference in New Issue
Block a user