From 854301da6fcb8c5bec5633a3f924b5ccd1e742c5 Mon Sep 17 00:00:00 2001 From: Mario Date: Wed, 31 Jul 2024 20:31:04 +1000 Subject: [PATCH] Migrate client commands to a registry --- qcsrc/server/command/cmd.qc | 76 ++++++++++++++++--------------------- qcsrc/server/command/reg.qh | 14 +++++++ 2 files changed, 47 insertions(+), 43 deletions(-) diff --git a/qcsrc/server/command/cmd.qc b/qcsrc/server/command/cmd.qc index 1e14def1d..9ed9da34f 100644 --- a/qcsrc/server/command/cmd.qc +++ b/qcsrc/server/command/cmd.qc @@ -920,68 +920,58 @@ void ClientCommand_(entity caller, int request) // ===================================== // Do not hard code aliases for these, instead create them in commands.cfg... also: keep in alphabetical order, please ;) -#define CLIENT_COMMANDS(ent, request, arguments, command) \ - CLIENT_COMMAND("autoswitch", ClientCommand_autoswitch(ent, request, arguments), "Whether or not to switch automatically when getting a better weapon") \ - CLIENT_COMMAND("clear_ignores", ClientCommand_clear_ignores(ent, request), "Remove all existing ignored clients") \ - CLIENT_COMMAND("clientversion", ClientCommand_clientversion(ent, request, arguments), "Release version of the game") \ - CLIENT_COMMAND("ignore", ClientCommand_ignore(ent, request, arguments, command), "Ignore a client in the game keeping out of personal chat log for a match") \ - CLIENT_COMMAND("join", ClientCommand_join(ent, request), "Become a player in the game") \ - CLIENT_COMMAND("kill", ClientCommand_kill(ent, request), "Become a member of the dead") \ - CLIENT_COMMAND("minigame", ClientCommand_minigame(ent, request, arguments, command), "Start a minigame") \ - CLIENT_COMMAND("mv_getpicture", ClientCommand_mv_getpicture(ent, request, arguments), "Retrieve mapshot picture from the server") \ - CLIENT_COMMAND("physics", ClientCommand_physics(ent, request, arguments), "Change physics set") \ - CLIENT_COMMAND("ready", ClientCommand_ready(ent, request), "Qualify as ready to end warmup stage (or restart server if allowed)") \ - CLIENT_COMMAND("say", ClientCommand_say(ent, request, arguments, command), "Print a message to chat to all players") \ - CLIENT_COMMAND("say_team", ClientCommand_say_team(ent, request, arguments, command), "Print a message to chat to all team mates") \ - CLIENT_COMMAND("selectteam", ClientCommand_selectteam(ent, request, arguments), "Attempt to choose a team to join into") \ - CLIENT_COMMAND("selfstuff", ClientCommand_selfstuff(ent, request, command), "Stuffcmd a command to your own client") \ - CLIENT_COMMAND("sentcvar", ClientCommand_sentcvar(ent, request, arguments), "New system for sending a client cvar to the server") \ - CLIENT_COMMAND("spectate", ClientCommand_spectate(ent, request), "Become an observer") \ - CLIENT_COMMAND("suggestmap", ClientCommand_suggestmap(ent, request, arguments), "Suggest a map to the mapvote at match end") \ - CLIENT_COMMAND("tell", ClientCommand_tell(ent, request, arguments, command), "Send a message directly to a player") \ - CLIENT_COMMAND("voice", ClientCommand_voice(ent, request, arguments, command), "Send voice message via sound") \ - CLIENT_COMMAND("unignore", ClientCommand_unignore(ent, request, arguments, command), "Remove an existing ignored player") \ - CLIENT_COMMAND("wpeditor", ClientCommand_wpeditor(ent, request, arguments), "Waypoint editor commands") \ - /* nothing */ +CLIENT_COMMAND(autoswitch, "Whether or not to switch automatically when getting a better weapon") { ClientCommand_autoswitch(caller, request, arguments); } +CLIENT_COMMAND(clear_ignores, "Remove all existing ignored clients") { ClientCommand_clear_ignores(caller, request); } +CLIENT_COMMAND(clientversion, "Release version of the game") { ClientCommand_clientversion(caller, request, arguments); } +CLIENT_COMMAND(ignore, "Ignore a client in the game keeping out of personal chat log for a match") { ClientCommand_ignore(caller, request, arguments, command); } +CLIENT_COMMAND(join, "Become a player in the game") { ClientCommand_join(caller, request); } +CLIENT_COMMAND(kill, "Become a member of the dead") { ClientCommand_kill(caller, request); } +CLIENT_COMMAND(minigame, "Start a minigame") { ClientCommand_minigame(caller, request, arguments, command); } +CLIENT_COMMAND(mv_getpicture, "Retrieve mapshot picture from the server") { ClientCommand_mv_getpicture(caller, request, arguments); } +CLIENT_COMMAND(physics, "Change physics set") { ClientCommand_physics(caller, request, arguments); } +CLIENT_COMMAND(ready, "Qualify as ready to end warmup stage (or restart server if allowed)") { ClientCommand_ready(caller, request); } +CLIENT_COMMAND(say, "Print a message to chat to all players") { ClientCommand_say(caller, request, arguments, command); } +CLIENT_COMMAND(say_team, "Print a message to chat to all team mates") { ClientCommand_say_team(caller, request, arguments, command); } +CLIENT_COMMAND(selectteam, "Attempt to choose a team to join into") { ClientCommand_selectteam(caller, request, arguments); } +CLIENT_COMMAND(selfstuff, "Stuffcmd a command to your own client") { ClientCommand_selfstuff(caller, request, command); } +CLIENT_COMMAND(sentcvar, "New system for sending a client cvar to the server") { ClientCommand_sentcvar(caller, request, arguments); } +CLIENT_COMMAND(spectate, "Become an observer") { ClientCommand_spectate(caller, request); } +CLIENT_COMMAND(suggestmap, "Suggest a map to the mapvote at match end") { ClientCommand_suggestmap(caller, request, arguments); } +CLIENT_COMMAND(tell, "Send a message directly to a player") { ClientCommand_tell(caller, request, arguments, command); } +CLIENT_COMMAND(voice, "Send voice message via sound") { ClientCommand_voice(caller, request, arguments, command); } +CLIENT_COMMAND(unignore, "Remove an existing ignored player") { ClientCommand_unignore(caller, request, arguments, command); } +CLIENT_COMMAND(wpeditor, "Waypoint editor commands") { ClientCommand_wpeditor(caller, request, arguments); } void ClientCommand_macro_help(entity caller) { - #define CLIENT_COMMAND(name, function, description) \ - { sprint(caller, " ^2", name, "^7: ", description, "\n"); } - - CLIENT_COMMANDS(NULL, 0, 0, ""); -#undef CLIENT_COMMAND + FOREACH(CLIENT_COMMANDS, true, { print_to(caller, sprintf(" ^2%s^7: %s", it.m_name, it.m_description)); }); } float ClientCommand_macro_command(int argc, entity caller, string command) { - #define CLIENT_COMMAND(name, function, description) \ - { if (name == strtolower(argv(0))) { function; return true; } } - - CLIENT_COMMANDS(caller, CMD_REQUEST_COMMAND, argc, command); -#undef CLIENT_COMMAND + string c = strtolower(argv(0)); + FOREACH(CLIENT_COMMANDS, it.m_name == c, { + it.m_invokecmd(it, CMD_REQUEST_COMMAND, caller, argc, command); + return true; + }); return false; } float ClientCommand_macro_usage(int argc, entity caller) { - #define CLIENT_COMMAND(name, function, description) \ - { if (name == strtolower(argv(1))) { function; return true; } } - - CLIENT_COMMANDS(caller, CMD_REQUEST_USAGE, argc, ""); -#undef CLIENT_COMMAND + string c = strtolower(argv(1)); + FOREACH(CLIENT_COMMANDS, it.m_name == c, { + it.m_invokecmd(it, CMD_REQUEST_USAGE, caller, argc, ""); + return true; + }); return false; } void ClientCommand_macro_write_aliases(float fh) { - #define CLIENT_COMMAND(name, function, description) \ - { CMD_Write_Alias("qc_cmd_cmd", name, description); } - - CLIENT_COMMANDS(NULL, 0, 0, ""); -#undef CLIENT_COMMAND + FOREACH(CLIENT_COMMANDS, true, { CMD_Write_Alias("qc_cmd_cmd", it.m_name, it.m_description); }); } // ====================================== diff --git a/qcsrc/server/command/reg.qh b/qcsrc/server/command/reg.qh index 71ace677a..121c1dc0b 100644 --- a/qcsrc/server/command/reg.qh +++ b/qcsrc/server/command/reg.qh @@ -17,3 +17,17 @@ REGISTRY_DEFINE_GET(SERVER_COMMANDS, NULL) STATIC_INIT(SERVER_COMMANDS_aliases) { FOREACH(SERVER_COMMANDS, true, { localcmd(sprintf("alias %1$s \"%2$s %1$s ${* ?}\"\n", it.m_name, "qc_cmd_sv")); }); } + +REGISTRY(CLIENT_COMMANDS, 64) +REGISTER_REGISTRY(CLIENT_COMMANDS) +REGISTRY_SORT(CLIENT_COMMANDS) + +REGISTRY_DEFINE_GET(CLIENT_COMMANDS, NULL) + +#define CLIENT_COMMAND(id, description) \ + CLASS(clientcommand_##id, Command) \ + ATTRIB(clientcommand_##id, m_name, string, #id); \ + ATTRIB(clientcommand_##id, m_description, string, description); \ + ENDCLASS(clientcommand_##id) \ + REGISTER(CLIENT_COMMANDS, CMD_SVCL, id, m_id, NEW(clientcommand_##id)); \ + METHOD(clientcommand_##id, m_invokecmd, void(clientcommand_##id this, int request, entity caller, int arguments, string command)) -- 2.39.2