using System; using System.Diagnostics; using System.Threading; using System.Threading.Tasks; using System.Windows; using Launcher.Code; using Launcher.XAML; namespace Launcher { /// /// Interaction logic for App.xaml /// public partial class App { private static Mutex mutex = new Mutex(true, "OMGPatcherSingleInstanceChecker"); ~App() { if(ClientProcess != null) try { ClientProcess.Kill(); } catch (InvalidOperationException) { return; } } public bool Running() { if (mutex.WaitOne(TimeSpan.Zero, true)) { mutex.ReleaseMutex(); return false; } return true; } public TaskScheduler UIScheduler; public TaskFactory UIFactory; public Process ClientProcess; private void UnhandledException(object sender, UnhandledExceptionEventArgs e) { #if DEBUG MessageWindow.Show("error", e.ExceptionObject.ToString()); SaveShutdown(-2); #else SaveShutdown(-2); #endif } private void Application_Startup(object sender, StartupEventArgs e) { try { if (!BackgroundChecks.MacAllowed()) { MessageWindow msgWindow = new MessageWindow("Banned", "You are banned."); msgWindow.Show(); msgWindow.Closed += (o, args) => this.Shutdown(-1); msgWindow.Show(); } else { DataProvider.GetMaxVersion(); MainWindow mainWindow = new MainWindow(); mainWindow.Show(); } } catch (Exception ex) { Trace.WriteLine(ex.ToString()); #if DEBUG MessageWindow window = new MessageWindow("error", ex.ToString()); #else MessageWindow window = new MessageWindow("Unable to connect", "Unable to connect to the server. Please check your internet connection and try again"); #endif window.Closed += (o, args) => this.Shutdown(-1); Task t = new Task(() => window.ShowDialog()); t.Start(UIScheduler); } } public void SaveShutdown(int code) { Task t = new Task(() => this.Shutdown(code)); t.Start(UIScheduler); } public bool ClientRunning() { try { if(ClientProcess.Id != 0) { } return true; } catch { return false; } } protected override void OnExit(ExitEventArgs e) { DataProvider.Save(); base.OnExit(e); } protected override void OnStartup(StartupEventArgs e) { if (Running()) { if (!ClientRunning()) { NativeMethods.PostMessage((IntPtr) NativeMethods.HWND_BROADCAST, NativeMethods.WM_SHOWME, IntPtr.Zero, IntPtr.Zero); } Shutdown(-1); return; } AppDomain.CurrentDomain.ProcessExit += (s, args) => { try { this.ClientProcess.Kill(); } catch { return; } }; DataProvider.Initialize(); Trace.Listeners.Add(new FileTraceListener()); UIScheduler = TaskScheduler.FromCurrentSynchronizationContext(); UIFactory = new TaskFactory(UIScheduler); AppDomain.CurrentDomain.UnhandledException += UnhandledException; base.OnStartup(e); } } }