2022-02-27 07:53:44 -05:00
|
|
|
#include <STDInclude.hpp>
|
2016-11-24 02:28:33 -05:00
|
|
|
|
|
|
|
namespace Components
|
|
|
|
{
|
2022-04-12 08:34:51 -04:00
|
|
|
std::unordered_map<std::string, std::function<void(Command::Params*)>> Command::FunctionMap;
|
|
|
|
std::unordered_map<std::string, std::function<void(Command::Params*)>> Command::FunctionMapSV;
|
2016-11-24 02:28:33 -05:00
|
|
|
|
2022-08-21 12:52:54 -04:00
|
|
|
std::string Command::Params::join(const int index) const
|
2016-11-24 02:28:33 -05:00
|
|
|
{
|
|
|
|
std::string result;
|
|
|
|
|
2022-03-17 14:50:20 -04:00
|
|
|
for (auto i = index; i < this->size(); i++)
|
2016-11-24 02:28:33 -05:00
|
|
|
{
|
2022-03-17 14:50:20 -04:00
|
|
|
if (i > index) result.append(" ");
|
|
|
|
result.append(this->get(i));
|
2016-11-24 02:28:33 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2022-03-17 14:50:20 -04:00
|
|
|
Command::ClientParams::ClientParams()
|
|
|
|
: nesting_(Game::cmd_args->nesting)
|
2016-12-07 14:00:24 -05:00
|
|
|
{
|
2022-03-17 14:50:20 -04:00
|
|
|
assert(Game::cmd_args->nesting < Game::CMD_MAX_NESTING);
|
2016-12-07 14:00:24 -05:00
|
|
|
}
|
|
|
|
|
2022-08-21 12:52:54 -04:00
|
|
|
int Command::ClientParams::size() const
|
2016-12-07 12:18:11 -05:00
|
|
|
{
|
2022-03-17 14:50:20 -04:00
|
|
|
return Game::cmd_args->argc[this->nesting_];
|
2016-12-07 12:18:11 -05:00
|
|
|
}
|
|
|
|
|
2022-08-21 12:52:54 -04:00
|
|
|
const char* Command::ClientParams::get(const int index) const
|
2016-12-07 12:18:11 -05:00
|
|
|
{
|
2022-03-17 14:50:20 -04:00
|
|
|
if (index >= this->size())
|
|
|
|
{
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
|
|
|
return Game::cmd_args->argv[this->nesting_][index];
|
2016-12-07 12:18:11 -05:00
|
|
|
}
|
|
|
|
|
2022-03-17 14:50:20 -04:00
|
|
|
Command::ServerParams::ServerParams()
|
|
|
|
: nesting_(Game::sv_cmd_args->nesting)
|
2016-12-07 12:18:11 -05:00
|
|
|
{
|
2022-03-17 14:50:20 -04:00
|
|
|
assert(Game::sv_cmd_args->nesting < Game::CMD_MAX_NESTING);
|
2016-12-07 12:18:11 -05:00
|
|
|
}
|
|
|
|
|
2022-08-21 12:52:54 -04:00
|
|
|
int Command::ServerParams::size() const
|
2016-12-07 12:18:11 -05:00
|
|
|
{
|
2022-03-17 14:50:20 -04:00
|
|
|
return Game::sv_cmd_args->argc[this->nesting_];
|
|
|
|
}
|
|
|
|
|
2022-08-21 12:52:54 -04:00
|
|
|
const char* Command::ServerParams::get(const int index) const
|
2022-03-17 14:50:20 -04:00
|
|
|
{
|
|
|
|
if (index >= this->size())
|
|
|
|
{
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
|
|
|
return Game::sv_cmd_args->argv[this->nesting_][index];
|
2016-12-07 12:18:11 -05:00
|
|
|
}
|
|
|
|
|
2022-06-12 18:02:20 -04:00
|
|
|
void Command::Add(const char* name, const std::function<void()>& callback)
|
|
|
|
{
|
|
|
|
Add(name, [callback]([[maybe_unused]] const Command::Params* params)
|
|
|
|
{
|
|
|
|
callback();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
void Command::Add(const char* name, const std::function<void(Command::Params*)>& callback)
|
2016-11-24 02:28:33 -05:00
|
|
|
{
|
2022-03-17 14:50:20 -04:00
|
|
|
const auto command = Utils::String::ToLower(name);
|
2016-11-24 02:28:33 -05:00
|
|
|
|
2022-05-31 12:38:09 -04:00
|
|
|
if (!Command::FunctionMap.contains(command))
|
2016-11-24 02:28:33 -05:00
|
|
|
{
|
|
|
|
Command::AddRaw(name, Command::MainCallback);
|
|
|
|
}
|
|
|
|
|
2022-04-12 08:34:51 -04:00
|
|
|
Command::FunctionMap.insert_or_assign(command, std::move(callback));
|
2016-11-24 02:28:33 -05:00
|
|
|
}
|
|
|
|
|
2022-06-12 18:02:20 -04:00
|
|
|
void Command::AddSV(const char* name, const std::function<void(Command::Params*)>& callback)
|
2016-11-24 02:28:33 -05:00
|
|
|
{
|
|
|
|
if (Loader::IsPregame())
|
|
|
|
{
|
2022-06-16 10:15:26 -04:00
|
|
|
MessageBoxA(nullptr, "Registering server commands in pregame state is illegal!", nullptr, MB_ICONERROR);
|
2016-11-24 02:28:33 -05:00
|
|
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
__debugbreak();
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-03-17 14:50:20 -04:00
|
|
|
const auto command = Utils::String::ToLower(name);
|
2016-11-24 02:28:33 -05:00
|
|
|
|
2022-05-31 12:38:09 -04:00
|
|
|
if (!Command::FunctionMapSV.contains(command))
|
2016-11-24 02:28:33 -05:00
|
|
|
{
|
|
|
|
Command::AddRawSV(name, Command::MainCallbackSV);
|
|
|
|
}
|
|
|
|
|
2022-06-16 10:15:26 -04:00
|
|
|
FunctionMapSV.insert_or_assign(command, callback);
|
2016-11-24 02:28:33 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void Command::AddRaw(const char* name, void(*callback)(), bool key)
|
|
|
|
{
|
|
|
|
Game::Cmd_AddCommand(name, callback, Command::Allocate(), key);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Command::AddRawSV(const char* name, void(*callback)())
|
|
|
|
{
|
|
|
|
Game::Cmd_AddServerCommand(name, callback, Command::Allocate());
|
|
|
|
|
|
|
|
// If the main command is registered as Cbuf_AddServerText, the command will be redirected to the SV handler
|
2022-08-27 17:19:09 -04:00
|
|
|
Command::AddRaw(name, Game::Cbuf_AddServerText_f, false);
|
2016-11-24 02:28:33 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void Command::Execute(std::string command, bool sync)
|
|
|
|
{
|
|
|
|
command.append("\n"); // Make sure it's terminated
|
|
|
|
|
|
|
|
if (sync)
|
|
|
|
{
|
|
|
|
Game::Cmd_ExecuteSingleCommand(0, 0, command.data());
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Game::Cbuf_AddText(0, command.data());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-17 08:29:18 -05:00
|
|
|
Game::cmd_function_t* Command::Find(const std::string& command)
|
2016-11-24 02:28:33 -05:00
|
|
|
{
|
2022-04-12 08:34:51 -04:00
|
|
|
auto* cmdFunction = *Game::cmd_functions;
|
2016-11-24 02:28:33 -05:00
|
|
|
|
2022-04-12 08:34:51 -04:00
|
|
|
while (cmdFunction != nullptr)
|
2016-11-24 02:28:33 -05:00
|
|
|
{
|
2022-04-12 08:34:51 -04:00
|
|
|
if (cmdFunction->name != nullptr && cmdFunction->name == command)
|
2016-11-24 02:28:33 -05:00
|
|
|
{
|
|
|
|
return cmdFunction;
|
|
|
|
}
|
|
|
|
|
|
|
|
cmdFunction = cmdFunction->next;
|
|
|
|
}
|
|
|
|
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
Game::cmd_function_t* Command::Allocate()
|
|
|
|
{
|
2017-06-02 09:36:20 -04:00
|
|
|
return Utils::Memory::GetAllocator()->allocate<Game::cmd_function_t>();
|
2016-11-24 02:28:33 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void Command::MainCallback()
|
|
|
|
{
|
2022-04-12 08:34:51 -04:00
|
|
|
ClientParams params;
|
2022-03-17 14:50:20 -04:00
|
|
|
const auto command = Utils::String::ToLower(params[0]);
|
2016-11-24 02:28:33 -05:00
|
|
|
|
2022-04-12 08:34:51 -04:00
|
|
|
if (const auto got = FunctionMap.find(command); got != FunctionMap.end())
|
2016-11-24 02:28:33 -05:00
|
|
|
{
|
2022-03-17 14:50:20 -04:00
|
|
|
got->second(¶ms);
|
2016-11-24 02:28:33 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Command::MainCallbackSV()
|
|
|
|
{
|
2022-04-12 08:34:51 -04:00
|
|
|
ServerParams params;
|
2022-03-17 14:50:20 -04:00
|
|
|
const auto command = Utils::String::ToLower(params[0]);
|
2016-11-24 02:28:33 -05:00
|
|
|
|
2022-04-12 08:34:51 -04:00
|
|
|
if (const auto got = FunctionMapSV.find(command); got != FunctionMapSV.end())
|
2016-11-24 02:28:33 -05:00
|
|
|
{
|
2022-03-17 14:50:20 -04:00
|
|
|
got->second(¶ms);
|
2016-11-24 02:28:33 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|