MCGalaxy/GUI/Program.cs
2022-05-26 22:48:33 +10:00

94 lines
3.7 KiB
C#

/*
Copyright 2010 MCSharp team (Modified for use with MCZall/MCLawl/MCGalaxy)
Dual-licensed under the Educational Community License, Version 2.0 and
the GNU General Public License, Version 3 (the "Licenses"); you may
not use this file except in compliance with the Licenses. You may
obtain a copy of the Licenses at
http://www.opensource.org/licenses/ecl2.php
http://www.gnu.org/licenses/gpl-3.0.html
Unless required by applicable law or agreed to in writing,
software distributed under the Licenses are distributed on an "AS IS"
BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
or implied. See the Licenses for the specific language governing
permissions and limitations under the Licenses.
*/
using System;
using System.IO;
using System.Reflection;
using System.Threading;
using System.Windows.Forms;
namespace MCGalaxy.Gui
{
public static class Program
{
[STAThread]
public static void Main(string[] args) {
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() {
FileLogger.Init();
Server.RestartPath = Application.ExecutablePath;
AppDomain.CurrentDomain.UnhandledException += GlobalExHandler;
Application.ThreadException += ThreadExHandler;
try {
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault( false );
Application.Run(new Window());
} catch (Exception e) {
Logger.LogError(e);
}
}
static void LogAndRestart(Exception ex) {
Logger.LogError(ex);
FileLogger.Flush(null);
Thread.Sleep(500);
if (Server.Config.restartOnError) {
Thread stopThread = Server.Stop(true, "Server restart - unhandled error");
stopThread.Join();
}
}
static void GlobalExHandler(object sender, UnhandledExceptionEventArgs e) {
LogAndRestart((Exception)e.ExceptionObject);
}
static void ThreadExHandler(object sender, ThreadExceptionEventArgs e) {
LogAndRestart(e.Exception);
}
}
}