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