From 9bf1ad71b43e41a3dfe645ccf66c6fb6b2e96242 Mon Sep 17 00:00:00 2001 From: Jesse Plamondon-Willard Date: Mon, 21 Nov 2016 22:09:02 -0500 Subject: intercept event handler exceptions (#179) --- src/StardewModdingAPI/Events/GraphicsEvents.cs | 97 +++++++++++--------------- 1 file changed, 41 insertions(+), 56 deletions(-) (limited to 'src/StardewModdingAPI/Events/GraphicsEvents.cs') diff --git a/src/StardewModdingAPI/Events/GraphicsEvents.cs b/src/StardewModdingAPI/Events/GraphicsEvents.cs index 3f52212b..5f4feeac 100644 --- a/src/StardewModdingAPI/Events/GraphicsEvents.cs +++ b/src/StardewModdingAPI/Events/GraphicsEvents.cs @@ -1,4 +1,5 @@ using System; +using StardewModdingAPI.Framework; namespace StardewModdingAPI.Events { @@ -72,19 +73,19 @@ namespace StardewModdingAPI.Events ** Generic events ****/ /// Raise a event. + /// Encapsulates monitoring and logging. /// The object which raised the event. /// The event arguments. - internal static void InvokeResize(object sender, EventArgs e) + internal static void InvokeResize(IMonitor monitor, object sender, EventArgs e) { - GraphicsEvents.Resize?.Invoke(sender, e); + monitor.SafelyRaisePlainEvent($"{nameof(GraphicsEvents)}.{nameof(GraphicsEvents.Resize)}", GraphicsEvents.Resize?.GetInvocationList(), sender, e); } /// Raise a event. - /// The object which raised the event. - /// The event arguments. - internal static void InvokeDrawDebug(object sender, EventArgs e) + /// Encapsulates monitoring and logging. + internal static void InvokeDrawDebug(IMonitor monitor) { - GraphicsEvents.DrawDebug?.Invoke(sender, e); + monitor.SafelyRaisePlainEvent($"{nameof(GraphicsEvents)}.{nameof(GraphicsEvents.DrawDebug)}", GraphicsEvents.DrawDebug?.GetInvocationList()); } /// Raise a event. @@ -92,110 +93,94 @@ namespace StardewModdingAPI.Events [Obsolete("Should not be used.")] public static void InvokeDrawTick(IMonitor monitor) { - try - { - GraphicsEvents.DrawTick?.Invoke(null, EventArgs.Empty); - } - catch (Exception ex) - { - monitor.Log($"A mod crashed handling an event.\n{ex}", LogLevel.Error); - } + monitor.SafelyRaisePlainEvent($"{nameof(GraphicsEvents)}.{nameof(GraphicsEvents.DrawTick)}", GraphicsEvents.DrawTick?.GetInvocationList()); } /// Raise a event. + /// Encapsulates monitoring and logging. [Obsolete("Should not be used.")] - public static void InvokeDrawInRenderTargetTick() + public static void InvokeDrawInRenderTargetTick(IMonitor monitor) { - GraphicsEvents.DrawInRenderTargetTick?.Invoke(null, EventArgs.Empty); + monitor.SafelyRaisePlainEvent($"{nameof(GraphicsEvents)}.{nameof(GraphicsEvents.DrawInRenderTargetTick)}", GraphicsEvents.DrawInRenderTargetTick?.GetInvocationList()); } /**** ** Main render events ****/ /// Raise an event. - /// The object which raised the event. - /// The event arguments. - internal static void InvokeOnPreRenderEvent(object sender, EventArgs e) + /// Encapsulates monitoring and logging. + internal static void InvokeOnPreRenderEvent(IMonitor monitor) { - GraphicsEvents.OnPreRenderEvent?.Invoke(sender, e); + monitor.SafelyRaisePlainEvent($"{nameof(GraphicsEvents)}.{nameof(GraphicsEvents.OnPreRenderEvent)}", GraphicsEvents.OnPreRenderEvent?.GetInvocationList()); } /// Raise an event. - /// The object which raised the event. - /// The event arguments. - internal static void InvokeOnPostRenderEvent(object sender, EventArgs e) + /// Encapsulates monitoring and logging. + internal static void InvokeOnPostRenderEvent(IMonitor monitor) { - GraphicsEvents.OnPostRenderEvent?.Invoke(sender, e); + monitor.SafelyRaisePlainEvent($"{nameof(GraphicsEvents)}.{nameof(GraphicsEvents.OnPostRenderEvent)}", GraphicsEvents.OnPostRenderEvent?.GetInvocationList()); } /**** ** HUD events ****/ /// Raise an event. - /// The object which raised the event. - /// The event arguments. - internal static void InvokeOnPreRenderGuiEvent(object sender, EventArgs e) + /// Encapsulates monitoring and logging. + internal static void InvokeOnPreRenderGuiEvent(IMonitor monitor) { - GraphicsEvents.OnPreRenderGuiEvent?.Invoke(sender, e); + monitor.SafelyRaisePlainEvent($"{nameof(GraphicsEvents)}.{nameof(GraphicsEvents.OnPreRenderGuiEvent)}", GraphicsEvents.OnPreRenderGuiEvent?.GetInvocationList()); } /// Raise an event. - /// The object which raised the event. - /// The event arguments. - internal static void InvokeOnPreRenderGuiEventNoCheck(object sender, EventArgs e) + /// Encapsulates monitoring and logging. + internal static void InvokeOnPreRenderGuiEventNoCheck(IMonitor monitor) { - GraphicsEvents.OnPreRenderGuiEventNoCheck?.Invoke(sender, e); + monitor.SafelyRaisePlainEvent($"{nameof(GraphicsEvents)}.{nameof(GraphicsEvents.OnPreRenderGuiEventNoCheck)}", GraphicsEvents.OnPreRenderGuiEventNoCheck?.GetInvocationList()); } /// Raise an event. - /// The object which raised the event. - /// The event arguments. - internal static void InvokeOnPostRenderGuiEvent(object sender, EventArgs e) + /// Encapsulates monitoring and logging. + internal static void InvokeOnPostRenderGuiEvent(IMonitor monitor) { - GraphicsEvents.OnPostRenderGuiEvent?.Invoke(sender, e); + monitor.SafelyRaisePlainEvent($"{nameof(GraphicsEvents)}.{nameof(GraphicsEvents.OnPostRenderGuiEvent)}", GraphicsEvents.OnPostRenderGuiEvent?.GetInvocationList()); } /// Raise an event. - /// The object which raised the event. - /// The event arguments. - internal static void InvokeOnPostRenderGuiEventNoCheck(object sender, EventArgs e) + /// Encapsulates monitoring and logging. + internal static void InvokeOnPostRenderGuiEventNoCheck(IMonitor monitor) { - GraphicsEvents.OnPostRenderGuiEventNoCheck?.Invoke(sender, e); + monitor.SafelyRaisePlainEvent($"{nameof(GraphicsEvents)}.{nameof(GraphicsEvents.OnPostRenderGuiEventNoCheck)}", GraphicsEvents.OnPostRenderGuiEventNoCheck?.GetInvocationList()); } /**** ** GUI events ****/ /// Raise an event. - /// The object which raised the event. - /// The event arguments. - internal static void InvokeOnPreRenderHudEvent(object sender, EventArgs e) + /// Encapsulates monitoring and logging. + internal static void InvokeOnPreRenderHudEvent(IMonitor monitor) { - GraphicsEvents.OnPreRenderHudEvent?.Invoke(sender, e); + monitor.SafelyRaisePlainEvent($"{nameof(GraphicsEvents)}.{nameof(GraphicsEvents.OnPreRenderHudEvent)}", GraphicsEvents.OnPreRenderHudEvent?.GetInvocationList()); } /// Raise an event. - /// The object which raised the event. - /// The event arguments. - internal static void InvokeOnPreRenderHudEventNoCheck(object sender, EventArgs e) + /// Encapsulates monitoring and logging. + internal static void InvokeOnPreRenderHudEventNoCheck(IMonitor monitor) { - GraphicsEvents.OnPreRenderHudEventNoCheck?.Invoke(sender, e); + monitor.SafelyRaisePlainEvent($"{nameof(GraphicsEvents)}.{nameof(GraphicsEvents.OnPreRenderHudEventNoCheck)}", GraphicsEvents.OnPreRenderHudEventNoCheck?.GetInvocationList()); } /// Raise an event. - /// The object which raised the event. - /// The event arguments. - internal static void InvokeOnPostRenderHudEvent(object sender, EventArgs e) + /// Encapsulates monitoring and logging. + internal static void InvokeOnPostRenderHudEvent(IMonitor monitor) { - GraphicsEvents.OnPostRenderHudEvent?.Invoke(sender, e); + monitor.SafelyRaisePlainEvent($"{nameof(GraphicsEvents)}.{nameof(GraphicsEvents.OnPostRenderHudEvent)}", GraphicsEvents.OnPostRenderHudEvent?.GetInvocationList()); } /// Raise an event. - /// The object which raised the event. - /// The event arguments. - internal static void InvokeOnPostRenderHudEventNoCheck(object sender, EventArgs e) + /// Encapsulates monitoring and logging. + internal static void InvokeOnPostRenderHudEventNoCheck(IMonitor monitor) { - GraphicsEvents.OnPostRenderHudEventNoCheck?.Invoke(sender, e); + monitor.SafelyRaisePlainEvent($"{nameof(GraphicsEvents)}.{nameof(GraphicsEvents.OnPostRenderHudEventNoCheck)}", GraphicsEvents.OnPostRenderHudEventNoCheck?.GetInvocationList()); } } } -- cgit