From 366769e8a27e8de39842c82907cf40451c488a2c Mon Sep 17 00:00:00 2001 From: Jesse Plamondon-Willard Date: Wed, 8 Feb 2017 00:11:46 -0500 Subject: add SaveEvents.AfterReturnToTitle event (#231) --- src/StardewModdingAPI/Events/SaveEvents.cs | 10 ++++++++++ src/StardewModdingAPI/Framework/SGame.cs | 16 ++++++++++++++++ 2 files changed, 26 insertions(+) (limited to 'src') diff --git a/src/StardewModdingAPI/Events/SaveEvents.cs b/src/StardewModdingAPI/Events/SaveEvents.cs index 2921003a..50e6d729 100644 --- a/src/StardewModdingAPI/Events/SaveEvents.cs +++ b/src/StardewModdingAPI/Events/SaveEvents.cs @@ -18,6 +18,9 @@ namespace StardewModdingAPI.Events /// Raised after the player loads a save slot. public static event EventHandler AfterLoad; + /// Raised after the game returns to the title screen. + public static event EventHandler AfterReturnToTitle; + /********* ** Internal methods @@ -42,5 +45,12 @@ namespace StardewModdingAPI.Events { monitor.SafelyRaisePlainEvent($"{nameof(SaveEvents)}.{nameof(SaveEvents.AfterLoad)}", SaveEvents.AfterLoad?.GetInvocationList(), null, EventArgs.Empty); } + + /// Raise a event. + /// Encapsulates monitoring and logging. + internal static void InvokeAfterReturnToTitle(IMonitor monitor) + { + monitor.SafelyRaisePlainEvent($"{nameof(SaveEvents)}.{nameof(SaveEvents.AfterReturnToTitle)}", SaveEvents.AfterReturnToTitle?.GetInvocationList(), null, EventArgs.Empty); + } } } diff --git a/src/StardewModdingAPI/Framework/SGame.cs b/src/StardewModdingAPI/Framework/SGame.cs index 24abd4fd..6a751b85 100644 --- a/src/StardewModdingAPI/Framework/SGame.cs +++ b/src/StardewModdingAPI/Framework/SGame.cs @@ -37,6 +37,9 @@ namespace StardewModdingAPI.Framework /// Whether the player has loaded a save and the world has finished initialising. private bool IsWorldReady => this.AfterLoadTimer < 0; + /// Whether the game is returning to the menu. + private bool IsExiting = false; + /// The debug messages to add to the next debug output. internal static Queue DebugMessageQueue { get; private set; } @@ -1160,6 +1163,19 @@ namespace StardewModdingAPI.Framework this.AfterLoadTimer--; } + // before exit to title + if (Game1.exitToTitle) + this.IsExiting = true; + + // after exit to title + if (this.IsWorldReady && this.IsExiting && Game1.activeClickableMenu is TitleMenu) + { + Console.WriteLine($"{Game1.currentGameTime.TotalGameTime}: after return to title"); + SaveEvents.InvokeAfterReturnToTitle(this.Monitor); + this.AfterLoadTimer = 5; + this.IsExiting = false; + } + // input events { // get latest state -- cgit