42 changes: 15 additions & 27 deletions Mirror/Runtime/NetworkServer.cs
Expand Up @@ -30,7 +30,6 @@ public sealed class NetworkServer
public static Dictionary<int, NetworkConnection> connections = new Dictionary<int, NetworkConnection>();
public static Dictionary<short, NetworkMessageDelegate> handlers = new Dictionary<short, NetworkMessageDelegate>();

public static Dictionary<uint, NetworkIdentity> objects { get { return s_NetworkScene.localObjects; } }
public static bool dontListen;
public static bool useWebSockets;

Expand Down Expand Up @@ -187,27 +186,20 @@ internal static void RemoveLocalClient(NetworkConnection localClientConnection)
RemoveConnection(0);
}

internal static void SetLocalObjectOnServer(uint netId, GameObject obj)
{
if (LogFilter.Debug) { Debug.Log("SetLocalObjectOnServer " + netId + " " + obj); }

s_NetworkScene.SetLocalObject(netId, obj, false, true);
}

internal static void ActivateLocalClientScene()
{
if (s_LocalClientActive)
return;

// ClientScene for a local connection is becoming active. any spawned objects need to be started as client objects
s_LocalClientActive = true;
foreach (var uv in objects.Values)
foreach (var uv in NetworkIdentity.spawned.Values)
{
if (!uv.isClient)
{
if (LogFilter.Debug) { Debug.Log("ActivateClientScene " + uv.netId + " " + uv.gameObject); }
if (LogFilter.Debug) { Debug.Log("ActivateClientScene " + uv.netId + " " + uv); }

ClientScene.SetLocalObject(uv.netId, uv.gameObject);
uv.EnableIsClient();
uv.OnStartClient();
}
}
Expand Down Expand Up @@ -322,7 +314,7 @@ static void UpdateServerObjects()
// vis2k: original code only removed null entries every 100 frames. this was unnecessarily complicated and
// probably even slower than removing null entries each time (hence less iterations next time).
List<uint> removeNetIds = new List<uint>();
foreach (var kvp in objects)
foreach (var kvp in NetworkIdentity.spawned)
{
if (kvp.Value != null && kvp.Value.gameObject != null)
{
Expand All @@ -337,7 +329,7 @@ static void UpdateServerObjects()
// now remove
foreach (uint netId in removeNetIds)
{
objects.Remove(netId);
NetworkIdentity.spawned.Remove(netId);
}
}

Expand Down Expand Up @@ -748,7 +740,7 @@ internal static void SetClientReadyInternal(NetworkConnection conn)
// Setup spawned objects for local player
// Only handle the local objects for the first player (no need to redo it when doing more local players)
// and don't handle player objects here, they were done above
foreach (NetworkIdentity uv in objects.Values)
foreach (NetworkIdentity uv in NetworkIdentity.spawned.Values)
{
// Need to call OnStartClient directly here, as it's already been added to the local object dictionary
// in the above SetLocalPlayer call
Expand All @@ -770,13 +762,13 @@ internal static void SetClientReadyInternal(NetworkConnection conn)
}

// Spawn/update all current server objects
if (LogFilter.Debug) { Debug.Log("Spawning " + objects.Count + " objects for conn " + conn.connectionId); }
if (LogFilter.Debug) { Debug.Log("Spawning " + NetworkIdentity.spawned.Count + " objects for conn " + conn.connectionId); }

ObjectSpawnFinishedMessage msg = new ObjectSpawnFinishedMessage();
msg.state = 0;
conn.Send((short)MsgType.SpawnFinished, msg);

foreach (NetworkIdentity uv in objects.Values)
foreach (NetworkIdentity uv in NetworkIdentity.spawned.Values)
{
if (uv == null)
{
Expand Down Expand Up @@ -1041,10 +1033,7 @@ static void DestroyObject(GameObject obj)
static void DestroyObject(NetworkIdentity uv, bool destroyServerObject)
{
if (LogFilter.Debug) { Debug.Log("DestroyObject instance:" + uv.netId); }
if (objects.ContainsKey(uv.netId))
{
objects.Remove(uv.netId);
}
NetworkIdentity.spawned.Remove(uv.netId);

if (uv.clientAuthorityOwner != null)
{
Expand All @@ -1059,7 +1048,6 @@ static void DestroyObject(NetworkIdentity uv, bool destroyServerObject)
if (NetworkClient.active && s_LocalClientActive)
{
uv.OnNetworkDestroy();
ClientScene.SetLocalObject(msg.netId, null);
}

// when unspawning, dont destroy the server's object
Expand All @@ -1070,11 +1058,6 @@ static void DestroyObject(NetworkIdentity uv, bool destroyServerObject)
uv.MarkForReset();
}

public static void ClearLocalObjects()
{
objects.Clear();
}

public static void Spawn(GameObject obj)
{
if (VerifyCanSpawn(obj))
Expand Down Expand Up @@ -1197,7 +1180,12 @@ internal static bool InvokeBytes(ULocalConnectionToServer conn, byte[] buffer)

public static GameObject FindLocalObject(uint netId)
{
return s_NetworkScene.FindLocalObject(netId);
NetworkIdentity ni;
if (NetworkIdentity.spawned.TryGetValue(netId, out ni) && ni != null)
{
return ni.gameObject;
}
return null;
}

static bool ValidateSceneObject(NetworkIdentity netId)
Expand Down