diff --git a/Plugins/Shared/ServerPack/ServerPackPlugin.cs b/Plugins/Shared/ServerPack/ServerPackPlugin.cs index f1f30c3..d2f1a96 100644 --- a/Plugins/Shared/ServerPack/ServerPackPlugin.cs +++ b/Plugins/Shared/ServerPack/ServerPackPlugin.cs @@ -33,6 +33,8 @@ // plugins that apply a server rule without knowing the server. // ServerCommandVerbs(list) its own "/rserver " words, handled in ServerChatCommand. // RunOnTick(action) work for the next tick (a page's button, on the render thread). +// ServerCardActions() the server's own buttons at the top of its card, under RynthCore's verdict +// (render thread: ImGui calls only; queue work with RunOnTick). // Send(command) a chat command as if typed (pump thread, logged in only); // LogTagged(text) the engine log, "[RynthConquest] ..."; ChatLine(text) the chat window. // Resources\quests..csv (no code) the server's own quest list: embedded by ServerPlugin.props and @@ -102,7 +104,7 @@ public sealed partial class ServerPackPlugin : RynthPluginBase var verbs = new List(); ServerCommandVerbs(verbs); foreach (string v in verbs) if (!string.IsNullOrWhiteSpace(v)) _serverVerbs.Add(v.Trim()); - _ui = new ServerPackUi(_model, RynthPluginInfo.Name + " " + Marshal.PtrToStringAnsi(VersionPointer), () => _world, _pages); + _ui = new ServerPackUi(_model, RynthPluginInfo.Name + " " + Marshal.PtrToStringAnsi(VersionPointer), () => _world, _pages, DrawCardActions); } internal ServerPackInfo Info => _model.Info; @@ -147,6 +149,10 @@ public sealed partial class ServerPackPlugin : RynthPluginBase /// Every frame while logged in (render thread, AC's main thread; the engine's ImGui context current). partial void ServerRender(); /// + /// Drawn at the top of the server card, under RynthCore's verdict; render thread; queue work with RunOnTick. + /// + partial void ServerCardActions(); + /// /// An incoming chat line (pump thread, logged in only): set to keep it out of /// the chat window (it comes in true when an earlier plugin already ate it). /// @@ -165,6 +171,9 @@ public sealed partial class ServerPackPlugin : RynthPluginBase /// The live numbers of the profile ("values"). Any thread. internal ServerProfileValues ProfileValues => _profileValues; + // A delegate cannot bind an unimplemented partial method (CS0762), so the card gets this wrapper. + private void DrawCardActions() => ServerCardActions(); + /// The pages this plugin draws. internal IReadOnlyList Pages => _pages; diff --git a/Plugins/Shared/ServerPack/ServerPackUi.cs b/Plugins/Shared/ServerPack/ServerPackUi.cs index f1a65da..e5fe52c 100644 --- a/Plugins/Shared/ServerPack/ServerPackUi.cs +++ b/Plugins/Shared/ServerPack/ServerPackUi.cs @@ -26,6 +26,7 @@ internal sealed class ServerPackUi private readonly Func _world; private readonly string _windowTitle; private readonly IReadOnlyList _pages; + private readonly Action? _cardActions; private string _status = string.Empty; /// The standalone window is open ("/rserver"). @@ -34,12 +35,14 @@ internal sealed class ServerPackUi /// "RynthColdeve 0.1.0", for the title and the card. /// The world name the client reports now (null when unknown). /// The server's own pages: tabs of the window after the card. - public ServerPackUi(ServerPackModel model, string pluginLabel, Func world, IReadOnlyList? pages = null) + /// The server's own buttons, drawn under the verdict (render thread); null when it has none. + public ServerPackUi(ServerPackModel model, string pluginLabel, Func world, IReadOnlyList? pages = null, Action? cardActions = null) { _model = model; _pluginLabel = pluginLabel; _world = world; _pages = pages ?? Array.Empty(); + _cardActions = cardActions; _windowTitle = model.Info.Name + " server (" + pluginLabel + ")##server_" + model.Info.Id; } @@ -82,6 +85,7 @@ internal sealed class ServerPackUi { ServerPackInfo info = _model.Info; RenderVerdict(info); + if (_cardActions != null) { ImGui.Separator(); _cardActions(); } ImGui.Separator(); RenderFacts(info); if (info.Commands.Length > 0) diff --git a/Tools/RynthServerPack.Tests/ServerPackHooksTests.cs b/Tools/RynthServerPack.Tests/ServerPackHooksTests.cs index 66de8ea..68ce76b 100644 --- a/Tools/RynthServerPack.Tests/ServerPackHooksTests.cs +++ b/Tools/RynthServerPack.Tests/ServerPackHooksTests.cs @@ -24,6 +24,7 @@ internal static class ServerPackHooksTests r.Add("hooks: settings migration runs once, and a broken file means defaults", SettingsMigration); r.Add("hooks: profile values are published, validated and read back", ProfileValues); r.Add("hooks: the own-reply window opens, stretches and closes", ReplyWindow); + r.Add("hooks: the card draws server actions under the verdict, once", CardActions); } internal static string TempDir() @@ -199,4 +200,40 @@ internal static class ServerPackHooksTests req.Clear(); Check.False(req.Awaiting(1001), "cleared"); } -} + + // Source scan: the card hands the server's own buttons a spot right under RynthCore's verdict. + private static string ServerPackDir() + { + string? dir = AppContext.BaseDirectory; + while (dir != null && !Directory.Exists(Path.Combine(dir, "Plugins", "Shared", "ServerPack"))) + dir = Path.GetDirectoryName(dir); + if (dir == null) throw new DirectoryNotFoundException("Plugins\\Shared\\ServerPack not found above " + AppContext.BaseDirectory); + return Path.Combine(dir, "Plugins", "Shared", "ServerPack"); + } + + private static int CountOf(string text, string needle) + { + int n = 0, at = 0; + while ((at = text.IndexOf(needle, at, StringComparison.Ordinal)) >= 0) { n++; at += needle.Length; } + return n; + } + + private static void CardActions() + { + string ui = File.ReadAllText(Path.Combine(ServerPackDir(), "ServerPackUi.cs")); + string plugin = File.ReadAllText(Path.Combine(ServerPackDir(), "ServerPackPlugin.cs")); + + int body = ui.IndexOf("public void RenderBody()", StringComparison.Ordinal); + Check.True(body >= 0, "RenderBody exists"); + int verdict = ui.IndexOf("RenderVerdict(info);", body, StringComparison.Ordinal); + int facts = ui.IndexOf("RenderFacts(info);", body, StringComparison.Ordinal); + Check.True(verdict > body && facts > verdict, "RenderBody draws the verdict, then the facts"); + Check.Eq(CountOf(ui, "_cardActions()"), 1, "the card actions are invoked exactly once"); + int call = ui.IndexOf("_cardActions()", StringComparison.Ordinal); + Check.True(call > verdict && call < facts, "the actions are drawn after the verdict and before the facts"); + + Check.True(plugin.Contains("DrawCardActions", StringComparison.Ordinal) && plugin.Contains("partial void ServerCardActions();", StringComparison.Ordinal), + "the plugin declares the ServerCardActions hook and DrawCardActions"); + Check.True(plugin.Contains("_pages, DrawCardActions)", StringComparison.Ordinal), "the plugin passes DrawCardActions to ServerPackUi"); + } +} \ No newline at end of file