From b5b20e955239a901715414733094b2a87cdb3a9a Mon Sep 17 00:00:00 2001 From: pixeltris <6952411+pixeltris@users.noreply.github.com> Date: Tue, 12 May 2020 07:23:12 +0100 Subject: [PATCH] Allow the program to be ran from a different directory - Also fix potential issue with MacOS #8 --- GK6X/Program.cs | 24 +++++++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) diff --git a/GK6X/Program.cs b/GK6X/Program.cs index 1012a78..d6d8223 100644 --- a/GK6X/Program.cs +++ b/GK6X/Program.cs @@ -8,11 +8,16 @@ namespace GK6X { class Program { - public static readonly string DataBasePath = "Data"; - public static readonly string UserDataPath = "UserData"; + public static string BasePath; + public static string DataBasePath = "Data"; + public static string UserDataPath = "UserData"; static void Main(string[] args) { + BasePath = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location); + DataBasePath = Path.Combine(BasePath, DataBasePath); + UserDataPath = Path.Combine(BasePath, UserDataPath); + if (!Localization.Load()) { LogFatalError("Failed to load localization data"); @@ -68,9 +73,22 @@ static void Main(string[] args) KeyboardDeviceManager.StartListener(); bool running = true; + bool hasNullInput = false; while (running) { string line = Console.ReadLine(); + if (line == null) + { + // Handler for potential issue where ReadLine() returns null - see https://github.com/pixeltris/GK6X/issues/8 + if (hasNullInput) + { + Console.WriteLine("Cannot read from command line. Exiting."); + break; + } + hasNullInput = true; + continue; + } + hasNullInput = false; string[] splitted = line.Split(); switch (splitted[0].ToLower()) { @@ -297,7 +315,7 @@ internal static void Log(string str) { lock (logLocker) { - File.AppendAllText("KbLog.txt", "[" + DateTime.Now.TimeOfDay + "] " + str + Environment.NewLine); + File.AppendAllText(Path.Combine(BasePath, "KbLog.txt"), "[" + DateTime.Now.TimeOfDay + "] " + str + Environment.NewLine); Console.WriteLine(str); } }