corrade-vassal – Blame information for rev 7

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