iw4x-client/iw4/Components/Command.cpp

78 lines
1.4 KiB
C++
Raw Normal View History

2015-12-23 08:45:53 -05:00
#include "..\STDInclude.hpp"
namespace Components
{
std::vector<Game::cmd_function_t*> Command::Functions;
std::map<std::string, Command::Callback> Command::FunctionMap;
2015-12-23 16:21:03 -05:00
char* Command::Params::operator[](size_t index)
2015-12-23 08:45:53 -05:00
{
if (index >= this->Length())
{
return "";
}
return Game::cmd_argv[this->CommandId][index];
}
size_t Command::Params::Length()
{
return Game::cmd_argc[this->CommandId];
}
Command::~Command()
{
for (auto command : Command::Functions)
{
delete command;
}
Command::Functions.clear();
}
void Command::Add(const char* name, Command::Callback callback)
{
Command::FunctionMap[Utils::StrToLower(name)] = callback;
Game::Cmd_AddCommand(name, Command::MainCallback, Command::Allocate(), 0);
}
2015-12-25 15:42:35 -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());
}
}
2015-12-23 08:45:53 -05:00
Game::cmd_function_t* Command::Allocate()
{
Game::cmd_function_t* cmd = new Game::cmd_function_t;
Command::Functions.push_back(cmd);
return cmd;
}
void Command::MainCallback()
{
Command::Params params(*Game::cmd_id);
std::string command = Utils::StrToLower(params[0]);
if (Command::FunctionMap.find(command) != Command::FunctionMap.end())
{
Command::FunctionMap[command](params);
}
}
Command::Command()
{
// TODO: Add commands here?
}
}