Skip to content

Commit

Permalink
refactor: Remove offline/online scenes (#120)
Browse files Browse the repository at this point in the history
BREAKING CHANGE: offline/online scenes are gone
  • Loading branch information
Lymdun committed Mar 26, 2020
1 parent 82ebd71 commit a4c881a
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 126 deletions.
4 changes: 2 additions & 2 deletions Assets/Mirror/Examples/Room/Scripts/NetworkRoomManagerExt.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public override void OnRoomStopClient()
{
// Demonstrates how to get the Network Manager out of DontDestroyOnLoad when
// going to the offline scene to avoid collision with the one that lives there.
if (gameObject.scene.name == "DontDestroyOnLoad" && !string.IsNullOrEmpty(offlineScene) && SceneManager.GetActiveScene().name != offlineScene)
if (gameObject.scene.name == "DontDestroyOnLoad")
SceneManager.MoveGameObjectToScene(gameObject, SceneManager.GetActiveScene());

base.OnRoomStopClient();
Expand All @@ -35,7 +35,7 @@ public override void OnRoomStopServer()
{
// Demonstrates how to get the Network Manager out of DontDestroyOnLoad when
// going to the offline scene to avoid collision with the one that lives there.
if (gameObject.scene.name == "DontDestroyOnLoad" && !string.IsNullOrEmpty(offlineScene) && SceneManager.GetActiveScene().name != offlineScene)
if (gameObject.scene.name == "DontDestroyOnLoad")
SceneManager.MoveGameObjectToScene(gameObject, SceneManager.GetActiveScene());

base.OnRoomStopServer();
Expand Down
130 changes: 14 additions & 116 deletions Assets/Mirror/Runtime/NetworkManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,25 +54,6 @@ public class NetworkManager : MonoBehaviour
[Tooltip("Server Update frequency, per second. Use around 60Hz for fast paced games like Counter-Strike to minimize latency. Use around 30Hz for games like WoW to minimize computations. Use around 1-10Hz for slow paced games like EVE.")]
public int serverTickRate = 30;

/// <summary>
/// The scene to switch to when offline.
/// <para>Setting this makes the NetworkManager do scene management. This scene will be switched to when a network session is completed - such as a client disconnect, or a server shutdown.</para>
/// </summary>
[Header("Scene Management")]
[Scene]
[FormerlySerializedAs("m_OfflineScene")]
[Tooltip("Scene that Mirror will switch to when the client or server is stopped")]
public string offlineScene = "";

/// <summary>
/// The scene to switch to when online.
/// <para>Setting this makes the NetworkManager do scene management. This scene will be switched to when a network session is started - such as a client connect, or a server listen.</para>
/// </summary>
[Scene]
[FormerlySerializedAs("m_OnlineScene")]
[Tooltip("Scene that Mirror will switch to when the server is started. Clients will recieve a Scene Message to load the server's current scene when they connect.")]
public string onlineScene = "";

public NetworkServer server;
public NetworkClient client;

Expand Down Expand Up @@ -187,7 +168,7 @@ public virtual void Start()

// Set the networkSceneName to prevent a scene reload
// if client connection to server fails.
networkSceneName = offlineScene;
networkSceneName = null;

client.Authenticated.AddListener(OnAuthenticated);

Expand Down Expand Up @@ -221,14 +202,6 @@ public virtual void LateUpdate()

#region Start & Stop

// keep the online scene change check in a separate function
bool IsServerOnlineSceneChangeNeeded()
{
// Only change scene if the requested online scene is not blank, and is not already loaded
string loadedSceneName = SceneManager.GetActiveScene().name;
return !string.IsNullOrEmpty(onlineScene) && onlineScene != loadedSceneName && onlineScene != offlineScene;
}

// full server setup code, without spawning objects yet
void SetupServer()
{
Expand Down Expand Up @@ -279,16 +252,7 @@ public void StartServer()

SetupServer();

// scene change needed? then change scene and spawn afterwards.
if (IsServerOnlineSceneChangeNeeded())
{
ServerChangeScene(onlineScene);
}
// otherwise spawn directly
else
{
server.SpawnObjects();
}
server.SpawnObjects();
}

/// <summary>
Expand Down Expand Up @@ -364,26 +328,9 @@ public void StartHost()
// is called after the server is actually properly started.
OnStartHost();

// scene change needed? then change scene and spawn afterwards.
// => BEFORE host client connects. if client auth succeeds then the
// server tells it to load 'onlineScene'. we can't do that if
// server is still in 'offlineScene'. so load on server first.
if (IsServerOnlineSceneChangeNeeded())
{
// call FinishStartHost after changing scene.
finishStartHostPending = true;
ServerChangeScene(onlineScene);
}
// otherwise call FinishStartHost directly
else
{
FinishStartHost();
}
FinishStartHost();
}

// This may be set true in StartHost and is evaluated in FinishStartHost
bool finishStartHostPending;

// FinishStartHost is guaranteed to be called after the host server was
// fully started and all the asynchronous StartHost magic is finished
// (= scene loading), or immediately if there was no asynchronous magic.
Expand Down Expand Up @@ -468,11 +415,6 @@ public void StopServer()
// doesn't think we need initialize anything.
mode = NetworkManagerMode.Offline;

if (!string.IsNullOrEmpty(offlineScene))
{
ServerChangeScene(offlineScene);
}

startPositionIndex = 0;
}

Expand All @@ -483,6 +425,7 @@ public void StopClient()
{
if (LogFilter.Debug) Debug.Log("NetworkManager StopClient");
isNetworkActive = false;
clientLoadedScene = false;

// shutdown client
client.Disconnect();
Expand All @@ -492,13 +435,6 @@ public void StopClient()
// doesn't think we need initialize anything.
mode = NetworkManagerMode.Offline;

// If this is the host player, StopServer will already be changing scenes.
// Check loadingSceneAsync to ensure we don't double-invoke the scene change.
if (!string.IsNullOrEmpty(offlineScene) && SceneManager.GetActiveScene().name != offlineScene && loadingSceneAsync == null)
{
ClientChangeScene(offlineScene, SceneOperation.Normal);
}

}

/// <summary>
Expand Down Expand Up @@ -756,8 +692,6 @@ void FinishLoadScene()
{
FinishLoadSceneClientOnly();
}
// otherwise we called it after stopping when loading offline scene.
// do nothing then.
}

