RynthLua: run script commands as /lua <name> [args]
In a Decal (bridge-mode) client the engine only forwards a fixed list of
chat-bar commands to plugins (/rc /ra /rv /rn /rnav /lua), so a script's
game.RegisterCommand("/demo") never ran there. When the /lua subcommand is
not a built-in verb, try it as a script command via the existing queue, so
/lua demo status runs the /demo handler once with args "status". Usage text
is shown only when no script owns the name, and lists the registered
commands. Built-in verbs always win. Docs: RegisterCommand in
rynthlua.d.lua.
- "/lua /demo status" works too: one leading slash on the subcommand is
stripped, and the usage lists names without the slash, the form the
fallback takes ("/lua demo").
- A script command named like a built-in verb (list, exec, start, ...) can
never be reached through /lua, so the usage summary leaves it out. One
BuiltInLuaVerbs set gates the fallback and filters the summary.
Tests (CompatTests luacmd.lua): plain and slash forms, case, spacing and
quotes, unknown command usage, summary form and filtering, a script /exec
not run by /lua exec, stopped owner gives usage, none-registered branch.
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
parent
fe1eca0ca7
commit
536bbf5aa5
3 changed files with 172 additions and 0 deletions
|
|
@ -255,11 +255,30 @@ public sealed partial class RynthLuaPlugin
|
|||
|
||||
// ── /lua commands ───────────────────────────────────────────────────────
|
||||
|
||||
/// <summary>
|
||||
/// Every built-in /lua verb. The one list behind both the dispatch gate in HandleLuaChatCommand
|
||||
/// (a verb never falls back to a script command) and the usage summary (a script command with
|
||||
/// one of these names is unreachable through /lua). A new verb goes in the switch AND here.
|
||||
/// </summary>
|
||||
private static readonly HashSet<string> BuiltInLuaVerbs = new(StringComparer.OrdinalIgnoreCase)
|
||||
{
|
||||
"run", "start", "restart", "stop", "exec", "execin", "autostart", "list", "hud",
|
||||
};
|
||||
|
||||
private void HandleLuaChatCommand(string line)
|
||||
{
|
||||
string[] parts = line.Split(' ', 3, StringSplitOptions.RemoveEmptyEntries);
|
||||
string sub = parts.Length > 1 ? parts[1].ToLowerInvariant() : "";
|
||||
if (sub.StartsWith('/')) sub = sub[1..]; // "/lua /demo": the usage lists names with a slash, one is accepted
|
||||
string rest = parts.Length > 2 ? parts[2].Trim() : "";
|
||||
// Not a built-in verb (those always win): in a Decal (bridge-mode) client the engine only
|
||||
// forwards /lua, so "/lua demo status" (or "/lua /demo status", as the usage text lists them) is
|
||||
// the way to reach a script's game.RegisterCommand("/demo"). Same queue as a typed "/demo status":
|
||||
// the handler runs once on the tick thread with args "status".
|
||||
if (!BuiltInLuaVerbs.Contains(sub))
|
||||
{
|
||||
if (sub.Length > 0 && TryQueueScriptCommand("/" + sub + (rest.Length > 0 ? " " + rest : ""))) return;
|
||||
}
|
||||
switch (sub)
|
||||
{
|
||||
case "run":
|
||||
|
|
@ -322,7 +341,21 @@ public sealed partial class RynthLuaPlugin
|
|||
break;
|
||||
default:
|
||||
ChatLine("/lua list | start <script> | stop <script>|all | restart <script> | exec <code> | execin <script> <code> | autostart <script> none|global|character | hud list|show|hide|toggle <script>/<hud>|bench <n>|bench off");
|
||||
ChatLine("/lua <command> [args] runs a script command (game.RegisterCommand) - " + ScriptCommandsSummary());
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>The registered script commands for the /lua usage text.</summary>
|
||||
private string ScriptCommandsSummary()
|
||||
{
|
||||
// Names as /lua takes them (no slash); a name equal to a built-in verb can't be reached here.
|
||||
var names = _commandOwners.Keys
|
||||
.Select(k => k.TrimStart('/'))
|
||||
.Where(n => n.Length > 0 && !BuiltInLuaVerbs.Contains(n))
|
||||
.Distinct(StringComparer.OrdinalIgnoreCase)
|
||||
.OrderBy(n => n, StringComparer.OrdinalIgnoreCase)
|
||||
.ToList();
|
||||
return names.Count == 0 ? "none registered now." : "registered: " + string.Join(", ", names) + ".";
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -442,6 +442,10 @@ function Capabilities.Get() end
|
|||
|
||||
---A chat command owned by this script. A typed line starting with it is eaten and
|
||||
---`fn(args)` runs on the next tick with the rest of the line.
|
||||
---In a Decal (bridge-mode) client the engine only passes a few commands to plugins
|
||||
---(/rc /ra /rv /rn /rnav /lua), so a bare "/name" never reaches this script there:
|
||||
---players type `/lua name ...` instead (same handler, same args). Built-in /lua verbs
|
||||
---(list, start, stop, ...) win over a script command of the same name.
|
||||
---@param command string "/name" (the slash is added when missing)
|
||||
---@param fn fun(args: string)
|
||||
function Game.RegisterCommand(command, fn) end
|
||||
|
|
|
|||
135
Tools/RynthLua.CompatTests/Scripts/luacmd.lua
Normal file
135
Tools/RynthLua.CompatTests/Scripts/luacmd.lua
Normal file
|
|
@ -0,0 +1,135 @@
|
|||
-- "/lua <name> [args]" runs a script's game.RegisterCommand("/name") handler. In a Decal
|
||||
-- (bridge-mode) client the engine only forwards /lua to the plugin, so this is the only way
|
||||
-- to reach a script command there. Built-in /lua verbs always win over a script command.
|
||||
local calls = {}
|
||||
game.RegisterCommand("/demo", function(args) calls[#calls + 1] = args end)
|
||||
game.RegisterCommand("/list", function(args) calls[#calls + 1] = "SHADOW:" .. args end)
|
||||
|
||||
local function settle(ms)
|
||||
local t = 0
|
||||
while t < ms do sleep(20); t = t + 20 end
|
||||
end
|
||||
|
||||
local function chatHas(needle)
|
||||
for _, l in ipairs(harness.chatLog()) do
|
||||
if string.find(l, needle, 1, true) then return true end
|
||||
end
|
||||
return false
|
||||
end
|
||||
|
||||
-- 1. a script command through /lua: eaten, runs once, with the rest of the line
|
||||
harness.clearCalls()
|
||||
check(harness.chatInput("/lua demo status"), "/lua demo status is eaten")
|
||||
settle(300)
|
||||
eq(#calls, 1, "/lua demo status ran the /demo handler exactly once")
|
||||
eq(calls[1], "status", "the handler got args 'status'")
|
||||
check(not chatHas("/lua list | start"), "no usage text when a script owns the command")
|
||||
|
||||
-- no args and mixed case (owners are case-insensitive, args keep their case)
|
||||
harness.chatInput("/lua DEMO")
|
||||
settle(300)
|
||||
eq(#calls, 2, "/lua DEMO ran the handler")
|
||||
eq(calls[2], "", "no args gives an empty string")
|
||||
harness.chatInput("/lua demo Two Words")
|
||||
settle(300)
|
||||
eq(calls[3], "Two Words", "args pass through unchanged")
|
||||
|
||||
-- 2. an unknown name prints usage and runs nothing
|
||||
harness.clearCalls()
|
||||
local before = #calls
|
||||
harness.chatInput("/lua nosuchcmd x")
|
||||
settle(300)
|
||||
eq(#calls, before, "an unknown command ran no handler")
|
||||
check(chatHas("/lua list | start"), "an unknown command prints usage")
|
||||
check(chatHas("registered: demo."), "usage lists the registered script commands")
|
||||
harness.clearCalls()
|
||||
harness.chatInput("/lua")
|
||||
settle(300)
|
||||
check(chatHas("/lua list | start"), "bare /lua prints usage")
|
||||
|
||||
-- 3. a built-in verb is not shadowed by a script command of the same name
|
||||
harness.clearCalls()
|
||||
before = #calls
|
||||
harness.chatInput("/lua list")
|
||||
settle(300)
|
||||
eq(#calls, before, "/lua list did not run the script's /list handler")
|
||||
check(chatHas("Scripts ("), "/lua list ran the built-in list")
|
||||
|
||||
-- typed natively, the script's own /list is still its own
|
||||
check(harness.chatInput("/list direct"), "a typed /list still reaches the script")
|
||||
settle(300)
|
||||
eq(calls[#calls], "SHADOW:direct", "typed /list ran the script handler")
|
||||
|
||||
-- 4. the slash form the usage text shows is accepted too
|
||||
harness.clearCalls()
|
||||
before = #calls
|
||||
harness.chatInput("/lua /demo hi")
|
||||
settle(300)
|
||||
eq(#calls, before + 1, "/lua /demo hi ran the handler once")
|
||||
eq(calls[#calls], "hi", "/lua /demo hi: args 'hi'")
|
||||
|
||||
-- 5. odd spacing and quoted args pass through; the line is trimmed
|
||||
before = #calls
|
||||
harness.chatInput(" /lua demo hi there ")
|
||||
settle(300)
|
||||
eq(#calls, before + 1, "extra spaces: ran once")
|
||||
eq(calls[#calls], "hi there", "extra spaces: args trimmed at the ends only")
|
||||
harness.chatInput("/lua demo say \"hello world\" 'x'")
|
||||
settle(300)
|
||||
eq(calls[#calls], "say \"hello world\" 'x'", "quotes are not interpreted")
|
||||
|
||||
-- 6. the usage lists names in the form the fallback accepts, minus the unreachable ones
|
||||
harness.clearCalls()
|
||||
harness.chatInput("/lua nosuchcmd")
|
||||
settle(300)
|
||||
local summary
|
||||
for _, l in ipairs(harness.chatLog()) do
|
||||
summary = summary or string.match(l, "registered: ([^%.]*)%.")
|
||||
end
|
||||
check(summary ~= nil, "usage has a registered: list")
|
||||
check(string.find(summary or "", "demo", 1, true) ~= nil, "the list names demo")
|
||||
check(string.find(summary or "", "/", 1, true) == nil, "the list has no slash: " .. tostring(summary))
|
||||
check(string.find(summary or "", "list", 1, true) == nil, "a script /list is left out (it is a built-in verb)")
|
||||
-- and every name it shows really works
|
||||
harness.clearCalls()
|
||||
before = #calls
|
||||
harness.chatInput("/lua " .. summary)
|
||||
settle(300)
|
||||
eq(#calls, before + 1, "the listed form runs the handler")
|
||||
|
||||
-- 7. a script command named like a built-in verb is not reachable through /lua
|
||||
game.RegisterCommand("/exec", function(args) calls[#calls + 1] = "SHADOW-EXEC:" .. args end)
|
||||
harness.clearCalls()
|
||||
before = #calls
|
||||
harness.chatInput("/lua exec return 1")
|
||||
harness.chatInput("/lua /exec return 1")
|
||||
settle(300)
|
||||
eq(#calls, before, "/lua exec did not run the script's /exec handler")
|
||||
game.UnregisterCommand("/exec")
|
||||
|
||||
-- 8. a stopped owner no longer answers: usage instead
|
||||
harness.chatInput("/lua exec game.RegisterCommand('/ghost', function(a) end)")
|
||||
settle(300)
|
||||
harness.clearCalls()
|
||||
harness.chatInput("/lua ghost")
|
||||
settle(300)
|
||||
check(not chatHas("/lua list | start"), "a live owner's /ghost is found (no usage)")
|
||||
harness.chatInput("/lua stop exec")
|
||||
settle(300)
|
||||
harness.clearCalls()
|
||||
harness.chatInput("/lua ghost")
|
||||
settle(300)
|
||||
check(chatHas("/lua list | start"), "after its owner stopped, /lua ghost prints usage")
|
||||
check(not chatHas("ghost."), "a stopped owner's command is not listed")
|
||||
|
||||
-- 9. nothing registered: the usage says so
|
||||
game.UnregisterCommand("/demo")
|
||||
game.UnregisterCommand("/list")
|
||||
harness.clearCalls()
|
||||
before = #calls
|
||||
harness.chatInput("/lua demo status")
|
||||
settle(300)
|
||||
check(chatHas("none registered now."), "no commands: usage says none registered")
|
||||
eq(#calls, before, "no handler ran")
|
||||
|
||||
done()
|
||||
Loading…
Reference in a new issue