Skip to content

Commit

Permalink
fix: use deep clone to copy settings
Browse files Browse the repository at this point in the history
  • Loading branch information
revam committed Nov 15, 2023
1 parent fbfa242 commit 96620a8
Showing 1 changed file with 11 additions and 15 deletions.
26 changes: 11 additions & 15 deletions Shoko.Server/Settings/SettingsProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -33,8 +34,8 @@ public SettingsProvider(ILogger<SettingsProvider> 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;
}

Expand All @@ -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))
Expand All @@ -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()
Expand Down Expand Up @@ -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<ServerSettings>(settings);
Instance = Deserialize<ServerSettings>(settings);
}
catch (Exception e)
{
_logger.LogError(e, "An error occurred while loading the settings from file");
return new();
}
}

Expand Down

0 comments on commit 96620a8

Please sign in to comment.