From 539fe69585ece468db6ac64babf754dcfc21f1ab Mon Sep 17 00:00:00 2001 From: vis2k Date: Tue, 31 Dec 2019 10:11:20 +0100 Subject: [PATCH 1/4] StartServer code mostly moved into SetupServer --- Assets/Mirror/Runtime/NetworkManager.cs | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/Assets/Mirror/Runtime/NetworkManager.cs b/Assets/Mirror/Runtime/NetworkManager.cs index 324a6f850..7ecb9df6e 100644 --- a/Assets/Mirror/Runtime/NetworkManager.cs +++ b/Assets/Mirror/Runtime/NetworkManager.cs @@ -273,15 +273,10 @@ bool IsServerOnlineSceneChangeNeeded() return !string.IsNullOrEmpty(onlineScene) && onlineScene != loadedSceneName && onlineScene != offlineScene; } - /// - /// This starts a new server. - /// This uses the networkPort property as the listen port. - /// - /// - public void StartServer() + // full server setup code, without spawning objects yet + void SetupServer() { - if (LogFilter.Debug) Debug.Log("NetworkManager StartServer"); - + if (LogFilter.Debug) Debug.Log("NetworkManager SetupServer"); InitializeSingleton(); if (runInBackground) @@ -312,6 +307,16 @@ public void StartServer() RegisterServerMessages(); isNetworkActive = true; + } + + /// + /// This starts a new server. + /// This uses the networkPort property as the listen port. + /// + /// + public void StartServer() + { + SetupServer(); // scene change needed? then change scene and spawn afterwards. if (IsServerOnlineSceneChangeNeeded()) From 07a85309e08bb44c4cb9a0bbd38a048f66f11e99 Mon Sep 17 00:00:00 2001 From: vis2k Date: Tue, 31 Dec 2019 10:11:34 +0100 Subject: [PATCH 2/4] StartHost calls SetupServer + scene change + spawnobjects manually --- Assets/Mirror/Runtime/NetworkManager.cs | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/Assets/Mirror/Runtime/NetworkManager.cs b/Assets/Mirror/Runtime/NetworkManager.cs index 7ecb9df6e..4461a5351 100644 --- a/Assets/Mirror/Runtime/NetworkManager.cs +++ b/Assets/Mirror/Runtime/NetworkManager.cs @@ -401,11 +401,11 @@ public virtual void StartHost() { OnStartHost(); - // SetupLocalConnection needs to be called BEFORE StartServer: + // SetupLocalConnection needs to be called BEFORE SpawnObjects: // https://github.com/vis2k/Mirror/pull/1249/ // -> this sets NetworkServer.localConnection. - // -> localConnection needs to be set before StartServer because: - // -> StartServer calls OnStartServer + // -> localConnection needs to be set before SpawnObjects because: + // -> SpawnObjects calls OnStartServer in all NetworkBehaviours // -> OnStartServer might spawn an object and set [SyncVar(hook="OnColorChanged")] object.color = green; // -> this calls SyncVar.set (generated by Weaver), which has // a custom case for host mode (because host mode doesn't @@ -426,7 +426,20 @@ public virtual void StartHost() // -> localClientActive needs to be true, otherwise the hook // isn't called in host mode! NetworkClient.SetupLocalConnection(); - StartServer(); + + SetupServer(); + + // scene change needed? then change scene and spawn afterwards. + if (IsServerOnlineSceneChangeNeeded()) + { + ServerChangeScene(onlineScene); + } + // otherwise spawn directly + else + { + NetworkServer.SpawnObjects(); + } + ConnectLocalClient(); OnStartClient(); } From 23b5b50155b0c6c88eb3796ce4917520cb3111d9 Mon Sep 17 00:00:00 2001 From: vis2k Date: Tue, 31 Dec 2019 10:24:10 +0100 Subject: [PATCH 3/4] Call SetupLocalConnection after SetupServer to prepare for local connection removal, where a real connection can only connect to the server after it was started, not before. --- Assets/Mirror/Runtime/NetworkManager.cs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/Assets/Mirror/Runtime/NetworkManager.cs b/Assets/Mirror/Runtime/NetworkManager.cs index 4461a5351..034349bbb 100644 --- a/Assets/Mirror/Runtime/NetworkManager.cs +++ b/Assets/Mirror/Runtime/NetworkManager.cs @@ -400,6 +400,7 @@ public void StartClient(Uri uri) public virtual void StartHost() { OnStartHost(); + SetupServer(); // SetupLocalConnection needs to be called BEFORE SpawnObjects: // https://github.com/vis2k/Mirror/pull/1249/ @@ -427,8 +428,6 @@ public virtual void StartHost() // isn't called in host mode! NetworkClient.SetupLocalConnection(); - SetupServer(); - // scene change needed? then change scene and spawn afterwards. if (IsServerOnlineSceneChangeNeeded()) { From ed260cfb181a87bc5488f09c55f60ab6c149eaf1 Mon Sep 17 00:00:00 2001 From: vis2k Date: Tue, 31 Dec 2019 10:24:21 +0100 Subject: [PATCH 4/4] add comments --- Assets/Mirror/Runtime/NetworkManager.cs | 3 +++ 1 file changed, 3 insertions(+) diff --git a/Assets/Mirror/Runtime/NetworkManager.cs b/Assets/Mirror/Runtime/NetworkManager.cs index 034349bbb..845e53c7f 100644 --- a/Assets/Mirror/Runtime/NetworkManager.cs +++ b/Assets/Mirror/Runtime/NetworkManager.cs @@ -399,6 +399,7 @@ public void StartClient(Uri uri) /// public virtual void StartHost() { + // setup server first OnStartHost(); SetupServer(); @@ -439,6 +440,8 @@ public virtual void StartHost() NetworkServer.SpawnObjects(); } + // connect client and call OnStartClient AFTER any possible server + // scene changes. ConnectLocalClient(); OnStartClient(); }