corrade-vassal – Blame information for rev 22

Subversion Repositories:
Rev:
Rev Author Line No. Line
7 zed 1 ///////////////////////////////////////////////////////////////////////////
2 // Copyright (C) Wizardry and Steamworks 2015 - License: GNU GPLv3 //
3 // Please see: http://www.gnu.org/licenses/gpl.html for legal details, //
4 // rights of fair usage, the disclaimer and warranty conditions. //
5 ///////////////////////////////////////////////////////////////////////////
6  
7 using System;
2 zed 8 using System.Drawing;
9 using System.Linq;
16 eva 10 using System.Net.Http.Headers;
7 zed 11 using System.Net.Sockets;
12 using System.Reflection;
2 zed 13 using System.Threading;
14 using System.Windows.Forms;
15 using OpenMetaverse;
16 eva 16 using wasSharp;
22 office 17 using wasSharp.Web;
2 zed 18  
19 namespace Vassal
20 {
21 public partial class SettingsForm : Form
22 {
23 private static SettingsForm mainForm;
24  
25 private readonly Action GetUserConfiguration = () =>
26 {
27 // general
28 mainForm.HTTPServerURL.Text = Vassal.vassalConfiguration.HTTPServerURL;
29 mainForm.Group.Text = Vassal.vassalConfiguration.Group;
30 mainForm.Password.Text = Vassal.vassalConfiguration.Password;
31 mainForm.TeleportTimeout.Text = Vassal.vassalConfiguration.TeleportTimeout.ToString(Utils.EnUsCulture);
16 eva 32 mainForm.ServicesTimeout.Text = Vassal.vassalConfiguration.ServicesTimeout.ToString(Utils.EnUsCulture);
5 eva 33 mainForm.RegionRestartDelay.Text = Vassal.vassalConfiguration.RegionRestartDelay.ToString(Utils.EnUsCulture);
2 zed 34  
35 // filters
36 mainForm.ActiveInputFilters.Items.Clear();
16 eva 37 foreach (var filter in Vassal.vassalConfiguration.InputFilters)
2 zed 38 {
39 mainForm.ActiveInputFilters.Items.Add(new ListViewItem
40 {
16 eva 41 Text = Reflection.GetDescriptionFromEnumValue(filter),
2 zed 42 Tag = filter
43 });
44 }
45 mainForm.ActiveOutputFilters.Items.Clear();
46 mainForm.ActiveInputFilters.DisplayMember = "Text";
16 eva 47 foreach (var filter in Vassal.vassalConfiguration.OutputFilters)
2 zed 48 {
49 mainForm.ActiveOutputFilters.Items.Add(new ListViewItem
50 {
16 eva 51 Text = Reflection.GetDescriptionFromEnumValue(filter),
2 zed 52 Tag = filter
53 });
54 }
55 mainForm.ActiveOutputFilters.DisplayMember = "Text";
56  
57 // cryptography
58 mainForm.ENIGMARotorSequence.Items.Clear();
16 eva 59 foreach (var rotor in Vassal.vassalConfiguration.ENIGMA.rotors)
2 zed 60 {
61 mainForm.ENIGMARotorSequence.Items.Add(new ListViewItem
62 {
63 Text = rotor.ToString(),
64 Tag = rotor
65 });
66 }
67 mainForm.ENIGMARotorSequence.DisplayMember = "Text";
68 mainForm.ENIGMAPlugSequence.Items.Clear();
16 eva 69 foreach (var plug in Vassal.vassalConfiguration.ENIGMA.plugs)
2 zed 70 {
71 mainForm.ENIGMAPlugSequence.Items.Add(new ListViewItem
72 {
73 Text = plug.ToString(),
74 Tag = plug
75 });
76 }
77 mainForm.ENIGMAPlugSequence.DisplayMember = "Text";
78 mainForm.ENIGMAReflector.Text = Vassal.vassalConfiguration.ENIGMA.reflector.ToString();
79 mainForm.VIGENERESecret.Text = Vassal.vassalConfiguration.VIGENERESecret;
16 eva 80  
81 // HTTP
82 string mediaType;
83 switch (Vassal.vassalConfiguration.InputFilters.LastOrDefault())
84 {
85 case Filter.RFC1738:
86 mediaType = @"application/x-www-form-urlencoded";
87 break;
88 default:
89 mediaType = @"text/plain";
90 break;
91 }
22 office 92 Vassal.HTTPClient = new wasHTTPClient(new ProductInfoHeaderValue(@"Vassal", Vassal.VASSAL_VERSION),
93 mediaType, 60000);
2 zed 94 };
95  
8 eva 96 private readonly Action SetUserConfiguration = () =>
97 {
98 // general
99 Vassal.vassalConfiguration.HTTPServerURL = mainForm.HTTPServerURL.Text;
100 Vassal.vassalConfiguration.Group = mainForm.Group.Text;
101 Vassal.vassalConfiguration.Password = mainForm.Password.Text;
102 uint outUint;
16 eva 103 if (uint.TryParse(mainForm.ServicesTimeout.Text, out outUint))
104 {
105 Vassal.vassalConfiguration.ServicesTimeout = outUint;
106 }
8 eva 107 if (uint.TryParse(mainForm.TeleportTimeout.Text, out outUint))
108 {
109 Vassal.vassalConfiguration.TeleportTimeout = outUint;
110 }
111 if (uint.TryParse(mainForm.RegionRestartDelay.Text, out outUint))
112 {
113 Vassal.vassalConfiguration.RegionRestartDelay = outUint;
114 }
115  
116 // filters
117 Vassal.vassalConfiguration.InputFilters =
118 mainForm.ActiveInputFilters.Items.Cast<ListViewItem>().Select(o => (Filter) o.Tag).ToList();
119 Vassal.vassalConfiguration.OutputFilters =
120 mainForm.ActiveOutputFilters.Items.Cast<ListViewItem>().Select(o => (Filter) o.Tag).ToList();
121  
122 // cryptography
123 Vassal.vassalConfiguration.ENIGMA = new ENIGMA
124 {
125 rotors = mainForm.ENIGMARotorSequence.Items.Cast<ListViewItem>().Select(o => (char) o.Tag).ToArray(),
126 plugs = mainForm.ENIGMAPlugSequence.Items.Cast<ListViewItem>().Select(o => (char) o.Tag).ToArray(),
127 reflector = mainForm.ENIGMAReflector.Text[0]
128 };
129  
130 Vassal.vassalConfiguration.VIGENERESecret = mainForm.VIGENERESecret.Text;
16 eva 131  
132 // HTTP
133 string mediaType;
134 switch (Vassal.vassalConfiguration.InputFilters.LastOrDefault())
135 {
136 case Filter.RFC1738:
137 mediaType = @"application/x-www-form-urlencoded";
138 break;
139 default:
140 mediaType = @"text/plain";
141 break;
142 }
143 // Create HTTP Client
22 office 144 Vassal.HTTPClient = new wasHTTPClient(new ProductInfoHeaderValue(@"Vassal",
20 office 145 Vassal.VASSAL_VERSION), mediaType, 60000);
8 eva 146 };
147  
2 zed 148 public SettingsForm()
149 {
150 InitializeComponent();
151 mainForm = this;
152 }
153  
154 private void LoadSettingsRequested(object sender, EventArgs e)
155 {
8 eva 156 mainForm.BeginInvoke((MethodInvoker) (() =>
2 zed 157 {
158 switch (mainForm.LoadSettingsDialog.ShowDialog())
159 {
160 case DialogResult.OK:
16 eva 161 var file = mainForm.LoadSettingsDialog.FileName;
2 zed 162 new Thread(() =>
163 {
8 eva 164 mainForm.BeginInvoke((MethodInvoker) (() =>
2 zed 165 {
166 try
167 {
168 mainForm.StatusText.Text = @"loading settings...";
169 mainForm.StatusProgress.Value = 0;
170  
171 // load settings
172 VassalConfiguration.Load(file, ref Vassal.vassalConfiguration);
173 mainForm.StatusProgress.Value = 50;
174 GetUserConfiguration.Invoke();
175  
176 mainForm.StatusText.Text = @"settings loaded";
177 mainForm.StatusProgress.Value = 100;
178 }
179 catch (Exception ex)
180 {
181 mainForm.StatusText.Text = ex.Message;
182 }
183 }));
184 })
8 eva 185 {IsBackground = true, Priority = ThreadPriority.Normal}.Start();
2 zed 186 break;
187 }
188 }));
189 }
190  
191 private void SaveSettingsRequested(object sender, EventArgs e)
192 {
8 eva 193 mainForm.BeginInvoke((MethodInvoker) (() =>
2 zed 194 {
195 switch (mainForm.SaveSettingsDialog.ShowDialog())
196 {
197 case DialogResult.OK:
16 eva 198 var file = mainForm.SaveSettingsDialog.FileName;
2 zed 199 new Thread(() =>
200 {
8 eva 201 mainForm.BeginInvoke((MethodInvoker) (() =>
2 zed 202 {
203 try
204 {
205 mainForm.StatusText.Text = @"saving settings...";
206 mainForm.StatusProgress.Value = 0;
207  
208 // apply configuration
209 SetUserConfiguration.Invoke();
210 mainForm.StatusProgress.Value = 50;
211  
212 // save settings
213 VassalConfiguration.Save(file, ref Vassal.vassalConfiguration);
214  
215 mainForm.StatusText.Text = @"settings saved";
216 mainForm.StatusProgress.Value = 100;
217 }
218 catch (Exception ex)
219 {
220 mainForm.StatusText.Text = ex.Message;
221 }
222 }));
223 })
8 eva 224 {IsBackground = true, Priority = ThreadPriority.Normal}.Start();
2 zed 225 break;
226 }
227 }));
228 }
229  
230 private void AddInputDecoderRequested(object sender, EventArgs e)
231 {
8 eva 232 mainForm.BeginInvoke((MethodInvoker) (() =>
2 zed 233 {
234 if (string.IsNullOrEmpty(InputDecode.Text))
235 {
236 InputDecode.BackColor = Color.MistyRose;
237 return;
238 }
239 InputDecode.BackColor = Color.Empty;
240 ActiveInputFilters.Items.Add(new ListViewItem
241 {
242 Text = InputDecode.Text,
20 office 243 Tag = Reflection.GetEnumValueFromName<Filter>(InputDecode.Text)
2 zed 244 });
245 }));
246 }
247  
248 private void AddInputDecryptionRequested(object sender, EventArgs e)
249 {
8 eva 250 mainForm.BeginInvoke((MethodInvoker) (() =>
2 zed 251 {
252 if (string.IsNullOrEmpty(InputDecryption.Text))
253 {
254 InputDecryption.BackColor = Color.MistyRose;
255 return;
256 }
257 InputDecryption.BackColor = Color.Empty;
258 ActiveInputFilters.Items.Add(new ListViewItem
259 {
260 Text = InputDecryption.Text,
20 office 261 Tag = Reflection.GetEnumValueFromName<Filter>(InputDecryption.Text)
2 zed 262 });
263 }));
264 }
265  
266 private void AddOutputEncryptionRequested(object sender, EventArgs e)
267 {
8 eva 268 mainForm.BeginInvoke((MethodInvoker) (() =>
2 zed 269 {
270 if (string.IsNullOrEmpty(OutputEncrypt.Text))
271 {
272 OutputEncrypt.BackColor = Color.MistyRose;
273 return;
274 }
275 OutputEncrypt.BackColor = Color.Empty;
276 ActiveOutputFilters.Items.Add(new ListViewItem
277 {
278 Text = OutputEncrypt.Text,
20 office 279 Tag = Reflection.GetEnumValueFromName<Filter>(OutputEncrypt.Text)
2 zed 280 });
281 }));
282 }
283  
284 private void AddOutputEncoderRequested(object sender, EventArgs e)
285 {
8 eva 286 mainForm.BeginInvoke((MethodInvoker) (() =>
2 zed 287 {
288 if (string.IsNullOrEmpty(OutputEncode.Text))
289 {
290 OutputEncode.BackColor = Color.MistyRose;
291 return;
292 }
293 OutputEncode.BackColor = Color.Empty;
294 ActiveOutputFilters.Items.Add(new ListViewItem
295 {
296 Text = OutputEncode.Text,
20 office 297 Tag = Reflection.GetEnumValueFromName<Filter>(OutputEncode.Text)
2 zed 298 });
299 }));
300 }
301  
302 private void DeleteSelectedOutputFilterRequested(object sender, EventArgs e)
303 {
8 eva 304 mainForm.BeginInvoke((MethodInvoker) (() =>
2 zed 305 {
16 eva 306 var listViewItem = ActiveOutputFilters.SelectedItem as ListViewItem;
2 zed 307 if (listViewItem == null)
308 {
309 ActiveOutputFilters.BackColor = Color.MistyRose;
310 return;
311 }
312 ActiveOutputFilters.BackColor = Color.Empty;
313 ActiveOutputFilters.Items.RemoveAt(ActiveOutputFilters.SelectedIndex);
314 }));
315 }
316  
317 private void DeleteSelectedInputFilterRequested(object sender, EventArgs e)
318 {
8 eva 319 mainForm.BeginInvoke((MethodInvoker) (() =>
2 zed 320 {
16 eva 321 var listViewItem = ActiveInputFilters.SelectedItem as ListViewItem;
2 zed 322 if (listViewItem == null)
323 {
324 ActiveInputFilters.BackColor = Color.MistyRose;
325 return;
326 }
327 ActiveInputFilters.BackColor = Color.Empty;
328 ActiveInputFilters.Items.RemoveAt(ActiveInputFilters.SelectedIndex);
329 }));
330 }
331  
332 private void AddENIGMARotorRequested(object sender, EventArgs e)
333 {
8 eva 334 mainForm.BeginInvoke((MethodInvoker) (() =>
2 zed 335 {
336 if (string.IsNullOrEmpty(ENIGMARotor.Text))
337 {
338 ENIGMARotor.BackColor = Color.MistyRose;
339 return;
340 }
341 ENIGMARotor.BackColor = Color.Empty;
342 ENIGMARotorSequence.Items.Add(new ListViewItem
343 {
344 Text = ENIGMARotor.Text,
345 Tag = ENIGMARotor.Text[0]
346 });
347 }));
348 }
349  
350 private void DeleteENIGMARotorRequested(object sender, EventArgs e)
351 {
8 eva 352 mainForm.BeginInvoke((MethodInvoker) (() =>
2 zed 353 {
16 eva 354 var listViewItem = ENIGMARotorSequence.SelectedItem as ListViewItem;
2 zed 355 if (listViewItem == null)
356 {
357 ENIGMARotorSequence.BackColor = Color.MistyRose;
358 return;
359 }
360 ENIGMARotorSequence.BackColor = Color.Empty;
361 ENIGMARotorSequence.Items.RemoveAt(ENIGMARotorSequence.SelectedIndex);
362 }));
363 }
364  
365 private void AddENIGMAPlugRequested(object sender, EventArgs e)
366 {
8 eva 367 mainForm.BeginInvoke((MethodInvoker) (() =>
2 zed 368 {
369 if (string.IsNullOrEmpty(ENIGMARing.Text))
370 {
371 ENIGMARing.BackColor = Color.MistyRose;
372 return;
373 }
374 ENIGMARing.BackColor = Color.Empty;
375 ENIGMAPlugSequence.Items.Add(new ListViewItem
376 {
377 Text = ENIGMARing.Text,
378 Tag = ENIGMARing.Text[0]
379 });
380 }));
381 }
382  
383 private void DeleteENIGMAPlugRequested(object sender, EventArgs e)
384 {
8 eva 385 mainForm.BeginInvoke((MethodInvoker) (() =>
2 zed 386 {
16 eva 387 var listViewItem = ENIGMAPlugSequence.SelectedItem as ListViewItem;
2 zed 388 if (listViewItem == null)
389 {
390 ENIGMAPlugSequence.BackColor = Color.MistyRose;
391 return;
392 }
393 ENIGMAPlugSequence.BackColor = Color.Empty;
394 ENIGMAPlugSequence.Items.RemoveAt(ENIGMAPlugSequence.SelectedIndex);
395 }));
396 }
397  
398 private void SettingsFormShown(object sender, EventArgs e)
399 {
400 GetUserConfiguration.Invoke();
401 }
5 eva 402  
403 private void SettingsFormClosing(object sender, FormClosingEventArgs e)
404 {
405 // apply configuration
406 SetUserConfiguration.Invoke();
407 // save settings
408 VassalConfiguration.Save(Vassal.VASSAL_CONSTANTS.VASSAL_CONFIGURATION_FILE, ref Vassal.vassalConfiguration);
7 zed 409 // Spawn a thread to check Corrade's connection status.
410 new Thread(() =>
411 {
16 eva 412 var tcpClient = new TcpClient();
7 zed 413 try
414 {
16 eva 415 var uri = new Uri(Vassal.vassalConfiguration.HTTPServerURL);
7 zed 416 tcpClient.Connect(uri.Host, uri.Port);
417 // port open
8 eva 418 Vassal.vassalForm.BeginInvoke((MethodInvoker) (() => { Vassal.vassalForm.Tabs.Enabled = true; }));
7 zed 419 // set the loading spinner
8 eva 420 if (Vassal.vassalForm.RegionAvatarsMap.Image == null)
7 zed 421 {
16 eva 422 var thisAssembly = Assembly.GetExecutingAssembly();
423 var file =
8 eva 424 thisAssembly.GetManifestResourceStream("Vassal.img.loading.gif");
425 switch (file != null)
426 {
427 case true:
428 Vassal.vassalForm.BeginInvoke((MethodInvoker) (() =>
429 {
430 Vassal.vassalForm.RegionAvatarsMap.SizeMode = PictureBoxSizeMode.CenterImage;
431 Vassal.vassalForm.RegionAvatarsMap.Image = Image.FromStream(file);
432 Vassal.vassalForm.RegionAvatarsMap.Refresh();
433 }));
434 break;
435 }
7 zed 436 }
437 }
438 catch (Exception)
439 {
440 // port closed
8 eva 441 Vassal.vassalForm.BeginInvoke((MethodInvoker) (() => { Vassal.vassalForm.Tabs.Enabled = false; }));
7 zed 442 }
443 })
8 eva 444 {IsBackground = true}.Start();
445  
446 // set parameters for Vassal
13 eva 447 mainForm.Invoke((MethodInvoker) (() =>
8 eva 448 {
13 eva 449 Vassal.vassalForm.Invoke((MethodInvoker) (() =>
8 eva 450 {
451 if (string.IsNullOrEmpty(Vassal.vassalForm.RegionRestartDelayBox.Text))
452 {
453 Vassal.vassalForm.RegionRestartDelayBox.Text = mainForm.RegionRestartDelay.Text;
454 }
455 Vassal.vassalForm.VassalStatusGroup.Enabled = true;
456 }));
457 }));
5 eva 458 }
16 eva 459  
460 private void ShowToolTip(object sender, EventArgs e)
461 {
462 mainForm.BeginInvoke(
463 (Action) (() =>
464 {
465 var pictureBox = sender as PictureBox;
466 if (pictureBox != null)
467 {
468 toolTip1.Show(toolTip1.GetToolTip(pictureBox), pictureBox);
469 }
470 }));
471 }
2 zed 472 }
8 eva 473 }