mirror of
https://github.com/Sanae6/SmoOnlineServer.git
synced 2024-11-25 04:35:18 +00:00
Use Json.NET instead of Tomlyn for settings file
Tomlyn can't cope with Guids, so moving to Json.NET
This commit is contained in:
parent
821301e756
commit
bf15f51eb3
3 changed files with 32 additions and 34 deletions
|
@ -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,7 +84,8 @@ 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]}";
|
||||
}
|
||||
case "remove" when args.Length == 2: {
|
||||
|
@ -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.");
|
||||
|
|
|
@ -12,7 +12,7 @@
|
|||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Tomlyn" Version="0.11.0" />
|
||||
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
|
|
@ -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;
|
||||
|
||||
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;
|
||||
if (File.Exists("settings.json")) {
|
||||
string text = File.ReadAllText("settings.json");
|
||||
try {
|
||||
Instance = JsonConvert.DeserializeObject<Settings>(text, new StringEnumConverter(new CamelCaseNamingStrategy())) ?? Instance;
|
||||
Logger.Info("Loaded settings from settings.json");
|
||||
} catch (Exception e) {
|
||||
Logger.Warn($"Failed to load settings.json: {e}");
|
||||
}
|
||||
}))
|
||||
Logger.Info("Loaded settings from settings.toml");
|
||||
else
|
||||
Logger.Warn($"Failed to load settings.toml: {bag}");
|
||||
if (settings != null) Instance = settings;
|
||||
} 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}");
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue