Put locks bc im afraid

This commit is contained in:
Nick 2022-12-24 12:31:50 -05:00
parent f6dcf2f4b8
commit bdc4c4d170

View File

@ -11,40 +11,45 @@ namespace NewHorizons.External
private static string _activeProfileName; private static string _activeProfileName;
private static readonly string FileName = "save.json"; private static readonly string FileName = "save.json";
private static object _lock;
// This is its own method so it can be patched by NH-QSB compat // This is its own method so it can be patched by NH-QSB compat
public static string GetProfileName() => StandaloneProfileManager.SharedInstance?.currentProfile?.profileName; public static string GetProfileName() => StandaloneProfileManager.SharedInstance?.currentProfile?.profileName;
public static void Load() public static void Load()
{ {
_activeProfileName = GetProfileName(); lock (_lock)
if (_activeProfileName == null)
{ {
Logger.LogWarning("Couldn't find active profile, are you on Gamepass?"); _activeProfileName = GetProfileName();
_activeProfileName = "XboxGamepassDefaultProfile"; if (_activeProfileName == null)
} {
Logger.LogWarning("Couldn't find active profile, are you on Gamepass?");
_activeProfileName = "XboxGamepassDefaultProfile";
}
try
{
_saveFile = Main.Instance.ModHelper.Storage.Load<NewHorizonsSaveFile>(FileName, false);
if (!_saveFile.Profiles.ContainsKey(_activeProfileName))
_saveFile.Profiles.Add(_activeProfileName, new NewHorizonsProfile());
_activeProfile = _saveFile.Profiles[_activeProfileName];
Logger.LogVerbose($"Loaded save data for {_activeProfileName}");
}
catch (Exception)
{
try try
{ {
Logger.LogVerbose($"Couldn't load save data from {FileName}, creating a new file"); _saveFile = Main.Instance.ModHelper.Storage.Load<NewHorizonsSaveFile>(FileName, false);
_saveFile = new NewHorizonsSaveFile(); if (!_saveFile.Profiles.ContainsKey(_activeProfileName))
_saveFile.Profiles.Add(_activeProfileName, new NewHorizonsProfile()); _saveFile.Profiles.Add(_activeProfileName, new NewHorizonsProfile());
_activeProfile = _saveFile.Profiles[_activeProfileName]; _activeProfile = _saveFile.Profiles[_activeProfileName];
Main.Instance.ModHelper.Storage.Save(_saveFile, FileName);
Logger.LogVerbose($"Loaded save data for {_activeProfileName}"); Logger.LogVerbose($"Loaded save data for {_activeProfileName}");
} }
catch (Exception e) catch (Exception)
{ {
Logger.LogError($"Couldn't create save data:\n{e}"); try
{
Logger.LogVerbose($"Couldn't load save data from {FileName}, creating a new file");
_saveFile = new NewHorizonsSaveFile();
_saveFile.Profiles.Add(_activeProfileName, new NewHorizonsProfile());
_activeProfile = _saveFile.Profiles[_activeProfileName];
Main.Instance.ModHelper.Storage.Save(_saveFile, FileName);
Logger.LogVerbose($"Loaded save data for {_activeProfileName}");
}
catch (Exception e)
{
Logger.LogError($"Couldn't create save data:\n{e}");
}
} }
} }
} }
@ -52,7 +57,19 @@ namespace NewHorizons.External
public static void Save() public static void Save()
{ {
if (_saveFile == null) return; if (_saveFile == null) return;
Main.Instance.ModHelper.Storage.Save(_saveFile, FileName);
// Threads exist
lock (_lock)
{
try
{
Main.Instance.ModHelper.Storage.Save(_saveFile, FileName);
}
catch (Exception ex)
{
Logger.LogError($"Couldn't save data:\n{ex}");
}
}
} }
public static void Reset() public static void Reset()