From bf15f51eb3a6cb126e76300c8b25b46740735e67 Mon Sep 17 00:00:00 2001 From: Sanae Date: Thu, 17 Feb 2022 20:39:48 -0600 Subject: [PATCH] Use Json.NET instead of Tomlyn for settings file Tomlyn can't cope with Guids, so moving to Json.NET --- Server/Program.cs | 18 +++++++++++++---- Server/Server.csproj | 2 +- Server/Settings.cs | 46 ++++++++++++++++---------------------------- 3 files changed, 32 insertions(+), 34 deletions(-) diff --git a/Server/Program.cs b/Server/Program.cs index 47291f1..d9c9f0f 100644 --- a/Server/Program.cs +++ b/Server/Program.cs @@ -3,7 +3,6 @@ using System.Numerics; using Server; using Shared; using Shared.Packet.Packets; -using Tomlyn; using Timer = System.Timers.Timer; Server.Server server = new Server.Server(); @@ -85,8 +84,9 @@ CommandHandler.RegisterCommand("flip", args => { Settings.Instance.Flip.Players.Add(result); Settings.SaveSettings(); return $"Added {result} to flipped players"; - } else - return $"Invalid user id {args[1]}"; + } + + return $"Invalid user id {args[1]}"; } case "remove" when args.Length == 2: { if (Guid.TryParse(args[1], out Guid result)) { @@ -119,7 +119,7 @@ CommandHandler.RegisterCommand("flip", args => { } }); -CommandHandler.RegisterCommand("shine", (args) => { +CommandHandler.RegisterCommand("shine", args => { const string optionUsage = "Valid options: list"; if (args.Length < 1) return optionUsage; @@ -131,6 +131,16 @@ CommandHandler.RegisterCommand("shine", (args) => { } }); +CommandHandler.RegisterCommand("loadsettings", _ => { + Settings.LoadSettings(); + return "Loaded settings.json"; +}); + +CommandHandler.RegisterCommand("savesettings", _ => { + Settings.SaveSettings(); + return "Saved settings.json"; +}); + Task.Run(() => { Logger logger = new Logger("Console"); logger.Info("Run help command for valid commands."); diff --git a/Server/Server.csproj b/Server/Server.csproj index 9d38e5f..994ae4d 100644 --- a/Server/Server.csproj +++ b/Server/Server.csproj @@ -12,7 +12,7 @@ - + diff --git a/Server/Settings.cs b/Server/Settings.cs index 3b51dc8..1864d71 100644 --- a/Server/Settings.cs +++ b/Server/Settings.cs @@ -1,51 +1,39 @@ using System.Net; +using Newtonsoft.Json; +using Newtonsoft.Json.Converters; +using Newtonsoft.Json.Serialization; using Shared; -using Tomlyn; -using Tomlyn.Model; -using Tomlyn.Syntax; -namespace Server; +namespace Server; public class Settings { public static Settings Instance = new Settings(); private static readonly Logger Logger = new Logger("Settings"); + static Settings() { LoadSettings(); } public static void LoadSettings() { - if (File.Exists("settings.toml")) { - string text = File.ReadAllText("settings.toml"); - if (Toml.TryToModel(text, out Settings? settings, out DiagnosticsBag? bag, options: new TomlModelOptions() { - ConvertTo = (value, _) => { - if (value is string str && Guid.TryParse(str, out Guid result)) - return result; - - return null; - } - })) - Logger.Info("Loaded settings from settings.toml"); - else - Logger.Warn($"Failed to load settings.toml: {bag}"); - if (settings != null) Instance = settings; + if (File.Exists("settings.json")) { + string text = File.ReadAllText("settings.json"); + try { + Instance = JsonConvert.DeserializeObject(text, new StringEnumConverter(new CamelCaseNamingStrategy())) ?? Instance; + Logger.Info("Loaded settings from settings.json"); + } catch (Exception e) { + Logger.Warn($"Failed to load settings.json: {e}"); + } } else { SaveSettings(); } } - public static void SaveSettings(Settings? settings = null) { + public static void SaveSettings() { try { - File.WriteAllText("settings.toml", Toml.FromModel(settings ?? Instance!, new TomlModelOptions { - ConvertTo = (x, _) => { - if (x is Guid guid) - return guid.ToString(); - - return null!; - } - })); - Logger.Info("Saved settings to settings.toml"); + File.WriteAllText("settings.json", JsonConvert.SerializeObject(Instance, Formatting.Indented, new StringEnumConverter(new CamelCaseNamingStrategy()))); + Logger.Info("Saved settings to settings.json"); } catch (Exception e) { - Logger.Error($"Failed to save settings.toml {e}"); + Logger.Error($"Failed to save settings.json {e}"); } }