Skip to content

Commit 370121a

Browse files
committed
Added support for object visiblity
1 parent 3ee1b1c commit 370121a

File tree

11 files changed

+401
-361
lines changed

11 files changed

+401
-361
lines changed

MLAPI-Editor/NetworkingManagerEditor.cs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -124,11 +124,9 @@ public override void OnInspectorGUI()
124124
EditorGUILayout.PropertyField(RunInBackgroundProperty);
125125
EditorGUILayout.PropertyField(LogLevelProperty);
126126

127-
if (networkingManager.NetworkConfig.HandleObjectSpawning)
128-
{
129-
EditorGUILayout.Space();
130-
networkPrefabsList.DoLayoutList();
131-
}
127+
EditorGUILayout.Space();
128+
networkPrefabsList.DoLayoutList();
129+
132130
EditorGUILayout.Space();
133131
channelsList.DoLayoutList();
134132
EditorGUILayout.Space();

MLAPI/Data/MLAPIConstants.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ public static class MLAPIConstants
1414
public const byte MLAPI_CONNECTION_REQUEST = 3;
1515
public const byte MLAPI_CONNECTION_APPROVED = 4;
1616
public const byte MLAPI_ADD_OBJECT = 5;
17-
public const byte MLAPI_CLIENT_DISCONNECT = 6;
1817
public const byte MLAPI_DESTROY_OBJECT = 7;
1918
public const byte MLAPI_SWITCH_SCENE = 8;
2019
public const byte MLAPI_CLIENT_SWITCH_SCENE_COMPLETED = 9;

MLAPI/Data/NetworkConfig.cs

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -111,10 +111,6 @@ public class NetworkConfig
111111
/// </summary>
112112
public int SecondsHistory = 5;
113113
/// <summary>
114-
/// Wheter or not to make the library handle object spawning
115-
/// </summary>
116-
public bool HandleObjectSpawning = true;
117-
/// <summary>
118114
/// Wheter or not to enable scene switching
119115
/// </summary>
120116
public bool EnableSceneSwitching = true;
@@ -235,7 +231,6 @@ public string ToBase64()
235231
writer.WriteInt32Packed(config.ClientConnectionBufferTimeout);
236232
writer.WriteBool(config.ConnectionApproval);
237233
writer.WriteInt32Packed(config.SecondsHistory);
238-
writer.WriteBool(config.HandleObjectSpawning);
239234
writer.WriteBool(config.EnableEncryption);
240235
writer.WriteBool(config.SignKeyExchange);
241236
writer.WriteBool(config.EnableSceneSwitching);
@@ -317,7 +312,6 @@ public void FromBase64(string base64, bool createDummyObject = false)
317312
config.ClientConnectionBufferTimeout = reader.ReadInt32Packed();
318313
config.ConnectionApproval = reader.ReadBool();
319314
config.SecondsHistory = reader.ReadInt32Packed();
320-
config.HandleObjectSpawning = reader.ReadBool();
321315
config.EnableEncryption = reader.ReadBool();
322316
config.SignKeyExchange = reader.ReadBool();
323317
config.EnableSceneSwitching = reader.ReadBool();
@@ -363,7 +357,7 @@ public ulong GetConfig(bool cache = true)
363357
}
364358
}
365359

366-
if (HandleObjectSpawning && ForceSamePrefabs)
360+
if (ForceSamePrefabs)
367361
{
368362
List<NetworkedPrefab> sortedPrefabList = NetworkedPrefabs.OrderBy(x => x.hash).ToList();
369363
for (int i = 0; i < sortedPrefabList.Count; i++)
@@ -373,7 +367,6 @@ public ulong GetConfig(bool cache = true)
373367
}
374368

375369
writer.WriteBool(ForceSamePrefabs);
376-
writer.WriteBool(HandleObjectSpawning);
377370
writer.WriteBool(EnableEncryption);
378371
writer.WriteBool(EnableSceneSwitching);
379372
writer.WriteBool(SignKeyExchange);

MLAPI/MonoBehaviours/Core/NetworkedBehaviour.cs

Lines changed: 68 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -346,48 +346,52 @@ internal void NetworkedVarUpdate()
346346

