From 1ce42bf5ecce8086cd35f0b042c4843b3ad6233a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Milan=20Suwa=C5=82a?= Date: Wed, 6 Sep 2017 18:30:54 +0200 Subject: [PATCH] Added proper settings configuration --- .../InstantGameLauncher.csproj | 12 ++ InstantGameLauncher/Program.cs | 73 ++++---- InstantGameLauncher/Settings.Designer.cs | 156 ++++++++++++++++++ InstantGameLauncher/Settings.cs | 76 +++++++++ InstantGameLauncher/Settings.resx | 120 ++++++++++++++ 5 files changed, 402 insertions(+), 35 deletions(-) create mode 100644 InstantGameLauncher/Settings.Designer.cs create mode 100644 InstantGameLauncher/Settings.cs create mode 100644 InstantGameLauncher/Settings.resx diff --git a/InstantGameLauncher/InstantGameLauncher.csproj b/InstantGameLauncher/InstantGameLauncher.csproj index 21322bb..76ea3a3 100644 --- a/InstantGameLauncher/InstantGameLauncher.csproj +++ b/InstantGameLauncher/InstantGameLauncher.csproj @@ -34,6 +34,9 @@ icon.ico + + + @@ -47,6 +50,12 @@ + + Form + + + Settings.cs + Component @@ -70,6 +79,9 @@ Settings.settings True + + Settings.cs + diff --git a/InstantGameLauncher/Program.cs b/InstantGameLauncher/Program.cs index e1d6300..7f36ff5 100644 --- a/InstantGameLauncher/Program.cs +++ b/InstantGameLauncher/Program.cs @@ -1,5 +1,4 @@ using System; -using System.Collections.Generic; using System.Diagnostics; using System.IO; using System.Net; @@ -11,12 +10,12 @@ namespace InstantGameLauncher { static class Program { - [STAThread] static void Main() { String Username, Password, ServerAddress, serverLoginResponse, encryptedPassword, LoginToken, UserId, Executable; - String ConfigFile = Assembly.GetExecutingAssembly().GetName().Name + ".ini"; + String ConfigFile = AppDomain.CurrentDomain.FriendlyName.Replace(".exe", "") + ".ini"; IniFile Config = new IniFile(ConfigFile); + if (!File.Exists(Directory.GetCurrentDirectory() + "/lightfx.dll")) { File.WriteAllBytes(Directory.GetCurrentDirectory() + "/lightfx.dll", ExtractResource.AsByte("InstantGameLauncher.SoapBoxModules.lightfx.dll")); Directory.CreateDirectory(Directory.GetCurrentDirectory() + "/modules"); @@ -27,12 +26,13 @@ static void Main() { } if (!File.Exists(ConfigFile)) { - DialogResult InstallerAsk = MessageBox.Show(null, "There's no " + ConfigFile + " file. Do you wanna create and edit it?", "InstantGameLauncher", MessageBoxButtons.YesNo, MessageBoxIcon.Warning); + DialogResult InstallerAsk = MessageBox.Show(null, "There's no " + ConfigFile + " file. Do you wanna run Settings page?", "InstantGameLauncher", MessageBoxButtons.YesNo, MessageBoxIcon.Warning); if (InstallerAsk == DialogResult.Yes) { - Config.Write("ServerAddress", "", "Configuration"); - Config.Write("Username", "", "Configuration"); - Config.Write("Password", "", "Configuration"); - Process.Start(ConfigFile); + + //Let's show form + Application.EnableVisualStyles(); + Application.SetCompatibleTextRenderingDefault(false); + Application.Run(new Settings()); } Environment.Exit(Environment.ExitCode); @@ -58,8 +58,7 @@ static void Main() { encryptedPassword = sb.ToString(); serverLoginResponse = ""; - try - { + try { WebClient wc = new WebClientWithTimeout(); wc.Headers.Add("user-agent", "InstantGameLauncher (+https://github.com/metonator/InstantGameLauncher_NFSW)"); Server_Address = new Uri(ServerAddress); @@ -73,44 +72,48 @@ static void Main() { serverLoginResponse = sr.ReadToEnd(); } } else { - MessageBox.Show(null, "Failed to connect to the server. " + ex.Message, "InstantGameLauncher", MessageBoxButtons.OK, MessageBoxIcon.Error); + MessageBox.Show("Failed to connect to the server. " + ex.Message); Environment.Exit(Environment.ExitCode); } } else { - MessageBox.Show(null, "Failed to connect to the server. " + ex.Message, "InstantGameLauncher", MessageBoxButtons.OK, MessageBoxIcon.Error); + MessageBox.Show("Failed to connect to the server. " + ex.Message); Environment.Exit(Environment.ExitCode); } } - XmlDocument SBRW_XML = new XmlDocument(); - SBRW_XML.LoadXml(serverLoginResponse); + try { + XmlDocument SBRW_XML = new XmlDocument(); + SBRW_XML.LoadXml(serverLoginResponse); - XmlNode DescriptionNode, LoginTokenNode, UserIdNode; + XmlNode DescriptionNode, LoginTokenNode, UserIdNode; - DescriptionNode = SBRW_XML.SelectSingleNode("LoginStatusVO/Description"); - LoginTokenNode = SBRW_XML.SelectSingleNode("LoginStatusVO/LoginToken"); - UserIdNode = SBRW_XML.SelectSingleNode("LoginStatusVO/UserId"); + DescriptionNode = SBRW_XML.SelectSingleNode("LoginStatusVO/Description"); + LoginTokenNode = SBRW_XML.SelectSingleNode("LoginStatusVO/LoginToken"); + UserIdNode = SBRW_XML.SelectSingleNode("LoginStatusVO/UserId"); - if (String.IsNullOrEmpty(DescriptionNode.InnerText)) { - UserId = UserIdNode.InnerText; - LoginToken = LoginTokenNode.InnerText; + if (String.IsNullOrEmpty(DescriptionNode.InnerText)) { + UserId = UserIdNode.InnerText; + LoginToken = LoginTokenNode.InnerText; - if(Config.KeyExists("UseExecutable", "Configuration")) { - Executable = Config.Read("UseExecutable", "Configuration"); - } else { - Executable = "nfsw.exe"; - } + if(Config.KeyExists("UseExecutable", "Configuration")) { + Executable = Config.Read("UseExecutable", "Configuration"); + } else { + Executable = "nfsw.exe"; + } - if(!File.Exists(Executable)) { - MessageBox.Show(null, "Failed to launch " + Executable + ". File not found.", "InstantGameLauncher", MessageBoxButtons.OK, MessageBoxIcon.Error); - Environment.Exit(Environment.ExitCode); - } + if(!File.Exists(Executable)) { + MessageBox.Show("Failed to launch " + Executable + ". File not found."); + Environment.Exit(Environment.ExitCode); + } - string filename = Directory.GetCurrentDirectory() + "\\" + Executable; - String cParams = "US " + ServerAddress + " " + LoginToken + " " + UserId; - Process.Start(filename, cParams); - } else { - MessageBox.Show(null, "Invalid username or password.", "InstantGameLauncher", MessageBoxButtons.OK, MessageBoxIcon.Error); + string filename = Directory.GetCurrentDirectory() + "\\" + Executable; + String cParams = "US " + ServerAddress + " " + LoginToken + " " + UserId; + Process.Start(filename, cParams); + } else { + MessageBox.Show("Invalid username or password."); + } + } catch { + MessageBox.Show("Server is offline."); } } } diff --git a/InstantGameLauncher/Settings.Designer.cs b/InstantGameLauncher/Settings.Designer.cs new file mode 100644 index 0000000..1e9b81a --- /dev/null +++ b/InstantGameLauncher/Settings.Designer.cs @@ -0,0 +1,156 @@ +namespace InstantGameLauncher +{ + partial class Settings + { + /// + /// Required designer variable. + /// + private System.ComponentModel.IContainer components = null; + + /// + /// Clean up any resources being used. + /// + /// true if managed resources should be disposed; otherwise, false. + protected override void Dispose(bool disposing) + { + if (disposing && (components != null)) + { + components.Dispose(); + } + base.Dispose(disposing); + } + + #region Windows Form Designer generated code + + /// + /// Required method for Designer support - do not modify + /// the contents of this method with the code editor. + /// + private void InitializeComponent() + { + this.emailLabel = new System.Windows.Forms.Label(); + this.passwordLabel = new System.Windows.Forms.Label(); + this.serverLabel = new System.Windows.Forms.Label(); + this.executableLabel = new System.Windows.Forms.Label(); + this.usernameText = new System.Windows.Forms.TextBox(); + this.passwordText = new System.Windows.Forms.TextBox(); + this.serverText = new System.Windows.Forms.ComboBox(); + this.executableText = new System.Windows.Forms.ComboBox(); + this.saveButton = new System.Windows.Forms.Button(); + this.SuspendLayout(); + // + // emailLabel + // + this.emailLabel.AutoSize = true; + this.emailLabel.Location = new System.Drawing.Point(12, 15); + this.emailLabel.Name = "emailLabel"; + this.emailLabel.Size = new System.Drawing.Size(39, 13); + this.emailLabel.TabIndex = 0; + this.emailLabel.Text = "E-Mail:"; + // + // passwordLabel + // + this.passwordLabel.AutoSize = true; + this.passwordLabel.Location = new System.Drawing.Point(12, 41); + this.passwordLabel.Name = "passwordLabel"; + this.passwordLabel.Size = new System.Drawing.Size(56, 13); + this.passwordLabel.TabIndex = 1; + this.passwordLabel.Text = "Password:"; + // + // serverLabel + // + this.serverLabel.AutoSize = true; + this.serverLabel.Location = new System.Drawing.Point(12, 68); + this.serverLabel.Name = "serverLabel"; + this.serverLabel.Size = new System.Drawing.Size(41, 13); + this.serverLabel.TabIndex = 2; + this.serverLabel.Text = "Server:"; + // + // executableLabel + // + this.executableLabel.AutoSize = true; + this.executableLabel.Location = new System.Drawing.Point(12, 95); + this.executableLabel.Name = "executableLabel"; + this.executableLabel.Size = new System.Drawing.Size(117, 13); + this.executableLabel.TabIndex = 3; + this.executableLabel.Text = "Executable (nfsw.exe): "; + // + // usernameText + // + this.usernameText.Location = new System.Drawing.Point(155, 12); + this.usernameText.Name = "usernameText"; + this.usernameText.Size = new System.Drawing.Size(177, 20); + this.usernameText.TabIndex = 4; + // + // passwordText + // + this.passwordText.Location = new System.Drawing.Point(155, 38); + this.passwordText.Name = "passwordText"; + this.passwordText.Size = new System.Drawing.Size(177, 20); + this.passwordText.TabIndex = 5; + // + // serverText + // + this.serverText.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList; + this.serverText.FormattingEnabled = true; + this.serverText.Location = new System.Drawing.Point(155, 65); + this.serverText.Name = "serverText"; + this.serverText.Size = new System.Drawing.Size(177, 21); + this.serverText.TabIndex = 6; + // + // executableText + // + this.executableText.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList; + this.executableText.FormattingEnabled = true; + this.executableText.Location = new System.Drawing.Point(155, 92); + this.executableText.Name = "executableText"; + this.executableText.Size = new System.Drawing.Size(177, 21); + this.executableText.TabIndex = 7; + // + // saveButton + // + this.saveButton.Location = new System.Drawing.Point(221, 119); + this.saveButton.Name = "saveButton"; + this.saveButton.Size = new System.Drawing.Size(111, 28); + this.saveButton.TabIndex = 8; + this.saveButton.Text = "Save and Run"; + this.saveButton.UseVisualStyleBackColor = true; + this.saveButton.Click += new System.EventHandler(this.saveButton_Click); + // + // Settings + // + this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); + this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; + this.ClientSize = new System.Drawing.Size(351, 159); + this.Controls.Add(this.saveButton); + this.Controls.Add(this.executableText); + this.Controls.Add(this.serverText); + this.Controls.Add(this.passwordText); + this.Controls.Add(this.usernameText); + this.Controls.Add(this.executableLabel); + this.Controls.Add(this.serverLabel); + this.Controls.Add(this.passwordLabel); + this.Controls.Add(this.emailLabel); + this.MaximizeBox = false; + this.Name = "Settings"; + this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen; + this.Text = "Settings"; + this.Load += new System.EventHandler(this.Settings_Load); + this.ResumeLayout(false); + this.PerformLayout(); + + } + + #endregion + + private System.Windows.Forms.Label emailLabel; + private System.Windows.Forms.Label passwordLabel; + private System.Windows.Forms.Label serverLabel; + private System.Windows.Forms.Label executableLabel; + private System.Windows.Forms.TextBox usernameText; + private System.Windows.Forms.TextBox passwordText; + private System.Windows.Forms.ComboBox serverText; + private System.Windows.Forms.ComboBox executableText; + private System.Windows.Forms.Button saveButton; + } +} \ No newline at end of file diff --git a/InstantGameLauncher/Settings.cs b/InstantGameLauncher/Settings.cs new file mode 100644 index 0000000..ae5dbc8 --- /dev/null +++ b/InstantGameLauncher/Settings.cs @@ -0,0 +1,76 @@ +using System; +using System.Collections.Generic; +using System.ComponentModel; +using System.Data; +using System.Diagnostics; +using System.Drawing; +using System.IO; +using System.Net; +using System.Reflection; +using System.Text; +using System.Windows.Forms; + +namespace InstantGameLauncher { + public partial class Settings : Form { + IniFile Config = new IniFile(AppDomain.CurrentDomain.FriendlyName.Replace(".exe", "") + ".ini"); + + public Settings() { + InitializeComponent(); + } + + private void Settings_Load(object sender, EventArgs e) { + + //Serverlist + var response = ""; + try { + WebClient wc = new WebClientWithTimeout(); + wc.Headers.Add("user-agent", "GameLauncher (+https://github.com/metonator/GameLauncher_NFSW)"); + + string serverurl = "http://nfsw.metonator.ct8.pl/serverlist.txt"; + response = wc.DownloadString(serverurl); + } catch (Exception) { } + + serverText.DisplayMember = "Text"; + serverText.ValueMember = "Value"; + + List items = new List(); + + String[] substrings = response.Split(new string[] { "\r\n", "\n" }, StringSplitOptions.None); + foreach (var substring in substrings) { + if (!String.IsNullOrEmpty(substring)) { + String[] substrings22 = substring.Split(new string[] { ";" }, StringSplitOptions.None); + items.Add(new { Text = substrings22[0], Value = substrings22[1] }); + } + } + + serverText.DataSource = items; + serverText.SelectedIndex = 0; + + //Executable + var executables = Directory.GetFiles(".", "*.exe"); + executableText.DisplayMember = "Text"; + executableText.ValueMember = "Value"; + + List items2 = new List(); + + foreach (var substring2 in executables) { + var dummy = substring2.Replace(".\\", ""); + if (dummy != AppDomain.CurrentDomain.FriendlyName) { + items2.Add(dummy); + } + } + + executableText.DataSource = items2; + } + + private void saveButton_Click(object sender, EventArgs e) { + Config.Write("ServerAddress", serverText.SelectedValue.ToString(), "Configuration"); + Config.Write("Username", usernameText.Text, "Configuration"); + Config.Write("Password", passwordText.Text, "Configuration"); + Config.Write("UseExecutable", executableText.SelectedValue.ToString(), "Configuration"); + + Process.Start(AppDomain.CurrentDomain.FriendlyName); + Application.Exit(); + } + } +} diff --git a/InstantGameLauncher/Settings.resx b/InstantGameLauncher/Settings.resx new file mode 100644 index 0000000..7080a7d --- /dev/null +++ b/InstantGameLauncher/Settings.resx @@ -0,0 +1,120 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + text/microsoft-resx + + + 2.0 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + \ No newline at end of file