From 4da9e954df3846d01aa0536f4e8143466a1d62f3 Mon Sep 17 00:00:00 2001 From: Jesse Plamondon-Willard Date: Fri, 11 Feb 2022 00:49:49 -0500 Subject: use Array.Empty to avoid unneeded array allocations --- src/SMAPI/Framework/Events/ManagedEvent.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/SMAPI/Framework/Events') diff --git a/src/SMAPI/Framework/Events/ManagedEvent.cs b/src/SMAPI/Framework/Events/ManagedEvent.cs index fa20a079..f48c3aeb 100644 --- a/src/SMAPI/Framework/Events/ManagedEvent.cs +++ b/src/SMAPI/Framework/Events/ManagedEvent.cs @@ -21,7 +21,7 @@ namespace StardewModdingAPI.Framework.Events private readonly List> Handlers = new List>(); /// A cached snapshot of , or null to rebuild it next raise. - private ManagedEventHandler[] CachedHandlers = new ManagedEventHandler[0]; + private ManagedEventHandler[] CachedHandlers = Array.Empty>(); /// The total number of event handlers registered for this events, regardless of whether they're still registered. private int RegistrationIndex; -- cgit From d3fbdf484a4d90365a55fb5058d75a8623f17d0f Mon Sep 17 00:00:00 2001 From: Jesse Plamondon-Willard Date: Tue, 22 Mar 2022 20:46:21 -0400 Subject: reduce duplicated doc blocks --- src/SMAPI/Framework/Events/EventManager.cs | 105 ++++++++++----------- src/SMAPI/Framework/Events/ManagedEvent.cs | 4 +- src/SMAPI/Framework/Events/ManagedEventHandler.cs | 4 +- src/SMAPI/Framework/Events/ModDisplayEvents.cs | 23 +++-- src/SMAPI/Framework/Events/ModEvents.cs | 16 ++-- src/SMAPI/Framework/Events/ModGameLoopEvents.cs | 30 +++--- src/SMAPI/Framework/Events/ModInputEvents.cs | 12 +-- src/SMAPI/Framework/Events/ModMultiplayerEvents.cs | 10 +- src/SMAPI/Framework/Events/ModPlayerEvents.cs | 8 +- src/SMAPI/Framework/Events/ModSpecialisedEvents.cs | 8 +- src/SMAPI/Framework/Events/ModWorldEvents.cs | 20 ++-- 11 files changed, 114 insertions(+), 126 deletions(-) (limited to 'src/SMAPI/Framework/Events') diff --git a/src/SMAPI/Framework/Events/EventManager.cs b/src/SMAPI/Framework/Events/EventManager.cs index dfc289ed..fa4d564d 100644 --- a/src/SMAPI/Framework/Events/EventManager.cs +++ b/src/SMAPI/Framework/Events/EventManager.cs @@ -1,6 +1,4 @@ -using System.Collections.Generic; using System.Diagnostics.CodeAnalysis; -using System.Reflection; using StardewModdingAPI.Events; namespace StardewModdingAPI.Framework.Events @@ -15,166 +13,166 @@ namespace StardewModdingAPI.Framework.Events /**** ** Display ****/ - /// Raised after a game menu is opened, closed, or replaced. + /// public readonly ManagedEvent MenuChanged; - /// Raised before the game draws anything to the screen in a draw tick, as soon as the sprite batch is opened. The sprite batch may be closed and reopened multiple times after this event is called, but it's only raised once per draw tick. This event isn't useful for drawing to the screen, since the game will draw over it. + /// public readonly ManagedEvent Rendering; - /// Raised after the game draws to the sprite patch in a draw tick, just before the final sprite batch is rendered to the screen. Since the game may open/close the sprite batch multiple times in a draw tick, the sprite batch may not contain everything being drawn and some things may already be rendered to the screen. Content drawn to the sprite batch at this point will be drawn over all vanilla content (including menus, HUD, and cursor). + /// public readonly ManagedEvent Rendered; - /// Raised before the game world is drawn to the screen. + /// public readonly ManagedEvent RenderingWorld; - /// Raised after the game world is drawn to the sprite patch, before it's rendered to the screen. + /// public readonly ManagedEvent RenderedWorld; - /// When a menu is open ( isn't null), raised before that menu is drawn to the screen. + /// public readonly ManagedEvent RenderingActiveMenu; - /// When a menu is open ( isn't null), raised after that menu is drawn to the sprite batch but before it's rendered to the screen. + /// public readonly ManagedEvent RenderedActiveMenu; - /// Raised before drawing the HUD (item toolbar, clock, etc) to the screen. + /// public readonly ManagedEvent RenderingHud; - /// Raised after drawing the HUD (item toolbar, clock, etc) to the sprite batch, but before it's rendered to the screen. + /// public readonly ManagedEvent RenderedHud; - /// Raised after the game window is resized. + /// public readonly ManagedEvent WindowResized; /**** ** Game loop ****/ - /// Raised after the game is launched, right before the first update tick. + /// public readonly ManagedEvent GameLaunched; - /// Raised before the game performs its overall update tick (≈60 times per second). + /// public readonly ManagedEvent UpdateTicking; - /// Raised after the game performs its overall update tick (≈60 times per second). + /// public readonly ManagedEvent UpdateTicked; - /// Raised once per second before the game performs its overall update tick. + /// public readonly ManagedEvent OneSecondUpdateTicking; - /// Raised once per second after the game performs its overall update tick. + /// public readonly ManagedEvent OneSecondUpdateTicked; - /// Raised before the game creates the save file. + /// public readonly ManagedEvent SaveCreating; - /// Raised after the game finishes creating the save file. + /// public readonly ManagedEvent SaveCreated; - /// Raised before the game begins writes data to the save file (except the initial save creation). + /// public readonly ManagedEvent Saving; - /// Raised after the game finishes writing data to the save file (except the initial save creation). + /// public readonly ManagedEvent Saved; - /// Raised after the player loads a save slot and the world is initialized. + /// public readonly ManagedEvent SaveLoaded; - /// Raised after the game begins a new day, including when loading a save. + /// public readonly ManagedEvent DayStarted; - /// Raised before the game ends the current day. This happens before it starts setting up the next day and before . + /// public readonly ManagedEvent DayEnding; - /// Raised after the in-game clock time changes. + /// public readonly ManagedEvent TimeChanged; - /// Raised after the game returns to the title screen. + /// public readonly ManagedEvent ReturnedToTitle; /**** ** Input ****/ - /// Raised after the player presses or releases any buttons on the keyboard, controller, or mouse. + /// public readonly ManagedEvent ButtonsChanged; - /// Raised after the player presses a button on the keyboard, controller, or mouse. + /// public readonly ManagedEvent ButtonPressed; - /// Raised after the player released a button on the keyboard, controller, or mouse. + /// public readonly ManagedEvent ButtonReleased; - /// Raised after the player moves the in-game cursor. + /// public readonly ManagedEvent CursorMoved; - /// Raised after the player scrolls the mouse wheel. + /// public readonly ManagedEvent MouseWheelScrolled; /**** ** Multiplayer ****/ - /// Raised after the mod context for a peer is received. This happens before the game approves the connection (), so the player doesn't yet exist in the game. This is the earliest point where messages can be sent to the peer via SMAPI. + /// public readonly ManagedEvent PeerContextReceived; - /// Raised after a peer connection is approved by the game. + /// public readonly ManagedEvent PeerConnected; - /// Raised after a mod message is received over the network. + /// public readonly ManagedEvent ModMessageReceived; - /// Raised after the connection with a peer is severed. + /// public readonly ManagedEvent PeerDisconnected; /**** ** Player ****/ - /// Raised after items are added or removed to a player's inventory. + /// public readonly ManagedEvent InventoryChanged; - /// Raised after a player skill level changes. This happens as soon as they level up, not when the game notifies the player after their character goes to bed. + /// public readonly ManagedEvent LevelChanged; - /// Raised after a player warps to a new location. + /// public readonly ManagedEvent Warped; /**** ** World ****/ - /// Raised after a game location is added or removed. + /// public readonly ManagedEvent LocationListChanged; - /// Raised after buildings are added or removed in a location. + /// public readonly ManagedEvent BuildingListChanged; - /// Raised after debris are added or removed in a location. + /// public readonly ManagedEvent DebrisListChanged; - /// Raised after large terrain features (like bushes) are added or removed in a location. + /// public readonly ManagedEvent LargeTerrainFeatureListChanged; - /// Raised after NPCs are added or removed in a location. + /// public readonly ManagedEvent NpcListChanged; - /// Raised after objects are added or removed in a location. + /// public readonly ManagedEvent ObjectListChanged; - /// Raised after items are added or removed from a chest. + /// public readonly ManagedEvent ChestInventoryChanged; - /// Raised after terrain features (like floors and trees) are added or removed in a location. + /// public readonly ManagedEvent TerrainFeatureListChanged; - /// Raised after furniture are added or removed in a location. + /// public readonly ManagedEvent FurnitureListChanged; /**** ** Specialized ****/ - /// Raised when the low-level stage in the game's loading process has changed. See notes on . + /// public readonly ManagedEvent LoadStageChanged; - /// Raised before the game performs its overall update tick (≈60 times per second). See notes on . + /// public readonly ManagedEvent UnvalidatedUpdateTicking; - /// Raised after the game performs its overall update tick (≈60 times per second). See notes on . + /// public readonly ManagedEvent UnvalidatedUpdateTicked; @@ -247,12 +245,5 @@ namespace StardewModdingAPI.Framework.Events this.UnvalidatedUpdateTicking = ManageEventOf(nameof(IModEvents.Specialized), nameof(ISpecializedEvents.UnvalidatedUpdateTicking), isPerformanceCritical: true); this.UnvalidatedUpdateTicked = ManageEventOf(nameof(IModEvents.Specialized), nameof(ISpecializedEvents.UnvalidatedUpdateTicked), isPerformanceCritical: true); } - - /// Get all managed events. - public IEnumerable GetAllEvents() - { - foreach (FieldInfo field in this.GetType().GetFields()) - yield return (IManagedEvent)field.GetValue(this); - } } } diff --git a/src/SMAPI/Framework/Events/ManagedEvent.cs b/src/SMAPI/Framework/Events/ManagedEvent.cs index f48c3aeb..a200393d 100644 --- a/src/SMAPI/Framework/Events/ManagedEvent.cs +++ b/src/SMAPI/Framework/Events/ManagedEvent.cs @@ -33,10 +33,10 @@ namespace StardewModdingAPI.Framework.Events /********* ** Accessors *********/ - /// A human-readable name for the event. + /// public string EventName { get; } - /// Whether the event is typically called at least once per second. + /// public bool IsPerformanceCritical { get; } diff --git a/src/SMAPI/Framework/Events/ManagedEventHandler.cs b/src/SMAPI/Framework/Events/ManagedEventHandler.cs index b32a2a22..28e88be0 100644 --- a/src/SMAPI/Framework/Events/ManagedEventHandler.cs +++ b/src/SMAPI/Framework/Events/ManagedEventHandler.cs @@ -39,9 +39,7 @@ namespace StardewModdingAPI.Framework.Events this.SourceMod = sourceMod; } - /// Compares the current instance with another object of the same type and returns an integer that indicates whether the current instance precedes, follows, or occurs in the same position in the sort order as the other object. - /// An object to compare with this instance. - /// is not the same type as this instance. + /// public int CompareTo(object obj) { if (!(obj is ManagedEventHandler other)) diff --git a/src/SMAPI/Framework/Events/ModDisplayEvents.cs b/src/SMAPI/Framework/Events/ModDisplayEvents.cs index 54d40dee..48f55324 100644 --- a/src/SMAPI/Framework/Events/ModDisplayEvents.cs +++ b/src/SMAPI/Framework/Events/ModDisplayEvents.cs @@ -1,79 +1,78 @@ using System; using StardewModdingAPI.Events; -using StardewValley; namespace StardewModdingAPI.Framework.Events { - /// Events related to UI and drawing to the screen. + /// internal class ModDisplayEvents : ModEventsBase, IDisplayEvents { /********* ** Accessors *********/ - /// Raised after a game menu is opened, closed, or replaced. + /// public event EventHandler MenuChanged { add => this.EventManager.MenuChanged.Add(value, this.Mod); remove => this.EventManager.MenuChanged.Remove(value); } - /// Raised before the game draws anything to the screen in a draw tick, as soon as the sprite batch is opened. The sprite batch may be closed and reopened multiple times after this event is called, but it's only raised once per draw tick. This event isn't useful for drawing to the screen, since the game will draw over it. + /// public event EventHandler Rendering { add => this.EventManager.Rendering.Add(value, this.Mod); remove => this.EventManager.Rendering.Remove(value); } - /// Raised after the game draws to the sprite patch in a draw tick, just before the final sprite batch is rendered to the screen. Since the game may open/close the sprite batch multiple times in a draw tick, the sprite batch may not contain everything being drawn and some things may already be rendered to the screen. Content drawn to the sprite batch at this point will be drawn over all vanilla content (including menus, HUD, and cursor). + /// public event EventHandler Rendered { add => this.EventManager.Rendered.Add(value, this.Mod); remove => this.EventManager.Rendered.Remove(value); } - /// Raised before the game world is drawn to the screen. This event isn't useful for drawing to the screen, since the game will draw over it. + /// public event EventHandler RenderingWorld { add => this.EventManager.RenderingWorld.Add(value, this.Mod); remove => this.EventManager.RenderingWorld.Remove(value); } - /// Raised after the game world is drawn to the sprite patch, before it's rendered to the screen. Content drawn to the sprite batch at this point will be drawn over the world, but under any active menu, HUD elements, or cursor. + /// public event EventHandler RenderedWorld { add => this.EventManager.RenderedWorld.Add(value, this.Mod); remove => this.EventManager.RenderedWorld.Remove(value); } - /// When a menu is open ( isn't null), raised before that menu is drawn to the screen. This includes the game's internal menus like the title screen. Content drawn to the sprite batch at this point will appear under the menu. + /// public event EventHandler RenderingActiveMenu { add => this.EventManager.RenderingActiveMenu.Add(value, this.Mod); remove => this.EventManager.RenderingActiveMenu.Remove(value); } - /// When a menu is open ( isn't null), raised after that menu is drawn to the sprite batch but before it's rendered to the screen. Content drawn to the sprite batch at this point will appear over the menu and menu cursor. + /// public event EventHandler RenderedActiveMenu { add => this.EventManager.RenderedActiveMenu.Add(value, this.Mod); remove => this.EventManager.RenderedActiveMenu.Remove(value); } - /// Raised before drawing the HUD (item toolbar, clock, etc) to the screen. The vanilla HUD may be hidden at this point (e.g. because a menu is open). Content drawn to the sprite batch at this point will appear under the HUD. + /// public event EventHandler RenderingHud { add => this.EventManager.RenderingHud.Add(value, this.Mod); remove => this.EventManager.RenderingHud.Remove(value); } - /// Raised after drawing the HUD (item toolbar, clock, etc) to the sprite batch, but before it's rendered to the screen. The vanilla HUD may be hidden at this point (e.g. because a menu is open). Content drawn to the sprite batch at this point will appear over the HUD. + /// public event EventHandler RenderedHud { add => this.EventManager.RenderedHud.Add(value, this.Mod); remove => this.EventManager.RenderedHud.Remove(value); } - /// Raised after the game window is resized. + /// public event EventHandler WindowResized { add => this.EventManager.WindowResized.Add(value, this.Mod); diff --git a/src/SMAPI/Framework/Events/ModEvents.cs b/src/SMAPI/Framework/Events/ModEvents.cs index 1d1c92c6..0c365d42 100644 --- a/src/SMAPI/Framework/Events/ModEvents.cs +++ b/src/SMAPI/Framework/Events/ModEvents.cs @@ -2,31 +2,31 @@ using StardewModdingAPI.Events; namespace StardewModdingAPI.Framework.Events { - /// Manages access to events raised by SMAPI. + /// internal class ModEvents : IModEvents { /********* ** Accessors *********/ - /// Events related to UI and drawing to the screen. + /// public IDisplayEvents Display { get; } - /// Events linked to the game's update loop. The update loop runs roughly ≈60 times/second to run game logic like state changes, action handling, etc. These can be useful, but you should consider more semantic events like if possible. + /// public IGameLoopEvents GameLoop { get; } - /// Events raised when the player provides input using a controller, keyboard, or mouse. + /// public IInputEvents Input { get; } - /// Events raised for multiplayer messages and connections. + /// public IMultiplayerEvents Multiplayer { get; } - /// Events raised when the player data changes. + /// public IPlayerEvents Player { get; } - /// Events raised when something changes in the world. + /// public IWorldEvents World { get; } - /// Events serving specialized edge cases that shouldn't be used by most mods. + /// public ISpecializedEvents Specialized { get; } diff --git a/src/SMAPI/Framework/Events/ModGameLoopEvents.cs b/src/SMAPI/Framework/Events/ModGameLoopEvents.cs index 1150d641..5f0db369 100644 --- a/src/SMAPI/Framework/Events/ModGameLoopEvents.cs +++ b/src/SMAPI/Framework/Events/ModGameLoopEvents.cs @@ -3,104 +3,104 @@ using StardewModdingAPI.Events; namespace StardewModdingAPI.Framework.Events { - /// Events linked to the game's update loop. The update loop runs roughly ≈60 times/second to run game logic like state changes, action handling, etc. These can be useful, but you should consider more semantic events like if possible. + /// internal class ModGameLoopEvents : ModEventsBase, IGameLoopEvents { /********* ** Accessors *********/ - /// Raised after the game is launched, right before the first update tick. + /// public event EventHandler GameLaunched { add => this.EventManager.GameLaunched.Add(value, this.Mod); remove => this.EventManager.GameLaunched.Remove(value); } - /// Raised before the game performs its overall update tick (≈60 times per second). + /// public event EventHandler UpdateTicking { add => this.EventManager.UpdateTicking.Add(value, this.Mod); remove => this.EventManager.UpdateTicking.Remove(value); } - /// Raised after the game performs its overall update tick (≈60 times per second). + /// public event EventHandler UpdateTicked { add => this.EventManager.UpdateTicked.Add(value, this.Mod); remove => this.EventManager.UpdateTicked.Remove(value); } - /// Raised once per second before the game state is updated. + /// public event EventHandler OneSecondUpdateTicking { add => this.EventManager.OneSecondUpdateTicking.Add(value, this.Mod); remove => this.EventManager.OneSecondUpdateTicking.Remove(value); } - /// Raised once per second after the game state is updated. + /// public event EventHandler OneSecondUpdateTicked { add => this.EventManager.OneSecondUpdateTicked.Add(value, this.Mod); remove => this.EventManager.OneSecondUpdateTicked.Remove(value); } - /// Raised before the game creates a new save file. + /// public event EventHandler SaveCreating { add => this.EventManager.SaveCreating.Add(value, this.Mod); remove => this.EventManager.SaveCreating.Remove(value); } - /// Raised after the game finishes creating the save file. + /// public event EventHandler SaveCreated { add => this.EventManager.SaveCreated.Add(value, this.Mod); remove => this.EventManager.SaveCreated.Remove(value); } - /// Raised before the game begins writes data to the save file. + /// public event EventHandler Saving { add => this.EventManager.Saving.Add(value, this.Mod); remove => this.EventManager.Saving.Remove(value); } - /// Raised after the game finishes writing data to the save file. + /// public event EventHandler Saved { add => this.EventManager.Saved.Add(value, this.Mod); remove => this.EventManager.Saved.Remove(value); } - /// Raised after the player loads a save slot and the world is initialized. + /// public event EventHandler SaveLoaded { add => this.EventManager.SaveLoaded.Add(value, this.Mod); remove => this.EventManager.SaveLoaded.Remove(value); } - /// Raised after the game begins a new day (including when the player loads a save). + /// public event EventHandler DayStarted { add => this.EventManager.DayStarted.Add(value, this.Mod); remove => this.EventManager.DayStarted.Remove(value); } - /// Raised before the game ends the current day. This happens before it starts setting up the next day and before . + /// public event EventHandler DayEnding { add => this.EventManager.DayEnding.Add(value, this.Mod); remove => this.EventManager.DayEnding.Remove(value); } - /// Raised after the in-game clock time changes. + /// public event EventHandler TimeChanged { add => this.EventManager.TimeChanged.Add(value, this.Mod); remove => this.EventManager.TimeChanged.Remove(value); } - /// Raised after the game returns to the title screen. + /// public event EventHandler ReturnedToTitle { add => this.EventManager.ReturnedToTitle.Add(value, this.Mod); diff --git a/src/SMAPI/Framework/Events/ModInputEvents.cs b/src/SMAPI/Framework/Events/ModInputEvents.cs index 6f423e5d..40edf806 100644 --- a/src/SMAPI/Framework/Events/ModInputEvents.cs +++ b/src/SMAPI/Framework/Events/ModInputEvents.cs @@ -3,41 +3,41 @@ using StardewModdingAPI.Events; namespace StardewModdingAPI.Framework.Events { - /// Events raised when the player provides input using a controller, keyboard, or mouse. + /// internal class ModInputEvents : ModEventsBase, IInputEvents { /********* ** Accessors *********/ - /// Raised after the player presses or releases any buttons on the keyboard, controller, or mouse. + /// public event EventHandler ButtonsChanged { add => this.EventManager.ButtonsChanged.Add(value, this.Mod); remove => this.EventManager.ButtonsChanged.Remove(value); } - /// Raised after the player presses a button on the keyboard, controller, or mouse. + /// public event EventHandler ButtonPressed { add => this.EventManager.ButtonPressed.Add(value, this.Mod); remove => this.EventManager.ButtonPressed.Remove(value); } - /// Raised after the player releases a button on the keyboard, controller, or mouse. + /// public event EventHandler ButtonReleased { add => this.EventManager.ButtonReleased.Add(value, this.Mod); remove => this.EventManager.ButtonReleased.Remove(value); } - /// Raised after the player moves the in-game cursor. + /// public event EventHandler CursorMoved { add => this.EventManager.CursorMoved.Add(value, this.Mod); remove => this.EventManager.CursorMoved.Remove(value); } - /// Raised after the player scrolls the mouse wheel. + /// public event EventHandler MouseWheelScrolled { add => this.EventManager.MouseWheelScrolled.Add(value, this.Mod); diff --git a/src/SMAPI/Framework/Events/ModMultiplayerEvents.cs b/src/SMAPI/Framework/Events/ModMultiplayerEvents.cs index 2f9b9482..b90f64fa 100644 --- a/src/SMAPI/Framework/Events/ModMultiplayerEvents.cs +++ b/src/SMAPI/Framework/Events/ModMultiplayerEvents.cs @@ -3,34 +3,34 @@ using StardewModdingAPI.Events; namespace StardewModdingAPI.Framework.Events { - /// Events raised for multiplayer messages and connections. + /// internal class ModMultiplayerEvents : ModEventsBase, IMultiplayerEvents { /********* ** Accessors *********/ - /// Raised after the mod context for a peer is received. This happens before the game approves the connection (), so the player doesn't yet exist in the game. This is the earliest point where messages can be sent to the peer via SMAPI. + /// public event EventHandler PeerContextReceived { add => this.EventManager.PeerContextReceived.Add(value, this.Mod); remove => this.EventManager.PeerContextReceived.Remove(value); } - /// Raised after a peer connection is approved by the game. + /// public event EventHandler PeerConnected { add => this.EventManager.PeerConnected.Add(value, this.Mod); remove => this.EventManager.PeerConnected.Remove(value); } - /// Raised after a mod message is received over the network. + /// public event EventHandler ModMessageReceived { add => this.EventManager.ModMessageReceived.Add(value, this.Mod); remove => this.EventManager.ModMessageReceived.Remove(value); } - /// Raised after the connection with a peer is severed. + /// public event EventHandler PeerDisconnected { add => this.EventManager.PeerDisconnected.Add(value, this.Mod); diff --git a/src/SMAPI/Framework/Events/ModPlayerEvents.cs b/src/SMAPI/Framework/Events/ModPlayerEvents.cs index 240beb8d..b2d89e9a 100644 --- a/src/SMAPI/Framework/Events/ModPlayerEvents.cs +++ b/src/SMAPI/Framework/Events/ModPlayerEvents.cs @@ -3,27 +3,27 @@ using StardewModdingAPI.Events; namespace StardewModdingAPI.Framework.Events { - /// Events raised when the player data changes. + /// internal class ModPlayerEvents : ModEventsBase, IPlayerEvents { /********* ** Accessors *********/ - /// Raised after items are added or removed to a player's inventory. NOTE: this event is currently only raised for the local player. + /// public event EventHandler InventoryChanged { add => this.EventManager.InventoryChanged.Add(value, this.Mod); remove => this.EventManager.InventoryChanged.Remove(value); } - /// Raised after a player skill level changes. This happens as soon as they level up, not when the game notifies the player after their character goes to bed. NOTE: this event is currently only raised for the local player. + /// public event EventHandler LevelChanged { add => this.EventManager.LevelChanged.Add(value, this.Mod); remove => this.EventManager.LevelChanged.Remove(value); } - /// Raised after a player warps to a new location. NOTE: this event is currently only raised for the local player. + /// public event EventHandler Warped { add => this.EventManager.Warped.Add(value, this.Mod); diff --git a/src/SMAPI/Framework/Events/ModSpecialisedEvents.cs b/src/SMAPI/Framework/Events/ModSpecialisedEvents.cs index 1d6788e1..7980208b 100644 --- a/src/SMAPI/Framework/Events/ModSpecialisedEvents.cs +++ b/src/SMAPI/Framework/Events/ModSpecialisedEvents.cs @@ -3,27 +3,27 @@ using StardewModdingAPI.Events; namespace StardewModdingAPI.Framework.Events { - /// Events serving specialized edge cases that shouldn't be used by most mods. + /// internal class ModSpecializedEvents : ModEventsBase, ISpecializedEvents { /********* ** Accessors *********/ - /// Raised when the low-level stage in the game's loading process has changed. This is an advanced event for mods which need to run code at specific points in the loading process. The available stages or when they happen might change without warning in future versions (e.g. due to changes in the game's load process), so mods using this event are more likely to break or have bugs. Most mods should use instead. + /// public event EventHandler LoadStageChanged { add => this.EventManager.LoadStageChanged.Add(value, this.Mod); remove => this.EventManager.LoadStageChanged.Remove(value); } - /// Raised before the game state is updated (≈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 event will trigger a stability warning in the SMAPI console. + /// public event EventHandler UnvalidatedUpdateTicking { add => this.EventManager.UnvalidatedUpdateTicking.Add(value, this.Mod); remove => this.EventManager.UnvalidatedUpdateTicking.Remove(value); } - /// Raised after the game state is updated (≈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 event will trigger a stability warning in the SMAPI console. + /// public event EventHandler UnvalidatedUpdateTicked { add => this.EventManager.UnvalidatedUpdateTicked.Add(value, this.Mod); diff --git a/src/SMAPI/Framework/Events/ModWorldEvents.cs b/src/SMAPI/Framework/Events/ModWorldEvents.cs index f4c40abc..a7b7d799 100644 --- a/src/SMAPI/Framework/Events/ModWorldEvents.cs +++ b/src/SMAPI/Framework/Events/ModWorldEvents.cs @@ -3,69 +3,69 @@ using StardewModdingAPI.Events; namespace StardewModdingAPI.Framework.Events { - /// Events raised when something changes in the world. + /// internal class ModWorldEvents : ModEventsBase, IWorldEvents { /********* ** Accessors *********/ - /// Raised after a game location is added or removed. + /// public event EventHandler LocationListChanged { add => this.EventManager.LocationListChanged.Add(value, this.Mod); remove => this.EventManager.LocationListChanged.Remove(value); } - /// Raised after buildings are added or removed in a location. + /// public event EventHandler BuildingListChanged { add => this.EventManager.BuildingListChanged.Add(value, this.Mod); remove => this.EventManager.BuildingListChanged.Remove(value); } - /// Raised after debris are added or removed in a location. + /// public event EventHandler DebrisListChanged { add => this.EventManager.DebrisListChanged.Add(value, this.Mod); remove => this.EventManager.DebrisListChanged.Remove(value); } - /// Raised after large terrain features (like bushes) are added or removed in a location. + /// public event EventHandler LargeTerrainFeatureListChanged { add => this.EventManager.LargeTerrainFeatureListChanged.Add(value, this.Mod); remove => this.EventManager.LargeTerrainFeatureListChanged.Remove(value); } - /// Raised after NPCs are added or removed in a location. + /// public event EventHandler NpcListChanged { add => this.EventManager.NpcListChanged.Add(value, this.Mod); remove => this.EventManager.NpcListChanged.Remove(value); } - /// Raised after objects are added or removed in a location. + /// public event EventHandler ObjectListChanged { add => this.EventManager.ObjectListChanged.Add(value, this.Mod); remove => this.EventManager.ObjectListChanged.Remove(value); } - /// Raised after items are added or removed from a chest. + /// public event EventHandler ChestInventoryChanged { add => this.EventManager.ChestInventoryChanged.Add(value, this.Mod); remove => this.EventManager.ChestInventoryChanged.Remove(value); } - /// Raised after terrain features (like floors and trees) are added or removed in a location. + /// public event EventHandler TerrainFeatureListChanged { add => this.EventManager.TerrainFeatureListChanged.Add(value, this.Mod); remove => this.EventManager.TerrainFeatureListChanged.Remove(value); } - /// Raised after furniture are added or removed in a location. + /// public event EventHandler FurnitureListChanged { add => this.EventManager.FurnitureListChanged.Add(value, this.Mod); -- cgit From 584725bb8e554e314843315facca1fd15868bee4 Mon Sep 17 00:00:00 2001 From: Jesse Plamondon-Willard Date: Wed, 23 Mar 2022 01:06:11 -0400 Subject: add initial AssetRequested content event (#766) --- src/SMAPI/Framework/Events/EventManager.cs | 11 +++++++++- src/SMAPI/Framework/Events/ManagedEvent.cs | 10 ++++++++- src/SMAPI/Framework/Events/ModContentEvents.cs | 29 ++++++++++++++++++++++++++ src/SMAPI/Framework/Events/ModEvents.cs | 4 ++++ 4 files changed, 52 insertions(+), 2 deletions(-) create mode 100644 src/SMAPI/Framework/Events/ModContentEvents.cs (limited to 'src/SMAPI/Framework/Events') diff --git a/src/SMAPI/Framework/Events/EventManager.cs b/src/SMAPI/Framework/Events/EventManager.cs index fa4d564d..8142f00e 100644 --- a/src/SMAPI/Framework/Events/EventManager.cs +++ b/src/SMAPI/Framework/Events/EventManager.cs @@ -10,6 +10,13 @@ namespace StardewModdingAPI.Framework.Events /********* ** Events *********/ + /**** + ** Content + ****/ + /// + public readonly ManagedEvent AssetRequested; + + /**** ** Display ****/ @@ -189,7 +196,9 @@ namespace StardewModdingAPI.Framework.Events return new ManagedEvent($"{typeName}.{eventName}", modRegistry, isPerformanceCritical); } - // init events (new) + // init events + this.AssetRequested = ManageEventOf(nameof(IModEvents.Content), nameof(IContentEvents.AssetRequested)); + this.MenuChanged = ManageEventOf(nameof(IModEvents.Display), nameof(IDisplayEvents.MenuChanged)); this.Rendering = ManageEventOf(nameof(IModEvents.Display), nameof(IDisplayEvents.Rendering), isPerformanceCritical: true); this.Rendered = ManageEventOf(nameof(IModEvents.Display), nameof(IDisplayEvents.Rendered), isPerformanceCritical: true); diff --git a/src/SMAPI/Framework/Events/ManagedEvent.cs b/src/SMAPI/Framework/Events/ManagedEvent.cs index a200393d..154ef659 100644 --- a/src/SMAPI/Framework/Events/ManagedEvent.cs +++ b/src/SMAPI/Framework/Events/ManagedEvent.cs @@ -99,6 +99,14 @@ namespace StardewModdingAPI.Framework.Events /// The event arguments to pass. /// A lambda which returns true if the event should be raised for the given mod. public void Raise(TEventArgs args, Func match = null) + { + this.Raise((_, invoke) => invoke(args), match); + } + + /// Raise the event and notify all handlers. + /// Invoke an event handler. This receives the mod which registered the handler, and should invoke the callback with the event arguments to pass it. + /// A lambda which returns true if the event should be raised for the given mod. + public void Raise(Action> invoke, Func match = null) { // skip if no handlers if (this.Handlers.Count == 0) @@ -128,7 +136,7 @@ namespace StardewModdingAPI.Framework.Events try { - handler.Handler.Invoke(null, args); + invoke(handler.SourceMod, args => handler.Handler.Invoke(null, args)); } catch (Exception ex) { diff --git a/src/SMAPI/Framework/Events/ModContentEvents.cs b/src/SMAPI/Framework/Events/ModContentEvents.cs new file mode 100644 index 00000000..b4d4279c --- /dev/null +++ b/src/SMAPI/Framework/Events/ModContentEvents.cs @@ -0,0 +1,29 @@ +using System; +using StardewModdingAPI.Events; + +namespace StardewModdingAPI.Framework.Events +{ + /// + internal class ModContentEvents : ModEventsBase, IContentEvents + { + /********* + ** Accessors + *********/ + /// + public event EventHandler AssetRequested + { + add => this.EventManager.AssetRequested.Add(value, this.Mod); + remove => this.EventManager.AssetRequested.Remove(value); + } + + + /********* + ** Public methods + *********/ + /// Construct an instance. + /// The mod which uses this instance. + /// The underlying event manager. + internal ModContentEvents(IModMetadata mod, EventManager eventManager) + : base(mod, eventManager) { } + } +} diff --git a/src/SMAPI/Framework/Events/ModEvents.cs b/src/SMAPI/Framework/Events/ModEvents.cs index 0c365d42..1fb3482c 100644 --- a/src/SMAPI/Framework/Events/ModEvents.cs +++ b/src/SMAPI/Framework/Events/ModEvents.cs @@ -8,6 +8,9 @@ namespace StardewModdingAPI.Framework.Events /********* ** Accessors *********/ + /// + public IContentEvents Content { get; } + /// public IDisplayEvents Display { get; } @@ -38,6 +41,7 @@ namespace StardewModdingAPI.Framework.Events /// The underlying event manager. public ModEvents(IModMetadata mod, EventManager eventManager) { + this.Content = new ModContentEvents(mod, eventManager); this.Display = new ModDisplayEvents(mod, eventManager); this.GameLoop = new ModGameLoopEvents(mod, eventManager); this.Input = new ModInputEvents(mod, eventManager); -- cgit From 2b0ce2bb4d6690b7d00da0a243855db9bffffbf0 Mon Sep 17 00:00:00 2001 From: Jesse Plamondon-Willard Date: Thu, 24 Mar 2022 22:55:55 -0400 Subject: add AssetInvalidated content event (#766) --- src/SMAPI/Framework/Events/EventManager.cs | 4 ++++ src/SMAPI/Framework/Events/ModContentEvents.cs | 7 +++++++ 2 files changed, 11 insertions(+) (limited to 'src/SMAPI/Framework/Events') diff --git a/src/SMAPI/Framework/Events/EventManager.cs b/src/SMAPI/Framework/Events/EventManager.cs index 8142f00e..96582380 100644 --- a/src/SMAPI/Framework/Events/EventManager.cs +++ b/src/SMAPI/Framework/Events/EventManager.cs @@ -16,6 +16,9 @@ namespace StardewModdingAPI.Framework.Events /// public readonly ManagedEvent AssetRequested; + /// + public readonly ManagedEvent AssetsInvalidated; + /**** ** Display @@ -198,6 +201,7 @@ namespace StardewModdingAPI.Framework.Events // init events this.AssetRequested = ManageEventOf(nameof(IModEvents.Content), nameof(IContentEvents.AssetRequested)); + this.AssetsInvalidated = ManageEventOf(nameof(IModEvents.Content), nameof(IContentEvents.AssetsInvalidated)); this.MenuChanged = ManageEventOf(nameof(IModEvents.Display), nameof(IDisplayEvents.MenuChanged)); this.Rendering = ManageEventOf(nameof(IModEvents.Display), nameof(IDisplayEvents.Rendering), isPerformanceCritical: true); diff --git a/src/SMAPI/Framework/Events/ModContentEvents.cs b/src/SMAPI/Framework/Events/ModContentEvents.cs index b4d4279c..4d0cfb97 100644 --- a/src/SMAPI/Framework/Events/ModContentEvents.cs +++ b/src/SMAPI/Framework/Events/ModContentEvents.cs @@ -16,6 +16,13 @@ namespace StardewModdingAPI.Framework.Events remove => this.EventManager.AssetRequested.Remove(value); } + /// + public event EventHandler AssetsInvalidated + { + add => this.EventManager.AssetsInvalidated.Add(value, this.Mod); + remove => this.EventManager.AssetsInvalidated.Remove(value); + } + /********* ** Public methods -- cgit From b77eab6e0a09099998aa806302694e82216e79f8 Mon Sep 17 00:00:00 2001 From: Jesse Plamondon-Willard Date: Fri, 25 Mar 2022 00:35:31 -0400 Subject: add AssetReady content event (#766) --- src/SMAPI/Framework/Events/EventManager.cs | 6 ++++-- src/SMAPI/Framework/Events/ModContentEvents.cs | 7 +++++++ 2 files changed, 11 insertions(+), 2 deletions(-) (limited to 'src/SMAPI/Framework/Events') diff --git a/src/SMAPI/Framework/Events/EventManager.cs b/src/SMAPI/Framework/Events/EventManager.cs index 96582380..bcfd7dd7 100644 --- a/src/SMAPI/Framework/Events/EventManager.cs +++ b/src/SMAPI/Framework/Events/EventManager.cs @@ -1,10 +1,8 @@ -using System.Diagnostics.CodeAnalysis; using StardewModdingAPI.Events; namespace StardewModdingAPI.Framework.Events { /// Manages SMAPI events. - [SuppressMessage("ReSharper", "InconsistentNaming", Justification = "Private fields are deliberately named to simplify organisation.")] internal class EventManager { /********* @@ -19,6 +17,9 @@ namespace StardewModdingAPI.Framework.Events /// public readonly ManagedEvent AssetsInvalidated; + /// + public readonly ManagedEvent AssetReady; + /**** ** Display @@ -202,6 +203,7 @@ namespace StardewModdingAPI.Framework.Events // init events this.AssetRequested = ManageEventOf(nameof(IModEvents.Content), nameof(IContentEvents.AssetRequested)); this.AssetsInvalidated = ManageEventOf(nameof(IModEvents.Content), nameof(IContentEvents.AssetsInvalidated)); + this.AssetReady = ManageEventOf(nameof(IModEvents.Content), nameof(IContentEvents.AssetReady)); this.MenuChanged = ManageEventOf(nameof(IModEvents.Display), nameof(IDisplayEvents.MenuChanged)); this.Rendering = ManageEventOf(nameof(IModEvents.Display), nameof(IDisplayEvents.Rendering), isPerformanceCritical: true); diff --git a/src/SMAPI/Framework/Events/ModContentEvents.cs b/src/SMAPI/Framework/Events/ModContentEvents.cs index 4d0cfb97..cb242e99 100644 --- a/src/SMAPI/Framework/Events/ModContentEvents.cs +++ b/src/SMAPI/Framework/Events/ModContentEvents.cs @@ -23,6 +23,13 @@ namespace StardewModdingAPI.Framework.Events remove => this.EventManager.AssetsInvalidated.Remove(value); } + /// + public event EventHandler AssetReady + { + add => this.EventManager.AssetReady.Add(value, this.Mod); + remove => this.EventManager.AssetReady.Remove(value); + } + /********* ** Public methods -- cgit From 03efea26676464933513383eb1c841f1ca5db34d Mon Sep 17 00:00:00 2001 From: Jesse Plamondon-Willard Date: Sat, 26 Mar 2022 19:08:25 -0400 Subject: add LocaleChanged content event (#766) --- src/SMAPI/Framework/Events/EventManager.cs | 4 ++++ src/SMAPI/Framework/Events/ModContentEvents.cs | 7 +++++++ 2 files changed, 11 insertions(+) (limited to 'src/SMAPI/Framework/Events') diff --git a/src/SMAPI/Framework/Events/EventManager.cs b/src/SMAPI/Framework/Events/EventManager.cs index bcfd7dd7..41540047 100644 --- a/src/SMAPI/Framework/Events/EventManager.cs +++ b/src/SMAPI/Framework/Events/EventManager.cs @@ -20,6 +20,9 @@ namespace StardewModdingAPI.Framework.Events /// public readonly ManagedEvent AssetReady; + /// + public readonly ManagedEvent LocaleChanged; + /**** ** Display @@ -204,6 +207,7 @@ namespace StardewModdingAPI.Framework.Events this.AssetRequested = ManageEventOf(nameof(IModEvents.Content), nameof(IContentEvents.AssetRequested)); this.AssetsInvalidated = ManageEventOf(nameof(IModEvents.Content), nameof(IContentEvents.AssetsInvalidated)); this.AssetReady = ManageEventOf(nameof(IModEvents.Content), nameof(IContentEvents.AssetReady)); + this.LocaleChanged = ManageEventOf(nameof(IModEvents.Content), nameof(IContentEvents.LocaleChanged)); this.MenuChanged = ManageEventOf(nameof(IModEvents.Display), nameof(IDisplayEvents.MenuChanged)); this.Rendering = ManageEventOf(nameof(IModEvents.Display), nameof(IDisplayEvents.Rendering), isPerformanceCritical: true); diff --git a/src/SMAPI/Framework/Events/ModContentEvents.cs b/src/SMAPI/Framework/Events/ModContentEvents.cs index cb242e99..beb96031 100644 --- a/src/SMAPI/Framework/Events/ModContentEvents.cs +++ b/src/SMAPI/Framework/Events/ModContentEvents.cs @@ -30,6 +30,13 @@ namespace StardewModdingAPI.Framework.Events remove => this.EventManager.AssetReady.Remove(value); } + /// + public event EventHandler LocaleChanged + { + add => this.EventManager.LocaleChanged.Add(value, this.Mod); + remove => this.EventManager.LocaleChanged.Remove(value); + } + /********* ** Public methods -- cgit From a593eda30f82af474887d91458b0e9158f66fefc Mon Sep 17 00:00:00 2001 From: Jesse Plamondon-Willard Date: Wed, 6 Apr 2022 18:24:59 -0400 Subject: use target-typed new --- src/SMAPI/Framework/Events/ManagedEvent.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/SMAPI/Framework/Events') diff --git a/src/SMAPI/Framework/Events/ManagedEvent.cs b/src/SMAPI/Framework/Events/ManagedEvent.cs index 154ef659..1c8b9d04 100644 --- a/src/SMAPI/Framework/Events/ManagedEvent.cs +++ b/src/SMAPI/Framework/Events/ManagedEvent.cs @@ -18,7 +18,7 @@ namespace StardewModdingAPI.Framework.Events protected readonly ModRegistry ModRegistry; /// The underlying event handlers. - private readonly List> Handlers = new List>(); + private readonly List> Handlers = new(); /// A cached snapshot of , or null to rebuild it next raise. private ManagedEventHandler[] CachedHandlers = Array.Empty>(); -- cgit From b6c8cfc28b2c94e6dc3cb07d3058371dd6775e70 Mon Sep 17 00:00:00 2001 From: Jesse Plamondon-Willard Date: Wed, 6 Apr 2022 18:24:59 -0400 Subject: simplify 'is not' patterns --- src/SMAPI/Framework/Events/ManagedEventHandler.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/SMAPI/Framework/Events') diff --git a/src/SMAPI/Framework/Events/ManagedEventHandler.cs b/src/SMAPI/Framework/Events/ManagedEventHandler.cs index 28e88be0..97040f76 100644 --- a/src/SMAPI/Framework/Events/ManagedEventHandler.cs +++ b/src/SMAPI/Framework/Events/ManagedEventHandler.cs @@ -42,7 +42,7 @@ namespace StardewModdingAPI.Framework.Events /// public int CompareTo(object obj) { - if (!(obj is ManagedEventHandler other)) + if (obj is not ManagedEventHandler other) throw new ArgumentException("Can't compare to an unrelated object type."); int priorityCompare = -this.Priority.CompareTo(other.Priority); // higher value = sort first -- cgit From 2e7c233f6c9bf6430672b39f970a3324deba79dd Mon Sep 17 00:00:00 2001 From: Jesse Plamondon-Willard Date: Wed, 6 Apr 2022 21:48:55 -0400 Subject: enable nullable annotations by default (#837) This adds `#nullable disable` to all existing code (except where null is impossible like enum files), so it can be migrated incrementally. --- src/SMAPI/Framework/Events/EventManager.cs | 2 ++ src/SMAPI/Framework/Events/IManagedEvent.cs | 2 ++ src/SMAPI/Framework/Events/ManagedEvent.cs | 2 ++ src/SMAPI/Framework/Events/ManagedEventHandler.cs | 2 ++ src/SMAPI/Framework/Events/ModContentEvents.cs | 2 ++ src/SMAPI/Framework/Events/ModDisplayEvents.cs | 2 ++ src/SMAPI/Framework/Events/ModEvents.cs | 2 ++ src/SMAPI/Framework/Events/ModEventsBase.cs | 2 ++ src/SMAPI/Framework/Events/ModGameLoopEvents.cs | 2 ++ src/SMAPI/Framework/Events/ModInputEvents.cs | 2 ++ src/SMAPI/Framework/Events/ModMultiplayerEvents.cs | 2 ++ src/SMAPI/Framework/Events/ModPlayerEvents.cs | 2 ++ src/SMAPI/Framework/Events/ModSpecialisedEvents.cs | 2 ++ src/SMAPI/Framework/Events/ModWorldEvents.cs | 2 ++ 14 files changed, 28 insertions(+) (limited to 'src/SMAPI/Framework/Events') diff --git a/src/SMAPI/Framework/Events/EventManager.cs b/src/SMAPI/Framework/Events/EventManager.cs index 41540047..c977e73d 100644 --- a/src/SMAPI/Framework/Events/EventManager.cs +++ b/src/SMAPI/Framework/Events/EventManager.cs @@ -1,3 +1,5 @@ +#nullable disable + using StardewModdingAPI.Events; namespace StardewModdingAPI.Framework.Events diff --git a/src/SMAPI/Framework/Events/IManagedEvent.cs b/src/SMAPI/Framework/Events/IManagedEvent.cs index e4e3ca08..57277576 100644 --- a/src/SMAPI/Framework/Events/IManagedEvent.cs +++ b/src/SMAPI/Framework/Events/IManagedEvent.cs @@ -1,3 +1,5 @@ +#nullable disable + namespace StardewModdingAPI.Framework.Events { /// Metadata for an event raised by SMAPI. diff --git a/src/SMAPI/Framework/Events/ManagedEvent.cs b/src/SMAPI/Framework/Events/ManagedEvent.cs index 1c8b9d04..8fa31165 100644 --- a/src/SMAPI/Framework/Events/ManagedEvent.cs +++ b/src/SMAPI/Framework/Events/ManagedEvent.cs @@ -1,3 +1,5 @@ +#nullable disable + using System; using System.Collections.Generic; using System.Linq; diff --git a/src/SMAPI/Framework/Events/ManagedEventHandler.cs b/src/SMAPI/Framework/Events/ManagedEventHandler.cs index 97040f76..f31bc04d 100644 --- a/src/SMAPI/Framework/Events/ManagedEventHandler.cs +++ b/src/SMAPI/Framework/Events/ManagedEventHandler.cs @@ -1,3 +1,5 @@ +#nullable disable + using System; using StardewModdingAPI.Events; diff --git a/src/SMAPI/Framework/Events/ModContent