2011-01-11 02:55:51 +00:00
using System ;
2012-10-29 08:37:22 +00:00
using System.IO ;
using System.Reflection ;
using System.Runtime.InteropServices ;
2011-01-11 02:55:51 +00:00
using System.Windows.Forms ;
2012-03-12 04:44:34 +00:00
#if WINDOWS
2011-01-11 02:55:51 +00:00
using SlimDX.Direct3D9 ;
using SlimDX.DirectSound ;
2011-05-23 00:33:05 +00:00
using Microsoft.VisualBasic.ApplicationServices ;
2012-03-12 04:44:34 +00:00
#endif
2011-01-11 02:55:51 +00:00
namespace BizHawk.MultiClient
{
2011-06-19 23:39:25 +00:00
static class Program
{
[STAThread]
2012-09-04 00:20:36 +00:00
unsafe static void Main ( string [ ] args )
2011-06-19 23:39:25 +00:00
{
2012-10-29 08:37:22 +00:00
#if WINDOWS
// this will look in subdirectory "dll" to load pinvoked stuff
SetDllDirectory ( System . IO . Path . Combine ( PathManager . GetExeDirectoryAbsolute ( ) , "dll" ) ) ;
2012-09-04 00:20:36 +00:00
2012-10-29 08:37:22 +00:00
//in case assembly resolution fails, such as if we moved them into the dll subdiretory, this event handler can reroute to them
AppDomain . CurrentDomain . AssemblyResolve + = new ResolveEventHandler ( CurrentDomain_AssemblyResolve ) ;
#endif
2012-09-04 00:20:36 +00:00
2012-10-29 08:37:22 +00:00
SubMain ( args ) ;
}
2012-09-04 00:20:36 +00:00
2012-10-29 08:37:22 +00:00
static void SubMain ( string [ ] args )
{
2012-09-04 00:20:36 +00:00
2011-06-19 23:39:25 +00:00
Application . EnableVisualStyles ( ) ;
Application . SetCompatibleTextRenderingDefault ( false ) ;
2011-01-11 02:55:51 +00:00
2012-12-17 15:38:56 +00:00
// catch morons who mix versions
2012-12-25 20:36:04 +00:00
// zero 25-dec-2012 - only do for public builds. its annoying during development
if ( ! MainForm . INTERIM )
2012-12-17 15:38:56 +00:00
{
var thisversion = typeof ( Program ) . Assembly . GetName ( ) . Version ;
var utilversion = typeof ( InputValidate ) . Assembly . GetName ( ) . Version ;
var emulversion = typeof ( Log ) . Assembly . GetName ( ) . Version ;
if ( thisversion ! = utilversion | | thisversion ! = emulversion )
{
MessageBox . Show ( "Conflicting revisions found! Don't mix .dll versions!" ) ;
return ;
}
}
2012-03-24 17:47:52 +00:00
Global . Config = ConfigService . Load < Config > ( PathManager . DefaultIniPath , new Config ( ) ) ;
2011-03-21 00:54:30 +00:00
2012-10-10 02:43:33 +00:00
#if WINDOWS
2011-06-19 23:39:25 +00:00
try { Global . DSound = new DirectSound ( ) ; }
catch
{
2012-03-02 03:39:09 +00:00
MessageBox . Show ( "Couldn't initialize DirectSound! Things may go poorly for you. Try changing your sound driver to 41khz instead of 48khz in mmsys.cpl." , "Initialization Error" , MessageBoxButtons . OK , MessageBoxIcon . Error ) ;
2011-06-19 23:39:25 +00:00
}
2011-01-11 02:55:51 +00:00
2011-03-21 00:54:30 +00:00
try { Global . Direct3D = new Direct3D ( ) ; }
catch
{
2011-08-21 01:07:58 +00:00
//fallback to GDI rendering
if ( ! Global . Config . DisplayGDI )
DisplayDirect3DError ( ) ;
2011-03-21 00:54:30 +00:00
}
2012-03-12 04:44:34 +00:00
#endif
2011-01-11 02:55:51 +00:00
2011-06-19 23:39:25 +00:00
try
{
2012-03-12 04:44:34 +00:00
#if WINDOWS
2011-06-19 23:39:25 +00:00
if ( Global . Config . SingleInstanceMode )
{
SingleInstanceController controller = new SingleInstanceController ( args ) ;
controller . Run ( args ) ;
}
else
{
2012-03-12 04:44:34 +00:00
#endif
2012-04-16 08:18:41 +00:00
using ( var mf = new MainForm ( args ) )
{
var title = mf . Text ;
mf . Show ( ) ;
mf . Text = title ;
mf . ProgramRunLoop ( ) ;
}
2012-03-12 04:44:34 +00:00
#if WINDOWS
2011-06-19 23:39:25 +00:00
}
2012-12-17 15:38:56 +00:00
#endif
2011-06-19 23:39:25 +00:00
}
catch ( Exception e )
{
2012-04-16 08:18:41 +00:00
MessageBox . Show ( e . ToString ( ) ) ;
2011-06-19 23:39:25 +00:00
}
2012-03-12 04:44:34 +00:00
#if WINDOWS
2011-06-19 23:39:25 +00:00
finally
{
if ( Global . DSound ! = null & & Global . DSound . Disposed = = false )
Global . DSound . Dispose ( ) ;
if ( Global . Direct3D ! = null & & Global . Direct3D . Disposed = = false )
Global . Direct3D . Dispose ( ) ;
2012-11-09 19:21:33 +00:00
GamePad . CloseAll ( ) ;
2011-06-19 23:39:25 +00:00
}
2012-03-12 04:44:34 +00:00
#endif
2011-06-19 23:39:25 +00:00
}
2011-05-23 00:33:05 +00:00
2012-10-29 08:37:22 +00:00
//declared here instead of a more usual place to avoid dependencies on the more usual place
#if WINDOWS
[DllImport("kernel32.dll", SetLastError = true)]
static extern bool SetDllDirectory ( string lpPathName ) ;
#endif
static Assembly CurrentDomain_AssemblyResolve ( object sender , ResolveEventArgs args )
{
//load missing assemblies by trying to find them in the dll directory
string dllname = new AssemblyName ( args . Name ) . Name + ".dll" ;
string directory = System . IO . Path . Combine ( PathManager . GetExeDirectoryAbsolute ( ) , "dll" ) ;
string fname = Path . Combine ( directory , dllname ) ;
if ( ! File . Exists ( fname ) ) return null ;
//it is important that we use LoadFile here and not load from a byte array; otherwise mixed (managed/unamanged) assemblies can't load
return Assembly . LoadFile ( fname ) ;
}
2012-03-12 04:44:34 +00:00
#if WINDOWS
2011-06-19 23:39:25 +00:00
public class SingleInstanceController : WindowsFormsApplicationBase
{
MainForm mf ;
string [ ] cmdArgs ;
public SingleInstanceController ( string [ ] args )
{
cmdArgs = args ;
IsSingleInstance = true ;
StartupNextInstance + = this_StartupNextInstance ;
}
void this_StartupNextInstance ( object sender , StartupNextInstanceEventArgs e )
{
mf . LoadRom ( e . CommandLine [ 0 ] ) ;
}
2011-05-23 00:33:05 +00:00
2011-06-19 23:39:25 +00:00
protected override void OnCreateMainForm ( )
{
MainForm = new RamWatch ( ) ;
2011-05-23 00:33:05 +00:00
2011-06-19 23:39:25 +00:00
mf = new MainForm ( cmdArgs ) ;
MainForm = mf ;
mf . Show ( ) ;
mf . ProgramRunLoop ( ) ;
}
}
2011-08-21 01:07:58 +00:00
public static void DisplayDirect3DError ( )
{
MessageBox . Show ( "Failure to initialize Direct3D, reverting to GDI+ display method. Change the option in Config > GUI or install DirectX web update." , "Initialization Error" , MessageBoxButtons . OK , MessageBoxIcon . Error ) ;
}
2012-03-12 04:44:34 +00:00
#endif
2011-06-19 23:39:25 +00:00
}
2011-01-11 02:55:51 +00:00
}