/Vassal/Vassal/SettingsForm.cs |
@@ -5,15 +5,16 @@ |
/////////////////////////////////////////////////////////////////////////// |
|
using System; |
using System.ComponentModel; |
using System.Drawing; |
using System.IO; |
using System.Linq; |
using System.Net; |
using System.Net.Http.Headers; |
using System.Net.Sockets; |
using System.Reflection; |
using System.Threading; |
using System.Windows.Forms; |
using OpenMetaverse; |
using wasSharp; |
|
namespace Vassal |
{ |
@@ -28,26 +29,26 @@ |
mainForm.Group.Text = Vassal.vassalConfiguration.Group; |
mainForm.Password.Text = Vassal.vassalConfiguration.Password; |
mainForm.TeleportTimeout.Text = Vassal.vassalConfiguration.TeleportTimeout.ToString(Utils.EnUsCulture); |
mainForm.DataTimeout.Text = Vassal.vassalConfiguration.DataTimeout.ToString(Utils.EnUsCulture); |
mainForm.ServicesTimeout.Text = Vassal.vassalConfiguration.ServicesTimeout.ToString(Utils.EnUsCulture); |
mainForm.RegionRestartDelay.Text = Vassal.vassalConfiguration.RegionRestartDelay.ToString(Utils.EnUsCulture); |
|
// filters |
mainForm.ActiveInputFilters.Items.Clear(); |
foreach (Filter filter in Vassal.vassalConfiguration.InputFilters) |
foreach (var filter in Vassal.vassalConfiguration.InputFilters) |
{ |
mainForm.ActiveInputFilters.Items.Add(new ListViewItem |
{ |
Text = wasGetDescriptionFromEnumValue(filter), |
Text = Reflection.GetDescriptionFromEnumValue(filter), |
Tag = filter |
}); |
} |
mainForm.ActiveOutputFilters.Items.Clear(); |
mainForm.ActiveInputFilters.DisplayMember = "Text"; |
foreach (Filter filter in Vassal.vassalConfiguration.OutputFilters) |
foreach (var filter in Vassal.vassalConfiguration.OutputFilters) |
{ |
mainForm.ActiveOutputFilters.Items.Add(new ListViewItem |
{ |
Text = wasGetDescriptionFromEnumValue(filter), |
Text = Reflection.GetDescriptionFromEnumValue(filter), |
Tag = filter |
}); |
} |
@@ -55,7 +56,7 @@ |
|
// cryptography |
mainForm.ENIGMARotorSequence.Items.Clear(); |
foreach (char rotor in Vassal.vassalConfiguration.ENIGMA.rotors) |
foreach (var rotor in Vassal.vassalConfiguration.ENIGMA.rotors) |
{ |
mainForm.ENIGMARotorSequence.Items.Add(new ListViewItem |
{ |
@@ -65,7 +66,7 @@ |
} |
mainForm.ENIGMARotorSequence.DisplayMember = "Text"; |
mainForm.ENIGMAPlugSequence.Items.Clear(); |
foreach (char plug in Vassal.vassalConfiguration.ENIGMA.plugs) |
foreach (var plug in Vassal.vassalConfiguration.ENIGMA.plugs) |
{ |
mainForm.ENIGMAPlugSequence.Items.Add(new ListViewItem |
{ |
@@ -76,6 +77,21 @@ |
mainForm.ENIGMAPlugSequence.DisplayMember = "Text"; |
mainForm.ENIGMAReflector.Text = Vassal.vassalConfiguration.ENIGMA.reflector.ToString(); |
mainForm.VIGENERESecret.Text = Vassal.vassalConfiguration.VIGENERESecret; |
|
// HTTP |
string mediaType; |
switch (Vassal.vassalConfiguration.InputFilters.LastOrDefault()) |
{ |
case Filter.RFC1738: |
mediaType = @"application/x-www-form-urlencoded"; |
break; |
default: |
mediaType = @"text/plain"; |
break; |
} |
// Create HTTP Client |
Vassal.HTTPClient = new Web.wasHTTPClient(new ProductInfoHeaderValue(@"Vassal", |
Vassal.VASSAL_VERSION), new CookieContainer(), mediaType, 60000); |
}; |
|
private readonly Action SetUserConfiguration = () => |
@@ -85,14 +101,14 @@ |
Vassal.vassalConfiguration.Group = mainForm.Group.Text; |
Vassal.vassalConfiguration.Password = mainForm.Password.Text; |
uint outUint; |
if (uint.TryParse(mainForm.ServicesTimeout.Text, out outUint)) |
{ |
Vassal.vassalConfiguration.ServicesTimeout = outUint; |
} |
if (uint.TryParse(mainForm.TeleportTimeout.Text, out outUint)) |
{ |
Vassal.vassalConfiguration.TeleportTimeout = outUint; |
} |
if (uint.TryParse(mainForm.DataTimeout.Text, out outUint)) |
{ |
Vassal.vassalConfiguration.DataTimeout = outUint; |
} |
if (uint.TryParse(mainForm.RegionRestartDelay.Text, out outUint)) |
{ |
Vassal.vassalConfiguration.RegionRestartDelay = outUint; |
@@ -113,6 +129,21 @@ |
}; |
|
Vassal.vassalConfiguration.VIGENERESecret = mainForm.VIGENERESecret.Text; |
|
// HTTP |
string mediaType; |
switch (Vassal.vassalConfiguration.InputFilters.LastOrDefault()) |
{ |
case Filter.RFC1738: |
mediaType = @"application/x-www-form-urlencoded"; |
break; |
default: |
mediaType = @"text/plain"; |
break; |
} |
// Create HTTP Client |
Vassal.HTTPClient = new Web.wasHTTPClient(new ProductInfoHeaderValue(@"Vassal", |
Vassal.VASSAL_VERSION), new CookieContainer(), mediaType, 60000); |
}; |
|
public SettingsForm() |
@@ -121,42 +152,6 @@ |
mainForm = this; |
} |
|
/////////////////////////////////////////////////////////////////////////// |
// Copyright (C) 2015 Wizardry and Steamworks - License: GNU GPLv3 // |
/////////////////////////////////////////////////////////////////////////// |
/// <summary> |
/// Get enumeration value from its description. |
/// </summary> |
/// <typeparam name="T">the enumeration type</typeparam> |
/// <param name="description">the description of a member</param> |
/// <returns>the value or the default of T if case no description found</returns> |
private static T wasGetEnumValueFromDescription<T>(string description) |
{ |
var field = typeof (T).GetFields() |
.AsParallel().SelectMany(f => f.GetCustomAttributes( |
typeof (DescriptionAttribute), false), ( |
f, a) => new {Field = f, Att = a}).SingleOrDefault(a => ((DescriptionAttribute) a.Att) |
.Description.Equals(description)); |
return field != null ? (T) field.Field.GetRawConstantValue() : default(T); |
} |
|
/////////////////////////////////////////////////////////////////////////// |
// Copyright (C) 2015 Wizardry and Steamworks - License: GNU GPLv3 // |
/////////////////////////////////////////////////////////////////////////// |
/// <summary> |
/// Get the description from an enumeration value. |
/// </summary> |
/// <param name="value">an enumeration value</param> |
/// <returns>the description or the empty string</returns> |
private static string wasGetDescriptionFromEnumValue(Enum value) |
{ |
DescriptionAttribute attribute = value.GetType() |
.GetField(value.ToString()) |
.GetCustomAttributes(typeof (DescriptionAttribute), false) |
.SingleOrDefault() as DescriptionAttribute; |
return attribute != null ? attribute.Description : string.Empty; |
} |
|
private void LoadSettingsRequested(object sender, EventArgs e) |
{ |
mainForm.BeginInvoke((MethodInvoker) (() => |
@@ -164,7 +159,7 @@ |
switch (mainForm.LoadSettingsDialog.ShowDialog()) |
{ |
case DialogResult.OK: |
string file = mainForm.LoadSettingsDialog.FileName; |
var file = mainForm.LoadSettingsDialog.FileName; |
new Thread(() => |
{ |
mainForm.BeginInvoke((MethodInvoker) (() => |
@@ -201,7 +196,7 @@ |
switch (mainForm.SaveSettingsDialog.ShowDialog()) |
{ |
case DialogResult.OK: |
string file = mainForm.SaveSettingsDialog.FileName; |
var file = mainForm.SaveSettingsDialog.FileName; |
new Thread(() => |
{ |
mainForm.BeginInvoke((MethodInvoker) (() => |
@@ -246,7 +241,7 @@ |
ActiveInputFilters.Items.Add(new ListViewItem |
{ |
Text = InputDecode.Text, |
Tag = wasGetEnumValueFromDescription<Filter>(InputDecode.Text) |
Tag = Reflection.GetEnumValueFromDescription<Filter>(InputDecode.Text) |
}); |
})); |
} |
@@ -264,7 +259,7 @@ |
ActiveInputFilters.Items.Add(new ListViewItem |
{ |
Text = InputDecryption.Text, |
Tag = wasGetEnumValueFromDescription<Filter>(InputDecryption.Text) |
Tag = Reflection.GetEnumValueFromDescription<Filter>(InputDecryption.Text) |
}); |
})); |
} |
@@ -282,7 +277,7 @@ |
ActiveOutputFilters.Items.Add(new ListViewItem |
{ |
Text = OutputEncrypt.Text, |
Tag = wasGetEnumValueFromDescription<Filter>(OutputEncrypt.Text) |
Tag = Reflection.GetEnumValueFromDescription<Filter>(OutputEncrypt.Text) |
}); |
})); |
} |
@@ -300,7 +295,7 @@ |
ActiveOutputFilters.Items.Add(new ListViewItem |
{ |
Text = OutputEncode.Text, |
Tag = wasGetEnumValueFromDescription<Filter>(OutputEncode.Text) |
Tag = Reflection.GetEnumValueFromDescription<Filter>(OutputEncode.Text) |
}); |
})); |
} |
@@ -309,7 +304,7 @@ |
{ |
mainForm.BeginInvoke((MethodInvoker) (() => |
{ |
ListViewItem listViewItem = ActiveOutputFilters.SelectedItem as ListViewItem; |
var listViewItem = ActiveOutputFilters.SelectedItem as ListViewItem; |
if (listViewItem == null) |
{ |
ActiveOutputFilters.BackColor = Color.MistyRose; |
@@ -324,7 +319,7 @@ |
{ |
mainForm.BeginInvoke((MethodInvoker) (() => |
{ |
ListViewItem listViewItem = ActiveInputFilters.SelectedItem as ListViewItem; |
var listViewItem = ActiveInputFilters.SelectedItem as ListViewItem; |
if (listViewItem == null) |
{ |
ActiveInputFilters.BackColor = Color.MistyRose; |
@@ -357,7 +352,7 @@ |
{ |
mainForm.BeginInvoke((MethodInvoker) (() => |
{ |
ListViewItem listViewItem = ENIGMARotorSequence.SelectedItem as ListViewItem; |
var listViewItem = ENIGMARotorSequence.SelectedItem as ListViewItem; |
if (listViewItem == null) |
{ |
ENIGMARotorSequence.BackColor = Color.MistyRose; |
@@ -390,7 +385,7 @@ |
{ |
mainForm.BeginInvoke((MethodInvoker) (() => |
{ |
ListViewItem listViewItem = ENIGMAPlugSequence.SelectedItem as ListViewItem; |
var listViewItem = ENIGMAPlugSequence.SelectedItem as ListViewItem; |
if (listViewItem == null) |
{ |
ENIGMAPlugSequence.BackColor = Color.MistyRose; |
@@ -415,10 +410,10 @@ |
// Spawn a thread to check Corrade's connection status. |
new Thread(() => |
{ |
TcpClient tcpClient = new TcpClient(); |
var tcpClient = new TcpClient(); |
try |
{ |
Uri uri = new Uri(Vassal.vassalConfiguration.HTTPServerURL); |
var uri = new Uri(Vassal.vassalConfiguration.HTTPServerURL); |
tcpClient.Connect(uri.Host, uri.Port); |
// port open |
Vassal.vassalForm.BeginInvoke((MethodInvoker) (() => { Vassal.vassalForm.Tabs.Enabled = true; })); |
@@ -425,8 +420,8 @@ |
// set the loading spinner |
if (Vassal.vassalForm.RegionAvatarsMap.Image == null) |
{ |
Assembly thisAssembly = Assembly.GetExecutingAssembly(); |
Stream file = |
var thisAssembly = Assembly.GetExecutingAssembly(); |
var file = |
thisAssembly.GetManifestResourceStream("Vassal.img.loading.gif"); |
switch (file != null) |
{ |
@@ -462,5 +457,18 @@ |
})); |
})); |
} |
|
private void ShowToolTip(object sender, EventArgs e) |
{ |
mainForm.BeginInvoke( |
(Action) (() => |
{ |
var pictureBox = sender as PictureBox; |
if (pictureBox != null) |
{ |
toolTip1.Show(toolTip1.GetToolTip(pictureBox), pictureBox); |
} |
})); |
} |
} |
} |