summaryrefslogtreecommitdiff
path: root/src/SMAPI/Framework/Events
diff options
context:
space:
mode:
authorJesse Plamondon-Willard <Pathoschild@users.noreply.github.com>2019-02-24 19:56:08 -0500
committerJesse Plamondon-Willard <Pathoschild@users.noreply.github.com>2019-09-14 16:59:29 -0400
commitbad2ac2a29b8775be97133e4c4cfb67a4a7406ee (patch)
tree9cc61caa21be489a9eb6b67368e2c40aca17af80 /src/SMAPI/Framework/Events
parent4a297e29eb0c97be799ee3939e9747dc1c631427 (diff)
downloadSMAPI-bad2ac2a29b8775be97133e4c4cfb67a4a7406ee.tar.gz
SMAPI-bad2ac2a29b8775be97133e4c4cfb67a4a7406ee.tar.bz2
SMAPI-bad2ac2a29b8775be97133e4c4cfb67a4a7406ee.zip
remove deprecated APIs (#606)
Diffstat (limited to 'src/SMAPI/Framework/Events')
-rw-r--r--src/SMAPI/Framework/Events/EventManager.cs260
-rw-r--r--src/SMAPI/Framework/Events/ManagedEvent.cs105
-rw-r--r--src/SMAPI/Framework/Events/ManagedEventBase.cs93
3 files changed, 55 insertions, 403 deletions
diff --git a/src/SMAPI/Framework/Events/EventManager.cs b/src/SMAPI/Framework/Events/EventManager.cs
index 13244601..23879f1d 100644
--- a/src/SMAPI/Framework/Events/EventManager.cs
+++ b/src/SMAPI/Framework/Events/EventManager.cs
@@ -1,7 +1,4 @@
using System.Diagnostics.CodeAnalysis;
-#if !SMAPI_3_0_STRICT
-using Microsoft.Xna.Framework.Input;
-#endif
using StardewModdingAPI.Events;
namespace StardewModdingAPI.Framework.Events
@@ -167,196 +164,6 @@ namespace StardewModdingAPI.Framework.Events
public readonly ManagedEvent<UnvalidatedUpdateTickedEventArgs> UnvalidatedUpdateTicked;
-#if !SMAPI_3_0_STRICT
- /*********
- ** Events (old)
- *********/
- /****
- ** ContentEvents
- ****/
- /// <summary>Raised after the content language changes.</summary>
- public readonly ManagedEvent<EventArgsValueChanged<string>> Legacy_LocaleChanged;
-
- /****
- ** ControlEvents
- ****/
- /// <summary>Raised when the <see cref="KeyboardState"/> changes. That happens when the player presses or releases a key.</summary>
- public readonly ManagedEvent<EventArgsKeyboardStateChanged> Legacy_KeyboardChanged;
-
- /// <summary>Raised after the player presses a keyboard key.</summary>
- public readonly ManagedEvent<EventArgsKeyPressed> Legacy_KeyPressed;
-
- /// <summary>Raised after the player releases a keyboard key.</summary>
- public readonly ManagedEvent<EventArgsKeyPressed> Legacy_KeyReleased;
-
- /// <summary>Raised when the <see cref="MouseState"/> changes. That happens when the player moves the mouse, scrolls the mouse wheel, or presses/releases a button.</summary>
- public readonly ManagedEvent<EventArgsMouseStateChanged> Legacy_MouseChanged;
-
- /// <summary>The player pressed a controller button. This event isn't raised for trigger buttons.</summary>
- public readonly ManagedEvent<EventArgsControllerButtonPressed> Legacy_ControllerButtonPressed;
-
- /// <summary>The player released a controller button. This event isn't raised for trigger buttons.</summary>
- public readonly ManagedEvent<EventArgsControllerButtonReleased> Legacy_ControllerButtonReleased;
-
- /// <summary>The player pressed a controller trigger button.</summary>
- public readonly ManagedEvent<EventArgsControllerTriggerPressed> Legacy_ControllerTriggerPressed;
-
- /// <summary>The player released a controller trigger button.</summary>
- public readonly ManagedEvent<EventArgsControllerTriggerReleased> Legacy_ControllerTriggerReleased;
-
- /****
- ** GameEvents
- ****/
- /// <summary>Raised once after the game initialises and all <see cref="IMod.Entry"/> methods have been called.</summary>
- public readonly ManagedEvent Legacy_FirstUpdateTick;
-
- /// <summary>Raised when the game updates its state (≈60 times per second).</summary>
- public readonly ManagedEvent Legacy_UpdateTick;
-
- /// <summary>Raised every other tick (≈30 times per second).</summary>
- public readonly ManagedEvent Legacy_SecondUpdateTick;
-
- /// <summary>Raised every fourth tick (≈15 times per second).</summary>
- public readonly ManagedEvent Legacy_FourthUpdateTick;
-
- /// <summary>Raised every eighth tick (≈8 times per second).</summary>
- public readonly ManagedEvent Legacy_EighthUpdateTick;
-
- /// <summary>Raised every 15th tick (≈4 times per second).</summary>
- public readonly ManagedEvent Legacy_QuarterSecondTick;
-
- /// <summary>Raised every 30th tick (≈twice per second).</summary>
- public readonly ManagedEvent Legacy_HalfSecondTick;
-
- /// <summary>Raised every 60th tick (≈once per second).</summary>
- public readonly ManagedEvent Legacy_OneSecondTick;
-
- /****
- ** GraphicsEvents
- ****/
- /// <summary>Raised after the game window is resized.</summary>
- public readonly ManagedEvent Legacy_Resize;
-
- /// <summary>Raised before drawing the world to the screen.</summary>
- public readonly ManagedEvent Legacy_OnPreRenderEvent;
-
- /// <summary>Raised after drawing the world to the screen.</summary>
- public readonly ManagedEvent Legacy_OnPostRenderEvent;
-
- /// <summary>Raised before drawing the HUD (item toolbar, clock, etc) to the screen. The HUD is available at this point, but not necessarily visible. (For example, the event is raised even if a menu is open.)</summary>
- public readonly ManagedEvent Legacy_OnPreRenderHudEvent;
-
- /// <summary>Raised after drawing the HUD (item toolbar, clock, etc) to the screen. The HUD is available at this point, but not necessarily visible. (For example, the event is raised even if a menu is open.)</summary>
- public readonly ManagedEvent Legacy_OnPostRenderHudEvent;
-
- /// <summary>Raised before drawing a menu to the screen during a draw loop. This includes the game's internal menus like the title screen.</summary>
- public readonly ManagedEvent Legacy_OnPreRenderGuiEvent;
-
- /// <summary>Raised after drawing a menu to the screen during a draw loop. This includes the game's internal menus like the title screen.</summary>
- public readonly ManagedEvent Legacy_OnPostRenderGuiEvent;
-
- /****
- ** InputEvents
- ****/
- /// <summary>Raised after the player presses a button on the keyboard, controller, or mouse.</summary>
- public readonly ManagedEvent<EventArgsInput> Legacy_ButtonPressed;
-
- /// <summary>Raised after the player releases a keyboard key on the keyboard, controller, or mouse.</summary>
- public readonly ManagedEvent<EventArgsInput> Legacy_ButtonReleased;
-
- /****
- ** LocationEvents
- ****/
- /// <summary>Raised after a game location is added or removed.</summary>
- public readonly ManagedEvent<EventArgsLocationsChanged> Legacy_LocationsChanged;
-
- /// <summary>Raised after buildings are added or removed in a location.</summary>
- public readonly ManagedEvent<EventArgsLocationBuildingsChanged> Legacy_BuildingsChanged;
-
- /// <summary>Raised after objects are added or removed in a location.</summary>
- public readonly ManagedEvent<EventArgsLocationObjectsChanged> Legacy_ObjectsChanged;
-
- /****
- ** MenuEvents
- ****/
- /// <summary>Raised after a game menu is opened or replaced with another menu. This event is not invoked when a menu is closed.</summary>
- public readonly ManagedEvent<EventArgsClickableMenuChanged> Legacy_MenuChanged;
-
- /// <summary>Raised after a game menu is closed.</summary>
- public readonly ManagedEvent<EventArgsClickableMenuClosed> Legacy_MenuClosed;
-
- /****
- ** MultiplayerEvents
- ****/
- /// <summary>Raised before the game syncs changes from other players.</summary>
- public readonly ManagedEvent Legacy_BeforeMainSync;
-
- /// <summary>Raised after the game syncs changes from other players.</summary>
- public readonly ManagedEvent Legacy_AfterMainSync;
-
- /// <summary>Raised before the game broadcasts changes to other players.</summary>
- public readonly ManagedEvent Legacy_BeforeMainBroadcast;
-
- /// <summary>Raised after the game broadcasts changes to other players.</summary>
- public readonly ManagedEvent Legacy_AfterMainBroadcast;
-
- /****
- ** MineEvents
- ****/
- /// <summary>Raised after the player warps to a new level of the mine.</summary>
- public readonly ManagedEvent<EventArgsMineLevelChanged> Legacy_MineLevelChanged;
-
- /****
- ** PlayerEvents
- ****/
- /// <summary>Raised after the player's inventory changes in any way (added or removed item, sorted, etc).</summary>
- public readonly ManagedEvent<EventArgsInventoryChanged> Legacy_InventoryChanged;
-
- /// <summary> Raised after the player levels up a skill. This happens as soon as they level up, not when the game notifies the player after their character goes to bed.</summary>
- public readonly ManagedEvent<EventArgsLevelUp> Legacy_LeveledUp;
-
- /// <summary>Raised after the player warps to a new location.</summary>
- public readonly ManagedEvent<EventArgsPlayerWarped> Legacy_PlayerWarped;
-
-
- /****
- ** SaveEvents
- ****/
- /// <summary>Raised before the game creates the save file.</summary>
- public readonly ManagedEvent Legacy_BeforeCreateSave;
-
- /// <summary>Raised after the game finishes creating the save file.</summary>
- public readonly ManagedEvent Legacy_AfterCreateSave;
-
- /// <summary>Raised before the game begins writes data to the save file.</summary>
- public readonly ManagedEvent Legacy_BeforeSave;
-
- /// <summary>Raised after the game finishes writing data to the save file.</summary>
- public readonly ManagedEvent Legacy_AfterSave;
-
- /// <summary>Raised after the player loads a save slot.</summary>
- public readonly ManagedEvent Legacy_AfterLoad;
-
- /// <summary>Raised after the game returns to the title screen.</summary>
- public readonly ManagedEvent Legacy_AfterReturnToTitle;
-
- /****
- ** SpecialisedEvents
- ****/
- /// <summary>Raised when the game updates its state (≈60 times per second), regardless of normal SMAPI validation. This event is not thread-safe and may be invoked while game logic is running asynchronously. Changes to game state in this method may crash the game or corrupt an in-progress save. Do not use this event unless you're fully aware of the context in which your code will be run. Mods using this method will trigger a stability warning in the SMAPI console.</summary>
- public readonly ManagedEvent Legacy_UnvalidatedUpdateTick;
-
- /****
- ** TimeEvents
- ****/
- /// <summary>Raised after the game begins a new day, including when loading a save.</summary>
- public readonly ManagedEvent Legacy_AfterDayStarted;
-
- /// <summary>Raised after the in-game clock changes.</summary>
- public readonly ManagedEvent<EventArgsIntChanged> Legacy_TimeOfDayChanged;
-#endif
-
-
/*********
** Public methods
*********/
@@ -367,9 +174,6 @@ namespace StardewModdingAPI.Framework.Events
{
// create shortcut initialisers
ManagedEvent<TEventArgs> ManageEventOf<TEventArgs>(string typeName, string eventName) => new ManagedEvent<TEventArgs>($"{typeName}.{eventName}", monitor, modRegistry);
-#if !SMAPI_3_0_STRICT
- ManagedEvent ManageEvent(string typeName, string eventName) => new ManagedEvent($"{typeName}.{eventName}", monitor, modRegistry);
-#endif
// init events (new)
this.MenuChanged = ManageEventOf<MenuChangedEventArgs>(nameof(IModEvents.Display), nameof(IDisplayEvents.MenuChanged));
@@ -422,70 +226,6 @@ namespace StardewModdingAPI.Framework.Events
this.LoadStageChanged = ManageEventOf<LoadStageChangedEventArgs>(nameof(IModEvents.Specialised), nameof(ISpecialisedEvents.LoadStageChanged));
this.UnvalidatedUpdateTicking = ManageEventOf<UnvalidatedUpdateTickingEventArgs>(nameof(IModEvents.Specialised), nameof(ISpecialisedEvents.UnvalidatedUpdateTicking));
this.UnvalidatedUpdateTicked = ManageEventOf<UnvalidatedUpdateTickedEventArgs>(nameof(IModEvents.Specialised), nameof(ISpecialisedEvents.UnvalidatedUpdateTicked));
-
-#if !SMAPI_3_0_STRICT
- // init events (old)
- this.Legacy_LocaleChanged = ManageEventOf<EventArgsValueChanged<string>>(nameof(ContentEvents), nameof(ContentEvents.AfterLocaleChanged));
-
- this.Legacy_ControllerButtonPressed = ManageEventOf<EventArgsControllerButtonPressed>(nameof(ControlEvents), nameof(ControlEvents.ControllerButtonPressed));
- this.Legacy_ControllerButtonReleased = ManageEventOf<EventArgsControllerButtonReleased>(nameof(ControlEvents), nameof(ControlEvents.ControllerButtonReleased));
- this.Legacy_ControllerTriggerPressed = ManageEventOf<EventArgsControllerTriggerPressed>(nameof(ControlEvents), nameof(ControlEvents.ControllerTriggerPressed));
- this.Legacy_ControllerTriggerReleased = ManageEventOf<EventArgsControllerTriggerReleased>(nameof(ControlEvents), nameof(ControlEvents.ControllerTriggerReleased));
- this.Legacy_KeyboardChanged = ManageEventOf<EventArgsKeyboardStateChanged>(nameof(ControlEvents), nameof(ControlEvents.KeyboardChanged));
- this.Legacy_KeyPressed = ManageEventOf<EventArgsKeyPressed>(nameof(ControlEvents), nameof(ControlEvents.KeyPressed));
- this.Legacy_KeyReleased = ManageEventOf<EventArgsKeyPressed>(nameof(ControlEvents), nameof(ControlEvents.KeyReleased));
- this.Legacy_MouseChanged = ManageEventOf<EventArgsMouseStateChanged>(nameof(ControlEvents), nameof(ControlEvents.MouseChanged));
-
- this.Legacy_FirstUpdateTick = ManageEvent(nameof(GameEvents), nameof(GameEvents.FirstUpdateTick));
- this.Legacy_UpdateTick = ManageEvent(nameof(GameEvents), nameof(GameEvents.UpdateTick));
- this.Legacy_SecondUpdateTick = ManageEvent(nameof(GameEvents), nameof(GameEvents.SecondUpdateTick));
- this.Legacy_FourthUpdateTick = ManageEvent(nameof(GameEvents), nameof(GameEvents.FourthUpdateTick));
- this.Legacy_EighthUpdateTick = ManageEvent(nameof(GameEvents), nameof(GameEvents.EighthUpdateTick));
- this.Legacy_QuarterSecondTick = ManageEvent(nameof(GameEvents), nameof(GameEvents.QuarterSecondTick));
- this.Legacy_HalfSecondTick = ManageEvent(nameof(GameEvents), nameof(GameEvents.HalfSecondTick));
- this.Legacy_OneSecondTick = ManageEvent(nameof(GameEvents), nameof(GameEvents.OneSecondTick));
-
- this.Legacy_Resize = ManageEvent(nameof(GraphicsEvents), nameof(GraphicsEvents.Resize));
- this.Legacy_OnPreRenderEvent = ManageEvent(nameof(GraphicsEvents), nameof(GraphicsEvents.OnPreRenderEvent));
- this.Legacy_OnPostRenderEvent = ManageEvent(nameof(GraphicsEvents), nameof(GraphicsEvents.OnPostRenderEvent));
- this.Legacy_OnPreRenderHudEvent = ManageEvent(nameof(GraphicsEvents), nameof(GraphicsEvents.OnPreRenderHudEvent));
- this.Legacy_OnPostRenderHudEvent = ManageEvent(nameof(GraphicsEvents), nameof(GraphicsEvents.OnPostRenderHudEvent));
- this.Legacy_OnPreRenderGuiEvent = ManageEvent(nameof(GraphicsEvents), nameof(GraphicsEvents.OnPreRenderGuiEvent));
- this.Legacy_OnPostRenderGuiEvent = ManageEvent(nameof(GraphicsEvents), nameof(GraphicsEvents.OnPostRenderGuiEvent));
-
- this.Legacy_ButtonPressed = ManageEventOf<EventArgsInput>(nameof(InputEvents), nameof(InputEvents.ButtonPressed));
- this.Legacy_ButtonReleased = ManageEventOf<EventArgsInput>(nameof(InputEvents), nameof(InputEvents.ButtonReleased));
-
- this.Legacy_LocationsChanged = ManageEventOf<EventArgsLocationsChanged>(nameof(LocationEvents), nameof(LocationEvents.LocationsChanged));
- this.Legacy_BuildingsChanged = ManageEventOf<EventArgsLocationBuildingsChanged>(nameof(LocationEvents), nameof(LocationEvents.BuildingsChanged));
- this.Legacy_ObjectsChanged = ManageEventOf<EventArgsLocationObjectsChanged>(nameof(LocationEvents), nameof(LocationEvents.ObjectsChanged));
-
- this.Legacy_MenuChanged = ManageEventOf<EventArgsClickableMenuChanged>(nameof(MenuEvents), nameof(MenuEvents.MenuChanged));
- this.Legacy_MenuClosed = ManageEventOf<EventArgsClickableMenuClosed>(nameof(MenuEvents), nameof(MenuEvents.MenuClosed));
-
- this.Legacy_BeforeMainBroadcast = ManageEvent(nameof(MultiplayerEvents), nameof(MultiplayerEvents.BeforeMainBroadcast));
- this.Legacy_AfterMainBroadcast = ManageEvent(nameof(MultiplayerEvents), nameof(MultiplayerEvents.AfterMainBroadcast));
- this.Legacy_BeforeMainSync = ManageEvent(nameof(MultiplayerEvents), nameof(MultiplayerEvents.BeforeMainSync));
- this.Legacy_AfterMainSync = ManageEvent(nameof(MultiplayerEvents), nameof(MultiplayerEvents.AfterMainSync));
-
- this.Legacy_MineLevelChanged = ManageEventOf<EventArgsMineLevelChanged>(nameof(MineEvents), nameof(MineEvents.MineLevelChanged));
-
- this.Legacy_InventoryChanged = ManageEventOf<EventArgsInventoryChanged>(nameof(PlayerEvents), nameof(PlayerEvents.InventoryChanged));
- this.Legacy_LeveledUp = ManageEventOf<EventArgsLevelUp>(nameof(PlayerEvents), nameof(PlayerEvents.LeveledUp));
- this.Legacy_PlayerWarped = ManageEventOf<EventArgsPlayerWarped>(nameof(PlayerEvents), nameof(PlayerEvents.Warped));
-
- this.Legacy_BeforeCreateSave = ManageEvent(nameof(SaveEvents), nameof(SaveEvents.BeforeCreate));
- this.Legacy_AfterCreateSave = ManageEvent(nameof(SaveEvents), nameof(SaveEvents.AfterCreate));
- this.Legacy_BeforeSave = ManageEvent(nameof(SaveEvents), nameof(SaveEvents.BeforeSave));
- this.Legacy_AfterSave = ManageEvent(nameof(SaveEvents), nameof(SaveEvents.AfterSave));
- this.Legacy_AfterLoad = ManageEvent(nameof(SaveEvents), nameof(SaveEvents.AfterLoad));
- this.Legacy_AfterReturnToTitle = ManageEvent(nameof(SaveEvents), nameof(SaveEvents.AfterReturnToTitle));
-
- this.Legacy_UnvalidatedUpdateTick = ManageEvent(nameof(SpecialisedEvents), nameof(SpecialisedEvents.UnvalidatedUpdateTick));
-
- this.Legacy_AfterDayStarted = ManageEvent(nameof(TimeEvents), nameof(TimeEvents.AfterDayStarted));
- this.Legacy_TimeOfDayChanged = ManageEventOf<EventArgsIntChanged>(nameof(TimeEvents), nameof(TimeEvents.TimeOfDayChanged));
-#endif
}
}
}
diff --git a/src/SMAPI/Framework/Events/ManagedEvent.cs b/src/SMAPI/Framework/Events/ManagedEvent.cs
index f9e7f6ec..2afe7a03 100644
--- a/src/SMAPI/Framework/Events/ManagedEvent.cs
+++ b/src/SMAPI/Framework/Events/ManagedEvent.cs
@@ -1,11 +1,12 @@
using System;
+using System.Collections.Generic;
using System.Linq;
namespace StardewModdingAPI.Framework.Events
{
/// <summary>An event wrapper which intercepts and logs errors in handler code.</summary>
/// <typeparam name="TEventArgs">The event arguments type.</typeparam>
- internal class ManagedEvent<TEventArgs> : ManagedEventBase<EventHandler<TEventArgs>>
+ internal class ManagedEvent<TEventArgs>
{
/*********
** Fields
@@ -13,6 +14,21 @@ namespace StardewModdingAPI.Framework.Events
/// <summary>The underlying event.</summary>
private event EventHandler<TEventArgs> Event;
+ /// <summary>A human-readable name for the event.</summary>
+ private readonly string EventName;
+
+ /// <summary>Writes messages to the log.</summary>
+ private readonly IMonitor Monitor;
+
+ /// <summary>The mod registry with which to identify mods.</summary>
+ protected readonly ModRegistry ModRegistry;
+
+ /// <summary>The display names for the mods which added each delegate.</summary>
+ private readonly IDictionary<EventHandler<TEventArgs>, IModMetadata> SourceMods = new Dictionary<EventHandler<TEventArgs>, IModMetadata>();
+
+ /// <summary>The cached invocation list.</summary>
+ private EventHandler<TEventArgs>[] CachedInvocationList;
+
/*********
** Public methods
@@ -22,7 +38,17 @@ namespace StardewModdingAPI.Framework.Events
/// <param name="monitor">Writes messages to the log.</param>
/// <param name="modRegistry">The mod registry with which to identify mods.</param>
public ManagedEvent(string eventName, IMonitor monitor, ModRegistry modRegistry)
- : base(eventName, monitor, modRegistry) { }
+ {
+ this.EventName = eventName;
+ this.Monitor = monitor;
+ this.ModRegistry = modRegistry;
+ }
+
+ /// <summary>Get whether anything is listening to the event.</summary>
+ public bool HasListeners()
+ {
+ return this.CachedInvocationList?.Length > 0;
+ }
/// <summary>Add an event handler.</summary>
/// <param name="handler">The event handler.</param>
@@ -91,71 +117,50 @@ namespace StardewModdingAPI.Framework.Events
}
}
}
- }
-
-#if !SMAPI_3_0_STRICT
- /// <summary>An event wrapper which intercepts and logs errors in handler code.</summary>
- internal class ManagedEvent : ManagedEventBase<EventHandler>
- {
- /*********
- ** Fields
- *********/
- /// <summary>The underlying event.</summary>
- private event EventHandler Event;
/*********
- ** Public methods
+ ** Private methods
*********/
- /// <summary>Construct an instance.</summary>
- /// <param name="eventName">A human-readable name for the event.</param>
- /// <param name="monitor">Writes messages to the log.</param>
- /// <param name="modRegistry">The mod registry with which to identify mods.</param>
- public ManagedEvent(string eventName, IMonitor monitor, ModRegistry modRegistry)
- : base(eventName, monitor, modRegistry) { }
-
- /// <summary>Add an event handler.</summary>
+ /// <summary>Track an event handler.</summary>
+ /// <param name="mod">The mod which added the handler.</param>
/// <param name="handler">The event handler.</param>
- public void Add(EventHandler handler)
+ /// <param name="invocationList">The updated event invocation list.</param>
+ protected void AddTracking(IModMetadata mod, EventHandler<TEventArgs> handler, IEnumerable<EventHandler<TEventArgs>> invocationList)
{
- this.Add(handler, this.ModRegistry.GetFromStack());
+ this.SourceMods[handler] = mod;
+ this.CachedInvocationList = invocationList?.ToArray() ?? new EventHandler<TEventArgs>[0];
}
- /// <summary>Add an event handler.</summary>
+ /// <summary>Remove tracking for an event handler.</summary>
/// <param name="handler">The event handler.</param>
- /// <param name="mod">The mod which added the event handler.</param>
- public void Add(EventHandler handler, IModMetadata mod)
+ /// <param name="invocationList">The updated event invocation list.</param>
+ protected void RemoveTracking(EventHandler<TEventArgs> handler, IEnumerable<EventHandler<TEventArgs>> invocationList)
{
- this.Event += handler;
- this.AddTracking(mod, handler, this.Event?.GetInvocationList().Cast<EventHandler>());
+ this.CachedInvocationList = invocationList?.ToArray() ?? new EventHandler<TEventArgs>[0];
+ if (!this.CachedInvocationList.Contains(handler)) // don't remove if there's still a reference to the removed handler (e.g. it was added twice and removed once)
+ this.SourceMods.Remove(handler);
}
- /// <summary>Remove an event handler.</summary>
+ /// <summary>Get the mod which registered the given event handler, if available.</summary>
/// <param name="handler">The event handler.</param>
- public void Remove(EventHandler handler)
+ protected IModMetadata GetSourceMod(EventHandler<TEventArgs> handler)
{
- this.Event -= handler;
- this.RemoveTracking(handler, this.Event?.GetInvocationList().Cast<EventHandler>());
+ return this.SourceMods.TryGetValue(handler, out IModMetadata mod)
+ ? mod
+ : null;
}
- /// <summary>Raise the event and notify all handlers.</summary>
- public void Raise()
+ /// <summary>Log an exception from an event handler.</summary>
+ /// <param name="handler">The event handler instance.</param>
+ /// <param name="ex">The exception that was raised.</param>
+ protected void LogError(EventHandler<TEventArgs> handler, Exception ex)
{
- if (this.Event == null)
- return;
-
- foreach (EventHandler handler in this.CachedInvocationList)
- {
- try
- {
- handler.Invoke(null, EventArgs.Empty);
- }
- catch (Exception ex)
- {
- this.LogError(handler, ex);
- }
- }
+ IModMetadata mod = this.GetSourceMod(handler);
+ if (mod != null)
+ mod.LogAsMod($"This mod failed in the {this.EventName} event. Technical details: \n{ex.GetLogSummary()}", LogLevel.Error);
+ else
+ this.Monitor.Log($"A mod failed in the {this.EventName} event. Technical details: \n{ex.GetLogSummary()}", LogLevel.Error);
}
}
-#endif
}
diff --git a/src/SMAPI/Framework/Events/ManagedEventBase.cs b/src/SMAPI/Framework/Events/ManagedEventBase.cs
deleted file mode 100644
index c8c3516b..00000000
--- a/src/SMAPI/Framework/Events/ManagedEventBase.cs
+++ /dev/null
@@ -1,93 +0,0 @@
-using System;
-using System.Collections.Generic;
-using System.Linq;
-
-namespace StardewModdingAPI.Framework.Events
-{
- /// <summary>The base implementation for an event wrapper which intercepts and logs errors in handler code.</summary>
- internal abstract class ManagedEventBase<TEventHandler>
- {
- /*********
- ** Fields
- *********/
- /// <summary>A human-readable name for the event.</summary>
- private readonly string EventName;
-
- /// <summary>Writes messages to the log.</summary>
- private readonly IMonitor Monitor;
-
- /// <summary>The mod registry with which to identify mods.</summary>
- protected readonly ModRegistry ModRegistry;
-
- /// <summary>The display names for the mods which added each delegate.</summary>
- private readonly IDictionary<TEventHandler, IModMetadata> SourceMods = new Dictionary<TEventHandler, IModMetadata>();
-
- /// <summary>The cached invocation list.</summary>
- protected TEventHandler[] CachedInvocationList { get; private set; }
-
-
- /*********
- ** Public methods
- *********/
- /// <summary>Get whether anything is listening to the event.</summary>
- public bool HasListeners()
- {
- return this.CachedInvocationList?.Length > 0;
- }
-
- /*********
- ** Protected methods
- *********/
- /// <summary>Construct an instance.</summary>
- /// <param name="eventName">A human-readable name for the event.</param>
- /// <param name="monitor">Writes messages to the log.</param>
- /// <param name="modRegistry">The mod registry with which to identify mods.</param>
- protected ManagedEventBase(string eventName, IMonitor monitor, ModRegistry modRegistry)
- {
- this.EventName = eventName;
- this.Monitor = monitor;
- this.ModRegistry = modRegistry;
- }
-
- /// <summary>Track an event handler.</summary>
- /// <param name="mod">The mod which added the handler.</param>
- /// <param name="handler">The event handler.</param>
- /// <param name="invocationList">The updated event invocation list.</param>
- protected void AddTracking(IModMetadata mod, TEventHandler handler, IEnumerable<TEventHandler> invocationList)
- {
- this.SourceMods[handler] = mod;
- this.CachedInvocationList = invocationList?.ToArray() ?? new TEventHandler[0];
- }
-
- /// <summary>Remove tracking for an event handler.</summary>
- /// <param name="handler">The event handler.</param>
- /// <param name="invocationList">The updated event invocation list.</param>
- protected void RemoveTracking(TEventHandler handler, IEnumerable<TEventHandler> invocationList)
- {
- this.CachedInvocationList = invocationList?.ToArray() ?? new TEventHandler[0];
- if (!this.CachedInvocationList.Contains(handler)) // don't remove if there's still a reference to the removed handler (e.g. it was added twice and removed once)
- this.SourceMods.Remove(handler);
- }
-
- /// <summary>Get the mod which registered the given event handler, if available.</summary>
- /// <param name="handler">The event handler.</param>
- protected IModMetadata GetSourceMod(TEventHandler handler)
- {
- return this.SourceMods.TryGetValue(handler, out IModMetadata mod)
- ? mod
- : null;
- }
-
- /// <summary>Log an exception from an event handler.</summary>
- /// <param name="handler">The event handler instance.</param>
- /// <param name="ex">The exception that was raised.</param>
- protected void LogError(TEventHandler handler, Exception ex)
- {
- IModMetadata mod = this.GetSourceMod(handler);
- if (mod != null)
- mod.LogAsMod($"This mod failed in the {this.EventName} event. Technical details: \n{ex.GetLogSummary()}", LogLevel.Error);
- else
- this.Monitor.Log($"A mod failed in the {this.EventName} event. Technical details: \n{ex.GetLogSummary()}", LogLevel.Error);
- }
- }
-}