// IltHubContext.cs — Shared services handed to every ILT Hub feature.
//
// Threading contract:
// * Feature Tick / OnChat run on the plugin pump thread and may call the host freely.
// * Feature 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). This mirrors the existing
// DrainMetaCommands pattern in RynthAiPlugin.
using System;
using System.Collections.Concurrent;
using RynthCore.Plugin.RynthAi.LegacyUi;
using RynthCore.PluginSdk;
namespace RynthCore.Plugin.RynthAi.IltHub;
/// A yes/no question waiting for the player in the Hub window.
internal sealed class IltConfirmRequest
{
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 IltHubContext
{
private readonly ConcurrentQueue _posted = new();
private readonly ConcurrentQueue _confirms = new();
private readonly Func _settings;
public IltHubContext(RynthCoreHost host, IltHubState state, IltHubStore store, IltChatCapture capture,
IltServerOptions options, IltInventory inventory, Func settings)
{
Host = host;
State = state;
Store = store;
Capture = capture;
Options = options;
Inventory = inventory;
_settings = settings;
}
public RynthCoreHost Host { get; }
public IltHubState State { get; }
public IltHubStore Store { get; }
public IltChatCapture Capture { get; }
public IltServerOptions Options { get; }
public IltInventory Inventory { get; }
/// The live combat settings (rule lists the Pet picker / 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;
/// True when the Hub window is open (features use it to gate auto-refreshes).
public bool WindowOpen => State.WindowVisible;
/// 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 IltConfirmRequest { Title = title, Message = message, OnYes = onYes, YesLabel = yesLabel });
/// Render thread: takes the next pending confirmation, if any.
public bool TryTakeConfirm(out IltConfirmRequest 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.IltHub, ex, "posted action"); }
}
}
/// Elapsed-ms clock used for every Hub timer.
public static long NowMs => Environment.TickCount64;
}
/// Contract every Hub feature module implements.
internal interface IIltFeature
{
/// Pump-thread tick (~every plugin tick).
void Tick(long nowMs);
/// Pump-thread chat line. Return true to hide the line from the chat window.
bool OnChat(string text);
/// Logout / teardown: stop automation, drop transient state.
void OnLogout();
}