From 6406f9a3dc842a327c053f548916ffceb6982321 Mon Sep 17 00:00:00 2001 From: Dominick Schroer Date: Tue, 16 Jul 2024 10:55:31 -0600 Subject: [PATCH 1/3] feat: initialized callback --- .../Runtime/Core/NetworkManager.cs | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/com.unity.netcode.gameobjects/Runtime/Core/NetworkManager.cs b/com.unity.netcode.gameobjects/Runtime/Core/NetworkManager.cs index 95f9605729..d6290afd31 100644 --- a/com.unity.netcode.gameobjects/Runtime/Core/NetworkManager.cs +++ b/com.unity.netcode.gameobjects/Runtime/Core/NetworkManager.cs @@ -139,6 +139,11 @@ public bool DAHost /// public event OnSessionOwnerPromotedDelegateHandler OnSessionOwnerPromoted; + /// + /// When a server or client is initializing, before the network connecting is established + /// + public event Action OnInitialized; + internal void SetSessionOwner(ulong sessionOwner) { var previousSessionOwner = CurrentSessionOwner; @@ -1166,6 +1171,18 @@ internal void Initialize(bool server) NetworkConfig.InitializePrefabs(); PrefabHandler.RegisterPlayerPrefab(); + + // Invoke initialized callback + try + { + OnInitialized.Invoke(); + } + catch (Exception e) + { + NetworkLog.LogError(e.ToString()); + Shutdown(); + return; + } } private enum StartType From b3f7b1dce27b6611a08eb217fb05ae8765d53746 Mon Sep 17 00:00:00 2001 From: Dominick Schroer Date: Tue, 16 Jul 2024 12:05:32 -0600 Subject: [PATCH 2/3] add missing tests --- .../Runtime/Core/NetworkManager.cs | 7 +-- .../Runtime/NetworkManagerInitializedTests.cs | 62 +++++++++++++++++++ .../NetworkManagerInitializedTests.cs.meta | 3 + 3 files changed, 68 insertions(+), 4 deletions(-) create mode 100644 com.unity.netcode.gameobjects/Tests/Runtime/NetworkManagerInitializedTests.cs create mode 100644 com.unity.netcode.gameobjects/Tests/Runtime/NetworkManagerInitializedTests.cs.meta diff --git a/com.unity.netcode.gameobjects/Runtime/Core/NetworkManager.cs b/com.unity.netcode.gameobjects/Runtime/Core/NetworkManager.cs index d6290afd31..3b9f772682 100644 --- a/com.unity.netcode.gameobjects/Runtime/Core/NetworkManager.cs +++ b/com.unity.netcode.gameobjects/Runtime/Core/NetworkManager.cs @@ -1175,13 +1175,12 @@ internal void Initialize(bool server) // Invoke initialized callback try { - OnInitialized.Invoke(); + OnInitialized?.Invoke(); } - catch (Exception e) + catch (Exception) { - NetworkLog.LogError(e.ToString()); Shutdown(); - return; + throw; } } diff --git a/com.unity.netcode.gameobjects/Tests/Runtime/NetworkManagerInitializedTests.cs b/com.unity.netcode.gameobjects/Tests/Runtime/NetworkManagerInitializedTests.cs new file mode 100644 index 0000000000..db50332ff2 --- /dev/null +++ b/com.unity.netcode.gameobjects/Tests/Runtime/NetworkManagerInitializedTests.cs @@ -0,0 +1,62 @@ +using System; +using NUnit.Framework; +using Unity.Netcode.Transports.UTP; +using UnityEngine; +using UnityEngine.TestTools; +using Object = UnityEngine.Object; + +namespace Unity.Netcode.RuntimeTests +{ + internal class NetworkManagerInitializedTests + { + [Test] + [TestCase(true)] + [TestCase(false)] + public void OnInitializedIsCalled(bool isServer) + { + var networkManagerObject = CreateNetworkManager(out var networkManager); + + var callbackHit = false; + + networkManager.OnInitialized += () => callbackHit = true; + networkManager.Initialize(isServer); + + Assert.True(callbackHit, "OnInitialized callback was never triggered"); + + // Clean up + Object.Destroy(networkManagerObject); + } + + [Test] + public void OnInitializedShutsDownOnException() + { + var networkManagerObject = CreateNetworkManager(out var networkManager); + + networkManager.OnInitialized += () => throw new Exception(); + + try + { + networkManager.StartServer(); + } + catch (Exception) + { + // do nothing + } + + Assert.True(networkManager.ShutdownInProgress, "Manager did not shutdown"); + + // Clean up + Object.Destroy(networkManagerObject); + } + + private GameObject CreateNetworkManager(out NetworkManager networkManager) + { + var networkManagerObject = new GameObject(nameof(OnInitializedIsCalled)); + + var unityTransport = networkManagerObject.AddComponent(); + networkManager = networkManagerObject.AddComponent(); + networkManager.NetworkConfig = new NetworkConfig() { NetworkTransport = unityTransport }; + return networkManagerObject; + } + } +} diff --git a/com.unity.netcode.gameobjects/Tests/Runtime/NetworkManagerInitializedTests.cs.meta b/com.unity.netcode.gameobjects/Tests/Runtime/NetworkManagerInitializedTests.cs.meta new file mode 100644 index 0000000000..62086a604b --- /dev/null +++ b/com.unity.netcode.gameobjects/Tests/Runtime/NetworkManagerInitializedTests.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: a8a60a1c41f9436a9d98d9773bed6f92 +timeCreated: 1721149159 \ No newline at end of file From 32308c2bb71ddb20079acb2df27fc3ef46c1a24b Mon Sep 17 00:00:00 2001 From: NoelStephensUnity Date: Tue, 16 Jul 2024 19:52:08 -0500 Subject: [PATCH 3/3] style removing unused using directive namespace. --- .../Tests/Runtime/NetworkManagerInitializedTests.cs | 1 - 1 file changed, 1 deletion(-) diff --git a/com.unity.netcode.gameobjects/Tests/Runtime/NetworkManagerInitializedTests.cs b/com.unity.netcode.gameobjects/Tests/Runtime/NetworkManagerInitializedTests.cs index db50332ff2..8fd9b08b43 100644 --- a/com.unity.netcode.gameobjects/Tests/Runtime/NetworkManagerInitializedTests.cs +++ b/com.unity.netcode.gameobjects/Tests/Runtime/NetworkManagerInitializedTests.cs @@ -2,7 +2,6 @@ using NUnit.Framework; using Unity.Netcode.Transports.UTP; using UnityEngine; -using UnityEngine.TestTools; using Object = UnityEngine.Object; namespace Unity.Netcode.RuntimeTests