NetworkServer: sort similar functions together

This commit is contained in:
vis2k 2021-03-07 15:04:13 +08:00
parent cf7de49def
commit 7daab9dd40

View File

@ -361,6 +361,7 @@ public static bool NoExternalConnections()
[Obsolete("NoConnections was renamed to NoExternalConnections because that's what it checks for.")] [Obsolete("NoConnections was renamed to NoExternalConnections because that's what it checks for.")]
public static bool NoConnections() => NoExternalConnections(); public static bool NoConnections() => NoExternalConnections();
// update //////////////////////////////////////////////////////////////
// NetworkEarlyUpdate called before any Update/FixedUpdate // NetworkEarlyUpdate called before any Update/FixedUpdate
// (we add this to the UnityEngine in NetworkLoop) // (we add this to the UnityEngine in NetworkLoop)
internal static void NetworkEarlyUpdate() 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.")] [Obsolete("NetworkServer.Update is now called internally from our custom update loop. No need to call Update manually anymore.")]
public static void Update() => NetworkLateUpdate(); public static void Update() => NetworkLateUpdate();
// transport events ////////////////////////////////////////////////////
static void AddTransportHandlers() static void AddTransportHandlers()
{ {
Transport.activeTransport.OnServerConnected = OnConnected; Transport.activeTransport.OnServerConnected = OnConnected;
@ -637,6 +639,7 @@ static void OnError(int connectionId, Exception exception)
Debug.LogException(exception); Debug.LogException(exception);
} }
// message handlers ////////////////////////////////////////////////////
/// <summary>Register a handler for message type T. Most should require authentication.</summary> /// <summary>Register a handler for message type T. Most should require authentication.</summary>
public static void RegisterHandler<T>(Action<NetworkConnection, T> handler, bool requireAuthentication = true) public static void RegisterHandler<T>(Action<NetworkConnection, T> handler, bool requireAuthentication = true)
where T : struct, NetworkMessage where T : struct, NetworkMessage
@ -930,6 +933,7 @@ internal static bool GetNetworkIdentity(GameObject go, out NetworkIdentity ident
return true; return true;
} }
// ready ///////////////////////////////////////////////////////////////
/// <summary>Flags client connection as ready (=joined world).</summary> /// <summary>Flags client connection as ready (=joined world).</summary>
// When a client has signaled that it is ready, this method tells the // 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 // server that the client is ready to receive spawned objects and state
@ -949,33 +953,6 @@ public static void SetClientReady(NetworkConnection conn)
SpawnObserversForConnection(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);
}
/// <summary>Marks all connected clients as no longer ready.</summary>
// 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);
}
}
/// <summary>Marks the client of the connection to be not-ready.</summary> /// <summary>Marks the client of the connection to be not-ready.</summary>
// Clients that are not ready do not receive spawned objects or state // Clients that are not ready do not receive spawned objects or state
// synchronization updates. They client can be made ready again by // synchronization updates. They client can be made ready again by
@ -992,6 +969,18 @@ public static void SetClientNotReady(NetworkConnection conn)
} }
} }
/// <summary>Marks all connected clients as no longer ready.</summary>
// 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. // default ready handler.
static void OnClientReadyMessage(NetworkConnection conn, ReadyMessage msg) static void OnClientReadyMessage(NetworkConnection conn, ReadyMessage msg)
{ {
@ -999,6 +988,22 @@ static void OnClientReadyMessage(NetworkConnection conn, ReadyMessage msg)
SetClientReady(conn); 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);
}
/// <summary>Removes the player object from the connection</summary> /// <summary>Removes the player object from the connection</summary>
// destroyServerObject: Indicates whether the server object should be destroyed // destroyServerObject: Indicates whether the server object should be destroyed
public static void RemovePlayerForConnection(NetworkConnection conn, bool destroyServerObject) 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); identity.HandleRemoteCall(msg.componentIndex, msg.functionHash, MirrorInvokeType.Command, networkReader, conn as NetworkConnectionToClient);
} }
// spawning ////////////////////////////////////////////////////////////
internal static void SpawnObject(GameObject obj, NetworkConnection ownerConnection) internal static void SpawnObject(GameObject obj, NetworkConnection ownerConnection)
{ {
if (!active) if (!active)
@ -1137,17 +1143,6 @@ internal static void SendSpawnMessage(NetworkIdentity identity, NetworkConnectio
} }
} }
/// <summary>Destroys all of the connection's owned objects on the server.</summary>
// 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;
}
/// <summary>Spawn the given game object on all clients which are ready.</summary> /// <summary>Spawn the given game object on all clients which are ready.</summary>
// This will cause a new object to be instantiated from the registered // This will cause a new object to be instantiated from the registered
// prefab, or from a custom spawn function. // prefab, or from a custom spawn function.
@ -1203,6 +1198,70 @@ static bool VerifyCanSpawn(GameObject obj)
return true; return true;
} }
/// <summary>This takes an object that has been spawned and un-spawns it.</summary>
// 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;
}
/// <summary>Spawns NetworkIdentities in the scene on the server.</summary>
// 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<NetworkIdentity>();
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 /////////////////////////////////////////////////////////////
/// <summary>Destroys all of the connection's owned objects on the server.</summary>
// 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) static void DestroyObject(NetworkIdentity identity, bool destroyServerObject)
{ {
// Debug.Log("DestroyObject instance:" + identity.netId); // Debug.Log("DestroyObject instance:" + identity.netId);
@ -1258,58 +1317,6 @@ static void DestroyObject(GameObject obj, bool destroyServerObject)
// NetworkServer.Destroy(). // NetworkServer.Destroy().
public static void Destroy(GameObject obj) => DestroyObject(obj, true); public static void Destroy(GameObject obj) => DestroyObject(obj, true);
/// <summary>This takes an object that has been spawned and un-spawns it.</summary>
// 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;
}
/// <summary>Spawns NetworkIdentities in the scene on the server.</summary>
// 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<NetworkIdentity>();
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 ///////////////////////////////////////////////// // interest management /////////////////////////////////////////////////
// Helper function to add all server connections as observers. // Helper function to add all server connections as observers.
// This is used if none of the components provides their own // This is used if none of the components provides their own