From 00fca1bce0d35437007b3941b069e5f06aa39320 Mon Sep 17 00:00:00 2001 From: lightyears Date: Mon, 20 Feb 2023 11:20:25 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E5=AD=98=E5=82=A8=E9=80=BB?= =?UTF-8?q?=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 只在时游戏设置有变动时写入磁盘。 --- Autoloads/GameSettings.cs | 24 ++++++++++++++---------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/Autoloads/GameSettings.cs b/Autoloads/GameSettings.cs index adc73ed..e8b4bb3 100644 --- a/Autoloads/GameSettings.cs +++ b/Autoloads/GameSettings.cs @@ -4,6 +4,8 @@ namespace CMSGame { public partial class GameSettings : Node { + public BattleSettings OriginalBattleSettings; + public BattleSettings BattleSettings; protected string BattleSettingsSavePath = new GodotPath("user://Settings/BattleSettings.json"); @@ -11,12 +13,17 @@ namespace CMSGame public override void _Ready() { MakeDirectories(); - BattleSettings = GetSettings(BattleSettingsSavePath); + + OriginalBattleSettings = GetSettings(BattleSettingsSavePath); + BattleSettings = OriginalBattleSettings with { }; } public override void _ExitTree() { - SaveSettings(BattleSettingsSavePath); + if (BattleSettings != OriginalBattleSettings) + { + SaveSettings(BattleSettingsSavePath); + } } private void MakeDirectories() @@ -24,10 +31,11 @@ namespace CMSGame DirAccess.MakeDirRecursiveAbsolute("user://Settings/"); } - private SettingsType GetSettings(string path) where SettingsType : new() + private TSettings GetSettings(string path) + where TSettings : SettingsBase, new() { string settings_text = ReadFileAsString(path); - var settings = JsonConvert.DeserializeObject(settings_text) ?? new SettingsType(); + var settings = JsonConvert.DeserializeObject(settings_text) ?? new TSettings(); return settings; } @@ -49,15 +57,11 @@ namespace CMSGame } } - public class SettingsBase + public record class SettingsBase { - public override string ToString() - { - return JsonConvert.SerializeObject(this); - } } - public class BattleSettings : SettingsBase + public record class BattleSettings : SettingsBase { public bool PauseBattleWhenCharacterIsSelected = true; }