You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
69 lines
1.9 KiB
69 lines
1.9 KiB
using Newtonsoft.Json;
|
|
|
|
namespace CMSGame
|
|
{
|
|
public partial class GameSettings : Node
|
|
{
|
|
public BattleSettings? OriginalBattleSettings;
|
|
|
|
public BattleSettings? BattleSettings;
|
|
|
|
protected string BattleSettingsSavePath = new GodotPath("user://Settings/BattleSettings.json");
|
|
|
|
public override void _Ready()
|
|
{
|
|
MakeDirectories();
|
|
|
|
OriginalBattleSettings = GetSettings<BattleSettings>(BattleSettingsSavePath);
|
|
BattleSettings = OriginalBattleSettings with { };
|
|
}
|
|
|
|
public override void _ExitTree()
|
|
{
|
|
if (BattleSettings != OriginalBattleSettings)
|
|
{
|
|
SaveSettings(BattleSettingsSavePath);
|
|
}
|
|
}
|
|
|
|
private void MakeDirectories()
|
|
{
|
|
DirAccess.MakeDirRecursiveAbsolute("user://Settings/");
|
|
}
|
|
|
|
private TSettings GetSettings<TSettings>(string path)
|
|
where TSettings : SettingsBase, new()
|
|
{
|
|
string settings_text = ReadFileAsString(path);
|
|
var settings = JsonConvert.DeserializeObject<TSettings>(settings_text) ?? new TSettings();
|
|
return settings;
|
|
}
|
|
|
|
private string ReadFileAsString(string path)
|
|
{
|
|
if (FileAccess.FileExists(path))
|
|
{
|
|
using var file = FileAccess.Open(path, FileAccess.ModeFlags.Read);
|
|
return file.GetAsText();
|
|
}
|
|
return "null";
|
|
}
|
|
|
|
private void SaveSettings(string path)
|
|
{
|
|
string settings_text = JsonConvert.SerializeObject(BattleSettings);
|
|
using var file = FileAccess.Open(path, FileAccess.ModeFlags.Write);
|
|
file.StoreString(settings_text);
|
|
}
|
|
}
|
|
|
|
public record class SettingsBase
|
|
{
|
|
}
|
|
|
|
public record class BattleSettings : SettingsBase
|
|
{
|
|
public bool PauseBattleWhenCharacterIsSelected = true;
|
|
}
|
|
}
|