From c3a52b366ec6872fb349d89a68312adc7ccf4289 Mon Sep 17 00:00:00 2001 From: Arkshine Date: Sun, 24 Jan 2016 19:29:09 +0100 Subject: [PATCH] Add cs_get_item_alias() native & cleanup alias datas --- gamedata/modules.games/game.cstrike.txt | 222 ++++-------------- modules/cstrike/cstrike/CstrikeDatas.h | 2 + modules/cstrike/cstrike/CstrikeHacks.cpp | 2 +- modules/cstrike/cstrike/CstrikeItemsInfos.cpp | 110 +++++---- modules/cstrike/cstrike/CstrikeItemsInfos.h | 10 +- modules/cstrike/cstrike/CstrikeNatives.cpp | 35 +++ plugins/include/cstrike.inc | 15 +- plugins/include/cstrike_const.inc | 2 + 8 files changed, 165 insertions(+), 233 deletions(-) diff --git a/gamedata/modules.games/game.cstrike.txt b/gamedata/modules.games/game.cstrike.txt index dbf6578c..116af2e6 100644 --- a/gamedata/modules.games/game.cstrike.txt +++ b/gamedata/modules.games/game.cstrike.txt @@ -144,12 +144,17 @@ { "ItemInfos" { - "CommonAlias" + "BuyAliases" { + // + // Weapon + // + "p228" { "itemid" "1" // CSW/I_P228 "classid" "2" // CS_WEAPONCLASS_PISTOL + "altname" "228compact" } "scout" { @@ -160,6 +165,7 @@ { "itemid" "5" // CSW/I_XM1014 "classid" "5" // CS_WEAPONCLASS_SHOTGUN + "altname" "autoshotgun" } "mac10" { @@ -170,244 +176,123 @@ { "itemid" "8" // CSW/I_AUG "classid" "7" // CS_WEAPONCLASS_RIFLE + "altname" "bullpup" } "elites" { - "itemid" "10" // CSW/I_ELITE + "itemid" "10" // CSW/I_ELITE "classid" "2" // CS_WEAPONCLASS_PISTOL } "fn57" { - "itemid" "11" // CSW/I_FIVESEVEN + "itemid" "11" // CSW/I_FIVESEVEN "classid" "2" // CS_WEAPONCLASS_PISTOL + "altname" "fiveseven" } "ump45" { - "itemid" "12" // CSW/I_UMP45 + "itemid" "12" // CSW/I_UMP45 "classid" "4" // CS_WEAPONCLASS_SUBMACHINEGUN } "sg550" { - "itemid" "13" // CSW/I_SG550 + "itemid" "13" // CSW/I_SG550 "classid" "8" // CS_WEAPONCLASS_SNIPERRIFLE + "altname" "krieg550" } "galil" { - "itemid" "14" // CSW/I_GALIL + "itemid" "14" // CSW/I_GALIL "classid" "7" // CS_WEAPONCLASS_RIFLE + "altname" "defender" } "famas" { - "itemid" "15" // CSW/I_FAMAS + "itemid" "15" // CSW/I_FAMAS "classid" "7" // CS_WEAPONCLASS_RIFLE + "altname" "clarion" } "usp" { - "itemid" "16" // CSW/I_USP + "itemid" "16" // CSW/I_USP "classid" "2" // CS_WEAPONCLASS_PISTOL + "altname" "km45" } "glock" { - "itemid" "17" // CSW/I_GLOCK18 + "itemid" "17" // CSW/I_GLOCK18 "classid" "2" // CS_WEAPONCLASS_PISTOL + "altname" "9x19mm" } "awp" { - "itemid" "18" // CSW/I_AWP + "itemid" "18" // CSW/I_AWP "classid" "8" // CS_WEAPONCLASS_SNIPERRIFLE + "altname" "magnum" } "mp5" { - "itemid" "19" // CSW/I_MP5NAVY + "itemid" "19" // CSW/I_MP5NAVY "classid" "4" // CS_WEAPONCLASS_SUBMACHINEGUN + "altname" "smg" } "m249" { - "itemid" "20" // CSW/I_M249 + "itemid" "20" // CSW/I_M249 "classid" "6" // CS_WEAPONCLASS_MACHINEGUN } "m3" { - "itemid" "21" // CSW/I_M3 + "itemid" "21" // CSW/I_M3 "classid" "5" // CS_WEAPONCLASS_SHOTGUN + "altname" "12gauge" } "m4a1" { - "itemid" "22" // CSW/I_M4A1 + "itemid" "22" // CSW/I_M4A1 "classid" "7" // CS_WEAPONCLASS_RIFLE } "tmp" { - "itemid" "23" // CSW/I_TMP + "itemid" "23" // CSW/I_TMP "classid" "4" // CS_WEAPONCLASS_SUBMACHINEGUN + "altname" "mp" } "g3sg1" { - "itemid" "24" // CSW/I_G3SG1 + "itemid" "24" // CSW/I_G3SG1 "classid" "8" // CS_WEAPONCLASS_SNIPERRIFLE + "altname" "d3au1" } "deagle" { - "itemid" "26" // CSW/I_DEAGLE + "itemid" "26" // CSW/I_DEAGLE "classid" "2" // CS_WEAPONCLASS_PISTOL + "altname" "nighthawk" } "sg552" { - "itemid" "27" // CSW/I_SG552 + "itemid" "27" // CSW/I_SG552 "classid" "7" // CS_WEAPONCLASS_RIFLE + "altname" "krieg552" } "ak47" { - "itemid" "28" // CSW/I_AK47 + "itemid" "28" // CSW/I_AK47 "classid" "7" // CS_WEAPONCLASS_RIFLE + "altname" "cv47" } "p90" { - "itemid" "30" // CSW/I_P90 + "itemid" "30" // CSW/I_P90 "classid" "4" // CS_WEAPONCLASS_SUBMACHINEGUN + "altname" "c90" } - "fiveseven" - { - "itemid" "11" // CSW/I_FIVESEVEN - "classid" "2" // CS_WEAPONCLASS_PISTOL - } - } - "WeaponAlias" - { - "grenade" - { - "itemid" "4" // CSW/I_HEGRENADE - "classid" "3" // CS_WEAPONCLASS_GRENADE - } - "hegrenade" - { - "itemid" "4" // CSW/I_HEGRENADE - "classid" "3" // CS_WEAPONCLASS_GRENADE - } - "c4" - { - "itemid" "6" // CSW/I_C4 - "classid" "3" // CS_WEAPONCLASS_GRENADE - } - "elite" - { - "itemid" "10" // CSW/I_ELITE - "classid" "2" // CS_WEAPONCLASS_PISTOL - } - "glock18" - { - "itemid" "17" // CSW/I_GLOCK18 - "classid" "2" // CS_WEAPONCLASS_PISTOL - } - "mp5navy" - { - "itemid" "19" // CSW/I_MP5NAVY - "classid" "4" // CS_WEAPONCLASS_SUBMACHINEGUN - } - "knife" - { - "itemid" "29" // CSW/I_KNIFE - "classid" "1" // CS_WEAPONCLASS_KNIFE - } - } + // + // Equipment + // - "BuyAlias" - { - "228compact" - { - "itemid" "1" // CSW/I_P228 - "classid" "2" // CS_WEAPONCLASS_PISTOL - } - "autoshotgun" - { - "itemid" "5" // CSW/I_XM1014 - "classid" "5" // CS_WEAPONCLASS_SHOTGUN - } - "bullpup" - { - "itemid" "8" // CSW/I_AUG - "classid" "7" // CS_WEAPONCLASS_RIFLE - } - "sg550" - { - "itemid" "13" // CSW/I_SG550 - "classid" "7" // CS_WEAPONCLASS_RIFLE - } - "krieg550" - { - "itemid" "13" // CSW/I_SG550 - "classid" "7" // CS_WEAPONCLASS_RIFLE - } - "defender" - { - "itemid" "14" // CSW/I_GALIL - "classid" "7" // CS_WEAPONCLASS_RIFLE - } - "clarion" - { - "itemid" "15" // CSW/I_FAMAS - "classid" "7" // CS_WEAPONCLASS_RIFLE - } - "km45" - { - "itemid" "16" // CSW/I_USP - "classid" "2" // CS_WEAPONCLASS_PISTOL - } - "9x19mm" - { - "itemid" "17" // CSW/I_GLOCK18 - "classid" "2" // CS_WEAPONCLASS_PISTOL - } - "magnum" - { - "itemid" "18" // CSW/I_AWP - "classid" "8" // CS_WEAPONCLASS_SNIPERRIFLE - } - "smg" - { - "itemid" "19" // CSW/I_MP5NAVY - "classid" "4" // CS_WEAPONCLASS_SUBMACHINEGUN - } - "12gauge" - { - "itemid" "21" // CSW/I_M3 - "classid" "5" // CS_WEAPONCLASS_SHOTGUN - } - "mp" - { - "itemid" "23" // CSW/I_TMP - "classid" "4" // CS_WEAPONCLASS_SUBMACHINEGUN - } - "d3au1" - { - "itemid" "24" // CSW/I_G3SG1 - "classid" "8" // CS_WEAPONCLASS_SNIPERRIFLE - } - "nighthawk" - { - "itemid" "26" // CSW/I_DEAGLE - "classid" "2" // CS_WEAPONCLASS_PISTOL - } - "krieg552" - { - "itemid" "27" // CSW/I_SG552 - "classid" "7" // CS_WEAPONCLASS_RIFLE - } - "cv47" - { - "itemid" "28" // CSW/I_AK47 - "classid" "7" // CS_WEAPONCLASS_RIFLE - } - "c90" - { - "itemid" "30" // CSW/I_P90 - "classid" "4" // CS_WEAPONCLASS_SUBMACHINEGUN - } - } - - "BuyEquipAlias" - { "hegren" { "itemid" "4" // CSW/I_HEGRENADE @@ -455,25 +340,20 @@ "classid" "2" // CS_WEAPONCLASS_PISTOL "classname" "weapon_shield" } - } - "BuyAmmoAlias" - { + // + // Ammunition + // + "primammo" { "itemid" "36" // CSI_PRIAMMO + "altname" "buyammo1" } "secammo" { "itemid" "37" // CSI_SECAMMO - } - "buyammo1" - { - "itemid" "36" // CSI_PRIAMMO - } - "buyammo2" - { - "itemid" "37" // CSI_SECAMMO + "altname" "buyammo2" } } } diff --git a/modules/cstrike/cstrike/CstrikeDatas.h b/modules/cstrike/cstrike/CstrikeDatas.h index 607d7895..433651c0 100644 --- a/modules/cstrike/cstrike/CstrikeDatas.h +++ b/modules/cstrike/cstrike/CstrikeDatas.h @@ -19,6 +19,7 @@ */ #define CSI_NONE CSW_NONE #define CSI_P228 CSW_P228 +#define CSI_GLOCK CSW_GLOCK // Unused by game #define CSI_SCOUT CSW_SCOUT #define CSI_HEGRENADE CSW_HEGRENADE #define CSI_XM1014 CSW_XM1014 @@ -73,6 +74,7 @@ */ #define CSW_NONE 0 #define CSW_P228 1 +#define CSW_GLOCK 2 // Unused by game #define CSW_SCOUT 3 #define CSW_HEGRENADE 4 #define CSW_XM1014 5 diff --git a/modules/cstrike/cstrike/CstrikeHacks.cpp b/modules/cstrike/cstrike/CstrikeHacks.cpp index 9230c4c2..2af17096 100644 --- a/modules/cstrike/cstrike/CstrikeHacks.cpp +++ b/modules/cstrike/cstrike/CstrikeHacks.cpp @@ -162,7 +162,7 @@ DETOUR_DECL_STATIC1(C_ClientCommand, void, edict_t*, pEdict) // void ClientComma UTIL_StringToLower(command, commandLowered, sizeof(commandLowered)); - if (ItemsManager.GetAliasInfosFromBuy(commandLowered, &info)) + if (ItemsManager.GetAliasInfos(commandLowered, &info)) { CurrentItemId = info.itemid; } diff --git a/modules/cstrike/cstrike/CstrikeItemsInfos.cpp b/modules/cstrike/cstrike/CstrikeItemsInfos.cpp index 40b83b41..36018e8c 100644 --- a/modules/cstrike/cstrike/CstrikeItemsInfos.cpp +++ b/modules/cstrike/cstrike/CstrikeItemsInfos.cpp @@ -24,7 +24,6 @@ char WeaponNameList[MAX_WEAPONS][64]; CsItemInfo::CsItemInfo() : m_ParseState(PSTATE_ALIASES_TYPE), - m_List(nullptr), m_ListsRetrievedFromConfig(false) {} @@ -35,9 +34,8 @@ CsItemInfo::~CsItemInfo() void CsItemInfo::Clear() { - m_CommonAliasesList.clear(); - m_WeaponAliasesList.clear(); m_BuyAliasesList.clear(); + m_BuyAliasesAltList.clear(); } bool CsItemInfo::HasConfigError() @@ -51,33 +49,15 @@ SMCResult CsItemInfo::ReadSMC_NewSection(const SMCStates *states, const char *na { case PSTATE_ALIASES_TYPE: { - m_List = nullptr; - - if (!strcmp(name, "CommonAlias")) - { - m_List = &m_CommonAliasesList; - } - else if (!strcmp(name, "WeaponAlias")) - { - m_List = &m_WeaponAliasesList; - } - else if (!strcmp(name, "BuyAlias") || !strcmp(name, "BuyEquipAlias") || !strcmp(name, "BuyAmmoAlias")) - { - m_List = &m_BuyAliasesList; - } - - if (m_List) + if (!strcmp(name, "BuyAliases")) { m_ParseState = PSTATE_ALIASES_ALIAS; } - break; } case PSTATE_ALIASES_ALIAS: { - m_AliasInfo.clear(); m_Alias = name; - m_ParseState = PSTATE_ALIASES_ALIAS_DEFS; break; } @@ -129,6 +109,10 @@ SMCResult CsItemInfo::ReadSMC_KeyValue(const SMCStates *states, const char *key, } } } + else if (!strcmp(key, "altname")) + { + m_AliasAlt = value; + } break; } } @@ -147,11 +131,16 @@ SMCResult CsItemInfo::ReadSMC_LeavingSection(const SMCStates *states) } case PSTATE_ALIASES_ALIAS_DEFS: { - m_List->replace(m_Alias.chars(), m_AliasInfo); + if (m_AliasAlt.length()) + { + m_BuyAliasesAltList.replace(m_AliasAlt.chars(), m_AliasInfo); + m_AliasInfo.alt_alias = Move(m_AliasAlt); + } + + m_BuyAliasesList.replace(m_Alias.chars(), m_AliasInfo); m_WeaponIdToClass[m_AliasInfo.itemid] = static_cast(m_AliasInfo.classid); m_AliasInfo.clear(); - m_ParseState = PSTATE_ALIASES_ALIAS; break; } @@ -170,7 +159,7 @@ void CsItemInfo::ReadSMC_ParseEnd(bool halted, bool failed) bool CsItemInfo::GetAliasInfos(const char *alias, AliasInfo *info) { - if (GetAliasInfosFromBuy(alias, info) || m_WeaponAliasesList.retrieve(alias, info)) + if (m_BuyAliasesList.retrieve(alias, info) || m_BuyAliasesAltList.retrieve(alias, info)) { return true; } @@ -178,11 +167,17 @@ bool CsItemInfo::GetAliasInfos(const char *alias, AliasInfo *info) return false; } -bool CsItemInfo::GetAliasInfosFromBuy(const char *alias, AliasInfo *info) +bool CsItemInfo::GetAliasFromId(size_t id, ke::AString &name, ke::AString &altname) { - if (m_CommonAliasesList.retrieve(alias, info) || m_BuyAliasesList.retrieve(alias, info)) + for (auto iter = m_BuyAliasesList.iter(); !iter.empty(); iter.next()) { - return true; + if (iter->value.itemid == id) + { + name = iter->key; + altname = iter->value.alt_alias; + + return true; + } } return false; @@ -190,47 +185,50 @@ bool CsItemInfo::GetAliasInfosFromBuy(const char *alias, AliasInfo *info) bool CsItemInfo::GetAliasInfosFromName(const char *name, AliasInfo *info) { - static const char prefix_weapon[] = "weapon_"; - static const char prefix_item[] = "item_"; + static const char prefix_weapon[] = "weapon"; + static const char prefix_item[] = "item"; - const char *alias = name; + static const size_t prefix_weapon_length = sizeof(prefix_weapon) - 1; + static const size_t prefix_item_length = sizeof(prefix_item) - 1; - if (strstr(name, prefix_weapon) && strcmp(name + sizeof(prefix_weapon) - 1, "shield")) + if (name[prefix_weapon_length] == '_' && !strncmp(name, prefix_weapon, prefix_weapon_length)) { - for (size_t id = 0; id < ARRAYSIZE(WeaponNameList); ++id) - { - if (!strcmp(name, WeaponNameList[id])) - { - info->classname = name; - info->itemid = id; - info->classid = WeaponIdToClass(id); + name += prefix_weapon_length + 1; // including '_' + } + else if (name[prefix_item_length] == '_' && !strncmp(name, prefix_item, prefix_item_length)) + { + name += prefix_item_length + 1; + } - return true; - } + for (size_t id = 0; id < ARRAYSIZE(WeaponNameList); ++id) + { + const char *weapon = WeaponNameList[id]; + + if (weapon[prefix_weapon_length] == '_' && !strncmp(weapon, prefix_weapon, prefix_weapon_length)) + { + weapon += prefix_weapon_length + 1; } - alias = name + sizeof(prefix_weapon) - 1; - } - else if (strstr(name, prefix_item)) - { - for (auto iter = m_BuyAliasesList.iter(); !iter.empty(); iter.next()) + if (!strcmp(weapon, name)) { - if (iter->value.classname.length() && !iter->value.classname.compare(name)) - { - *info = iter->value; - return true; - } + info->classname = name; + info->itemid = id; + info->classid = WeaponIdToClass(id); + + return true; } - - alias = name + sizeof(prefix_item) - 1; } - if (GetAliasInfos(alias, info)) + for (auto iter = m_BuyAliasesList.iter(); !iter.empty(); iter.next()) { - return true; + if (iter->value.classname.length() && !strcmp(iter->value.classname.chars() + prefix_item_length + 1, name)) + { + *info = iter->value; + return true; + } } - return false; + return GetAliasInfos(name, info); } CsWeaponClassType CsItemInfo::WeaponIdToClass(int id) diff --git a/modules/cstrike/cstrike/CstrikeItemsInfos.h b/modules/cstrike/cstrike/CstrikeItemsInfos.h index 2729f19f..e3d886e9 100644 --- a/modules/cstrike/cstrike/CstrikeItemsInfos.h +++ b/modules/cstrike/cstrike/CstrikeItemsInfos.h @@ -31,11 +31,14 @@ struct AliasInfo { itemid = CSI_NONE; classid = CS_WEAPONCLASS_NONE; + classname = nullptr; + alt_alias = nullptr; } int itemid; int classid; ke::AString classname; + ke::AString alt_alias; }; enum class Equipments @@ -73,8 +76,8 @@ class CsItemInfo : public ITextListener_SMC public: bool GetAliasInfos(const char *alias, AliasInfo *info); - bool GetAliasInfosFromBuy(const char *alias, AliasInfo *info); bool GetAliasInfosFromName(const char *classname, AliasInfo *info); + bool GetAliasFromId(size_t id, ke::AString &name, ke::AString &altname); CsWeaponClassType WeaponIdToClass(int id); @@ -84,17 +87,16 @@ class CsItemInfo : public ITextListener_SMC typedef StringHashMap AliasMap; - AliasMap m_CommonAliasesList; - AliasMap m_WeaponAliasesList; AliasMap m_BuyAliasesList; + AliasMap m_BuyAliasesAltList; CsWeaponClassType m_WeaponIdToClass[CSI_MAX_COUNT]; private: // Config parsing int m_ParseState; - AliasMap* m_List; ke::AString m_Alias; + ke::AString m_AliasAlt; AliasInfo m_AliasInfo; bool m_ListsRetrievedFromConfig; int m_EquipmentsPrice[static_cast(Equipments::Count)]; diff --git a/modules/cstrike/cstrike/CstrikeNatives.cpp b/modules/cstrike/cstrike/CstrikeNatives.cpp index 92594e56..5685b2f5 100644 --- a/modules/cstrike/cstrike/CstrikeNatives.cpp +++ b/modules/cstrike/cstrike/CstrikeNatives.cpp @@ -1809,6 +1809,40 @@ static cell AMX_NATIVE_CALL cs_get_item_id(AMX* amx, cell* params) return CSI_NONE; } +// native bool:cs_get_item_alias(itemid, name[], name_maxlen, altname[] = "", altname_maxlen = 0); +static cell AMX_NATIVE_CALL cs_get_item_alias(AMX* amx, cell* params) +{ + if (ItemsManager.HasConfigError()) + { + MF_LogError(amx, AMX_ERR_NATIVE, "Native cs_get_item_alias() is disabled because of corrupted or missing gamedata"); + return 0; + } + + auto itemid = params[1]; + + if (itemid == CSI_SHIELDGUN) + { + itemid = CSI_SHIELD; + } + else if (itemid == CSI_GLOCK) + { + itemid = CSI_GLOCK18; + } + else if (itemid <= CSI_NONE || itemid >= CSI_MAX_COUNT) + { + MF_LogError(amx, AMX_ERR_NATIVE, "Invalid item id: %d", itemid); + return FALSE; + } + + ke::AString name, altname; + auto result = ItemsManager.GetAliasFromId(itemid, name, altname); + + MF_SetAmxString(amx, params[2], name.chars(), params[3]); + MF_SetAmxString(amx, params[4], altname.chars(), params[5]); + + return result ? TRUE : FALSE; +} + // native bool:cs_get_translated_item_alias(const alias[], itemname[], maxlength); static cell AMX_NATIVE_CALL cs_get_translated_item_alias(AMX* amx, cell* params) { @@ -2005,6 +2039,7 @@ AMX_NATIVE_INFO CstrikeNatives[] = {"cs_find_ent_by_class", cs_find_ent_by_class}, {"cs_find_ent_by_owner", cs_find_ent_by_owner}, {"cs_get_item_id", cs_get_item_id}, + {"cs_get_item_alias", cs_get_item_alias}, {"cs_get_translated_item_alias",cs_get_translated_item_alias}, {"cs_get_weapon_info", cs_get_weapon_info}, {"cs_get_user_weapon_entity", cs_get_user_weapon_entity}, diff --git a/plugins/include/cstrike.inc b/plugins/include/cstrike.inc index 73e58628..4f8cf010 100755 --- a/plugins/include/cstrike.inc +++ b/plugins/include/cstrike.inc @@ -361,7 +361,7 @@ native cs_set_user_plant(index, plant = 1, showbombicon = 1); * @param index Client index * @param team Team id * @param model Internal model id, if CS_DONTCHANGE the game will choose the model - or if CS_NORESET the game will not update it. + * or if CS_NORESET the game will not update it. * @param send_teaminfo If true, a TeamInfo message will be sent * * @noreturn @@ -1060,6 +1060,19 @@ native cs_find_ent_by_owner(start_index, const classname[], owner); */ native any:cs_get_item_id(const name[], &CsWeaponClassType:classid = CS_WEAPONCLASS_NONE); +/** + * Returns the alias name associated with an item index. + * + * @param itemid Item id (CSI_* constants) + * @param name Buffer to store alias name to + * @param name_maxlen Maximum buffer size + * @param altname Optional buffer to store if available alternative alias name to + * @param altname_maxlen Maximum buffer size + * + * @return True if alias is found, false otherwise + */ +native bool:cs_get_item_alias(itemid, name[], name_maxlen, altname[] = "", altname_maxlen = 0); + /** * Returns an item name associated with a command alias. * diff --git a/plugins/include/cstrike_const.inc b/plugins/include/cstrike_const.inc index 4478b9fb..d13a2025 100644 --- a/plugins/include/cstrike_const.inc +++ b/plugins/include/cstrike_const.inc @@ -21,6 +21,7 @@ */ #define CSW_NONE 0 #define CSW_P228 1 +#define CSW_GLOCK 2 // Unused by game, See CSW_GLOCK18. #define CSW_SCOUT 3 #define CSW_HEGRENADE 4 #define CSW_XM1014 5 @@ -147,6 +148,7 @@ enum */ #define CSI_NONE CSW_NONE #define CSI_P228 CSW_P228 +#define CSI_GLOCK CSW_GLOCK // Unused by game, See CSI_GLOCK18. #define CSI_SCOUT CSW_SCOUT #define CSI_HEGRENADE CSW_HEGRENADE #define CSI_XM1014 CSW_XM1014