2019-02-12 22:13:09 -05:00
using System ;
using System.Collections.Generic ;
using System.Diagnostics ;
using System.IO ;
using System.Linq ;
2019-05-11 22:07:22 -04:00
using System.Net ;
2019-02-12 22:13:09 -05:00
using System.Reflection ;
using System.Runtime.ExceptionServices ;
using System.Runtime.InteropServices ;
using System.Threading ;
using System.Threading.Tasks ;
using System.Windows.Forms ;
using System.Xml.Serialization ;
using Mesen.GUI.Config ;
2019-03-30 21:47:30 -04:00
using Mesen.GUI.Debugger.Workspace ;
2019-02-12 22:13:09 -05:00
using Mesen.GUI.Forms ;
2019-03-15 12:24:02 -04:00
using Mesen.GUI.Utilities ;
2019-02-12 22:13:09 -05:00
namespace Mesen.GUI
{
static class Program
{
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool SetForegroundWindow ( IntPtr hWnd ) ;
public static bool IsMono { get ; private set ; }
public static string OriginalFolder { get ; private set ; }
private static void Application_ThreadException ( object sender , ThreadExceptionEventArgs e )
{
MesenMsgBox . Show ( "UnexpectedError" , MessageBoxButtons . OK , MessageBoxIcon . Error , e . Exception . ToString ( ) ) ;
}
private static void CurrentDomain_UnhandledException ( object sender , UnhandledExceptionEventArgs e )
{
MesenMsgBox . Show ( "UnexpectedError" , MessageBoxButtons . OK , MessageBoxIcon . Error , e . ExceptionObject . ToString ( ) ) ;
}
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
[HandleProcessCorruptedStateExceptions]
private static void Main ( string [ ] args )
{
try {
Task . Run ( ( ) = > {
//Cache deserializers in another thread
new XmlSerializer ( typeof ( Configuration ) ) ;
2019-03-30 21:47:30 -04:00
new XmlSerializer ( typeof ( DebugWorkspace ) ) ;
2019-10-12 22:40:25 -04:00
new XmlSerializer ( typeof ( CheatCodes ) ) ;
2019-10-14 00:37:10 -04:00
new XmlSerializer ( typeof ( CheatDatabase ) ) ;
2019-02-12 22:13:09 -05:00
} ) ;
if ( Type . GetType ( "Mono.Runtime" ) ! = null ) {
Program . IsMono = true ;
}
Program . OriginalFolder = Directory . GetCurrentDirectory ( ) ;
Application . SetUnhandledExceptionMode ( UnhandledExceptionMode . CatchException ) ;
Application . ThreadException + = Application_ThreadException ;
AppDomain . CurrentDomain . UnhandledException + = CurrentDomain_UnhandledException ;
2019-05-11 22:07:22 -04:00
//Enable TLS 1.0/1.1/1.2 support
ServicePointManager . SecurityProtocol = SecurityProtocolType . Tls | SecurityProtocolType . Tls11 | SecurityProtocolType . Tls12 ;
2019-02-12 22:13:09 -05:00
Application . EnableVisualStyles ( ) ;
Application . SetCompatibleTextRenderingDefault ( false ) ;
2019-03-15 16:19:42 -04:00
if ( ConfigManager . GetConfigFile ( ) = = null ) {
2019-02-12 22:13:09 -05:00
//Show config wizard
2019-04-01 07:12:07 -04:00
ResourceHelper . LoadResources ( Language . English ) ;
2019-02-12 22:13:09 -05:00
Application . Run ( new frmConfigWizard ( ) ) ;
if ( ConfigManager . GetConfigFile ( ) = = null ) {
return ;
}
2019-03-15 16:19:42 -04:00
}
2019-02-12 22:13:09 -05:00
Directory . CreateDirectory ( ConfigManager . HomeFolder ) ;
Directory . SetCurrentDirectory ( ConfigManager . HomeFolder ) ;
try {
if ( ! ResourceExtractor . ExtractResources ( ) ) {
return ;
}
} catch ( FileNotFoundException e ) {
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 ;
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 ;
case Language . Ukrainian : message = "Microsoft .NET Framework 4.5 не знайдений. Будь ласка завантажте і встановіть останню версію .NET Framework з сайту Microsoft і спробуйте знову." ; break ;
case Language . Portuguese : message = "Microsoft .NET Framework 4.5 não foi encontrado. Por favor, baixe a versão mais recente de .NET Framework do site da Microsoft e tente novamente." ; break ;
case Language . Chinese : message = "找不到 Microsoft .NET Framework 4.5,请访问 Microsoft 官网下载安装之后再试。" ; break ;
}
2019-03-15 16:19:42 -04:00
MessageBox . Show ( message + Environment . NewLine + Environment . NewLine + e . ToString ( ) , "Mesen-S" , MessageBoxButtons . OK , MessageBoxIcon . Error ) ;
2019-02-12 22:13:09 -05:00
return ;
} 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 ;
case Language . Ukrainian : message = "Невідома помилка.

Детально:
{0}" ; break ;
case Language . Portuguese : message = "Houve um erro inesperado.

Detalhes do erro:
{0}" ; break ;
case Language . Chinese : message = "发生意外错误。\n\n详情:\n{0}" ; break ;
}
2019-03-15 16:19:42 -04:00
MessageBox . Show ( string . Format ( message , e . ToString ( ) ) , "Mesen-S" , MessageBoxButtons . OK , MessageBoxIcon . Error ) ;
2019-02-12 22:13:09 -05:00
}
if ( ! RuntimeChecker . TestDll ( ) ) {
return ;
}
using ( SingleInstance singleInstance = new SingleInstance ( ) ) {
2019-03-11 17:56:54 -04:00
if ( singleInstance . FirstInstance | | ! ConfigManager . Config . Preferences . SingleInstance ) {
2019-02-12 22:13:09 -05:00
frmMain frmMain = new frmMain ( args ) ;
singleInstance . ListenForArgumentsFromSuccessiveInstances ( ) ;
singleInstance . ArgumentsReceived + = ( object sender , ArgumentsReceivedEventArgs e ) = > {
if ( frmMain . IsHandleCreated ) {
frmMain . BeginInvoke ( ( MethodInvoker ) ( ( ) = > {
2019-03-15 12:24:02 -04:00
new CommandLineHelper ( e . Args ) . LoadGameFromCommandLine ( ) ;
2019-02-12 22:13:09 -05:00
} ) ) ;
}
} ;
Application . Run ( frmMain ) ;
2019-03-11 17:56:54 -04:00
} else {
2019-02-12 22:13:09 -05:00
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 ;
}
}
} else {
Application . Run ( new frmMain ( args ) ) ;
}
2019-03-11 17:56:54 -04:00
}
2019-02-12 22:13:09 -05:00
}
} catch ( Exception e ) {
MesenMsgBox . Show ( "UnexpectedError" , MessageBoxButtons . OK , MessageBoxIcon . Error , e . ToString ( ) ) ;
}
}
}
}