From ce23e162bc3c6de7e31286ea2012726cfc108666 Mon Sep 17 00:00:00 2001 From: Piotrekol Date: Wed, 24 Apr 2019 19:31:17 +0200 Subject: [PATCH] Misc: Don't treat exceptions as fatal errors --- osu!StreamCompanion/Code/Windows/ErrorFrm.cs | 5 +- .../Code/Windows/ErrorFrm.designer.cs | 20 +-- osu!StreamCompanion/Program.cs | 148 +++++++++++------- 3 files changed, 106 insertions(+), 67 deletions(-) diff --git a/osu!StreamCompanion/Code/Windows/ErrorFrm.cs b/osu!StreamCompanion/Code/Windows/ErrorFrm.cs index d3ba5e47..781c349d 100644 --- a/osu!StreamCompanion/Code/Windows/ErrorFrm.cs +++ b/osu!StreamCompanion/Code/Windows/ErrorFrm.cs @@ -20,10 +20,13 @@ protected override bool ShowWithoutActivation get { return true; } } - public Error(String Message) + public Error(string Message, string exitText) { InitializeComponent(); this.textBox1.Text = Message; + + if(!string.IsNullOrEmpty(exitText)) + this.label_exitText.Text = exitText; } private void button_message_Click(object sender, EventArgs e) diff --git a/osu!StreamCompanion/Code/Windows/ErrorFrm.designer.cs b/osu!StreamCompanion/Code/Windows/ErrorFrm.designer.cs index b124484c..770040f3 100644 --- a/osu!StreamCompanion/Code/Windows/ErrorFrm.designer.cs +++ b/osu!StreamCompanion/Code/Windows/ErrorFrm.designer.cs @@ -30,7 +30,7 @@ private void InitializeComponent() { this.label1 = new System.Windows.Forms.Label(); this.textBox1 = new System.Windows.Forms.TextBox(); - this.label2 = new System.Windows.Forms.Label(); + this.label_exitText = new System.Windows.Forms.Label(); this.SuspendLayout(); // // label1 @@ -51,21 +51,21 @@ private void InitializeComponent() this.textBox1.Size = new System.Drawing.Size(515, 216); this.textBox1.TabIndex = 1; // - // label2 + // label_exitText // - this.label2.AutoSize = true; - this.label2.Location = new System.Drawing.Point(12, 262); - this.label2.Name = "label2"; - this.label2.Size = new System.Drawing.Size(155, 13); - this.label2.TabIndex = 2; - this.label2.Text = "StreamCompanion will now exit."; + this.label_exitText.AutoSize = true; + this.label_exitText.Location = new System.Drawing.Point(12, 262); + this.label_exitText.Name = "label_exitText"; + this.label_exitText.Size = new System.Drawing.Size(155, 13); + this.label_exitText.TabIndex = 2; + this.label_exitText.Text = "StreamCompanion will now exit."; // // Error // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(543, 284); - this.Controls.Add(this.label2); + this.Controls.Add(this.label_exitText); this.Controls.Add(this.textBox1); this.Controls.Add(this.label1); this.Name = "Error"; @@ -79,6 +79,6 @@ private void InitializeComponent() private System.Windows.Forms.Label label1; private System.Windows.Forms.TextBox textBox1; - private System.Windows.Forms.Label label2; + private System.Windows.Forms.Label label_exitText; } } \ No newline at end of file diff --git a/osu!StreamCompanion/Program.cs b/osu!StreamCompanion/Program.cs index cc0b1715..3388cdb2 100644 --- a/osu!StreamCompanion/Program.cs +++ b/osu!StreamCompanion/Program.cs @@ -1,24 +1,27 @@ +using osu_StreamCompanion.Code.Core; +using osu_StreamCompanion.Code.Core.Loggers; +using osu_StreamCompanion.Code.Helpers; +using osu_StreamCompanion.Code.Windows; +using SharpRaven; +using SharpRaven.Data; using System; using System.Collections.Generic; +using System.Diagnostics; using System.Reflection; using System.Runtime.InteropServices; using System.Security.AccessControl; using System.Security.Principal; using System.Threading; using System.Windows.Forms; -using osu_StreamCompanion.Code.Core; -using osu_StreamCompanion.Code.Core.Loggers; -using osu_StreamCompanion.Code.Helpers; -using osu_StreamCompanion.Code.Windows; -using SharpRaven; -using SharpRaven.Data; namespace osu_StreamCompanion { static class Program { - public static string ScVersion ="v190307.19"; + public static string ScVersion ="v190424.19"; private static Initializer _initializer; + private const bool AllowMultiInstance = false; + /// /// The main entry point for the application. /// @@ -27,52 +30,58 @@ static void Main(string[] args) { string appGuid = ((GuidAttribute)Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(GuidAttribute), false).GetValue(0)).Value.ToString(); string mutexId = string.Format("Global\\{{{0}}}", appGuid); - using (var mutex = new Mutex(false, mutexId)) - { - var allowEveryoneRule = new MutexAccessRule(new SecurityIdentifier(WellKnownSidType.WorldSid, null), MutexRights.FullControl, AccessControlType.Allow); - var securitySettings = new MutexSecurity(); - securitySettings.AddAccessRule(allowEveryoneRule); - mutex.SetAccessControl(securitySettings); - - // edited by acidzombie24 - var hasHandle = false; - try + if (AllowMultiInstance) + Run(); + else + using (var mutex = new Mutex(false, mutexId)) { + + var allowEveryoneRule = new MutexAccessRule(new SecurityIdentifier(WellKnownSidType.WorldSid, null), MutexRights.FullControl, AccessControlType.Allow); + var securitySettings = new MutexSecurity(); + securitySettings.AddAccessRule(allowEveryoneRule); + mutex.SetAccessControl(securitySettings); + + var hasHandle = false; try { - hasHandle = mutex.WaitOne(2000, false); - if (hasHandle == false) + try + { + hasHandle = mutex.WaitOne(2000, false); + if (hasHandle == false) + { + MessageBox.Show("osu!StreamCompanion is already running.", "Error"); + return; + } + } + catch (AbandonedMutexException) { - MessageBox.Show("osu!StreamCompanion is already running.", "Error"); - return; + hasHandle = true; } + + Run(); + } - catch (AbandonedMutexException) + finally { - hasHandle = true; + if (hasHandle) + mutex.ReleaseMutex(); } - Application.EnableVisualStyles(); - Application.SetCompatibleTextRenderingDefault(false); - AppDomain.CurrentDomain.UnhandledException += - new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException); - Application.ThreadException += Application_ThreadException; - _initializer = new Initializer(); - _initializer.Start(); - Application.Run(_initializer); - - } - finally - { - if (hasHandle) - mutex.ReleaseMutex(); - } - } - - + } + private static void Run() + { + Application.EnableVisualStyles(); + Application.SetCompatibleTextRenderingDefault(false); + AppDomain.CurrentDomain.UnhandledException += + new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException); + Application.ThreadException += Application_ThreadException; + _initializer = new Initializer(); + _initializer.Start(); + Application.Run(_initializer); } + private static void Application_ThreadException(object sender, ThreadExceptionEventArgs e) { HandleException(e.Exception); @@ -129,35 +138,62 @@ static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEv HandleException(ex); } } - private static string errorNotification = @"There was unhandled problem with a program and it needs to close." + Environment.NewLine + "Error report was sent to Piotrekol"; + private static (bool ForceQuit, bool SendReport, string Message) GetErrorData() + { + if (Exceptions.Count <= 4) + return (false, true, $"{errorNotificationFormat} {willAttemptToRun} {messageWasSent}"); + + return (false, false, $"{errorNotificationFormat} {willAttemptToRun} {messageWasNotSent}"); + } + + private static string errorNotificationFormat = @"There was unhandled problem with a program"; + private static string needToExit = @"and it needs to exit."; + private static string willAttemptToRun = @"but it will attempt to run."; + private static string messageWasSent = "Error report was sent to Piotrekol."; + private static string messageWasNotSent = "Any further errors will !not! be sent to Piotrekol until StreamCompanion restarts."; + + private static List Exceptions { get; set; } public static void HandleException(Exception ex) { + bool shouldQuit = true; try { + ex.Data["ExceptionCount"] = Exceptions.Count; + Exceptions.Add(ex); + ex.Data.Add("netFramework", GetDotNetVersion.Get45PlusFromRegistry()); -#if !DEBUG - var ravenClient = SentryLogger.RavenClient; - ravenClient.Release = ScVersion; - var sentryEvent = new SentryEvent(ex); - ravenClient.Capture(sentryEvent); + + var errorConsensus = GetErrorData(); + shouldQuit = errorConsensus.ForceQuit; + +#if DEBUG + if (errorConsensus.SendReport) + { + var ravenClient = SentryLogger.RavenClient; + ravenClient.Release = ScVersion; + var sentryEvent = new SentryEvent(ex); + ravenClient.Capture(sentryEvent); + } #endif - MessageBox.Show(errorNotification, "Error", MessageBoxButtons.OK, MessageBoxIcon.Warning); - var form = new Error(ex.Message + Environment.NewLine + ex.StackTrace); + MessageBox.Show(errorConsensus.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Warning); + var form = new Error(ex.Message + Environment.NewLine + ex.StackTrace, !errorConsensus.ForceQuit ? "StreamCompanion will attempt to run" : null); form.ShowDialog(); } finally { - try - { - SafeQuit(); - } - catch + if (shouldQuit) { - _initializer.ExitThread(); + try + { + SafeQuit(); + } + catch + { + _initializer.ExitThread(); + } } } } - } }