Overall Optimization #6

Merged
caskd merged 5 commits from dev into release 2018-02-11 13:37:00 +00:00
1 changed files with 144 additions and 143 deletions

View File

@ -12,15 +12,64 @@ namespace UnturnedSL
{ {
static void Main(string[] args) static void Main(string[] args)
{ {
string lauversion = "1.12 rewrite"; string lauversion = "1.13 rewrite";
string title = "USL by casKd running on version " + lauversion; string title = "USL by casKd running on version " + lauversion;
Console.Title = title; Console.Title = title;
Console.SetWindowSize(100,20); Console.SetWindowSize(100,20);
/*Console.WriteLine("Currently running from:" + Environment.NewLine + Directory.GetCurrentDirectory() + Environment.NewLine +
"Config exists: " + File.Exists("settings.cfg") + Environment.NewLine); DEBUGGING PURPOSES ONLY*/
if (!File.Exists("settings.cfg")) { /*Checks for new users*/ if (!File.Exists("settings.cfg")) { /*Checks for new users*/
FirstSetup();
TextReader settings = new StreamReader("settings.cfg", true);
string name = settings.ReadLine();
string map = settings.ReadLine();
string welcome = settings.ReadLine();
string port = settings.ReadLine();
string data = settings.ReadLine();
string extralo = settings.ReadLine();
string path = settings.ReadLine();
settings.Close(); /*Loads settings from file and checks if all lines are present and valid*/
Console.ForegroundColor = ConsoleColor.Green; Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine("Welcome!", Environment.NewLine); string launchop = "-nographics -batchmode -name " + "\"" + name + "\"" + " -map " + map + " -welcome " + "\"" + welcome + "\"" + " -port:" + port + " " + extralo + " +secureserver/" + data;
Validation(name, map, welcome, port, data, extralo, path, out bool valid);
Run(valid, name, map, welcome, data, port, extralo, path);
} else {
TextReader settings = new StreamReader("settings.cfg", true);
string name = settings.ReadLine();
string map = settings.ReadLine();
string welcome = settings.ReadLine();
string port = settings.ReadLine();
string data = settings.ReadLine();
string extralo = settings.ReadLine();
string path = settings.ReadLine();
settings.Close(); /*Loads settings from file and checks if all lines are present and valid*/
Validation(name, map, welcome, port, data, extralo, path, out bool valid);
Run(valid, name, map, welcome, port, data, extralo, path);
}
}
/*Validate if all input is correct*/
static void Validation(string name, string map, string welcome, string port, string data, string extralo, string path,out bool valid)
{
/*Checks if every condition is true*/
bool bNum = int.TryParse(port, out int i);
if (
bNum != true ||
String.IsNullOrWhiteSpace(name) ||
String.IsNullOrWhiteSpace(map) ||
String.IsNullOrWhiteSpace(welcome) ||
String.IsNullOrWhiteSpace(port) ||
String.IsNullOrWhiteSpace(data) ||
String.IsNullOrWhiteSpace(extralo) ||
String.IsNullOrWhiteSpace(path) ||
!File.Exists(path + @"\Unturned.exe")
)
{ valid = true;} else { valid = false;}
}
/*First setup*/
static void FirstSetup()
{
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine("Welcome!" + Environment.NewLine);
string[] defvalue = { string[] defvalue = {
"PEI", "PEI",
"USL-My server", "USL-My server",
@ -40,7 +89,6 @@ namespace UnturnedSL
}; };
/*Asks user to setup their own server, which later saves in a config file*/ /*Asks user to setup their own server, which later saves in a config file*/
Console.ForegroundColor = ConsoleColor.Yellow; Console.ForegroundColor = ConsoleColor.Yellow;
string[] setvals = { "map", "name", "welcome", "port", "data", "extralo", "path" }; string[] setvals = { "map", "name", "welcome", "port", "data", "extralo", "path" };
var loopval = 0; var loopval = 0;
string[] answ = new string[7]; string[] answ = new string[7];
@ -54,10 +102,12 @@ namespace UnturnedSL
{ {
answ[loopval] = defvalue[loopval]; answ[loopval] = defvalue[loopval];
loopval++; loopval++;
} else }
else
{ {
loopval++; loopval++;
} }
Console.Clear();
} }
} }
/*Saves data*/ /*Saves data*/
@ -68,7 +118,6 @@ namespace UnturnedSL
string data = answ[4]; string data = answ[4];
string extralo = answ[5]; string extralo = answ[5];
string path = answ[6]; string path = answ[6];
Console.Clear(); Console.Clear();
/*Stores data into a file*/ /*Stores data into a file*/
TextWriter settings = new StreamWriter("settings.cfg", true); TextWriter settings = new StreamWriter("settings.cfg", true);
@ -80,102 +129,54 @@ namespace UnturnedSL
settings.WriteLine(extralo); settings.WriteLine(extralo);
settings.WriteLine(path); settings.WriteLine(path);
settings.Close(); settings.Close();
Console.ForegroundColor = ConsoleColor.Green; }
string launchop = "-nographics -batchmode -name " + "\"" + name + "\"" + " -map " + map + " -welcome " + "\"" + welcome + "\"" + " -port:" + port + " " + extralo + " +secureserver/" + data; static void DisplayText(string name, string map, string welcome, string port, string data, string extralo, string path)
string curdirargs = path + @"\Unturned.exe " + launchop; /*Outputs info of the current configuration*/
if ( {
String.IsNullOrWhiteSpace(name) || string displaytext =
String.IsNullOrWhiteSpace(map) || "Server info:" + Environment.NewLine + Environment.NewLine +
String.IsNullOrWhiteSpace(welcome) || "Server Name:" + "\t" + "\t" + name + Environment.NewLine +
String.IsNullOrWhiteSpace(port) || "Running Map:" + "\t" + "\t" + map + Environment.NewLine +
String.IsNullOrWhiteSpace(data) || "Welcome Msg:" + "\t" + "\t" + welcome + Environment.NewLine +
String.IsNullOrWhiteSpace(extralo) || "Data folder:" + "\t" + "\t" + data + Environment.NewLine +
String.IsNullOrWhiteSpace(path) || "Running port:" + "\t" + "\t" + port + Environment.NewLine +
!File.Exists(path + @"\Unturned.exe") "Extra options:" + "\t" + "\t" + extralo + Environment.NewLine +
) "Path to Game:" + "\t" + "\t" + path + Environment.NewLine;
Console.WriteLine(displaytext);
}
static void DebugInfo()
/*Helps at debugging, mostly needed when having problems*/
{
string debugtext =
"Currently running from:" + Environment.NewLine +
Directory.GetCurrentDirectory() + Environment.NewLine +
"Config exists: " + File.Exists("settings.cfg") + Environment.NewLine;
Console.WriteLine(debugtext);
}
static void Run(bool valid, string name, string map, string welcome, string port, string data, string extralo, string path)
{
if (valid == true)
/*If conditions have problems, output errors and variables, else, start the server*/
{ {
/*Shows an error with information to fix it*/
Console.ForegroundColor = ConsoleColor.Red; Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("Oh, noes! Seems like the settings file contains wrong info." + Environment.NewLine + Console.WriteLine("Oh, noes! Seems like the settings file contains wrong info." + Environment.NewLine +
"Try deleting it or correcting it!" + Environment.NewLine + Environment.NewLine + "Try deleting it or correcting it!" + Environment.NewLine + Environment.NewLine +
"If you still get the problem, report it on GitHub!"); "If you still get the problem, report it on GitHub!");
Console.WriteLine(Environment.NewLine + "Your settings:" + DisplayText(name, map, welcome, data, port, extralo, path);
Environment.NewLine + Environment.NewLine + "Server Name:" + "\t" + "\t" + name + Environment.NewLine +
"Running Map:" + "\t" + "\t" + map + Environment.NewLine +
"Welcome Msg:" + "\t" + "\t" + welcome + Environment.NewLine +
"Data folder:" + "\t" + "\t" + data + Environment.NewLine +
"Extra options:" + "\t" + "\t" + extralo + Environment.NewLine +
"Path to Game:" + "\t" + "\t" + path + Environment.NewLine);
Console.Beep(2300, 250); Console.Beep(2300, 250);
Console.ReadKey(); Console.ReadKey();
Environment.Exit(1); Environment.Exit(1);
} }
string displaytext = else
"Server info:" + Environment.NewLine + Environment.NewLine +
"Server Name:" + "\t" + "\t" + name + Environment.NewLine +
"Running Map:" + "\t" + "\t" + map + Environment.NewLine +
"Welcome Msg:" + "\t" + "\t" + welcome + Environment.NewLine +
"Data folder:" + "\t" + "\t" + data + Environment.NewLine +
"Extra options:" + "\t" + "\t" + extralo + Environment.NewLine +
"Path to Game:" + "\t" + "\t" + path + Environment.NewLine;
Console.WriteLine(displaytext);
try { Process.Start(path + @"\Unturned.exe" + launchop);} catch { }
Console.ReadKey();
} else {
TextReader settings = new StreamReader("settings.cfg", true);
string name = settings.ReadLine();
string map = settings.ReadLine();
string welcome = settings.ReadLine();
string port = settings.ReadLine();
string data = settings.ReadLine();
string extralo = settings.ReadLine();
string path = settings.ReadLine();
settings.Close(); /*Loads settings from file and checks if all lines are present and valid*/
if (
String.IsNullOrWhiteSpace(name) ||
String.IsNullOrWhiteSpace(map) ||
String.IsNullOrWhiteSpace(welcome) ||
String.IsNullOrWhiteSpace(port) ||
String.IsNullOrWhiteSpace(data) ||
String.IsNullOrWhiteSpace(extralo) ||
String.IsNullOrWhiteSpace(path) ||
!File.Exists(path + @"\Unturned.exe")
)
{ {
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("Oh, noes! Seems like the settings file contains wrong info." + Environment.NewLine +
"Try deleting it or correcting it!" + Environment.NewLine + Environment.NewLine +
"If you still get the problem, report it on GitHub!" );
Console.WriteLine(Environment.NewLine + "Your settings:" +
Environment.NewLine + Environment.NewLine + "Server Name:" + "\t" + "\t" + name + Environment.NewLine +
"Running Map:" + "\t" + "\t" + map + Environment.NewLine +
"Welcome Msg:" + "\t" + "\t" + welcome + Environment.NewLine +
"Data folder:" + "\t" + "\t" + data + Environment.NewLine +
"Extra options:" + "\t" + "\t" + extralo + Environment.NewLine +
"Path to Game:" + "\t" + "\t" + path + Environment.NewLine);
Console.Beep(2300, 250);
Console.ReadKey();
Environment.Exit(1);
} else {
Console.ForegroundColor = ConsoleColor.Green; Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine("Welcome back!" + Environment.NewLine); Console.WriteLine("Welcome!" + Environment.NewLine);
string displaytext = DisplayText(name, map, welcome, data, port, extralo, path);
"Server info:" + Environment.NewLine + Environment.NewLine +
"Server Name:" + "\t" + "\t" + name + Environment.NewLine +
"Running Map:" + "\t" + "\t" + map + Environment.NewLine +
"Welcome Msg:" + "\t" + "\t" + welcome + Environment.NewLine +
"Data folder:" + "\t" + "\t" + data + Environment.NewLine +
"Extra options:" + "\t" + "\t" + extralo + Environment.NewLine +
"Path to Game:" + "\t" + "\t" + path + Environment.NewLine;
Console.WriteLine(displaytext);
string launchop = "-nographics -batchmode -name " + "\"" + name + "\"" + " -map " + map + " -welcome " + "\"" + welcome + "\"" + " -port:" + port + " " + extralo + " +secureserver/" + data; string launchop = "-nographics -batchmode -name " + "\"" + name + "\"" + " -map " + map + " -welcome " + "\"" + welcome + "\"" + " -port:" + port + " " + extralo + " +secureserver/" + data;
try { Process.Start(path + @"\Unturned.exe ", launchop);} catch { } Process.Start(path + @"\Unturned.exe ", launchop);
Console.ReadKey(); Console.ReadKey();
Environment.Exit(0); Environment.Exit(0);
} }
} }
} }
}
} }