重构 GameSettings

master
lightyears 2 years ago
parent c873f9806f
commit b35d57ef57
Signed by: lightyears
GPG Key ID: 98D80DDF26D4F2F9

1
.gitignore vendored

@ -4,6 +4,7 @@
# IDE # IDE
.vscode .vscode
.vs .vs
Properties/launchSettings.json
# Temporary files # Temporary files
Temp Temp

@ -1,50 +1,88 @@
using Newtonsoft.Json; using Newtonsoft.Json;
using System.Runtime;
namespace CMSGame namespace CMSGame
{ {
/// <summary> /// <summary>
/// ÓÎÏ·ÉèÖó־û¯ /// 游戏设置持久化
///
/// TODO Ìí¼Ó VideoSettings ²¢Öع¹
/// </summary> /// </summary>
public partial class GameSettings : Node public partial class GameSettings : Node
{ {
public BattleSettings? OriginalBattleSettings; protected Dictionary<Type, SettingsBase> CurrentSettings = new();
public BattleSettings BattleSettings { set; get; } = new(); protected Dictionary<Type, SettingsBase> OriginalSettings = new();
protected string BattleSettingsSavePath = new GodotPath("user://Settings/BattleSettings.json"); protected Dictionary<Type, string> SettingsPaths = new();
public BattleSettings BattleSettings => GetSettings<BattleSettings>();
public VideoSettings VideoSettings => GetSettings<VideoSettings>();
public GameSettings()
{
RegisterAllSettings();
}
public override void _Ready() public override void _Ready()
{ {
MakeDirectories(); MakeDirectories();
LoadAllSettings();
}
OriginalBattleSettings = GetSettings<BattleSettings>(BattleSettingsSavePath); protected void RegisterAllSettings()
BattleSettings = OriginalBattleSettings with { }; {
RegisterSettings<BattleSettings>("BattleSettings.json");
RegisterSettings<VideoSettings>("VideoSettings.json");
}
protected void RegisterSettings<TSettings>(string filename) where TSettings : SettingsBase, new()
{
var defaultSettings = new TSettings();
CurrentSettings.Add(typeof(TSettings), defaultSettings);
SettingsPaths.Add(typeof(TSettings), new GodotPath($"user://Settings/{filename}"));
}
protected void LoadAllSettings()
{
foreach (var settingsType in SettingsPaths.Keys)
{
LoadSettings(settingsType);
}
} }
public override void _ExitTree() public override void _ExitTree()
{ {
if (BattleSettings != OriginalBattleSettings) foreach (var settingsType in SettingsPaths.Keys)
{ {
SaveSettings(BattleSettingsSavePath); if (!OriginalSettings.ContainsKey(settingsType) || OriginalSettings[settingsType] != CurrentSettings[settingsType])
{
SaveSettings(settingsType);
}
} }
} }
private void MakeDirectories() private static void MakeDirectories()
{ {
DirAccess.MakeDirRecursiveAbsolute("user://Settings/"); DirAccess.MakeDirRecursiveAbsolute("user://Settings/");
} }
private TSettings GetSettings<TSettings>(string path) private void LoadSettings(Type settingsType)
where TSettings : SettingsBase, new()
{ {
string settings_text = ReadFileAsString(path); string settingsText = ReadFileAsString(SettingsPaths[settingsType]);
var settings = JsonConvert.DeserializeObject<TSettings>(settings_text) ?? new TSettings(); var settings = JsonConvert.DeserializeObject(settingsText, settingsType);
return settings; if (settings != null)
{
OriginalSettings[settingsType] = (SettingsBase)settings;
CurrentSettings[settingsType] = (SettingsBase)settings;
}
} }
private string ReadFileAsString(string path) public TSettings GetSettings<TSettings>() where TSettings : SettingsBase, new()
{
return (TSettings)CurrentSettings[typeof(TSettings)];
}
private static string ReadFileAsString(string path)
{ {
if (FileAccess.FileExists(path)) if (FileAccess.FileExists(path))
{ {
@ -54,11 +92,11 @@ namespace CMSGame
return "null"; return "null";
} }
private void SaveSettings(string path) private void SaveSettings(Type settingsType)
{ {
string settings_text = JsonConvert.SerializeObject(BattleSettings); string settingsText = JsonConvert.SerializeObject(CurrentSettings[settingsType]);
using var file = FileAccess.Open(path, FileAccess.ModeFlags.Write); using var file = FileAccess.Open(SettingsPaths[settingsType], FileAccess.ModeFlags.Write);
file.StoreString(settings_text); file.StoreString(settingsText);
} }
} }
@ -67,7 +105,8 @@ namespace CMSGame
} }
public record class BattleSettings : SettingsBase public record class BattleSettings : SettingsBase
{ { }
public bool PauseBattleWhenCharacterIsSelected = true;
} public record class VideoSettings : SettingsBase
{ }
} }

@ -1,9 +0,0 @@
{
"profiles": {
"CMSGame": {
"commandName": "Executable",
"executablePath": "C:\\Users\\light\\AppData\\Local\\Programs\\Godot\\Godot_v4.0.2-stable_mono_win64\\Godot_v4.0.2-stable_mono_win64.exe",
"workingDirectory": "."
}
}
}
Loading…
Cancel
Save