Skip to content

Commit e41ced9

Browse files
committed
Added per NetworkBehaviour message Targets
1 parent 368d719 commit e41ced9

File tree

6 files changed

+111
-41
lines changed

6 files changed

+111
-41
lines changed

MLAPI/MonoBehaviours/Core/NetworkedBehaviour.cs

Lines changed: 34 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -104,16 +104,40 @@ public virtual void OnLostOwnership()
104104

105105
}
106106

107-
protected int RegisterMessageHandler(string name, Action<int, byte[]> action)
107+
protected void RegisterMessageHandler(string name, Action<int, byte[]> action)
108108
{
109+
if (!MessageManager.messageTypes.ContainsKey(name))
110+
{
111+
Debug.LogWarning("MLAPI: The messageType " + name + " is not registered");
112+
return;
113+
}
114+
ushort messageType = MessageManager.messageTypes[name];
115+
ushort behaviourOrder = networkedObject.GetOrderIndex(this);
116+
117+
if (!networkedObject.targetMessageActions.ContainsKey(behaviourOrder))
118+
networkedObject.targetMessageActions.Add(behaviourOrder, new Dictionary<ushort, Action<int, byte[]>>());
119+
if (networkedObject.targetMessageActions[behaviourOrder].ContainsKey(messageType))
120+
{
121+
Debug.LogWarning("MLAPI: Each NetworkedBehaviour can only register one callback per instance per message type");
122+
return;
123+
}
124+
125+
networkedObject.targetMessageActions[behaviourOrder].Add(messageType, action);
109126
int counter = MessageManager.AddIncomingMessageHandler(name, action, networkId);
110127
registeredMessageHandlers.Add(name, counter);
111-
return counter;
112128
}
113129

114130
protected void DeregisterMessageHandler(string name, int counter)
115131
{
116132
MessageManager.RemoveIncomingMessageHandler(name, counter, networkId);
133+
ushort messageType = MessageManager.messageTypes[name];
134+
ushort behaviourOrder = networkedObject.GetOrderIndex(this);
135+
136+
if (networkedObject.targetMessageActions.ContainsKey(behaviourOrder) &&
137+
networkedObject.targetMessageActions[behaviourOrder].ContainsKey(messageType))
138+
{
139+
networkedObject.targetMessageActions[behaviourOrder].Remove(messageType);
140+
}
117141
}
118142

119143
private void OnDisable()
@@ -619,7 +643,7 @@ protected void SendToServerTarget(string messageType, string channelName, byte[]
619643
Debug.LogWarning("MLAPI: Server can not send messages to server.");
620644
return;
621645
}
622-
NetworkingManager.singleton.Send(NetworkingManager.singleton.serverClientId, messageType, channelName, data, networkId);
646+
NetworkingManager.singleton.Send(NetworkingManager.singleton.serverClientId, messageType, channelName, data, networkId, networkedObject.GetOrderIndex(this));
623647
}
624648

625649
protected void SendToLocalClient(string messageType, string channelName, byte[] data)
@@ -649,7 +673,7 @@ protected void SendToLocalClientTarget(string messageType, string channelName, b
649673
Debug.LogWarning("MLAPI: Invalid Passthrough send. Ensure AllowPassthroughMessages are turned on and that the MessageType " + messageType + " is registered as a passthroughMessageType");
650674
return;
651675
}
652-
NetworkingManager.singleton.Send(ownerClientId, messageType, channelName, data, networkId);
676+
NetworkingManager.singleton.Send(ownerClientId, messageType, channelName, data, networkId, networkedObject.GetOrderIndex(this));
653677
}
654678

655679
protected void SendToNonLocalClients(string messageType, string channelName, byte[] data)
@@ -664,7 +688,7 @@ protected void SendToNonLocalClients(string messageType, string channelName, byt
664688
Debug.LogWarning("MLAPI: Sending messages from client to other clients is not yet supported");
665689
return;
666690
}
667-
NetworkingManager.singleton.Send(messageType, channelName, data, ownerClientId, null);
691+
NetworkingManager.singleton.Send(messageType, channelName, data, ownerClientId);
668692
}
669693

