corrade-vassal – Blame information for rev 2
?pathlinks?
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 | } |
||
72 | |||
73 | // filters |
||
74 | Vassal.vassalConfiguration.InputFilters = |
||
75 | mainForm.ActiveInputFilters.Items.Cast<ListViewItem>().Select(o => (Filter)o.Tag).ToList(); |
||
76 | Vassal.vassalConfiguration.OutputFilters = |
||
77 | mainForm.ActiveOutputFilters.Items.Cast<ListViewItem>().Select(o => (Filter)o.Tag).ToList(); |
||
78 | |||
79 | // cryptography |
||
80 | Vassal.vassalConfiguration.ENIGMA = new ENIGMA |
||
81 | { |
||
82 | rotors = mainForm.ENIGMARotorSequence.Items.Cast<ListViewItem>().Select(o => (char)o.Tag).ToArray(), |
||
83 | plugs = mainForm.ENIGMAPlugSequence.Items.Cast<ListViewItem>().Select(o => (char)o.Tag).ToArray(), |
||
84 | reflector = mainForm.ENIGMAReflector.Text[0] |
||
85 | }; |
||
86 | |||
87 | Vassal.vassalConfiguration.VIGENERESecret = mainForm.VIGENERESecret.Text; |
||
88 | |||
89 | }; |
||
90 | |||
91 | private readonly Action GetUserConfiguration = () => |
||
92 | { |
||
93 | // general |
||
94 | mainForm.HTTPServerURL.Text = Vassal.vassalConfiguration.HTTPServerURL; |
||
95 | mainForm.Group.Text = Vassal.vassalConfiguration.Group; |
||
96 | mainForm.Password.Text = Vassal.vassalConfiguration.Password; |
||
97 | mainForm.TeleportTimeout.Text = Vassal.vassalConfiguration.TeleportTimeout.ToString(Utils.EnUsCulture); |
||
98 | mainForm.DataTimeout.Text = Vassal.vassalConfiguration.DataTimeout.ToString(Utils.EnUsCulture); |
||
99 | |||
100 | // filters |
||
101 | mainForm.ActiveInputFilters.Items.Clear(); |
||
102 | foreach (Filter filter in Vassal.vassalConfiguration.InputFilters) |
||
103 | { |
||
104 | mainForm.ActiveInputFilters.Items.Add(new ListViewItem |
||
105 | { |
||
106 | Text = wasGetDescriptionFromEnumValue(filter), |
||
107 | Tag = filter |
||
108 | }); |
||
109 | } |
||
110 | mainForm.ActiveOutputFilters.Items.Clear(); |
||
111 | mainForm.ActiveInputFilters.DisplayMember = "Text"; |
||
112 | foreach (Filter filter in Vassal.vassalConfiguration.OutputFilters) |
||
113 | { |
||
114 | mainForm.ActiveOutputFilters.Items.Add(new ListViewItem |
||
115 | { |
||
116 | Text = wasGetDescriptionFromEnumValue(filter), |
||
117 | Tag = filter |
||
118 | }); |
||
119 | } |
||
120 | mainForm.ActiveOutputFilters.DisplayMember = "Text"; |
||
121 | |||
122 | // cryptography |
||
123 | mainForm.ENIGMARotorSequence.Items.Clear(); |
||
124 | foreach (char rotor in Vassal.vassalConfiguration.ENIGMA.rotors) |
||
125 | { |
||
126 | mainForm.ENIGMARotorSequence.Items.Add(new ListViewItem |
||
127 | { |
||
128 | Text = rotor.ToString(), |
||
129 | Tag = rotor |
||
130 | }); |
||
131 | } |
||
132 | mainForm.ENIGMARotorSequence.DisplayMember = "Text"; |
||
133 | mainForm.ENIGMAPlugSequence.Items.Clear(); |
||
134 | foreach (char plug in Vassal.vassalConfiguration.ENIGMA.plugs) |
||
135 | { |
||
136 | mainForm.ENIGMAPlugSequence.Items.Add(new ListViewItem |
||
137 | { |
||
138 | Text = plug.ToString(), |
||
139 | Tag = plug |
||
140 | }); |
||
141 | } |
||
142 | mainForm.ENIGMAPlugSequence.DisplayMember = "Text"; |
||
143 | mainForm.ENIGMAReflector.Text = Vassal.vassalConfiguration.ENIGMA.reflector.ToString(); |
||
144 | mainForm.VIGENERESecret.Text = Vassal.vassalConfiguration.VIGENERESecret; |
||
145 | }; |
||
146 | |||
147 | public SettingsForm() |
||
148 | { |
||
149 | InitializeComponent(); |
||
150 | mainForm = this; |
||
151 | } |
||
152 | |||
153 | private void LoadSettingsRequested(object sender, EventArgs e) |
||
154 | { |
||
155 | mainForm.BeginInvoke((MethodInvoker)(() => |
||
156 | { |
||
157 | switch (mainForm.LoadSettingsDialog.ShowDialog()) |
||
158 | { |
||
159 | case DialogResult.OK: |
||
160 | string file = mainForm.LoadSettingsDialog.FileName; |
||
161 | new Thread(() => |
||
162 | { |
||
163 | mainForm.BeginInvoke((MethodInvoker)(() => |
||
164 | { |
||
165 | try |
||
166 | { |
||
167 | mainForm.StatusText.Text = @"loading settings..."; |
||
168 | mainForm.StatusProgress.Value = 0; |
||
169 | |||
170 | // load settings |
||
171 | VassalConfiguration.Load(file, ref Vassal.vassalConfiguration); |
||
172 | mainForm.StatusProgress.Value = 50; |
||
173 | GetUserConfiguration.Invoke(); |
||
174 | |||
175 | mainForm.StatusText.Text = @"settings loaded"; |
||
176 | mainForm.StatusProgress.Value = 100; |
||
177 | } |
||
178 | catch (Exception ex) |
||
179 | { |
||
180 | mainForm.StatusText.Text = ex.Message; |
||
181 | } |
||
182 | })); |
||
183 | }) |
||
184 | { IsBackground = true, Priority = ThreadPriority.Normal }.Start(); |
||
185 | break; |
||
186 | } |
||
187 | })); |
||
188 | } |
||
189 | |||
190 | private void SaveSettingsRequested(object sender, EventArgs e) |
||
191 | { |
||
192 | mainForm.BeginInvoke((MethodInvoker)(() => |
||
193 | { |
||
194 | switch (mainForm.SaveSettingsDialog.ShowDialog()) |
||
195 | { |
||
196 | case DialogResult.OK: |
||
197 | string file = mainForm.SaveSettingsDialog.FileName; |
||
198 | new Thread(() => |
||
199 | { |
||
200 | mainForm.BeginInvoke((MethodInvoker)(() => |
||
201 | { |
||
202 | try |
||
203 | { |
||
204 | mainForm.StatusText.Text = @"saving settings..."; |
||
205 | mainForm.StatusProgress.Value = 0; |
||
206 | |||
207 | // apply configuration |
||
208 | SetUserConfiguration.Invoke(); |
||
209 | mainForm.StatusProgress.Value = 50; |
||
210 | |||
211 | // save settings |
||
212 | VassalConfiguration.Save(file, ref Vassal.vassalConfiguration); |
||
213 | |||
214 | mainForm.StatusText.Text = @"settings saved"; |
||
215 | mainForm.StatusProgress.Value = 100; |
||
216 | } |
||
217 | catch (Exception ex) |
||
218 | { |
||
219 | mainForm.StatusText.Text = ex.Message; |
||
220 | } |
||
221 | })); |
||
222 | }) |
||
223 | { IsBackground = true, Priority = ThreadPriority.Normal }.Start(); |
||
224 | break; |
||
225 | } |
||
226 | })); |
||
227 | } |
||
228 | |||
229 | private void AddInputDecoderRequested(object sender, EventArgs e) |
||
230 | { |
||
231 | mainForm.BeginInvoke((MethodInvoker)(() => |
||
232 | { |
||
233 | if (string.IsNullOrEmpty(InputDecode.Text)) |
||
234 | { |
||
235 | InputDecode.BackColor = Color.MistyRose; |
||
236 | return; |
||
237 | } |
||
238 | InputDecode.BackColor = Color.Empty; |
||
239 | ActiveInputFilters.Items.Add(new ListViewItem |
||
240 | { |
||
241 | Text = InputDecode.Text, |
||
242 | Tag = wasGetEnumValueFromDescription<Filter>(InputDecode.Text) |
||
243 | }); |
||
244 | })); |
||
245 | } |
||
246 | |||
247 | private void AddInputDecryptionRequested(object sender, EventArgs e) |
||
248 | { |
||
249 | mainForm.BeginInvoke((MethodInvoker)(() => |
||
250 | { |
||
251 | if (string.IsNullOrEmpty(InputDecryption.Text)) |
||
252 | { |
||
253 | InputDecryption.BackColor = Color.MistyRose; |
||
254 | return; |
||
255 | } |
||
256 | InputDecryption.BackColor = Color.Empty; |
||
257 | ActiveInputFilters.Items.Add(new ListViewItem |
||
258 | { |
||
259 | Text = InputDecryption.Text, |
||
260 | Tag = wasGetEnumValueFromDescription<Filter>(InputDecryption.Text) |
||
261 | }); |
||
262 | })); |
||
263 | } |
||
264 | |||
265 | private void AddOutputEncryptionRequested(object sender, EventArgs e) |
||
266 | { |
||
267 | mainForm.BeginInvoke((MethodInvoker)(() => |
||
268 | { |
||
269 | if (string.IsNullOrEmpty(OutputEncrypt.Text)) |
||
270 | { |
||
271 | OutputEncrypt.BackColor = Color.MistyRose; |
||
272 | return; |
||
273 | } |
||
274 | OutputEncrypt.BackColor = Color.Empty; |
||
275 | ActiveOutputFilters.Items.Add(new ListViewItem |
||
276 | { |
||
277 | Text = OutputEncrypt.Text, |
||
278 | Tag = wasGetEnumValueFromDescription<Filter>(OutputEncrypt.Text) |
||
279 | }); |
||
280 | })); |
||
281 | } |
||
282 | |||
283 | private void AddOutputEncoderRequested(object sender, EventArgs e) |
||
284 | { |
||
285 | mainForm.BeginInvoke((MethodInvoker)(() => |
||
286 | { |
||
287 | if (string.IsNullOrEmpty(OutputEncode.Text)) |
||
288 | { |
||
289 | OutputEncode.BackColor = Color.MistyRose; |
||
290 | return; |
||
291 | } |
||
292 | OutputEncode.BackColor = Color.Empty; |
||
293 | ActiveOutputFilters.Items.Add(new ListViewItem |
||
294 | { |
||
295 | Text = OutputEncode.Text, |
||
296 | Tag = wasGetEnumValueFromDescription<Filter>(OutputEncode.Text) |
||
297 | }); |
||
298 | })); |
||
299 | } |
||
300 | |||
301 | private void DeleteSelectedOutputFilterRequested(object sender, EventArgs e) |
||
302 | { |
||
303 | mainForm.BeginInvoke((MethodInvoker)(() => |
||
304 | { |
||
305 | ListViewItem listViewItem = ActiveOutputFilters.SelectedItem as ListViewItem; |
||
306 | if (listViewItem == null) |
||
307 | { |
||
308 | ActiveOutputFilters.BackColor = Color.MistyRose; |
||
309 | return; |
||
310 | } |
||
311 | ActiveOutputFilters.BackColor = Color.Empty; |
||
312 | ActiveOutputFilters.Items.RemoveAt(ActiveOutputFilters.SelectedIndex); |
||
313 | })); |
||
314 | } |
||
315 | |||
316 | private void DeleteSelectedInputFilterRequested(object sender, EventArgs e) |
||
317 | { |
||
318 | mainForm.BeginInvoke((MethodInvoker)(() => |
||
319 | { |
||
320 | ListViewItem listViewItem = ActiveInputFilters.SelectedItem as ListViewItem; |
||
321 | if (listViewItem == null) |
||
322 | { |
||
323 | ActiveInputFilters.BackColor = Color.MistyRose; |
||
324 | return; |
||
325 | } |
||
326 | ActiveInputFilters.BackColor = Color.Empty; |
||
327 | ActiveInputFilters.Items.RemoveAt(ActiveInputFilters.SelectedIndex); |
||
328 | })); |
||
329 | } |
||
330 | |||
331 | private void AddENIGMARotorRequested(object sender, EventArgs e) |
||
332 | { |
||
333 | mainForm.BeginInvoke((MethodInvoker)(() => |
||
334 | { |
||
335 | if (string.IsNullOrEmpty(ENIGMARotor.Text)) |
||
336 | { |
||
337 | ENIGMARotor.BackColor = Color.MistyRose; |
||
338 | return; |
||
339 | } |
||
340 | ENIGMARotor.BackColor = Color.Empty; |
||
341 | ENIGMARotorSequence.Items.Add(new ListViewItem |
||
342 | { |
||
343 | Text = ENIGMARotor.Text, |
||
344 | Tag = ENIGMARotor.Text[0] |
||
345 | }); |
||
346 | })); |
||
347 | } |
||
348 | |||
349 | private void DeleteENIGMARotorRequested(object sender, EventArgs e) |
||
350 | { |
||
351 | mainForm.BeginInvoke((MethodInvoker)(() => |
||
352 | { |
||
353 | ListViewItem listViewItem = ENIGMARotorSequence.SelectedItem as ListViewItem; |
||
354 | if (listViewItem == null) |
||
355 | { |
||
356 | ENIGMARotorSequence.BackColor = Color.MistyRose; |
||
357 | return; |
||
358 | } |
||
359 | ENIGMARotorSequence.BackColor = Color.Empty; |
||
360 | ENIGMARotorSequence.Items.RemoveAt(ENIGMARotorSequence.SelectedIndex); |
||
361 | })); |
||
362 | } |
||
363 | |||
364 | private void AddENIGMAPlugRequested(object sender, EventArgs e) |
||
365 | { |
||
366 | mainForm.BeginInvoke((MethodInvoker)(() => |
||
367 | { |
||
368 | if (string.IsNullOrEmpty(ENIGMARing.Text)) |
||
369 | { |
||
370 | ENIGMARing.BackColor = Color.MistyRose; |
||
371 | return; |
||
372 | } |
||
373 | ENIGMARing.BackColor = Color.Empty; |
||
374 | ENIGMAPlugSequence.Items.Add(new ListViewItem |
||
375 | { |
||
376 | Text = ENIGMARing.Text, |
||
377 | Tag = ENIGMARing.Text[0] |
||
378 | }); |
||
379 | })); |
||
380 | } |
||
381 | |||
382 | private void DeleteENIGMAPlugRequested(object sender, EventArgs e) |
||
383 | { |
||
384 | mainForm.BeginInvoke((MethodInvoker)(() => |
||
385 | { |
||
386 | ListViewItem listViewItem = ENIGMAPlugSequence.SelectedItem as ListViewItem; |
||
387 | if (listViewItem == null) |
||
388 | { |
||
389 | ENIGMAPlugSequence.BackColor = Color.MistyRose; |
||
390 | return; |
||
391 | } |
||
392 | ENIGMAPlugSequence.BackColor = Color.Empty; |
||
393 | ENIGMAPlugSequence.Items.RemoveAt(ENIGMAPlugSequence.SelectedIndex); |
||
394 | })); |
||
395 | } |
||
396 | |||
397 | private void SettingsFormShown(object sender, EventArgs e) |
||
398 | { |
||
399 | GetUserConfiguration.Invoke(); |
||
400 | } |
||
401 | } |
||
402 | } |