summaryrefslogtreecommitdiff
path: root/src/SMAPI/Framework/Events
diff options
context:
space:
mode:
Diffstat (limited to 'src/SMAPI/Framework/Events')
-rw-r--r--src/SMAPI/Framework/Events/EventManager.cs278
-rw-r--r--src/SMAPI/Framework/Events/ManagedEvent.cs105
-rw-r--r--src/SMAPI/Framework/Events/ManagedEventBase.cs93
-rw-r--r--src/SMAPI/Framework/Events/ModEvents.cs6
-rw-r--r--src/SMAPI/Framework/Events/ModGameLoopEvents.cs2
-rw-r--r--src/SMAPI/Framework/Events/ModSpecialisedEvents.cs6
6 files changed, 71 insertions, 419 deletions
diff --git a/src/SMAPI/Framework/Events/EventManager.cs b/src/SMAPI/Framework/Events/EventManager.cs
index 13244601..18b00f69 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
@@ -76,7 +73,7 @@ namespace StardewModdingAPI.Framework.Events
/// <summary>Raised after the game finishes writing data to the save file (except the initial save creation).</summary>
public readonly ManagedEvent<SavedEventArgs> Saved;
- /// <summary>Raised after the player loads a save slot and the world is initialised.</summary>
+ /// <summary>Raised after the player loads a save slot and the world is initialized.</summary>
public readonly ManagedEvent<SaveLoadedEventArgs> SaveLoaded;
/// <summary>Raised after the game begins a new day, including when loading a save.</summary>
@@ -155,208 +152,18 @@ namespace StardewModdingAPI.Framework.Events
public readonly ManagedEvent<TerrainFeatureListChangedEventArgs> TerrainFeatureListChanged;
/****
- ** Specialised
+ ** Specialized
****/
- /// <summary>Raised when the low-level stage in the game's loading process has changed. See notes on <see cref="ISpecialisedEvents.LoadStageChanged"/>.</summary>
+ /// <summary>Raised when the low-level stage in the game's loading process has changed. See notes on <see cref="ISpecializedEvents.LoadStageChanged"/>.</summary>
public readonly ManagedEvent<LoadStageChangedEventArgs> LoadStageChanged;
- /// <summary>Raised before the game performs its overall update tick (≈60 times per second). See notes on <see cref="ISpecialisedEvents.UnvalidatedUpdateTicking"/>.</summary>
+ /// <summary>Raised before the game performs its overall update tick (≈60 times per second). See notes on <see cref="ISpecializedEvents.UnvalidatedUpdateTicking"/>.</summary>
public readonly ManagedEvent<UnvalidatedUpdateTickingEventArgs> UnvalidatedUpdateTicking;
- /// <summary>Raised after the game performs its overall update tick (≈60 times per second). See notes on <see cref="ISpecialisedEvents.UnvalidatedUpdateTicked"/>.</summary>
+ /// <summary>Raised after the game performs its overall update tick (≈60 times per second). See notes on <see cref="ISpecializedEvents.UnvalidatedUpdateTicked"/>.</summary>
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
*********/
@@ -365,11 +172,8 @@ namespace StardewModdingAPI.Framework.Events
/// <param name="modRegistry">The mod registry with which to identify mods.</param>
public EventManager(IMonitor monitor, ModRegistry modRegistry)
{
- // create shortcut initialisers
+ // create shortcut initializers
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));
@@ -419,73 +223,9 @@ namespace StardewModdingAPI.Framework.Events
this.ObjectListChanged = ManageEventOf<ObjectListChangedEventArgs>(nameof(IModEvents.World), nameof(IWorldEvents.ObjectListChanged));
this.TerrainFeatureListChanged = ManageEventOf<TerrainFeatureListChangedEventArgs>(nameof(IModEvents.World), nameof(IWorldEvents.TerrainFeatureListChanged));
- 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
+ this.LoadStageChanged = ManageEventOf<LoadStageChangedEventArgs>(nameof(IModEvents.Specialized), nameof(ISpecializedEvents.LoadStageChanged));
+ this.UnvalidatedUpdateTicking = ManageEventOf<UnvalidatedUpdateTickingEventArgs>(nameof(IModEvents.Specialized), nameof(ISpecializedEvents.UnvalidatedUpdateTicking));
+ this.UnvalidatedUpdateTicked = ManageEventOf<UnvalidatedUpdateTickedEventArgs>(nameof(IModEvents.Specialized), nameof(ISpecializedEvents.UnvalidatedUpdateTicked));
}
}
}
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);
- }
- }
-}
diff --git a/src/SMAPI/Framework/Events/ModEvents.cs b/src/SMAPI/Framework/Events/ModEvents.cs
index 8ad3936c..1d1c92c6 100644
--- a/src/SMAPI/Framework/Events/ModEvents.cs
+++ b/src/SMAPI/Framework/Events/ModEvents.cs
@@ -26,8 +26,8 @@ namespace StardewModdingAPI.Framework.Events
/// <summary>Events raised when something changes in the world.</summary>
public IWorldEvents World { get; }
- /// <summary>Events serving specialised edge cases that shouldn't be used by most mods.</summary>
- public ISpecialisedEvents Specialised { get; }
+ /// <summary>Events serving specialized edge cases that shouldn't be used by most mods.</summary>
+ public ISpecializedEvents Specialized { get; }
/*********
@@ -44,7 +44,7 @@ namespace StardewModdingAPI.Framework.Events
this.Multiplayer = new ModMultiplayerEvents(mod, eventManager);
this.Player = new ModPlayerEvents(mod, eventManager);
this.World = new ModWorldEvents(mod, eventManager);
- this.Specialised = new ModSpecialisedEvents(mod, eventManager);
+ this.Specialized = new ModSpecializedEvents(mod, eventManager);
}
}
}
diff --git a/src/SMAPI/Framework/Events/ModGameLoopEvents.cs b/src/SMAPI/Framework/Events/ModGameLoopEvents.cs
index 0177c22e..c15460fa 100644
--- a/src/SMAPI/Framework/Events/ModGameLoopEvents.cs
+++ b/src/SMAPI/Framework/Events/ModGameLoopEvents.cs
@@ -72,7 +72,7 @@ namespace StardewModdingAPI.Framework.Events
remove => this.EventManager.Saved.Remove(value);
}
- /// <summary>Raised after the player loads a save slot and the world is initialised.</summary>
+ /// <summary>Raised after the player loads a save slot and the world is initialized.</summary>
public event EventHandler<SaveLoadedEventArgs> SaveLoaded
{
add => this.EventManager.SaveLoaded.Add(value);
diff --git a/src/SMAPI/Framework/Events/ModSpecialisedEvents.cs b/src/SMAPI/Framework/Events/ModSpecialisedEvents.cs
index 7c3e9dee..9388bdb2 100644
--- a/src/SMAPI/Framework/Events/ModSpecialisedEvents.cs
+++ b/src/SMAPI/Framework/Events/ModSpecialisedEvents.cs
@@ -3,8 +3,8 @@ using StardewModdingAPI.Events;
namespace StardewModdingAPI.Framework.Events
{
- /// <summary>Events serving specialised edge cases that shouldn't be used by most mods.</summary>
- internal class ModSpecialisedEvents : ModEventsBase, ISpecialisedEvents
+ /// <summary>Events serving specialized edge cases that shouldn't be used by most mods.</summary>
+ internal class ModSpecializedEvents : ModEventsBase, ISpecializedEvents
{
/*********
** Accessors
@@ -37,7 +37,7 @@ namespace StardewModdingAPI.Framework.Events
/// <summary>Construct an instance.</summary>
/// <param name="mod">The mod which uses this instance.</param>
/// <param name="eventManager">The underlying event manager.</param>
- internal ModSpecialisedEvents(IModMetadata mod, EventManager eventManager)
+ internal ModSpecializedEvents(IModMetadata mod, EventManager eventManager)
: base(mod, eventManager) { }
}
}