mirror of
https://github.com/Sanae6/SmoOnlineServer.git
synced 2024-11-28 22:23:05 +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 Server;
|
||||||
using Shared;
|
using Shared;
|
||||||
using Shared.Packet.Packets;
|
using Shared.Packet.Packets;
|
||||||
using Tomlyn;
|
|
||||||
using Timer = System.Timers.Timer;
|
using Timer = System.Timers.Timer;
|
||||||
|
|
||||||
Server.Server server = new Server.Server();
|
Server.Server server = new Server.Server();
|
||||||
|
@ -85,7 +84,8 @@ CommandHandler.RegisterCommand("flip", args => {
|
||||||
Settings.Instance.Flip.Players.Add(result);
|
Settings.Instance.Flip.Players.Add(result);
|
||||||
Settings.SaveSettings();
|
Settings.SaveSettings();
|
||||||
return $"Added {result} to flipped players";
|
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: {
|
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";
|
const string optionUsage = "Valid options: list";
|
||||||
if (args.Length < 1)
|
if (args.Length < 1)
|
||||||
return optionUsage;
|
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(() => {
|
Task.Run(() => {
|
||||||
Logger logger = new Logger("Console");
|
Logger logger = new Logger("Console");
|
||||||
logger.Info("Run help command for valid commands.");
|
logger.Info("Run help command for valid commands.");
|
||||||
|
|
|
@ -12,7 +12,7 @@
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<PackageReference Include="Tomlyn" Version="0.11.0" />
|
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
</Project>
|
</Project>
|
||||||
|
|
|
@ -1,51 +1,39 @@
|
||||||
using System.Net;
|
using System.Net;
|
||||||
|
using Newtonsoft.Json;
|
||||||
|
using Newtonsoft.Json.Converters;
|
||||||
|
using Newtonsoft.Json.Serialization;
|
||||||
using Shared;
|
using Shared;
|
||||||
using Tomlyn;
|
|
||||||
using Tomlyn.Model;
|
|
||||||
using Tomlyn.Syntax;
|
|
||||||
|
|
||||||
namespace Server;
|
namespace Server;
|
||||||
|
|
||||||
public class Settings {
|
public class Settings {
|
||||||
public static Settings Instance = new Settings();
|
public static Settings Instance = new Settings();
|
||||||
private static readonly Logger Logger = new Logger("Settings");
|
private static readonly Logger Logger = new Logger("Settings");
|
||||||
|
|
||||||
static Settings() {
|
static Settings() {
|
||||||
LoadSettings();
|
LoadSettings();
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void LoadSettings() {
|
public static void LoadSettings() {
|
||||||
if (File.Exists("settings.toml")) {
|
if (File.Exists("settings.json")) {
|
||||||
string text = File.ReadAllText("settings.toml");
|
string text = File.ReadAllText("settings.json");
|
||||||
if (Toml.TryToModel(text, out Settings? settings, out DiagnosticsBag? bag, options: new TomlModelOptions() {
|
try {
|
||||||
ConvertTo = (value, _) => {
|
Instance = JsonConvert.DeserializeObject<Settings>(text, new StringEnumConverter(new CamelCaseNamingStrategy())) ?? Instance;
|
||||||
if (value is string str && Guid.TryParse(str, out Guid result))
|
Logger.Info("Loaded settings from settings.json");
|
||||||
return result;
|
} catch (Exception e) {
|
||||||
|
Logger.Warn($"Failed to load settings.json: {e}");
|
||||||
return null;
|
|
||||||
}
|
}
|
||||||
}))
|
|
||||||
Logger.Info("Loaded settings from settings.toml");
|
|
||||||
else
|
|
||||||
Logger.Warn($"Failed to load settings.toml: {bag}");
|
|
||||||
if (settings != null) Instance = settings;
|
|
||||||
} else {
|
} else {
|
||||||
SaveSettings();
|
SaveSettings();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void SaveSettings(Settings? settings = null) {
|
public static void SaveSettings() {
|
||||||
try {
|
try {
|
||||||
File.WriteAllText("settings.toml", Toml.FromModel(settings ?? Instance!, new TomlModelOptions {
|
File.WriteAllText("settings.json", JsonConvert.SerializeObject(Instance, Formatting.Indented, new StringEnumConverter(new CamelCaseNamingStrategy())));
|
||||||
ConvertTo = (x, _) => {
|
Logger.Info("Saved settings to settings.json");
|
||||||
if (x is Guid guid)
|
|
||||||
return guid.ToString();
|
|
||||||
|
|
||||||
return null!;
|
|
||||||
}
|
|
||||||
}));
|
|
||||||
Logger.Info("Saved settings to settings.toml");
|
|
||||||
} catch (Exception e) {
|
} 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