670694
protected void SendToNonLocalClientsTarget(string messageType, string channelName, byte[] data)
@@ -679,7 +703,7 @@ protected void SendToNonLocalClientsTarget(string messageType, string channelNam
679703
Debug.LogWarning("MLAPI: Sending messages from client to other clients is not yet supported");
680704
return;
681705
}
682-
NetworkingManager.singleton.Send(messageType, channelName, data, ownerClientId, networkId);
706+
NetworkingManager.singleton.Send(messageType, channelName, data, ownerClientId, networkId, networkedObject.GetOrderIndex(this));
683707
}
684708

685709
protected void SendToClient(int clientId, string messageType, string channelName, byte[] data)
@@ -709,7 +733,7 @@ protected void SendToClientTarget(int clientId, string messageType, string chann
709733
Debug.LogWarning("MLAPI: Invalid Passthrough send. Ensure AllowPassthroughMessages are turned on and that the MessageType " + messageType + " is registered as a passthroughMessageType");
710734
return;
711735
}
712-
NetworkingManager.singleton.Send(clientId, messageType, channelName, data, networkId);
736+
NetworkingManager.singleton.Send(clientId, messageType, channelName, data, networkId, networkedObject.GetOrderIndex(this));
713737
}
714738

715739
protected void SendToClients(int[] clientIds, string messageType, string channelName, byte[] data)
@@ -739,7 +763,7 @@ protected void SendToClientsTarget(int[] clientIds, string messageType, string c
739763
Debug.LogWarning("MLAPI: Sending messages from client to other clients is not yet supported");
740764
return;
741765
}
742-
NetworkingManager.singleton.Send(clientIds, messageType, channelName, data, networkId);
766+
NetworkingManager.singleton.Send(clientIds, messageType, channelName, data, networkId, networkedObject.GetOrderIndex(this));
743767
}
744768

745769
protected void SendToClients(List<int> clientIds, string messageType, string channelName, byte[] data)
@@ -769,7 +793,7 @@ protected void SendToClientsTarget(List<int> clientIds, string messageType, stri
769793
Debug.LogWarning("MLAPI: Sending messages from client to other clients is not yet supported");
770794
return;
771795
}
772-
NetworkingManager.singleton.Send(clientIds, messageType, channelName, data, networkId);
796+
NetworkingManager.singleton.Send(clientIds, messageType, channelName, data, networkId, networkedObject.GetOrderIndex(this));
773797
}
774798

775799
protected void SendToClients(string messageType, string channelName, byte[] data)
@@ -799,7 +823,7 @@ protected void SendToClientsTarget(string messageType, string channelName, byte[
799823
Debug.LogWarning("MLAPI: Sending messages from client to other clients is not yet supported");
800824
return;
801825
}
802-
NetworkingManager.singleton.Send(messageType, channelName, data, networkId);
826+
NetworkingManager.singleton.Send(messageType, channelName, data, networkId, networkedObject.GetOrderIndex(this));
803827
}
804828

805829
protected NetworkedObject GetNetworkedObject(uint networkId)

MLAPI/MonoBehaviours/Core/NetworkedObject.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using MLAPI.NetworkingManagerComponents;
2+
using System;
23
using System.Collections.Generic;
34
using UnityEngine;
45

@@ -143,5 +144,8 @@ internal NetworkedBehaviour GetBehaviourAtOrderIndex(ushort index)
143144
//TODO index out of bounds
144145
return childNetworkedBehaviours[index];
145146
}
147+
148+
//Key: behaviourOrderId, value key: messageType, value value callback
149+
internal Dictionary<ushort, Dictionary<ushort, Action<int, byte[]>>> targetMessageActions = new Dictionary<ushort, Dictionary<ushort, Action<int, byte[]>>>();
146150
}
147151
}

0 commit comments

Comments
 (0)