// MiniRemoteContext.cs — Shared services for the Mini Remote's universal tabs (Quests, Pets) and // the VTank .usd importer. The universal half of the ILT Hub's IltHubContext (SilentKelpie's ILT // Hub; its Infinite Leaftide half went to the RynthILT plugin 2026-10-13, whose feature bits and // sections come through ServerHub). // // Threading contract (as before): // * Tick / OnChat run on the plugin pump thread and may call the host freely. // * Render runs on the ImGui render thread and must NOT call AC actions directly. It calls // Post(...) to run work on the next pump tick, or Confirm(...) to ask the player first (the // confirmed action is then posted). using System; using System.Collections.Concurrent; using System.Collections.Generic; using RynthCore.Plugin.RynthAi.LegacyUi; using RynthCore.Plugin.Shared; using RynthCore.PluginSdk; namespace RynthCore.Plugin.RynthAi.MiniRemote; /// A yes/no question waiting for the player (a modal over the Mini Remote). internal sealed class MiniRemoteConfirm { public string Title = "Confirm"; public string Message = string.Empty; public string YesLabel = "Yes"; /// Runs on the pump thread after the player clicks Yes. public Action? OnYes; } internal sealed class MiniRemoteContext { private readonly ConcurrentQueue _posted = new(); private readonly ConcurrentQueue _confirms = new(); private readonly Func _settings; public MiniRemoteContext(RynthCoreHost host, MiniRemoteState state, MiniRemoteStore store, MiniRemoteInventory inventory, ServerHubClient serverHub, Func settings) { Host = host; State = state; Store = store; Inventory = inventory; ServerHub = serverHub; _settings = settings; } public RynthCoreHost Host { get; } public MiniRemoteState State { get; } public MiniRemoteStore Store { get; } public MiniRemoteInventory Inventory { get; } /// RynthILT's view (ILT world, feature bits, ILT sections); "not ILT" without RynthILT. public ServerHubClient ServerHub { get; } /// The live combat settings (rule lists the Pets tab / USD importer edit). Null before login. public LegacyUiSettings? Settings => _settings(); /// Set by the controller when combat settings were edited and must be saved. public Action? SaveCombatSettings; /// Logged-in character name (resolved at login). public string CharName = string.Empty; /// The connected world's name ("" before login or on an engine without it); keys the Crafting tab's custom rows. public string WorldName => Host.HasGetWorldName && Host.TryGetWorldName(out string world) ? (world ?? string.Empty).Trim() : string.Empty; // ── Active server (RynthCore's verdict and its server plugin's profile) ───────────────────── /// What the render thread reads about the active server: replaced whole, never edited. private sealed record ServerView(string Name, IReadOnlyList Salvage, ServerTrinketRule Trinkets) { public static readonly ServerView None = new(string.Empty, Array.Empty(), ServerTrinketRule.None); } private readonly ServerStateClient _server = new(); private int _serverRevision = -1; private volatile ServerView _serverView = ServerView.None; /// The active server's display name from RynthCore ("" when none is active or the engine is pre-v86). public string ServerName => _serverView.Name; /// The active server plugin's own tinkering salvage (empty when it defines none). Any thread. public IReadOnlyList ServerSalvage => _serverView.Salvage; /// Which jewelry the active server plugin calls a trinket (none without one). Any thread. public ServerTrinketRule ServerTrinkets => _serverView.Trinkets; /// Pump thread: polls RynthCore's server state (cheap until due) and rebuilds the view when it changed. public void PollServer(long now) { _server.Poll(Host, now); if (_server.Revision == _serverRevision) return; _serverRevision = _server.Revision; EngineServerState? st = _server.State; string name = st != null && st.HasActive ? st.ActiveName : string.Empty; _serverView = new ServerView(name, ServerSalvageView.ParseList(_server.ProfileJson), ServerTrinketRule.Parse(_server.ProfileJson)); } /// Logout: forget the server; the next login polls fresh. public void ClearServer() { _server.Clear(); _serverRevision = -1; _serverView = ServerView.None; } /// /// Shows the Mini Remote on one of its tabs (tab id; set by the controller). Any thread: it /// only records the tab and the show request, which the next frame applies. /// public Action? ShowTab; /// Writes a local chat line (never sent to the server). public void Chat(string text) { if (Host.HasWriteToChat) Host.WriteToChat(text, 1); } /// Queues work for the next pump tick (safe from the render thread). public void Post(Action action) => _posted.Enqueue(action); /// Asks the player to confirm before runs on the pump thread. public void Confirm(string title, string message, Action onYes, string yesLabel = "Yes") => _confirms.Enqueue(new MiniRemoteConfirm { Title = title, Message = message, OnYes = onYes, YesLabel = yesLabel }); /// Render thread: takes the next pending confirmation, if any. public bool TryTakeConfirm(out MiniRemoteConfirm request) => _confirms.TryDequeue(out request!); /// Pump thread: runs everything posted since the last tick. public void DrainPosted() { int guard = 0; while (guard++ < 64 && _posted.TryDequeue(out var a)) { try { a(); } catch (Exception ex) { RynthLog.Exception(LogCat.UI, ex, "[MiniRemote] posted action"); } } } /// Elapsed-ms clock used for every timer. public static long NowMs => Environment.TickCount64; } /// Text helpers the universal tabs share (the ILT Hub's IltParse.Duration). internal static class MiniRemoteText { /// "ready", "45s", "3m 12s", "2h 5m 0s", "1d 4h 30m". public static string Duration(TimeSpan span) { if (span <= TimeSpan.Zero) return "ready"; if (span.TotalDays >= 1) return $"{(int)span.TotalDays}d {span.Hours}h {span.Minutes}m"; if (span.TotalHours >= 1) return $"{span.Hours}h {span.Minutes}m {span.Seconds}s"; if (span.TotalMinutes >= 1) return $"{span.Minutes}m {span.Seconds}s"; return $"{span.Seconds}s"; } } /// The Mini Remote's own tab ids (RynthILT supplies the Infinite Leaftide ones). internal static class MiniRemoteTabs { /// The Remote tab (the remote itself). public const string Remote = ""; public const string Pets = "pets"; public const string Crafting = "crafting"; /// /// The universal tabs in tab-bar order (after Remote, before RynthILT's tabs). The Quests tab /// moved to RynthOracle on 2026-10-13 (MiniRemoteController.QuestsMovedText). /// public static readonly string[] Universal = { Pets, Crafting }; public static string Label(string id) => id switch { Pets => "Pets", Crafting => "Crafting", _ => "Remote", }; /// "pets" / "crafting" (and their short forms) to the tab id; null for anything else. public static string? Parse(string? text) => (text ?? string.Empty).Trim().ToLowerInvariant() switch { "pets" or "pet" => Pets, "crafting" or "craft" or "tinker" or "tinkering" => Crafting, _ => null, }; /// Size of a universal tab's body (the old section window's default size). public static System.Numerics.Vector2 Size(string id) => id switch { Pets => new System.Numerics.Vector2(560, 580), Crafting => new System.Numerics.Vector2(680, 600), _ => new System.Numerics.Vector2(520, 440), }; }