From 7daab9dd40fd324e8e0c3f9ae012cec0cc0d5841 Mon Sep 17 00:00:00 2001 From: vis2k Date: Sun, 7 Mar 2021 15:04:13 +0800 Subject: [PATCH] NetworkServer: sort similar functions together --- Assets/Mirror/Runtime/NetworkServer.cs | 187 +++++++++++++------------ 1 file changed, 97 insertions(+), 90 deletions(-) diff --git a/Assets/Mirror/Runtime/NetworkServer.cs b/Assets/Mirror/Runtime/NetworkServer.cs index 749abf240..91a224a58 100644 --- a/Assets/Mirror/Runtime/NetworkServer.cs +++ b/Assets/Mirror/Runtime/NetworkServer.cs @@ -361,6 +361,7 @@ public static bool NoExternalConnections() [Obsolete("NoConnections was renamed to NoExternalConnections because that's what it checks for.")] public static bool NoConnections() => NoExternalConnections(); + // update ////////////////////////////////////////////////////////////// // NetworkEarlyUpdate called before any Update/FixedUpdate // (we add this to the UnityEngine in NetworkLoop) internal static void NetworkEarlyUpdate() @@ -543,6 +544,7 @@ internal static void NetworkLateUpdate() [Obsolete("NetworkServer.Update is now called internally from our custom update loop. No need to call Update manually anymore.")] public static void Update() => NetworkLateUpdate(); + // transport events //////////////////////////////////////////////////// static void AddTransportHandlers() { Transport.activeTransport.OnServerConnected = OnConnected; @@ -637,6 +639,7 @@ static void OnError(int connectionId, Exception exception) Debug.LogException(exception); } + // message handlers //////////////////////////////////////////////////// /// Register a handler for message type T. Most should require authentication. public static void RegisterHandler(Action handler, bool requireAuthentication = true) where T : struct, NetworkMessage @@ -930,6 +933,7 @@ internal static bool GetNetworkIdentity(GameObject go, out NetworkIdentity ident return true; } + // ready /////////////////////////////////////////////////////////////// /// Flags client connection as ready (=joined world). // When a client has signaled that it is ready, this method tells the // server that the client is ready to receive spawned objects and state @@ -949,33 +953,6 @@ public static void SetClientReady(NetworkConnection conn) SpawnObserversForConnection(conn); } - internal static void ShowForConnection(NetworkIdentity identity, NetworkConnection conn) - { - if (conn.isReady) - SendSpawnMessage(identity, conn); - } - - internal static void HideForConnection(NetworkIdentity identity, NetworkConnection conn) - { - ObjectHideMessage msg = new ObjectHideMessage - { - netId = identity.netId - }; - conn.Send(msg); - } - - /// Marks all connected clients as no longer ready. - // All clients will no longer be sent state synchronization updates. The - // player's clients can call ClientManager.Ready() again to re-enter the - // ready state. This is useful when switching scenes. - public static void SetAllClientsNotReady() - { - foreach (NetworkConnectionToClient conn in connections.Values) - { - SetClientNotReady(conn); - } - } - /// Marks the client of the connection to be not-ready. // Clients that are not ready do not receive spawned objects or state // synchronization updates. They client can be made ready again by @@ -992,6 +969,18 @@ public static void SetClientNotReady(NetworkConnection conn) } } + /// Marks all connected clients as no longer ready. + // All clients will no longer be sent state synchronization updates. The + // player's clients can call ClientManager.Ready() again to re-enter the + // ready state. This is useful when switching scenes. + public static void SetAllClientsNotReady() + { + foreach (NetworkConnectionToClient conn in connections.Values) + { + SetClientNotReady(conn); + } + } + // default ready handler. static void OnClientReadyMessage(NetworkConnection conn, ReadyMessage msg) { @@ -999,6 +988,22 @@ static void OnClientReadyMessage(NetworkConnection conn, ReadyMessage msg) SetClientReady(conn); } + // show / hide for connection ////////////////////////////////////////// + internal static void ShowForConnection(NetworkIdentity identity, NetworkConnection conn) + { + if (conn.isReady) + SendSpawnMessage(identity, conn); + } + + internal static void HideForConnection(NetworkIdentity identity, NetworkConnection conn) + { + ObjectHideMessage msg = new ObjectHideMessage + { + netId = identity.netId + }; + conn.Send(msg); + } + /// Removes the player object from the connection // destroyServerObject: Indicates whether the server object should be destroyed public static void RemovePlayerForConnection(NetworkConnection conn, bool destroyServerObject) @@ -1043,6 +1048,7 @@ static void OnCommandMessage(NetworkConnection conn, CommandMessage msg) identity.HandleRemoteCall(msg.componentIndex, msg.functionHash, MirrorInvokeType.Command, networkReader, conn as NetworkConnectionToClient); } + // spawning //////////////////////////////////////////////////////////// internal static void SpawnObject(GameObject obj, NetworkConnection ownerConnection) { if (!active) @@ -1137,17 +1143,6 @@ internal static void SendSpawnMessage(NetworkIdentity identity, NetworkConnectio } } - /// Destroys all of the connection's owned objects on the server. - // This is used when a client disconnects, to remove the players for - // that client. This also destroys non-player objects that have client - // authority set for this connection. - public static void DestroyPlayerForConnection(NetworkConnection conn) - { - // destroy all objects owned by this connection, including the player object - conn.DestroyOwnedObjects(); - conn.identity = null; - } - /// Spawn the given game object on all clients which are ready. // This will cause a new object to be instantiated from the registered // prefab, or from a custom spawn function. @@ -1203,6 +1198,70 @@ static bool VerifyCanSpawn(GameObject obj) return true; } + /// This takes an object that has been spawned and un-spawns it. + // The object will be removed from clients that it was spawned on, or + // the custom spawn handler function on the client will be called for + // the object. + // Unlike when calling NetworkServer.Destroy(), on the server the object + // will NOT be destroyed. This allows the server to re-use the object, + // even spawn it again later. + public static void UnSpawn(GameObject obj) => DestroyObject(obj, false); + + internal static bool ValidateSceneObject(NetworkIdentity identity) + { + if (identity.gameObject.hideFlags == HideFlags.NotEditable || + identity.gameObject.hideFlags == HideFlags.HideAndDontSave) + return false; + +#if UNITY_EDITOR + if (UnityEditor.EditorUtility.IsPersistent(identity.gameObject)) + return false; +#endif + + // If not a scene object + return identity.sceneId != 0; + } + + /// Spawns NetworkIdentities in the scene on the server. + // NetworkIdentity objects in a scene are disabled by default. Calling + // SpawnObjects() causes these scene objects to be enabled and spawned. + // It is like calling NetworkServer.Spawn() for each of them. + public static bool SpawnObjects() + { + // only if server active + if (!active) + return false; + + NetworkIdentity[] identities = Resources.FindObjectsOfTypeAll(); + foreach (NetworkIdentity identity in identities) + { + if (ValidateSceneObject(identity)) + { + // Debug.Log("SpawnObjects sceneId:" + identity.sceneId.ToString("X") + " name:" + identity.gameObject.name); + identity.gameObject.SetActive(true); + } + } + + foreach (NetworkIdentity identity in identities) + { + if (ValidateSceneObject(identity)) + Spawn(identity.gameObject); + } + return true; + } + + // destroy ///////////////////////////////////////////////////////////// + /// Destroys all of the connection's owned objects on the server. + // This is used when a client disconnects, to remove the players for + // that client. This also destroys non-player objects that have client + // authority set for this connection. + public static void DestroyPlayerForConnection(NetworkConnection conn) + { + // destroy all objects owned by this connection, including the player object + conn.DestroyOwnedObjects(); + conn.identity = null; + } + static void DestroyObject(NetworkIdentity identity, bool destroyServerObject) { // Debug.Log("DestroyObject instance:" + identity.netId); @@ -1258,58 +1317,6 @@ static void DestroyObject(GameObject obj, bool destroyServerObject) // NetworkServer.Destroy(). public static void Destroy(GameObject obj) => DestroyObject(obj, true); - /// This takes an object that has been spawned and un-spawns it. - // The object will be removed from clients that it was spawned on, or - // the custom spawn handler function on the client will be called for - // the object. - // Unlike when calling NetworkServer.Destroy(), on the server the object - // will NOT be destroyed. This allows the server to re-use the object, - // even spawn it again later. - public static void UnSpawn(GameObject obj) => DestroyObject(obj, false); - - internal static bool ValidateSceneObject(NetworkIdentity identity) - { - if (identity.gameObject.hideFlags == HideFlags.NotEditable || - identity.gameObject.hideFlags == HideFlags.HideAndDontSave) - return false; - -#if UNITY_EDITOR - if (UnityEditor.EditorUtility.IsPersistent(identity.gameObject)) - return false; -#endif - - // If not a scene object - return identity.sceneId != 0; - } - - /// Spawns NetworkIdentities in the scene on the server. - // NetworkIdentity objects in a scene are disabled by default. Calling - // SpawnObjects() causes these scene objects to be enabled and spawned. - // It is like calling NetworkServer.Spawn() for each of them. - public static bool SpawnObjects() - { - // only if server active - if (!active) - return false; - - NetworkIdentity[] identities = Resources.FindObjectsOfTypeAll(); - foreach (NetworkIdentity identity in identities) - { - if (ValidateSceneObject(identity)) - { - // Debug.Log("SpawnObjects sceneId:" + identity.sceneId.ToString("X") + " name:" + identity.gameObject.name); - identity.gameObject.SetActive(true); - } - } - - foreach (NetworkIdentity identity in identities) - { - if (ValidateSceneObject(identity)) - Spawn(identity.gameObject); - } - return true; - } - // interest management ///////////////////////////////////////////////// // Helper function to add all server connections as observers. // This is used if none of the components provides their own