summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--docs/release-notes.md1
-rw-r--r--src/SMAPI/Events/SaveEvents.cs22
-rw-r--r--src/SMAPI/Framework/SGame.cs18
3 files changed, 40 insertions, 1 deletions
diff --git a/docs/release-notes.md b/docs/release-notes.md
index 38fb680b..afd89c68 100644
--- a/docs/release-notes.md
+++ b/docs/release-notes.md
@@ -12,6 +12,7 @@
* Fixed parse error for logs with zero installed mods.
* For modders:
+ * Added `SaveEvents.BeforeCreate` and `AfterCreate` events.
* Added `SButton` `IsActionButton()` and `IsUseToolButton()` extensions.
* Improved JSON parse errors to provide more useful info for troubleshooting.
* Fixed events being raised while the game is loading a save file.
diff --git a/src/SMAPI/Events/SaveEvents.cs b/src/SMAPI/Events/SaveEvents.cs
index 50e6d729..99b6c8d2 100644
--- a/src/SMAPI/Events/SaveEvents.cs
+++ b/src/SMAPI/Events/SaveEvents.cs
@@ -1,4 +1,4 @@
-using System;
+using System;
using StardewModdingAPI.Framework;
namespace StardewModdingAPI.Events
@@ -9,6 +9,12 @@ namespace StardewModdingAPI.Events
/*********
** Events
*********/
+ /// <summary>Raised before the game creates the save file.</summary>
+ public static event EventHandler BeforeCreate;
+
+ /// <summary>Raised after the game finishes creating the save file.</summary>
+ public static event EventHandler AfterCreate;
+
/// <summary>Raised before the game begins writes data to the save file.</summary>
public static event EventHandler BeforeSave;
@@ -25,6 +31,20 @@ namespace StardewModdingAPI.Events
/*********
** Internal methods
*********/
+ /// <summary>Raise a <see cref="BeforeCreate"/> event.</summary>
+ /// <param name="monitor">Encapsulates monitoring and logging.</param>
+ internal static void InvokeBeforeCreate(IMonitor monitor)
+ {
+ monitor.SafelyRaisePlainEvent($"{nameof(SaveEvents)}.{nameof(SaveEvents.BeforeCreate)}", SaveEvents.BeforeCreate?.GetInvocationList(), null, EventArgs.Empty);
+ }
+
+ /// <summary>Raise a <see cref="AfterCreate"/> event.</summary>
+ /// <param name="monitor">Encapsulates monitoring and logging.</param>
+ internal static void InvokeAfterCreated(IMonitor monitor)
+ {
+ monitor.SafelyRaisePlainEvent($"{nameof(SaveEvents)}.{nameof(SaveEvents.AfterCreate)}", SaveEvents.AfterCreate?.GetInvocationList(), null, EventArgs.Empty);
+ }
+
/// <summary>Raise a <see cref="BeforeSave"/> event.</summary>
/// <param name="monitor">Encapsulates monitoring and logging.</param>
internal static void InvokeBeforeSave(IMonitor monitor)
diff --git a/src/SMAPI/Framework/SGame.cs b/src/SMAPI/Framework/SGame.cs
index 2eb2da99..e82ee778 100644
--- a/src/SMAPI/Framework/SGame.cs
+++ b/src/SMAPI/Framework/SGame.cs
@@ -51,6 +51,9 @@ namespace StardewModdingAPI.Framework
/// <summary>Whether the game is saving and SMAPI has already raised <see cref="SaveEvents.BeforeSave"/>.</summary>
private bool IsBetweenSaveEvents;
+ /// <summary>Whether the game is creating the save file and SMAPI has already raised <see cref="SaveEvents.BeforeCreate"/>.</summary>
+ private bool IsBetweenCreateEvents;
+
/****
** Game state
****/
@@ -246,6 +249,14 @@ namespace StardewModdingAPI.Framework
// opened (since the save hasn't started yet), but all other events should be suppressed.
if (Context.IsSaving)
{
+ // raise before-create
+ if (!Context.IsWorldReady && !this.IsBetweenCreateEvents)
+ {
+ this.IsBetweenCreateEvents = true;
+ this.Monitor.Log("Context: before save creation.", LogLevel.Trace);
+ SaveEvents.InvokeBeforeCreate(this.Monitor);
+ }
+
// raise before-save
if (Context.IsWorldReady && !this.IsBetweenSaveEvents)
{
@@ -258,6 +269,13 @@ namespace StardewModdingAPI.Framework
base.Update(gameTime);
return;
}
+ if (this.IsBetweenCreateEvents)
+ {
+ // raise after-create
+ this.IsBetweenCreateEvents = false;
+ this.Monitor.Log($"Context: after save creation, starting {Game1.currentSeason} {Game1.dayOfMonth} Y{Game1.year}.", LogLevel.Trace);
+ SaveEvents.InvokeAfterCreated(this.Monitor);
+ }
if (this.IsBetweenSaveEvents)
{
// raise after-save