static void Main(string[] args) { AppDomain.CurrentDomain.UnhandledException += UnhandledExceptionHandler; #if DEBUG bool debug = true; #else bool debug = false; #endif if (args.Length > 0) if (args[0].ToLower() == "debug") debug = true; if (string.IsNullOrEmpty(Thread.CurrentThread.Name)) Thread.CurrentThread.Name = "Main"; if (debug && !System.Diagnostics.Debugger.IsAttached) { AllocConsole(); IntPtr stdHandle = GetStdHandle(STD_OUTPUT_HANDLE); var safeFileHandle = new SafeFileHandle(stdHandle, true); var fileStream = new FileStream(safeFileHandle, FileAccess.Write); var encoding = System.Text.Encoding.GetEncoding(CODE_PAGE); var stdOut = new StreamWriter(fileStream, encoding) {AutoFlush = true}; Console.SetOut(stdOut); } LogManager.LoadConfig(debug); _log = LogManager.GetLogger(typeof (Program)); _log.Info("!!! APPLICATION LOAD !!!"); _log.Info("Detecting components..."); foreach (var file in Directory.GetFiles(Directory.GetCurrentDirectory())) { string ext = Path.GetExtension(file); if (string.IsNullOrEmpty(ext)) continue; ext = ext.Substring(1); if (ext == "dll" || ext == "exe") { string version = AssemblyName.GetAssemblyName(file).Version.ToString(); string name = Path.GetFileNameWithoutExtension(file); _log.Info(name + " v" + version); } } _log.Info("Components detected!"); _log.Info("Sharpcraft is loading..."); try { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); if (!Directory.Exists(Constants.SettingsDirectory)) Directory.CreateDirectory(Constants.SettingsDirectory); #if SC_DIRECT new Sharpcraft(null).Run(); #else _log.Debug("Starting launcher..."); _launcher = new Launcher(); _launcher.Show(); _context = new ApplicationContext(_launcher); Application.Run(_context); _log.Info("Launcher has returned execution to main thread."); #endif } catch(FileNotFoundException ex) { _log.Fatal("Required file \"" + ex.FileName + "\" not found! Application is exiting..."); } catch(IOException ex) { _log.Fatal("IO Operation failed: " + ex.Message); _log.Fatal("Application is exiting..."); } catch(Exception ex) { UnhandledExceptionHandler(null, new UnhandledExceptionEventArgs(ex, false)); #if DEBUG throw; #endif } finally { if (_launcher != null) { _log.Debug("Closing launcher."); _launcher.Close(); _launcher.Dispose(); _launcher = null; _log.Info("Launcher closed."); } if (debug) { _log.Debug("Closing debug console."); FreeConsole(); } _log.Info("!!! APPLICATION EXIT !!!"); } }