// 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 Ilt).
//
// 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 RynthCore.Plugin.RynthAi.LegacyUi;
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,
IltClient ilt, Func settings)
{
Host = host;
State = state;
Store = store;
Inventory = inventory;
Ilt = ilt;
_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 IltClient Ilt { 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;
///
/// 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 Quests = "quests";
public const string Pets = "pets";
/// The universal tabs in tab-bar order (after Remote, before RynthILT's tabs).
public static readonly string[] Universal = { Quests, Pets };
public static string Label(string id) => id switch
{
Quests => "Quests",
Pets => "Pets",
_ => "Remote",
};
/// "quests" / "quest" / "pets" / "pet" → the tab id; null for anything else.
public static string? Parse(string? text) => (text ?? string.Empty).Trim().ToLowerInvariant() switch
{
"quests" or "quest" => Quests,
"pets" or "pet" => Pets,
_ => 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
{
Quests => new System.Numerics.Vector2(700, 500),
Pets => new System.Numerics.Vector2(560, 580),
_ => new System.Numerics.Vector2(520, 440),
};
}