ServerPack: optional bar menu on the server's window (ServerBarMenu hook)
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
865c3a3997
commit
4afb8f3ac3
3 changed files with 83 additions and 2 deletions
|
|
@ -21,6 +21,9 @@ namespace RynthCore.Plugin.ServerPack;
|
|||
/// <summary>RynthCore's verdict for this plugin's server.</summary>
|
||||
internal sealed record ServerVerdictView(string Name, bool Active, string Force, bool Detected, string Reason);
|
||||
|
||||
/// <summary>One entry of the server window's bar-icon dropdown: a click runs <paramref name="Command"/> as a chat command. <paramref name="Icon"/> is one Phosphor glyph.</summary>
|
||||
internal sealed record ServerBarMenuItem(string Id, string Tooltip, string Icon, string Command);
|
||||
|
||||
internal sealed class ServerPackModel
|
||||
{
|
||||
/// <summary>Payloads longer than this are not ours (a push is a few hundred bytes).</summary>
|
||||
|
|
@ -170,7 +173,7 @@ internal sealed class ServerPackModel
|
|||
/// shared "Server" section of the Mini Remote.
|
||||
/// </summary>
|
||||
public static string BuildSectionsJson(ServerPackInfo info, string pluginName, IReadOnlyList<ServerPage>? pages = null,
|
||||
IReadOnlyList<ServerWindow>? windows = null, bool frameless = false)
|
||||
IReadOnlyList<ServerWindow>? windows = null, bool frameless = false, IReadOnlyList<ServerBarMenuItem>? barMenu = null)
|
||||
{
|
||||
var sb = new StringBuilder(512);
|
||||
sb.Append("{\"version\":1,\"servers\":[{\"id\":").Append(Q(info.Id)).Append(",\"name\":").Append(Q(info.Name));
|
||||
|
|
@ -185,6 +188,20 @@ internal sealed class ServerPackModel
|
|||
sb.Append(",\"icon\":").Append(Q(WindowIcon));
|
||||
sb.Append(",\"width\":").Append(tabs ? ServerPackPages.Width : 460).Append(",\"height\":").Append(ServerPackPages.Height);
|
||||
sb.Append(",\"minWidth\":320,\"minHeight\":200,\"bar\":true,\"server\":").Append(Q(info.Id));
|
||||
// The bar icon's hover dropdown (RynthCore API 94); an older engine ignores the key.
|
||||
if (barMenu != null && barMenu.Count > 0)
|
||||
{
|
||||
sb.Append(",\"barMenu\":[");
|
||||
for (int i = 0; i < barMenu.Count; i++)
|
||||
{
|
||||
ServerBarMenuItem item = barMenu[i];
|
||||
sb.Append(i == 0 ? "" : ",").Append("{\"id\":").Append(Q(item.Id));
|
||||
sb.Append(",\"tooltip\":").Append(Q(item.Tooltip));
|
||||
sb.Append(",\"icon\":").Append(Q(item.Icon));
|
||||
sb.Append(",\"command\":").Append(Q(item.Command)).Append('}');
|
||||
}
|
||||
sb.Append(']');
|
||||
}
|
||||
sb.Append('}');
|
||||
// The server's own windows (ServerWindows, ServerPackWindows.cs, 2026-11-07).
|
||||
if (windows != null) sb.Append(ServerPackWindows.Json(windows, info.Id, frameless));
|
||||
|
|
|
|||
|
|
@ -77,6 +77,8 @@ public sealed partial class ServerPackPlugin : RynthPluginBase
|
|||
private readonly List<ServerPage> _pages;
|
||||
// The server's own windows (ServerWindows), validated the same way.
|
||||
private readonly List<ServerWindow> _windows;
|
||||
// The server window's bar-icon dropdown (ServerBarMenu), for the sections manifest.
|
||||
private readonly List<ServerBarMenuItem> _barMenu = new();
|
||||
private readonly List<string> _pageNotes = new();
|
||||
private readonly ServerPackSettings _settings = new(ServerPackSettings.DefaultPath(RynthPluginInfo.Name));
|
||||
private readonly ServerProfileValues _profileValues = new();
|
||||
|
|
@ -109,6 +111,7 @@ public sealed partial class ServerPackPlugin : RynthPluginBase
|
|||
foreach (string v in verbs) if (!string.IsNullOrWhiteSpace(v)) _serverVerbs.Add(v.Trim());
|
||||
ServerUsageLines(_usageLines.Add);
|
||||
_ui = new ServerPackUi(_model, RynthPluginInfo.Name + " " + Marshal.PtrToStringAnsi(VersionPointer), () => _world, _pages, DrawCardActions);
|
||||
ServerBarMenu(_barMenu);
|
||||
}
|
||||
|
||||
internal ServerPackInfo Info => _model.Info;
|
||||
|
|
@ -158,6 +161,11 @@ public sealed partial class ServerPackPlugin : RynthPluginBase
|
|||
/// <summary>Every frame while logged in (render thread, AC's main thread; the engine's ImGui context current).</summary>
|
||||
partial void ServerRender();
|
||||
/// <summary>
|
||||
/// Items for a hover dropdown on the server window's bar icon (RynthCore API 94; an older engine
|
||||
/// ignores it). Called once, from the constructor (no Host yet).
|
||||
/// </summary>
|
||||
partial void ServerBarMenu(List<ServerBarMenuItem> items);
|
||||
/// <summary>
|
||||
/// Drawn at the top of the server card, under RynthCore's verdict; render thread; queue work with RunOnTick.
|
||||
/// </summary>
|
||||
partial void ServerCardActions();
|
||||
|
|
@ -301,7 +309,7 @@ public sealed partial class ServerPackPlugin : RynthPluginBase
|
|||
/// </summary>
|
||||
internal string? BuildSectionsJson()
|
||||
=> _sectionsJson ??= ServerPackModel.BuildSectionsJson(Info, RynthPluginInfo.Name, _pages, _windows,
|
||||
frameless: _windows.Count > 0 && Host.HasFramelessWindows);
|
||||
frameless: _windows.Count > 0 && Host.HasFramelessWindows, barMenu: _barMenu);
|
||||
|
||||
/// <summary>
|
||||
/// RynthPluginRenderEmbed "window:<id>": one of the server's own windows (ServerWindows), into the
|
||||
|
|
|
|||
|
|
@ -1,6 +1,8 @@
|
|||
using System;
|
||||
using System.Linq;
|
||||
using System.Collections.Generic;
|
||||
using System.Text.Json;
|
||||
using RynthCore.Engine.UI.Sections;
|
||||
using RynthCore.Plugin.ServerPack;
|
||||
using RynthCore.RynthAiTests.Harness;
|
||||
|
||||
|
|
@ -18,6 +20,9 @@ internal static class ServerPackModelTests
|
|||
r.Add("server pack: before any push the world name decides", WorldFallback);
|
||||
r.Add("server pack: override commands are checked before sending", ForceCommands);
|
||||
r.Add("server pack: the sections manifest declares the server and a server-only card", SectionsJson);
|
||||
r.Add("server pack: no bar menu writes no barMenu key", NoBarMenuKey);
|
||||
r.Add("server pack: the bar menu items reach the engine parser as given", BarMenuRoundTrip);
|
||||
r.Add("server pack: a bar menu tooltip with quotes and backslashes round-trips", BarMenuEscapes);
|
||||
r.Add("server pack: logout clears the verdict", ClearOnLogout);
|
||||
r.Add("server pack: the card's botting line and address", InfoText);
|
||||
r.Add("server pack: the RynthServer.Profile JSON carries the server's settings", ProfileJson);
|
||||
|
|
@ -217,6 +222,57 @@ internal static class ServerPackModelTests
|
|||
Check.Eq(odd.RootElement.GetProperty("servers")[0].GetProperty("hosts")[0].GetString(), "a\\b", "escaped");
|
||||
}
|
||||
|
||||
private static ServerBarMenuItem[] MenuItems() => new[]
|
||||
{
|
||||
new ServerBarMenuItem("market", "Market", ((char)0xE470).ToString(), "/rserver market"),
|
||||
new ServerBarMenuItem("suit", "Suit Builder", ((char)0xE40A).ToString(), "/rserver suit"),
|
||||
new ServerBarMenuItem("abilities", "Ability Planner", ((char)0xE6A2).ToString(), "/rserver abilities"),
|
||||
};
|
||||
|
||||
private static void NoBarMenuKey()
|
||||
{
|
||||
string plain = ServerPackModel.BuildSectionsJson(Coldeve(), "RynthColdeve");
|
||||
Check.Eq(ServerPackModel.BuildSectionsJson(Coldeve(), "RynthColdeve", null, barMenu: null), plain, "null pages and null menu are the default");
|
||||
Check.Eq(ServerPackModel.BuildSectionsJson(Coldeve(), "RynthColdeve", null, barMenu: new List<ServerBarMenuItem>()), plain, "an empty menu is the default");
|
||||
Check.False(plain.Contains("barMenu"), "no barMenu key");
|
||||
}
|
||||
|
||||
private static void BarMenuRoundTrip()
|
||||
{
|
||||
ServerBarMenuItem[] items = MenuItems();
|
||||
string json = ServerPackModel.BuildSectionsJson(Coldeve(), "RynthColdeve", null, barMenu: items);
|
||||
SectionManifest m = SectionManifestParser.Parse("RynthColdeve", json, out string error);
|
||||
Check.Eq(error, string.Empty, "parses clean: " + error);
|
||||
SectionBarMenuItem[] menu = m.Windows.Single().Menu;
|
||||
Check.Eq(menu.Length, 3, "three items");
|
||||
for (int i = 0; i < items.Length; i++)
|
||||
{
|
||||
Check.Eq(menu[i].Id, items[i].Id, "id " + i);
|
||||
Check.Eq(menu[i].Tooltip, items[i].Tooltip, "tooltip " + i);
|
||||
Check.Eq(menu[i].Icon, items[i].Icon, "icon " + i);
|
||||
Check.Eq(menu[i].Command, items[i].Command, "command " + i);
|
||||
}
|
||||
|
||||
// With windows of the server's own (ServerWindows), the menu stays on the server's window only.
|
||||
var own = new[] { new ServerWindow("skills", "Skills", "x", 300, 400, 200, 200, false, _ => default) };
|
||||
SectionManifest both = SectionManifestParser.Parse("RynthColdeve",
|
||||
ServerPackModel.BuildSectionsJson(Coldeve(), "RynthColdeve", null, own, barMenu: items), out error);
|
||||
Check.Eq(error, string.Empty, "with a window of its own: parses clean: " + error);
|
||||
Check.Eq(both.Windows.Length, 2, "with a window of its own: two windows");
|
||||
Check.Eq(both.Windows[0].Menu.Length, 3, "with a window of its own: the server window has the menu");
|
||||
Check.Eq(both.Windows[1].Menu.Length, 0, "with a window of its own: that window has none");
|
||||
}
|
||||
|
||||
private static void BarMenuEscapes()
|
||||
{
|
||||
const string tip = "Say \"hi\" \\ there";
|
||||
var items = new[] { new ServerBarMenuItem("odd", tip, ((char)0xE470).ToString(), "/rserver market") };
|
||||
string json = ServerPackModel.BuildSectionsJson(Coldeve(), "RynthColdeve", null, barMenu: items);
|
||||
SectionManifest m = SectionManifestParser.Parse("RynthColdeve", json, out string error);
|
||||
Check.Eq(error, string.Empty, "parses clean: " + error);
|
||||
Check.Eq(m.Windows.Single().Menu.Single().Tooltip, tip, "tooltip survives escaping");
|
||||
}
|
||||
|
||||
private static void ClearOnLogout()
|
||||
{
|
||||
var model = new ServerPackModel(Coldeve());
|
||||
|
|
|
|||
Loading…
Reference in a new issue