From b35d57ef571be1f0345d93a2d42889dfff30b05b Mon Sep 17 00:00:00 2001 From: lightyears Date: Fri, 2 Jun 2023 14:33:44 +0800 Subject: [PATCH] =?UTF-8?q?=E9=87=8D=E6=9E=84=20GameSettings?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 1 + Autoloads/GameSettings.cs | 87 ++++++++++++++++++++++++---------- Properties/.gdignore | 0 Properties/launchSettings.json | 9 ---- 4 files changed, 64 insertions(+), 33 deletions(-) create mode 100644 Properties/.gdignore delete mode 100644 Properties/launchSettings.json diff --git a/.gitignore b/.gitignore index 70364ff..418a8b8 100644 --- a/.gitignore +++ b/.gitignore @@ -4,6 +4,7 @@ # IDE .vscode .vs +Properties/launchSettings.json # Temporary files Temp diff --git a/Autoloads/GameSettings.cs b/Autoloads/GameSettings.cs index 32fa19e..4269602 100644 --- a/Autoloads/GameSettings.cs +++ b/Autoloads/GameSettings.cs @@ -1,50 +1,88 @@ using Newtonsoft.Json; +using System.Runtime; namespace CMSGame { /// - /// 游戏设置持久化 - /// - /// TODO 添加 VideoSettings 并重构 + /// 娓告垙璁剧疆鎸佷箙鍖 /// public partial class GameSettings : Node { - public BattleSettings? OriginalBattleSettings; + protected Dictionary CurrentSettings = new(); - public BattleSettings BattleSettings { set; get; } = new(); + protected Dictionary OriginalSettings = new(); - protected string BattleSettingsSavePath = new GodotPath("user://Settings/BattleSettings.json"); + protected Dictionary SettingsPaths = new(); + + public BattleSettings BattleSettings => GetSettings(); + + public VideoSettings VideoSettings => GetSettings(); + + public GameSettings() + { + RegisterAllSettings(); + } public override void _Ready() { MakeDirectories(); + LoadAllSettings(); + } - OriginalBattleSettings = GetSettings(BattleSettingsSavePath); - BattleSettings = OriginalBattleSettings with { }; + protected void RegisterAllSettings() + { + RegisterSettings("BattleSettings.json"); + RegisterSettings("VideoSettings.json"); + } + + protected void RegisterSettings(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() { - 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/"); } - private TSettings GetSettings(string path) - where TSettings : SettingsBase, new() + private void LoadSettings(Type settingsType) { - string settings_text = ReadFileAsString(path); - var settings = JsonConvert.DeserializeObject(settings_text) ?? new TSettings(); - return settings; + string settingsText = ReadFileAsString(SettingsPaths[settingsType]); + var settings = JsonConvert.DeserializeObject(settingsText, settingsType); + if (settings != null) + { + OriginalSettings[settingsType] = (SettingsBase)settings; + CurrentSettings[settingsType] = (SettingsBase)settings; + } } - private string ReadFileAsString(string path) + public TSettings GetSettings() where TSettings : SettingsBase, new() + { + return (TSettings)CurrentSettings[typeof(TSettings)]; + } + + private static string ReadFileAsString(string path) { if (FileAccess.FileExists(path)) { @@ -54,11 +92,11 @@ namespace CMSGame return "null"; } - private void SaveSettings(string path) + private void SaveSettings(Type settingsType) { - string settings_text = JsonConvert.SerializeObject(BattleSettings); - using var file = FileAccess.Open(path, FileAccess.ModeFlags.Write); - file.StoreString(settings_text); + string settingsText = JsonConvert.SerializeObject(CurrentSettings[settingsType]); + using var file = FileAccess.Open(SettingsPaths[settingsType], FileAccess.ModeFlags.Write); + file.StoreString(settingsText); } } @@ -67,7 +105,8 @@ namespace CMSGame } public record class BattleSettings : SettingsBase - { - public bool PauseBattleWhenCharacterIsSelected = true; - } + { } + + public record class VideoSettings : SettingsBase + { } } diff --git a/Properties/.gdignore b/Properties/.gdignore new file mode 100644 index 0000000..e69de29 diff --git a/Properties/launchSettings.json b/Properties/launchSettings.json deleted file mode 100644 index 9db68ed..0000000 --- a/Properties/launchSettings.json +++ /dev/null @@ -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": "." - } - } -} \ No newline at end of file