// ============================================================================ // RynthSuite - Plugins/Shared/EngineServerState.cs // RynthCore's answer to "which server is this?" (plugin API v86, // GetServerStateJson), parsed: every server the engine knows (built-in ILT and // Aelrynth, plus each loaded server plugin's declaration), whether it is // active, this character's override, and which plugin owns it (the one to ask // for its settings through "RynthServer.Profile"; RynthILT answers it for "ilt"). // The same verdict the Mini Remote, the menus and the Diagnostics server // picker use, so every plugin follows one choice, overrides included. // // Pure (BCL only): the plugins' test tools compile it. ServerStateClient.cs // polls the engine and the owner plugin. // ============================================================================ using System; using System.Collections.Generic; using System.Text.Json; namespace RynthCore.Plugin.Shared; /// One server in the engine's list. internal sealed record EngineServer(string Id, string Name, bool Active, bool Detected, string Force, bool BuiltIn, string Plugin, string Reason); /// The engine's server list at one moment (GetServerStateJson, schema 1). internal sealed class EngineServerState { /// Engine server ids with a meaning across RynthSuite. public const string IltId = "ilt"; public const string AelrynthId = "aelrynth"; public int Revision { get; private init; } /// The connect host the client was launched with ("" when not known). public string Host { get; private init; } = string.Empty; /// The world name the server announced at login ("" before, or without the hook). public string World { get; private init; } = string.Empty; /// "auto", a server id (picked on the Diagnostics page or "/rc server pick"), or "mixed". public string Pick { get; private init; } = "auto"; /// The active server's id, "" when none is (a retail-like world). public string Active { get; private init; } = string.Empty; public string ActiveName { get; private init; } = string.Empty; /// The plugin that owns the active server ("" when none is loaded). public string ActivePlugin { get; private init; } = string.Empty; public IReadOnlyList Servers { get; private init; } = Array.Empty(); /// True when a server is active. public bool HasActive => Active.Length > 0; /// True when the active server was chosen by hand (a pick), not detected. public bool Picked => !Pick.Equals("auto", StringComparison.OrdinalIgnoreCase); /// The server with , or null when the engine doesn't list it. public EngineServer? Find(string id) { foreach (EngineServer s in Servers) if (s.Id.Equals(id, StringComparison.OrdinalIgnoreCase)) return s; return null; } /// True when is the active server. public bool IsActive(string id) => Active.Equals(id, StringComparison.OrdinalIgnoreCase) && Active.Length > 0; /// /// Whether this world is one of (a user's server list, "InfiniteLeaftide, /// Conquest"): names compare case- and space-insensitively against server ids, names and the world. /// Without the engine ( null) the world name decides. With it, an active /// server decides ("ilt" counts as any Leaftide name); with none active the world name still counts, /// unless it names a server the engine lists, which is then off on purpose (not detected, or this /// character's override). /// public static bool MatchesNameList(EngineServerState? engine, string? world, IReadOnlyList names) { bool Named(string? s) { string n = Norm(s); if (n.Length == 0) return false; foreach (string name in names) if (Norm(name) == n) return true; return false; } bool anyLeaftide = false; foreach (string name in names) if (Norm(name).Contains("leaftide", StringComparison.Ordinal)) anyLeaftide = true; if (engine == null) return Named(world); if (engine.HasActive) return Named(engine.Active) || Named(engine.ActiveName) || (anyLeaftide && engine.IsActive(IltId)); if (!Named(world)) return false; string w = Norm(world); foreach (EngineServer s in engine.Servers) { if (Norm(s.Id) == w || Norm(s.Name) == w) return false; // ILT's world names ("InfiniteLeaftide") are the engine's own ILT detection. if (s.Id.Equals(IltId, StringComparison.OrdinalIgnoreCase) && w.Contains("leaftide", StringComparison.Ordinal)) return false; } return true; } /// Lower case without spaces, so "Infinite Leaftide" equals "InfiniteLeaftide". private static string Norm(string? s) => (s ?? string.Empty).Replace(" ", string.Empty).Trim().ToLowerInvariant(); /// /// Parses GetServerStateJson output. Null for empty or unusable text, or another schema's. /// Missing fields read as empty; nothing throws. /// public static EngineServerState? Parse(string? json) { if (string.IsNullOrWhiteSpace(json)) return null; try { using JsonDocument doc = JsonDocument.Parse(json); JsonElement root = doc.RootElement; if (root.ValueKind != JsonValueKind.Object) return null; if (Int(root, "schema") != 1) return null; var servers = new List(); if (root.TryGetProperty("servers", out JsonElement list) && list.ValueKind == JsonValueKind.Array) { foreach (JsonElement e in list.EnumerateArray()) { if (e.ValueKind != JsonValueKind.Object) continue; string id = Str(e, "id"); if (id.Length == 0) continue; servers.Add(new EngineServer(id, Str(e, "name"), Bool(e, "active"), Bool(e, "detected"), Str(e, "force"), Bool(e, "builtIn"), Str(e, "plugin"), Str(e, "reason"))); } } return new EngineServerState { Revision = Int(root, "revision"), Host = Str(root, "host"), World = Str(root, "world"), Pick = Str(root, "pick") is { Length: > 0 } pick ? pick : "auto", Active = Str(root, "active"), ActiveName = Str(root, "activeName"), ActivePlugin = Str(root, "activePlugin"), Servers = servers, }; } catch (JsonException) { return null; } } internal static string Str(JsonElement e, string name) => e.TryGetProperty(name, out JsonElement v) && v.ValueKind == JsonValueKind.String ? (v.GetString() ?? string.Empty) : string.Empty; internal static bool Bool(JsonElement e, string name) => e.TryGetProperty(name, out JsonElement v) && v.ValueKind == JsonValueKind.True; internal static int Int(JsonElement e, string name) => e.TryGetProperty(name, out JsonElement v) && v.ValueKind == JsonValueKind.Number && v.TryGetInt32(out int n) ? n : 0; } /// /// The headline facts of a server owner's profile JSON ("RynthServer.Profile" from the plugin that /// owns the server), read leniently: a field the owner doesn't send stays "". /// internal sealed record ServerProfileView(string Id, string Name, string Plugin, string Ruleset, string Emulator, string Botting, string BottingNote, string AccountLimit, bool Ilt, int FeaturesOn) { /// Null for empty or unusable text. public static ServerProfileView? Parse(string? json) { if (string.IsNullOrWhiteSpace(json)) return null; try { using JsonDocument doc = JsonDocument.Parse(json); JsonElement root = doc.RootElement; if (root.ValueKind != JsonValueKind.Object) return null; // RynthILT's profile: its feature list, state 1 = on. int on = 0; if (root.TryGetProperty("features", out JsonElement features) && features.ValueKind == JsonValueKind.Array) foreach (JsonElement f in features.EnumerateArray()) if (f.ValueKind == JsonValueKind.Object && EngineServerState.Int(f, "state") == 1) on++; return new ServerProfileView( EngineServerState.Str(root, "id"), EngineServerState.Str(root, "name"), EngineServerState.Str(root, "plugin"), EngineServerState.Str(root, "ruleset"), EngineServerState.Str(root, "emulator"), EngineServerState.Str(root, "botting"), EngineServerState.Str(root, "bottingNote"), EngineServerState.Str(root, "accountLimit"), EngineServerState.Bool(root, "ilt"), on); } catch (JsonException) { return null; } } /// One plain-ASCII line for a status report ("PvE, ACE; botting forbidden - ..."). public string Summary() { var parts = new List(); if (Ruleset.Length > 0 || Emulator.Length > 0) parts.Add(Ruleset.Length > 0 && Emulator.Length > 0 ? Ruleset + ", " + Emulator : Ruleset + Emulator); if (Botting.Length > 0) parts.Add("botting " + Botting + (BottingNote.Length > 0 ? " - " + BottingNote : string.Empty)); if (AccountLimit.Length > 0) parts.Add("accounts: " + AccountLimit); if (Ilt || FeaturesOn > 0) parts.Add((Ilt ? "ILT world" : "not ILT") + ", " + FeaturesOn + " Leaftide features on"); return parts.Count == 0 ? "no details" : string.Join("; ", parts); } } /// /// One salvage from a server plugin's profile "salvage" list (ServerPackInfo.Salvage): a tinkering /// recipe the server adds, for the Mini Remote's Crafting tab. The fields after /// are optional in the JSON; absent, a row behaves as before them. /// /// "applied": an int property of the item that reads 2 once the row is on it (0 = none). /// "prepared": the bag must first be prepared on the chosen tier's Ammunition. /// "order": apply order (lower first); 0 = the kind-based default. /// "afterCap": applied last, only once the item's tink slots are full. /// "stackGroup": rows in one group share per item ("" = none). /// "stackCap": the group's combined cap per item. internal sealed record ServerSalvageView(string Bench, int Material, string Kind, string Effect, bool UsesTinkSlot, string NameMatch, int AppliedInt = 0, bool Prepared = false, int Order = 0, bool AfterCap = false, string StackGroup = "", int StackCap = 0) { /// /// The profile's salvage rows, read leniently: a missing list (an older plugin) is empty; a row /// without a bench or a material id is skipped; "slot" defaults to true, the optional fields to /// none (a stack group without a positive cap is no group). /// public static IReadOnlyList ParseList(string? json) { var rows = new List(); if (string.IsNullOrWhiteSpace(json)) return rows; try { using JsonDocument doc = JsonDocument.Parse(json); JsonElement root = doc.RootElement; if (root.ValueKind != JsonValueKind.Object || !root.TryGetProperty("salvage", out JsonElement list) || list.ValueKind != JsonValueKind.Array) return rows; foreach (JsonElement e in list.EnumerateArray()) { if (e.ValueKind != JsonValueKind.Object) continue; string bench = EngineServerState.Str(e, "bench").Trim().ToLowerInvariant(); int material = EngineServerState.Int(e, "material"); if (bench.Length == 0 || material <= 0) continue; string kind = EngineServerState.Str(e, "kind").Trim().ToLowerInvariant(); bool slot = !(e.TryGetProperty("slot", out JsonElement s) && s.ValueKind == JsonValueKind.False); string group = EngineServerState.Str(e, "stackGroup").Trim(); int cap = EngineServerState.Int(e, "stackCap"); if (cap <= 0) group = string.Empty; rows.Add(new ServerSalvageView(bench, material, kind.Length > 0 ? kind : "tink", EngineServerState.Str(e, "effect"), slot, EngineServerState.Str(e, "nameMatch"), AppliedInt: EngineServerState.Int(e, "applied"), Prepared: EngineServerState.Bool(e, "prepared"), Order: EngineServerState.Int(e, "order"), AfterCap: EngineServerState.Bool(e, "afterCap"), StackGroup: group, StackCap: group.Length > 0 ? cap : 0)); } } catch (JsonException) { rows.Clear(); } return rows; } }