Skip to content

Commit 2f645a8

Browse files
committed
Fixed additional code formatting
1 parent 339ab37 commit 2f645a8

4 files changed

Lines changed: 111 additions & 104 deletions

File tree

MLAPI/Data/SceneSwitchProgress.cs

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using UnityEngine;
4+
5+
namespace MLAPI.Components
6+
{
7+
/// <summary>
8+
/// Class for tracking scene switching progress by server and clients.
9+
/// </summary>
10+
public class SceneSwitchProgress
11+
{
12+
/// <summary>
13+
/// List of clientIds of those clients that is done loading the scene.
14+
/// </summary>
15+
public List<uint> DoneClients { get; } = new List<uint>();
16+
/// <summary>
17+
/// The NetworkTime time at the moment the scene switch was initiated by the server.
18+
/// </summary>
19+
public float TimeAtInitiation { get; } = NetworkingManager.singleton.NetworkTime;
20+
/// <summary>
21+
/// Delegate type for when the switch scene progress is completed. Either by all clients done loading the scene or by time out.
22+
/// </summary>
23+
public delegate void OnCompletedDelegate(bool timedOut);
24+
/// <summary>
25+
/// The callback invoked when the switch scene progress is completed. Either by all clients done loading the scene or by time out.
26+
/// </summary>
27+
public event OnCompletedDelegate OnComplete;
28+
/// <summary>
29+
/// Is this scene switch progresses completed, all clients are done loading the scene or a timeout has occured.
30+
/// </summary>
31+
public bool isCompleted { get; private set; }
32+
/// <summary>
33+
/// If all clients are done loading the scene, at the moment of completed.
34+
/// </summary>
35+
public bool isAllClientsDoneLoading { get; private set; }
36+
/// <summary>
37+
/// Delegate type for when a client is done loading the scene.
38+
/// </summary>
39+
public delegate void OnClientLoadedSceneDelegate(uint clientId);
40+
/// <summary>
41+
/// The callback invoked when a client is done loading the scene.
42+
/// </summary>
43+
public event OnClientLoadedSceneDelegate OnClientLoadedScene;
44+
45+
internal Guid guid { get; } = Guid.NewGuid();
46+
47+
private Coroutine timeOutCoroutine;
48+
private AsyncOperation sceneLoadOperation;
49+
50+
internal SceneSwitchProgress()
51+
{
52+
timeOutCoroutine = NetworkingManager.singleton.StartCoroutine(NetworkingManager.singleton.TimeOutSwitchSceneProgress(this));
53+
}
54+
55+
internal void AddClientAsDone(uint clientId)
56+
{
57+
DoneClients.Add(clientId);
58+
if (OnClientLoadedScene != null)
59+
OnClientLoadedScene.Invoke(clientId);
60+
CheckCompletion();
61+
}
62+
63+
internal void RemoveClientAsDone(uint clientId)
64+
{
65+
DoneClients.Remove(clientId);
66+
CheckCompletion();
67+
}
68+
69+
internal void SetSceneLoadOperation(AsyncOperation sceneLoadOperation)
70+
{
71+
this.sceneLoadOperation = sceneLoadOperation;
72+
this.sceneLoadOperation.completed += (AsyncOperation operation) => { CheckCompletion(); };
73+
}
74+
75+
internal void CheckCompletion()
76+
{
77+
if (!isCompleted && DoneClients.Count == NetworkingManager.singleton.ConnectedClientsList.Count && sceneLoadOperation.isDone)
78+
{
79+
isCompleted = true;
80+
isAllClientsDoneLoading = true;
81+
NetworkSceneManager.sceneSwitchProgresses.Remove(guid);
82+
if (OnComplete != null)
83+
OnComplete.Invoke(false);
84+
85+
NetworkingManager.singleton.StopCoroutine(timeOutCoroutine);
86+
}
87+
}
88+
89+
internal void SetTimedOut()
90+
{
91+
if (!isCompleted)
92+
{
93+
isCompleted = true;
94+
NetworkSceneManager.sceneSwitchProgresses.Remove(guid);
95+
if (OnComplete != null)
96+
OnComplete.Invoke(true);
97+
}
98+
}
99+
100+
}
101+
}