// finish load scene part for host mode. makes code easier and is
Expand All @@ -776,42 +710,15 @@ void FinishLoadSceneHost()
client.connection = null;
}

// do we need to finish a StartHost() call?
// then call FinishStartHost and let it take care of spawning etc.
if (finishStartHostPending)
{
finishStartHostPending = false;
FinishStartHost();
FinishStartHost();

// call OnServerSceneChanged
OnServerSceneChanged(networkSceneName);
// call OnServerSceneChanged
OnServerSceneChanged(networkSceneName);

if (client.isConnected)
{
// DO NOT call OnClientSceneChanged here.
// the scene change happened because StartHost loaded the
// server's online scene. it has nothing to do with the client.
// this was not meant as a client scene load, so don't call it.
//
// otherwise AddPlayer would be called twice:
// -> once for client OnConnected
// -> once in OnClientSceneChanged
}
}
// otherwise we just changed a scene in host mode
else
if (client.isConnected)
{
// spawn server objects
server.SpawnObjects();

// call OnServerSceneChanged
OnServerSceneChanged(networkSceneName);

if (client.isConnected)
{
// let client know that we changed scene
OnClientSceneChanged(client.connection);
}
// let client know that we changed scene
OnClientSceneChanged(client.connection);
}
}

Expand Down Expand Up @@ -908,7 +815,7 @@ void OnServerAuthenticated(NetworkConnectionToClient conn)
if (LogFilter.Debug) Debug.Log("NetworkManager.OnServerAuthenticated");

// proceed with the login handshake by calling OnServerConnect
if (networkSceneName != "" && networkSceneName != offlineScene)
if (networkSceneName != "")
{
var msg = new SceneMessage() { sceneName = networkSceneName };
conn.Send(msg);
Expand Down Expand Up @@ -983,18 +890,9 @@ void OnClientAuthenticated(NetworkConnectionToServer conn)

if (LogFilter.Debug) Debug.Log("NetworkManager.OnClientAuthenticated");

// proceed with the login handshake by calling OnClientConnect
string loadedSceneName = SceneManager.GetActiveScene().name;
if (string.IsNullOrEmpty(onlineScene) || onlineScene == offlineScene || loadedSceneName == onlineScene)
{
clientLoadedScene = false;
}
else
{
// will wait for scene id to come from the server.
clientLoadedScene = true;
client.connection = conn;
}
// will wait for scene id to come from the server.
clientLoadedScene = true;
client.connection = conn;
}

void OnClientNotReadyMessageInternal(NetworkConnection conn, NotReadyMessage msg)
Expand Down
11 changes: 4 additions & 7 deletions Assets/Mirror/Runtime/NetworkServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -614,7 +614,7 @@ public bool AddPlayerForConnection(NetworkConnection conn, GameObject player)

// Set the connection on the NetworkIdentity on the server, NetworkIdentity.SetLocalPlayer is not called on the server (it is on clients)
identity.SetClientOwner(conn);

// special case, we are in host mode, set hasAuthority to true so that all overrides see it
if (conn is ULocalConnectionToClient)
{
Expand Down Expand Up @@ -1080,7 +1080,7 @@ public bool SpawnObjects()
// only if server active
if (!active)
return false;

NetworkIdentity[] identities = Resources.FindObjectsOfTypeAll<NetworkIdentity>();
foreach (NetworkIdentity identity in identities)
{
Expand All @@ -1089,14 +1089,11 @@ public bool SpawnObjects()
if (LogFilter.Debug) Debug.Log("SpawnObjects sceneId:" + identity.sceneId.ToString("X") + " name:" + identity.gameObject.name);
identity.Reset();
identity.gameObject.SetActive(true);
}
}

foreach (NetworkIdentity identity in identities)
{
if (ValidateSceneObject(identity))
Spawn(identity.gameObject);
}
}

return true;
}
}
Expand Down
1 change: 0 additions & 1 deletion Assets/Mirror/Tests/Runtime/NetworkManagerTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ public void VariableTest()
Assert.That(manager.startOnHeadless, Is.False);
Assert.That(manager.showDebugMessages, Is.False);
Assert.That(manager.serverTickRate, Is.EqualTo(30));
Assert.That(manager.offlineScene, Is.Empty);
Assert.That(manager.server.MaxConnections, Is.EqualTo(4));
Assert.That(manager.autoCreatePlayer, Is.False);
Assert.That(manager.numPlayers, Is.Zero);
Expand Down

0 comments on commit a4c881a

Please sign in to comment.