Skip to content

Commit dc437c7

Browse files
fix: load event completed fires early (#1379) (#1464)
* fix Only allow a host to include itself in the clients completed list of a SceneEventProgress (i.e. don't include dedicate-server only). * style adding adjusted comments. * test Updated SceneLoadingAndNotifications to run as a host and a server in order to test that when running as a server the SceneEventType.LoadEventCompleted and SceneEventType.UnloadEventCompleted events are triggered without the server being added to the ClientsThatCompleted list within the SceneEvent. * update changelog Updating the changelog file with this fix information. Co-authored-by: Unity Netcode CI <74025435+netcode-ci-service@users.noreply.github.com>
1 parent cabb4df commit dc437c7

File tree

2 files changed

+50
-5
lines changed

2 files changed

+50
-5
lines changed

com.unity.netcode.gameobjects/Runtime/SceneManagement/NetworkSceneManager.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1028,8 +1028,8 @@ private void OnSceneUnloaded(uint sceneEventId)
10281028
// despawned that no longer exists
10291029
SendSceneEventData(sceneEventId, m_NetworkManager.ConnectedClientsIds.Where(c => c != m_NetworkManager.ServerClientId).ToArray());
10301030

1031-
//Second, server sets itself as having finished unloading
1032-
if (SceneEventProgressTracking.ContainsKey(sceneEventData.SceneEventProgressId))
1031+
//Only if we are a host do we want register having loaded for the associated SceneEventProgress
1032+
if (SceneEventProgressTracking.ContainsKey(sceneEventData.SceneEventProgressId) && m_NetworkManager.IsHost)
10331033
{
10341034
SceneEventProgressTracking[sceneEventData.SceneEventProgressId].AddClientAsDone(m_NetworkManager.ServerClientId);
10351035
}
@@ -1344,8 +1344,8 @@ private void OnServerLoadedScene(uint sceneEventId, Scene scene)
13441344

13451345
OnLoadComplete?.Invoke(m_NetworkManager.ServerClientId, SceneNameFromHash(sceneEventData.SceneHash), sceneEventData.LoadSceneMode);
13461346

1347-
//Second, set the server as having loaded for the associated SceneEventProgress
1348-
if (SceneEventProgressTracking.ContainsKey(sceneEventData.SceneEventProgressId))
1347+
//Second, only if we are a host do we want register having loaded for the associated SceneEventProgress
1348+
if (SceneEventProgressTracking.ContainsKey(sceneEventData.SceneEventProgressId) && m_NetworkManager.IsHost)
13491349
{
13501350
SceneEventProgressTracking[sceneEventData.SceneEventProgressId].AddClientAsDone(m_NetworkManager.ServerClientId);
13511351
}

testproject/Assets/Tests/Runtime/NetworkSceneManagerTests.cs

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,13 +52,49 @@ private class SceneTestInfo
5252
private NetworkSceneManager.VerifySceneBeforeLoadingDelegateHandler m_ClientVerificationAction;
5353
private NetworkSceneManager.VerifySceneBeforeLoadingDelegateHandler m_ServerVerificationAction;
5454

55+
public enum ServerType
56+
{
57+
Host,
58+
Server
59+
}
60+
5561
/// <summary>
5662
/// Tests the different types of NetworkSceneManager notifications (including exceptions) generated
5763
/// Also tests invalid loading scenarios (i.e. client trying to load a scene)
5864
/// </summary>
5965
[UnityTest]
60-
public IEnumerator SceneLoadingAndNotifications([Values(LoadSceneMode.Single, LoadSceneMode.Additive)] LoadSceneMode clientSynchronizationMode)
66+
public IEnumerator SceneLoadingAndNotifications([Values(LoadSceneMode.Single, LoadSceneMode.Additive)] LoadSceneMode clientSynchronizationMode, [Values(ServerType.Host, ServerType.Server)] ServerType serverType)
6167
{
68+
// First we disconnect and shutdown because we want to verify the synchronize events
69+
yield return Teardown();
70+
71+
// Give a little time for handling clean-up and the like
72+
yield return new WaitForSeconds(0.01f);
73+
74+
// We set this to true in order to bypass the automatic starting of the host and clients
75+
m_BypassStartAndWaitForClients = true;
76+
77+
// Now just create the instances (server and client) without starting anything
78+
yield return Setup();
79+
80+
// This provides both host and server coverage, when a server we should still get SceneEventType.LoadEventCompleted and SceneEventType.UnloadEventCompleted events
81+
// but the client count as a server should be 1 less than when a host
82+
var isHost = serverType == ServerType.Host ? true : false;
83+
84+
// Start the host and clients
85+
if (!MultiInstanceHelpers.Start(isHost, m_ServerNetworkManager, m_ClientNetworkManagers, SceneManagerValidationAndTestRunnerInitialization))
86+
{
87+
Debug.LogError("Failed to start instances");
88+
Assert.Fail("Failed to start instances");
89+
}
90+
91+
// Wait for connection on client side
92+
yield return MultiInstanceHelpers.Run(MultiInstanceHelpers.WaitForClientsConnected(m_ClientNetworkManagers));
93+
94+
var numberOfClients = isHost ? NbClients + 1 : NbClients;
95+
// Wait for connection on server side
96+
yield return MultiInstanceHelpers.Run(MultiInstanceHelpers.WaitForClientsConnectedToServer(m_ServerNetworkManager, numberOfClients));
97+
6298
m_ServerNetworkManager.SceneManager.OnSceneEvent += SceneManager_OnSceneEvent;
6399
m_CurrentSceneName = k_AdditiveScene1;
64100

@@ -327,8 +363,17 @@ private void SceneManager_OnSceneEvent(SceneEvent sceneEvent)
327363
{
328364
Assert.AreEqual(sceneEvent.SceneName, m_CurrentSceneName);
329365
Assert.IsTrue(ContainsClient(sceneEvent.ClientId));
366+
367+
// If we are a server and this is being processed by the server, then add the server to the completed list
368+
// to validate that the event completed on all clients (and the server).
369+
if (!m_ServerNetworkManager.IsHost && sceneEvent.ClientId == m_ServerNetworkManager.LocalClientId &&
370+
!sceneEvent.ClientsThatCompleted.Contains(m_ServerNetworkManager.LocalClientId))
371+
{
372+
sceneEvent.ClientsThatCompleted.Add(m_ServerNetworkManager.LocalClientId);
373+
}
330374
Assert.IsTrue(ContainsAllClients(sceneEvent.ClientsThatCompleted));
331375
SetClientWaitDone(sceneEvent.ClientsThatCompleted);
376+
332377
break;
333378
}
334379
}

0 commit comments

Comments
 (0)