2016-11-24 02:28:33 -05:00
|
|
|
#include "STDInclude.hpp"
|
|
|
|
|
|
|
|
namespace Components
|
|
|
|
{
|
2017-01-06 09:27:35 -05:00
|
|
|
std::unordered_map<std::string, Utils::Slot<Command::Callback>> Command::FunctionMap;
|
|
|
|
std::unordered_map<std::string, Utils::Slot<Command::Callback>> Command::FunctionMapSV;
|
2016-11-24 02:28:33 -05:00
|
|
|
|
2016-12-07 14:00:24 -05:00
|
|
|
std::string Command::Params::join(size_t startIndex)
|
2016-11-24 02:28:33 -05:00
|
|
|
{
|
|
|
|
std::string result;
|
|
|
|
|
|
|
|
for (size_t i = startIndex; i < this->length(); ++i)
|
|
|
|
{
|
|
|
|
if (i > startIndex) result.append(" ");
|
|
|
|
result.append(this->operator[](i));
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2016-12-07 14:00:24 -05:00
|
|
|
char* Command::Params::operator[](size_t index)
|
|
|
|
{
|
|
|
|
return this->get(index);
|
|
|
|
}
|
|
|
|
|
|
|
|
char* Command::ClientParams::get(size_t index)
|
2016-12-07 12:18:11 -05:00
|
|
|
{
|
2020-12-09 14:13:34 -05:00
|
|
|
if (index >= this->length()) return const_cast<char*>("");
|
2016-12-07 12:18:11 -05:00
|
|
|
return Game::cmd_argv[this->commandId][index];
|
|
|
|
}
|
|
|
|
|
2016-12-07 14:00:24 -05:00
|
|
|
size_t Command::ClientParams::length()
|
2016-12-07 12:18:11 -05:00
|
|
|
{
|
|
|
|
return Game::cmd_argc[this->commandId];
|
|
|
|
}
|
|
|
|
|
2016-12-07 14:00:24 -05:00
|
|
|
char* Command::ServerParams::get(size_t index)
|
2016-12-07 12:18:11 -05:00
|
|
|
{
|
2020-12-09 14:13:34 -05:00
|
|
|
if (index >= this->length()) return const_cast<char*>("");
|
2016-12-07 12:18:11 -05:00
|
|
|
return Game::cmd_argv_sv[this->commandId][index];
|
|
|
|
}
|
|
|
|
|
2016-12-07 14:00:24 -05:00
|
|
|
size_t Command::ServerParams::length()
|
2016-12-07 12:18:11 -05:00
|
|
|
{
|
|
|
|
return Game::cmd_argc_sv[this->commandId];
|
|
|
|
}
|
|
|
|
|
2017-01-06 09:27:35 -05:00
|
|
|
void Command::Add(const char* name, Utils::Slot<Command::Callback> callback)
|
2016-11-24 02:28:33 -05:00
|
|
|
{
|
|
|
|
std::string command = Utils::String::ToLower(name);
|
|
|
|
|
|
|
|
if (Command::FunctionMap.find(command) == Command::FunctionMap.end())
|
|
|
|
{
|
|
|
|
Command::AddRaw(name, Command::MainCallback);
|
|
|
|
}
|
|
|
|
|
|
|
|
Command::FunctionMap[command] = callback;
|
|
|
|
}
|
|
|
|
|
2017-01-06 09:27:35 -05:00
|
|
|
void Command::AddSV(const char* name, Utils::Slot<Command::Callback> callback)
|
2016-11-24 02:28:33 -05:00
|
|
|
{
|
|
|
|
if (Loader::IsPregame())
|
|
|
|
{
|
2017-01-20 16:41:03 -05:00
|
|
|
MessageBoxA(nullptr, "Registering server commands in pregamestate is illegal!", nullptr, MB_ICONERROR);
|
2016-11-24 02:28:33 -05:00
|
|
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
__debugbreak();
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string command = Utils::String::ToLower(name);
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
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());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
|
|
|
Game::cmd_function_t* cmdFunction = *Game::cmd_functions;
|
|
|
|
|
|
|
|
while (cmdFunction)
|
|
|
|
{
|
|
|
|
if (cmdFunction->name && cmdFunction->name == command)
|
|
|
|
{
|
|
|
|
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()
|
|
|
|
{
|
2016-12-07 14:00:24 -05:00
|
|
|
Command::ClientParams params(*Game::cmd_id);
|
2016-11-24 02:28:33 -05:00
|
|
|
|
|
|
|
std::string command = Utils::String::ToLower(params[0]);
|
|
|
|
|
|
|
|
if (Command::FunctionMap.find(command) != Command::FunctionMap.end())
|
|
|
|
{
|
2016-12-07 12:18:11 -05:00
|
|
|
Command::FunctionMap[command](¶ms);
|
2016-11-24 02:28:33 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Command::MainCallbackSV()
|
|
|
|
{
|
2016-12-07 14:00:24 -05:00
|
|
|
Command::ServerParams params(*Game::cmd_id_sv);
|
2016-11-24 02:28:33 -05:00
|
|
|
|
|
|
|
std::string command = Utils::String::ToLower(params[0]);
|
|
|
|
|
|
|
|
if (Command::FunctionMapSV.find(command) != Command::FunctionMapSV.end())
|
|
|
|
{
|
2016-12-07 12:18:11 -05:00
|
|
|
Command::FunctionMapSV[command](¶ms);
|
2016-11-24 02:28:33 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Command::Command()
|
|
|
|
{
|
|
|
|
AssertSize(Game::cmd_function_t, 24);
|
|
|
|
|
2020-12-04 16:17:18 -05:00
|
|
|
static int toastDurationShort = 1000;
|
|
|
|
static int toastDurationMedium = 2500;
|
|
|
|
static int toastDurationLong = 5000;
|
|
|
|
|
2016-11-24 02:28:33 -05:00
|
|
|
// Disable native noclip command
|
|
|
|
Utils::Hook::Nop(0x474846, 5);
|
|
|
|
|
2017-06-14 06:06:04 -04:00
|
|
|
Command::Add("noclip", [](Command::Params*)
|
2016-11-24 02:28:33 -05:00
|
|
|
{
|
|
|
|
int clientNum = Game::CG_GetClientNum();
|
|
|
|
if (!Game::CL_IsCgameInitialized() || clientNum >= 18 || clientNum < 0 || !Game::g_entities[clientNum].client)
|
|
|
|
{
|
|
|
|
Logger::Print("You are not hosting a match!\n");
|
2020-12-04 16:17:18 -05:00
|
|
|
Toast::Show("cardicon_stop", "Error", "You are not hosting a match!", toastDurationMedium);
|
2016-11-24 02:28:33 -05:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!Dvar::Var("sv_cheats").get<bool>())
|
|
|
|
{
|
|
|
|
Logger::Print("Cheats disabled!\n");
|
2020-12-04 16:17:18 -05:00
|
|
|
Toast::Show("cardicon_stop", "Error", "Cheats disabled!", toastDurationMedium);
|
2016-11-24 02:28:33 -05:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
Game::g_entities[clientNum].client->flags ^= Game::PLAYER_FLAG_NOCLIP;
|
|
|
|
|
|
|
|
Logger::Print("Noclip toggled\n");
|
2020-12-04 16:17:18 -05:00
|
|
|
Toast::Show("cardicon_abduction", "Success", "Noclip toggled", toastDurationShort);
|
2016-11-24 02:28:33 -05:00
|
|
|
});
|
|
|
|
|
2017-06-14 06:06:04 -04:00
|
|
|
Command::Add("ufo", [](Command::Params*)
|
2016-11-24 02:28:33 -05:00
|
|
|
{
|
|
|
|
int clientNum = Game::CG_GetClientNum();
|
|
|
|
if (!Game::CL_IsCgameInitialized() || clientNum >= 18 || clientNum < 0 || !Game::g_entities[clientNum].client)
|
|
|
|
{
|
|
|
|
Logger::Print("You are not hosting a match!\n");
|
2020-12-04 16:17:18 -05:00
|
|
|
Toast::Show("cardicon_stop", "Error", "You are not hosting a match!", toastDurationMedium);
|
2016-11-24 02:28:33 -05:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!Dvar::Var("sv_cheats").get<bool>())
|
|
|
|
{
|
|
|
|
Logger::Print("Cheats disabled!\n");
|
2020-12-04 16:17:18 -05:00
|
|
|
Toast::Show("cardicon_stop", "Error", "Cheats disabled!", toastDurationMedium);
|
2016-11-24 02:28:33 -05:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
Game::g_entities[clientNum].client->flags ^= Game::PLAYER_FLAG_UFO;
|
|
|
|
|
|
|
|
Logger::Print("UFO toggled\n");
|
2020-12-04 16:17:18 -05:00
|
|
|
Toast::Show("cardicon_abduction", "Success", "UFO toggled", toastDurationShort);
|
2016-11-24 02:28:33 -05:00
|
|
|
});
|
|
|
|
|
2016-12-07 14:00:24 -05:00
|
|
|
Command::Add("setviewpos", [](Command::Params* params)
|
2016-11-24 02:28:33 -05:00
|
|
|
{
|
|
|
|
int clientNum = Game::CG_GetClientNum();
|
|
|
|
if (!Game::CL_IsCgameInitialized() || clientNum >= 18 || clientNum < 0 || !Game::g_entities[clientNum].client)
|
|
|
|
{
|
|
|
|
Logger::Print("You are not hosting a match!\n");
|
2020-12-04 16:17:18 -05:00
|
|
|
Toast::Show("cardicon_stop", "Error", "You are not hosting a match!", toastDurationMedium);
|
2016-11-24 02:28:33 -05:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!Dvar::Var("sv_cheats").get<bool>())
|
|
|
|
{
|
|
|
|
Logger::Print("Cheats disabled!\n");
|
2020-12-04 16:17:18 -05:00
|
|
|
Toast::Show("cardicon_stop", "Error", "Cheats disabled!", toastDurationMedium);
|
2016-11-24 02:28:33 -05:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-12-07 14:00:24 -05:00
|
|
|
if (params->length() != 4 && params->length() != 6)
|
2016-11-24 02:28:33 -05:00
|
|
|
{
|
|
|
|
Logger::Print("Invalid coordinate specified!\n");
|
2020-12-04 16:17:18 -05:00
|
|
|
Toast::Show("cardicon_stop", "Error", "Invalid coordinate specified!", toastDurationMedium);
|
2016-11-24 02:28:33 -05:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
float pos[3] = { 0.0f, 0.0f, 0.0f };
|
|
|
|
float orientation[3] = { 0.0f, 0.0f, 0.0f };
|
2017-06-14 06:06:04 -04:00
|
|
|
|
2017-01-20 08:36:52 -05:00
|
|
|
pos[0] = strtof(params->get(1), nullptr);
|
|
|
|
pos[1] = strtof(params->get(2), nullptr);
|
|
|
|
pos[2] = strtof(params->get(3), nullptr);
|
2017-06-14 06:06:04 -04:00
|
|
|
|
|
|
|
if (params->length() == 6)
|
2016-11-24 02:29:21 -05:00
|
|
|
{
|
2017-01-20 08:36:52 -05:00
|
|
|
orientation[0] = strtof(params->get(4), nullptr);
|
|
|
|
orientation[1] = strtof(params->get(5), nullptr);
|
2016-11-24 02:29:21 -05:00
|
|
|
}
|
2016-11-24 02:28:33 -05:00
|
|
|
|
|
|
|
Game::TeleportPlayer(&Game::g_entities[clientNum], pos, orientation);
|
|
|
|
|
|
|
|
// Logging that will spam the console and screen if people use cinematics
|
|
|
|
//Logger::Print("Successfully teleported player!\n");
|
2020-12-04 16:17:18 -05:00
|
|
|
//Toast::Show("cardicon_abduction", "Success", "You have been teleported!", toastDurationShort);
|
2016-11-24 02:28:33 -05:00
|
|
|
});
|
2016-12-18 11:42:25 -05:00
|
|
|
|
|
|
|
Command::Add("openLink", [](Command::Params* params)
|
|
|
|
{
|
|
|
|
if (params->length() > 1)
|
|
|
|
{
|
2017-06-11 15:25:00 -04:00
|
|
|
Utils::OpenUrl(params->get(1));
|
2016-12-18 11:42:25 -05:00
|
|
|
}
|
|
|
|
});
|
2016-11-24 02:28:33 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
Command::~Command()
|
|
|
|
{
|
|
|
|
Command::FunctionMap.clear();
|
|
|
|
Command::FunctionMapSV.clear();
|
|
|
|
}
|
|
|
|
}
|