MLAPI/MLAPI.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@
9191
<Compile Include="Data\NetworkedVar.cs" />
9292
<Compile Include="Data\NetworkedVarMeta.cs" />
9393
<Compile Include="Data\PendingClient.cs" />
94+
<Compile Include="Data\SceneSwitchProgress.cs" />
9495
<Compile Include="Data\SecuritySendFlags.cs" />
9596
<Compile Include="Data\Transports\ChannelType.cs" />
9697
<Compile Include="Data\FixedQueue.cs" />

MLAPI/MonoBehaviours/Core/NetworkingManager.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,7 @@ private object Init(bool server)
260260
NetworkSceneManager.registeredSceneNames.Clear();
261261
NetworkSceneManager.sceneIndexToString.Clear();
262262
NetworkSceneManager.sceneNameToIndex.Clear();
263-
NetworkSceneManager.switchSceneProgresses.Clear();
263+
NetworkSceneManager.sceneSwitchProgresses.Clear();
264264

265265
try
266266
{
@@ -799,7 +799,7 @@ private IEnumerator ApprovalTimeout(uint clientId)
799799
}
800800
}
801801

802-
internal IEnumerator TimeOutSwitchSceneProgress(SwitchSceneProgress switchSceneProgress)
802+
internal IEnumerator TimeOutSwitchSceneProgress(SceneSwitchProgress switchSceneProgress)
803803
{
804804
yield return new WaitForSeconds(this.NetworkConfig.LoadSceneTimeOut);
805805
switchSceneProgress.SetTimedOut();

MLAPI/NetworkingManagerComponents/Core/NetworkSceneManager.cs

Lines changed: 7 additions & 102 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public static class NetworkSceneManager
1717
internal static readonly HashSet<string> registeredSceneNames = new HashSet<string>();
1818
internal static readonly Dictionary<string, uint> sceneNameToIndex = new Dictionary<string, uint>();
1919
internal static readonly Dictionary<uint, string> sceneIndexToString = new Dictionary<uint, string>();
20-
internal static Dictionary<Guid, SwitchSceneProgress> switchSceneProgresses = new Dictionary<Guid, SwitchSceneProgress>();
20+
internal static Dictionary<Guid, SceneSwitchProgress> sceneSwitchProgresses = new Dictionary<Guid, SceneSwitchProgress>();
2121
private static Scene lastScene;
2222
private static Scene nextScene;
2323
private static bool isSwitching = false;
@@ -41,7 +41,7 @@ internal static void SetCurrentSceneIndex ()
4141
/// Switches to a scene with a given name. Can only be called from Server
4242
/// </summary>
4343
/// <param name="sceneName">The name of the scene to switch to</param>
44-
public static SwitchSceneProgress SwitchScene(string sceneName)
44+
public static SceneSwitchProgress SwitchScene(string sceneName)
4545
{
4646
if(!NetworkingManager.singleton.NetworkConfig.EnableSceneSwitching)
4747
{
@@ -63,8 +63,8 @@ public static SwitchSceneProgress SwitchScene(string sceneName)
6363
isSwitching = true;
6464
lastScene = SceneManager.GetActiveScene();
6565

66-
SwitchSceneProgress switchSceneProgress = new SwitchSceneProgress();
67-
switchSceneProgresses.Add(switchSceneProgress.guid, switchSceneProgress);
66+
SceneSwitchProgress switchSceneProgress = new SceneSwitchProgress();
67+
sceneSwitchProgresses.Add(switchSceneProgress.guid, switchSceneProgress);
6868
CurrentSceneSwitchProgressGuid = switchSceneProgress.guid;
6969

7070
AsyncOperation sceneLoad = SceneManager.LoadSceneAsync(sceneName, LoadSceneMode.Additive);
@@ -201,114 +201,19 @@ internal static void OnClientSwitchSceneCompleted(uint clientId, Guid switchScen
201201
//If Guid is empty it means the client has loaded the start scene of the server and the server would never have a switchSceneProgresses created for the start scene.
202202
return;
203203
}
204-
if (!switchSceneProgresses.ContainsKey(switchSceneGuid))
204+
if (!sceneSwitchProgresses.ContainsKey(switchSceneGuid))
205205
{
206206
return;
207207
}
208208

209-
switchSceneProgresses[switchSceneGuid].AddClientAsDone(clientId);
209+
sceneSwitchProgresses[switchSceneGuid].AddClientAsDone(clientId);
210210
}
211211

212212

213213
internal static void removeClientFromSceneSwitchProgresses(uint clientId)
214214
{
215-
foreach (SwitchSceneProgress switchSceneProgress in switchSceneProgresses.Values)
215+
foreach (SceneSwitchProgress switchSceneProgress in sceneSwitchProgresses.Values)
216216
switchSceneProgress.RemoveClientAsDone(clientId);
217217
}
218218
}
219-
220-
/// <summary>
221-
/// Class for tracking scene switching progress by server and clients.
222-
/// </summary>
223-
public class SwitchSceneProgress
224-
{
225-
/// <summary>
226-
/// List of clientIds of those clients that is done loading the scene.
227-
/// </summary>
228-
public List<uint> DoneClients { get; } = new List<uint>();
229-
/// <summary>
230-
/// The NetworkTime time at the moment the scene switch was initiated by the server.
231-
/// </summary>
232-
public float TimeAtInitiation { get; } = NetworkingManager.singleton.NetworkTime;
233-
/// <summary>
234-
/// Delegate type for when the switch scene progress is completed. Either by all clients done loading the scene or by time out.
235-
/// </summary>
236-
public delegate void OnCompletedDelegate(bool timedOut);
237-
/// <summary>
238-
/// The callback invoked when the switch scene progress is completed. Either by all clients done loading the scene or by time out.
239-
/// </summary>
240-
public event OnCompletedDelegate OnComplete;
241-
/// <summary>
242-
/// Is this scene switch progresses completed, all clients are done loading the scene or a timeout has occured.
243-
/// </summary>
244-
public bool isCompleted { get; private set; }
245-
/// <summary>
246-
/// If all clients are done loading the scene, at the moment of completed.
247-
/// </summary>
248-
public bool isAllClientsDoneLoading { get; private set; }
249-
/// <summary>
250-
/// Delegate type for when a client is done loading the scene.
251-
/// </summary>
252-
public delegate void OnClientLoadedSceneDelegate(uint clientId);
253-
/// <summary>
254-
/// The callback invoked when a client is done loading the scene.
255-
/// </summary>
256-
public event OnClientLoadedSceneDelegate OnClientLoadedScene;
257-
258-
internal Guid guid { get; } = Guid.NewGuid();
259-
260-
private Coroutine timeOutCoroutine;
261-
private AsyncOperation sceneLoadOperation;
262-
263-
internal SwitchSceneProgress()
264-
{
265-
timeOutCoroutine = NetworkingManager.singleton.StartCoroutine(NetworkingManager.singleton.TimeOutSwitchSceneProgress(this));
266-
}
267-
268-
internal void AddClientAsDone(uint clientId)
269-
{
270-
DoneClients.Add(clientId);
271-
if (OnClientLoadedScene != null)
272-
OnClientLoadedScene.Invoke(clientId);
273-
CheckCompletion();
274-
}
275-
276-
internal void RemoveClientAsDone(uint clientId)
277-
{
278-
DoneClients.Remove(clientId);
279-
CheckCompletion();
280-
}
281-
282-
internal void SetSceneLoadOperation(AsyncOperation sceneLoadOperation)
283-
{
284-
this.sceneLoadOperation = sceneLoadOperation;
285-
this.sceneLoadOperation.completed += (AsyncOperation operation) => { CheckCompletion(); };
286-
}
287-
288-
internal void CheckCompletion()
289-
{
290-
if (!isCompleted && DoneClients.Count == NetworkingManager.singleton.ConnectedClientsList.Count && sceneLoadOperation.isDone)
291-
{
292-
isCompleted = true;
293-
isAllClientsDoneLoading = true;
294-
NetworkSceneManager.switchSceneProgresses.Remove(guid);
295-
if (OnComplete != null)
296-
OnComplete.Invoke(false);
297-
298-
NetworkingManager.singleton.StopCoroutine(timeOutCoroutine);
299-
}
300-
}
301-
302-
internal void SetTimedOut()
303-
{
304-
if (!isCompleted)
305-
{
306-
isCompleted = true;
307-
NetworkSceneManager.switchSceneProgresses.Remove(guid);
308-
if (OnComplete != null)
309-
OnComplete.Invoke(true);
310-
}
311-
}
312-
313-
}
314219
}

0 commit comments

Comments
 (0)