WingMan – Blame information for rev 8
?pathlinks?
Rev | Author | Line No. | Line |
---|---|---|---|
1 | office | 1 | using System; |
2 | using System.Collections.Generic; |
||
3 | using System.Drawing; |
||
4 | using System.Linq; |
||
5 | using System.Net; |
||
7 | office | 6 | using System.Threading; |
6 | office | 7 | using System.Threading.Tasks; |
1 | office | 8 | using System.Windows.Forms; |
4 | office | 9 | using Gma.System.MouseKeyHook; |
1 | office | 10 | using WingMan.Communication; |
7 | office | 11 | using WingMan.Lobby; |
5 | office | 12 | using WingMan.MouseKey; |
13 | using WingMan.Properties; |
||
8 | office | 14 | using WingMan.Utilities; |
1 | office | 15 | |
16 | namespace WingMan |
||
17 | { |
||
2 | office | 18 | public partial class WingManForm : Form |
1 | office | 19 | { |
5 | office | 20 | public WingManForm() |
21 | { |
||
22 | InitializeComponent(); |
||
1 | office | 23 | |
6 | office | 24 | FormTaskScheduler = TaskScheduler.FromCurrentSynchronizationContext(); |
7 | office | 25 | FormCancellationTokenSource = new CancellationTokenSource(); |
1 | office | 26 | |
7 | office | 27 | MQTTCommunication = new MQTTCommunication(FormTaskScheduler, FormCancellationTokenSource.Token); |
8 | office | 28 | MQTTCommunication.OnClientAuthenticationFailed += OnMQTTClientAuthenticationFailed; |
29 | MQTTCommunication.OnServerAuthenticationFailed += OnMQTTServerAuthenticationFailed; |
||
6 | office | 30 | |
5 | office | 31 | MouseKeyBindings = new MouseKeyBindings(new List<MouseKeyBinding>()); |
1 | office | 32 | |
7 | office | 33 | HelmListBoxBindingSource = new BindingSource |
5 | office | 34 | { |
35 | DataSource = MouseKeyBindings.Bindings |
||
36 | }; |
||
37 | HelmBindingsListBox.DisplayMember = "DisplayName"; |
||
38 | HelmBindingsListBox.ValueMember = "Keys"; |
||
7 | office | 39 | HelmBindingsListBox.DataSource = HelmListBoxBindingSource; |
1 | office | 40 | |
7 | office | 41 | MouseKeyBindingsExchange = new MouseKeyBindingsExchange |
5 | office | 42 | { |
7 | office | 43 | ExchangeBindings = new List<MouseKeyBindingExchange>() |
44 | }; |
||
45 | |||
46 | WingBindingsComboBoxSource = new BindingSource |
||
47 | { |
||
5 | office | 48 | DataSource = MouseKeyBindingsExchange.ExchangeBindings |
49 | }; |
||
50 | WingBindingsComboBox.DisplayMember = "Nick"; |
||
7 | office | 51 | WingBindingsComboBox.ValueMember = "MouseKeyBindings"; |
52 | WingBindingsComboBox.DataSource = WingBindingsComboBoxSource; |
||
4 | office | 53 | |
5 | office | 54 | // Start lobby message synchronizer. |
7 | office | 55 | LobbyMessageSynchronizer = new LobbyMessageSynchronizer(MQTTCommunication, FormTaskScheduler, |
56 | FormCancellationTokenSource.Token); |
||
5 | office | 57 | LobbyMessageSynchronizer.OnLobbyMessageReceived += OnLobbyMessageReceived; |
4 | office | 58 | |
5 | office | 59 | // Start mouse key bindings synchronizer. |
7 | office | 60 | MouseKeyBindingsSynchronizer = new MouseKeyBindingsSynchronizer(MouseKeyBindings, MQTTCommunication, |
61 | FormTaskScheduler, FormCancellationTokenSource.Token); |
||
5 | office | 62 | MouseKeyBindingsSynchronizer.OnMouseKeyBindingsSynchronized += OnMouseKeyBindingsSynchronized; |
63 | } |
||
4 | office | 64 | |
7 | office | 65 | private static CancellationTokenSource FormCancellationTokenSource { get; set; } |
4 | office | 66 | |
7 | office | 67 | private static TaskScheduler FormTaskScheduler { get; set; } |
4 | office | 68 | |
7 | office | 69 | private static IKeyboardMouseEvents MouseKeyApplicationHook { get; set; } |
4 | office | 70 | |
7 | office | 71 | private List<string> MouseKeyCombo { get; set; } |
2 | office | 72 | |
7 | office | 73 | private MouseKeyBindings MouseKeyBindings { get; } |
4 | office | 74 | |
7 | office | 75 | private BindingSource HelmListBoxBindingSource { get; } |
4 | office | 76 | |
7 | office | 77 | private BindingSource WingBindingsComboBoxSource { get; } |
4 | office | 78 | |
7 | office | 79 | private MouseKeyBindingsExchange MouseKeyBindingsExchange { get; } |
80 | |||
81 | public MQTTCommunication MQTTCommunication { get; set; } |
||
82 | |||
83 | public LobbyMessageSynchronizer LobbyMessageSynchronizer { get; set; } |
||
84 | |||
85 | public MouseKeyBindingsSynchronizer MouseKeyBindingsSynchronizer { get; set; } |
||
86 | |||
8 | office | 87 | private void OnMQTTServerAuthenticationFailed(object sender, EventArgs e) |
88 | { |
||
89 | ActivityTextBox.AppendText( |
||
90 | $"{Strings.Failed_to_authenticate_client}{Environment.NewLine}"); |
||
91 | } |
||
92 | |||
93 | private void OnMQTTClientAuthenticationFailed(object sender, EventArgs e) |
||
94 | { |
||
95 | ActivityTextBox.AppendText( |
||
96 | $"{Strings.Server_authentication_failed}{Environment.NewLine}"); |
||
97 | } |
||
98 | |||
7 | office | 99 | /// <summary> |
100 | /// Clean up any resources being used. |
||
101 | /// </summary> |
||
102 | /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param> |
||
103 | protected override void Dispose(bool disposing) |
||
104 | { |
||
105 | if (disposing && components != null) |
||
106 | { |
||
107 | FormCancellationTokenSource.Dispose(); |
||
108 | FormCancellationTokenSource = null; |
||
109 | |||
110 | components.Dispose(); |
||
6 | office | 111 | } |
4 | office | 112 | |
7 | office | 113 | base.Dispose(disposing); |
1 | office | 114 | } |
115 | |||
7 | office | 116 | private void OnMouseKeyBindingsSynchronized(object sender, MouseKeyBindingsSynchronizedEventArgs e) |
117 | { |
||
118 | ActivityTextBox.AppendText( |
||
119 | $"{Strings.Synchronized_bindings_with_client} : {e.Bindings.Nick} : {e.Bindings.MouseKeyBindings.Count}{Environment.NewLine}"); |
||
6 | office | 120 | |
7 | office | 121 | var exchangeBindings = MouseKeyBindingsExchange.ExchangeBindings.FirstOrDefault(exchangeBinding => |
122 | string.Equals(exchangeBinding.Nick, e.Bindings.Nick, StringComparison.Ordinal)); |
||
5 | office | 123 | |
7 | office | 124 | // If the nick does not exist then add it. |
125 | if (exchangeBindings == null) |
||
126 | { |
||
127 | MouseKeyBindingsExchange.ExchangeBindings.Add(e.Bindings); |
||
128 | WingBindingsComboBoxSource.ResetBindings(false); |
||
129 | UpdateWingListBoxItems(); |
||
130 | return; |
||
131 | } |
||
5 | office | 132 | |
7 | office | 133 | // If the bindings for the nick have not changed then do not update. |
134 | if (exchangeBindings.MouseKeyBindings.SequenceEqual(e.Bindings.MouseKeyBindings)) |
||
135 | { |
||
136 | WingBindingsComboBoxSource.ResetBindings(false); |
||
137 | UpdateWingListBoxItems(); |
||
138 | return; |
||
139 | } |
||
5 | office | 140 | |
7 | office | 141 | // Update the bindings. |
142 | exchangeBindings.MouseKeyBindings = e.Bindings.MouseKeyBindings; |
||
143 | WingBindingsComboBoxSource.ResetBindings(false); |
||
144 | UpdateWingListBoxItems(); |
||
145 | } |
||
5 | office | 146 | |
7 | office | 147 | private void UpdateWingListBoxItems() |
148 | { |
||
149 | var exchangeBinding = (List<MouseKeyBinding>) WingBindingsComboBox.SelectedValue; |
||
150 | if (exchangeBinding == null) |
||
151 | return; |
||
5 | office | 152 | |
7 | office | 153 | WingBindingsListBox.Items.Clear(); |
154 | WingBindingsListBox.DisplayMember = "Name"; |
||
155 | WingBindingsListBox.ValueMember = "Name"; |
||
156 | var i = exchangeBinding.Select(binding => (object) binding.Name).ToArray(); |
||
157 | if (i.Length == 0) |
||
158 | return; |
||
5 | office | 159 | |
7 | office | 160 | WingBindingsListBox.Items.AddRange(i); |
161 | } |
||
5 | office | 162 | |
163 | private void OnLobbyMessageReceived(object sender, LobbyMessageReceivedEventArgs e) |
||
164 | { |
||
165 | LobbyTextBox.Invoke((MethodInvoker) delegate |
||
166 | { |
||
167 | LobbyTextBox.AppendText($"{e.Nick} : {e.Message}{Environment.NewLine}"); |
||
168 | }); |
||
169 | } |
||
170 | |||
1 | office | 171 | private void AddressTextBoxClick(object sender, EventArgs e) |
172 | { |
||
173 | Address.BackColor = Color.Empty; |
||
174 | } |
||
175 | |||
176 | private void PortTextBoxClick(object sender, EventArgs e) |
||
177 | { |
||
178 | Port.BackColor = Color.Empty; |
||
179 | } |
||
180 | |||
181 | private async void HostButtonClickAsync(object sender, EventArgs e) |
||
182 | { |
||
183 | // Stop the MQTT server if it is running. |
||
5 | office | 184 | if (MQTTCommunication.Running) |
1 | office | 185 | { |
5 | office | 186 | await MQTTCommunication.Stop().ConfigureAwait(false); |
187 | toolStripStatusLabel.Text = Strings.Server_stopped; |
||
1 | office | 188 | HostButton.BackColor = Color.Empty; |
189 | |||
3 | office | 190 | // Enable controls. |
1 | office | 191 | ConnectButton.Enabled = true; |
5 | office | 192 | Address.Enabled = true; |
193 | Port.Enabled = true; |
||
194 | Nick.Enabled = true; |
||
195 | |||
1 | office | 196 | return; |
197 | } |
||
198 | |||
8 | office | 199 | if (!ValidateAddressPort(out var ipAddress, out var port, out var nick, out var password)) |
1 | office | 200 | return; |
201 | |||
202 | // Start the MQTT server. |
||
8 | office | 203 | await MQTTCommunication.Start(MQTTCommunicationType.Server, ipAddress, port, nick, password) |
204 | .ConfigureAwait(false); |
||
5 | office | 205 | toolStripStatusLabel.Text = Strings.Server_started; |
1 | office | 206 | HostButton.BackColor = Color.Aquamarine; |
207 | |||
3 | office | 208 | // Disable controls |
1 | office | 209 | ConnectButton.Enabled = false; |
3 | office | 210 | Address.Enabled = false; |
211 | Port.Enabled = false; |
||
212 | Nick.Enabled = false; |
||
213 | } |
||
214 | |||
8 | office | 215 | private bool ValidateAddressPort(out IPAddress address, out int port, out string nick, out string password) |
1 | office | 216 | { |
217 | address = IPAddress.Any; |
||
218 | port = 0; |
||
2 | office | 219 | nick = string.Empty; |
8 | office | 220 | password = string.Empty; |
1 | office | 221 | |
222 | if (string.IsNullOrEmpty(Address.Text) && |
||
2 | office | 223 | string.IsNullOrEmpty(Port.Text) && |
8 | office | 224 | string.IsNullOrEmpty(Nick.Text) && |
225 | string.IsNullOrEmpty(Password.Text)) |
||
1 | office | 226 | { |
227 | Address.BackColor = Color.LightPink; |
||
228 | Port.BackColor = Color.LightPink; |
||
2 | office | 229 | Nick.BackColor = Color.LightPink; |
8 | office | 230 | Password.BackColor = Color.LightPink; |
1 | office | 231 | return false; |
232 | } |
||
233 | |||
234 | if (!IPAddress.TryParse(Address.Text, out address)) |
||
235 | { |
||
236 | Address.BackColor = Color.LightPink; |
||
237 | return false; |
||
238 | } |
||
239 | |||
240 | if (!uint.TryParse(Port.Text, out var uPort)) |
||
241 | { |
||
242 | Port.BackColor = Color.LightPink; |
||
243 | return false; |
||
244 | } |
||
245 | |||
246 | port = (int) uPort; |
||
247 | |||
2 | office | 248 | if (string.IsNullOrEmpty(Nick.Text)) |
249 | { |
||
250 | Nick.BackColor = Color.LightPink; |
||
251 | return false; |
||
252 | } |
||
253 | |||
254 | nick = Nick.Text; |
||
255 | |||
8 | office | 256 | if (string.IsNullOrEmpty(Password.Text)) |
257 | { |
||
258 | Password.BackColor = Color.LightPink; |
||
259 | return false; |
||
260 | } |
||
261 | |||
262 | password = AES.LinearFeedbackShiftPassword(Password.Text); |
||
263 | |||
1 | office | 264 | Address.BackColor = Color.Empty; |
265 | Port.BackColor = Color.Empty; |
||
2 | office | 266 | Nick.BackColor = Color.Empty; |
8 | office | 267 | Password.BackColor = Color.Empty; |
1 | office | 268 | |
269 | return true; |
||
270 | } |
||
271 | |||
272 | private async void ConnectButtonClickAsync(object sender, EventArgs e) |
||
273 | { |
||
5 | office | 274 | if (MQTTCommunication.Running) |
1 | office | 275 | { |
5 | office | 276 | await MQTTCommunication.Stop().ConfigureAwait(false); |
277 | ConnectButton.Text = Strings.Connect; |
||
1 | office | 278 | ConnectButton.BackColor = Color.Empty; |
279 | |||
5 | office | 280 | Address.Enabled = true; |
281 | Port.Enabled = true; |
||
282 | Nick.Enabled = true; |
||
1 | office | 283 | HostButton.Enabled = true; |
284 | return; |
||
285 | } |
||
286 | |||
8 | office | 287 | if (!ValidateAddressPort(out var ipAddress, out var port, out var nick, out var password)) |
1 | office | 288 | return; |
289 | |||
8 | office | 290 | await MQTTCommunication.Start(MQTTCommunicationType.Client, ipAddress, port, nick, password) |
291 | .ConfigureAwait(false); |
||
1 | office | 292 | |
5 | office | 293 | toolStripStatusLabel.Text = Strings.Client_started; |
294 | ConnectButton.Text = Strings.Disconnect; |
||
1 | office | 295 | ConnectButton.BackColor = Color.Aquamarine; |
296 | |||
297 | HostButton.Enabled = false; |
||
5 | office | 298 | Address.Enabled = false; |
299 | Port.Enabled = false; |
||
300 | Nick.Enabled = false; |
||
1 | office | 301 | } |
302 | |||
2 | office | 303 | private async void LobbySayTextBoxKeyDown(object sender, KeyEventArgs e) |
304 | { |
||
305 | if (e.KeyCode != Keys.Enter) |
||
306 | return; |
||
307 | |||
5 | office | 308 | await LobbyMessageSynchronizer.Broadcast(LobbySayTextBox.Text).ConfigureAwait(false); |
2 | office | 309 | |
5 | office | 310 | LobbySayTextBox.Text = string.Empty; |
2 | office | 311 | } |
4 | office | 312 | |
313 | private void HelmAddButtonClick(object sender, EventArgs e) |
||
314 | { |
||
315 | if (string.IsNullOrEmpty(HelmNameTextBox.Text)) |
||
316 | { |
||
317 | HelmNameTextBox.BackColor = Color.LightPink; |
||
318 | return; |
||
319 | } |
||
320 | |||
8 | office | 321 | ShowOverlayPanel(); |
4 | office | 322 | |
323 | MouseKeyCombo = new List<string>(); |
||
324 | |||
325 | MouseKeyApplicationHook = Hook.AppEvents(); |
||
326 | MouseKeyApplicationHook.MouseDown += MouseKeyHookOnMouseDown; |
||
327 | MouseKeyApplicationHook.KeyUp += MouseKeyHookOnKeyUp; |
||
328 | MouseKeyApplicationHook.KeyDown += MouseKeyHookOnKeyDown; |
||
329 | MouseKeyApplicationHook.MouseUp += MouseKeyHookOnMouseUp; |
||
330 | } |
||
331 | |||
8 | office | 332 | private void ShowOverlayPanel() |
333 | { |
||
334 | OverlayPanel.BringToFront(); |
||
335 | OverlayPanel.Visible = true; |
||
336 | OverlayPanel.Invalidate(); |
||
337 | } |
||
338 | |||
4 | office | 339 | private void MouseKeyHookOnKeyUp(object sender, KeyEventArgs e) |
340 | { |
||
5 | office | 341 | MouseKeyBindings.Bindings.Add(new MouseKeyBinding(HelmNameTextBox.Text, MouseKeyCombo)); |
4 | office | 342 | |
7 | office | 343 | HelmListBoxBindingSource.ResetBindings(false); |
4 | office | 344 | |
345 | MouseKeyApplicationHook.KeyDown -= MouseKeyHookOnKeyDown; |
||
346 | MouseKeyApplicationHook.KeyUp -= MouseKeyHookOnKeyUp; |
||
5 | office | 347 | MouseKeyApplicationHook.KeyDown -= MouseKeyHookOnKeyDown; |
348 | MouseKeyApplicationHook.KeyUp -= MouseKeyHookOnKeyUp; |
||
4 | office | 349 | |
350 | MouseKeyApplicationHook.Dispose(); |
||
351 | |||
352 | HelmNameTextBox.Text = string.Empty; |
||
8 | office | 353 | HideOverlayPanel(); |
4 | office | 354 | } |
355 | |||
8 | office | 356 | private void HideOverlayPanel() |
357 | { |
||
358 | OverlayPanel.SendToBack(); |
||
359 | OverlayPanel.Visible = false; |
||
360 | OverlayPanel.Invalidate(); |
||
361 | } |
||
362 | |||
4 | office | 363 | private void MouseKeyHookOnMouseUp(object sender, MouseEventArgs e) |
364 | { |
||
5 | office | 365 | MouseKeyBindings.Bindings.Add(new MouseKeyBinding(HelmNameTextBox.Text, MouseKeyCombo)); |
4 | office | 366 | |
7 | office | 367 | HelmListBoxBindingSource.ResetBindings(false); |
4 | office | 368 | |
369 | MouseKeyApplicationHook.KeyDown -= MouseKeyHookOnKeyDown; |
||
370 | MouseKeyApplicationHook.KeyUp -= MouseKeyHookOnKeyUp; |
||
5 | office | 371 | MouseKeyApplicationHook.KeyDown -= MouseKeyHookOnKeyDown; |
372 | MouseKeyApplicationHook.KeyUp -= MouseKeyHookOnKeyUp; |
||
4 | office | 373 | |
374 | MouseKeyApplicationHook.Dispose(); |
||
375 | |||
376 | HelmNameTextBox.Text = string.Empty; |
||
8 | office | 377 | HideOverlayPanel(); |
4 | office | 378 | } |
379 | |||
380 | |||
381 | private void MouseKeyHookOnMouseDown(object sender, MouseEventArgs e) |
||
382 | { |
||
5 | office | 383 | MouseKeyCombo.Add(e.Button.ToDisplayName()); |
4 | office | 384 | } |
385 | |||
386 | private void MouseKeyHookOnKeyDown(object sender, KeyEventArgs e) |
||
387 | { |
||
388 | e.SuppressKeyPress = true; |
||
389 | |||
5 | office | 390 | MouseKeyCombo.Add(e.KeyCode.ToDisplayName()); |
4 | office | 391 | } |
392 | |||
393 | private void HelmNameTextBoxClick(object sender, EventArgs e) |
||
394 | { |
||
395 | HelmNameTextBox.BackColor = Color.Empty; |
||
396 | } |
||
397 | |||
5 | office | 398 | private void HelmRemoveButtonClick(object sender, EventArgs e) |
4 | office | 399 | { |
5 | office | 400 | var helmBinding = (MouseKeyBinding) HelmBindingsListBox.SelectedItem; |
401 | if (helmBinding == null) |
||
402 | return; |
||
4 | office | 403 | |
5 | office | 404 | MouseKeyBindings.Bindings.Remove(helmBinding); |
7 | office | 405 | HelmListBoxBindingSource.ResetBindings(false); |
4 | office | 406 | } |
407 | |||
5 | office | 408 | private async void LobbySayButtonClick(object sender, EventArgs e) |
4 | office | 409 | { |
5 | office | 410 | await LobbyMessageSynchronizer.Broadcast(LobbySayTextBox.Text).ConfigureAwait(false); |
4 | office | 411 | |
5 | office | 412 | LobbySayTextBox.Text = string.Empty; |
4 | office | 413 | } |
414 | |||
6 | office | 415 | private void WingBindingsComboBoxSelectionChangeCompleted(object sender, EventArgs e) |
4 | office | 416 | { |
7 | office | 417 | UpdateWingListBoxItems(); |
4 | office | 418 | } |
8 | office | 419 | |
420 | private void WingBindingsBindButtonClicked(object sender, EventArgs e) |
||
421 | { |
||
422 | } |
||
1 | office | 423 | } |
424 | } |