From 96620a832ab4a86e49c0a40afc5560b4fe0a8b01 Mon Sep 17 00:00:00 2001 From: Mikal Stordal Date: Wed, 15 Nov 2023 21:10:51 +0100 Subject: [PATCH] fix: use deep clone to copy settings --- Shoko.Server/Settings/SettingsProvider.cs | 26 ++++++++++------------- 1 file changed, 11 insertions(+), 15 deletions(-) diff --git a/Shoko.Server/Settings/SettingsProvider.cs b/Shoko.Server/Settings/SettingsProvider.cs index 087d3cadb..2cda95a35 100644 --- a/Shoko.Server/Settings/SettingsProvider.cs +++ b/Shoko.Server/Settings/SettingsProvider.cs @@ -5,6 +5,7 @@ using System.IO; using System.Linq; using System.Reflection; +using Force.DeepCloner; using Microsoft.Extensions.Logging; using Newtonsoft.Json; using Newtonsoft.Json.Converters; @@ -33,8 +34,8 @@ public SettingsProvider(ILogger logger) public IServerSettings GetSettings(bool copy = false) { - if (copy || Instance == null) - return LoadSettings(copy); + if (Instance == null) LoadSettings(); + if (copy) return Instance.DeepClone(); return Instance; } @@ -44,7 +45,7 @@ public void SaveSettings(IServerSettings settings) SaveSettings(); } - public ServerSettings LoadSettings(bool copy = false) + public void LoadSettings() { var appPath = Utils.ApplicationPath; if (!Directory.Exists(appPath)) @@ -53,19 +54,15 @@ public ServerSettings LoadSettings(bool copy = false) var path = Path.Combine(appPath, SettingsFilename); if (!File.Exists(path)) { - var convertedInstance = File.Exists(Path.Combine(appPath, "settings.json")) + Instance = File.Exists(Path.Combine(appPath, "settings.json")) ? LoadLegacySettings() : new(); - - if (!copy) - SaveSettings(convertedInstance); - return convertedInstance; + SaveSettings(); + return; } - var instance = LoadSettingsFromFile(path); - if (!copy) - SaveSettings(instance); - return instance; + LoadSettingsFromFile(path); + SaveSettings(); } private static ServerSettings LoadLegacySettings() @@ -254,19 +251,18 @@ public static object Deserialize(Type t, string json) return result; } - public ServerSettings LoadSettingsFromFile(string path) + public void LoadSettingsFromFile(string path) { var settings = File.ReadAllText(path); settings = SettingsMigrations.MigrateSettings(settings); settings = FixNonEmittedDefaults(path, settings); try { - return Deserialize(settings); + Instance = Deserialize(settings); } catch (Exception e) { _logger.LogError(e, "An error occurred while loading the settings from file"); - return new(); } }