StartServer split into SetupServer and spawning. Prepares for local connection removal.

This commit is contained in:
vis2k 2019-12-31 10:39:11 +01:00
commit d9b77ec4d0

View File

@ -273,15 +273,10 @@ bool IsServerOnlineSceneChangeNeeded()
return !string.IsNullOrEmpty(onlineScene) && onlineScene != loadedSceneName && onlineScene != offlineScene; return !string.IsNullOrEmpty(onlineScene) && onlineScene != loadedSceneName && onlineScene != offlineScene;
} }
/// <summary> // full server setup code, without spawning objects yet
/// This starts a new server. void SetupServer()
/// <para>This uses the networkPort property as the listen port.</para>
/// </summary>
/// <returns></returns>
public void StartServer()
{ {
if (LogFilter.Debug) Debug.Log("NetworkManager StartServer"); if (LogFilter.Debug) Debug.Log("NetworkManager SetupServer");
InitializeSingleton(); InitializeSingleton();
if (runInBackground) if (runInBackground)
@ -312,6 +307,16 @@ public void StartServer()
RegisterServerMessages(); RegisterServerMessages();
isNetworkActive = true; isNetworkActive = true;
}
/// <summary>
/// This starts a new server.
/// <para>This uses the networkPort property as the listen port.</para>
/// </summary>
/// <returns></returns>
public void StartServer()
{
SetupServer();
// scene change needed? then change scene and spawn afterwards. // scene change needed? then change scene and spawn afterwards.
if (IsServerOnlineSceneChangeNeeded()) if (IsServerOnlineSceneChangeNeeded())
@ -394,13 +399,15 @@ public void StartClient(Uri uri)
/// </summary> /// </summary>
public virtual void StartHost() public virtual void StartHost()
{ {
// setup server first
OnStartHost(); OnStartHost();
SetupServer();
// SetupLocalConnection needs to be called BEFORE StartServer: // SetupLocalConnection needs to be called BEFORE SpawnObjects:
// https://github.com/vis2k/Mirror/pull/1249/ // https://github.com/vis2k/Mirror/pull/1249/
// -> this sets NetworkServer.localConnection. // -> this sets NetworkServer.localConnection.
// -> localConnection needs to be set before StartServer because: // -> localConnection needs to be set before SpawnObjects because:
// -> StartServer calls OnStartServer // -> SpawnObjects calls OnStartServer in all NetworkBehaviours
// -> OnStartServer might spawn an object and set [SyncVar(hook="OnColorChanged")] object.color = green; // -> OnStartServer might spawn an object and set [SyncVar(hook="OnColorChanged")] object.color = green;
// -> this calls SyncVar.set (generated by Weaver), which has // -> this calls SyncVar.set (generated by Weaver), which has
// a custom case for host mode (because host mode doesn't // a custom case for host mode (because host mode doesn't
@ -421,7 +428,20 @@ public virtual void StartHost()
// -> localClientActive needs to be true, otherwise the hook // -> localClientActive needs to be true, otherwise the hook
// isn't called in host mode! // isn't called in host mode!
NetworkClient.SetupLocalConnection(); NetworkClient.SetupLocalConnection();
StartServer();
// scene change needed? then change scene and spawn afterwards.
if (IsServerOnlineSceneChangeNeeded())
{
ServerChangeScene(onlineScene);
}
// otherwise spawn directly
else
{
NetworkServer.SpawnObjects();
}
// connect client and call OnStartClient AFTER any possible server
// scene changes.
ConnectLocalClient(); ConnectLocalClient();
OnStartClient(); OnStartClient();
} }