347347
for (int i = 0; i < NetworkingManager.Singleton.ConnectedClientsList.Count; i++)
348348
{
349-
//This iterates over every "channel group".
350-
for (int j = 0; j < channelMappedVarIndexes.Count; j++)
349+
// Do this check here to prevent doing all the expensive dirty checks
350+
if (!IsServer || this.NetworkedObject.observers.Contains(NetworkingManager.Singleton.ConnectedClientsList[i].ClientId))
351351
{
352-
using (PooledBitStream stream = PooledBitStream.Get())
352+
//This iterates over every "channel group".
353+
for (int j = 0; j < channelMappedVarIndexes.Count; j++)
353354
{
354-
using (PooledBitWriter writer = PooledBitWriter.Get(stream))
355+
using (PooledBitStream stream = PooledBitStream.Get())
355356
{
356-
writer.WriteUInt32Packed(NetworkId);
357-
writer.WriteUInt16Packed(NetworkedObject.GetOrderIndex(this));
358-
359-
uint clientId = NetworkingManager.Singleton.ConnectedClientsList[i].ClientId;
360-
bool writtenAny = false;
361-
for (int k = 0; k < networkedVarFields.Count; k++)
357+
using (PooledBitWriter writer = PooledBitWriter.Get(stream))
362358
{
363-
if (!channelMappedVarIndexes[j].Contains(k))
359+
writer.WriteUInt32Packed(NetworkId);
360+
writer.WriteUInt16Packed(NetworkedObject.GetOrderIndex(this));
361+
362+
uint clientId = NetworkingManager.Singleton.ConnectedClientsList[i].ClientId;
363+
bool writtenAny = false;
364+
for (int k = 0; k < networkedVarFields.Count; k++)
364365
{
365-
//This var does not belong to the currently iterating channel group.
366-
writer.WriteBool(false);
367-
continue;
368-
}
366+
if (!channelMappedVarIndexes[j].Contains(k))
367+
{
368+
//This var does not belong to the currently iterating channel group.
369+
writer.WriteBool(false);
370+
continue;
371+
}
369372

370-
bool isDirty = networkedVarFields[k].IsDirty(); //cache this here. You never know what operations users will do in the dirty methods
371-
writer.WriteBool(isDirty);
373+
bool isDirty = networkedVarFields[k].IsDirty(); //cache this here. You never know what operations users will do in the dirty methods
374+
writer.WriteBool(isDirty);
372375

373-
if (isDirty && (!IsServer || networkedVarFields[k].CanClientRead(clientId)))
374-
{
375-
writtenAny = true;
376-
networkedVarFields[k].WriteDelta(stream);
377-
if (!networkedVarIndexesToResetSet.Contains(k))
376+
if (isDirty && (!IsServer || networkedVarFields[k].CanClientRead(clientId)))
378377
{
379-
networkedVarIndexesToResetSet.Add(k);
380-
networkedVarIndexesToReset.Add(k);
378+
writtenAny = true;
379+
networkedVarFields[k].WriteDelta(stream);
380+
if (!networkedVarIndexesToResetSet.Contains(k))
381+
{
382+
networkedVarIndexesToResetSet.Add(k);
383+
networkedVarIndexesToReset.Add(k);
384+
}
381385
}
382386
}
383-
}
384-
385-
if (writtenAny)
386-
{
387-
if (IsServer)
388-
InternalMessageHandler.Send(clientId, MLAPIConstants.MLAPI_NETWORKED_VAR_DELTA, channelsForVarGroups[j], stream, SecuritySendFlags.None);
389-
else
390-
InternalMessageHandler.Send(NetworkingManager.Singleton.ServerClientId, MLAPIConstants.MLAPI_NETWORKED_VAR_DELTA, channelsForVarGroups[j], stream, SecuritySendFlags.None);
387+
388+
if (writtenAny)
389+
{
390+
if (IsServer)
391+
InternalMessageHandler.Send(clientId, MLAPIConstants.MLAPI_NETWORKED_VAR_DELTA, channelsForVarGroups[j], stream, SecuritySendFlags.None, this.NetworkedObject);
392+
else
393+
InternalMessageHandler.Send(NetworkingManager.Singleton.ServerClientId, MLAPIConstants.MLAPI_NETWORKED_VAR_DELTA, channelsForVarGroups[j], stream, SecuritySendFlags.None, null);
394+
}
391395
}
392396
}
393397
}
@@ -875,7 +879,7 @@ internal void SendServerRPCPerformance(ulong hash, Stream messageStream, string
875879
}
876880
else
877881
{
878-
InternalMessageHandler.Send(NetworkingManager.Singleton.ServerClientId, MLAPIConstants.MLAPI_SERVER_RPC, string.IsNullOrEmpty(channel) ? "MLAPI_DEFAULT_MESSAGE" : channel, stream, security);
882+
InternalMessageHandler.Send(NetworkingManager.Singleton.ServerClientId, MLAPIConstants.MLAPI_SERVER_RPC, string.IsNullOrEmpty(channel) ? "MLAPI_DEFAULT_MESSAGE" : channel, stream, security, null);
879883
}
880884
}
881885
}
@@ -932,7 +936,7 @@ internal RpcResponse<T> SendServerRPCPerformanceResponse<T>(ulong hash, Stream m
932936

933937
ResponseMessageManager.Add(response.Id, response);
934938

935-
InternalMessageHandler.Send(NetworkingManager.Singleton.ServerClientId, MLAPIConstants.MLAPI_SERVER_RPC_REQUEST, string.IsNullOrEmpty(channel) ? "MLAPI_DEFAULT_MESSAGE" : channel, stream, security);
939+
InternalMessageHandler.Send(NetworkingManager.Singleton.ServerClientId, MLAPIConstants.MLAPI_SERVER_RPC_REQUEST, string.IsNullOrEmpty(channel) ? "MLAPI_DEFAULT_MESSAGE" : channel, stream, security, null);
936940

937941
return response;
938942
}
@@ -963,29 +967,40 @@ internal void SendClientRPCPerformance(ulong hash, List<uint> clientIds, Stream
963967
{
964968
for (int i = 0; i < NetworkingManager.Singleton.ConnectedClientsList.Count; i++)
965969
{
970+
if (!this.NetworkedObject.observers.Contains(NetworkingManager.Singleton.ConnectedClientsList[i].ClientId))
971+
{
972+
continue;
973+
}
974+
966975
if (IsHost && NetworkingManager.Singleton.ConnectedClientsList[i].ClientId == NetworkingManager.Singleton.LocalClientId)
967976
{
968977
messageStream.Position = 0;
969978
InvokeClientRPCLocal(hash, NetworkingManager.Singleton.LocalClientId, messageStream);
970979
}
971980
else
972981
{
973-
InternalMessageHandler.Send(NetworkingManager.Singleton.ConnectedClientsList[i].ClientId, MLAPIConstants.MLAPI_CLIENT_RPC, string.IsNullOrEmpty(channel) ? "MLAPI_DEFAULT_MESSAGE" : channel, stream, security);
982+
InternalMessageHandler.Send(NetworkingManager.Singleton.ConnectedClientsList[i].ClientId, MLAPIConstants.MLAPI_CLIENT_RPC, string.IsNullOrEmpty(channel) ? "MLAPI_DEFAULT_MESSAGE" : channel, stream, security, null);
974983
}
975984
}
976985
}
977986
else
978987
{
979988
for (int i = 0; i < clientIds.Count; i++)
980989
{
990+
if (!this.NetworkedObject.observers.Contains(clientIds[i]))
991+
{
992+
if (LogHelper.CurrentLogLevel <= LogLevel.Normal) LogHelper.LogWarning("Cannot send ClientRPC to client without visibility to the object");
993+
continue;
994+
}
995+
981996
if (IsHost && clientIds[i] == NetworkingManager.Singleton.LocalClientId)
982997
{
983998
messageStream.Position = 0;
984999
InvokeClientRPCLocal(hash, NetworkingManager.Singleton.LocalClientId, messageStream);
9851000
}
9861001
else
9871002
{
988-
InternalMessageHandler.Send(clientIds[i], MLAPIConstants.MLAPI_CLIENT_RPC, string.IsNullOrEmpty(channel) ? "MLAPI_DEFAULT_MESSAGE" : channel, stream, security);
1003+
InternalMessageHandler.Send(clientIds[i], MLAPIConstants.MLAPI_CLIENT_RPC, string.IsNullOrEmpty(channel) ? "MLAPI_DEFAULT_MESSAGE" : channel, stream, security, null);
9891004
}
9901005
}
9911006
}
@@ -1015,16 +1030,17 @@ internal void SendClientRPCPerformance(ulong hash, Stream messageStream, uint cl
10151030

10161031
for (int i = 0; i < NetworkingManager.Singleton.ConnectedClientsList.Count; i++)
10171032
{
1018-
if (NetworkingManager.Singleton.ConnectedClientsList[i].ClientId == clientIdToIgnore)
1033+
if (NetworkingManager.Singleton.ConnectedClientsList[i].ClientId == clientIdToIgnore || !this.NetworkedObject.observers.Contains(NetworkingManager.Singleton.ConnectedClientsList[i].ClientId))
10191034
continue;
1035+
10201036
if (IsHost && NetworkingManager.Singleton.ConnectedClientsList[i].ClientId == NetworkingManager.Singleton.LocalClientId)
10211037
{
10221038
messageStream.Position = 0;
10231039
InvokeClientRPCLocal(hash, NetworkingManager.Singleton.LocalClientId, messageStream);
10241040
}
10251041
else
10261042
{
1027-
InternalMessageHandler.Send(NetworkingManager.Singleton.ConnectedClientsList[i].ClientId, MLAPIConstants.MLAPI_CLIENT_RPC, string.IsNullOrEmpty(channel) ? "MLAPI_DEFAULT_MESSAGE" : channel, stream, security);
1043+
InternalMessageHandler.Send(NetworkingManager.Singleton.ConnectedClientsList[i].ClientId, MLAPIConstants.MLAPI_CLIENT_RPC, string.IsNullOrEmpty(channel) ? "MLAPI_DEFAULT_MESSAGE" : channel, stream, security, null);
10281044
}
10291045
}
10301046
}
@@ -1039,6 +1055,12 @@ internal void SendClientRPCPerformance(ulong hash, uint clientId, Stream message
10391055
if (LogHelper.CurrentLogLevel <= LogLevel.Normal) LogHelper.LogWarning("Only clients and host can invoke ClientRPC");
10401056
return;
10411057
}
1058+
1059+
if (!this.NetworkedObject.observers.Contains(clientId))
1060+
{
1061+
if (LogHelper.CurrentLogLevel <= LogLevel.Normal) LogHelper.LogWarning("Cannot send ClientRPC to client without visibility to the object");
1062+
return;
1063+
}
10421064

10431065
using (PooledBitStream stream = PooledBitStream.Get())
10441066
{
@@ -1057,7 +1079,7 @@ internal void SendClientRPCPerformance(ulong hash, uint clientId, Stream message
10571079
}
10581080
else
10591081
{
1060-
InternalMessageHandler.Send(clientId, MLAPIConstants.MLAPI_CLIENT_RPC, string.IsNullOrEmpty(channel) ? "MLAPI_DEFAULT_MESSAGE" : channel, stream, security);
1082+
InternalMessageHandler.Send(clientId, MLAPIConstants.MLAPI_CLIENT_RPC, string.IsNullOrEmpty(channel) ? "MLAPI_DEFAULT_MESSAGE" : channel, stream, security, null);
10611083
}
10621084
}
10631085
}
@@ -1072,6 +1094,12 @@ internal RpcResponse<T> SendClientRPCPerformanceResponse<T>(ulong hash, uint cli
10721094
return null;
10731095
}
10741096

