Refactor script param checking behaviour

This commit is contained in:
FutureRave 2022-01-16 13:45:18 +00:00
parent 5231077b95
commit 5d26af4a28
No known key found for this signature in database
GPG Key ID: E883E2BC9657D955
5 changed files with 44 additions and 112 deletions

View File

@ -144,23 +144,17 @@ namespace Components
void Bots::AddMethods()
{
Script::AddFunction("SetPing", [](Game::scr_entref_t id) // gsc: self SetPing(<int>)
Script::AddFunction("SetPing", [](Game::scr_entref_t entref) // gsc: self SetPing(<int>)
{
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<int16_t>(ping);
});
Script::AddFunction("IsBot", [](Game::scr_entref_t id) // Usage: <bot> IsBot();
Script::AddFunction("IsBot", [](Game::scr_entref_t entref) // Usage: <bot> 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: <bot> BotStop();
Script::AddFunction("BotStop", [](Game::scr_entref_t entref) // Usage: <bot> 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: <bot> BotWeapon(<str>);
Script::AddFunction("BotWeapon", [](Game::scr_entref_t entref) // Usage: <bot> BotWeapon(<str>);
{
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<uint16_t>(weapId);
});
Script::AddFunction("BotAction", [](Game::scr_entref_t id) // Usage: <bot> BotAction(<str action>);
Script::AddFunction("BotAction", [](Game::scr_entref_t entref) // Usage: <bot> BotAction(<str action>);
{
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: <bot> BotMovement(<int>, <int>);
Script::AddFunction("BotMovement", [](Game::scr_entref_t entref) // Usage: <bot> BotMovement(<int>, <int>);
{
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)

View File

@ -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);

View File

@ -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))
{

View File

@ -403,7 +403,7 @@ namespace Components
{
if (static_cast<unsigned int>(index) >= Game::scrVmPub->outparamcount)
{
Game::Scr_Error("^1GetCodePosForParam: Index is out of range!\n");
Game::Scr_ParamError(static_cast<unsigned int>(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<unsigned int>(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(<string>)
{
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(<string>)
{
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(<str key>, <str data>);
{
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(<str key>);
{
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(<str key>);
{
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(<str key>);
{
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(<int toggle>)
{
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()

View File

@ -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