From 5d26af4a28cd0f4bf4aaeb093d0e8c2ae10efa65 Mon Sep 17 00:00:00 2001 From: FutureRave Date: Sun, 16 Jan 2022 13:45:18 +0000 Subject: [PATCH] Refactor script param checking behaviour --- src/Components/Modules/Bots.cpp | 54 +++++------------- src/Components/Modules/Client.cpp | 8 +-- src/Components/Modules/ClientCommand.cpp | 14 ++--- src/Components/Modules/Script.cpp | 73 ++++-------------------- src/Game/Structs.hpp | 7 ++- 5 files changed, 44 insertions(+), 112 deletions(-) diff --git a/src/Components/Modules/Bots.cpp b/src/Components/Modules/Bots.cpp index 8e59d30d..f056034a 100644 --- a/src/Components/Modules/Bots.cpp +++ b/src/Components/Modules/Bots.cpp @@ -144,23 +144,17 @@ namespace Components void Bots::AddMethods() { - Script::AddFunction("SetPing", [](Game::scr_entref_t id) // gsc: self SetPing() + Script::AddFunction("SetPing", [](Game::scr_entref_t entref) // gsc: self SetPing() { - if (Game::Scr_GetNumParam() != 1u || Game::Scr_GetType(0) != Game::VAR_INTEGER) - { - Game::Scr_Error("^1SetPing: Needs one integer parameter!\n"); - return; - } - const auto ping = Game::Scr_GetInt(0); if (ping < 0 || ping > 999) { - Game::Scr_Error("^1SetPing: Ping needs to between 0 and 999!\n"); + Game::Scr_ParamError(0, "^1SetPing: Ping needs to be between 0 and 999!\n"); return; } - const auto* gentity = Script::GetEntFromEntRef(id); + const auto* gentity = Script::GetEntFromEntRef(entref); auto* client = Script::GetClientFromEnt(gentity); if (!client->isBot) @@ -172,17 +166,17 @@ namespace Components client->ping = static_cast(ping); }); - Script::AddFunction("IsBot", [](Game::scr_entref_t id) // Usage: IsBot(); + Script::AddFunction("IsBot", [](Game::scr_entref_t entref) // Usage: IsBot(); { - const auto* gentity = Script::GetEntFromEntRef(id); + const auto* gentity = Script::GetEntFromEntRef(entref); const auto* client = Script::GetClientFromEnt(gentity); Game::Scr_AddBool(client->isBot == 1); }); - Script::AddFunction("BotStop", [](Game::scr_entref_t id) // Usage: BotStop(); + Script::AddFunction("BotStop", [](Game::scr_entref_t entref) // Usage: BotStop(); { - const auto* gentity = Script::GetEntFromEntRef(id); + const auto* gentity = Script::GetEntFromEntRef(entref); const auto* client = Script::GetClientFromEnt(gentity); if (!client->isBot) @@ -195,17 +189,11 @@ namespace Components g_botai[gentity->s.number].weapon = 1; }); - Script::AddFunction("BotWeapon", [](Game::scr_entref_t id) // Usage: BotWeapon(); + Script::AddFunction("BotWeapon", [](Game::scr_entref_t entref) // Usage: BotWeapon(); { - if (Game::Scr_GetNumParam() != 1u || Game::Scr_GetType(0) != Game::VAR_STRING) - { - Game::Scr_Error("^1BotWeapon: Needs one string parameter!\n"); - return; - } - const auto* weapon = Game::Scr_GetString(0); - const auto* gentity = Script::GetEntFromEntRef(id); + const auto* gentity = Script::GetEntFromEntRef(entref); const auto* client = Script::GetClientFromEnt(gentity); if (!client->isBot) @@ -224,17 +212,11 @@ namespace Components g_botai[gentity->s.number].weapon = static_cast(weapId); }); - Script::AddFunction("BotAction", [](Game::scr_entref_t id) // Usage: BotAction(); + Script::AddFunction("BotAction", [](Game::scr_entref_t entref) // Usage: BotAction(); { - if (Game::Scr_GetNumParam() != 1u || Game::Scr_GetType(0) != Game::VAR_STRING) - { - Game::Scr_Error("^1BotAction: Needs one string parameter!\n"); - return; - } - const auto* action = Game::Scr_GetString(0); - const auto* gentity = Script::GetEntFromEntRef(id); + const auto* gentity = Script::GetEntFromEntRef(entref); const auto* client = Script::GetClientFromEnt(gentity); if (!client->isBot) @@ -245,7 +227,7 @@ namespace Components if (action[0] != '+' && action[0] != '-') { - Game::Scr_Error("^1BotAction: Sign for action must be '+' or '-'.\n"); + Game::Scr_ParamError(0, "^1BotAction: Sign for action must be '+' or '-'.\n"); return; } @@ -262,21 +244,15 @@ namespace Components return; } - Game::Scr_Error("^1BotAction: Unknown action.\n"); + Game::Scr_ParamError(0, "^1BotAction: Unknown action.\n"); }); - Script::AddFunction("BotMovement", [](Game::scr_entref_t id) // Usage: BotMovement(, ); + Script::AddFunction("BotMovement", [](Game::scr_entref_t entref) // Usage: BotMovement(, ); { - if (Game::Scr_GetNumParam() != 2u || Game::Scr_GetType(0) != Game::VAR_INTEGER || Game::Scr_GetType(1) != Game::VAR_INTEGER) - { - Game::Scr_Error("^1BotMovement: Needs two integer parameters!\n"); - return; - } - auto forwardInt = Game::Scr_GetInt(0); auto rightInt = Game::Scr_GetInt(1); - const auto* gentity = Script::GetEntFromEntRef(id); + const auto* gentity = Script::GetEntFromEntRef(entref); const auto* client = Script::GetClientFromEnt(gentity); if (!client->isBot) diff --git a/src/Components/Modules/Client.cpp b/src/Components/Modules/Client.cpp index 76ce68e2..cb58cd29 100644 --- a/src/Components/Modules/Client.cpp +++ b/src/Components/Modules/Client.cpp @@ -126,9 +126,9 @@ namespace Components void Client::AddMethods() { // Client methods - Script::AddFunction("GetIp", [](Game::scr_entref_t id) // gsc: self GetIp() + Script::AddFunction("GetIp", [](Game::scr_entref_t entref) // gsc: self GetIp() { - const auto* gentity = Script::GetEntFromEntRef(id); + const auto* gentity = Script::GetEntFromEntRef(entref); const auto* client = Script::GetClientFromEnt(gentity); std::string ip = Game::NET_AdrToString(client->netchan.remoteAddress); @@ -139,9 +139,9 @@ namespace Components Game::Scr_AddString(ip.data()); }); - Script::AddFunction("GetPing", [](Game::scr_entref_t id) // gsc: self GetPing() + Script::AddFunction("GetPing", [](Game::scr_entref_t entref) // gsc: self GetPing() { - const auto* gentity = Script::GetEntFromEntRef(id); + const auto* gentity = Script::GetEntFromEntRef(entref); const auto* client = Script::GetClientFromEnt(gentity); Game::Scr_AddInt(client->ping); diff --git a/src/Components/Modules/ClientCommand.cpp b/src/Components/Modules/ClientCommand.cpp index c1bdf775..d18ecc36 100644 --- a/src/Components/Modules/ClientCommand.cpp +++ b/src/Components/Modules/ClientCommand.cpp @@ -146,11 +146,11 @@ namespace Components if (ent->client == nullptr) { - Game::Scr_ObjectError(Utils::String::VA("^1NoClip: entity %u is not a client\n", entref)); + Game::Scr_ObjectError(Utils::String::VA("^1NoClip: entity %i is not a client\n", ent->s.number)); return; } - if (Game::Scr_GetNumParam() == 1u && Game::Scr_GetType(0) == Game::VAR_INTEGER) + if (Game::Scr_GetNumParam() == 1u) { if (Game::Scr_GetInt(0)) { @@ -173,11 +173,11 @@ namespace Components if (ent->client == nullptr) { - Game::Scr_ObjectError(Utils::String::VA("^1Ufo: entity %u is not a client\n", entref)); + Game::Scr_ObjectError(Utils::String::VA("^1Ufo: entity %i is not a client\n", ent->s.number)); return; } - if (Game::Scr_GetNumParam() == 1u && Game::Scr_GetType(0) == Game::VAR_INTEGER) + if (Game::Scr_GetNumParam() == 1u) { if (Game::Scr_GetInt(0)) { @@ -198,7 +198,7 @@ namespace Components { auto* ent = Script::GetEntFromEntRef(entref); - if (Game::Scr_GetNumParam() == 1u && Game::Scr_GetType(0) == Game::VAR_INTEGER) + if (Game::Scr_GetNumParam() == 1u) { if (Game::Scr_GetInt(0)) { @@ -219,7 +219,7 @@ namespace Components { auto* ent = Script::GetEntFromEntRef(entref); - if (Game::Scr_GetNumParam() == 1u && Game::Scr_GetType(0) == Game::VAR_INTEGER) + if (Game::Scr_GetNumParam() == 1u) { if (Game::Scr_GetInt(0)) { @@ -240,7 +240,7 @@ namespace Components { auto* ent = Script::GetEntFromEntRef(entref); - if (Game::Scr_GetNumParam() == 1u && Game::Scr_GetType(0) == Game::VAR_INTEGER) + if (Game::Scr_GetNumParam() == 1u) { if (Game::Scr_GetInt(0)) { diff --git a/src/Components/Modules/Script.cpp b/src/Components/Modules/Script.cpp index 4bf0bb87..8a6f9256 100644 --- a/src/Components/Modules/Script.cpp +++ b/src/Components/Modules/Script.cpp @@ -403,7 +403,7 @@ namespace Components { if (static_cast(index) >= Game::scrVmPub->outparamcount) { - Game::Scr_Error("^1GetCodePosForParam: Index is out of range!\n"); + Game::Scr_ParamError(static_cast(index), "^1GetCodePosForParam: Index is out of range!\n"); return ""; } @@ -411,7 +411,7 @@ namespace Components if (value->type != Game::VAR_FUNCTION) { - Game::Scr_Error("^1GetCodePosForParam: Expects a function as parameter!\n"); + Game::Scr_ParamError(static_cast(index), "^1GetCodePosForParam: Expects a function as parameter!\n"); return ""; } @@ -489,13 +489,15 @@ namespace Components Game::gentity_t* Script::GetEntFromEntRef(const Game::scr_entref_t entref) { - if (entref >= Game::MAX_GENTITIES) + if (entref.classnum != 0) { Game::Scr_ObjectError("Not an entity"); return nullptr; } - return &Game::g_entities[entref]; + assert(entref.entnum < Game::MAX_GENTITIES); + + return &Game::g_entities[entref.entnum]; } Game::client_t* Script::GetClientFromEnt(const Game::gentity_t* gentity) @@ -545,28 +547,14 @@ namespace Components // Print to console, even without being in 'developer 1'. Script::AddFunction("PrintConsole", [](Game::scr_entref_t) // gsc: PrintConsole() { - if (Game::Scr_GetNumParam() != 1u || Game::Scr_GetType(0) != Game::VAR_STRING) - { - Game::Scr_Error("^1PrintConsole: Needs one string parameter!\n"); - return; - } - const auto str = Game::Scr_GetString(0); - Game::Com_Printf(0, str); }); // Executes command to the console Script::AddFunction("Exec", [](Game::scr_entref_t) // gsc: Exec() { - if (Game::Scr_GetNumParam() != 1u || Game::Scr_GetType(0) != Game::VAR_STRING) - { - Game::Scr_Error("^1Exec: Needs one string parameter!\n"); - return; - } - const auto str = Game::Scr_GetString(0); - Command::Execute(str, false); }); @@ -574,12 +562,6 @@ namespace Components // Script Storage Funcs Script::AddFunction("StorageSet", [](Game::scr_entref_t) // gsc: StorageSet(, ); { - if (Game::Scr_GetNumParam() != 2u || Game::Scr_GetType(0) != Game::VAR_STRING || Game::Scr_GetType(1) != Game::VAR_STRING) - { - Game::Scr_Error("^1StorageSet: Needs two string parameters!\n"); - return; - } - std::string key = Game::Scr_GetString(0); std::string data = Game::Scr_GetString(1); @@ -588,17 +570,11 @@ namespace Components Script::AddFunction("StorageRemove", [](Game::scr_entref_t) // gsc: StorageRemove(); { - if (Game::Scr_GetNumParam() != 1u || Game::Scr_GetType(0) != Game::VAR_STRING) - { - Game::Scr_Error("^1StorageRemove: Needs one string parameter!\n"); - return; - } - std::string key = Game::Scr_GetString(0); if (!Script::ScriptStorage.count(key)) { - Game::Scr_Error(Utils::String::VA("^1StorageRemove: Store does not have key '%s'!\n", key.c_str())); + Game::Scr_Error(Utils::String::VA("^1StorageRemove: Store does not have key '%s'!\n", key.data())); return; } @@ -607,35 +583,22 @@ namespace Components Script::AddFunction("StorageGet", [](Game::scr_entref_t) // gsc: StorageGet(); { - if (Game::Scr_GetNumParam() != 1u || Game::Scr_GetType(0) != Game::VAR_STRING) - { - Game::Scr_Error("^1StorageGet: Needs one string parameter!\n"); - return; - } - std::string key = Game::Scr_GetString(0); if (!Script::ScriptStorage.count(key)) { - Game::Scr_Error(Utils::String::VA("^1StorageGet: Store does not have key '%s'!\n", key.c_str())); + Game::Scr_Error(Utils::String::VA("^1StorageGet: Store does not have key '%s'!\n", key.data())); return; } - auto data = Script::ScriptStorage.at(key); - Game::Scr_AddString(data.c_str()); + const auto& data = Script::ScriptStorage.at(key); + Game::Scr_AddString(data.data()); }); Script::AddFunction("StorageHas", [](Game::scr_entref_t) // gsc: StorageHas(); { - if (Game::Scr_GetNumParam() != 1u || Game::Scr_GetType(0) != Game::VAR_STRING) - { - Game::Scr_Error("^1StorageHas: Needs one string parameter!\n"); - return; - } - std::string key = Game::Scr_GetString(0); - - Game::Scr_AddInt(Script::ScriptStorage.count(key)); + Game::Scr_AddBool(Script::ScriptStorage.count(key)); }); Script::AddFunction("StorageClear", [](Game::scr_entref_t) // gsc: StorageClear(); @@ -650,24 +613,12 @@ namespace Components if (ent->client == nullptr) { - Game::Scr_ObjectError(Utils::String::VA("Entity %u is not a player", entref)); + Game::Scr_ObjectError(Utils::String::VA("Entity %i is not a player", ent->s.number)); return; } Game::Scr_AddBool((ent->client->flags & Game::PLAYER_FLAG_FROZEN) != 0); }); - - Script::AddFunction("DebugCode", [](Game::scr_entref_t) // gsc: DebugCode() - { - if (Game::Scr_GetNumParam() != 1u) - { - Game::Scr_Error("^1DebugCode: Needs one int parameter!\n"); - return; - } - - const auto toggle = Game::Scr_GetInt(0); - Game::scrVmPub->debugCode = (toggle) ? true : false; - }, true); } Script::Script() diff --git a/src/Game/Structs.hpp b/src/Game/Structs.hpp index 8a74e54d..28537005 100644 --- a/src/Game/Structs.hpp +++ b/src/Game/Structs.hpp @@ -20,7 +20,12 @@ namespace Game typedef vec_t vec3_t[3]; typedef vec_t vec4_t[4]; - typedef unsigned int scr_entref_t; + typedef struct + { + unsigned __int16 entnum; + unsigned __int16 classnum; + } scr_entref_t; + typedef void(__cdecl * scr_function_t)(scr_entref_t); enum XAssetType