❇️ Added Settings classes to save and load local configurations
This commit is contained in:
@@ -0,0 +1,108 @@
|
||||
using System;
|
||||
using System.Text.Json;
|
||||
using Godot;
|
||||
|
||||
namespace Babushka.scripts.CSharp.Common.Savegame;
|
||||
|
||||
/// <summary>
|
||||
/// Handles the saving and loading of local settings data.
|
||||
/// </summary>
|
||||
[GlobalClass]
|
||||
public partial class SettingsSaveController : Node
|
||||
{
|
||||
public static string SETTINGS_FILE_PATH = "user://userSettings.json";
|
||||
public static SettingsSaveController Instance;
|
||||
|
||||
public SettingsData? settings = new SettingsData();
|
||||
public SettingsData settingsDefault = new SettingsData();
|
||||
|
||||
public event Action OnSettingsReloaded;
|
||||
|
||||
private bool _loadedData;
|
||||
private DateTime _readyTime;
|
||||
|
||||
public bool LoadedData => _loadedData;
|
||||
|
||||
public override void _EnterTree()
|
||||
{
|
||||
SETTINGS_FILE_PATH = ProjectSettings.GlobalizePath(SETTINGS_FILE_PATH);
|
||||
_readyTime = DateTime.Now;
|
||||
Instance = this;
|
||||
LoadSettings();
|
||||
}
|
||||
|
||||
public override void _ExitTree()
|
||||
{
|
||||
if (settings != null)
|
||||
settings.runtimeSeconds += (DateTime.Now - _readyTime).TotalSeconds;
|
||||
SaveSettings();
|
||||
Instance = null;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Saves Settings Data onto disk.
|
||||
/// </summary>
|
||||
public void SaveSettings()
|
||||
{
|
||||
try
|
||||
{
|
||||
string jsonString = JsonSerializer.Serialize(
|
||||
settings,
|
||||
new JsonSerializerOptions() { WriteIndented = true}
|
||||
);
|
||||
|
||||
System.IO.File.WriteAllText(SETTINGS_FILE_PATH, jsonString);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
GD.PrintErr("Error Saving Settings:", e);
|
||||
Console.WriteLine(e);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Loads Settings data from disk.
|
||||
/// </summary>
|
||||
public void LoadSettings()
|
||||
{
|
||||
_loadedData = false;
|
||||
|
||||
try
|
||||
{
|
||||
if (!System.IO.File.Exists(SETTINGS_FILE_PATH))
|
||||
{
|
||||
settings = new SettingsData();
|
||||
}
|
||||
else
|
||||
{
|
||||
string jsonString = System.IO.File.ReadAllText(SETTINGS_FILE_PATH);
|
||||
SettingsData? loadedSettings = JsonSerializer.Deserialize<SettingsData>(jsonString);
|
||||
if (loadedSettings != null && !loadedSettings.IsVersionValid())
|
||||
{
|
||||
_loadedData = false;
|
||||
}
|
||||
else
|
||||
{
|
||||
settings = loadedSettings;
|
||||
_loadedData = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
GD.PrintErr("Loading Error:", e);
|
||||
Console.WriteLine(e);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Resets Settings to default.
|
||||
/// </summary>
|
||||
public void ResetSettings()
|
||||
{
|
||||
settings = JsonSerializer.Deserialize<SettingsData>(JsonSerializer.Serialize<SettingsData>(settingsDefault));
|
||||
OnSettingsReloaded?.Invoke();
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user