2015-07-01 23:17:14 -04:00
using System ;
using System.Collections.Generic ;
2015-12-27 22:05:45 -05:00
using System.Diagnostics ;
2016-01-16 22:40:41 -05:00
using System.IO ;
2015-07-01 23:17:14 -04:00
using System.Linq ;
2016-05-22 14:43:07 -04:00
using System.Reflection ;
2016-01-17 11:11:53 -05:00
using System.Runtime.ExceptionServices ;
2015-12-27 22:05:45 -05:00
using System.Runtime.InteropServices ;
using System.Threading ;
2015-07-01 23:17:14 -04:00
using System.Threading.Tasks ;
using System.Windows.Forms ;
2016-01-16 22:40:41 -05:00
using Mesen.GUI.Config ;
2016-02-19 13:05:04 -05:00
using Mesen.GUI.Forms ;
2015-07-01 23:17:14 -04:00
namespace Mesen.GUI
{
static class Program
{
2015-12-27 22:05:45 -05:00
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool SetForegroundWindow ( IntPtr hWnd ) ;
2016-12-11 10:56:23 -05:00
public static bool IsMono { get ; private set ; }
2016-01-16 22:40:41 -05:00
private static void Application_ThreadException ( object sender , ThreadExceptionEventArgs e )
{
2016-02-19 13:05:04 -05:00
MesenMsgBox . Show ( "UnexpectedError" , MessageBoxButtons . OK , MessageBoxIcon . Error , e . Exception . ToString ( ) ) ;
2016-01-16 22:40:41 -05:00
}
2016-09-05 12:12:57 -04:00
2016-01-17 11:11:53 -05:00
private static void CurrentDomain_UnhandledException ( object sender , UnhandledExceptionEventArgs e )
{
2016-02-19 13:05:04 -05:00
MesenMsgBox . Show ( "UnexpectedError" , MessageBoxButtons . OK , MessageBoxIcon . Error , e . ExceptionObject . ToString ( ) ) ;
2016-01-17 11:11:53 -05:00
}
2016-01-16 22:40:41 -05:00
2016-05-22 14:43:07 -04:00
[DebuggerNonUserCode]
private static Assembly LoadAssemblies ( object sender , ResolveEventArgs e )
{
//Allow assemblies to be loaded from subfolders in the home folder (used for Google Drive API dlls)
string assemblyFile = e . Name . Contains ( ',' ) ? e . Name . Substring ( 0 , e . Name . IndexOf ( ',' ) ) : e . Name ;
assemblyFile + = ".dll" ;
string absoluteFolder = new FileInfo ( ( new System . Uri ( Assembly . GetExecutingAssembly ( ) . CodeBase ) ) . LocalPath ) . Directory . FullName ;
string targetPath = Path . Combine ( ConfigManager . HomeFolder , "GoogleDrive" , assemblyFile ) ;
try {
if ( File . Exists ( targetPath ) ) {
return Assembly . LoadFile ( targetPath ) ;
}
} catch ( Exception ) {
return null ;
}
return null ;
}
2015-07-01 23:17:14 -04:00
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
2016-01-17 11:11:53 -05:00
[HandleProcessCorruptedStateExceptions]
2016-01-16 22:40:41 -05:00
private static void Main ( string [ ] args )
2015-07-01 23:17:14 -04:00
{
2016-01-17 11:11:53 -05:00
try {
2016-12-11 10:56:23 -05:00
if ( Type . GetType ( "Mono.Runtime" ) ! = null ) {
Program . IsMono = true ;
}
2016-05-22 14:43:07 -04:00
AppDomain . CurrentDomain . AssemblyResolve + = LoadAssemblies ;
2016-01-17 11:11:53 -05:00
Application . SetUnhandledExceptionMode ( UnhandledExceptionMode . CatchException ) ;
Application . ThreadException + = Application_ThreadException ;
AppDomain . CurrentDomain . UnhandledException + = CurrentDomain_UnhandledException ;
2016-01-16 22:40:41 -05:00
2016-01-17 11:11:53 -05:00
Directory . CreateDirectory ( ConfigManager . HomeFolder ) ;
Directory . SetCurrentDirectory ( ConfigManager . HomeFolder ) ;
2016-05-02 20:22:17 -04:00
try {
2016-07-24 13:38:24 -04:00
ResourceHelper . LoadResources ( ConfigManager . Config . PreferenceInfo . DisplayLanguage ) ;
2016-12-18 12:43:20 -05:00
if ( ! ResourceManager . ExtractResources ( ) ) {
return ;
}
2016-07-24 13:38:24 -04:00
} catch ( FileNotFoundException ) {
string message = "The Microsoft .NET Framework 4.5 could not be found. Please download and install the latest version of the .NET Framework from Microsoft's website and try again." ;
switch ( ResourceHelper . GetCurrentLanguage ( ) ) {
case Language . French : message = "Le .NET Framework 4.5 de Microsoft n'a pas été trouvé. Veuillez télécharger la plus récente version du .NET Framework à partir du site de Microsoft et essayer à nouveau." ; break ;
case Language . Japanese : message = "Microsoft .NET Framework 4.5はインストールされていないため、Mesenは起動できません。Microsoft .NET Frameworkの最新版をMicrosoftのサイトからダウンロードして、インストールしてください。" ; break ;
case Language . Russian : message = "Microsoft .NET Framework 4.5 не найден. Пожалуйста загрузите и установите последнюю версию .NET Framework с сайта Microsoft и попробуйте снова." ; break ;
2016-09-05 12:12:57 -04:00
case Language . Spanish : message = "Microsoft .NET Framework 4.5 no se ha encontrado. Por favor, descargue la versión más reciente de .NET Framework desde el sitio de Microsoft y vuelva a intentarlo." ; break ;
2016-09-02 20:10:24 -04:00
case Language . Ukrainian : message = "Microsoft .NET Framework 4.5 не знайдений. Будь ласка завантажте і встановіть останню версію .NET Framework з сайту Microsoft і спробуйте знову." ; break ;
2016-07-24 13:38:24 -04:00
}
MessageBox . Show ( message , "Mesen" , MessageBoxButtons . OK , MessageBoxIcon . Error ) ;
2016-05-02 20:22:17 -04:00
return ;
2016-07-24 13:38:24 -04:00
} catch ( Exception e ) {
string message = "An unexpected error has occurred.\n\nError details:\n{0}" ;
switch ( ResourceHelper . GetCurrentLanguage ( ) ) {
case Language . French : message = "Une erreur inattendue s'est produite.\n\nDétails de l'erreur :\n{0}" ; break ;
case Language . Japanese : message = "予期しないエラーが発生しました。\n\nエラーの詳細:\n{0}" ; break ;
case Language . Russian : message = "Неизвестная ошибка.

Подробно:
{0}" ; break ;
case Language . Spanish : message = "Se ha producido un error inesperado.

Detalles del error:
{0}" ; break ;
2016-09-02 20:10:24 -04:00
case Language . Ukrainian : message = "Невідома помилка.

Детально:
{0}" ; break ;
2016-07-24 13:38:24 -04:00
}
MessageBox . Show ( string . Format ( message , e . ToString ( ) ) , "Mesen" , MessageBoxButtons . OK , MessageBoxIcon . Error ) ;
2016-05-02 20:22:17 -04:00
}
2016-01-16 22:40:41 -05:00
2016-01-17 11:11:53 -05:00
if ( ! RuntimeChecker . TestDll ( ) ) {
return ;
}
2016-01-16 19:33:10 -05:00
2016-05-02 20:22:17 -04:00
ResourceHelper . UpdateEmuLanguage ( ) ;
2016-12-13 20:31:44 -05:00
using ( SingleInstance singleInstance = new SingleInstance ( ) ) {
2016-01-17 11:11:53 -05:00
if ( singleInstance . FirstInstance | | ! Config . ConfigManager . Config . PreferenceInfo . SingleInstance ) {
Application . EnableVisualStyles ( ) ;
Application . SetCompatibleTextRenderingDefault ( false ) ;
2015-12-27 22:05:45 -05:00
2016-01-17 11:11:53 -05:00
Mesen . GUI . Forms . frmMain frmMain = new Mesen . GUI . Forms . frmMain ( args ) ;
2015-12-27 22:05:45 -05:00
2016-01-17 11:11:53 -05:00
if ( Config . ConfigManager . Config . PreferenceInfo . SingleInstance ) {
singleInstance . ListenForArgumentsFromSuccessiveInstances ( ) ;
singleInstance . ArgumentsReceived + = ( object sender , ArgumentsReceivedEventArgs e ) = > {
frmMain . BeginInvoke ( ( MethodInvoker ) ( ( ) = > {
2016-07-29 19:05:15 -04:00
frmMain . ProcessCommandLineArguments ( e . Args , false ) ;
2016-01-17 11:11:53 -05:00
} ) ) ;
} ;
}
2015-12-27 22:05:45 -05:00
2016-01-17 11:11:53 -05: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-27 22:05:45 -05:00
}
2016-01-17 11:11:53 -05:00
} else {
Application . EnableVisualStyles ( ) ;
Application . SetCompatibleTextRenderingDefault ( false ) ;
Application . Run ( new Mesen . GUI . Forms . frmMain ( args ) ) ;
2015-12-27 22:05:45 -05:00
}
}
}
2016-01-17 11:11:53 -05:00
} catch ( Exception e ) {
2016-02-19 13:05:04 -05:00
MesenMsgBox . Show ( "UnexpectedError" , MessageBoxButtons . OK , MessageBoxIcon . Error , e . ToString ( ) ) ;
2015-12-27 22:05:45 -05:00
}
2015-07-01 23:17:14 -04:00
}
}
}