Static utility for loading and saving GameSettings to the game's data folder using FileSystem.Data JSON helpers. It returns defaults on missing/corrupt files, clamps loaded settings, logs warnings on failures, and writes settings with exception handling.
namespace Coilgarden;
/// <summary>
/// Reads and writes <see cref="GameSettings"/> to the game's data folder.
/// <para>
/// Best effort, always, exactly like <see cref="HighScoreStore"/>: a missing, unreadable or
/// corrupt file falls back to designed defaults rather than stopping the player getting into a
/// game, and every failure is logged so it is not silent.
/// </para>
/// </summary>
public static class SettingsStore
{
public const string FileName = "coilgarden-settings.json";
public static GameSettings Load()
{
try
{
if ( !FileSystem.Data.FileExists( FileName ) ) return new GameSettings();
var loaded = FileSystem.Data.ReadJsonOrDefault<GameSettings>( FileName, null );
if ( loaded is null )
{
Log.Warning( $"Coilgarden: {FileName} could not be read, starting from defaults." );
return new GameSettings();
}
loaded.Clamp();
return loaded;
}
catch ( Exception e )
{
Log.Warning( $"Coilgarden: {FileName} is unreadable ({e.Message}), starting from defaults." );
return new GameSettings();
}
}
public static void Save( GameSettings settings )
{
if ( settings is null ) return;
try
{
FileSystem.Data.WriteJson( FileName, settings );
}
catch ( Exception e )
{
Log.Warning( $"Coilgarden: could not save {FileName} ({e.Message})" );
}
}
}