Refactor command (changes from s1x)

This commit is contained in:
FutureRave 2022-03-17 18:50:20 +00:00
parent 204a752a96
commit a41d7356cc
No known key found for this signature in database
GPG Key ID: E883E2BC9657D955
28 changed files with 147 additions and 147 deletions

View File

@ -83,7 +83,7 @@ namespace Components
Command::ServerParams params;
if (params.length() < 3)
if (params.size() < 3)
{
Game::SV_Cmd_EndTokenizedString();
Logger::SoftError("Connecting failed: Command parsing error!");
@ -170,7 +170,7 @@ namespace Components
Command::ServerParams params;
// Ensure there are enough params
if (params.length() < 3)
if (params.size() < 3)
{
Network::Send(address, "error\nInvalid connect string!");
return;
@ -455,7 +455,7 @@ namespace Components
{
Command::Add("securityLevel", [](Command::Params* params)
{
if (params->length() < 2)
if (params->size() < 2)
{
uint32_t level = Auth::GetZeroBits(Auth::GuidToken, Auth::GuidKey.getPublicKey());
Logger::Print("Your current security level is %d\n", level);

View File

@ -234,17 +234,17 @@ namespace Components
{
Command::Add("banclient", [](Command::Params* params)
{
if (params->length() < 2) return;
if (params->size() < 2) return;
std::string reason = "EXE_ERR_BANNED_PERM";
if (params->length() >= 3) reason = params->join(2);
if (params->size() >= 3) reason = params->join(2);
Bans::BanClientNum(atoi(params->get(1)), reason);
});
Command::Add("unbanclient", [](Command::Params* params)
{
if (params->length() < 2) return;
if (params->size() < 2) return;
std::string type = params->get(1);

View File

@ -447,7 +447,7 @@ namespace Components
{
unsigned int count = 1;
if (params->length() > 1)
if (params->size() > 1)
{
if (params->get(1) == "all"s) count = static_cast<unsigned int>(-1);
else count = atoi(params->get(1));

View File

@ -199,7 +199,7 @@ namespace Components
{
if (params->get(1) == "customTitles"s && !Dedicated::IsEnabled())
{
if (params->length() == 3)
if (params->size() == 3)
{
CardTitles::ParseCustomTitles(params->get(2));
return true;

View File

@ -263,7 +263,7 @@ namespace Components
}
const auto* cmd = params->get(0);
if (params->length() < 2)
if (params->size() < 2)
{
Logger::Print("Usage: %s <client number> : prevent the player from using the chat\n", cmd);
return;
@ -285,7 +285,7 @@ namespace Components
}
const auto* cmd = params->get(0);
if (params->length() < 2)
if (params->size() < 2)
{
Logger::Print("Usage: %s <client number or guid>\n%s all = unmute everyone\n", cmd, cmd);
return;
@ -299,7 +299,7 @@ namespace Components
return;
}
if (params->get(1) == "all"s)
if (std::strcmp(params->get(1), "all") == 0)
{
Logger::Print("All players were unmuted\n");
Chat::UnmuteInternal(0, true);

View File

@ -83,7 +83,7 @@ namespace Components
{
if (params->get(1) == "clantags"s && !Dedicated::IsEnabled())
{
if (params->length() == 3)
if (params->size() == 3)
{
ClanTags::ParseClantags(params->get(2));
return true;

View File

@ -147,7 +147,7 @@ namespace Components
Command::ServerParams params = {};
Game::vec3_t origin, angles{0.f, 0.f, 0.f};
if (params.length() < 4u || params.length() > 6u)
if (params.size() < 4 || params.size() > 6)
{
Game::SV_GameSendServerCommand(ent->s.number, 0,
Utils::String::VA("%c \"GAME_USAGE\x15: setviewpos x y z [yaw] [pitch]\n\"", 0x65));
@ -159,12 +159,12 @@ namespace Components
origin[i] = std::strtof(params.get(i + 1), nullptr);
}
if (params.length() >= 5u)
if (params.size() >= 5)
{
angles[1] = std::strtof(params.get(4), nullptr); // Yaw
}
if (params.length() == 6u)
if (params.size() == 6)
{
angles[0] = std::strtof(params.get(5), nullptr); // Pitch
}

View File

@ -5,56 +5,72 @@ namespace Components
std::unordered_map<std::string, Utils::Slot<Command::Callback>> Command::FunctionMap;
std::unordered_map<std::string, Utils::Slot<Command::Callback>> Command::FunctionMapSV;
std::string Command::Params::join(size_t startIndex)
std::string Command::Params::join(const int index)
{
std::string result;
for (size_t i = startIndex; i < this->length(); ++i)
for (auto i = index; i < this->size(); i++)
{
if (i > startIndex) result.append(" ");
result.append(this->operator[](i));
if (i > index) result.append(" ");
result.append(this->get(i));
}
return result;
}
const char* Command::Params::operator[](size_t index)
Command::ClientParams::ClientParams()
: nesting_(Game::cmd_args->nesting)
{
return this->get(index);
assert(Game::cmd_args->nesting < Game::CMD_MAX_NESTING);
}
const char* Command::ClientParams::get(size_t index)
int Command::ClientParams::size()
{
if (index >= this->length()) return "";
return Game::cmd_argv[this->commandId][index];
return Game::cmd_args->argc[this->nesting_];
}
size_t Command::ClientParams::length()
const char* Command::ClientParams::get(const int index)
{
return Game::cmd_argc[this->commandId];
if (index >= this->size())
{
return "";
}
return Game::cmd_args->argv[this->nesting_][index];
}
const char* Command::ServerParams::get(size_t index)
Command::ServerParams::ServerParams()
: nesting_(Game::sv_cmd_args->nesting)
{
if (index >= this->length()) return "";
return Game::cmd_argv_sv[this->commandId][index];
assert(Game::sv_cmd_args->nesting < Game::CMD_MAX_NESTING);
}
size_t Command::ServerParams::length()
int Command::ServerParams::size()
{
return Game::cmd_argc_sv[this->commandId];
return Game::sv_cmd_args->argc[this->nesting_];
}
const char* Command::ServerParams::get(const int index)
{
if (index >= this->size())
{
return "";
}
return Game::sv_cmd_args->argv[this->nesting_][index];
}
void Command::Add(const char* name, Utils::Slot<Command::Callback> callback)
{
std::string command = Utils::String::ToLower(name);
const auto command = Utils::String::ToLower(name);
const auto got = Command::FunctionMap.find(command);
if (Command::FunctionMap.find(command) == Command::FunctionMap.end())
if (got == Command::FunctionMap.end())
{
Command::AddRaw(name, Command::MainCallback);
}
Command::FunctionMap[command] = callback;
got->second = std::move(callback);
}
void Command::AddSV(const char* name, Utils::Slot<Command::Callback> callback)
@ -70,9 +86,10 @@ namespace Components
return;
}
std::string command = Utils::String::ToLower(name);
const auto command = Utils::String::ToLower(name);
const auto got = Command::FunctionMapSV.find(command);
if (Command::FunctionMapSV.find(command) == Command::FunctionMapSV.end())
if (got == Command::FunctionMapSV.end())
{
Command::AddRawSV(name, Command::MainCallbackSV);
@ -80,7 +97,7 @@ namespace Components
Command::AddRaw(name, Game::Cbuf_AddServerText);
}
Command::FunctionMapSV[command] = callback;
got->second = std::move(callback);
}
void Command::AddRaw(const char* name, void(*callback)(), bool key)
@ -134,25 +151,27 @@ namespace Components
void Command::MainCallback()
{
Command::ClientParams params(*Game::cmd_id);
Command::ClientParams params;
std::string command = Utils::String::ToLower(params[0]);
const auto command = Utils::String::ToLower(params[0]);
const auto got = Command::FunctionMap.find(command);
if (Command::FunctionMap.find(command) != Command::FunctionMap.end())
if (got != Command::FunctionMap.end())
{
Command::FunctionMap[command](&params);
got->second(&params);
}
}
void Command::MainCallbackSV()
{
Command::ServerParams params(*Game::cmd_id_sv);
Command::ServerParams params;
std::string command = Utils::String::ToLower(params[0]);
const auto command = Utils::String::ToLower(params[0]);
const auto got = Command::FunctionMapSV.find(command);
if (Command::FunctionMapSV.find(command) != Command::FunctionMapSV.end())
if (got != Command::FunctionMapSV.end())
{
Command::FunctionMapSV[command](&params);
got->second(&params);
}
}
@ -162,7 +181,7 @@ namespace Components
Command::Add("openLink", [](Command::Params* params)
{
if (params->length() > 1)
if (params->size() > 1)
{
Utils::OpenUrl(params->get(1));
}

View File

@ -9,40 +9,39 @@ namespace Components
{
public:
Params() {};
virtual ~Params() {};
virtual const char* get(size_t index) = 0;
virtual size_t length() = 0;
virtual std::string join(size_t startIndex);
virtual const char* operator[](size_t index);
virtual int size() = 0;
virtual const char* get(int index) = 0;
virtual std::string join(int index);
virtual const char* operator[](const int index)
{
return this->get(index);
}
};
class ClientParams : public Params
{
public:
ClientParams(unsigned int id) : commandId(id) {};
ClientParams(const ClientParams &obj) : commandId(obj.commandId) {};
ClientParams() : ClientParams(*Game::cmd_id) {};
ClientParams();
const char* get(size_t index) override;
size_t length() override;
int size() override;
const char* get(int index) override;
private:
unsigned int commandId;
int nesting_;
};
class ServerParams : public Params
{
public:
ServerParams(unsigned int id) : commandId(id) {};
ServerParams(const ServerParams &obj) : commandId(obj.commandId) {};
ServerParams() : ServerParams(*Game::cmd_id_sv) {};
ServerParams();
const char* get(size_t index) override;
size_t length() override;
int size() override;
const char* get(int index) override;
private:
unsigned int commandId;
int nesting_;
};
typedef void(Callback)(Command::Params* params);

View File

@ -398,7 +398,7 @@ namespace Components
// Say command
Command::AddSV("say", [](Command::Params* params)
{
if (params->length() < 2) return;
if (params->size() < 2) return;
std::string message = params->join(1);
std::string name = Dvar::Var("sv_sayName").get<std::string>();
@ -418,7 +418,7 @@ namespace Components
// Tell command
Command::AddSV("tell", [](Command::Params* params)
{
if (params->length() < 3) return;
if (params->size() < 3) return;
int client = atoi(params->get(1));
std::string message = params->join(2);
@ -439,7 +439,7 @@ namespace Components
// Sayraw command
Command::AddSV("sayraw", [](Command::Params* params)
{
if (params->length() < 2) return;
if (params->size() < 2) return;
std::string message = params->join(1);
Game::SV_GameSendServerCommand(-1, 0, Utils::String::VA("%c \"%s\"", 104, message.data()));
@ -449,33 +449,13 @@ namespace Components
// Tellraw command
Command::AddSV("tellraw", [](Command::Params* params)
{
if (params->length() < 3) return;
if (params->size() < 3) return;
int client = atoi(params->get(1));
std::string message = params->join(2);
Game::SV_GameSendServerCommand(client, 0, Utils::String::VA("%c \"%s\"", 104, message.data()));
Game::Com_Printf(15, "Raw -> %i: %s\n", client, message.data());
});
// ! command
Command::AddSV("!", [](Command::Params* params)
{
if (params->length() != 2) return;
int client = -1;
if (params->get(1) != "all"s)
{
client = atoi(params->get(1));
if (client >= *reinterpret_cast<int*>(0x31D938C))
{
Game::Com_Printf(0, "Invalid player.\n");
return;
}
}
Game::SV_GameSendServerCommand(client, 0, Utils::String::VA("%c \"\"", 106));
});
});
}
}

View File

@ -181,22 +181,22 @@ namespace Components
}
}
template<> static Dvar::Var Dvar::Register(const char* name, bool value, Dvar::Flag flag, const char* description)
template<> Dvar::Var Dvar::Register(const char* name, bool value, Dvar::Flag flag, const char* description)
{
return Game::Dvar_RegisterBool(name, value, flag.val, description);
}
template<> static Dvar::Var Dvar::Register(const char* name, const char* value, Dvar::Flag flag, const char* description)
template<> Dvar::Var Dvar::Register(const char* name, const char* value, Dvar::Flag flag, const char* description)
{
return Game::Dvar_RegisterString(name, value, flag.val, description);
}
template<> static Dvar::Var Dvar::Register(const char* name, int value, int min, int max, Dvar::Flag flag, const char* description)
template<> Dvar::Var Dvar::Register(const char* name, int value, int min, int max, Dvar::Flag flag, const char* description)
{
return Game::Dvar_RegisterInt(name, value, min, max, flag.val, description);
}
template<> static Dvar::Var Dvar::Register(const char* name, float value, float min, float max, Dvar::Flag flag, const char* description)
template<> Dvar::Var Dvar::Register(const char* name, float value, float min, float max, Dvar::Flag flag, const char* description)
{
return Game::Dvar_RegisterFloat(name, value, min, max, flag.val, description);
}
@ -336,10 +336,10 @@ namespace Components
// remove write protection from fs_game
Utils::Hook::Xor<DWORD>(0x6431EA, Game::dvar_flag::DVAR_FLAG_WRITEPROTECTED);
// set cg_fov max to 90.0
// ...120 because of V2
static float cgFov90 = 120.0f;
Utils::Hook::Set<float*>(0x4F8E28, &cgFov90);
// set cg_fov max to 160.0
// because that's the max on SP
static float cg_Fov = 160.0f;
Utils::Hook::Set<float*>(0x4F8E28, &cg_Fov);
// set max volume to 1
static float volume = 1.0f;
@ -385,7 +385,6 @@ namespace Components
Utils::Hook(0x59386A, Dvar::DvarSetFromStringByNameStub, HOOK_CALL).install()->quick();
// If the game closed abruptly, the dvars would not have been restored
Dvar::OnInit([]
{
Dvar::ResetDvarsValue();

View File

@ -234,7 +234,7 @@ namespace Components
Game::UI_UpdateArenas();
std::string command;
for (auto i = 0; i < (params->length() >= 2 ? atoi(params->get(1)) : *Game::arenaCount); ++i)
for (auto i = 0; i < (params->size() >= 2 ? atoi(params->get(1)) : *Game::arenaCount); ++i)
{
const auto* mapname = ArenaLength::NewArenas[i % *Game::arenaCount].mapName;

View File

@ -594,7 +594,7 @@ namespace Components
Command::Add("loadzone", [](Command::Params* params)
{
if (params->length() < 2) return;
if (params->size() < 2) return;
Game::XZoneInfo info;
info.name = params->get(1);

View File

@ -583,7 +583,7 @@ namespace Components
Command::Add("addFriend", [](Command::Params* params)
{
if (params->length() < 2u)
if (params->size() < 2u)
{
Logger::Print("Usage: %s <Steam ID in hexadecimal format>\n", params->get(0));
return;

View File

@ -1641,7 +1641,7 @@ namespace Components
void Gamepad::Axis_Bind_f(Command::Params* params)
{
if (params->length() < 4)
if (params->size() < 4)
{
Logger::Print("bindaxis <real axis> <virtual axis> <input type>\n");
return;

View File

@ -261,7 +261,7 @@ namespace Components
{
Command::AddSV("log_add", [](Command::Params* params)
{
if (params->length() < 2) return;
if (params->size() < 2) return;
Network::Address addr(params->get(1));
@ -273,7 +273,7 @@ namespace Components
Command::AddSV("log_del", [](Command::Params* params)
{
if (params->length() < 2) return;
if (params->size() < 2) return;
int num = atoi(params->get(1));
if (Utils::String::VA("%i", num) == std::string(params->get(1)) && static_cast<unsigned int>(num) < Logger::LoggingAddresses[0].size())
@ -312,7 +312,7 @@ namespace Components
Command::AddSV("g_log_add", [](Command::Params* params)
{
if (params->length() < 2) return;
if (params->size() < 2) return;
Network::Address addr(params->get(1));
@ -324,7 +324,7 @@ namespace Components
Command::AddSV("g_log_del", [](Command::Params* params)
{
if (params->length() < 2) return;
if (params->size() < 2) return;
int num = atoi(params->get(1));
if (Utils::String::VA("%i", num) == std::string(params->get(1)) && static_cast<unsigned int>(num) < Logger::LoggingAddresses[1].size())

View File

@ -845,7 +845,7 @@ namespace Components
Command::Add("openmenu", [](Command::Params* params)
{
if (params->length() != 2)
if (params->size() != 2)
{
Logger::Print("USAGE: openmenu <menu name>\n");
return;

View File

@ -377,7 +377,7 @@ namespace Components
Command::Add("addnode", [](Command::Params* params)
{
if (params->length() < 2) return;
if (params->size() < 2) return;
Node::Add({ params->get(1) });
});
}

View File

@ -261,7 +261,7 @@ namespace Components
Command::Add("connect", [](Command::Params* params)
{
if (params->length() < 2)
if (params->size() < 2)
{
return;
}

View File

@ -86,17 +86,15 @@ namespace Components
void QuickPatch::SelectStringTableEntryInDvarStub()
{
Command::ClientParams args;
Command::ClientParams params;
if (args.length() >= 4)
if (params.size() >= 4)
{
std::string cmd = args[0];
std::string table = args[1];
std::string col = args[2];
std::string dvarName = args[3];
Game::dvar_t* dvar = Game::Dvar_FindVar(dvarName.data());
const auto* dvarName = params[3];
const auto* dvar = Game::Dvar_FindVar(dvarName);
if (Command::Find(dvarName) || (dvar && (dvar->flags & (Game::DVAR_FLAG_WRITEPROTECTED | Game::DVAR_FLAG_CHEAT | Game::DVAR_FLAG_READONLY))))
if (Command::Find(dvarName) ||
(dvar != nullptr && dvar->flags & (Game::DVAR_FLAG_WRITEPROTECTED | Game::DVAR_FLAG_CHEAT | Game::DVAR_FLAG_READONLY)))
{
return;
}
@ -671,7 +669,7 @@ namespace Components
Command::Add("dumptechsets", [](Command::Params* param)
{
if (param->length() != 2)
if (param->size() != 2)
{
Logger::Print("usage: dumptechsets <fastfile> | all\n");
return;

View File

@ -11,15 +11,15 @@ namespace Components
{
Command::Add("rcon", [](Command::Params* params)
{
if (params->length() < 2) return;
if (params->size() < 2) return;
std::string operation = params->get(1);
if (operation == "login")
const auto* operation = params->get(1);
if (std::strcmp(operation, "login") == 0)
{
if (params->length() < 3) return;
if (params->size() < 3) return;
RCon::Password = params->get(2);
}
else if (operation == "logout")
else if (std::strcmp(operation, "logout") == 0)
{
RCon::Password.clear();
}

View File

@ -24,7 +24,7 @@ namespace Components
Command::Add("dumpraw", [](Command::Params* params)
{
if (params->length() < 2)
if (params->size() < 2)
{
Logger::Print("Specify a filename!\n");
return;

View File

@ -11,11 +11,11 @@ namespace Components
bool ServerCommands::OnServerCommand()
{
Command::ClientParams params(*Game::cmd_id);
Command::ClientParams params;
for (auto &serverCommandCB : ServerCommands::Commands)
for (const auto& serverCommandCB : ServerCommands::Commands)
{
if (params.length() >= 1)
if (params.size() >= 1)
{
if (params.get(0)[0] == serverCommandCB.first)
{

View File

@ -37,7 +37,10 @@ namespace Components
int Weapon::ParseWeaponConfigStrings()
{
Command::ClientParams params;
if (params.length() <= 1) return 0;
if (params.size() <= 1)
return 0;
int index = atoi(params[1]);
if (index >= 4139)

View File

@ -1110,7 +1110,7 @@ namespace Components
Command::Add("verifyzone", [](Command::Params* params)
{
if (params->length() < 2) return;
if (params->size() < 2) return;
/*
Utils::Hook(0x4AE9C2, [] {
Game::WeaponCompleteDef** varPtr = (Game::WeaponCompleteDef**)0x112A9F4;
@ -1165,7 +1165,7 @@ namespace Components
Command::Add("buildzone", [](Command::Params* params)
{
if (params->length() < 2) return;
if (params->size() < 2) return;
std::string zoneName = params->get(1);
Logger::Print("Building zone '%s'...\n", zoneName.data());
@ -1455,7 +1455,7 @@ namespace Components
Command::Add("listassets", [](Command::Params* params)
{
if (params->length() < 2) return;
if (params->size() < 2) return;
Game::XAssetType type = Game::DB_GetXAssetNameType(params->get(1));
if (type != Game::XAssetType::ASSET_TYPE_INVALID)
@ -1470,7 +1470,7 @@ namespace Components
Command::Add("loadtempzone", [](Command::Params* params)
{
if (params->length() < 2) return;
if (params->size() < 2) return;
if (FastFiles::Exists(params->get(1)))
{
@ -1502,7 +1502,7 @@ namespace Components
Command::Add("iwiDump", [](Command::Params* params)
{
if (params->length() < 2) return;
if (params->size() < 2) return;
std::string path = Utils::String::VA("%s\\mods\\%s\\images", Dvar::Var("fs_basepath").get<const char*>(), params->get(1));
std::vector<std::string> images = FileSystem::GetSysFileList(path, "iwi", false);

View File

@ -390,13 +390,8 @@ namespace Game
XAssetHeader* DB_XAssetPool = reinterpret_cast<XAssetHeader*>(0x7998A8);
unsigned int* g_poolSize = reinterpret_cast<unsigned int*>(0x7995E8);
DWORD* cmd_id = reinterpret_cast<DWORD*>(0x1AAC5D0);
DWORD* cmd_argc = reinterpret_cast<DWORD*>(0x1AAC614);
char*** cmd_argv = reinterpret_cast<char***>(0x1AAC634);
DWORD* cmd_id_sv = reinterpret_cast<DWORD*>(0x1ACF8A0);
DWORD* cmd_argc_sv = reinterpret_cast<DWORD*>(0x1ACF8E4);
char*** cmd_argv_sv = reinterpret_cast<char***>(0x1ACF904);
CmdArgs* cmd_args = reinterpret_cast<CmdArgs*>(0x1AAC5D0);
CmdArgs* sv_cmd_args = reinterpret_cast<CmdArgs*>(0x1ACF8A0);
cmd_function_t** cmd_functions = reinterpret_cast<cmd_function_t**>(0x1AAC658);

View File

@ -921,13 +921,9 @@ namespace Game
extern XAssetHeader* DB_XAssetPool;
extern unsigned int* g_poolSize;
extern DWORD* cmd_id;
extern DWORD* cmd_argc;
extern char*** cmd_argv;
extern DWORD* cmd_id_sv;
extern DWORD* cmd_argc_sv;
extern char*** cmd_argv_sv;
constexpr auto CMD_MAX_NESTING = 8;
extern CmdArgs* cmd_args;
extern CmdArgs* sv_cmd_args;
extern cmd_function_t** cmd_functions;

View File

@ -170,8 +170,8 @@ namespace Game
typedef enum
{
CS_FREE = 0x0,
CS_UNKNOWN1 = 0x1,
CS_UNKNOWN2 = 0x2,
CS_ZOMBIE = 0x1,
CS_RECONNECTING = 0x2,
CS_CONNECTED = 0x3,
CS_CLIENTLOADING = 0x4,
CS_ACTIVE = 0x5,
@ -244,6 +244,17 @@ namespace Game
struct MenuEventHandlerSet;
struct menuDef_t;
struct CmdArgs
{
int nesting;
int localClientNum[8];
int controllerIndex[8];
int argc[8];
const char** argv[8];
};
static_assert(sizeof(CmdArgs) == 132);
typedef struct cmd_function_s
{
cmd_function_s *next;