using System; using System.Collections.Generic; using System.Globalization; using System.Numerics; using ImGuiNET; using RynthCore.Plugin.RynthAi.CommandButtons; using RynthCore.Plugin.RynthAi.Diagnostics; using RynthCore.PluginSdk; namespace RynthCore.Plugin.RynthAi; // Command Buttons (2026-11-26, a player's ask): a window of buttons that each send one chat line, per // character, per server and for every character (CommandButtons/CommandButtons.cs has the files). The // lists load once the character's settings folder is known, the tick saves edits a second after the // last one, and a press is sent on the tick through RynthCore's chat parser, as a line typed in the // chat box is (the game's commands, /rc, every plugin's commands, emotes, @ server commands, speech). // The window (a plugin window, API v89, no bar button) opens from the dashboard's options menu, // /ra buttons and Find a Feature. public sealed partial class RynthAiPlugin { internal const string CommandButtonsTitle = "Command Buttons", CommandButtonsSurface = "window:commandbuttons"; private const long ButtonsSaveAfterMs = 1000; private readonly CommandButtonSet _buttons = new(); private CommandButtonsWindow? _buttonsWindow; private string _buttonsLoadedFor = string.Empty; // the settings folder the lists are for private int _buttonsFailures; /// The tick (TickStep.CommandButtons): load for this character, save edits. private void TickCommandButtons() { if (!_loginComplete || _dashboard == null) return; string charFolder = _dashboard.CharFolder; if (string.IsNullOrEmpty(charFolder)) return; if (!string.Equals(charFolder, _buttonsLoadedFor, StringComparison.OrdinalIgnoreCase)) { SaveCommandButtons(); var problems = new List(); _buttons.Load(charFolder, problems); _buttonsLoadedFor = charFolder; foreach (string p in problems) ChatLine($"[RynthAi] Command buttons: couldn't read the list for {p}."); } if (_buttons.AnyDirty && Environment.TickCount64 - _buttons.ChangedAtMs >= ButtonsSaveAfterMs) SaveCommandButtons(); } private void SaveCommandButtons() { if (!_buttons.AnyDirty) return; var problems = new List(); _buttons.SaveDirty(problems); foreach (string p in problems) ChatLine($"[RynthAi] Command buttons: couldn't save the list for {p}."); } /// Logout: the lists are saved and forgotten (the next character loads its own). private void OnLogoutCommandButtons() { SaveCommandButtons(); _buttons.Unload(); _buttonsLoadedFor = string.Empty; } /// /// Sends button of 's list. On the tick only (the window /// and /ra buttons press queue it): RynthCore runs the line as typed, so a /ra line comes back to RynthAi, /// which must not be inside the chat parser's plugin step then. False (and why, in the window's note and /// in chat) when it can't be sent. /// private bool PressCommandButton(ButtonScope scope, int index) { string label, line; lock (_buttons.Sync) { List list = _buttons.List(scope); if (index < 0 || index >= list.Count) return false; label = list[index].Shown; line = list[index].Command; } CommandCheck check = CommandLine.Check(line); string? problem = check.Kind == CommandKind.Empty ? "it has no command yet" : check.Problem != null ? check.Problem.TrimEnd('.').ToLowerInvariant() : !Host.HasInvokeChatParser ? "this RynthCore can't send chat lines for a plugin" : null; if (problem != null) { _buttonsWindow?.Note($"{label}: not sent ({problem})."); ReplyFail($"[RynthAi] Command button '{label}' wasn't sent: {problem}."); return false; } // The parser's answer is the game's (speech and emotes can say 0 and still go): the log keeps it, the // line's own effect (a command's reply, the emote, the server's answer) is what the player sees. bool taken = Host.InvokeChatParser(check.Line); Host.Log($"[RynthAi] Command button ({CommandButtonSet.ScopeName(scope)}) '{label}': {check.Line}{(taken ? "" : " (the chat parser answered 0)")}"); _buttonsWindow?.Note("Sent: " + check.Line); return true; } /// The dashboard's ⋯ > Command buttons: the window open or closed. Any thread. internal void ToggleCommandButtonsWindow() { if (Host.HasSetPanelOpen) Host.SetPanelOpen(CommandButtonsTitle, RynthPanelOpen.Toggle); } // ── /ra buttons ────────────────────────────────────────────────────── internal const string ButtonsUsage = "Usage: /ra buttons [server|all] list | add