38 lines
1.5 KiB
C#
38 lines
1.5 KiB
C#
using System;
|
|
using System.IO;
|
|
using System.Text.Json;
|
|
|
|
namespace BSO
|
|
{
|
|
/// <summary>Einstellungen in <Spiel>\BSO\BSO.json - wird beim ersten Start mit Standardwerten angelegt.</summary>
|
|
public class Config
|
|
{
|
|
/// <summary>Name, den die anderen sehen.</summary>
|
|
public string Name { get; set; } = Environment.UserName;
|
|
/// <summary>Server als host oder host:port.</summary>
|
|
public string Server { get; set; } = "127.0.0.1:7777";
|
|
/// <summary>Beim Spielstart automatisch verbinden (sonst F8).</summary>
|
|
public bool AutoConnect { get; set; }
|
|
|
|
public static Config Current { get; private set; } = new();
|
|
|
|
static readonly JsonSerializerOptions Options = new() { WriteIndented = true };
|
|
static string ConfigFile => Path.Combine(Bootstrap.Folder, "BSO.json");
|
|
|
|
public static void Load()
|
|
{
|
|
try
|
|
{
|
|
if (File.Exists(ConfigFile)) Current = JsonSerializer.Deserialize<Config>(File.ReadAllText(ConfigFile), Options) ?? new Config();
|
|
// Immer zurueckschreiben: neue Einstellungen erscheinen so auch in alten Dateien
|
|
File.WriteAllText(ConfigFile, JsonSerializer.Serialize(Current, Options));
|
|
Bootstrap.Log($"Einstellungen: Name \"{Current.Name}\", Server {Current.Server}, AutoConnect {Current.AutoConnect}");
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
Bootstrap.Log("BSO.json nicht lesbar, nehme Standardwerte: " + e.Message);
|
|
}
|
|
}
|
|
}
|
|
}
|