From 711a072d651d203bde8815894e723326035af71a Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Albin=20Cor=C3=A9n?= <2108U9@gmail.com>
Date: Mon, 23 Apr 2018 16:23:40 +0200
Subject: [PATCH] Added Observer system
---
MLAPI-Editor/NetworkedObjectEditor.cs | 19 ++-
MLAPI/Data/SyncedVarField.cs | 3 +-
MLAPI/MLAPI.csproj | 1 +
.../MonoBehaviours/Core/NetworkedBehaviour.cs | 160 +++++++++++-------
MLAPI/MonoBehaviours/Core/NetworkedObject.cs | 68 ++++++++
.../MonoBehaviours/Core/NetworkingManager.cs | 29 +++-
.../Prototyping/NetworkedProximity.cs | 88 ++++++++++
.../Prototyping/NetworkedTransform.cs | 11 +-
.../Core/InternalMessageHandler.Receive.cs | 26 ++-
.../Core/InternalMessageHandler.Send.cs | 57 +++++--
.../Core/NetworkPoolManager.cs | 4 +-
.../Core/NetworkSceneManager.cs | 2 +-
.../Core/SpawnManager.cs | 10 +-
13 files changed, 389 insertions(+), 89 deletions(-)
create mode 100644 MLAPI/MonoBehaviours/Prototyping/NetworkedProximity.cs
diff --git a/MLAPI-Editor/NetworkedObjectEditor.cs b/MLAPI-Editor/NetworkedObjectEditor.cs
index 65ae98b..349f30d 100644
--- a/MLAPI-Editor/NetworkedObjectEditor.cs
+++ b/MLAPI-Editor/NetworkedObjectEditor.cs
@@ -9,6 +9,7 @@ namespace UnityEditor
{
private bool initialized;
private NetworkedObject networkedObject;
+ private bool showObservers;
private void Init()
{
@@ -44,7 +45,23 @@ namespace UnityEditor
EditorGUILayout.LabelField("isOwner: ", networkedObject.isOwner.ToString(), EditorStyles.label);
EditorGUILayout.LabelField("isPoolObject: ", networkedObject.isPlayerObject.ToString(), EditorStyles.label);
EditorGUILayout.LabelField("isPlayerObject: ", networkedObject.isPlayerObject.ToString(), EditorStyles.label);
- //EditorGUILayout.LabelField("ServerOnly: ", networkedObject.ServerOnly.ToString(), EditorStyles.label);
+
+ if (networkedObject.observers != null && networkedObject.observers.Count > 0)
+ {
+ showObservers = EditorGUILayout.Foldout(showObservers, "Observers");
+ if (showObservers)
+ {
+ EditorGUI.indentLevel += 1;
+ foreach (var o in networkedObject.observers)
+ {
+ if (NetworkingManager.singleton.ConnectedClients[o].PlayerObject != null)
+ EditorGUILayout.ObjectField("ClientId: " + o, NetworkingManager.singleton.ConnectedClients[o].PlayerObject, typeof(GameObject), false);
+ else
+ EditorGUILayout.TextField("ClientId: " + o, EditorStyles.label);
+ }
+ EditorGUI.indentLevel -= 1;
+ }
+ }
}
}
}
diff --git a/MLAPI/Data/SyncedVarField.cs b/MLAPI/Data/SyncedVarField.cs
index d2c132e..f2a3d26 100644
--- a/MLAPI/Data/SyncedVarField.cs
+++ b/MLAPI/Data/SyncedVarField.cs
@@ -1,4 +1,5 @@
-using System.Reflection;
+using System.Collections.Generic;
+using System.Reflection;
namespace MLAPI.Data
{
diff --git a/MLAPI/MLAPI.csproj b/MLAPI/MLAPI.csproj
index 8ee2128..604707c 100644
--- a/MLAPI/MLAPI.csproj
+++ b/MLAPI/MLAPI.csproj
@@ -84,6 +84,7 @@
+
diff --git a/MLAPI/MonoBehaviours/Core/NetworkedBehaviour.cs b/MLAPI/MonoBehaviours/Core/NetworkedBehaviour.cs
index eba61f5..8bc9137 100644
--- a/MLAPI/MonoBehaviours/Core/NetworkedBehaviour.cs
+++ b/MLAPI/MonoBehaviours/Core/NetworkedBehaviour.cs
@@ -142,6 +142,28 @@ namespace MLAPI.MonoBehaviours.Core
internal Dictionary cachedMethods = new Dictionary();
+ public virtual bool OnCheckObserver(uint clientId)
+ {
+ return true;
+ }
+
+ public virtual bool OnRebuildObservers(HashSet observers)
+ {
+ return false;
+ }
+
+ protected void RebuildObservers()
+ {
+ networkedObject.RebuildObservers();
+ }
+
+ public virtual void OnSetLocalVisibility(bool visible)
+ {
+ Renderer[] renderers = GetComponentsInChildren();
+ for (int i = 0; i < renderers.Length; i++)
+ renderers[i].enabled = visible;
+ }
+
private void CacheAttributedMethods()
{
MethodInfo[] methods = GetType().GetMethods(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.FlattenHierarchy);
@@ -193,7 +215,7 @@ namespace MLAPI.MonoBehaviours.Core
FieldTypeHelper.WriteFieldType(writer, methodParams[i], fieldType);
}
- InternalMessageHandler.Send(NetId.ServerNetId.GetClientId(), "MLAPI_COMMAND", "MLAPI_INTERNAL", writer.Finalize());
+ InternalMessageHandler.Send(NetId.ServerNetId.GetClientId(), "MLAPI_COMMAND", "MLAPI_INTERNAL", writer.Finalize(), null);
}
}
@@ -229,8 +251,7 @@ namespace MLAPI.MonoBehaviours.Core
writer.WriteBits((byte)fieldType, 5);
FieldTypeHelper.WriteFieldType(writer, methodParams[i], fieldType);
}
-
- InternalMessageHandler.Send("MLAPI_RPC", "MLAPI_INTERNAL", writer.Finalize());
+ InternalMessageHandler.Send("MLAPI_RPC", "MLAPI_INTERNAL", writer.Finalize(), networkId);
}
}
@@ -265,8 +286,7 @@ namespace MLAPI.MonoBehaviours.Core
writer.WriteBits((byte)fieldType, 5);
FieldTypeHelper.WriteFieldType(writer, methodParams[i], fieldType);
}
-
- InternalMessageHandler.Send(ownerClientId, "MLAPI_RPC", "MLAPI_INTERNAL", writer.Finalize());
+ InternalMessageHandler.Send(ownerClientId, "MLAPI_RPC", "MLAPI_INTERNAL", writer.Finalize(), networkId);
}
}
///
@@ -343,6 +363,7 @@ namespace MLAPI.MonoBehaviours.Core
#region SYNC_VAR
internal List syncedVarFields = new List();
+ private HashSet OutOfSyncClients = new HashSet();
private bool syncVarInit = false;
internal void SyncVarInit()
{
@@ -422,7 +443,9 @@ namespace MLAPI.MonoBehaviours.Core
writer.WriteByte(i); //FieldIndex
FieldTypeHelper.WriteFieldType(writer, syncedVarFields[i].FieldInfo, this, syncedVarFields[i].FieldType);
}
- InternalMessageHandler.Send(clientId, "MLAPI_SYNC_VAR_UPDATE", "MLAPI_INTERNAL", writer.Finalize());
+ bool observed = InternalMessageHandler.Send(clientId, "MLAPI_SYNC_VAR_UPDATE", "MLAPI_INTERNAL", writer.Finalize(), networkId);
+ if (observed)
+ OutOfSyncClients.Remove(clientId);
}
}
@@ -432,7 +455,7 @@ namespace MLAPI.MonoBehaviours.Core
if (!syncVarInit)
SyncVarInit();
SetDirtyness();
- if(NetworkingManager.singleton.NetworkTime - lastSyncTime >= SyncVarSyncDelay)
+ if (NetworkingManager.singleton.NetworkTime - lastSyncTime >= SyncVarSyncDelay)
{
byte nonTargetDirtyCount = 0;
byte totalDirtyCount = 0;
@@ -472,7 +495,12 @@ namespace MLAPI.MonoBehaviours.Core
syncedVarFields[i].Dirty = false;
}
}
- InternalMessageHandler.Send("MLAPI_SYNC_VAR_UPDATE", "MLAPI_INTERNAL", writer.Finalize());
+ List stillDirtyIds = InternalMessageHandler.Send("MLAPI_SYNC_VAR_UPDATE", "MLAPI_INTERNAL", writer.Finalize(), networkId);
+ if (stillDirtyIds != null)
+ {
+ for (int i = 0; i < stillDirtyIds.Count; i++)
+ OutOfSyncClients.Add(stillDirtyIds[i]);
+ }
}
}
else
@@ -501,7 +529,9 @@ namespace MLAPI.MonoBehaviours.Core
}
}
}
- InternalMessageHandler.Send(ownerClientId, "MLAPI_SYNC_VAR_UPDATE", "MLAPI_INTERNAL", writer.Finalize()); //Send only to target
+ bool observing = !InternalMessageHandler.Send(ownerClientId, "MLAPI_SYNC_VAR_UPDATE", "MLAPI_INTERNAL", writer.Finalize(), networkId); //Send only to target
+ if (!observing)
+ OutOfSyncClients.Add(ownerClientId);
}
}
@@ -526,7 +556,12 @@ namespace MLAPI.MonoBehaviours.Core
syncedVarFields[i].Dirty = false;
}
}
- InternalMessageHandler.Send("MLAPI_SYNC_VAR_UPDATE", "MLAPI_INTERNAL", writer.Finalize(), ownerClientId); // Send to everyone except target.
+ List stillDirtyIds = InternalMessageHandler.Send("MLAPI_SYNC_VAR_UPDATE", "MLAPI_INTERNAL", writer.Finalize(), ownerClientId, networkId, null, null); // Send to everyone except target.
+ if (stillDirtyIds != null)
+ {
+ for (int i = 0; i < stillDirtyIds.Count; i++)
+ OutOfSyncClients.Add(stillDirtyIds[i]);
+ }
}
}
lastSyncTime = NetworkingManager.singleton.NetworkTime;
@@ -571,7 +606,7 @@ namespace MLAPI.MonoBehaviours.Core
Debug.LogWarning("MLAPI: Server can not send messages to server.");
return;
}
- InternalMessageHandler.Send(NetId.ServerNetId.GetClientId(), messageType, channelName, data);
+ InternalMessageHandler.Send(NetId.ServerNetId.GetClientId(), messageType, channelName, data, null);
}
///
@@ -609,7 +644,7 @@ namespace MLAPI.MonoBehaviours.Core
Debug.LogWarning("MLAPI: Server can not send messages to server.");
return;
}
- InternalMessageHandler.Send(NetId.ServerNetId.GetClientId(), messageType, channelName, data, networkId, networkedObject.GetOrderIndex(this));
+ InternalMessageHandler.Send(NetId.ServerNetId.GetClientId(), messageType, channelName, data, null, networkId, networkedObject.GetOrderIndex(this));
}
///
@@ -630,7 +665,7 @@ namespace MLAPI.MonoBehaviours.Core
/// User defined messageType
/// User defined channelName
/// The binary data to send
- protected void SendToLocalClient(string messageType, string channelName, byte[] data)
+ protected void SendToLocalClient(string messageType, string channelName, byte[] data, bool respectObservers = false)
{
if (!MessageManager.messageTypes.ContainsKey(messageType))
{
@@ -647,7 +682,8 @@ namespace MLAPI.MonoBehaviours.Core
Debug.LogWarning("MLAPI: Invalid Passthrough send. Ensure AllowPassthroughMessages are turned on and that the MessageType " + messageType + " is registered as a passthroughMessageType");
return;
}
- InternalMessageHandler.Send(ownerClientId, messageType, channelName, data);
+ uint? fromNetId = respectObservers ? (uint?)networkId : null;
+ InternalMessageHandler.Send(ownerClientId, messageType, channelName, data, fromNetId);
}
///
@@ -657,9 +693,9 @@ namespace MLAPI.MonoBehaviours.Core
/// User defined messageType
/// User defined channelName
/// The instance to send
- protected void SendToLocalClient(string messageType, string channelName, T instance)
+ protected void SendToLocalClient(string messageType, string channelName, T instance, bool respectObservers = false)
{
- SendToLocalClient(messageType, channelName, BinarySerializer.Serialize(instance));
+ SendToLocalClient(messageType, channelName, BinarySerializer.Serialize(instance), respectObservers);
}
///
@@ -685,7 +721,7 @@ namespace MLAPI.MonoBehaviours.Core
Debug.LogWarning("MLAPI: Invalid Passthrough send. Ensure AllowPassthroughMessages are turned on and that the MessageType " + messageType + " is registered as a passthroughMessageType");
return;
}
- InternalMessageHandler.Send(ownerClientId, messageType, channelName, data, networkId, networkedObject.GetOrderIndex(this));
+ InternalMessageHandler.Send(ownerClientId, messageType, channelName, data, null, networkId, networkedObject.GetOrderIndex(this));
}
/// gh
@@ -706,7 +742,7 @@ namespace MLAPI.MonoBehaviours.Core
/// User defined messageType
/// User defined channelName
/// The binary data to send
- protected void SendToNonLocalClients(string messageType, string channelName, byte[] data)
+ protected void SendToNonLocalClients(string messageType, string channelName, byte[] data, bool respectObservers = false)
{
if (!MessageManager.messageTypes.ContainsKey(messageType))
{
@@ -723,7 +759,8 @@ namespace MLAPI.MonoBehaviours.Core
Debug.LogWarning("MLAPI: Sending messages from client to other clients is not yet supported");
return;
}
- InternalMessageHandler.Send(messageType, channelName, data, ownerClientId);
+ uint? fromNetId = respectObservers ? (uint?)networkId : null;
+ InternalMessageHandler.Send(messageType, channelName, data, ownerClientId, fromNetId, null, null);
}
///
@@ -733,9 +770,9 @@ namespace MLAPI.MonoBehaviours.Core
/// User defined messageType
/// User defined channelName
/// The instance to send
- protected void SendToNonLocalClients(string messageType, string channelName, T instance)
+ protected void SendToNonLocalClients(string messageType, string channelName, T instance, bool respectObservers = false)
{
- SendToNonLocalClients(messageType, channelName, BinarySerializer.Serialize(instance));
+ SendToNonLocalClients(messageType, channelName, BinarySerializer.Serialize(instance), respectObservers);
}
///
@@ -744,7 +781,7 @@ namespace MLAPI.MonoBehaviours.Core
/// User defined messageType
/// User defined channelName
/// The binary data to send
- protected void SendToNonLocalClientsTarget(string messageType, string channelName, byte[] data)
+ protected void SendToNonLocalClientsTarget(string messageType, string channelName, byte[] data, bool respectObservers = false)
{
if (!MessageManager.messageTypes.ContainsKey(messageType))
{
@@ -761,7 +798,8 @@ namespace MLAPI.MonoBehaviours.Core
Debug.LogWarning("MLAPI: Sending messages from client to other clients is not yet supported");
return;
}
- InternalMessageHandler.Send(messageType, channelName, data, ownerClientId, networkId, networkedObject.GetOrderIndex(this));
+ uint? fromNetId = respectObservers ? (uint?)networkId : null;
+ InternalMessageHandler.Send(messageType, channelName, data, ownerClientId, fromNetId, networkId, networkedObject.GetOrderIndex(this));
}
///
@@ -771,9 +809,9 @@ namespace MLAPI.MonoBehaviours.Core
/// User defined messageType
/// User defined channelName
/// The instance to send
- protected void SendToNonLocalClientsTarget(string messageType, string channelName, T instance)
+ protected void SendToNonLocalClientsTarget(string messageType, string channelName, T instance, bool respectObservers = false)
{
- SendToNonLocalClientsTarget(messageType, channelName, BinarySerializer.Serialize(instance));
+ SendToNonLocalClientsTarget(messageType, channelName, BinarySerializer.Serialize(instance), respectObservers);
}
///
@@ -783,7 +821,7 @@ namespace MLAPI.MonoBehaviours.Core
/// User defined messageType
/// User defined channelName
/// The binary data to send
- protected void SendToClient(uint clientId, string messageType, string channelName, byte[] data)
+ protected void SendToClient(uint clientId, string messageType, string channelName, byte[] data, bool respectObservers = false)
{
if (!MessageManager.messageTypes.ContainsKey(messageType))
{
@@ -800,7 +838,8 @@ namespace MLAPI.MonoBehaviours.Core
Debug.LogWarning("MLAPI: Invalid Passthrough send. Ensure AllowPassthroughMessages are turned on and that the MessageType " + messageType + " is registered as a passthroughMessageType");
return;
}
- InternalMessageHandler.Send(clientId, messageType, channelName, data);
+ uint? fromNetId = respectObservers ? (uint?)networkId : null;
+ InternalMessageHandler.Send(clientId, messageType, channelName, data, fromNetId);
}
///
@@ -811,9 +850,9 @@ namespace MLAPI.MonoBehaviours.Core
/// User defined messageType
/// User defined channelName
/// The instance to send
- protected void SendToClient(int clientId, string messageType, string channelName, T instance)
+ protected void SendToClient(int clientId, string messageType, string channelName, T instance, bool respectObservers = false)
{
- SendToClient(clientId, messageType, channelName, BinarySerializer.Serialize(instance));
+ SendToClient(clientId, messageType, channelName, BinarySerializer.Serialize(instance), respectObservers);
}
///
@@ -823,7 +862,7 @@ namespace MLAPI.MonoBehaviours.Core
/// User defined messageType
/// User defined channelName
/// The binary data to send
- protected void SendToClientTarget(uint clientId, string messageType, string channelName, byte[] data)
+ protected void SendToClientTarget(uint clientId, string messageType, string channelName, byte[] data, bool respectObservers = false)
{
if (!MessageManager.messageTypes.ContainsKey(messageType))
{
@@ -840,7 +879,8 @@ namespace MLAPI.MonoBehaviours.Core
Debug.LogWarning("MLAPI: Invalid Passthrough send. Ensure AllowPassthroughMessages are turned on and that the MessageType " + messageType + " is registered as a passthroughMessageType");
return;
}
- InternalMessageHandler.Send(clientId, messageType, channelName, data, networkId, networkedObject.GetOrderIndex(this));
+ uint? fromNetId = respectObservers ? (uint?)networkId : null;
+ InternalMessageHandler.Send(clientId, messageType, channelName, data, fromNetId, networkId, networkedObject.GetOrderIndex(this));
}
///
@@ -851,9 +891,9 @@ namespace MLAPI.MonoBehaviours.Core
/// User defined messageType
/// User defined channelName
/// The instance to send
- protected void SendToClientTarget(int clientId, string messageType, string channelName, T instance)
+ protected void SendToClientTarget(int clientId, string messageType, string channelName, T instance, bool respectObservers = false)
{
- SendToClientTarget(clientId, messageType, channelName, BinarySerializer.Serialize(instance));
+ SendToClientTarget(clientId, messageType, channelName, BinarySerializer.Serialize(instance), respectObservers);
}
///
@@ -863,7 +903,7 @@ namespace MLAPI.MonoBehaviours.Core
/// User defined messageType
/// User defined channelName
/// The binary data to send
- protected void SendToClients(uint[] clientIds, string messageType, string channelName, byte[] data)
+ protected void SendToClients(uint[] clientIds, string messageType, string channelName, byte[] data, bool respectObservers = false)
{
if (!MessageManager.messageTypes.ContainsKey(messageType))
{
@@ -880,7 +920,8 @@ namespace MLAPI.MonoBehaviours.Core
Debug.LogWarning("MLAPI: Sending messages from client to other clients is not yet supported");
return;
}
- InternalMessageHandler.Send(clientIds, messageType, channelName, data);
+ uint? fromNetId = respectObservers ? (uint?)networkId : null;
+ InternalMessageHandler.Send(clientIds, messageType, channelName, data, fromNetId);
}
///
@@ -891,9 +932,9 @@ namespace MLAPI.MonoBehaviours.Core
/// User defined messageType
/// User defined channelName
/// The instance to send
- protected void SendToClients(int[] clientIds, string messageType, string channelName, T instance)
+ protected void SendToClients(int[] clientIds, string messageType, string channelName, T instance, bool respectObservers = false)
{
- SendToClients(clientIds, messageType, channelName, BinarySerializer.Serialize(instance));
+ SendToClients(clientIds, messageType, channelName, BinarySerializer.Serialize(instance), respectObservers);
}
///
@@ -903,7 +944,7 @@ namespace MLAPI.MonoBehaviours.Core
/// User defined messageType
/// User defined channelName
/// The binary data to send
- protected void SendToClientsTarget(uint[] clientIds, string messageType, string channelName, byte[] data)
+ protected void SendToClientsTarget(uint[] clientIds, string messageType, string channelName, byte[] data, bool respectObservers = false)
{
if (!MessageManager.messageTypes.ContainsKey(messageType))
{
@@ -920,7 +961,8 @@ namespace MLAPI.MonoBehaviours.Core
Debug.LogWarning("MLAPI: Sending messages from client to other clients is not yet supported");
return;
}
- InternalMessageHandler.Send(clientIds, messageType, channelName, data, networkId, networkedObject.GetOrderIndex(this));
+ uint? fromNetId = respectObservers ? (uint?)networkId : null;
+ InternalMessageHandler.Send(clientIds, messageType, channelName, data, fromNetId, networkId, networkedObject.GetOrderIndex(this));
}
///
@@ -931,9 +973,9 @@ namespace MLAPI.MonoBehaviours.Core
/// User defined messageType
/// User defined channelName
/// The instance to send
- protected void SendToClientsTarget(int[] clientIds, string messageType, string channelName, T instance)
+ protected void SendToClientsTarget(int[] clientIds, string messageType, string channelName, T instance, bool respectObservers = false)
{
- SendToClientsTarget(clientIds, messageType, channelName, BinarySerializer.Serialize(instance));
+ SendToClientsTarget(clientIds, messageType, channelName, BinarySerializer.Serialize(instance), respectObservers);
}
///
@@ -943,7 +985,7 @@ namespace MLAPI.MonoBehaviours.Core
/// User defined messageType
/// User defined channelName
/// The binary data to send
- protected void SendToClients(List clientIds, string messageType, string channelName, byte[] data)
+ protected void SendToClients(List clientIds, string messageType, string channelName, byte[] data, bool respectObservers = false)
{
if (!MessageManager.messageTypes.ContainsKey(messageType))
{
@@ -960,7 +1002,8 @@ namespace MLAPI.MonoBehaviours.Core
Debug.LogWarning("MLAPI: Sending messages from client to other clients is not yet supported");
return;
}
- InternalMessageHandler.Send(clientIds, messageType, channelName, data);
+ uint? fromNetId = respectObservers ? (uint?)networkId : null;
+ InternalMessageHandler.Send(clientIds, messageType, channelName, data, fromNetId);
}
///
@@ -971,9 +1014,9 @@ namespace MLAPI.MonoBehaviours.Core
/// User defined messageType
/// User defined channelName
/// The instance to send
- protected void SendToClients(List clientIds, string messageType, string channelName, T instance)
+ protected void SendToClients(List clientIds, string messageType, string channelName, T instance, bool respectObservers = false)
{
- SendToClients(clientIds, messageType, channelName, BinarySerializer.Serialize(instance));
+ SendToClients(clientIds, messageType, channelName, BinarySerializer.Serialize(instance), respectObservers);
}
///
@@ -983,7 +1026,7 @@ namespace MLAPI.MonoBehaviours.Core
/// User defined messageType
/// User defined channelName
/// The binary data to send
- protected void SendToClientsTarget(List clientIds, string messageType, string channelName, byte[] data)
+ protected void SendToClientsTarget(List clientIds, string messageType, string channelName, byte[] data, bool respectObservers = false)
{
if (!MessageManager.messageTypes.ContainsKey(messageType))
{
@@ -1000,7 +1043,8 @@ namespace MLAPI.MonoBehaviours.Core
Debug.LogWarning("MLAPI: Sending messages from client to other clients is not yet supported");
return;
}
- InternalMessageHandler.Send(clientIds, messageType, channelName, data, networkId, networkedObject.GetOrderIndex(this));
+ uint? fromNetId = respectObservers ? (uint?)networkId : null;
+ InternalMessageHandler.Send(clientIds, messageType, channelName, data, fromNetId, networkId, networkedObject.GetOrderIndex(this));
}
///
@@ -1011,9 +1055,9 @@ namespace MLAPI.MonoBehaviours.Core
/// User defined messageType
/// User defined channelName
/// The instance to send
- protected void SendToClientsTarget(List clientIds, string messageType, string channelName, T instance)
+ protected void SendToClientsTarget(List clientIds, string messageType, string channelName, T instance, bool respectObservers = false)
{
- SendToClientsTarget(clientIds, messageType, channelName, BinarySerializer.Serialize(instance));
+ SendToClientsTarget(clientIds, messageType, channelName, BinarySerializer.Serialize(instance), respectObservers);
}
///
@@ -1022,7 +1066,7 @@ namespace MLAPI.MonoBehaviours.Core
/// User defined messageType
/// User defined channelName
/// The binary data to send
- protected void SendToClients(string messageType, string channelName, byte[] data)
+ protected void SendToClients(string messageType, string channelName, byte[] data, bool respectObservers = false)
{
if (!MessageManager.messageTypes.ContainsKey(messageType))
{
@@ -1039,7 +1083,8 @@ namespace MLAPI.MonoBehaviours.Core
Debug.LogWarning("MLAPI: Sending messages from client to other clients is not yet supported");
return;
}
- InternalMessageHandler.Send(messageType, channelName, data);
+ uint? fromNetId = respectObservers ? (uint?)networkId : null;
+ InternalMessageHandler.Send(messageType, channelName, data, fromNetId);
}
///
@@ -1049,9 +1094,9 @@ namespace MLAPI.MonoBehaviours.Core
/// User defined messageType
/// User defined channelName
/// The instance to send
- protected void SendToClients(string messageType, string channelName, T instance)
+ protected void SendToClients(string messageType, string channelName, T instance, bool respectObservers = false)
{
- SendToClients(messageType, channelName, BinarySerializer.Serialize(instance));
+ SendToClients(messageType, channelName, BinarySerializer.Serialize(instance), respectObservers);
}
///
@@ -1060,7 +1105,7 @@ namespace MLAPI.MonoBehaviours.Core
/// User defined messageType
/// User defined channelName
/// The binary data to send
- protected void SendToClientsTarget(string messageType, string channelName, byte[] data)
+ protected void SendToClientsTarget(string messageType, string channelName, byte[] data, bool respectObservers = false)
{
if (!MessageManager.messageTypes.ContainsKey(messageType))
{
@@ -1077,7 +1122,8 @@ namespace MLAPI.MonoBehaviours.Core
Debug.LogWarning("MLAPI: Sending messages from client to other clients is not yet supported");
return;
}
- InternalMessageHandler.Send(messageType, channelName, data, networkId, networkedObject.GetOrderIndex(this));
+ uint? fromNetId = respectObservers ? (uint?)networkId : null;
+ InternalMessageHandler.Send(messageType, channelName, data, fromNetId, networkId, networkedObject.GetOrderIndex(this));
}
///
@@ -1087,9 +1133,9 @@ namespace MLAPI.MonoBehaviours.Core
/// User defined messageType
/// User defined channelName
/// The instance to send
- protected void SendToClientsTarget(string messageType, string channelName, T instance)
+ protected void SendToClientsTarget(string messageType, string channelName, T instance, bool respectObservers = false)
{
- SendToClientsTarget(messageType, channelName, BinarySerializer.Serialize(instance));
+ SendToClientsTarget(messageType, channelName, BinarySerializer.Serialize(instance), respectObservers);
}
#endregion
diff --git a/MLAPI/MonoBehaviours/Core/NetworkedObject.cs b/MLAPI/MonoBehaviours/Core/NetworkedObject.cs
index 9e61a6e..d0994ff 100644
--- a/MLAPI/MonoBehaviours/Core/NetworkedObject.cs
+++ b/MLAPI/MonoBehaviours/Core/NetworkedObject.cs
@@ -1,4 +1,5 @@
using MLAPI.Data;
+using MLAPI.NetworkingManagerComponents.Binary;
using MLAPI.NetworkingManagerComponents.Core;
using System;
using System.Collections.Generic;
@@ -114,6 +115,73 @@ namespace MLAPI.MonoBehaviours.Core
internal bool _isSpawned = false;
internal bool? sceneObject = null;
+ public HashSet observers = new HashSet();
+
+ internal void RebuildObservers(uint? clientId = null)
+ {
+ bool initial = clientId != null;
+ if (initial)
+ {
+ bool shouldBeAdded = true;
+ for (int i = 0; i < childNetworkedBehaviours.Count; i++)
+ {
+ bool state = childNetworkedBehaviours[i].OnCheckObserver(clientId.Value);
+ if (state == false)
+ {
+ shouldBeAdded = false;
+ break;
+ }
+ }
+ if (shouldBeAdded)
+ observers.Add(clientId.Value);
+ }
+ else
+ {
+ HashSet previousObservers = new HashSet(observers);
+ HashSet newObservers = new HashSet();
+ bool update = false;
+ for (int i = 0; i < childNetworkedBehaviours.Count; i++)
+ {
+ bool changed = childNetworkedBehaviours[i].OnRebuildObservers(newObservers);
+ if (changed)
+ {
+ observers = newObservers;
+ update = true;
+ break;
+ }
+ }
+ if (update)
+ {
+ foreach (KeyValuePair pair in NetworkingManager.singleton.connectedClients)
+ {
+ if (new NetId(pair.Key).IsHost())
+ continue;
+ if ((previousObservers.Contains(pair.Key) && !newObservers.Contains(pair.Key)) ||
+ (!previousObservers.Contains(pair.Key) && newObservers.Contains(pair.Key)))
+ {
+ //Something changed for this client.
+ using (BitWriter writer = new BitWriter())
+ {
+ writer.WriteUInt(networkId);
+ writer.WriteBool(observers.Contains(pair.Key));
+
+ InternalMessageHandler.Send(pair.Key, "MLAPI_SET_VISIBILITY", "MLAPI_INTERNAL", writer.Finalize(), null);
+ }
+ FlushToClient(pair.Key);
+ }
+ }
+ }
+ }
+ }
+
+ internal void SetLocalVisibility(bool visibility)
+ {
+ for (int i = 0; i < childNetworkedBehaviours.Count; i++)
+ {
+ childNetworkedBehaviours[i].OnSetLocalVisibility(visibility);
+ }
+ }
+
private void OnDestroy()
{
if (NetworkingManager.singleton != null)
diff --git a/MLAPI/MonoBehaviours/Core/NetworkingManager.cs b/MLAPI/MonoBehaviours/Core/NetworkingManager.cs
index cdc2228..e8d13f5 100644
--- a/MLAPI/MonoBehaviours/Core/NetworkingManager.cs
+++ b/MLAPI/MonoBehaviours/Core/NetworkingManager.cs
@@ -384,6 +384,7 @@ namespace MLAPI.MonoBehaviours.Core
MessageManager.messageTypes.Add("MLAPI_COMMAND", 12);
MessageManager.messageTypes.Add("MLAPI_RPC", 13);
MessageManager.messageTypes.Add("MLAPI_TARGET", 14);
+ MessageManager.messageTypes.Add("MLAPI_SET_VISIBILITY", 15);
List messageTypes = new List(NetworkConfig.MessageTypes)
{
@@ -761,7 +762,7 @@ namespace MLAPI.MonoBehaviours.Core
if (NetworkConfig.ConnectionApproval)
writer.WriteByteArray(NetworkConfig.ConnectionData);
- InternalMessageHandler.Send(netId.GetClientId(), "MLAPI_CONNECTION_REQUEST", "MLAPI_INTERNAL", writer.Finalize(), null, null, true);
+ InternalMessageHandler.Send(netId.GetClientId(), "MLAPI_CONNECTION_REQUEST", "MLAPI_INTERNAL", writer.Finalize(), null, null, null, true);
}
}
break;
@@ -1032,6 +1033,12 @@ namespace MLAPI.MonoBehaviours.Core
InternalMessageHandler.HandleTargetRpc(clientId, incommingData, channelId);
}
break;
+ case 15:
+ if (isClient)
+ {
+ InternalMessageHandler.HandleSetVisibility(clientId, incommingData, channelId);
+ }
+ break;
}
#endregion
}
@@ -1051,6 +1058,9 @@ namespace MLAPI.MonoBehaviours.Core
if (diffieHellmanPublicKeys.ContainsKey(clientId))
diffieHellmanPublicKeys.Remove(clientId);
+ foreach (KeyValuePair pair in SpawnManager.spawnedObjects)
+ pair.Value.observers.Remove(clientId);
+
NetId netId = new NetId(clientId);
if (netId.IsHost() || netId.IsInvalid())
return;
@@ -1080,10 +1090,13 @@ namespace MLAPI.MonoBehaviours.Core
if (isServer)
{
+ foreach (KeyValuePair pair in SpawnManager.spawnedObjects)
+ pair.Value.observers.Remove(clientId);
+
using (BitWriter writer = new BitWriter())
{
writer.WriteUInt(clientId);
- InternalMessageHandler.Send("MLAPI_CLIENT_DISCONNECT", "MLAPI_INTERNAL", writer.Finalize(), clientId);
+ InternalMessageHandler.Send("MLAPI_CLIENT_DISCONNECT", "MLAPI_INTERNAL", writer.Finalize(), clientId, null);
}
}
}
@@ -1098,7 +1111,7 @@ namespace MLAPI.MonoBehaviours.Core
byte[] buffer = writer.Finalize();
foreach (KeyValuePair pair in connectedClients)
- InternalMessageHandler.Send("MLAPI_TIME_SYNC", "MLAPI_TIME_SYNC", buffer);
+ InternalMessageHandler.Send("MLAPI_TIME_SYNC", "MLAPI_TIME_SYNC", buffer, null);
}
}
@@ -1147,7 +1160,7 @@ namespace MLAPI.MonoBehaviours.Core
connectedClients[clientId].PlayerObject = go;
}
- int amountOfObjectsToSend = SpawnManager.spawnedObjects.Values.Count();
+ int amountOfObjectsToSend = SpawnManager.spawnedObjects.Values.Count;
using (BitWriter writer = new BitWriter())
{
@@ -1179,12 +1192,15 @@ namespace MLAPI.MonoBehaviours.Core
foreach (KeyValuePair pair in SpawnManager.spawnedObjects)
{
+ pair.Value.RebuildObservers(clientId); //Rebuilds observers for the new client
+
writer.WriteBool(pair.Value.isPlayerObject);
writer.WriteUInt(pair.Value.NetworkId);
writer.WriteUInt(pair.Value.OwnerClientId);
writer.WriteInt(NetworkConfig.NetworkPrefabIds[pair.Value.NetworkedPrefabName]);
writer.WriteBool(pair.Value.gameObject.activeInHierarchy);
writer.WriteBool(pair.Value.sceneObject == null ? true : pair.Value.sceneObject.Value);
+ writer.WriteBool(pair.Value.observers.Contains(clientId));
writer.WriteFloat(pair.Value.transform.position.x);
writer.WriteFloat(pair.Value.transform.position.y);
@@ -1196,7 +1212,7 @@ namespace MLAPI.MonoBehaviours.Core
}
}
- InternalMessageHandler.Send(clientId, "MLAPI_CONNECTION_APPROVED", "MLAPI_INTERNAL", writer.Finalize(), null, null, true);
+ InternalMessageHandler.Send(clientId, "MLAPI_CONNECTION_APPROVED", "MLAPI_INTERNAL", writer.Finalize(), null, null, null, true);
if (OnClientConnectedCallback != null)
OnClientConnectedCallback.Invoke(clientId);
@@ -1213,6 +1229,7 @@ namespace MLAPI.MonoBehaviours.Core
writer.WriteUInt(clientId);
writer.WriteInt(-1);
writer.WriteBool(false);
+ writer.WriteBool(connectedClients[clientId].PlayerObject.GetComponent().observers.Contains(clientId));
writer.WriteFloat(connectedClients[clientId].PlayerObject.transform.position.x);
writer.WriteFloat(connectedClients[clientId].PlayerObject.transform.position.y);
@@ -1227,7 +1244,7 @@ namespace MLAPI.MonoBehaviours.Core
writer.WriteUInt(clientId);
}
- InternalMessageHandler.Send("MLAPI_ADD_OBJECT", "MLAPI_INTERNAL", writer.Finalize(), clientId);
+ InternalMessageHandler.Send("MLAPI_ADD_OBJECT", "MLAPI_INTERNAL", writer.Finalize(), clientId, null);
}
//Flush syncvars:
foreach (KeyValuePair networkedObject in SpawnManager.spawnedObjects)
diff --git a/MLAPI/MonoBehaviours/Prototyping/NetworkedProximity.cs b/MLAPI/MonoBehaviours/Prototyping/NetworkedProximity.cs
new file mode 100644
index 0000000..bc9c1ac
--- /dev/null
+++ b/MLAPI/MonoBehaviours/Prototyping/NetworkedProximity.cs
@@ -0,0 +1,88 @@
+using MLAPI.MonoBehaviours.Core;
+using System.Collections.Generic;
+using UnityEngine;
+
+namespace MLAPI.MonoBehaviours.Prototyping
+{
+ //This class based on the code from the UNET HLAPI Proximity
+ public class NetworkedProximity : NetworkedBehaviour
+ {
+ public enum CheckMethod
+ {
+ Physics3D,
+ Physics2D
+ };
+
+ [Tooltip("The maximum range that objects will be visible at.")]
+ public int Range = 10;
+
+ [Tooltip("How often (in seconds) that this object should update the set of players that can see it.")]
+ public float VisibilityUpdateInterval = 1.0f; // in seconds
+
+ [Tooltip("Which method to use for checking proximity of players.\n\nPhysics3D uses 3D physics to determine proximity.\n\nPhysics2D uses 2D physics to determine proximity.")]
+ public CheckMethod CheckType = CheckMethod.Physics3D;
+
+ [Tooltip("Enable to force this object to be hidden from players.")]
+ public bool ForceHidden = false;
+
+ private float lastUpdateTime;
+
+ private void Update()
+ {
+ if (!isServer)
+ return;
+
+ if (Time.time - lastUpdateTime > VisibilityUpdateInterval)
+ {
+ RebuildObservers();
+ lastUpdateTime = NetworkingManager.singleton.NetworkTime;
+ }
+ }
+
+ public override bool OnCheckObserver(uint newClientId)
+ {
+ if (ForceHidden)
+ return false;
+ Vector3 pos = NetworkingManager.singleton.ConnectedClients[newClientId].PlayerObject.transform.position;
+ return (pos - transform.position).magnitude < Range;
+ }
+
+ public override bool OnRebuildObservers(HashSet observers)
+ {
+ if (ForceHidden)
+ {
+ // ensure player can still see themself
+ if (networkedObject != null && networkedObject.isPlayerObject)
+ observers.Add(networkedObject.OwnerClientId);
+ return true;
+ }
+
+ switch (CheckType)
+ {
+ case CheckMethod.Physics3D:
+ {
+ var hits = Physics.OverlapSphere(transform.position, Range);
+ for (int i = 0; i < hits.Length; i++)
+ {
+ var uv = hits[i].GetComponent();
+ if (uv != null && uv.isPlayerObject)
+ observers.Add(uv.OwnerClientId);
+ }
+ return true;
+ }
+ case CheckMethod.Physics2D:
+ {
+ var hits = Physics2D.OverlapCircleAll(transform.position, Range);
+ for (int i = 0; i < hits.Length; i++)
+ {
+ var uv = hits[i].GetComponent();
+ if (uv != null && (uv.isPlayerObject))
+ observers.Add(uv.OwnerClientId);
+ }
+ return true;
+ }
+ }
+ return false;
+ }
+ }
+}
diff --git a/MLAPI/MonoBehaviours/Prototyping/NetworkedTransform.cs b/MLAPI/MonoBehaviours/Prototyping/NetworkedTransform.cs
index d00d7da..9e1b817 100644
--- a/MLAPI/MonoBehaviours/Prototyping/NetworkedTransform.cs
+++ b/MLAPI/MonoBehaviours/Prototyping/NetworkedTransform.cs
@@ -122,12 +122,13 @@ namespace MLAPI.MonoBehaviours.Prototyping
writer.Write(transform.position.x);
writer.Write(transform.position.y);
writer.Write(transform.position.z);
+
writer.Write(transform.rotation.eulerAngles.x);
writer.Write(transform.rotation.eulerAngles.y);
writer.Write(transform.rotation.eulerAngles.z);
}
if (isServer)
- SendToClientsTarget("MLAPI_OnRecieveTransformFromServer", "MLAPI_POSITION_UPDATE", positionUpdateBuffer);
+ SendToClientsTarget("MLAPI_OnRecieveTransformFromServer", "MLAPI_POSITION_UPDATE", positionUpdateBuffer, true);
else
SendToServerTarget("MLAPI_OnRecieveTransformFromClient", "MLAPI_POSITION_UPDATE", positionUpdateBuffer);
}
@@ -160,9 +161,11 @@ namespace MLAPI.MonoBehaviours.Prototyping
float xPos = reader.ReadSingle();
float yPos = reader.ReadSingle();
float zPos = reader.ReadSingle();
+
float xRot = reader.ReadSingle();
float yRot = reader.ReadSingle();
float zRot = reader.ReadSingle();
+
lerpStartPos = transform.position;
lerpStartRot = transform.rotation;
lerpEndPos = new Vector3(xPos, yPos, zPos);
@@ -181,9 +184,11 @@ namespace MLAPI.MonoBehaviours.Prototyping
float xPos = reader.ReadSingle();
float yPos = reader.ReadSingle();
float zPos = reader.ReadSingle();
+
float xRot = reader.ReadSingle();
float yRot = reader.ReadSingle();
float zRot = reader.ReadSingle();
+
if (InterpolateServer)
{
lerpStartPos = transform.position;
@@ -215,13 +220,13 @@ namespace MLAPI.MonoBehaviours.Prototyping
{
if (Vector3.Distance(NetworkingManager.singleton.connectedClients[i].PlayerObject.transform.position, transform.position) <= ProximityRange)
{
- SendToClientTarget(NetworkingManager.singleton.connectedClients[i].ClientId, "MLAPI_OnRecieveTransformFromServer", "MLAPI_POSITION_UPDATE", positionUpdateBuffer);
+ SendToClientTarget(NetworkingManager.singleton.connectedClients[i].ClientId, "MLAPI_OnRecieveTransformFromServer", "MLAPI_POSITION_UPDATE", positionUpdateBuffer, true);
}
}
}
else
{
- SendToNonLocalClientsTarget("MLAPI_OnRecieveTransformFromServer", "MLAPI_POSITION_UPDATE", positionUpdateBuffer);
+ SendToNonLocalClientsTarget("MLAPI_OnRecieveTransformFromServer", "MLAPI_POSITION_UPDATE", positionUpdateBuffer, true);
}
}
}
diff --git a/MLAPI/NetworkingManagerComponents/Core/InternalMessageHandler.Receive.cs b/MLAPI/NetworkingManagerComponents/Core/InternalMessageHandler.Receive.cs
index a823c2e..b98d33a 100644
--- a/MLAPI/NetworkingManagerComponents/Core/InternalMessageHandler.Receive.cs
+++ b/MLAPI/NetworkingManagerComponents/Core/InternalMessageHandler.Receive.cs
@@ -98,6 +98,7 @@ namespace MLAPI.NetworkingManagerComponents.Core
int prefabId = reader.ReadInt();
bool isActive = reader.ReadBool();
bool sceneObject = reader.ReadBool();
+ bool visible = reader.ReadBool();
float xPos = reader.ReadFloat();
float yPos = reader.ReadFloat();
@@ -109,13 +110,15 @@ namespace MLAPI.NetworkingManagerComponents.Core
if (isPlayerObject)
{
- SpawnManager.SpawnPlayerObject(ownerId, networkId, new Vector3(xPos, yPos, zPos), Quaternion.Euler(xRot, yRot, zRot));
+ GameObject go = SpawnManager.SpawnPlayerObject(ownerId, networkId, new Vector3(xPos, yPos, zPos), Quaternion.Euler(xRot, yRot, zRot));
+ go.GetComponent().SetLocalVisibility(visible);
}
else
{
GameObject go = SpawnManager.SpawnPrefabIndexClient(prefabId, networkId, ownerId,
new Vector3(xPos, yPos, zPos), Quaternion.Euler(xRot, yRot, zRot));
+ go.GetComponent().SetLocalVisibility(visible);
go.GetComponent().sceneObject = sceneObject;
go.SetActive(isActive);
}
@@ -143,6 +146,7 @@ namespace MLAPI.NetworkingManagerComponents.Core
uint ownerId = reader.ReadUInt();
int prefabId = reader.ReadInt();
bool sceneObject = reader.ReadBool();
+ bool visible = reader.ReadBool();
float xPos = reader.ReadFloat();
float yPos = reader.ReadFloat();
@@ -155,12 +159,15 @@ namespace MLAPI.NetworkingManagerComponents.Core
if (isPlayerObject)
{
netManager.connectedClients.Add(ownerId, new NetworkedClient() { ClientId = ownerId });
- SpawnManager.SpawnPlayerObject(ownerId, networkId, new Vector3(xPos, yPos, zPos), Quaternion.Euler(xRot, yRot, zRot));
+ GameObject go = SpawnManager.SpawnPlayerObject(ownerId, networkId, new Vector3(xPos, yPos, zPos), Quaternion.Euler(xRot, yRot, zRot));
+ go.GetComponent().SetLocalVisibility(visible);
}
else
{
GameObject go = SpawnManager.SpawnPrefabIndexClient(prefabId, networkId, ownerId,
new Vector3(xPos, yPos, zPos), Quaternion.Euler(xRot, yRot, zRot));
+
+ go.GetComponent().SetLocalVisibility(visible);
go.GetComponent().sceneObject = sceneObject;
}
}
@@ -348,6 +355,7 @@ namespace MLAPI.NetworkingManagerComponents.Core
uint ownerId = reader.ReadUInt();
int prefabId = reader.ReadInt();
bool sceneObject = reader.ReadBool();
+ bool visible = reader.ReadBool();
float xPos = reader.ReadFloat();
float yPos = reader.ReadFloat();
@@ -360,12 +368,16 @@ namespace MLAPI.NetworkingManagerComponents.Core
if (isPlayerObject)
{
netManager.connectedClients.Add(ownerId, new NetworkedClient() { ClientId = ownerId });
- SpawnManager.SpawnPlayerObject(ownerId, networkId, new Vector3(xPos, yPos, zPos), Quaternion.Euler(xRot, yRot, zRot));
+ GameObject go = SpawnManager.SpawnPlayerObject(ownerId, networkId, new Vector3(xPos, yPos, zPos), Quaternion.Euler(xRot, yRot, zRot));
+
+ go.GetComponent().SetLocalVisibility(visible);
}
else
{
GameObject go = SpawnManager.SpawnPrefabIndexClient(prefabId, networkId, ownerId,
new Vector3(xPos, yPos, zPos), Quaternion.Euler(xRot, yRot, zRot));
+
+ go.GetComponent().SetLocalVisibility(visible);
go.GetComponent().sceneObject = sceneObject;
}
}
@@ -433,5 +445,13 @@ namespace MLAPI.NetworkingManagerComponents.Core
object[] methodParams = FieldTypeHelper.ReadObjects(reader, paramCount);
targetMethod.Invoke(behaviour, methodParams);
}
+
+ internal static void HandleSetVisibility(uint clientId, byte[] incommingData, int channelId)
+ {
+ BitReader reader = new BitReader(incommingData);
+ uint networkId = reader.ReadUInt();
+ bool visibility = reader.ReadBool();
+ SpawnManager.spawnedObjects[networkId].SetLocalVisibility(visibility);
+ }
}
}
diff --git a/MLAPI/NetworkingManagerComponents/Core/InternalMessageHandler.Send.cs b/MLAPI/NetworkingManagerComponents/Core/InternalMessageHandler.Send.cs
index 3b25def..b9bb698 100644
--- a/MLAPI/NetworkingManagerComponents/Core/InternalMessageHandler.Send.cs
+++ b/MLAPI/NetworkingManagerComponents/Core/InternalMessageHandler.Send.cs
@@ -48,26 +48,30 @@ namespace MLAPI.NetworkingManagerComponents.Core
}
}
- internal static void Send(uint clientId, string messageType, string channelName, byte[] data, uint? networkId = null, ushort? orderId = null, bool skipQueue = false)
+ //RETURNS IF IT SUCCEDED OR FAILED BECAUSE OF NON-OBSERVER. ANY OTHER FAIL WILL RETURN TRUE
+ internal static bool Send(uint clientId, string messageType, string channelName, byte[] data, uint? fromNetId, uint? networkId = null, ushort? orderId = null, bool skipQueue = false)
{
NetId netId = new NetId(clientId);
if (netManager.isHost && netId.IsHost())
{
//Don't invoke the message on our own machine. Instant stack overflow.
Debug.LogWarning("MLAPI: Cannot send message to own client");
- return;
+ return true;
}
else if (netId.IsHost())
{
//Client trying to send data to host
netId = NetId.ServerNetId;
}
+ //If we respect the observers, and the message is targeted (networkId != null) and the targetedNetworkId isnt observing the receiver. Then we return
+ if (netManager.isServer && fromNetId != null && !SpawnManager.spawnedObjects[fromNetId.Value].observers.Contains(clientId))
+ return false;
bool isPassthrough = (!netManager.isServer && clientId != NetId.ServerNetId.GetClientId() && netManager.NetworkConfig.AllowPassthroughMessages);
if (isPassthrough && !netManager.NetworkConfig.PassthroughMessageHashSet.Contains(MessageManager.messageTypes[messageType]))
{
Debug.LogWarning("MLAPI: The The MessageType " + messageType + " is not registered as an allowed passthrough message type.");
- return;
+ return true;
}
using (BitWriter writer = new BitWriter())
@@ -112,10 +116,12 @@ namespace MLAPI.NetworkingManagerComponents.Core
NetworkTransport.Send(netId.HostId, netId.ConnectionId, MessageManager.channels[channelName], FinalMessageBuffer, (int)writer.GetFinalizeSize(), out error);
else
NetworkTransport.QueueMessageForSending(netId.HostId, netId.ConnectionId, MessageManager.channels[channelName], FinalMessageBuffer, (int)writer.GetFinalizeSize(), out error);
+
+ return true;
}
}
- internal static void Send(uint[] clientIds, string messageType, string channelName, byte[] data, uint? networkId = null, ushort? orderId = null)
+ internal static void Send(uint[] clientIds, string messageType, string channelName, byte[] data, uint? fromNetId, uint? networkId = null, ushort? orderId = null)
{
if (netManager.NetworkConfig.EncryptedChannelsHashSet.Contains(channelName))
{
@@ -155,6 +161,10 @@ namespace MLAPI.NetworkingManagerComponents.Core
netId = NetId.ServerNetId;
}
+ //If we respect the observers, and the message is targeted (networkId != null) and the targetedNetworkId isnt observing the receiver. Then we continue
+ if (netManager.isServer && fromNetId != null && !SpawnManager.spawnedObjects[fromNetId.Value].observers.Contains(clientIds[i]))
+ continue;
+
writer.Finalize(ref FinalMessageBuffer);
byte error;
@@ -163,7 +173,7 @@ namespace MLAPI.NetworkingManagerComponents.Core
}
}
- internal static void Send(List clientIds, string messageType, string channelName, byte[] data, uint? networkId = null, ushort? orderId = null)
+ internal static void Send(List clientIds, string messageType, string channelName, byte[] data, uint? fromNetId, uint? networkId = null, ushort? orderId = null)
{
if (netManager.NetworkConfig.EncryptedChannelsHashSet.Contains(channelName))
{
@@ -203,6 +213,10 @@ namespace MLAPI.NetworkingManagerComponents.Core
netId = NetId.ServerNetId;
}
+ //If we respect the observers, and the message is targeted (networkId != null) and the targetedNetworkId isnt observing the receiver. Then we continue
+ if (netManager.isServer && fromNetId != null && !SpawnManager.spawnedObjects[fromNetId.Value].observers.Contains(clientIds[i]))
+ continue;
+
writer.Finalize(ref FinalMessageBuffer);
byte error;
@@ -211,15 +225,19 @@ namespace MLAPI.NetworkingManagerComponents.Core
}
}
- internal static void Send(string messageType, string channelName, byte[] data, uint? networkId = null, ushort? orderId = null)
+ //RETURNS THE CLIENTIDS WHICH WAS NOT BEING OBSERVED
+ internal static List Send(string messageType, string channelName, byte[] data, uint? fromNetId, uint? networkId = null, ushort? orderId = null)
{
if (netManager.connectedClients.Count == 0)
- return;
+ return null;
if (netManager.NetworkConfig.EncryptedChannels.Contains(channelName))
{
Debug.LogWarning("MLAPI: Cannot send messages over encrypted channel to multiple clients.");
- return;
+ return null;
}
+
+ List nonObservedIds = new List();
+
using (BitWriter writer = new BitWriter())
{
writer.WriteUShort(MessageManager.messageTypes[messageType]);
@@ -252,22 +270,33 @@ namespace MLAPI.NetworkingManagerComponents.Core
netId = NetId.ServerNetId;
}
+ //If we respect the observers, and the message is targeted (networkId != null) and the targetedNetworkId isnt observing the receiver. Then we continue
+ if (netManager.isServer && fromNetId != null && !SpawnManager.spawnedObjects[fromNetId.Value].observers.Contains(pair.Key))
+ {
+ nonObservedIds.Add(pair.Key);
+ continue;
+ }
+
writer.Finalize(ref FinalMessageBuffer);
byte error;
NetworkTransport.QueueMessageForSending(netId.HostId, netId.ConnectionId, channel, FinalMessageBuffer, (int)writer.GetFinalizeSize(), out error);
}
+ return nonObservedIds;
}
}
- internal static void Send(string messageType, string channelName, byte[] data, uint clientIdToIgnore, uint? networkId = null, ushort? orderId = null)
+ //RETURNS THE CLIENTIDS WHICH WAS NOT BEING OBSERVED
+ internal static List Send(string messageType, string channelName, byte[] data, uint clientIdToIgnore, uint? fromNetId, uint? networkId = null, ushort? orderId = null)
{
if (netManager.NetworkConfig.EncryptedChannels.Contains(channelName))
{
Debug.LogWarning("MLAPI: Cannot send messages over encrypted channel to multiple clients.");
- return;
+ return null;
}
+ List nonObservedIds = new List();
+
using (BitWriter writer = new BitWriter())
{
writer.WriteUShort(MessageManager.messageTypes[messageType]);
@@ -303,11 +332,19 @@ namespace MLAPI.NetworkingManagerComponents.Core
netId = NetId.ServerNetId;
}
+ //If we respect the observers, and the message is targeted (networkId != null) and the targetedNetworkId isnt observing the receiver. Then we continue
+ if (netManager.isServer && fromNetId != null && !SpawnManager.spawnedObjects[fromNetId.Value].observers.Contains(pair.Key))
+ {
+ nonObservedIds.Add(pair.Key);
+ continue;
+ }
+
writer.Finalize(ref FinalMessageBuffer);
byte error;
NetworkTransport.QueueMessageForSending(netId.HostId, netId.ConnectionId, channel, FinalMessageBuffer, (int)writer.GetFinalizeSize(), out error);
}
+ return nonObservedIds;
}
}
}
diff --git a/MLAPI/NetworkingManagerComponents/Core/NetworkPoolManager.cs b/MLAPI/NetworkingManagerComponents/Core/NetworkPoolManager.cs
index 4e830f7..5e69982 100644
--- a/MLAPI/NetworkingManagerComponents/Core/NetworkPoolManager.cs
+++ b/MLAPI/NetworkingManagerComponents/Core/NetworkPoolManager.cs
@@ -78,7 +78,7 @@ namespace MLAPI.NetworkingManagerComponents.Core
writer.WriteFloat(rotation.eulerAngles.y);
writer.WriteFloat(rotation.eulerAngles.z);
- InternalMessageHandler.Send("MLAPI_SPAWN_POOL_OBJECT", "MLAPI_INTERNAL", writer.Finalize());
+ InternalMessageHandler.Send("MLAPI_SPAWN_POOL_OBJECT", "MLAPI_INTERNAL", writer.Finalize(), null);
}
return go;
}
@@ -99,7 +99,7 @@ namespace MLAPI.NetworkingManagerComponents.Core
{
writer.WriteUInt(netObject.NetworkId);
- InternalMessageHandler.Send("MLAPI_DESTROY_POOL_OBJECT", "MLAPI_INTERNAL", writer.Finalize());
+ InternalMessageHandler.Send("MLAPI_DESTROY_POOL_OBJECT", "MLAPI_INTERNAL", writer.Finalize(), null);
}
}
}
diff --git a/MLAPI/NetworkingManagerComponents/Core/NetworkSceneManager.cs b/MLAPI/NetworkingManagerComponents/Core/NetworkSceneManager.cs
index 75692f9..7b6e2a1 100644
--- a/MLAPI/NetworkingManagerComponents/Core/NetworkSceneManager.cs
+++ b/MLAPI/NetworkingManagerComponents/Core/NetworkSceneManager.cs
@@ -62,7 +62,7 @@ namespace MLAPI.NetworkingManagerComponents.Core
{
writer.WriteUInt(sceneNameToIndex[sceneName]);
- InternalMessageHandler.Send("MLAPI_SWITCH_SCENE", "MLAPI_INTERNAL", writer.Finalize());
+ InternalMessageHandler.Send("MLAPI_SWITCH_SCENE", "MLAPI_INTERNAL", writer.Finalize(), null);
}
}
diff --git a/MLAPI/NetworkingManagerComponents/Core/SpawnManager.cs b/MLAPI/NetworkingManagerComponents/Core/SpawnManager.cs
index ca5e2a1..68dc879 100644
--- a/MLAPI/NetworkingManagerComponents/Core/SpawnManager.cs
+++ b/MLAPI/NetworkingManagerComponents/Core/SpawnManager.cs
@@ -43,7 +43,7 @@ namespace MLAPI.NetworkingManagerComponents.Core
writer.WriteUInt(netId);
writer.WriteUInt(netObject.ownerClientId);
- InternalMessageHandler.Send("MLAPI_CHANGE_OWNER", "MLAPI_INTERNAL", writer.Finalize());
+ InternalMessageHandler.Send("MLAPI_CHANGE_OWNER", "MLAPI_INTERNAL", writer.Finalize(), null);
}
}
@@ -59,7 +59,7 @@ namespace MLAPI.NetworkingManagerComponents.Core
writer.WriteUInt(netId);
writer.WriteUInt(clientId);
- InternalMessageHandler.Send("MLAPI_CHANGE_OWNER", "MLAPI_INTERNAL", writer.Finalize());
+ InternalMessageHandler.Send("MLAPI_CHANGE_OWNER", "MLAPI_INTERNAL", writer.Finalize(), null);
}
}
@@ -130,7 +130,7 @@ namespace MLAPI.NetworkingManagerComponents.Core
writer.WriteFloat(sceneObjectsToSync[i].transform.rotation.eulerAngles.z);
}
- InternalMessageHandler.Send("MLAPI_ADD_OBJECTS", "MLAPI_INTERNAL", writer.Finalize());
+ InternalMessageHandler.Send("MLAPI_ADD_OBJECTS", "MLAPI_INTERNAL", writer.Finalize(), null);
}
}
@@ -211,7 +211,7 @@ namespace MLAPI.NetworkingManagerComponents.Core
writer.WriteFloat(netObject.transform.rotation.eulerAngles.z);
- InternalMessageHandler.Send("MLAPI_ADD_OBJECT", "MLAPI_INTERNAL", writer.Finalize());
+ InternalMessageHandler.Send("MLAPI_ADD_OBJECT", "MLAPI_INTERNAL", writer.Finalize(), null);
}
}
@@ -265,7 +265,7 @@ namespace MLAPI.NetworkingManagerComponents.Core
{
writer.WriteUInt(networkId);
- InternalMessageHandler.Send("MLAPI_DESTROY_OBJECT", "MLAPI_INTERNAL", writer.Finalize());
+ InternalMessageHandler.Send("MLAPI_DESTROY_OBJECT", "MLAPI_INTERNAL", writer.Finalize(), null);
}
}
}