From 366ec3fe9e808dc8b0a2fd28fa1f9e22c30893d4 Mon Sep 17 00:00:00 2001 From: Justin Aquadro Date: Sun, 17 Nov 2013 13:26:19 -0500 Subject: [PATCH] More rigorous startup failure checking and reporting --- NBTExplorer.Installer/Product.wxs | 2 +- NBTExplorer/Program.cs | 62 ++++++++++++++++++++++++++ NBTExplorer/Properties/AssemblyInfo.cs | 4 +- 3 files changed, 65 insertions(+), 3 deletions(-) diff --git a/NBTExplorer.Installer/Product.wxs b/NBTExplorer.Installer/Product.wxs index 6d91ffb..5bef182 100644 --- a/NBTExplorer.Installer/Product.wxs +++ b/NBTExplorer.Installer/Product.wxs @@ -3,7 +3,7 @@ diff --git a/NBTExplorer/Program.cs b/NBTExplorer/Program.cs index dec0542..d879e98 100644 --- a/NBTExplorer/Program.cs +++ b/NBTExplorer/Program.cs @@ -1,4 +1,8 @@ using System; +using System.Diagnostics; +using System.IO; +using System.Text; +using System.Threading; using System.Windows.Forms; using NBTExplorer.Windows; @@ -12,6 +16,11 @@ static class Program [STAThread] static void Main () { + Application.ThreadException += AppThreadFailureHandler; + Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException); + + AppDomain.CurrentDomain.UnhandledException += AppDomainFailureHandler; + Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(new MainForm()); @@ -31,5 +40,58 @@ public static void StaticInitFailure (Exception e) MessageBox.Show("Application failed during static initialization: " + original.Message); Application.Exit(); } + + private static void AppThreadFailureHandler (object sender, ThreadExceptionEventArgs e) + { + ProcessException(e.Exception); + } + + private static void AppDomainFailureHandler (object sender, UnhandledExceptionEventArgs e) + { + if (e.ExceptionObject is Exception) + ProcessException(e.ExceptionObject as Exception); + else if (e.IsTerminating) { + MessageBox.Show("NBTExplorer encountered an unknown exception object: " + e.ExceptionObject.GetType().FullName, + "NBTExplorer failed to run", MessageBoxButtons.OK, MessageBoxIcon.Error); + Application.Exit(); + } + } + + private static void ProcessException (Exception ex) + { + if (IsMissingSubstrate(ex)) { + MessageBox.Show("NBTExplorer could not find required assembly \"Substrate.dll\".\n\nIf you obtained NBTExplorer from a ZIP distribution, make sure you've extracted NBTExplorer and all of its supporting files into another directory before running it.", + "NBTExplorer failed to run", MessageBoxButtons.OK, MessageBoxIcon.Error); + Application.Exit(); + return; + } + + StringBuilder errorText = new StringBuilder(); + errorText.AppendLine("NBTExplorer encountered the following exception while trying to run: " + ex.GetType().Name); + errorText.AppendLine("Message: " + ex.Message); + + while (ex.InnerException != null) { + ex = ex.InnerException; + errorText.AppendLine(); + errorText.AppendLine("Caused by Inner Exception: " + ex.GetType().Name); + errorText.AppendLine("Message: " + ex.Message); + } + + MessageBox.Show(errorText.ToString(), "NBTExplorer failed to run", MessageBoxButtons.OK, MessageBoxIcon.Error); + Application.Exit(); + } + + private static bool IsMissingSubstrate (Exception ex) + { + if (ex is TypeInitializationException && ex.InnerException != null) + ex = ex.InnerException; + if (ex is FileNotFoundException) { + FileNotFoundException fileEx = ex as FileNotFoundException; + if (fileEx.FileName.Contains("Substrate")) + return true; + } + + return false; + } } } diff --git a/NBTExplorer/Properties/AssemblyInfo.cs b/NBTExplorer/Properties/AssemblyInfo.cs index ce4df87..556a6d5 100644 --- a/NBTExplorer/Properties/AssemblyInfo.cs +++ b/NBTExplorer/Properties/AssemblyInfo.cs @@ -32,5 +32,5 @@ // You can specify all the values or you can default the Build and Revision Numbers // by using the '*' as shown below: // [assembly: AssemblyVersion("1.0.*")] -[assembly: AssemblyVersion("2.6.0.0")] -[assembly: AssemblyFileVersion("2.6.0.0")] +[assembly: AssemblyVersion("2.6.1.0")] +[assembly: AssemblyFileVersion("2.6.1.0")]