From 79705448f57c962e9331fb802097c24d2424476c Mon Sep 17 00:00:00 2001 From: Jesse Plamondon-Willard Date: Sat, 6 Oct 2018 00:51:45 -0400 Subject: add DayEnding event (#310) --- src/SMAPI/Framework/Events/EventManager.cs | 4 +++ src/SMAPI/Framework/Events/ModGameLoopEvents.cs | 7 +++++ src/SMAPI/Framework/SGame.cs | 7 +++++ src/SMAPI/Framework/SModHooks.cs | 34 +++++++++++++++++++++++++ 4 files changed, 52 insertions(+) create mode 100644 src/SMAPI/Framework/SModHooks.cs (limited to 'src/SMAPI/Framework') diff --git a/src/SMAPI/Framework/Events/EventManager.cs b/src/SMAPI/Framework/Events/EventManager.cs index 023c45de..616db7d9 100644 --- a/src/SMAPI/Framework/Events/EventManager.cs +++ b/src/SMAPI/Framework/Events/EventManager.cs @@ -41,6 +41,9 @@ namespace StardewModdingAPI.Framework.Events /// 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; + /**** ** Input ****/ @@ -291,6 +294,7 @@ namespace StardewModdingAPI.Framework.Events 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.ButtonPressed = ManageEventOf(nameof(IModEvents.Input), nameof(IInputEvents.ButtonPressed)); this.ButtonReleased = ManageEventOf(nameof(IModEvents.Input), nameof(IInputEvents.ButtonReleased)); diff --git a/src/SMAPI/Framework/Events/ModGameLoopEvents.cs b/src/SMAPI/Framework/Events/ModGameLoopEvents.cs index cf7e54aa..d1def91b 100644 --- a/src/SMAPI/Framework/Events/ModGameLoopEvents.cs +++ b/src/SMAPI/Framework/Events/ModGameLoopEvents.cs @@ -72,6 +72,13 @@ namespace StardewModdingAPI.Framework.Events 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); + remove => this.EventManager.DayEnding.Remove(value); + } + /********* ** Public methods diff --git a/src/SMAPI/Framework/SGame.cs b/src/SMAPI/Framework/SGame.cs index ef851afc..bd266ce1 100644 --- a/src/SMAPI/Framework/SGame.cs +++ b/src/SMAPI/Framework/SGame.cs @@ -151,6 +151,7 @@ namespace StardewModdingAPI.Framework this.OnGameExiting = onGameExiting; Game1.input = new SInputState(); Game1.multiplayer = new SMultiplayer(monitor, eventManager); + Game1.hooks = new SModHooks(this.OnNewDayAfterFade); // init observables Game1.locations = new ObservableCollection(); @@ -182,6 +183,12 @@ namespace StardewModdingAPI.Framework /**** ** Intercepted methods & events ****/ + /// A callback invoked before runs. + protected void OnNewDayAfterFade() + { + this.Events.DayEnding.RaiseEmpty(); + } + /// Constructor a content manager to read XNB files. /// The service provider to use to locate services. /// The root directory to search for content. diff --git a/src/SMAPI/Framework/SModHooks.cs b/src/SMAPI/Framework/SModHooks.cs new file mode 100644 index 00000000..9f0201c8 --- /dev/null +++ b/src/SMAPI/Framework/SModHooks.cs @@ -0,0 +1,34 @@ +using System; +using StardewValley; + +namespace StardewModdingAPI.Framework +{ + /// Invokes callbacks for mod hooks provided by the game. + internal class SModHooks : ModHooks + { + /********* + ** Properties + *********/ + /// A callback to invoke before runs. + private readonly Action BeforeNewDayAfterFade; + + + /********* + ** Public methods + *********/ + /// Construct an instance. + /// A callback to invoke before runs. + public SModHooks(Action beforeNewDayAfterFade) + { + this.BeforeNewDayAfterFade = beforeNewDayAfterFade; + } + + /// A hook invoked when is called. + /// The vanilla logic. + public override void OnGame1_NewDayAfterFade(Action action) + { + this.BeforeNewDayAfterFade?.Invoke(); + action(); + } + } +} -- cgit