Skip to content

Commit

Permalink
Added proper settings configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
MeTonaTOR committed Sep 6, 2017
1 parent f22a2ba commit 1ce42bf
Show file tree
Hide file tree
Showing 5 changed files with 402 additions and 35 deletions.
12 changes: 12 additions & 0 deletions InstantGameLauncher/InstantGameLauncher.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@
<PropertyGroup>
<ApplicationIcon>icon.ico</ApplicationIcon>
</PropertyGroup>
<PropertyGroup>
<StartupObject />
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Data" />
Expand All @@ -47,6 +50,12 @@
<Compile Include="IniFile.cs" />
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Settings.cs">
<SubType>Form</SubType>
</Compile>
<Compile Include="Settings.Designer.cs">
<DependentUpon>Settings.cs</DependentUpon>
</Compile>
<Compile Include="WebRequest.cs">
<SubType>Component</SubType>
</Compile>
Expand All @@ -70,6 +79,9 @@
<DependentUpon>Settings.settings</DependentUpon>
<DesignTimeSharedInput>True</DesignTimeSharedInput>
</Compile>
<EmbeddedResource Include="Settings.resx">
<DependentUpon>Settings.cs</DependentUpon>
</EmbeddedResource>
<EmbeddedResource Include="SoapBoxModules\udpcrc.soapbox.module" />
<EmbeddedResource Include="SoapBoxModules\udpcrypt1.soapbox.module" />
<EmbeddedResource Include="SoapBoxModules\udpcrypt2.soapbox.module" />
Expand Down
73 changes: 38 additions & 35 deletions InstantGameLauncher/Program.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Net;
Expand All @@ -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");
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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.");
}
}
}
Expand Down
156 changes: 156 additions & 0 deletions InstantGameLauncher/Settings.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

76 changes: 76 additions & 0 deletions InstantGameLauncher/Settings.cs
Original file line number Diff line number Diff line change
@@ -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<Object> items = new List<Object>();

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<Object> items2 = new List<Object>();

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();
}
}
}
Loading

0 comments on commit 1ce42bf

Please sign in to comment.