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/ControlEvents.cs | 41 ++++++++++++++++----------- 1 file changed, 25 insertions(+), 16 deletions(-) (limited to 'src/StardewModdingAPI/Events/ControlEvents.cs') diff --git a/src/StardewModdingAPI/Events/ControlEvents.cs b/src/StardewModdingAPI/Events/ControlEvents.cs index b7f69805..790bf193 100644 --- a/src/StardewModdingAPI/Events/ControlEvents.cs +++ b/src/StardewModdingAPI/Events/ControlEvents.cs @@ -1,6 +1,7 @@ using System; using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Input; +using StardewModdingAPI.Framework; namespace StardewModdingAPI.Events { @@ -39,69 +40,77 @@ namespace StardewModdingAPI.Events ** Internal methods *********/ /// Raise a event. + /// Encapsulates monitoring and logging. /// The previous keyboard state. /// The current keyboard state. - internal static void InvokeKeyboardChanged(KeyboardState priorState, KeyboardState newState) + internal static void InvokeKeyboardChanged(IMonitor monitor, KeyboardState priorState, KeyboardState newState) { - ControlEvents.KeyboardChanged?.Invoke(null, new EventArgsKeyboardStateChanged(priorState, newState)); + monitor.SafelyRaiseGenericEvent($"{nameof(ControlEvents)}.{nameof(ControlEvents.KeyboardChanged)}", ControlEvents.KeyboardChanged?.GetInvocationList(), null, new EventArgsKeyboardStateChanged(priorState, newState)); } /// Raise a event. + /// Encapsulates monitoring and logging. /// The previous mouse state. /// The current mouse state. /// The previous mouse position on the screen adjusted for the zoom level. /// The current mouse position on the screen adjusted for the zoom level. - internal static void InvokeMouseChanged(MouseState priorState, MouseState newState, Point priorPosition, Point newPosition) + internal static void InvokeMouseChanged(IMonitor monitor, MouseState priorState, MouseState newState, Point priorPosition, Point newPosition) { - ControlEvents.MouseChanged?.Invoke(null, new EventArgsMouseStateChanged(priorState, newState, priorPosition, newPosition)); + monitor.SafelyRaiseGenericEvent($"{nameof(ControlEvents)}.{nameof(ControlEvents.MouseChanged)}", ControlEvents.MouseChanged?.GetInvocationList(), null, new EventArgsMouseStateChanged(priorState, newState, priorPosition, newPosition)); } /// Raise a event. + /// Encapsulates monitoring and logging. /// The keyboard button that was pressed. - internal static void InvokeKeyPressed(Keys key) + internal static void InvokeKeyPressed(IMonitor monitor, Keys key) { - ControlEvents.KeyPressed?.Invoke(null, new EventArgsKeyPressed(key)); + monitor.SafelyRaiseGenericEvent($"{nameof(ControlEvents)}.{nameof(ControlEvents.KeyPressed)}", ControlEvents.KeyPressed?.GetInvocationList(), null, new EventArgsKeyPressed(key)); } /// Raise a event. + /// Encapsulates monitoring and logging. /// The keyboard button that was released. - internal static void InvokeKeyReleased(Keys key) + internal static void InvokeKeyReleased(IMonitor monitor, Keys key) { - ControlEvents.KeyReleased?.Invoke(null, new EventArgsKeyPressed(key)); + monitor.SafelyRaiseGenericEvent($"{nameof(ControlEvents)}.{nameof(ControlEvents.KeyReleased)}", ControlEvents.KeyReleased?.GetInvocationList(), null, new EventArgsKeyPressed(key)); } /// Raise a event. + /// Encapsulates monitoring and logging. /// The player who pressed the button. /// The controller button that was pressed. - internal static void InvokeButtonPressed(PlayerIndex playerIndex, Buttons button) + internal static void InvokeButtonPressed(IMonitor monitor, PlayerIndex playerIndex, Buttons button) { - ControlEvents.ControllerButtonPressed?.Invoke(null, new EventArgsControllerButtonPressed(playerIndex, button)); + monitor.SafelyRaiseGenericEvent($"{nameof(ControlEvents)}.{nameof(ControlEvents.ControllerButtonPressed)}", ControlEvents.ControllerButtonPressed?.GetInvocationList(), null, new EventArgsControllerButtonPressed(playerIndex, button)); } /// Raise a event. + /// Encapsulates monitoring and logging. /// The player who released the button. /// The controller button that was released. - internal static void InvokeButtonReleased(PlayerIndex playerIndex, Buttons button) + internal static void InvokeButtonReleased(IMonitor monitor, PlayerIndex playerIndex, Buttons button) { - ControlEvents.ControllerButtonReleased?.Invoke(null, new EventArgsControllerButtonReleased(playerIndex, button)); + monitor.SafelyRaiseGenericEvent($"{nameof(ControlEvents)}.{nameof(ControlEvents.ControllerButtonReleased)}", ControlEvents.ControllerButtonReleased?.GetInvocationList(), null, new EventArgsControllerButtonReleased(playerIndex, button)); } /// Raise a event. + /// Encapsulates monitoring and logging. /// The player who pressed the trigger button. /// The trigger button that was pressed. /// The current trigger value. - internal static void InvokeTriggerPressed(PlayerIndex playerIndex, Buttons button, float value) + internal static void InvokeTriggerPressed(IMonitor monitor, PlayerIndex playerIndex, Buttons button, float value) { - ControlEvents.ControllerTriggerPressed?.Invoke(null, new EventArgsControllerTriggerPressed(playerIndex, button, value)); + monitor.SafelyRaiseGenericEvent($"{nameof(ControlEvents)}.{nameof(ControlEvents.ControllerTriggerPressed)}", ControlEvents.ControllerTriggerPressed?.GetInvocationList(), null, new EventArgsControllerTriggerPressed(playerIndex, button, value)); } /// Raise a event. + /// Encapsulates monitoring and logging. /// The player who pressed the trigger button. /// The trigger button that was pressed. /// The current trigger value. - internal static void InvokeTriggerReleased(PlayerIndex playerIndex, Buttons button, float value) + internal static void InvokeTriggerReleased(IMonitor monitor, PlayerIndex playerIndex, Buttons button, float value) { - ControlEvents.ControllerTriggerReleased?.Invoke(null, new EventArgsControllerTriggerReleased(playerIndex, button, value)); + monitor.SafelyRaiseGenericEvent($"{nameof(ControlEvents)}.{nameof(ControlEvents.ControllerTriggerReleased)}", ControlEvents.ControllerTriggerReleased?.GetInvocationList(), null, new EventArgsControllerTriggerReleased(playerIndex, button, value)); } } } -- cgit