-
Notifications
You must be signed in to change notification settings - Fork 517
/
Copy pathNetworkSimulatorUIMediator.cs
291 lines (253 loc) · 9.63 KB
/
NetworkSimulatorUIMediator.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using TMPro;
using Unity.Multiplayer.Tools.NetworkSimulator.Runtime;
using Unity.Multiplayer.Tools.NetworkSimulator.Runtime.BuiltInScenarios;
using Unity.Netcode;
using UnityEngine;
using UnityEngine.UI;
namespace Unity.BossRoom.Utils
{
public class NetworkSimulatorUIMediator : MonoBehaviour
{
#if UNITY_EDITOR || DEVELOPMENT_BUILD
[SerializeField]
NetworkSimulator m_NetworkSimulator;
#endif
[SerializeField]
CanvasGroup m_CanvasGroup;
#if UNITY_EDITOR || DEVELOPMENT_BUILD
[SerializeField]
TMP_Dropdown m_PresetsDropdown;
[SerializeField]
TMP_Dropdown m_ScenariosDropdown;
[SerializeField]
Button m_ScenariosButton;
[SerializeField]
TextMeshProUGUI m_ScenariosButtonText;
[SerializeField]
TMP_InputField m_LagSpikeDuration;
[SerializeField]
KeyCode m_OpenWindowKeyCode = KeyCode.Tilde;
[SerializeField]
List<ConnectionsCycle.Configuration> m_ConnectionsCycleConfigurations;
[SerializeField]
List<RandomConnectionsSwap.Configuration> m_RandomConnectionsSwapConfigurations;
[SerializeField]
int m_RandomConnectionsSwapChangeIntervalMilliseconds;
const int k_NbTouchesToOpenWindow = 5;
Dictionary<string, INetworkSimulatorPreset> m_SimulatorPresets = new Dictionary<string, INetworkSimulatorPreset>();
#endif
bool m_Shown;
const string k_None = "None";
const string k_ConnectionCyclesScenarioName = "Connections Cycle";
const string k_RandomConnectionSwapScenarioName = "Random Connections Swap";
const string k_PauseString = "Pause";
const string k_ResumeString = "Resume";
void Awake()
{
#if UNITY_EDITOR || DEVELOPMENT_BUILD
InitializeUI();
#endif
// Hide UI until ready
Hide();
}
public void Hide()
{
m_CanvasGroup.alpha = 0f;
m_CanvasGroup.interactable = false;
m_CanvasGroup.blocksRaycasts = false;
m_Shown = false;
}
#if UNITY_EDITOR || DEVELOPMENT_BUILD
void Start()
{
NetworkManager.Singleton.OnClientStarted += OnNetworkManagerStarted;
NetworkManager.Singleton.OnServerStarted += OnNetworkManagerStarted;
}
void OnDestroy()
{
if (NetworkManager.Singleton is not null)
{
NetworkManager.Singleton.OnClientStarted -= OnNetworkManagerStarted;
NetworkManager.Singleton.OnServerStarted -= OnNetworkManagerStarted;
}
}
void OnNetworkManagerStarted()
{
if (m_NetworkSimulator.IsAvailable)
{
Show();
}
}
void OnPresetChanged(int optionIndex)
{
m_NetworkSimulator.ChangeConnectionPreset(m_SimulatorPresets[m_PresetsDropdown.options[optionIndex].text]);
}
void OnScenarioChanged(int optionIndex)
{
var scenarioName = m_ScenariosDropdown.options[optionIndex].text;
NetworkScenario scenario = null;
switch (scenarioName)
{
case k_None:
m_PresetsDropdown.captionText.color = m_PresetsDropdown.colors.normalColor;
m_PresetsDropdown.interactable = true;
break;
case k_ConnectionCyclesScenarioName:
var connectionsCyleScenario = new ConnectionsCycle();
connectionsCyleScenario.Configurations.Clear();
foreach (var configuration in m_ConnectionsCycleConfigurations)
{
connectionsCyleScenario.Configurations.Add(configuration);
}
m_PresetsDropdown.captionText.color = m_PresetsDropdown.colors.disabledColor;
m_PresetsDropdown.interactable = false;
scenario = connectionsCyleScenario;
break;
case k_RandomConnectionSwapScenarioName:
var randomConnectionsSwapScenario = new RandomConnectionsSwap();
randomConnectionsSwapScenario.Configurations.Clear();
foreach (var configuration in m_RandomConnectionsSwapConfigurations)
{
randomConnectionsSwapScenario.Configurations.Add(configuration);
}
m_PresetsDropdown.captionText.color = m_PresetsDropdown.colors.disabledColor;
m_PresetsDropdown.interactable = false;
scenario = randomConnectionsSwapScenario;
break;
default:
Debug.LogError("Invalid Scenario selected.");
m_PresetsDropdown.captionText.color = m_PresetsDropdown.colors.normalColor;
m_PresetsDropdown.interactable = true;
break;
}
m_NetworkSimulator.Scenario = scenario;
if (m_NetworkSimulator.Scenario != null)
{
m_NetworkSimulator.Scenario.Start(m_NetworkSimulator);
}
UpdateScenarioButton();
}
void Show()
{
m_CanvasGroup.alpha = 1f;
m_CanvasGroup.interactable = true;
m_CanvasGroup.blocksRaycasts = true;
UpdateScenarioButton();
m_Shown = true;
}
void ToggleVisibility()
{
if (m_Shown)
{
Hide();
}
else
{
Show();
}
}
void InitializeUI()
{
// Initialize connection presets dropdown
var optionData = new List<TMP_Dropdown.OptionData>();
// Adding all available presets
foreach (var networkSimulatorPreset in NetworkSimulatorPresets.Values)
{
m_SimulatorPresets[networkSimulatorPreset.Name] = networkSimulatorPreset;
optionData.Add(new TMP_Dropdown.OptionData(networkSimulatorPreset.Name));
}
m_PresetsDropdown.AddOptions(optionData);
m_PresetsDropdown.onValueChanged.AddListener(OnPresetChanged);
// Initialize scenario dropdown
optionData = new List<TMP_Dropdown.OptionData>();
// Adding empty scenario
optionData.Add(new TMP_Dropdown.OptionData(k_None));
// Adding ConnectionsCycle scenario
optionData.Add(new TMP_Dropdown.OptionData(k_ConnectionCyclesScenarioName));
// Adding RandomConnectionsSwap scenario
optionData.Add(new TMP_Dropdown.OptionData(k_RandomConnectionSwapScenarioName));
m_ScenariosDropdown.AddOptions(optionData);
m_ScenariosDropdown.onValueChanged.AddListener(OnScenarioChanged);
}
void Update()
{
if (m_NetworkSimulator.IsAvailable)
{
if (Input.touchCount == k_NbTouchesToOpenWindow && AnyTouchDown() ||
m_OpenWindowKeyCode != KeyCode.None && Input.GetKeyDown(m_OpenWindowKeyCode))
{
ToggleVisibility();
}
var selectedPreset = m_PresetsDropdown.options[m_PresetsDropdown.value].text;
if (selectedPreset != m_NetworkSimulator.CurrentPreset.Name)
{
for (var i = 0; i < m_PresetsDropdown.options.Count; i++)
{
if (m_PresetsDropdown.options[i].text == m_NetworkSimulator.CurrentPreset.Name)
{
m_PresetsDropdown.value = i;
}
}
}
}
else
{
if (m_Shown)
{
Hide();
}
}
}
static bool AnyTouchDown()
{
foreach (var touch in Input.touches)
{
if (touch.phase == TouchPhase.Began)
{
return true;
}
}
return false;
}
public void SimulateDisconnect()
{
m_NetworkSimulator.Disconnect();
}
public void TriggerLagSpike()
{
double.TryParse(m_LagSpikeDuration.text, out var duration);
m_NetworkSimulator.TriggerLagSpike(TimeSpan.FromMilliseconds(duration));
}
public void SanitizeLagSpikeDurationInputField()
{
m_LagSpikeDuration.text = Regex.Replace(m_LagSpikeDuration.text, "[^0-9]", "");
}
public void TriggerScenario()
{
if (m_NetworkSimulator.Scenario != null)
{
m_NetworkSimulator.Scenario.IsPaused = !m_NetworkSimulator.Scenario.IsPaused;
UpdateScenarioButton();
}
}
void UpdateScenarioButton()
{
if (m_NetworkSimulator.Scenario != null)
{
m_ScenariosButtonText.text = m_NetworkSimulator.Scenario.IsPaused ? k_ResumeString : k_PauseString;
m_ScenariosButton.interactable = true;
m_ScenariosButtonText.color = m_ScenariosButton.colors.normalColor;
}
else
{
m_ScenariosButtonText.text = "None";
m_ScenariosButton.interactable = false;
m_ScenariosButtonText.color = m_ScenariosButton.colors.disabledColor;
}
}
#endif
}
}