using System; using System.IO; using System.Text.Json; using System.Text.Json.Serialization; namespace CommonwealthOnline.Host.Services; // Mirrors CommonwealthOnline.Server Configuration. Keep the JSON shape aligned // with the server's own serializer so a config saved here loads there. public sealed class ServerConfig { public string Host { get; set; } = "0.0.0.0"; public int Port { get; set; } = 7777; public string ServerName { get; set; } = "Commonwealth Online Server"; public string ServerDescription { get; set; } = string.Empty; public int MaxPlayers { get; set; } = 16; public string LogVerbosity { get; set; } = "info"; public int AdminPort { get; set; } = 7779; public bool EnableGnsTransport { get; set; } public string? GnsBridgePath { get; set; } private static readonly JsonSerializerOptions Options = new() { WriteIndented = true, PropertyNamingPolicy = JsonNamingPolicy.CamelCase, DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull, }; public static ServerConfig Load(string path) { try { if (File.Exists(path)) { return JsonSerializer.Deserialize(File.ReadAllText(path), Options) ?? new ServerConfig(); } } catch (Exception) { // Fall back to defaults on unreadable/invalid config. } return new ServerConfig(); } public void Save(string path) => File.WriteAllText(path, JsonSerializer.Serialize(this, Options)); }