/* * Program.cs * Authors: August Zinsser * * Copyright Matthew Belmonte 2007 */ using System; using System.Windows.Forms; using System.Threading; using System.Runtime.InteropServices; using System.IO; using tAC_Engine; using Astropolis.ColonySimulator; namespace Astropolis { /// /// XNA automatically calls this class to start the program. From here, the mainGame class gets run which starts "the game." /// This is the entry point for ColonyGame specifically, so this file needs to not be generic /// static class Program { /// /// The main entry point for the application. /// [STAThread] static void Main(string[] args) { // Listen for the unhandled exception event AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(ApplicationThreadException); // Instantiate major components ParallelPortPanel outputPanel = new ParallelPortPanel(); AstroGameManager mainGame = new AstroGameManager(); ColonyGame colonyGame = new ColonyGame(); //Set the current directory to the TAC directory System.Environment.CurrentDirectory = System.Windows.Forms.Application.StartupPath; // Create the base application which serves as a parent to all components and allows them to talk to each other new AstroBaseApplication(); AstroBaseApplication.GameManager = mainGame; AstroBaseApplication.Game = colonyGame; AstroBaseApplication.OutputPanel = outputPanel; AstroBaseApplication.MiniGame = null; // Alter the clock cycle to increase logging accuracy (this setting has a big effect on performance!) // TODO (awz): set TargetElapsedTime to a low value for minigames (accurate but slow) and a high value for the main game (innacurate but fast) mainGame.IsFixedTimeStep = true; mainGame.TargetElapsedTime = System.TimeSpan.FromMilliseconds(1f); // Create the logfile Game.Astropolis.SubVersion.Load(); // Logger.LogCode(Logger.ExperimentalCode.SVN_REVISION, Game.Astropolis.SubVersion.Revision) Logger.FlushLog(); // Set the StartUp Sequence as the first mode AstroBaseApplication.SwitchMode(AstroBaseApplication.Modes.StartUp); mainGame.Run(); } /// /// Logs exception details to ErrorLog.txt and informs the user that the game blew up /// public static void ApplicationThreadException(object sender, UnhandledExceptionEventArgs e) { #if DEBUG // Don't bother logging the error since the debugger will catch it return; #else try { // Try to get the game window out of the way of the message box GenericBaseApplication.GameManager.GraphicsDeviceManager.IsFullScreen = false; GenericBaseApplication.GameManager.GraphicsDeviceManager.ApplyChanges(); } catch { } string errorLog = @"LogFiles\ErrorLog.txt"; try { // Write-out the error to a log file StreamWriter outputStream = new StreamWriter(errorLog, false, System.Text.Encoding.ASCII); //Adds the revision info. outputStream.WriteLine("Revision: " + Game.Astropolis.SubVersion.Revision); outputStream.WriteLine(System.DateTime.Now); outputStream.WriteLine("Total Seconds: " + GenericBaseApplication.GameManager.TotalSeconds); outputStream.WriteLine("\n" + ((Exception)e.ExceptionObject).ToString()); try { // Add any extra info the current minigame may have to offer outputStream.WriteLine("\nCurrent Minigame:\n" + AstroBaseApplication.MiniGame.GetType().FullName); outputStream.WriteLine("\nCustom Debug Info:\n" + AstroBaseApplication.MiniGame.DebugInfo()); } catch (Exception e3) { outputStream.WriteLine("\nException thrown in this overridden MiniGame.DebugInfo() call:"); outputStream.WriteLine(e3.ToString()); } outputStream.Flush(); } catch (Exception e2) { // Writing failed, so just output it MessageBox.Show("We're sorry, but you seem to have found a bug in our game.\nThere was a problem creating " + errorLog + ", but if you send an email to support@autismcollaborative.org containing the body of this error message we'll do our best to fix it!\n\nLog Error:\n" + e2.ToString() + "\n\nGame Error:\n" + ((Exception)e.ExceptionObject).StackTrace, "Fatal Error", MessageBoxButtons.OK, MessageBoxIcon.Stop); return; } try { // Try to save the game Logger.FlushLog(); // No problems saving, tell the user their data made it MessageBox.Show("We're sorry, but you seem to have found a bug in our game. \nEmail us at support@autismcollaborative.org and attach ErrorLog.txt so we can fix this problem! \nThe good news is that your game was saved as ???", "Fatal Error", MessageBoxButtons.OK, MessageBoxIcon.Stop); return; } catch { MessageBox.Show("We're sorry, but you seem to have found a bug in our game. \nEmail us at support@autismcollaborative.org and attach ErrorLog.txt so we can fix this problem!", "Fatal Error", MessageBoxButtons.OK, MessageBoxIcon.Error); AppDomain.Unload(AppDomain.CurrentDomain); } #endif } } }