0
0
Fork 0
mirror of https://github.com/Sanae6/SmoOnlineServer.git synced 2024-11-05 03:05:04 +00:00
SmoOnlineServer/Server/Settings.cs
Sanae bf15f51eb3 Use Json.NET instead of Tomlyn for settings file
Tomlyn can't cope with Guids, so moving to Json.NET
2022-02-17 20:39:48 -06:00

53 lines
No EOL
1.8 KiB
C#

using System.Net;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using Newtonsoft.Json.Serialization;
using Shared;
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.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}");
}
} else {
SaveSettings();
}
}
public static void SaveSettings() {
try {
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.json {e}");
}
}
public ServerTable Server { get; set; } = new ServerTable();
public FlipTable Flip { get; set; } = new FlipTable();
public class ServerTable {
public string Address { get; set; } = IPAddress.Any.ToString();
public ushort Port { get; set; } = 1027;
}
public class FlipTable {
public List<Guid> Players { get; set; } = new List<Guid>();
public bool EnabledOnStart { get; set; } = true;
public FlipOptions Pov { get; set; } = FlipOptions.Both;
}
}