fix edge case where save constants aren't set correctly

This commit is contained in:
Jesse Plamondon-Willard 2021-06-11 19:14:59 -04:00
parent c212ff15d2
commit 4df8f4a656
No known key found for this signature in database
GPG Key ID: CF8B1456B3E29F49
2 changed files with 31 additions and 19 deletions

View File

@ -11,6 +11,7 @@
* For players: * For players:
* Fixed error when running the Windows installer as administrator (thanks to LostLogic!). * Fixed error when running the Windows installer as administrator (thanks to LostLogic!).
* Fixed `player_add` and `list_items` console commands not including some shirts _(in Console Commands)_. * Fixed `player_add` and `list_items` console commands not including some shirts _(in Console Commands)_.
* Fixed installer error on some older Windows systems (thanks to eddyballs!).
* For mod authors: * For mod authors:
* Added `World.FurnitureListChanged` event (thanks to DiscipleOfEris!). * Added `World.FurnitureListChanged` event (thanks to DiscipleOfEris!).
@ -20,7 +21,7 @@
* Fixed JSON schema for `i18n` files requiring the wrong value for the `$schema` field. * Fixed JSON schema for `i18n` files requiring the wrong value for the `$schema` field.
* Fixed validation for mods with version `0.0.0`. * Fixed validation for mods with version `0.0.0`.
* Fixed _loaded with custom settings_ trace log when using default settings. * Fixed _loaded with custom settings_ trace log when using default settings.
* Fixed installer error on some older Windows systems (thanks to eddyballs!). * Fixed `Constants.SaveFolderName` and `Constants.CurrentSavePath` not set correctly in rare cases.
* For the web UI: * For the web UI:
* Updated the JSON validator/schema for Content Patcher 1.23. * Updated the JSON validator/schema for Content Patcher 1.23.

View File

@ -322,31 +322,42 @@ namespace StardewModdingAPI
/// <summary>Get the name of the save folder, if any.</summary> /// <summary>Get the name of the save folder, if any.</summary>
private static string GetSaveFolderName() private static string GetSaveFolderName()
{ {
// save not available return Constants.GetSaveFolder()?.Name;
if (Context.LoadStage == LoadStage.None)
return null;
// get basic info
string saveName = Game1.GetSaveGameName(set_value: false);
ulong saveID = Context.LoadStage == LoadStage.SaveParsed
? SaveGame.loaded.uniqueIDForThisGame
: Game1.uniqueIDForThisGame;
// build folder name
return $"{new string(saveName.Where(char.IsLetterOrDigit).ToArray())}_{saveID}";
} }
/// <summary>Get the path to the current save folder, if any.</summary> /// <summary>Get the path to the current save folder, if any.</summary>
private static string GetSaveFolderPathIfExists() private static string GetSaveFolderPathIfExists()
{ {
string folderName = Constants.GetSaveFolderName(); DirectoryInfo saveFolder = Constants.GetSaveFolder();
if (folderName == null) return saveFolder?.Exists == true
? saveFolder.FullName
: null;
}
/// <summary>Get the current save folder, if any.</summary>
private static DirectoryInfo GetSaveFolder()
{
// save not available
if (Context.LoadStage == LoadStage.None)
return null; return null;
string path = Path.Combine(Constants.SavesPath, folderName); // get basic info
return Directory.Exists(path) string rawSaveName = Game1.GetSaveGameName(set_value: false);
? path ulong saveID = Context.LoadStage == LoadStage.SaveParsed
: null; ? SaveGame.loaded.uniqueIDForThisGame
: Game1.uniqueIDForThisGame;
// get best match (accounting for rare case where folder name isn't sanitized)
DirectoryInfo folder = null;
foreach (string saveName in new[] { rawSaveName, new string(rawSaveName.Where(char.IsLetterOrDigit).ToArray()) })
{
folder = new DirectoryInfo(Path.Combine(Constants.SavesPath, $"{saveName}_{saveID}"));
if (folder.Exists)
return folder;
}
// if save doesn't exist yet, return the default one we expect to be created
return folder;
} }
} }
} }