[Command] Optimize setviewpos command

This commit is contained in:
momo5502 2016-11-24 08:28:33 +01:00
parent e8a5e164fc
commit 04c696a9d5

View File

@ -1,251 +1,250 @@
#include "STDInclude.hpp" #include "STDInclude.hpp"
namespace Components namespace Components
{ {
Utils::Memory::Allocator Command::MemAllocator; Utils::Memory::Allocator Command::MemAllocator;
std::map<std::string, wink::slot<Command::Callback>> Command::FunctionMap; std::map<std::string, wink::slot<Command::Callback>> Command::FunctionMap;
std::map<std::string, wink::slot<Command::Callback>> Command::FunctionMapSV; std::map<std::string, wink::slot<Command::Callback>> Command::FunctionMapSV;
char* Command::Params::operator[](size_t index) char* Command::Params::operator[](size_t index)
{ {
if (index >= this->length()) return ""; if (index >= this->length()) return "";
if (this->isSV) return Game::cmd_argv_sv[this->commandId][index]; if (this->isSV) return Game::cmd_argv_sv[this->commandId][index];
else return Game::cmd_argv[this->commandId][index]; else return Game::cmd_argv[this->commandId][index];
} }
size_t Command::Params::length() size_t Command::Params::length()
{ {
if (this->isSV) return Game::cmd_argc_sv[this->commandId]; if (this->isSV) return Game::cmd_argc_sv[this->commandId];
else return Game::cmd_argc[this->commandId]; else return Game::cmd_argc[this->commandId];
} }
std::string Command::Params::join(size_t startIndex) std::string Command::Params::join(size_t startIndex)
{ {
std::string result; std::string result;
for (size_t i = startIndex; i < this->length(); ++i) for (size_t i = startIndex; i < this->length(); ++i)
{ {
if (i > startIndex) result.append(" "); if (i > startIndex) result.append(" ");
result.append(this->operator[](i)); result.append(this->operator[](i));
} }
return result; return result;
} }
void Command::Add(const char* name, Command::Callback* callback) void Command::Add(const char* name, Command::Callback* callback)
{ {
std::string command = Utils::String::ToLower(name); std::string command = Utils::String::ToLower(name);
if (Command::FunctionMap.find(command) == Command::FunctionMap.end()) if (Command::FunctionMap.find(command) == Command::FunctionMap.end())
{ {
Command::AddRaw(name, Command::MainCallback); Command::AddRaw(name, Command::MainCallback);
} }
Command::FunctionMap[command] = callback; Command::FunctionMap[command] = callback;
} }
void Command::AddSV(const char* name, Command::Callback* callback) void Command::AddSV(const char* name, Command::Callback* callback)
{ {
if (Loader::IsPregame()) if (Loader::IsPregame())
{ {
MessageBoxA(0, "Registering server commands in pregamestate is illegal!", 0, MB_ICONERROR); MessageBoxA(0, "Registering server commands in pregamestate is illegal!", 0, MB_ICONERROR);
#ifdef DEBUG #ifdef DEBUG
__debugbreak(); __debugbreak();
#endif #endif
return; return;
} }
std::string command = Utils::String::ToLower(name); std::string command = Utils::String::ToLower(name);
if (Command::FunctionMapSV.find(command) == Command::FunctionMapSV.end()) if (Command::FunctionMapSV.find(command) == Command::FunctionMapSV.end())
{ {
Command::AddRawSV(name, Command::MainCallbackSV); Command::AddRawSV(name, Command::MainCallbackSV);
// If the main command is registered as Cbuf_AddServerText, the command will be redirected to the SV handler // 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::AddRaw(name, Game::Cbuf_AddServerText);
} }
Command::FunctionMapSV[command] = callback; Command::FunctionMapSV[command] = callback;
} }
void Command::AddRaw(const char* name, void(*callback)(), bool key) void Command::AddRaw(const char* name, void(*callback)(), bool key)
{ {
Game::Cmd_AddCommand(name, callback, Command::Allocate(), key); Game::Cmd_AddCommand(name, callback, Command::Allocate(), key);
} }
void Command::AddRawSV(const char* name, void(*callback)()) void Command::AddRawSV(const char* name, void(*callback)())
{ {
Game::Cmd_AddServerCommand(name, callback, Command::Allocate()); 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 // 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::AddRaw(name, Game::Cbuf_AddServerText);
} }
void Command::Execute(std::string command, bool sync) void Command::Execute(std::string command, bool sync)
{ {
command.append("\n"); // Make sure it's terminated command.append("\n"); // Make sure it's terminated
if (sync) if (sync)
{ {
Game::Cmd_ExecuteSingleCommand(0, 0, command.data()); Game::Cmd_ExecuteSingleCommand(0, 0, command.data());
} }
else else
{ {
Game::Cbuf_AddText(0, command.data()); Game::Cbuf_AddText(0, command.data());
} }
} }
Game::cmd_function_t* Command::Find(std::string command) Game::cmd_function_t* Command::Find(std::string command)
{ {
Game::cmd_function_t* cmdFunction = *Game::cmd_functions; Game::cmd_function_t* cmdFunction = *Game::cmd_functions;
while (cmdFunction) while (cmdFunction)
{ {
if (cmdFunction->name && cmdFunction->name == command) if (cmdFunction->name && cmdFunction->name == command)
{ {
return cmdFunction; return cmdFunction;
} }
cmdFunction = cmdFunction->next; cmdFunction = cmdFunction->next;
} }
return nullptr; return nullptr;
} }
Game::cmd_function_t* Command::Allocate() Game::cmd_function_t* Command::Allocate()
{ {
return Command::MemAllocator.allocate<Game::cmd_function_t>(); return Command::MemAllocator.allocate<Game::cmd_function_t>();
} }
void Command::MainCallback() void Command::MainCallback()
{ {
Command::Params params(false, *Game::cmd_id); Command::Params params(false, *Game::cmd_id);
std::string command = Utils::String::ToLower(params[0]); std::string command = Utils::String::ToLower(params[0]);
if (Command::FunctionMap.find(command) != Command::FunctionMap.end()) if (Command::FunctionMap.find(command) != Command::FunctionMap.end())
{ {
Command::FunctionMap[command](params); Command::FunctionMap[command](params);
} }
} }
void Command::MainCallbackSV() void Command::MainCallbackSV()
{ {
Command::Params params(true, *Game::cmd_id_sv); Command::Params params(true, *Game::cmd_id_sv);
std::string command = Utils::String::ToLower(params[0]); std::string command = Utils::String::ToLower(params[0]);
if (Command::FunctionMapSV.find(command) != Command::FunctionMapSV.end()) if (Command::FunctionMapSV.find(command) != Command::FunctionMapSV.end())
{ {
Command::FunctionMapSV[command](params); Command::FunctionMapSV[command](params);
} }
} }
Command::Command() Command::Command()
{ {
AssertSize(Game::cmd_function_t, 24); AssertSize(Game::cmd_function_t, 24);
// Disable native noclip command // Disable native noclip command
Utils::Hook::Nop(0x474846, 5); Utils::Hook::Nop(0x474846, 5);
Command::Add("noclip", [] (Command::Params) Command::Add("noclip", [] (Command::Params)
{ {
int clientNum = Game::CG_GetClientNum(); int clientNum = Game::CG_GetClientNum();
if (!Game::CL_IsCgameInitialized() || clientNum >= 18 || clientNum < 0 || !Game::g_entities[clientNum].client) if (!Game::CL_IsCgameInitialized() || clientNum >= 18 || clientNum < 0 || !Game::g_entities[clientNum].client)
{ {
Logger::Print("You are not hosting a match!\n"); Logger::Print("You are not hosting a match!\n");
Toast::Show("cardicon_stop", "Error", "You are not hosting a match!", 3000); Toast::Show("cardicon_stop", "Error", "You are not hosting a match!", 3000);
return; return;
} }
if (!Dvar::Var("sv_cheats").get<bool>()) if (!Dvar::Var("sv_cheats").get<bool>())
{ {
Logger::Print("Cheats disabled!\n"); Logger::Print("Cheats disabled!\n");
Toast::Show("cardicon_stop", "Error", "Cheats disabled!", 3000); Toast::Show("cardicon_stop", "Error", "Cheats disabled!", 3000);
return; return;
} }
Game::g_entities[clientNum].client->flags ^= Game::PLAYER_FLAG_NOCLIP; Game::g_entities[clientNum].client->flags ^= Game::PLAYER_FLAG_NOCLIP;
Logger::Print("Noclip toggled\n"); Logger::Print("Noclip toggled\n");
Toast::Show("cardicon_abduction", "Success", "Noclip toggled", 3000); Toast::Show("cardicon_abduction", "Success", "Noclip toggled", 3000);
}); });
Command::Add("ufo", [] (Command::Params) Command::Add("ufo", [] (Command::Params)
{ {
int clientNum = Game::CG_GetClientNum(); int clientNum = Game::CG_GetClientNum();
if (!Game::CL_IsCgameInitialized() || clientNum >= 18 || clientNum < 0 || !Game::g_entities[clientNum].client) if (!Game::CL_IsCgameInitialized() || clientNum >= 18 || clientNum < 0 || !Game::g_entities[clientNum].client)
{ {
Logger::Print("You are not hosting a match!\n"); Logger::Print("You are not hosting a match!\n");
Toast::Show("cardicon_stop", "Error", "You are not hosting a match!", 3000); Toast::Show("cardicon_stop", "Error", "You are not hosting a match!", 3000);
return; return;
} }
if (!Dvar::Var("sv_cheats").get<bool>()) if (!Dvar::Var("sv_cheats").get<bool>())
{ {
Logger::Print("Cheats disabled!\n"); Logger::Print("Cheats disabled!\n");
Toast::Show("cardicon_stop", "Error", "Cheats disabled!", 3000); Toast::Show("cardicon_stop", "Error", "Cheats disabled!", 3000);
return; return;
} }
Game::g_entities[clientNum].client->flags ^= Game::PLAYER_FLAG_UFO; Game::g_entities[clientNum].client->flags ^= Game::PLAYER_FLAG_UFO;
Logger::Print("UFO toggled\n"); Logger::Print("UFO toggled\n");
Toast::Show("cardicon_abduction", "Success", "UFO toggled", 3000); Toast::Show("cardicon_abduction", "Success", "UFO toggled", 3000);
}); });
Command::Add("setviewpos", [](Command::Params params) Command::Add("setviewpos", [](Command::Params params)
{ {
int clientNum = Game::CG_GetClientNum(); int clientNum = Game::CG_GetClientNum();
if (!Game::CL_IsCgameInitialized() || clientNum >= 18 || clientNum < 0 || !Game::g_entities[clientNum].client) if (!Game::CL_IsCgameInitialized() || clientNum >= 18 || clientNum < 0 || !Game::g_entities[clientNum].client)
{ {
Logger::Print("You are not hosting a match!\n"); Logger::Print("You are not hosting a match!\n");
Toast::Show("cardicon_stop", "Error", "You are not hosting a match!", 3000); Toast::Show("cardicon_stop", "Error", "You are not hosting a match!", 3000);
return; return;
} }
if (!Dvar::Var("sv_cheats").get<bool>()) if (!Dvar::Var("sv_cheats").get<bool>())
{ {
Logger::Print("Cheats disabled!\n"); Logger::Print("Cheats disabled!\n");
Toast::Show("cardicon_stop", "Error", "Cheats disabled!", 3000); Toast::Show("cardicon_stop", "Error", "Cheats disabled!", 3000);
return; return;
} }
if (params.length() < 4 || params.length() > 6) if (params.length() != 4 && params.length() != 6)
{ {
Logger::Print("Invalid coordinate specified!\n"); Logger::Print("Invalid coordinate specified!\n");
Toast::Show("cardicon_stop", "Error", "Invalid coordinate specified!", 3000); Toast::Show("cardicon_stop", "Error", "Invalid coordinate specified!", 3000);
return; return;
} }
float pos[3]; float pos[3] = { 0.0f, 0.0f, 0.0f };
float orientation[3]; float orientation[3] = { 0.0f, 0.0f, 0.0f };
for (unsigned int i = 0; i < (params.length() - 1); i++) pos[0] = strtof(params[1], NULL);
{ pos[1] = strtof(params[2], NULL);
if (i < 3) pos[2] = strtof(params[3], NULL);
{
pos[i] = strtof(params[i + 1], NULL); if(params.length() == 6)
} {
else orientation[0] = strtof(params[4], NULL);
{ orientation[1] = strtof(params[5], NULL);
orientation[i - 3] = strtof(params[i + 1], NULL); }
}
} Game::TeleportPlayer(&Game::g_entities[clientNum], pos, orientation);
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");
Logger::Print("Successfully teleported player!\n"); //Toast::Show("cardicon_abduction", "Success", "You have been teleported!", 3000);
Toast::Show("cardicon_abduction", "Success", "You have been teleported!", 3000); });
}); }
}
Command::~Command()
Command::~Command() {
{ Command::MemAllocator.clear();
Command::MemAllocator.clear(); Command::FunctionMap.clear();
Command::FunctionMap.clear(); Command::FunctionMapSV.clear();
Command::FunctionMapSV.clear(); }
} }
}