From 10a91128d750bc2c798940d7843b1ffb28b64257 Mon Sep 17 00:00:00 2001 From: UnknownShadow200 Date: Thu, 26 May 2022 22:48:33 +1000 Subject: [PATCH] Make GUI work with mkbundle --- GUI/Program.cs | 35 ++++++++++++++++++++++++++--------- 1 file changed, 26 insertions(+), 9 deletions(-) diff --git a/GUI/Program.cs b/GUI/Program.cs index 5fbc3f7ec..208966a7f 100644 --- a/GUI/Program.cs +++ b/GUI/Program.cs @@ -27,15 +27,32 @@ namespace MCGalaxy.Gui { [STAThread] public static void Main(string[] args) { - Environment.CurrentDirectory = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location); - - if (!File.Exists("MCGalaxy_.dll")) { - Popup.Error("Cannot start server as MCGalaxy_.dll is missing from " + Environment.CurrentDirectory - + "\r\nDownload it from " + Updater.UploadsURL); - return; - } - // separate method, in case MCGalaxy_.dll is missing - StartGUI(); + SetCurrentDirectory(); + + // separate method, in case MCGalaxy_.dll is missing + try { + StartGUI(); + } catch (FileNotFoundException) { + // If MCGalaxy_.dll is missing, a FileNotFoundException will get thrown for MCGalaxy dll + Popup.Error("Cannot start server as MCGalaxy_.dll is missing from " + Environment.CurrentDirectory + + "\n\nDownload it from " + Updater.UploadsURL); + return; + } + } + + static void SetCurrentDirectory() { + string path = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location); + try { + Environment.CurrentDirectory = path; + } catch (Exception ex) { + // assembly.Location usually gives full path of the .exe, but has issues with mkbundle + // https://mono-devel-list.ximian.narkive.com/KfCAxY1F/mkbundle-assembly-getentryassembly + // https://stackoverflow.com/questions/57648241/reliably-get-location-of-bundled-executable-on-linux + // Rather than trying to guess when this issue happens, just don't bother at all + // (since most users will not be trying to run .exe from a different folder anyways) + string msg = "Failed to set working directory to '{0}', running in current directory..\n\n{1}"; + Popup.Warning(string.Format(msg, path, ex)); + } } static void StartGUI() {