fix issue where loading a game after sleeping warps player back to last custom save (#12, #15)

This commit is contained in:
Jesse Plamondon-Willard 2017-08-05 23:17:51 -04:00
parent 80c4633e99
commit 26aa5530ca
5 changed files with 40 additions and 2 deletions

View File

@ -0,0 +1,7 @@
using StardewValley.Menus;
namespace Omegasis.SaveAnywhere.Framework
{
/// <summary>A marker subclass to detect when a custom save is in progress.</summary>
internal class NewSaveGameMenu : SaveGameMenu { }
}

View File

@ -71,11 +71,17 @@ namespace Omegasis.SaveAnywhere.Framework
// perform passive save
if (this.WaitingToSave && Game1.activeClickableMenu == null)
{
Game1.activeClickableMenu = new SaveGameMenu();
Game1.activeClickableMenu = new NewSaveGameMenu();
this.WaitingToSave = false;
}
}
/// <summary>Clear saved data.</summary>
public void ClearData()
{
Directory.Delete(this.SavePath, recursive: true);
}
/// <summary>Save all game data.</summary>
public void SaveGameAndPositions()
{
@ -89,7 +95,7 @@ namespace Omegasis.SaveAnywhere.Framework
this.WaitingToSave = true;
}
else
Game1.activeClickableMenu = new SaveGameMenu();
Game1.activeClickableMenu = new NewSaveGameMenu();
// save custom data
Directory.CreateDirectory(this.SaveAnimalsPath);

View File

@ -37,4 +37,5 @@ Press `K` to save anywhere. Edit `Save_Anywhere_Config.txt` to configure the key
2.5:
* Updated for SMAPI 2.0.
* Fixed issue where reloading after a sleep save restored some data from your last non-sleep save.
* Internal refactoring.

View File

@ -27,6 +27,8 @@ namespace Omegasis.SaveAnywhere
/// <summary>Whether villager schedules should be reset now.</summary>
private bool ShouldResetSchedules;
/// <summary>Whether we're performing a non-vanilla save (i.e. not by sleeping in bed).</summary>
private bool IsCustomSaving;
/*********
@ -39,6 +41,8 @@ namespace Omegasis.SaveAnywhere
this.ConfigUtilities = new ConfigUtilities(this.Helper.DirectoryPath);
SaveEvents.AfterLoad += this.SaveEvents_AfterLoad;
SaveEvents.AfterSave += this.SaveEvents_AfterSave;
MenuEvents.MenuChanged += this.MenuEvents_MenuChanged;
ControlEvents.KeyPressed += this.ControlEvents_KeyPressed;
GameEvents.UpdateTick += this.GameEvents_UpdateTick;
TimeEvents.AfterDayStarted += this.TimeEvents_AfterDayStarted;
@ -54,6 +58,7 @@ namespace Omegasis.SaveAnywhere
private void SaveEvents_AfterLoad(object sender, EventArgs e)
{
// reset state
this.IsCustomSaving = false;
this.ShouldResetSchedules = false;
// load config
@ -65,6 +70,24 @@ namespace Omegasis.SaveAnywhere
this.SaveManager.LoadPositions();
}
/// <summary>The method invoked after the player finishes saving.</summary>
/// <param name="sender">The event sender.</param>
/// <param name="e">The event data.</param>
private void SaveEvents_AfterSave(object sender, EventArgs e)
{
// clear custom data after a normal save (to avoid restoring old state)
if (!this.IsCustomSaving)
this.SaveManager.ClearData();
}
/// <summary>The method invoked after a menu is opened or changed.</summary>
/// <param name="sender">The event sender.</param>
/// <param name="e">The event data.</param>
private void MenuEvents_MenuChanged(object sender, EventArgsClickableMenuChanged e)
{
this.IsCustomSaving = e.NewMenu != null && (e.NewMenu is NewSaveGameMenu || e.NewMenu is NewSaveGameMenu);
}
/// <summary>The method invoked when the game updates (roughly 60 times per second).</summary>
/// <param name="sender">The event sender.</param>
/// <param name="e">The event data.</param>

View File

@ -37,6 +37,7 @@
<Compile Include="..\GlobalAssemblyInfo.cs">
<Link>Properties\GlobalAssemblyInfo.cs</Link>
</Compile>
<Compile Include="Framework\NewSaveMenu.cs" />
<Compile Include="Framework\SaveManager.cs" />
<Compile Include="Framework\ConfigUtilities.cs" />
<Compile Include="SaveAnywhere.cs" />