From f5d671d93a91894c7a85b1cfa3b9081a6d462b6f Mon Sep 17 00:00:00 2001 From: yankejustin Date: Sun, 24 May 2015 00:27:29 -0400 Subject: [PATCH 1/2] Removed some repetitive code --- Server/Forms/FrmBuilder.cs | 29 ++++++++++++++--------------- 1 file changed, 14 insertions(+), 15 deletions(-) diff --git a/Server/Forms/FrmBuilder.cs b/Server/Forms/FrmBuilder.cs index 4bbe31a2..b1179c91 100644 --- a/Server/Forms/FrmBuilder.cs +++ b/Server/Forms/FrmBuilder.cs @@ -24,6 +24,18 @@ namespace xServer.Forms _changed = true; } + private void UpdateControlStates() + { + txtInstallname.Enabled = chkInstall.Checked; + rbAppdata.Enabled = chkInstall.Checked; + rbProgramFiles.Enabled = chkInstall.Checked; + rbSystem.Enabled = chkInstall.Checked; + txtInstallsub.Enabled = chkInstall.Checked; + chkHide.Enabled = chkInstall.Checked; + chkStartup.Enabled = chkInstall.Checked; + txtRegistryKeyName.Enabled = (chkInstall.Checked && chkStartup.Checked); + } + private void LoadProfile(string profilename) { ProfileManager pm = new ProfileManager(profilename + ".xml"); @@ -95,13 +107,7 @@ namespace xServer.Forms txtMutex.Text = Helper.GetRandomName(32); } - txtInstallname.Enabled = chkInstall.Checked; - rbAppdata.Enabled = chkInstall.Checked; - rbProgramFiles.Enabled = chkInstall.Checked; - rbSystem.Enabled = chkInstall.Checked; - txtInstallsub.Enabled = chkInstall.Checked; - chkHide.Enabled = chkInstall.Checked; - chkStartup.Enabled = chkInstall.Checked; + UpdateControlStates(); txtRegistryKeyName.Enabled = (chkInstall.Checked && chkStartup.Checked); @@ -195,14 +201,7 @@ namespace xServer.Forms { HasChanged(); - txtInstallname.Enabled = chkInstall.Checked; - rbAppdata.Enabled = chkInstall.Checked; - rbProgramFiles.Enabled = chkInstall.Checked; - rbSystem.Enabled = chkInstall.Checked; - txtInstallsub.Enabled = chkInstall.Checked; - chkHide.Enabled = chkInstall.Checked; - chkStartup.Enabled = chkInstall.Checked; - txtRegistryKeyName.Enabled = (chkInstall.Checked && chkStartup.Checked); + UpdateControlStates(); } private void chkStartup_CheckedChanged(object sender, EventArgs e) From 94e50641cbba148a4a2c8e55280020b98f993484 Mon Sep 17 00:00:00 2001 From: yankejustin Date: Sun, 24 May 2015 00:38:58 -0400 Subject: [PATCH 2/2] Detecting illegal characters efficiently Instead of wasting lots of time creating a string from a char array, creating a string from another char array, concatenating them both into a new string, and casting the keychar to a string to see if the string contains it, make the operation natural and very efficient by creating a new char array that has both invalid characters and making sure the illegal character array does not contain the element. --- Server/Forms/FrmBuilder.cs | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/Server/Forms/FrmBuilder.cs b/Server/Forms/FrmBuilder.cs index b1179c91..e4105db0 100644 --- a/Server/Forms/FrmBuilder.cs +++ b/Server/Forms/FrmBuilder.cs @@ -1,5 +1,6 @@ using System; using System.IO; +using System.Linq; using System.Text.RegularExpressions; using System.Windows.Forms; using xServer.Core.Build; @@ -20,8 +21,7 @@ namespace xServer.Forms private void HasChanged() { - if (_loadedProfile && !_changed) - _changed = true; + _changed = (_loadedProfile && !_changed); } private void UpdateControlStates() @@ -137,22 +137,21 @@ namespace xServer.Forms private void txtDelay_KeyPress(object sender, KeyPressEventArgs e) { - if (!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar)) - e.Handled = true; + e.Handled = (!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar)); } private void txtInstallname_KeyPress(object sender, KeyPressEventArgs e) { - string illegal = new string(Path.GetInvalidPathChars()) + new string(Path.GetInvalidFileNameChars()); - if ((e.KeyChar == '\\' || illegal.Contains(e.KeyChar.ToString())) && !char.IsControl(e.KeyChar)) - e.Handled = true; + char[] illegal = Path.GetInvalidPathChars().Union(Path.GetInvalidFileNameChars()).ToArray(); + + e.Handled = ((e.KeyChar == '\\' || illegal.Any(illegalChar => (illegalChar == e.KeyChar))) && !char.IsControl(e.KeyChar)); } private void txtInstallsub_KeyPress(object sender, KeyPressEventArgs e) { - string illegal = new string(Path.GetInvalidPathChars()) + new string(Path.GetInvalidFileNameChars()); - if ((e.KeyChar == '\\' || illegal.Contains(e.KeyChar.ToString())) && !char.IsControl(e.KeyChar)) - e.Handled = true; + char[] illegal = Path.GetInvalidPathChars().Union(Path.GetInvalidFileNameChars()).ToArray(); + + e.Handled = ((e.KeyChar == '\\' || illegal.Any(illegalChar => (illegalChar == e.KeyChar))) && !char.IsControl(e.KeyChar)); } private void txtInstallname_TextChanged(object sender, EventArgs e)