From 453e9da7c55b62e93ee5e19fae6a660dc866f825 Mon Sep 17 00:00:00 2001 From: Ruben Gonzalez Date: Sun, 3 Sep 2017 09:19:12 -0400 Subject: [PATCH 01/11] Intial CS:Go changes and slight changes for CS:S WeaponID related function. --- extensions/cstrike/extension.cpp | 9 + extensions/cstrike/itemdef-hash.h | 95 +++++ extensions/cstrike/natives.cpp | 80 ++--- extensions/cstrike/util_cstrike.cpp | 454 ++++++++++-------------- extensions/cstrike/util_cstrike.h | 268 ++++++-------- gamedata/sm-cstrike.games/game.csgo.txt | 8 + plugins/include/cstrike.inc | 24 +- 7 files changed, 444 insertions(+), 494 deletions(-) create mode 100644 extensions/cstrike/itemdef-hash.h diff --git a/extensions/cstrike/extension.cpp b/extensions/cstrike/extension.cpp index 4a736fad..370f6bf6 100644 --- a/extensions/cstrike/extension.cpp +++ b/extensions/cstrike/extension.cpp @@ -36,6 +36,7 @@ #include "iplayerinfo.h" #include "ISDKTools.h" #include "forwards.h" +#include "util_cstrike.h" /** * @file extension.cpp @@ -126,6 +127,10 @@ void CStrike::SDK_OnUnload() forwards->ReleaseForward(g_pPriceForward); forwards->ReleaseForward(g_pTerminateRoundForward); forwards->ReleaseForward(g_pCSWeaponDropForward); + +#if SOURCE_ENGINE == SE_CSGO + ClearHashMaps(); +#endif } void CStrike::SDK_OnAllLoaded() @@ -146,6 +151,10 @@ void CStrike::SDK_OnAllLoaded() hooked_everything = true; SM_GET_LATE_IFACE(BINTOOLS, g_pBinTools); + +#if SOURCE_ENGINE == SE_CSGO + CreateHashMaps(); +#endif } bool CStrike::QueryRunning(char *error, size_t maxlength) diff --git a/extensions/cstrike/itemdef-hash.h b/extensions/cstrike/itemdef-hash.h new file mode 100644 index 00000000..7642804f --- /dev/null +++ b/extensions/cstrike/itemdef-hash.h @@ -0,0 +1,95 @@ +#ifndef _INCLUDE_CSTRIKE_HASH_H_ +#define _INCLUDE_CSTRIKE_HASH_H_ + +#include +#include +#include + +#define MAX_WEAPON_NAME_LENGTH 80 +class CEconItemDefinition; + +struct HashItemDef_Node +{ + int key; + CEconItemDefinition *pDef; + int iunknown; +}; + +class CHashItemDef +{ +public: + unsigned char padding[36]; + HashItemDef_Node *pMem; + int nAllocatedCount; + int nGrowSize; + int iunknown; + int currentElements; + int maxElements; +}; + +struct ItemDefHashValue +{ + ItemDefHashValue(int iLoadoutSlot, unsigned int iPrice, SMCSWeapon iWeaponID, unsigned int iDefIdx, const char *szClassname) + { + m_iLoadoutSlot = iLoadoutSlot; + m_iPrice = iPrice; + m_iDefIdx = iDefIdx; + m_iWeaponID = iWeaponID; + + Q_strncpy(m_szClassname, szClassname, sizeof(m_szClassname)); + + //Create the item name + Q_strncpy(m_szItemName, GetWeaponNameFromClassname(szClassname), sizeof(m_szItemName)); + } + int m_iLoadoutSlot; + SMCSWeapon m_iWeaponID; + unsigned int m_iPrice; + unsigned int m_iDefIdx; + char m_szClassname[MAX_WEAPON_NAME_LENGTH]; + char m_szItemName[MAX_WEAPON_NAME_LENGTH]; +}; + +struct StringPolicy +{ + static inline uint32_t hash(const char *key) + { + return ke::FastHashCharSequence(key, strlen(key)); + } + static inline bool matches(const char *find, const ke::AString &key) + { + return key.compare(find) == 0; + } +}; + +struct IntegerPolicy +{ + static inline uint32_t hash(const int key) + { + return ke::HashInt32(key); + } + static inline bool matches(const int find, const int &key) + { + return (key == find); + } +}; + +struct WeaponIDPolicy +{ + static inline uint32_t hash(const SMCSWeapon key) + { + return ke::HashInt32((int)key); + } + static inline bool matches(const SMCSWeapon find, const SMCSWeapon &key) + { + return (key == find); + } +}; + +typedef ke::HashMap ClassnameMap; +typedef ke::HashMap ItemIndexMap; +typedef ke::HashMap WeaponIDMap; + +extern ClassnameMap g_mapClassToDefIdx; +extern ItemIndexMap g_mapDefIdxToClass; +extern WeaponIDMap g_mapWeaponIDToDefIdx; +#endif //_INCLUDE_CSTRIKE_HASH_H_ diff --git a/extensions/cstrike/natives.cpp b/extensions/cstrike/natives.cpp index cb8043aa..bc5e5e51 100644 --- a/extensions/cstrike/natives.cpp +++ b/extensions/cstrike/natives.cpp @@ -35,13 +35,17 @@ #include "util_cstrike.h" #include +#if SOURCE_ENGINE == SE_CSGO +#include "itemdef-hash.h" +#endif + int g_iPriceOffset = -1; #define REGISTER_NATIVE_ADDR(name, code) \ void *addr; \ if (!g_pGameConf->GetMemSig(name, &addr) || !addr) \ { \ - return pContext->ThrowNativeError("Failed to locate function"); \ + return pContext->ThrowNativeError("Failed to lookup %s signature.", name); \ } \ code; \ g_RegNatives.Register(pWrapper); @@ -49,7 +53,7 @@ int g_iPriceOffset = -1; #define GET_MEMSIG(name) \ if (!g_pGameConf->GetMemSig(name, &addr) || !addr) \ { \ - return pContext->ThrowNativeError("Failed to locate function"); \ + return pContext->ThrowNativeError("Failed to lookup %s signature.", name); \ } inline CBaseEntity *GetCBaseEntity(int num, bool isplayer) @@ -366,6 +370,10 @@ static cell_t CS_TerminateRound(IPluginContext *pContext, const cell_t *params) static cell_t CS_WeaponIDToAlias(IPluginContext *pContext, const cell_t *params) { +#if SOURCE_ENGINE == SE_CSGO + if(g_mapClassToDefIdx.elements() == 0) + return pContext->ThrowNativeError("Failed to create weapon hashmap"); +#endif if (!IsValidWeaponID(params[1])) return pContext->ThrowNativeError("Invalid WeaponID passed for this game"); @@ -409,7 +417,7 @@ static cell_t CS_GetWeaponPrice(IPluginContext *pContext, const cell_t *params) if (!IsValidWeaponID(params[2])) return pContext->ThrowNativeError("Invalid WeaponID passed for this game"); - int id = GetRealWeaponID(params[2]); + int id = params[2]; //Hard code return values for weapons that dont call GetWeaponPrice and always use default value. if (id == WEAPON_C4 || id == WEAPON_KNIFE || id == WEAPON_SHIELD) @@ -449,55 +457,26 @@ static cell_t CS_GetWeaponPrice(IPluginContext *pContext, const cell_t *params) #else static cell_t CS_GetWeaponPrice(IPluginContext *pContext, const cell_t *params) { + if (g_mapClassToDefIdx.elements() == 0) + return pContext->ThrowNativeError("Failed to create weapon hashmap"); + CBaseEntity *pEntity; if (!(pEntity = GetCBaseEntity(params[1], true))) { return pContext->ThrowNativeError("Client index %d is not valid", params[1]); } - static const char *pPriceKey = NULL; - - if (!pPriceKey) - { - pPriceKey = g_pGameConf->GetKeyValue("PriceKey"); - if (!pPriceKey) - { - return pContext->ThrowNativeError("Failed to get PriceKey KeyValue."); - } - } - if (!IsValidWeaponID(params[2])) return pContext->ThrowNativeError("Invalid WeaponID passed for this game"); - int id = GetRealWeaponID(params[2]); + WeaponIDMap::Result res = g_mapWeaponIDToDefIdx.find((SMCSWeapon)params[2]); - char classname[128]; - - if (id < CSGOWeapon_KEVLAR) - Q_snprintf(classname, sizeof(classname), "weapon_%s", WeaponIDToAlias(params[2])); - else - Q_snprintf(classname, sizeof(classname), "item_%s", WeaponIDToAlias(params[2])); - - CEconItemDefinition *pDef = GetItemDefintionByName(classname); - - if (!pDef) - { - return pContext->ThrowNativeError("Failed to get CEconItemDefinition for %s", classname); - } - - KeyValues *pAttributes = pDef->m_pKv->FindKey("attributes", false); - - if (!pAttributes) - { - return pContext->ThrowNativeError("Failed to get item attributes keyvalue for %s", classname); - } - - int price = pAttributes->GetInt(pPriceKey, 0); + int price = res->value.m_iPrice; if (params[3] || weaponNameOffset == -1) return price; - return CallPriceForward(params[1], WeaponIDToAlias(params[2]), price); + return CallPriceForward(params[1], res->value.m_szClassname, price); } #endif @@ -577,30 +556,15 @@ static cell_t CS_SetClientClanTag(IPluginContext *pContext, const cell_t *params static cell_t CS_AliasToWeaponID(IPluginContext *pContext, const cell_t *params) { +#if SOURCE_ENGINE == SE_CSGO + if (g_mapClassToDefIdx.elements() == 0) + return pContext->ThrowNativeError("Failed to create weapon hashmap"); +#endif char *weapon; pContext->LocalToString(params[1], &weapon); -#if SOURCE_ENGINE == SE_CSGO - if (strstr(weapon, "usp_silencer") != NULL) - { - return SMCSWeapon_HKP2000; - } - else if(strstr(weapon, "cz75a") != NULL) - { - return SMCSWeapon_P250; - } - else if (strstr(weapon, "m4a1_silencer") != NULL) - { - return SMCSWeapon_M4A1; - } - else if (strstr(weapon, "revolver") != NULL) - { - return SMCSWeapon_DEAGLE; - } -#endif - - int id = GetFakeWeaponID(AliasToWeaponID(weapon)); + int id = AliasToWeaponID(weapon); if (!IsValidWeaponID(id)) return SMCSWeapon_NONE; diff --git a/extensions/cstrike/util_cstrike.cpp b/extensions/cstrike/util_cstrike.cpp index f5728663..414f7e71 100644 --- a/extensions/cstrike/util_cstrike.cpp +++ b/extensions/cstrike/util_cstrike.cpp @@ -29,11 +29,17 @@ * Version: $Id$ */ -#include "util_cstrike.h" - #include "extension.h" +#include "util_cstrike.h" #include "RegNatives.h" #include +#if SOURCE_ENGINE == SE_CSGO +#include "itemdef-hash.h" + +ClassnameMap g_mapClassToDefIdx; +ItemIndexMap g_mapDefIdxToClass; +WeaponIDMap g_mapWeaponIDToDefIdx; +#endif #define REGISTER_ADDR(name, defaultret, code) \ void *addr; \ @@ -240,6 +246,145 @@ CEconItemDefinition *GetItemDefintionByName(const char *classname) return pItemDef; } + +void CreateHashMaps() +{ + CEconItemSchema *pSchema = GetItemSchema(); + + if (!pSchema) + return; + + static const char *pPriceKey = NULL; + + if (!pPriceKey) + { + pPriceKey = g_pGameConf->GetKeyValue("PriceKey"); + if (!pPriceKey) + { + return; + } + } + + static int iHashMapOffset = -1; + + if (iHashMapOffset == -1) + { + if (!g_pGameConf->GetOffset("ItemDefHashOffset", &iHashMapOffset) || iHashMapOffset == -1) + { + return; + } + } + + g_mapClassToDefIdx.init(); + g_mapDefIdxToClass.init(); + g_mapWeaponIDToDefIdx.init(); + + CHashItemDef *map = (CHashItemDef *)((intptr_t)pSchema + iHashMapOffset); + + for (int i = 0; i < map->currentElements; i++) + { + HashItemDef_Node node = map->pMem[i]; + + if (!node.pDef || !node.pDef->m_pKv) + continue; + + KeyValues *pClassname = node.pDef->m_pKv->FindKey("name", false); + KeyValues *pAttributes = node.pDef->m_pKv->FindKey("attributes", false); + + if (pClassname && pAttributes) + { + KeyValues *pPrice = pAttributes->FindKey(pPriceKey, false); + + if (pPrice) + { + const char *classname = pClassname->GetString(); + + unsigned int price = pPrice->GetInt(); + uint16_t iItemDefIdx = node.pDef->m_iDefinitionIndex; + SMCSWeapon iWeaponID = GetWeaponIdFromDefIdx(iItemDefIdx); + int iLoadoutslot = node.pDef->GetDefaultLoadoutSlot(); + + ClassnameMap::Insert i = g_mapClassToDefIdx.findForAdd(classname); + g_mapClassToDefIdx.add(i, ke::AString(classname), ItemDefHashValue(iLoadoutslot, price, iWeaponID, iItemDefIdx, classname)); + + ItemIndexMap::Insert x = g_mapDefIdxToClass.findForAdd(iItemDefIdx); + g_mapDefIdxToClass.add(x, iItemDefIdx, ItemDefHashValue(iLoadoutslot, price, iWeaponID, iItemDefIdx, classname)); + + WeaponIDMap::Insert t = g_mapWeaponIDToDefIdx.findForAdd(iWeaponID); + g_mapWeaponIDToDefIdx.add(t, iWeaponID, ItemDefHashValue(iLoadoutslot, price, iWeaponID, iItemDefIdx, classname)); + } + } + } +} + +void ClearHashMaps() +{ + g_mapClassToDefIdx.clear(); + g_mapDefIdxToClass.clear(); + g_mapWeaponIDToDefIdx.clear(); +} + +SMCSWeapon GetWeaponIdFromDefIdx(uint16_t iDefIdx) +{ + //DEAR GOD THIS IS HIDEOUS + //None in the middle are weapons that dont exist. + //If they are added and use the same idx they should be changed to their respective ones + static SMCSWeapon weaponIDMap[SMCSWeapon_MAXWEAPONIDS] = + { + SMCSWeapon_NONE, SMCSWeapon_DEAGLE, SMCSWeapon_ELITE, SMCSWeapon_FIVESEVEN, + SMCSWeapon_GLOCK, SMCSWeapon_NONE, SMCSWeapon_NONE, SMCSWeapon_AK47, + SMCSWeapon_AUG, SMCSWeapon_AWP, SMCSWeapon_FAMAS, SMCSWeapon_G3SG1, + SMCSWeapon_NONE, SMCSWeapon_GALILAR, SMCSWeapon_M249, SMCSWeapon_NONE, + SMCSWeapon_M4A1, SMCSWeapon_MAC10, SMCSWeapon_NONE, SMCSWeapon_P90, + SMCSWeapon_NONE, SMCSWeapon_NONE, SMCSWeapon_NONE, SMCSWeapon_NONE, + SMCSWeapon_UMP45, SMCSWeapon_XM1014, SMCSWeapon_BIZON, SMCSWeapon_MAG7, + SMCSWeapon_NEGEV, SMCSWeapon_SAWEDOFF, SMCSWeapon_TEC9, SMCSWeapon_TASER, + SMCSWeapon_HKP2000, SMCSWeapon_MP7, SMCSWeapon_MP9, SMCSWeapon_NOVA, + SMCSWeapon_P250, SMCSWeapon_NONE, SMCSWeapon_SCAR20, SMCSWeapon_SG556, + SMCSWeapon_SSG08, SMCSWeapon_KNIFE_GG, SMCSWeapon_KNIFE, SMCSWeapon_FLASHBANG, + SMCSWeapon_HEGRENADE, SMCSWeapon_SMOKEGRENADE, SMCSWeapon_MOLOTOV, SMCSWeapon_DECOY, + SMCSWeapon_INCGRENADE, SMCSWeapon_C4, SMCSWeapon_KEVLAR, SMCSWeapon_ASSAULTSUIT, + SMCSWeapon_HEAVYASSAULTSUIT, SMCSWeapon_NONE, SMCSWeapon_NIGHTVISION, SMCSWeapon_DEFUSER + }; + + if (iDefIdx >= SMCSWeapon_MAXWEAPONIDS) + return (SMCSWeapon)iDefIdx; + else + return weaponIDMap[iDefIdx]; +} + +ItemDefHashValue *GetHashValueFromWeapon(const char *szWeapon) +{ + char tempWeapon[MAX_WEAPON_NAME_LENGTH]; + + Q_strncpy(tempWeapon, szWeapon, sizeof(tempWeapon)); + Q_strlower(tempWeapon); + + if (strstr(tempWeapon, "weapon_") == NULL && strstr(tempWeapon, "item_") == NULL) + { + static const char *szClassPrefixs[] = { "weapon_", "item_" }; + + for (unsigned int i = 0; i < SM_ARRAYSIZE(szClassPrefixs); i++) + { + char classname[MAX_WEAPON_NAME_LENGTH]; + Q_snprintf(classname, sizeof(classname), "%s%s", szClassPrefixs[i], tempWeapon); + + ClassnameMap::Result res = g_mapClassToDefIdx.find(classname); + + if (res.found()) + return &res->value; + } + + return NULL; + } + + ClassnameMap::Result res = g_mapClassToDefIdx.find(tempWeapon); + + if (res.found()) + return &res->value; + + return NULL; +} #endif #if SOURCE_ENGINE != SE_CSGO @@ -273,6 +418,20 @@ void *GetWeaponInfo(int weaponID) } #endif +const char *GetWeaponNameFromClassname(const char *weapon) +{ + char *szTemp = strstr((char *)weapon, "_"); + + if (!szTemp) + { + return weapon; + } + else + { + return (const char *)((intptr_t)szTemp + 1); + } +} + const char *GetTranslatedWeaponAlias(const char *weapon) { #if SOURCE_ENGINE != SE_CSGO @@ -297,12 +456,11 @@ const char *GetTranslatedWeaponAlias(const char *weapon) unsigned char vstk[sizeof(const char *)]; unsigned char *vptr = vstk; - *(const char **)vptr = weapon; + *(const char **)vptr = GetWeaponNameFromClassname(weapon); pWrapper->Execute(vstk, &alias); return alias; #else //this should work for both games maybe replace both? - static const char *szAliases[] = { "cv47", "ak47", @@ -320,14 +478,14 @@ const char *GetTranslatedWeaponAlias(const char *weapon) "vesthelm", "assaultsuit", "nvgs", "nightvision" }; - - for (int i = 0; i < SM_ARRAYSIZE(szAliases)/2; i++) + + for (int i = 0; i < SM_ARRAYSIZE(szAliases) / 2; i++) { - if (stricmp(weapon, szAliases[i * 2]) == 0) + if (Q_stristr(GetWeaponNameFromClassname(weapon), szAliases[i * 2]) != 0) return szAliases[i * 2 + 1]; } - return weapon; + return GetWeaponNameFromClassname(weapon); #endif } @@ -355,17 +513,17 @@ int AliasToWeaponID(const char *weapon) unsigned char vstk[sizeof(const char *)]; unsigned char *vptr = vstk; - *(const char **)vptr = weapon; + *(const char **)vptr = GetWeaponNameFromClassname(weapon); pWrapper->Execute(vstk, &weaponID); return weaponID; -#else //this is horribly broken hackfix for now. - for (unsigned int i = 0; i < SM_ARRAYSIZE(szWeaponInfo); i++) - { - if (stricmp(weapon, szWeaponInfo[i]) == 0) - return i; - } +#else + ItemDefHashValue *pHashValue = GetHashValueFromWeapon(weapon); + + if (pHashValue) + return pHashValue->m_iWeaponID; + return 0; #endif } @@ -390,283 +548,33 @@ const char *WeaponIDToAlias(int weaponID) retpass.size = sizeof(const char *); \ pWrapper = g_pBinTools->CreateCall(addr, CallConv_Cdecl, &retpass, pass, 1)) } - + unsigned char vstk[sizeof(int)]; unsigned char *vptr = vstk; - *(int *)vptr = GetRealWeaponID(weaponID); + *(int *)vptr = weaponID; pWrapper->Execute(vstk, &alias); return alias; #else - int realID = GetRealWeaponID(weaponID); + WeaponIDMap::Result res = g_mapWeaponIDToDefIdx.find((SMCSWeapon)weaponID); - if (realID < SM_ARRAYSIZE(szWeaponInfo) && realID > 0) - return szWeaponInfo[realID]; + if (res.found()) + return res->value.m_szItemName; return NULL; - #endif - } -int GetRealWeaponID(int weaponId) -{ -#if SOURCE_ENGINE == SE_CSGO - switch((SMCSWeapon)weaponId) - { - case SMCSWeapon_NONE: - return (int)CSGOWeapon_NONE; - case SMCSWeapon_DEAGLE: - return (int)CSGOWeapon_DEAGLE; - case SMCSWeapon_ELITE: - return (int)CSGOWeapon_ELITE; - case SMCSWeapon_FIVESEVEN: - return (int)CSGOWeapon_FIVESEVEN; - case SMCSWeapon_GLOCK: - return (int)CSGOWeapon_GLOCK; - case SMCSWeapon_P228: - return (int)CSGOWeapon_P228; - case SMCSWeapon_USP: - return (int)CSGOWeapon_USP; - case SMCSWeapon_AK47: - return (int)CSGOWeapon_AK47; - case SMCSWeapon_AUG: - return (int)CSGOWeapon_AUG; - case SMCSWeapon_AWP: - return (int)CSGOWeapon_AWP; - case SMCSWeapon_FAMAS: - return (int)CSGOWeapon_FAMAS; - case SMCSWeapon_G3SG1: - return (int)CSGOWeapon_G3SG1; - case SMCSWeapon_GALIL: - return (int)CSGOWeapon_GALIL; - case SMCSWeapon_GALILAR: - return (int)CSGOWeapon_GALILAR; - case SMCSWeapon_M249: - return (int)CSGOWeapon_M249; - case SMCSWeapon_M3: - return (int)CSGOWeapon_M3; - case SMCSWeapon_M4A1: - return (int)CSGOWeapon_M4A1; - case SMCSWeapon_MAC10: - return (int)CSGOWeapon_MAC10; - case SMCSWeapon_MP5NAVY: - return (int)CSGOWeapon_MP5NAVY; - case SMCSWeapon_P90: - return (int)CSGOWeapon_P90; - case SMCSWeapon_SCOUT: - return (int)CSGOWeapon_SCOUT; - case SMCSWeapon_SG550: - return (int)CSGOWeapon_SG550; - case SMCSWeapon_SG552: - return (int)CSGOWeapon_SG552; - case SMCSWeapon_TMP: - return (int)CSGOWeapon_TMP; - case SMCSWeapon_UMP45: - return (int)CSGOWeapon_UMP45; - case SMCSWeapon_XM1014: - return (int)CSGOWeapon_XM1014; - case SMCSWeapon_BIZON: - return (int)CSGOWeapon_BIZON; - case SMCSWeapon_MAG7: - return (int)CSGOWeapon_MAG7; - case SMCSWeapon_NEGEV: - return (int)CSGOWeapon_NEGEV; - case SMCSWeapon_SAWEDOFF: - return (int)CSGOWeapon_SAWEDOFF; - case SMCSWeapon_TEC9: - return (int)CSGOWeapon_TEC9; - case SMCSWeapon_TASER: - return (int)CSGOWeapon_TASER; - case SMCSWeapon_HKP2000: - return (int)CSGOWeapon_HKP2000; - case SMCSWeapon_MP7: - return (int)CSGOWeapon_MP7; - case SMCSWeapon_MP9: - return (int)CSGOWeapon_MP9; - case SMCSWeapon_NOVA: - return (int)CSGOWeapon_NOVA; - case SMCSWeapon_P250: - return (int)CSGOWeapon_P250; - case SMCSWeapon_SCAR17: - return (int)CSGOWeapon_SCAR17; - case SMCSWeapon_SCAR20: - return (int)CSGOWeapon_SCAR20; - case SMCSWeapon_SG556: - return (int)CSGOWeapon_SG556; - case SMCSWeapon_SSG08: - return (int)CSGOWeapon_SSG08; - case SMCSWeapon_KNIFE_GG: - return (int)CSGOWeapon_KNIFE_GG; - case SMCSWeapon_KNIFE: - return (int)CSGOWeapon_KNIFE; - case SMCSWeapon_FLASHBANG: - return (int)CSGOWeapon_FLASHBANG; - case SMCSWeapon_HEGRENADE: - return (int)CSGOWeapon_HEGRENADE; - case SMCSWeapon_SMOKEGRENADE: - return (int)CSGOWeapon_SMOKEGRENADE; - case SMCSWeapon_MOLOTOV: - return (int)CSGOWeapon_MOLOTOV; - case SMCSWeapon_DECOY: - return (int)CSGOWeapon_DECOY; - case SMCSWeapon_INCGRENADE: - return (int)CSGOWeapon_INCGRENADE; - case SMCSWeapon_C4: - return (int)CSGOWeapon_C4; - case SMCSWeapon_KEVLAR: - return (int)CSGOWeapon_KEVLAR; - case SMCSWeapon_ASSAULTSUIT: - return (int)CSGOWeapon_ASSAULTSUIT; - case SMCSWeapon_NIGHTVISION: - return (int)CSGOWeapon_NVG; - case SMCSWeapon_DEFUSER: - return (int)CSGOWeapon_DEFUSER; - default: - return (int)CSGOWeapon_NONE; - } -#else - if (weaponId > (int)SMCSWeapon_NIGHTVISION || weaponId < (int)SMCSWeapon_NONE) - return (int)SMCSWeapon_NONE; - else - return weaponId; -#endif -} - -int GetFakeWeaponID(int weaponId) -{ -#if SOURCE_ENGINE == SE_CSGO - switch((CSGOWeapon)weaponId) - { - case CSGOWeapon_NONE: - return (int)SMCSWeapon_NONE; - case CSGOWeapon_DEAGLE: - return (int)SMCSWeapon_DEAGLE; - case CSGOWeapon_ELITE: - return (int)SMCSWeapon_ELITE; - case CSGOWeapon_FIVESEVEN: - return (int)SMCSWeapon_FIVESEVEN; - case CSGOWeapon_GLOCK: - return (int)SMCSWeapon_GLOCK; - case CSGOWeapon_P228: - return (int)SMCSWeapon_P228; - case CSGOWeapon_USP: - return (int)SMCSWeapon_USP; - case CSGOWeapon_AK47: - return (int)SMCSWeapon_AK47; - case CSGOWeapon_AUG: - return (int)SMCSWeapon_AUG; - case CSGOWeapon_AWP: - return (int)SMCSWeapon_AWP; - case CSGOWeapon_FAMAS: - return (int)SMCSWeapon_FAMAS; - case CSGOWeapon_G3SG1: - return (int)SMCSWeapon_G3SG1; - case CSGOWeapon_GALIL: - return (int)SMCSWeapon_GALIL; - case CSGOWeapon_GALILAR: - return (int)SMCSWeapon_GALILAR; - case CSGOWeapon_M249: - return (int)SMCSWeapon_M249; - case CSGOWeapon_M3: - return (int)SMCSWeapon_M3; - case CSGOWeapon_M4A1: - return (int)SMCSWeapon_M4A1; - case CSGOWeapon_MAC10: - return (int)SMCSWeapon_MAC10; - case CSGOWeapon_MP5NAVY: - return (int)SMCSWeapon_MP5NAVY; - case CSGOWeapon_P90: - return (int)SMCSWeapon_P90; - case CSGOWeapon_SCOUT: - return (int)SMCSWeapon_SCOUT; - case CSGOWeapon_SG550: - return (int)SMCSWeapon_SG550; - case CSGOWeapon_SG552: - return (int)SMCSWeapon_SG552; - case CSGOWeapon_TMP: - return (int)SMCSWeapon_TMP; - case CSGOWeapon_UMP45: - return (int)SMCSWeapon_UMP45; - case CSGOWeapon_XM1014: - return (int)SMCSWeapon_XM1014; - case CSGOWeapon_BIZON: - return (int)SMCSWeapon_BIZON; - case CSGOWeapon_MAG7: - return (int)SMCSWeapon_MAG7; - case CSGOWeapon_NEGEV: - return (int)SMCSWeapon_NEGEV; - case CSGOWeapon_SAWEDOFF: - return (int)SMCSWeapon_SAWEDOFF; - case CSGOWeapon_TEC9: - return (int)SMCSWeapon_TEC9; - case CSGOWeapon_TASER: - return (int)SMCSWeapon_TASER; - case CSGOWeapon_HKP2000: - return (int)SMCSWeapon_HKP2000; - case CSGOWeapon_MP7: - return (int)SMCSWeapon_MP7; - case CSGOWeapon_MP9: - return (int)SMCSWeapon_MP9; - case CSGOWeapon_NOVA: - return (int)SMCSWeapon_NOVA; - case CSGOWeapon_P250: - return (int)SMCSWeapon_P250; - case CSGOWeapon_SCAR17: - return (int)SMCSWeapon_SCAR17; - case CSGOWeapon_SCAR20: - return (int)SMCSWeapon_SCAR20; - case CSGOWeapon_SG556: - return (int)SMCSWeapon_SG556; - case CSGOWeapon_SSG08: - return (int)SMCSWeapon_SSG08; - case CSGOWeapon_KNIFE_GG: - return (int)SMCSWeapon_KNIFE_GG; - case CSGOWeapon_KNIFE: - return (int)SMCSWeapon_KNIFE; - case CSGOWeapon_FLASHBANG: - return (int)SMCSWeapon_FLASHBANG; - case CSGOWeapon_HEGRENADE: - return (int)SMCSWeapon_HEGRENADE; - case CSGOWeapon_SMOKEGRENADE: - return (int)SMCSWeapon_SMOKEGRENADE; - case CSGOWeapon_MOLOTOV: - return (int)SMCSWeapon_MOLOTOV; - case CSGOWeapon_DECOY: - return (int)SMCSWeapon_DECOY; - case CSGOWeapon_INCGRENADE: - return (int)SMCSWeapon_INCGRENADE; - case CSGOWeapon_C4: - return (int)SMCSWeapon_C4; - case CSGOWeapon_KEVLAR: - return (int)SMCSWeapon_KEVLAR; - case CSGOWeapon_ASSAULTSUIT: - return (int)SMCSWeapon_ASSAULTSUIT; - case CSGOWeapon_NVG: - return (int)SMCSWeapon_NIGHTVISION; - case CSGOWeapon_DEFUSER: - return (int)SMCSWeapon_DEFUSER; - default: - return (int)SMCSWeapon_NONE; - } -#else - if (weaponId > (int)SMCSWeapon_NIGHTVISION || weaponId < (int)SMCSWeapon_NONE) - return (int)SMCSWeapon_NONE; - else - return weaponId; -#endif -} bool IsValidWeaponID(int id) { if (id <= (int)SMCSWeapon_NONE) 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_DEFUSER || 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; + WeaponIDMap::Result res = g_mapWeaponIDToDefIdx.find((SMCSWeapon)id); + if (!res.found()) + return false; #else else if (id > (int)SMCSWeapon_NIGHTVISION) return false; diff --git a/extensions/cstrike/util_cstrike.h b/extensions/cstrike/util_cstrike.h index cb98e861..bdd4581d 100644 --- a/extensions/cstrike/util_cstrike.h +++ b/extensions/cstrike/util_cstrike.h @@ -31,158 +31,9 @@ #ifndef _INCLUDE_CSTRIKE_UTIL_H_ #define _INCLUDE_CSTRIKE_UTIL_H_ - -#if SOURCE_ENGINE == SE_CSGO -#include "extension.h" - -class CEconItemView; -class CCSWeaponData; -class CEconItemSchema; - -class CEconItemDefinition -{ -public: - void **m_pVtable; - KeyValues *m_pKv; - uint16_t m_iDefinitionIndex; - int GetDefaultLoadoutSlot() - { - static int iLoadoutSlotOffset = -1; - - if (iLoadoutSlotOffset == -1) - { - if (!g_pGameConf->GetOffset("LoadoutSlotOffset", &iLoadoutSlotOffset) || iLoadoutSlotOffset == -1) - { - iLoadoutSlotOffset = -1; - return -1; - } - } - - return *(int *)((intptr_t)this + iLoadoutSlotOffset); - } -}; - -CEconItemView *GetEconItemView(CBaseEntity *pEntity, int iSlot); -CCSWeaponData *GetCCSWeaponData(CEconItemView *view); -CEconItemSchema *GetItemSchema(); -CEconItemDefinition *GetItemDefintionByName(const char *classname); - -static const char *szWeaponInfo[] = -{ - "none", - "deagle", - "elite", - "fiveseven", - "glock", - "p228", - "usp", - "ak47", - "aug", - "awp", - "famas", - "g3sg1", - "galil", - "galilar", - "m249", - "m3", - "m4a1", - "mac10", - "mp5", - "p90", - "scout", - "sg550", - "sg552", - "tmp", - "ump45", - "xm1014", - "bizon", - "mag7", - "negev", - "sawedoff", - "tec9", - "taser", - "hkp2000", - "mp7", - "mp9", - "nova", - "p250", - "scar17", - "scar20", - "sg556", - "ssg08", - "knifegg", - "knife", - "flashbang", - "hegrenade", - "smokegrenade", - "molotov", - "decoy", - "incgrenade", - "c4", - "kevlar", - "assaultsuit", - "nvg", - "defuser" -}; - -enum CSGOWeapon -{ - CSGOWeapon_NONE, - CSGOWeapon_DEAGLE, - CSGOWeapon_ELITE, - CSGOWeapon_FIVESEVEN, - CSGOWeapon_GLOCK, - CSGOWeapon_P228, - CSGOWeapon_USP, - CSGOWeapon_AK47, - CSGOWeapon_AUG, - CSGOWeapon_AWP, - CSGOWeapon_FAMAS, - CSGOWeapon_G3SG1, - CSGOWeapon_GALIL, - CSGOWeapon_GALILAR, - CSGOWeapon_M249, - CSGOWeapon_M3, - CSGOWeapon_M4A1, - CSGOWeapon_MAC10, - CSGOWeapon_MP5NAVY, - CSGOWeapon_P90, - CSGOWeapon_SCOUT, - CSGOWeapon_SG550, - CSGOWeapon_SG552, - CSGOWeapon_TMP, - CSGOWeapon_UMP45, - CSGOWeapon_XM1014, - CSGOWeapon_BIZON, - CSGOWeapon_MAG7, - CSGOWeapon_NEGEV, - CSGOWeapon_SAWEDOFF, - CSGOWeapon_TEC9, - CSGOWeapon_TASER, - CSGOWeapon_HKP2000, - CSGOWeapon_MP7, - CSGOWeapon_MP9, - CSGOWeapon_NOVA, - CSGOWeapon_P250, - CSGOWeapon_SCAR17, - CSGOWeapon_SCAR20, - CSGOWeapon_SG556, - CSGOWeapon_SSG08, - CSGOWeapon_KNIFE_GG, - CSGOWeapon_KNIFE, - CSGOWeapon_FLASHBANG, - CSGOWeapon_HEGRENADE, - CSGOWeapon_SMOKEGRENADE, - CSGOWeapon_MOLOTOV, - CSGOWeapon_DECOY, - CSGOWeapon_INCGRENADE, - CSGOWeapon_C4, //49 - CSGOWeapon_KEVLAR = 50, - CSGOWeapon_ASSAULTSUIT, - CSGOWeapon_NVG, - CSGOWeapon_DEFUSER -}; -#endif + //THIS IS THE INCLUDE ENUM DO NOT CHANGE ONLY UPDATE THE INCLUDE + //This is used to match to old weaponid's to their correct enum value + //Anything after defuser will pass the itemdef as they will be the id set in include enum SMCSWeapon { SMCSWeapon_NONE = 0, @@ -239,22 +90,117 @@ enum SMCSWeapon SMCSWeapon_MOLOTOV, SMCSWeapon_DECOY, SMCSWeapon_INCGRENADE, - SMCSWeapon_DEFUSER + SMCSWeapon_DEFUSER, + SMCSWeapon_HEAVYASSAULTSUIT, + SMCSWeapon_MAXWEAPONIDS, //This only exists here... the include has more. This is for easy array construction }; -#if SOURCE_ENGINE != SE_CSGO +#if SOURCE_ENGINE == SE_CSGO +//These are the ItemDefintion indexs they are used as a reference to create GetWeaponIdFromDefIdx +/* +enum CSGOItemDefs +{ + CSGOItemDef_NONE = 0, + CSGOItemDef_DEAGLE, + CSGOItemDef_ELITE, + CSGOItemDef_FIVESEVEN, + CSGOItemDef_GLOCK, + CSGOItemDef_P228, + CSGOItemDef_USP, + CSGOItemDef_AK47, + CSGOItemDef_AUG, + CSGOItemDef_AWP, + CSGOItemDef_FAMAS, + CSGOItemDef_G3SG1, + CSGOItemDef_GALIL, + CSGOItemDef_GALILAR, + CSGOItemDef_M249, + CSGOItemDef_M3, + CSGOItemDef_M4A1, + CSGOItemDef_MAC10, + CSGOItemDef_MP5NAVY, + CSGOItemDef_P90, + CSGOItemDef_SCOUT, + CSGOItemDef_SG550, + CSGOItemDef_SG552, + CSGOItemDef_TMP, + CSGOItemDef_UMP45, + CSGOItemDef_XM1014, + CSGOItemDef_BIZON, + CSGOItemDef_MAG7, + CSGOItemDef_NEGEV, + CSGOItemDef_SAWEDOFF, + CSGOItemDef_TEC9, + CSGOItemDef_TASER, + CSGOItemDef_HKP2000, + CSGOItemDef_MP7, + CSGOItemDef_MP9, + CSGOItemDef_NOVA, + CSGOItemDef_P250, + CSGOItemDef_SCAR17, + CSGOItemDef_SCAR20, + CSGOItemDef_SG556, + CSGOItemDef_SSG08, + CSGOItemDef_KNIFE_GG, + CSGOItemDef_KNIFE, + CSGOItemDef_FLASHBANG, + CSGOItemDef_HEGRENADE, + CSGOItemDef_SMOKEGRENADE, + CSGOItemDef_MOLOTOV, + CSGOItemDef_DECOY, + CSGOItemDef_INCGRENADE, + CSGOItemDef_C4, + CSGOItemDef_KEVLAR, + CSGOItemDef_ASSAULTSUIT, + CSGOItemDef_HEAVYASSAULTSUIT, + CSGOItemDef_UNUSED, + CSGOItemDef_NVG, + CSGOItemDef_DEFUSER, + CSGOItemDef_MAXDEFS, +}; +*/ +struct ItemDefHashValue; +class CEconItemView; +class CCSWeaponData; +class CEconItemSchema; +class CEconItemDefinition +{ +public: + void **m_pVtable; + KeyValues *m_pKv; + uint16_t m_iDefinitionIndex; + int GetDefaultLoadoutSlot() + { + static int iLoadoutSlotOffset = -1; + + if (iLoadoutSlotOffset == -1) + { + if (!g_pGameConf->GetOffset("LoadoutSlotOffset", &iLoadoutSlotOffset) || iLoadoutSlotOffset == -1) + { + iLoadoutSlotOffset = -1; + return -1; + } + } + + return *(int *)((intptr_t)this + iLoadoutSlotOffset); + } +}; + +CEconItemView *GetEconItemView(CBaseEntity *pEntity, int iSlot); +CCSWeaponData *GetCCSWeaponData(CEconItemView *view); +CEconItemSchema *GetItemSchema(); +CEconItemDefinition *GetItemDefintionByName(const char *classname); +void CreateHashMaps(); +void ClearHashMaps(); +SMCSWeapon GetWeaponIdFromDefIdx(uint16_t iDefIdx); +ItemDefHashValue *GetHashValueFromWeapon(const char *szWeapon); +#else //CS:S ONLY STUFF void *GetWeaponInfo(int weaponID); #endif +const char *GetWeaponNameFromClassname(const char *weapon); const char *GetTranslatedWeaponAlias(const char *weapon); - int AliasToWeaponID(const char *weapon); - const char *WeaponIDToAlias(int weaponID); - -int GetRealWeaponID(int weaponId); - -int GetFakeWeaponID(int weaponId); - bool IsValidWeaponID(int weaponId); #endif diff --git a/gamedata/sm-cstrike.games/game.csgo.txt b/gamedata/sm-cstrike.games/game.csgo.txt index 26107bd2..b060bdcb 100644 --- a/gamedata/sm-cstrike.games/game.csgo.txt +++ b/gamedata/sm-cstrike.games/game.csgo.txt @@ -82,6 +82,14 @@ "linux" "588" "mac" "588" } + // Offset into CEconItemSchema * to a hashmap of all itemdefs + // Offset can be found in... GetItemDefinitionMutable (easy to get offset on windows should be the same) + "ItemDefHashOffset" + { + "windows" "172" + "linux" "172" + "mac" "172" + } } "Signatures" { diff --git a/plugins/include/cstrike.inc b/plugins/include/cstrike.inc index a149833a..a3704207 100644 --- a/plugins/include/cstrike.inc +++ b/plugins/include/cstrike.inc @@ -109,7 +109,7 @@ enum CSWeaponID CSWeapon_SHIELD, CSWeapon_KEVLAR, CSWeapon_ASSAULTSUIT, - CSWeapon_NIGHTVISION, + CSWeapon_NIGHTVISION, //Anything below is CS:GO ONLY CSWeapon_GALILAR, CSWeapon_BIZON, CSWeapon_MAG7, @@ -130,7 +130,27 @@ enum CSWeaponID CSWeapon_MOLOTOV, CSWeapon_DECOY, CSWeapon_INCGRENADE, - CSWeapon_DEFUSER + CSWeapon_DEFUSER, + CSWeapon_HEAVYASSAULTSUIT, + //The rest are actual item definition indexes for CS:GO + CSWeapon_CUTTERS = 56, + CSWeapon_HEALTHSHOT = 57, + CSWeapon_KNIFE_T = 59, + CSWeapon_M4A1_SILENCER = 60. + CSWeapon_USP_SILENCER = 61, + CSWeapon_CZ75A = 63, + CSWeapon_REVOLVER = 64, + CSWeapon_TAGGRENADE = 68. + CSWeapon_BAYONET = 500, + CSWeapon_KNIFE_FLIP = 505, + CSWeapon_KNIFE_GUT = 506, + CSWeapon_KNIFE_KARAMBIT = 507, + CSWeapon_KNIFE_M9_BAYONET = 508, + CSWeapon_KNIFE_TATICAL = 509, + CSWeapon_KNIFE_FALCHION = 512, + CSWeapon_KNIFE_SURVIVAL_BOWIE = 514, + CSWeapon_KNIFE_BUTTERFLY = 515, + CSWeapon_KNIFE_PUSH = 516, }; /** From b68405b6513721f0f780eab33c48edfae84da1bd Mon Sep 17 00:00:00 2001 From: Ruben Gonzalez Date: Sun, 3 Sep 2017 19:30:06 -0400 Subject: [PATCH 02/11] Fix cstrike include file. --- plugins/include/cstrike.inc | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/plugins/include/cstrike.inc b/plugins/include/cstrike.inc index a3704207..f2b40dad 100644 --- a/plugins/include/cstrike.inc +++ b/plugins/include/cstrike.inc @@ -136,11 +136,11 @@ enum CSWeaponID CSWeapon_CUTTERS = 56, CSWeapon_HEALTHSHOT = 57, CSWeapon_KNIFE_T = 59, - CSWeapon_M4A1_SILENCER = 60. + CSWeapon_M4A1_SILENCER = 60, CSWeapon_USP_SILENCER = 61, CSWeapon_CZ75A = 63, CSWeapon_REVOLVER = 64, - CSWeapon_TAGGRENADE = 68. + CSWeapon_TAGGRENADE = 68, CSWeapon_BAYONET = 500, CSWeapon_KNIFE_FLIP = 505, CSWeapon_KNIFE_GUT = 506, @@ -151,6 +151,7 @@ enum CSWeaponID CSWeapon_KNIFE_SURVIVAL_BOWIE = 514, CSWeapon_KNIFE_BUTTERFLY = 515, CSWeapon_KNIFE_PUSH = 516, + CSWeapon_MAX_WEAPONS //THIS MUST BE LAST, EASY WAY TO CREATE LOOPS. When looping, do CS_IsValidWeaponID(i), to check. }; /** From d8f0484327a9194225d99058b65a0a680d1bea84 Mon Sep 17 00:00:00 2001 From: Ruben Gonzalez Date: Sun, 3 Sep 2017 19:30:35 -0400 Subject: [PATCH 03/11] Make IsValidWeaponID check weapon info on CS:S --- extensions/cstrike/util_cstrike.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/extensions/cstrike/util_cstrike.cpp b/extensions/cstrike/util_cstrike.cpp index 414f7e71..bcab0d80 100644 --- a/extensions/cstrike/util_cstrike.cpp +++ b/extensions/cstrike/util_cstrike.cpp @@ -576,7 +576,7 @@ bool IsValidWeaponID(int id) if (!res.found()) return false; #else - else if (id > (int)SMCSWeapon_NIGHTVISION) + else if (id > SMCSWeapon_NIGHTVISION || !GetWeaponInfo(id)) return false; #endif return true; From a341a0e85cb6d838bb7cd197e56c0cf88711579d Mon Sep 17 00:00:00 2001 From: Ruben Gonzalez Date: Sun, 3 Sep 2017 19:31:23 -0400 Subject: [PATCH 04/11] Use implemented function to get weapon name. --- extensions/cstrike/forwards.cpp | 10 +--------- 1 file changed, 1 insertion(+), 9 deletions(-) diff --git a/extensions/cstrike/forwards.cpp b/extensions/cstrike/forwards.cpp index 129aafe4..261807a2 100644 --- a/extensions/cstrike/forwards.cpp +++ b/extensions/cstrike/forwards.cpp @@ -53,15 +53,7 @@ DETOUR_DECL_MEMBER3(DetourHandleBuy, int, int, iLoadoutSlot, void *, pWpnDataRef } else { - const char *underscore = strstr(szClassname, "_"); - if (underscore) - { - Q_strncpy(weaponName, (const char *)((intptr_t)underscore + 1), sizeof(weaponName)); - } - else - { - Q_strncpy(weaponName, szClassname, sizeof(weaponName)); - } + Q_strncpy(weaponName, GetWeaponNameFromClassname(szClassname), sizeof(weaponName)); } cell_t result = Pl_Continue; From 1f3b14367d8ce6618bdc375b5b7ad6460c9f4dde Mon Sep 17 00:00:00 2001 From: Ruben Gonzalez Date: Sat, 9 Sep 2017 23:27:32 -0400 Subject: [PATCH 05/11] Add new native to get enum value based on item definition index. --- extensions/cstrike/natives.cpp | 16 ++++++++++++++++ plugins/include/cstrike.inc | 12 ++++++++++++ 2 files changed, 28 insertions(+) diff --git a/extensions/cstrike/natives.cpp b/extensions/cstrike/natives.cpp index bc5e5e51..5d4bbfcb 100644 --- a/extensions/cstrike/natives.cpp +++ b/extensions/cstrike/natives.cpp @@ -862,6 +862,21 @@ static cell_t CS_UpdateClientModel(IPluginContext *pContext, const cell_t *param return 1; } + +static cell_t CS_ItemDefIndexToID(IPluginContext *pContext, const cell_t *params) +{ +#if SOURCE_ENGINE == SE_CSGO + ItemIndexMap::Result res = g_mapDefIdxToClass.find((uint16_t)params[1]); + + if (!res.found()) + return pContext->ThrowNativeError("Invalid item definition passed."); + + return res->value.m_iWeaponID; +#else + return pContext->ThrowNativeError("CS_ItemDefIndexToID is not supported on this game"); +#endif +} + sp_nativeinfo_t g_CSNatives[] = { {"CS_RespawnPlayer", CS_RespawnPlayer}, @@ -884,6 +899,7 @@ sp_nativeinfo_t g_CSNatives[] = {"CS_SetClientAssists", CS_SetClientAssists}, {"CS_UpdateClientModel", CS_UpdateClientModel}, {"CS_IsValidWeaponID", CS_IsValidWeaponID}, + {"CS_ItemDefIndexToID", CS_ItemDefIndexToID }, {NULL, NULL} }; diff --git a/plugins/include/cstrike.inc b/plugins/include/cstrike.inc index f2b40dad..a80103b5 100644 --- a/plugins/include/cstrike.inc +++ b/plugins/include/cstrike.inc @@ -391,6 +391,17 @@ native bool CS_IsValidWeaponID(CSWeaponID id); */ native void CS_UpdateClientModel(int client); +/** + * Returns a CSWeaponID equivalent based on the item definition index. + * + * @param ItemDefinitionIndex Definition index to get the CSWeaponID value for. + * @return Returns CSWeaponID value for the definition index. + * + * @error Invalid definition index. + * @note In most cases the id will be the item definition index. + */ +native CSWeaponID CS_ItemDefIndexToID(int ItemDefinitionIndex); + /** * Do not edit below this line! */ @@ -429,5 +440,6 @@ public void __ext_cstrike_SetNTVOptional() MarkNativeAsOptional("CS_WeaponIDToAlias"); MarkNativeAsOptional("CS_IsValidWeaponID"); MarkNativeAsOptional("CS_UpdateClientModel"); + MarkNativeAsOptional("CS_ItemDefIndexToID"); } #endif From 8a36d0486b842575fe9b4443d840ce9f46cbd8fc Mon Sep 17 00:00:00 2001 From: Ruben Gonzalez Date: Sun, 10 Sep 2017 10:16:23 -0400 Subject: [PATCH 06/11] Add enum value for max regular items. --- plugins/include/cstrike.inc | 1 + 1 file changed, 1 insertion(+) diff --git a/plugins/include/cstrike.inc b/plugins/include/cstrike.inc index a80103b5..e7d84152 100644 --- a/plugins/include/cstrike.inc +++ b/plugins/include/cstrike.inc @@ -141,6 +141,7 @@ enum CSWeaponID CSWeapon_CZ75A = 63, CSWeapon_REVOLVER = 64, CSWeapon_TAGGRENADE = 68, + CSWeapon_MAX_WEAPONS_NO_KNIFES, // Max without the knife item defs, usefull when treating all knives as a regular knife. CSWeapon_BAYONET = 500, CSWeapon_KNIFE_FLIP = 505, CSWeapon_KNIFE_GUT = 506, From 8c8af88cee70e4b88fbf67a79e879ab156ac0688 Mon Sep 17 00:00:00 2001 From: Ruben Gonzalez Date: Tue, 19 Sep 2017 12:37:11 -0400 Subject: [PATCH 07/11] Add additional native, fix include file. --- extensions/cstrike/natives.cpp | 17 ++++++++++++++++- plugins/include/cstrike.inc | 22 +++++++++++++++++----- 2 files changed, 33 insertions(+), 6 deletions(-) diff --git a/extensions/cstrike/natives.cpp b/extensions/cstrike/natives.cpp index 5d4bbfcb..76cd66ee 100644 --- a/extensions/cstrike/natives.cpp +++ b/extensions/cstrike/natives.cpp @@ -877,6 +877,20 @@ static cell_t CS_ItemDefIndexToID(IPluginContext *pContext, const cell_t *params #endif } +static cell_t CS_WeaponIDToItemDefIndex(IPluginContext *pContext, const cell_t *params) +{ +#if SOURCE_ENGINE == SE_CSGO + WeaponIDMap::Result res = g_mapWeaponIDToDefIdx.find((uint16_t)params[1]); + + if (!res.found()) + return pContext->ThrowNativeError("Invalid weapon id passed."); + + return res->value.m_iDefIdx; +#else + return pContext->ThrowNativeError("CS_WeaponIDToItemDefIndex is not supported on this game"); +#endif +} + sp_nativeinfo_t g_CSNatives[] = { {"CS_RespawnPlayer", CS_RespawnPlayer}, @@ -899,7 +913,8 @@ sp_nativeinfo_t g_CSNatives[] = {"CS_SetClientAssists", CS_SetClientAssists}, {"CS_UpdateClientModel", CS_UpdateClientModel}, {"CS_IsValidWeaponID", CS_IsValidWeaponID}, - {"CS_ItemDefIndexToID", CS_ItemDefIndexToID }, + {"CS_ItemDefIndexToID", CS_ItemDefIndexToID}, + {"CS_WeaponIDToItemDefIndex", CS_WeaponIDToItemDefIndex}, {NULL, NULL} }; diff --git a/plugins/include/cstrike.inc b/plugins/include/cstrike.inc index e7d84152..49e0aec0 100644 --- a/plugins/include/cstrike.inc +++ b/plugins/include/cstrike.inc @@ -141,7 +141,7 @@ enum CSWeaponID CSWeapon_CZ75A = 63, CSWeapon_REVOLVER = 64, CSWeapon_TAGGRENADE = 68, - CSWeapon_MAX_WEAPONS_NO_KNIFES, // Max without the knife item defs, usefull when treating all knives as a regular knife. + CSWeapon_MAX_WEAPONS_NO_KNIFES, // Max without the knife item defs, useful when treating all knives as a regular knife. CSWeapon_BAYONET = 500, CSWeapon_KNIFE_FLIP = 505, CSWeapon_KNIFE_GUT = 506, @@ -380,7 +380,7 @@ native int CS_WeaponIDToAlias(CSWeaponID weaponID, char[] destination, int len); * @param weaponID WeaponID to check * @return Returns true if its a valid WeaponID false otherwise. * - * @note This will return false always for CSWeapon_NONE + * @note This will return false always for CSWeapon_NONE. Should only be called after OnMapStart since weapon info isnt intialized before. */ native bool CS_IsValidWeaponID(CSWeaponID id); @@ -395,13 +395,24 @@ native void CS_UpdateClientModel(int client); /** * Returns a CSWeaponID equivalent based on the item definition index. * - * @param ItemDefinitionIndex Definition index to get the CSWeaponID value for. + * @param iDefIndex Definition index to get the CSWeaponID value for. * @return Returns CSWeaponID value for the definition index. * * @error Invalid definition index. - * @note In most cases the id will be the item definition index. + * @note In most cases the id will be the item definition index. Works for CS:GO ONLY. */ -native CSWeaponID CS_ItemDefIndexToID(int ItemDefinitionIndex); +native CSWeaponID CS_ItemDefIndexToID(int iDefIndex); + +/** + * Returns a item definition index equivalent based on the CSWeaponID. + * + * @param id CSWeaponID to get the item definition for. + * @return Returns item definition index value for the weapon id. + * + * @error Invalid weapon id. + * @note In most cases the item deinition index will be the id. Works for CS:GO ONLY. + */ +native int CS_WeaponIDToItemDefIndex(CSWeaponID id); /** * Do not edit below this line! @@ -442,5 +453,6 @@ public void __ext_cstrike_SetNTVOptional() MarkNativeAsOptional("CS_IsValidWeaponID"); MarkNativeAsOptional("CS_UpdateClientModel"); MarkNativeAsOptional("CS_ItemDefIndexToID"); + MarkNativeAsOptional("CS_WeaponIDToItemDefIndex"); } #endif From 588e88c7ee657fab36f5db2af6fe654cf2b478e4 Mon Sep 17 00:00:00 2001 From: Ruben Gonzalez Date: Wed, 4 Oct 2017 14:42:07 -0400 Subject: [PATCH 08/11] Fix build error. --- extensions/cstrike/natives.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/extensions/cstrike/natives.cpp b/extensions/cstrike/natives.cpp index 76cd66ee..626347cb 100644 --- a/extensions/cstrike/natives.cpp +++ b/extensions/cstrike/natives.cpp @@ -880,7 +880,7 @@ static cell_t CS_ItemDefIndexToID(IPluginContext *pContext, const cell_t *params static cell_t CS_WeaponIDToItemDefIndex(IPluginContext *pContext, const cell_t *params) { #if SOURCE_ENGINE == SE_CSGO - WeaponIDMap::Result res = g_mapWeaponIDToDefIdx.find((uint16_t)params[1]); + WeaponIDMap::Result res = g_mapWeaponIDToDefIdx.find((SMCSWeapon)params[1]); if (!res.found()) return pContext->ThrowNativeError("Invalid weapon id passed."); From 687db96b38112ba455d21fab5515e94600236942 Mon Sep 17 00:00:00 2001 From: Ruben Gonzalez Date: Wed, 4 Oct 2017 18:20:06 -0400 Subject: [PATCH 09/11] Make CS:GO specific natives exist only on CS:GO --- extensions/cstrike/natives.cpp | 73 +++++++++++++--------------------- 1 file changed, 27 insertions(+), 46 deletions(-) diff --git a/extensions/cstrike/natives.cpp b/extensions/cstrike/natives.cpp index 626347cb..e0ed38bb 100644 --- a/extensions/cstrike/natives.cpp +++ b/extensions/cstrike/natives.cpp @@ -807,42 +807,6 @@ static cell_t CS_GetMVPCount(IPluginContext *pContext, const cell_t *params) return GetPlayerVar(pContext, params, "MVPs"); } -static cell_t CS_SetClientContributionScore(IPluginContext *pContext, const cell_t *params) -{ -#if SOURCE_ENGINE == SE_CSGO - return SetPlayerVar(pContext, params, "CScore"); -#else - return pContext->ThrowNativeError("SetClientContributionScore is not supported on this game"); -#endif -} - -static cell_t CS_GetClientContributionScore(IPluginContext *pContext, const cell_t *params) -{ -#if SOURCE_ENGINE == SE_CSGO - return GetPlayerVar(pContext, params, "CScore"); -#else - return pContext->ThrowNativeError("GetClientContributionScore is not supported on this game"); -#endif -} - -static cell_t CS_SetClientAssists(IPluginContext *pContext, const cell_t *params) -{ -#if SOURCE_ENGINE == SE_CSGO - return SetPlayerVar(pContext, params, "Assists"); -#else - return pContext->ThrowNativeError("SetClientAssists is not supported on this game"); -#endif -} - -static cell_t CS_GetClientAssists(IPluginContext *pContext, const cell_t *params) -{ -#if SOURCE_ENGINE == SE_CSGO - return GetPlayerVar(pContext, params, "Assists"); -#else - return pContext->ThrowNativeError("GetClientAssists is not supported on this game"); -#endif -} - static cell_t CS_UpdateClientModel(IPluginContext *pContext, const cell_t *params) { static ICallWrapper *pWrapper = NULL; @@ -863,33 +827,48 @@ static cell_t CS_UpdateClientModel(IPluginContext *pContext, const cell_t *param return 1; } +//CS:GO only natives below this. +#if SOURCE_ENGINE == SE_CSGO +static cell_t CS_SetClientContributionScore(IPluginContext *pContext, const cell_t *params) +{ + return SetPlayerVar(pContext, params, "CScore"); +} + +static cell_t CS_GetClientContributionScore(IPluginContext *pContext, const cell_t *params) +{ + return GetPlayerVar(pContext, params, "CScore"); +} + +static cell_t CS_SetClientAssists(IPluginContext *pContext, const cell_t *params) +{ + return SetPlayerVar(pContext, params, "Assists"); +} + +static cell_t CS_GetClientAssists(IPluginContext *pContext, const cell_t *params) +{ + return GetPlayerVar(pContext, params, "Assists"); +} + static cell_t CS_ItemDefIndexToID(IPluginContext *pContext, const cell_t *params) { -#if SOURCE_ENGINE == SE_CSGO ItemIndexMap::Result res = g_mapDefIdxToClass.find((uint16_t)params[1]); if (!res.found()) return pContext->ThrowNativeError("Invalid item definition passed."); return res->value.m_iWeaponID; -#else - return pContext->ThrowNativeError("CS_ItemDefIndexToID is not supported on this game"); -#endif } static cell_t CS_WeaponIDToItemDefIndex(IPluginContext *pContext, const cell_t *params) { -#if SOURCE_ENGINE == SE_CSGO WeaponIDMap::Result res = g_mapWeaponIDToDefIdx.find((SMCSWeapon)params[1]); if (!res.found()) return pContext->ThrowNativeError("Invalid weapon id passed."); return res->value.m_iDefIdx; -#else - return pContext->ThrowNativeError("CS_WeaponIDToItemDefIndex is not supported on this game"); -#endif } +#endif sp_nativeinfo_t g_CSNatives[] = { @@ -907,14 +886,16 @@ sp_nativeinfo_t g_CSNatives[] = {"CS_GetMVPCount", CS_GetMVPCount}, {"CS_SetMVPCount", CS_SetMVPCount}, {"CS_WeaponIDToAlias", CS_WeaponIDToAlias}, + {"CS_UpdateClientModel", CS_UpdateClientModel}, + {"CS_IsValidWeaponID", CS_IsValidWeaponID}, +#if SOURCE_ENGINE == SE_CSGO {"CS_GetClientContributionScore", CS_GetClientContributionScore}, {"CS_SetClientContributionScore", CS_SetClientContributionScore}, {"CS_GetClientAssists", CS_GetClientAssists}, {"CS_SetClientAssists", CS_SetClientAssists}, - {"CS_UpdateClientModel", CS_UpdateClientModel}, - {"CS_IsValidWeaponID", CS_IsValidWeaponID}, {"CS_ItemDefIndexToID", CS_ItemDefIndexToID}, {"CS_WeaponIDToItemDefIndex", CS_WeaponIDToItemDefIndex}, +#endif {NULL, NULL} }; From 26586363772da9fca794fdddf23feeb9a57be3ae Mon Sep 17 00:00:00 2001 From: Kyle Sanderson Date: Mon, 9 Oct 2017 13:00:52 -0700 Subject: [PATCH 10/11] Fix unsigned compiler warning. --- extensions/cstrike/util_cstrike.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/extensions/cstrike/util_cstrike.cpp b/extensions/cstrike/util_cstrike.cpp index bcab0d80..722e7c5c 100644 --- a/extensions/cstrike/util_cstrike.cpp +++ b/extensions/cstrike/util_cstrike.cpp @@ -479,7 +479,7 @@ const char *GetTranslatedWeaponAlias(const char *weapon) "nvgs", "nightvision" }; - for (int i = 0; i < SM_ARRAYSIZE(szAliases) / 2; i++) + for (size_t i = 0; i < SM_ARRAYSIZE(szAliases) / 2; i++) { if (Q_stristr(GetWeaponNameFromClassname(weapon), szAliases[i * 2]) != 0) return szAliases[i * 2 + 1]; From 6085afbaaed65302490d82dbf9c14bfaf638f52a Mon Sep 17 00:00:00 2001 From: Ruben Gonzalez Date: Fri, 27 Oct 2017 18:55:02 -0400 Subject: [PATCH 11/11] Fix comment info --- extensions/cstrike/util_cstrike.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/extensions/cstrike/util_cstrike.h b/extensions/cstrike/util_cstrike.h index bdd4581d..fb7063d4 100644 --- a/extensions/cstrike/util_cstrike.h +++ b/extensions/cstrike/util_cstrike.h @@ -33,7 +33,7 @@ #define _INCLUDE_CSTRIKE_UTIL_H_ //THIS IS THE INCLUDE ENUM DO NOT CHANGE ONLY UPDATE THE INCLUDE //This is used to match to old weaponid's to their correct enum value - //Anything after defuser will pass the itemdef as they will be the id set in include + //Anything after heavy assault suit will pass the itemdef as they will be the id set in include enum SMCSWeapon { SMCSWeapon_NONE = 0,