iw4x-client/src/Components/Modules/Command.cpp

207 lines
5.3 KiB
C++
Raw Normal View History

2016-07-11 11:14:58 -04:00
#include "STDInclude.hpp"
namespace Components
{
2016-08-13 10:39:05 -04:00
Utils::Memory::Allocator Command::MemAllocator;
2016-07-11 11:14:58 -04:00
std::map<std::string, wink::slot<Command::Callback>> Command::FunctionMap;
std::map<std::string, wink::slot<Command::Callback>> Command::FunctionMapSV;
char* Command::Params::operator[](size_t index)
{
if (index >= this->length()) return "";
if (this->isSV) return Game::cmd_argv_sv[this->commandId][index];
else return Game::cmd_argv[this->commandId][index];
2016-07-11 11:14:58 -04:00
}
size_t Command::Params::length()
2016-07-11 11:14:58 -04:00
{
if (this->isSV) return Game::cmd_argc_sv[this->commandId];
else return Game::cmd_argc[this->commandId];
2016-07-11 11:14:58 -04:00
}
std::string Command::Params::join(size_t startIndex)
2016-07-11 11:14:58 -04:00
{
std::string result;
for (size_t i = startIndex; i < this->length(); ++i)
2016-07-11 11:14:58 -04:00
{
if (i > startIndex) result.append(" ");
result.append(this->operator[](i));
}
return result;
}
void Command::Add(const char* name, Command::Callback* callback)
{
std::string command = Utils::String::ToLower(name);
2016-07-11 11:14:58 -04:00
if (Command::FunctionMap.find(command) == Command::FunctionMap.end())
{
Command::AddRaw(name, Command::MainCallback);
}
Command::FunctionMap[command] = callback;
}
void Command::AddSV(const char* name, Command::Callback* callback)
{
2016-09-03 09:52:40 -04:00
if (Loader::IsPregame())
{
MessageBoxA(0, "Registering server commands in pregamestate is illegal!", 0, MB_ICONERROR);
#ifdef DEBUG
__debugbreak();
#endif
return;
}
std::string command = Utils::String::ToLower(name);
2016-07-11 11:14:58 -04:00
if (Command::FunctionMapSV.find(command) == Command::FunctionMapSV.end())
{
Command::AddRawSV(name, Command::MainCallbackSV);
// If the main command is registered as Cbuf_AddServerText, the command will be redirected to the SV handler
Command::AddRaw(name, Game::Cbuf_AddServerText);
}
Command::FunctionMapSV[command] = callback;
}
2016-09-05 13:54:16 -04:00
void Command::AddRaw(const char* name, void(*callback)(), bool key)
2016-07-11 11:14:58 -04:00
{
2016-09-05 13:54:16 -04:00
Game::Cmd_AddCommand(name, callback, Command::Allocate(), key);
2016-07-11 11:14:58 -04:00
}
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
Command::AddRaw(name, Game::Cbuf_AddServerText);
}
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());
}
}
2016-08-07 12:25:44 -04:00
Game::cmd_function_t* Command::Find(std::string command)
{
Game::cmd_function_t* cmdFunction = *Game::cmd_functions;
while (cmdFunction)
{
if (cmdFunction->name && cmdFunction->name == command)
{
return cmdFunction;
}
cmdFunction = cmdFunction->next;
}
return nullptr;
}
2016-07-11 11:14:58 -04:00
Game::cmd_function_t* Command::Allocate()
{
return Command::MemAllocator.allocate<Game::cmd_function_t>();
2016-07-11 11:14:58 -04:00
}
void Command::MainCallback()
{
Command::Params params(false, *Game::cmd_id);
std::string command = Utils::String::ToLower(params[0]);
2016-07-11 11:14:58 -04:00
if (Command::FunctionMap.find(command) != Command::FunctionMap.end())
{
Command::FunctionMap[command](params);
}
}
void Command::MainCallbackSV()
{
Command::Params params(true, *Game::cmd_id_sv);
std::string command = Utils::String::ToLower(params[0]);
2016-07-11 11:14:58 -04:00
if (Command::FunctionMapSV.find(command) != Command::FunctionMapSV.end())
{
Command::FunctionMapSV[command](params);
}
}
Command::Command()
{
AssertSize(Game::cmd_function_t, 24);
2016-09-05 13:54:16 -04:00
2016-08-17 19:57:56 -04:00
// Disable native noclip command
Utils::Hook::Nop(0x474846, 5);
2016-08-17 20:18:45 -04:00
Command::Add("noclip", [] (Command::Params)
2016-08-17 19:57:56 -04:00
{
int clientNum = Game::CG_GetClientNum();
2016-08-18 06:36:18 -04:00
if (!Game::CL_IsCgameInitialized() || clientNum >= 18 || clientNum < 0 || !Game::g_entities[clientNum].client)
2016-08-17 19:57:56 -04:00
{
2016-08-18 06:36:18 -04:00
Logger::Print("You are not hosting a match!\n");
Toast::Show("cardicon_stop", "Error", "You are not hosting a match!", 3000);
2016-08-17 19:57:56 -04:00
return;
}
if (!Dvar::Var("sv_cheats").get<bool>())
2016-08-17 19:57:56 -04:00
{
2016-08-18 06:36:18 -04:00
Logger::Print("Cheats disabled!\n");
Toast::Show("cardicon_stop", "Error", "Cheats disabled!", 3000);
2016-08-17 19:57:56 -04:00
return;
}
2016-08-18 06:36:18 -04:00
Game::g_entities[clientNum].client->flags ^= Game::PLAYER_FLAG_NOCLIP;
2016-08-17 19:57:56 -04:00
Logger::Print("Noclip toggled\n");
2016-08-18 06:36:18 -04:00
Toast::Show("cardicon_abduction", "Success", "Noclip toggled", 3000);
2016-08-17 19:57:56 -04:00
});
2016-08-17 20:18:45 -04:00
Command::Add("ufo", [] (Command::Params)
2016-08-17 19:57:56 -04:00
{
int clientNum = Game::CG_GetClientNum();
2016-08-18 06:36:18 -04:00
if (!Game::CL_IsCgameInitialized() || clientNum >= 18 || clientNum < 0 || !Game::g_entities[clientNum].client)
2016-08-17 19:57:56 -04:00
{
2016-08-18 06:36:18 -04:00
Logger::Print("You are not hosting a match!\n");
Toast::Show("cardicon_stop", "Error", "You are not hosting a match!", 3000);
2016-08-17 19:57:56 -04:00
return;
}
if (!Dvar::Var("sv_cheats").get<bool>())
2016-08-17 19:57:56 -04:00
{
2016-08-18 06:36:18 -04:00
Logger::Print("Cheats disabled!\n");
Toast::Show("cardicon_stop", "Error", "Cheats disabled!", 3000);
2016-08-17 19:57:56 -04:00
return;
}
2016-08-18 06:36:18 -04:00
Game::g_entities[clientNum].client->flags ^= Game::PLAYER_FLAG_UFO;
2016-08-17 19:57:56 -04:00
Logger::Print("UFO toggled\n");
2016-08-18 06:36:18 -04:00
Toast::Show("cardicon_abduction", "Success", "UFO toggled", 3000);
2016-08-17 19:57:56 -04:00
});
2016-07-11 11:14:58 -04:00
}
Command::~Command()
{
Command::MemAllocator.clear();
2016-07-11 11:14:58 -04:00
Command::FunctionMap.clear();
Command::FunctionMapSV.clear();
}
}