0.2.0: Server-Browser und Spiel-Ticket

- Seite Server: Liste der Website, Ping/Spieler direkt abgefragt, BEITRETEN und Direkt verbinden
- Holt beim Spielen ein Spiel-Ticket und schreibt es in BSO.json

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-10-28 18:10:55 +01:00
co-authored by Claude Opus 5.5
parent 077e39fa8a
commit fb8d6d6844
8 changed files with 174 additions and 7 deletions
+14
View File
@@ -71,6 +71,20 @@ public static class Api
public static Task<Result> Logout(string token) => Send(HttpMethod.Post, "/api/auth/logout", new { }, token);
/// <summary>Spiel-Ticket fuer die Mod (signiert, 24 h) -> { ticket, username, expiresUtc }</summary>
public static Task<Result> Ticket(string token) => Send(HttpMethod.Post, "/api/auth/ticket", new { }, token);
public sealed record ServerEntry(string name, string description, string address, string host, int port, int players, int maxPlayers,
string version, int protocol, string onlineSinceUtc);
/// <summary>Oeffentliche, erreichbare Server (Serverliste der Website).</summary>
public static async Task<List<ServerEntry>> Servers()
{
var r = await Send(HttpMethod.Get, "/api/servers", null, null);
if (!r.Ok) throw new Exception(r.Error ?? "Serverliste nicht verfuegbar");
return r.Json.GetProperty("servers").Deserialize<List<ServerEntry>>() ?? new List<ServerEntry>();
}
public static async Task<Release?> Latest(string product)
{
var r = await Send(HttpMethod.Get, "/api/releases/latest?product=" + product, null, null);
+3 -2
View File
@@ -85,8 +85,8 @@ public static class Game
LauncherLog.Info("BSO installiert nach " + bin);
}
/// <summary>Einstellungen der Mod (BSO\BSO.json) - Name = Kontoname, Server, automatisch verbinden.</summary>
public static void WriteModConfig(string folder, string? name, string server, bool autoConnect)
/// <summary>Einstellungen der Mod (BSO\BSO.json) - Name = Kontoname, Server, automatisch verbinden, Spiel-Ticket.</summary>
public static void WriteModConfig(string folder, string? name, string server, bool autoConnect, string? ticket)
{
string file = Path.Combine(Bin(folder), "BSO", "BSO.json");
JsonObject json;
@@ -95,6 +95,7 @@ public static class Game
if (!string.IsNullOrEmpty(name)) json["Name"] = name;
json["Server"] = server;
json["AutoConnect"] = autoConnect;
json["Ticket"] = ticket;
Directory.CreateDirectory(Path.GetDirectoryName(file)!);
File.WriteAllText(file, json.ToJsonString(new JsonSerializerOptions { WriteIndented = true }));
}
+38
View File
@@ -0,0 +1,38 @@
using System.Diagnostics;
using System.Net.Sockets;
using System.Text;
namespace BSO.Launcher;
/// <summary>
/// Fragt einen BSO-Server direkt ab wie die Mod (wie ServerBrowser.cs bei MSCO): Info senden, InfoReply lesen. Liefert die
/// Laufzeit (Ping) und den aktuellen Stand (Spieler) - die Liste der Website ist bis zu einer Minute alt.
/// Paketaufbau: [int Laenge][byte Typ][Daten], Info = 9, InfoReply = 10 (string Name, string Version, int Spieler, int max).
/// </summary>
public static class ServerPing
{
public sealed record Info(int Ping, string Name, string Version, int Players, int MaxPlayers);
public static async Task<Info?> Query(string host, int port, int timeoutMs = 2500)
{
try
{
using var tcp = new TcpClient { NoDelay = true };
using var cts = new CancellationTokenSource(timeoutMs);
var clock = Stopwatch.StartNew();
await tcp.ConnectAsync(host, port, cts.Token);
var stream = tcp.GetStream();
await stream.WriteAsync(new byte[] { 1, 0, 0, 0, 9 }, cts.Token);
var head = new byte[5];
await stream.ReadExactlyAsync(head, cts.Token);
int ping = (int)clock.ElapsedMilliseconds;
int length = BitConverter.ToInt32(head, 0);
if (head[4] != 10 || length < 1 || length > 4096) return null;
var payload = new byte[length - 1];
await stream.ReadExactlyAsync(payload, cts.Token);
using var r = new BinaryReader(new MemoryStream(payload), Encoding.UTF8);
return new Info(ping, r.ReadString(), r.ReadString(), r.ReadInt32(), r.ReadInt32());
}
catch { return null; }
}
}