From fa0df413f47dbc8074999c3ce22c3c0b8ffee562 Mon Sep 17 00:00:00 2001 From: Drifter Date: Wed, 31 Jul 2013 22:11:15 -0400 Subject: [PATCH] Added CS_IsValidWeaponID native and validity checks to other natives (bug 5566, r=psychonic). --- extensions/cstrike/natives.cpp | 20 +++++++++++++++++--- extensions/cstrike/util_cstrike.cpp | 15 +++++++++++++++ extensions/cstrike/util_cstrike.h | 4 +++- plugins/include/cstrike.inc | 9 +++++++++ 4 files changed, 44 insertions(+), 4 deletions(-) diff --git a/extensions/cstrike/natives.cpp b/extensions/cstrike/natives.cpp index eaeaa240..d649fefa 100644 --- a/extensions/cstrike/natives.cpp +++ b/extensions/cstrike/natives.cpp @@ -296,6 +296,9 @@ static cell_t CS_TerminateRound(IPluginContext *pContext, const cell_t *params) static cell_t CS_WeaponIDToAlias(IPluginContext *pContext, const cell_t *params) { + if (!IsValidWeaponID(params[1])) + return pContext->ThrowNativeError("Invalid WeaponID passed for this game"); + char *dest; pContext->LocalToString(params[2], &dest); @@ -326,8 +329,8 @@ static cell_t CS_GetTranslatedWeaponAlias(IPluginContext *pContext, const cell_t static cell_t CS_GetWeaponPrice(IPluginContext *pContext, const cell_t *params) { - if (params[2] == (int)SMCSWeapon_NONE) - return pContext->ThrowNativeError("Invalid Weapon ID passed"); + if (!IsValidWeaponID(params[2])) + return pContext->ThrowNativeError("Invalid WeaponID passed for this game"); int id = GetRealWeaponID(params[2]); @@ -466,7 +469,12 @@ static cell_t CS_AliasToWeaponID(IPluginContext *pContext, const cell_t *params) pContext->LocalToString(params[1], &weapon); - return GetFakeWeaponID(AliasToWeaponID(weapon)); + int id = GetFakeWeaponID(AliasToWeaponID(weapon)); + + if (!IsValidWeaponID(id)) + return SMCSWeapon_NONE; + + return id; } static cell_t CS_SetTeamScore(IPluginContext *pContext, const cell_t *params) @@ -603,6 +611,11 @@ static cell_t CS_GetTeamScore(IPluginContext *pContext, const cell_t *params) return pContext->ThrowNativeError("Invalid team index passed (%i).", params[1]); } +static cell_t CS_IsValidWeaponID(IPluginContext *pContext, const cell_t *params) +{ + return IsValidWeaponID(params[1]) ? 1 : 0; +} + template static inline T *GetPlayerVarAddressOrError(const char *pszGamedataName, IPluginContext *pContext, CBaseEntity *pPlayerEntity) { @@ -755,6 +768,7 @@ sp_nativeinfo_t g_CSNatives[] = {"CS_SetClientContributionScore", CS_SetClientContributionScore}, {"CS_GetClientAssists", CS_GetClientAssists}, {"CS_SetClientAssists", CS_SetClientAssists}, + {"CS_IsValidWeaponID", CS_IsValidWeaponID}, {NULL, NULL} }; diff --git a/extensions/cstrike/util_cstrike.cpp b/extensions/cstrike/util_cstrike.cpp index 4a01f251..ffb616c4 100644 --- a/extensions/cstrike/util_cstrike.cpp +++ b/extensions/cstrike/util_cstrike.cpp @@ -406,3 +406,18 @@ int GetFakeWeaponID(int weaponId) return weaponId; #endif } +bool IsValidWeaponID(int id) +{ + if (id == (int)SMCSWeapon_NONE || id > (int)SMCSWeapon_DEFUSER) + return false; + //Why are these even HERE!?! They dont exist in CS:GO but have valid ID's still +#if SOURCE_ENGINE == SE_CSGO + else if (id == (int)SMCSWeapon_P228 || id == (int)SMCSWeapon_SCOUT || id == (int)SMCSWeapon_SG550 || id == (int)SMCSWeapon_GALIL || id == (int)SMCSWeapon_SCAR17 || + id == (int)SMCSWeapon_USP || id == (int)SMCSWeapon_M3 || id == (int)SMCSWeapon_MP5NAVY || id == (int)SMCSWeapon_TMP || id == (int)SMCSWeapon_SG552) + return false; +#else + else if (id > (int)SMCSWeapon_NIGHTVISION || id < (int)SMCSWeapon_NONE) + return false; +#endif + return true; +} diff --git a/extensions/cstrike/util_cstrike.h b/extensions/cstrike/util_cstrike.h index ddafb663..1a77ad39 100644 --- a/extensions/cstrike/util_cstrike.h +++ b/extensions/cstrike/util_cstrike.h @@ -90,6 +90,7 @@ enum CSGOWeapon CSGOWeapon_NVG, CSGOWeapon_DEFUSER }; +#endif enum SMCSWeapon { SMCSWeapon_NONE = 0, @@ -148,7 +149,6 @@ enum SMCSWeapon SMCSWeapon_INCGRENADE, SMCSWeapon_DEFUSER }; -#endif void *GetWeaponInfo(int weaponID); const char *GetTranslatedWeaponAlias(const char *weapon); @@ -161,4 +161,6 @@ int GetRealWeaponID(int weaponId); int GetFakeWeaponID(int weaponId); +bool IsValidWeaponID(int weaponId); + #endif diff --git a/plugins/include/cstrike.inc b/plugins/include/cstrike.inc index 0b4e9bc1..b07a3134 100644 --- a/plugins/include/cstrike.inc +++ b/plugins/include/cstrike.inc @@ -358,6 +358,15 @@ native CSWeaponID:CS_AliasToWeaponID(const String:alias[]); */ native CS_WeaponIDToAlias(CSWeaponID:weaponID, String:destination[], len); +/** + * Returns weather a WeaponID is valid on the current mod (css or csgo) + * @param weaponID WeaponID to check + * @return Returns true if its a valid WeaponID false otherwise. + * + * @note This will return false always for CSWeapon_NONE + */ +native bool:CS_IsValidWeaponID(CSWeaponID:id); + /** * Do not edit below this line! */