85 lines
4.0 KiB
C#
85 lines
4.0 KiB
C#
using System.Text.Json;
|
|
|
|
namespace MSCO.Server;
|
|
|
|
/// <summary>server.json - wird beim ersten Start mit Standardwerten angelegt.</summary>
|
|
public sealed class ServerConfig
|
|
{
|
|
public string LicenseKey { get; set; } = "";
|
|
public string Name { get; set; } = "My MSCO Server";
|
|
public string Description { get; set; } = "";
|
|
public int Port { get; set; } = 7777;
|
|
/// <summary>
|
|
/// Adresse, unter der Spieler den Server erreichen (IP oder Domain). Leer = die IP, von der
|
|
/// sich der Server beim Master meldet. Nötig z.B. wenn Master und Server im selben Docker-Netz laufen.
|
|
/// </summary>
|
|
public string PublicAddress { get; set; } = "";
|
|
public int MaxPlayers { get; set; } = 8;
|
|
/// <summary>In der Serverliste anzeigen.</summary>
|
|
public bool Public { get; set; } = true;
|
|
/// <summary>Nur Spieler mit Steam zulassen (false = auch ohne Steam, "Non-Steam"-Server).</summary>
|
|
public bool RequireSteam { get; set; } = true;
|
|
/// <summary>Weltzustand so oft auf die Festplatte schreiben.</summary>
|
|
public int AutosaveSeconds { get; set; } = 60;
|
|
/// <summary>Optional: Steam Web API Key, um Steam-Tickets der Spieler bei Steam zu prüfen.</summary>
|
|
public string SteamWebApiKey { get; set; } = "";
|
|
/// <summary>Gesperrte SteamID64s.</summary>
|
|
public List<string> BannedSteamIds { get; set; } = new();
|
|
/// <summary>Admins (SteamID64): dürfen im Spiel mit /admin Spieler kicken und bannen.</summary>
|
|
public List<string> Admins { get; set; } = new();
|
|
|
|
static readonly JsonSerializerOptions Options = new() { WriteIndented = true };
|
|
|
|
public static ServerConfig LoadOrCreate(string path, out bool created)
|
|
{
|
|
created = !File.Exists(path);
|
|
if (created)
|
|
{
|
|
var fresh = new ServerConfig();
|
|
fresh.Save(path);
|
|
return fresh;
|
|
}
|
|
var config = JsonSerializer.Deserialize<ServerConfig>(File.ReadAllText(path), Options) ?? new ServerConfig();
|
|
// Neue Einstellungen (z.B. Admins) auch in ältere server.json schreiben, damit man sie sieht
|
|
try { config.Save(path); } catch (Exception) { } // z.B. schreibgeschützt - dann eben nicht
|
|
return config;
|
|
}
|
|
|
|
public void Save(string path) => File.WriteAllText(path, JsonSerializer.Serialize(this, Options));
|
|
|
|
/// <summary>
|
|
/// Umgebungsvariablen überschreiben server.json (praktisch für Docker/systemd):
|
|
/// MSCO_LICENSE_KEY, MSCO_NAME, MSCO_DESCRIPTION, MSCO_PORT, MSCO_MAX_PLAYERS, MSCO_PUBLIC,
|
|
/// MSCO_PUBLIC_ADDRESS, MSCO_STEAM_WEB_API_KEY (die Master-Adresse ist fest, siehe MasterServer)
|
|
/// </summary>
|
|
/// <returns>Namen der gesetzten Umgebungsvariablen (fürs Log, damit klar ist, woher ein Wert kommt).</returns>
|
|
public List<string> ApplyEnvironment()
|
|
{
|
|
var used = new List<string>();
|
|
string Take(string name, string current)
|
|
{
|
|
string value = Env(name);
|
|
if (value == null) return current;
|
|
used.Add(name);
|
|
return value;
|
|
}
|
|
|
|
LicenseKey = Take("MSCO_LICENSE_KEY", LicenseKey);
|
|
Name = Take("MSCO_NAME", Name);
|
|
Description = Take("MSCO_DESCRIPTION", Description);
|
|
PublicAddress = Take("MSCO_PUBLIC_ADDRESS", PublicAddress);
|
|
SteamWebApiKey = Take("MSCO_STEAM_WEB_API_KEY", SteamWebApiKey);
|
|
if (int.TryParse(Env("MSCO_PORT"), out int port)) { Port = port; used.Add("MSCO_PORT"); }
|
|
if (int.TryParse(Env("MSCO_MAX_PLAYERS"), out int max)) { MaxPlayers = max; used.Add("MSCO_MAX_PLAYERS"); }
|
|
if (bool.TryParse(Env("MSCO_PUBLIC"), out bool isPublic)) { Public = isPublic; used.Add("MSCO_PUBLIC"); }
|
|
if (bool.TryParse(Env("MSCO_REQUIRE_STEAM"), out bool requireSteam)) { RequireSteam = requireSteam; used.Add("MSCO_REQUIRE_STEAM"); }
|
|
return used;
|
|
}
|
|
|
|
static string Env(string name)
|
|
{
|
|
string value = Environment.GetEnvironmentVariable(name);
|
|
return string.IsNullOrWhiteSpace(value) ? null : value.Trim();
|
|
}
|
|
}
|