NetworkIdentity.spawned dictionary to replace ClientScene.objects, NetworkServer.objects, NetworkScene.objects

This commit is contained in:
vis2k 2018-12-16 15:25:36 +01:00
parent 582bf98bdd
commit d979a45870
4 changed files with 25 additions and 29 deletions

View File

@ -1,7 +1,9 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using Guid = System.Guid;
using Object = UnityEngine.Object;
namespace Mirror
{
@ -25,9 +27,6 @@ internal static void SetNotReady()
public static bool ready { get { return s_IsReady; } }
public static NetworkConnection readyConnection { get { return s_ReadyConnection; }}
//NOTE: spawn handlers, prefabs and local objects now live in NetworkScene
// objects by net id
public static Dictionary<uint, NetworkIdentity> objects { get { return s_NetworkScene.localObjects; } }
public static Dictionary<Guid, GameObject> prefabs { get { return NetworkScene.guidToPrefab; } }
// scene id to NetworkIdentity
public static Dictionary<uint, NetworkIdentity> spawnableObjects;
@ -421,7 +420,7 @@ static void OnObjectSpawnFinished(NetworkMessage netMsg)
// paul: Initialize the objects in the same order as they were initialized
// in the server. This is important if spawned objects
// use data from scene objects
foreach (var uv in objects.Values.OrderBy(uv => uv.netId))
foreach (var uv in NetworkIdentity.spawned.Values.OrderBy(uv => uv.netId))
{
if (!uv.isClient)
{

View File

@ -50,6 +50,9 @@ public sealed class NetworkIdentity : MonoBehaviour
public bool localPlayerAuthority { get { return m_LocalPlayerAuthority; } set { m_LocalPlayerAuthority = value; } }
public NetworkConnection clientAuthorityOwner { get { return m_ClientAuthorityOwner; }}
// all spawned NetworkIdentities by netId. needed on server and client.
public static Dictionary<uint, NetworkIdentity> spawned = new Dictionary<uint, NetworkIdentity>();
public NetworkBehaviour[] NetworkBehaviours
{
get

View File

@ -8,11 +8,6 @@ namespace Mirror
// This code (mostly) used to be in ClientScene.
internal class NetworkScene
{
// localObjects is NOT static. For the Host, even though there is one scene and gameObjects are
// shared with the localClient, the set of active objects for each must be separate to prevent
// out-of-order object initialization problems.
internal Dictionary<uint, NetworkIdentity> localObjects = new Dictionary<uint, NetworkIdentity>();
internal static Dictionary<Guid, GameObject> guidToPrefab = new Dictionary<Guid, GameObject>();
internal static Dictionary<Guid, SpawnDelegate> spawnHandlers = new Dictionary<Guid, SpawnDelegate>();
internal static Dictionary<Guid, UnSpawnDelegate> unspawnHandlers = new Dictionary<Guid, UnSpawnDelegate>();
@ -29,20 +24,20 @@ internal void SetLocalObject(uint netId, GameObject obj, bool isClient, bool isS
if (obj == null)
{
localObjects[netId] = null;
NetworkIdentity.spawned[netId] = null;
return;
}
NetworkIdentity foundNetworkIdentity = null;
if (localObjects.ContainsKey(netId))
if (NetworkIdentity.spawned.ContainsKey(netId))
{
foundNetworkIdentity = localObjects[netId];
foundNetworkIdentity = NetworkIdentity.spawned[netId];
}
if (foundNetworkIdentity == null)
{
foundNetworkIdentity = obj.GetComponent<NetworkIdentity>();
localObjects[netId] = foundNetworkIdentity;
NetworkIdentity.spawned[netId] = foundNetworkIdentity;
}
foundNetworkIdentity.UpdateClientServer(isClient, isServer);
@ -63,17 +58,17 @@ internal GameObject FindLocalObject(uint netId)
internal bool GetNetworkIdentity(uint netId, out NetworkIdentity uv)
{
return localObjects.TryGetValue(netId, out uv) && uv != null;
return NetworkIdentity.spawned.TryGetValue(netId, out uv) && uv != null;
}
internal bool RemoveLocalObject(uint netId)
{
return localObjects.Remove(netId);
return NetworkIdentity.spawned.Remove(netId);
}
internal void ClearLocalObjects()
{
localObjects.Clear();
NetworkIdentity.spawned.Clear();
}
internal static void RegisterPrefab(GameObject prefab, Guid newAssetId)
@ -214,9 +209,9 @@ internal static bool InvokeUnSpawnHandler(Guid assetId, GameObject obj)
internal void DestroyAllClientObjects()
{
foreach (var netId in localObjects.Keys)
foreach (var netId in NetworkIdentity.spawned.Keys)
{
NetworkIdentity uv = localObjects[netId];
NetworkIdentity uv = NetworkIdentity.spawned[netId];
if (uv != null && uv.gameObject != null)
{

View File

@ -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;
@ -201,7 +200,7 @@ internal static void ActivateLocalClientScene()
// 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)
{
@ -322,7 +321,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)
{
@ -337,7 +336,7 @@ static void UpdateServerObjects()
// now remove
foreach (uint netId in removeNetIds)
{
objects.Remove(netId);
NetworkIdentity.spawned.Remove(netId);
}
}
@ -748,7 +747,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
@ -770,13 +769,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)
{
@ -1041,9 +1040,9 @@ 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))
if (NetworkIdentity.spawned.ContainsKey(uv.netId))
{
objects.Remove(uv.netId);
NetworkIdentity.spawned.Remove(uv.netId);
}
if (uv.clientAuthorityOwner != null)
@ -1072,7 +1071,7 @@ static void DestroyObject(NetworkIdentity uv, bool destroyServerObject)
public static void ClearLocalObjects()
{
objects.Clear();
NetworkIdentity.spawned.Clear();
}
public static void Spawn(GameObject obj)