NetworkIdentity.RebuildObservers code split into AddAllServerConnectionsToObservers helper function

This commit is contained in:
vis2k 2020-03-03 23:29:47 +01:00
parent d6a93275f9
commit 66d3e8ddf7

View File

@ -975,6 +975,7 @@ internal void AddObserver(NetworkConnection conn)
// -> HashSet is passed in so we can cache it! // -> HashSet is passed in so we can cache it!
// -> returns true if any of the components implemented // -> returns true if any of the components implemented
// OnRebuildObservers, false otherwise // OnRebuildObservers, false otherwise
// -> initialize is true on first rebuild, false on consecutive rebuilds
internal bool GetNewObservers(HashSet<NetworkConnection> observersSet, bool initialize) internal bool GetNewObservers(HashSet<NetworkConnection> observersSet, bool initialize)
{ {
bool rebuildOverwritten = false; bool rebuildOverwritten = false;
@ -988,6 +989,25 @@ internal bool GetNewObservers(HashSet<NetworkConnection> observersSet, bool init
return rebuildOverwritten; return rebuildOverwritten;
} }
// helper function to add all server connections as observers.
// this is used if none of the components provides their own
// OnRebuildObservers function.
internal void AddAllServerConnectionsToObservers()
{
// add all server connections
foreach (NetworkConnection conn in NetworkServer.connections.Values)
{
if (conn.isReady)
AddObserver(conn);
}
// add local host connection (if any)
if (NetworkServer.localConnection != null && NetworkServer.localConnection.isReady)
{
AddObserver(NetworkServer.localConnection);
}
}
static readonly HashSet<NetworkConnection> newObservers = new HashSet<NetworkConnection>(); static readonly HashSet<NetworkConnection> newObservers = new HashSet<NetworkConnection>();
/// <summary> /// <summary>
@ -1014,21 +1034,14 @@ public void RebuildObservers(bool initialize)
} }
// if no component implemented OnRebuildObservers, then add all // if no component implemented OnRebuildObservers, then add all
// connections. // server connections.
if (!rebuildOverwritten) if (!rebuildOverwritten)
{ {
// only add all connections when rebuilding the first time.
// second time we just keep them without rebuilding anything.
if (initialize) if (initialize)
{ {
foreach (NetworkConnection conn in NetworkServer.connections.Values) AddAllServerConnectionsToObservers();
{
if (conn.isReady)
AddObserver(conn);
}
if (NetworkServer.localConnection != null && NetworkServer.localConnection.isReady)
{
AddObserver(NetworkServer.localConnection);
}
} }
return; return;
} }