1097+
if (!this.NetworkedObject.observers.Contains(clientId))
1098+
{
1099+
if (LogHelper.CurrentLogLevel <= LogLevel.Normal) LogHelper.LogWarning("Cannot send ClientRPC to client without visibility to the object");
1100+
return null;
1101+
}
1102+
10751103
ulong responseId = ResponseMessageManager.GenerateMessageId();
10761104

10771105
using (PooledBitStream stream = PooledBitStream.Get())
@@ -1114,7 +1142,7 @@ internal RpcResponse<T> SendClientRPCPerformanceResponse<T>(ulong hash, uint cli
11141142

11151143
ResponseMessageManager.Add(response.Id, response);
11161144

1117-
InternalMessageHandler.Send(clientId, MLAPIConstants.MLAPI_CLIENT_RPC_REQUEST, string.IsNullOrEmpty(channel) ? "MLAPI_DEFAULT_MESSAGE" : channel, stream, security);
1145+
InternalMessageHandler.Send(clientId, MLAPIConstants.MLAPI_CLIENT_RPC_REQUEST, string.IsNullOrEmpty(channel) ? "MLAPI_DEFAULT_MESSAGE" : channel, stream, security, null);
11181146

11191147
return response;
11201148
}

0 commit comments

Comments
 (0)