summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/StardewModdingAPI/Events/SaveEvents.cs46
-rw-r--r--src/StardewModdingAPI/Inheritance/SGame.cs12
-rw-r--r--src/StardewModdingAPI/StardewModdingAPI.csproj1
3 files changed, 57 insertions, 2 deletions
diff --git a/src/StardewModdingAPI/Events/SaveEvents.cs b/src/StardewModdingAPI/Events/SaveEvents.cs
new file mode 100644
index 00000000..2921003a
--- /dev/null
+++ b/src/StardewModdingAPI/Events/SaveEvents.cs
@@ -0,0 +1,46 @@
+using System;
+using StardewModdingAPI.Framework;
+
+namespace StardewModdingAPI.Events
+{
+ /// <summary>Events raised before and after the player saves/loads the game.</summary>
+ public static class SaveEvents
+ {
+ /*********
+ ** Events
+ *********/
+ /// <summary>Raised before the game begins writes data to the save file.</summary>
+ public static event EventHandler BeforeSave;
+
+ /// <summary>Raised after the game finishes writing data to the save file.</summary>
+ public static event EventHandler AfterSave;
+
+ /// <summary>Raised after the player loads a save slot.</summary>
+ public static event EventHandler AfterLoad;
+
+
+ /*********
+ ** Internal methods
+ *********/
+ /// <summary>Raise a <see cref="BeforeSave"/> event.</summary>
+ /// <param name="monitor">Encapsulates monitoring and logging.</param>
+ internal static void InvokeBeforeSave(IMonitor monitor)
+ {
+ monitor.SafelyRaisePlainEvent($"{nameof(SaveEvents)}.{nameof(SaveEvents.BeforeSave)}", SaveEvents.BeforeSave?.GetInvocationList(), null, EventArgs.Empty);
+ }
+
+ /// <summary>Raise a <see cref="AfterSave"/> event.</summary>
+ /// <param name="monitor">Encapsulates monitoring and logging.</param>
+ internal static void InvokeAfterSave(IMonitor monitor)
+ {
+ monitor.SafelyRaisePlainEvent($"{nameof(SaveEvents)}.{nameof(SaveEvents.AfterSave)}", SaveEvents.AfterSave?.GetInvocationList(), null, EventArgs.Empty);
+ }
+
+ /// <summary>Raise a <see cref="AfterLoad"/> event.</summary>
+ /// <param name="monitor">Encapsulates monitoring and logging.</param>
+ internal static void InvokeAfterLoad(IMonitor monitor)
+ {
+ monitor.SafelyRaisePlainEvent($"{nameof(SaveEvents)}.{nameof(SaveEvents.AfterLoad)}", SaveEvents.AfterLoad?.GetInvocationList(), null, EventArgs.Empty);
+ }
+ }
+}
diff --git a/src/StardewModdingAPI/Inheritance/SGame.cs b/src/StardewModdingAPI/Inheritance/SGame.cs
index 16803a73..8e87bac6 100644
--- a/src/StardewModdingAPI/Inheritance/SGame.cs
+++ b/src/StardewModdingAPI/Inheritance/SGame.cs
@@ -913,10 +913,17 @@ namespace StardewModdingAPI.Inheritance
// raise menu changed
if (Game1.activeClickableMenu != this.PreviousActiveMenu)
{
- // raise events
IClickableMenu previousMenu = this.PreviousActiveMenu;
IClickableMenu newMenu = Game1.activeClickableMenu;
- if (Game1.activeClickableMenu != null)
+
+ // raise save events
+ if (newMenu is SaveGameMenu)
+ SaveEvents.InvokeBeforeSave(this.Monitor);
+ else if (previousMenu is SaveGameMenu)
+ SaveEvents.InvokeAfterSave(this.Monitor);
+
+ // raise menu events
+ if (newMenu != null)
MenuEvents.InvokeMenuChanged(this.Monitor, previousMenu, newMenu);
else
MenuEvents.InvokeMenuClosed(this.Monitor, previousMenu);
@@ -1024,6 +1031,7 @@ namespace StardewModdingAPI.Inheritance
// raise player loaded save (in the following tick to let the game finish updating first)
if (this.FireLoadedGameEvent)
{
+ SaveEvents.InvokeAfterLoad(this.Monitor);
PlayerEvents.InvokeLoadedGame(this.Monitor, new EventArgsLoadedGameChanged(Game1.hasLoadedGame));
this.FireLoadedGameEvent = false;
}
diff --git a/src/StardewModdingAPI/StardewModdingAPI.csproj b/src/StardewModdingAPI/StardewModdingAPI.csproj
index 07b1ff5e..125f287e 100644
--- a/src/StardewModdingAPI/StardewModdingAPI.csproj
+++ b/src/StardewModdingAPI/StardewModdingAPI.csproj
@@ -152,6 +152,7 @@
<Compile Include="Events\MenuEvents.cs" />
<Compile Include="Events\MineEvents.cs" />
<Compile Include="Events\PlayerEvents.cs" />
+ <Compile Include="Events\SaveEvents.cs" />
<Compile Include="Events\TimeEvents.cs" />
<Compile Include="Extensions.cs" />
<Compile Include="Framework\AssemblyRewriting\RewriteResult.cs" />