corrade-vassal – Blame information for rev 5

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