2015-07-02 03:17:14 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
2015-12-28 03:05:45 +00:00
|
|
|
|
using System.Diagnostics;
|
2016-01-17 03:40:41 +00:00
|
|
|
|
using System.IO;
|
2015-07-02 03:17:14 +00:00
|
|
|
|
using System.Linq;
|
2016-01-17 16:11:53 +00:00
|
|
|
|
using System.Runtime.ExceptionServices;
|
2015-12-28 03:05:45 +00:00
|
|
|
|
using System.Runtime.InteropServices;
|
|
|
|
|
using System.Threading;
|
2015-07-02 03:17:14 +00:00
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using System.Windows.Forms;
|
2016-01-17 03:40:41 +00:00
|
|
|
|
using Mesen.GUI.Config;
|
2015-07-02 03:17:14 +00:00
|
|
|
|
|
|
|
|
|
namespace Mesen.GUI
|
|
|
|
|
{
|
|
|
|
|
static class Program
|
|
|
|
|
{
|
2015-12-28 03:05:45 +00:00
|
|
|
|
[DllImport("user32.dll")]
|
|
|
|
|
[return: MarshalAs(UnmanagedType.Bool)]
|
|
|
|
|
private static extern bool SetForegroundWindow(IntPtr hWnd);
|
|
|
|
|
|
2016-01-17 03:40:41 +00:00
|
|
|
|
private static void Application_ThreadException(object sender, ThreadExceptionEventArgs e)
|
|
|
|
|
{
|
|
|
|
|
MessageBox.Show(e.Exception.ToString(), "Unexpected Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|
|
|
|
}
|
2016-01-17 16:11:53 +00:00
|
|
|
|
|
|
|
|
|
private static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
|
|
|
|
|
{
|
|
|
|
|
MessageBox.Show(e.ExceptionObject.ToString(), "Unexpected Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|
|
|
|
}
|
2016-01-17 03:40:41 +00:00
|
|
|
|
|
2015-07-02 03:17:14 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// The main entry point for the application.
|
|
|
|
|
/// </summary>
|
|
|
|
|
[STAThread]
|
2016-01-17 16:11:53 +00:00
|
|
|
|
[HandleProcessCorruptedStateExceptions]
|
2016-01-17 03:40:41 +00:00
|
|
|
|
private static void Main(string[] args)
|
2015-07-02 03:17:14 +00:00
|
|
|
|
{
|
2016-01-17 16:11:53 +00:00
|
|
|
|
try {
|
|
|
|
|
Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);
|
|
|
|
|
Application.ThreadException += Application_ThreadException;
|
|
|
|
|
AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
|
2016-01-17 03:40:41 +00:00
|
|
|
|
|
2016-01-17 16:11:53 +00:00
|
|
|
|
Directory.CreateDirectory(ConfigManager.HomeFolder);
|
|
|
|
|
Directory.SetCurrentDirectory(ConfigManager.HomeFolder);
|
|
|
|
|
ResourceManager.ExtractResources();
|
2016-01-17 03:40:41 +00:00
|
|
|
|
|
2016-01-17 16:11:53 +00:00
|
|
|
|
if(!RuntimeChecker.TestDll()) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2016-01-17 00:33:10 +00:00
|
|
|
|
|
2016-01-17 16:11:53 +00:00
|
|
|
|
Guid guid = new Guid("{A46606B7-2D1C-4CC5-A52F-43BCAF094AED}");
|
|
|
|
|
using(SingleInstance singleInstance = new SingleInstance(guid)) {
|
|
|
|
|
if(singleInstance.FirstInstance || !Config.ConfigManager.Config.PreferenceInfo.SingleInstance) {
|
|
|
|
|
Application.EnableVisualStyles();
|
|
|
|
|
Application.SetCompatibleTextRenderingDefault(false);
|
2015-12-28 03:05:45 +00:00
|
|
|
|
|
2016-01-17 16:11:53 +00:00
|
|
|
|
Mesen.GUI.Forms.frmMain frmMain = new Mesen.GUI.Forms.frmMain(args);
|
2015-12-28 03:05:45 +00:00
|
|
|
|
|
2016-01-17 16:11:53 +00:00
|
|
|
|
if(Config.ConfigManager.Config.PreferenceInfo.SingleInstance) {
|
|
|
|
|
singleInstance.ListenForArgumentsFromSuccessiveInstances();
|
|
|
|
|
singleInstance.ArgumentsReceived += (object sender, ArgumentsReceivedEventArgs e) => {
|
|
|
|
|
frmMain.BeginInvoke((MethodInvoker)(() => {
|
|
|
|
|
frmMain.ProcessCommandLineArguments(e.Args);
|
|
|
|
|
}));
|
|
|
|
|
};
|
|
|
|
|
}
|
2015-12-28 03:05:45 +00:00
|
|
|
|
|
2016-01-17 16:11:53 +00:00
|
|
|
|
Application.Run(frmMain);
|
|
|
|
|
} else {
|
|
|
|
|
if(singleInstance.PassArgumentsToFirstInstance(args)) {
|
|
|
|
|
Process current = Process.GetCurrentProcess();
|
|
|
|
|
foreach(Process process in Process.GetProcessesByName(current.ProcessName)) {
|
|
|
|
|
if(process.Id != current.Id) {
|
|
|
|
|
Program.SetForegroundWindow(process.MainWindowHandle);
|
|
|
|
|
break;
|
|
|
|
|
}
|
2015-12-28 03:05:45 +00:00
|
|
|
|
}
|
2016-01-17 16:11:53 +00:00
|
|
|
|
} else {
|
|
|
|
|
Application.EnableVisualStyles();
|
|
|
|
|
Application.SetCompatibleTextRenderingDefault(false);
|
|
|
|
|
Application.Run(new Mesen.GUI.Forms.frmMain(args));
|
2015-12-28 03:05:45 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2016-01-17 16:11:53 +00:00
|
|
|
|
} catch(Exception e) {
|
|
|
|
|
MessageBox.Show(e.ToString(), "Unexpected Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
2015-12-28 03:05:45 +00:00
|
|
|
|
}
|
2015-07-02 03:17:14 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|