using System.Collections.Generic; using System.Diagnostics.CodeAnalysis; using System.Reflection; 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 { /********* ** 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 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; /**** ** 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; /********* ** Public methods *********/ /// Construct an instance. /// The mod registry with which to identify mods. public EventManager(ModRegistry modRegistry) { // create shortcut initializers ManagedEvent ManageEventOf(string typeName, string eventName, bool isPerformanceCritical = false) { return new ManagedEvent($"{typeName}.{eventName}", modRegistry, isPerformanceCritical); } // init events (new) 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); this.RenderingWorld = ManageEventOf(nameof(IModEvents.Display), nameof(IDisplayEvents.RenderingWorld), isPerformanceCritical: true); this.RenderedWorld = ManageEventOf(nameof(IModEvents.Display), nameof(IDisplayEvents.RenderedWorld), isPerformanceCritical: true); this.RenderingActiveMenu = ManageEventOf(nameof(IModEvents.Display), nameof(IDisplayEvents.RenderingActiveMenu), isPerformanceCritical: true); this.RenderedActiveMenu = ManageEventOf(nameof(IModEvents.Display), nameof(IDisplayEvents.RenderedActiveMenu), isPerformanceCritical: true); this.RenderingHud = ManageEventOf(nameof(IModEvents.Display), nameof(IDisplayEvents.RenderingHud), isPerformanceCritical: true); this.RenderedHud = ManageEventOf(nameof(IModEvents.Display), nameof(IDisplayEvents.RenderedHud), isPerformanceCritical: true); this.WindowResized = ManageEventOf(nameof(IModEvents.Display), nameof(IDisplayEvents.WindowResized)); this.GameLaunched = ManageEventOf(nameof(IModEvents.GameLoop), nameof(IGameLoopEvents.GameLaunched)); this.UpdateTicking = ManageEventOf(nameof(IModEvents.GameLoop), nameof(IGameLoopEvents.UpdateTicking), isPerformanceCritical: true); this.UpdateTicked = ManageEventOf(nameof(IModEvents.GameLoop), nameof(IGameLoopEvents.UpdateTicked), isPerformanceCritical: true); this.OneSecondUpdateTicking = ManageEventOf(nameof(IModEvents.GameLoop), nameof(IGameLoopEvents.OneSecondUpdateTicking), isPerformanceCritical: true); this.OneSecondUpdateTicked = ManageEventOf(nameof(IModEvents.GameLoop), nameof(IGameLoopEvents.OneSecondUpdateTicked), isPerformanceCritical: true); this.SaveCreating = ManageEventOf(nameof(IModEvents.GameLoop), nameof(IGameLoopEvents.SaveCreating)); this.SaveCreated = ManageEventOf(nameof(IModEvents.GameLoop), nameof(IGameLoopEvents.SaveCreated)); this.Saving = ManageEventOf(nameof(IModEvents.GameLoop), nameof(IGameLoopEvents.Saving)); this.Saved = ManageEventOf(nameof(IModEvents.GameLoop), nameof(IGameLoopEvents.Saved)); this.SaveLoaded = ManageEventOf(nameof(IModEvents.GameLoop), nameof(IGameLoopEvents.SaveLoaded)); this.DayStarted = ManageEventOf(nameof(IModEvents.GameLoop), nameof(IGameLoopEvents.DayStarted)); this.DayEnding = ManageEventOf(nameof(IModEvents.GameLoop), nameof(IGameLoopEvents.DayEnding)); this.TimeChanged = ManageEventOf(nameof(IModEvents.GameLoop), nameof(IGameLoopEvents.TimeChanged)); this.ReturnedToTitle = ManageEventOf(nameof(IModEvents.GameLoop), nameof(IGameLoopEvents.ReturnedToTitle)); this.ButtonPressed = ManageEventOf(nameof(IModEvents.Input), nameof(IInputEvents.ButtonPressed)); this.ButtonReleased = ManageEventOf(nameof(IModEvents.Input), nameof(IInputEvents.ButtonReleased)); this.CursorMoved = ManageEventOf(nameof(IModEvents.Input), nameof(IInputEvents.CursorMoved), isPerformanceCritical: true); this.MouseWheelScrolled = ManageEventOf(nameof(IModEvents.Input), nameof(IInputEvents.MouseWheelScrolled)); this.PeerContextReceived = ManageEventOf(nameof(IModEvents.Multiplayer), nameof(IMultiplayerEvents.PeerContextReceived)); this.PeerConnected = ManageEventOf(nameof(IModEvents.Multiplayer), nameof(IMultiplayerEvents.PeerConnected)); this.ModMessageReceived = ManageEventOf(nameof(IModEvents.Multiplayer), nameof(IMultiplayerEvents.ModMessageReceived)); this.PeerDisconnected = ManageEventOf(nameof(IModEvents.Multiplayer), nameof(IMultiplayerEvents.PeerDisconnected)); this.InventoryChanged = ManageEventOf(nameof(IModEvents.Player), nameof(IPlayerEvents.InventoryChanged)); this.LevelChanged = ManageEventOf(nameof(IModEvents.Player), nameof(IPlayerEvents.LevelChanged)); this.Warped = ManageEventOf(nameof(IModEvents.Player), nameof(IPlayerEvents.Warped)); this.BuildingListChanged = ManageEventOf(nameof(IModEvents.World), nameof(IWorldEvents.LocationListChanged)); this.DebrisListChanged = ManageEventOf(nameof(IModEvents.World), nameof(IWorldEvents.DebrisListChanged)); this.LargeTerrainFeatureListChanged = ManageEventOf(nameof(IModEvents.World), nameof(IWorldEvents.LargeTerrainFeatureListChanged)); this.LocationListChanged = ManageEventOf(nameof(IModEvents.World), nameof(IWorldEvents.BuildingListChanged)); this.NpcListChanged = ManageEventOf(nameof(IModEvents.World), nameof(IWorldEvents.NpcListChanged)); this.ObjectListChanged = ManageEventOf(nameof(IModEvents.World), nameof(IWorldEvents.ObjectListChanged)); this.ChestInventoryChanged = ManageEventOf(nameof(IModEvents.World), nameof(IWorldEvents.ChestInventoryChanged)); this.TerrainFeatureListChanged = ManageEventOf(nameof(IModEvents.World), nameof(IWorldEvents.TerrainFeatureListChanged)); this.LoadStageChanged = ManageEventOf(nameof(IModEvents.Specialized), nameof(ISpecializedEvents.LoadStageChanged)); 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); } } }