corrade-vassal – Blame information for rev 8

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