diff --git a/amxmodx/CMisc.cpp b/amxmodx/CMisc.cpp index 8f2e508b..e1990618 100755 --- a/amxmodx/CMisc.cpp +++ b/amxmodx/CMisc.cpp @@ -65,11 +65,8 @@ void CPlayer::Disconnect() authorized = false; teamIdsInitialized = false; - if (newmenu >= 0 && newmenu < (int)g_NewMenus.size() && g_NewMenus[newmenu]) - { - Menu *pMenu = g_NewMenus[newmenu]; + if (Menu *pMenu = get_menu_by_id(newmenu)) pMenu->Close(index); - } List::iterator iter, end=queries.end(); for (iter=queries.begin(); iter!=end; iter++) diff --git a/amxmodx/amxmodx.cpp b/amxmodx/amxmodx.cpp index d2ae3f13..d491be17 100755 --- a/amxmodx/amxmodx.cpp +++ b/amxmodx/amxmodx.cpp @@ -1294,12 +1294,8 @@ static cell AMX_NATIVE_CALL show_menu(AMX *amx, cell *params) /* 3 param */ pPlayer->menu = 0; // Fire newmenu callback so closing it can be handled by the plugin - int menu = pPlayer->newmenu; - if (menu >= 0 && menu < (int)g_NewMenus.size() && g_NewMenus[menu]) - { - Menu *pMenu = g_NewMenus[menu]; + if (Menu *pMenu = get_menu_by_id(pPlayer->newmenu)) pMenu->Close(pPlayer->index); - } UTIL_FakeClientCommand(pPlayer->pEdict, "menuselect", "10", 0); @@ -1333,12 +1329,8 @@ static cell AMX_NATIVE_CALL show_menu(AMX *amx, cell *params) /* 3 param */ pPlayer->menu = 0; // Fire newmenu callback so closing it can be handled by the plugin - int menu = pPlayer->newmenu; - if (menu >= 0 && menu < (int)g_NewMenus.size() && g_NewMenus[menu]) - { - Menu *pMenu = g_NewMenus[menu]; + if (Menu *pMenu = get_menu_by_id(pPlayer->newmenu)) pMenu->Close(pPlayer->index); - } UTIL_FakeClientCommand(pPlayer->pEdict, "menuselect", "10", 0); @@ -2962,12 +2954,8 @@ static cell AMX_NATIVE_CALL get_user_menu(AMX *amx, cell *params) /* 3 param */ { if (gpGlobals->time > pPlayer->menuexpire) { - int menu = pPlayer->newmenu; - if (menu >= 0 && menu < (int)g_NewMenus.size() && g_NewMenus[menu]) - { - Menu *pMenu = g_NewMenus[menu]; + if (Menu *pMenu = get_menu_by_id(pPlayer->newmenu)) pMenu->Close(pPlayer->index); - } else pPlayer->menu = 0; diff --git a/amxmodx/meta_api.cpp b/amxmodx/meta_api.cpp index 86e9b998..67172920 100755 --- a/amxmodx/meta_api.cpp +++ b/amxmodx/meta_api.cpp @@ -963,10 +963,8 @@ void C_ClientCommand(edict_t *pEntity) { if (gpGlobals->time > pPlayer->menuexpire) { - int menu = pPlayer->newmenu; - if (menu >= 0 && menu < (int)g_NewMenus.size() && g_NewMenus[menu]) + if (Menu *pMenu = get_menu_by_id(pPlayer->newmenu)) { - Menu *pMenu = g_NewMenus[menu]; pMenu->Close(pPlayer->index); RETURN_META(MRES_SUPERCEDE); @@ -989,10 +987,8 @@ void C_ClientCommand(edict_t *pEntity) { int menu = pPlayer->newmenu; pPlayer->newmenu = -1; - - if (menu >= 0 && menu < (int)g_NewMenus.size() && g_NewMenus[menu]) + if (Menu *pMenu = get_menu_by_id(menu)) { - Menu *pMenu = g_NewMenus[menu]; int item = pMenu->PagekeyToItem(pPlayer->page, pressed_key+1); if (item == MENU_BACK) { diff --git a/amxmodx/newmenus.cpp b/amxmodx/newmenus.cpp index ee607ffe..691c3e6f 100755 --- a/amxmodx/newmenus.cpp +++ b/amxmodx/newmenus.cpp @@ -79,6 +79,14 @@ void validate_menu_text(char *str) *(str-offs) = '\0'; } } +} + +Menu *get_menu_by_id(int id) +{ + if (id < 0 || size_t(id) >= g_NewMenus.size() || !g_NewMenus[id]) + return NULL; + + return g_NewMenus[id]; } Menu::Menu(const char *title, AMX *amx, int fid) : m_Title(title), m_ItemColor("\\r"), @@ -606,10 +614,10 @@ const char *Menu::GetTextString(int player, page_t page, int &keys) return m_Text.c_str(); } -#define GETMENU(p) if (p >= (int)g_NewMenus.size() || p < 0 || !g_NewMenus[p] || g_NewMenus[p]->isDestroying) { \ +#define GETMENU(p) Menu *pMenu = get_menu_by_id(p); \ + if (pMenu == NULL || pMenu->isDestroying) { \ LogError(amx, AMX_ERR_NATIVE, "Invalid menu id %d(%d)", p, g_NewMenus.size()); \ - return 0; } \ - Menu *pMenu = g_NewMenus[p]; + return 0; } //Makes a new menu handle (-1 for failure) //native csdm_makemenu(title[]); @@ -1040,10 +1048,10 @@ static cell AMX_NATIVE_CALL menu_setprop(AMX *amx, cell *params) return 1; } -#define GETMENU_R(p) if (p >= (int)g_NewMenus.size() || p < 0 || !g_NewMenus[p]) { \ +#define GETMENU_R(p) Menu *pMenu = get_menu_by_id(p); \ + if (pMenu == NULL) { \ LogError(amx, AMX_ERR_NATIVE, "Invalid menu id %d(%d)", p, g_NewMenus.size()); \ - return 0; } \ - Menu *pMenu = g_NewMenus[p]; + return 0; } static cell AMX_NATIVE_CALL menu_cancel(AMX *amx, cell *params) { @@ -1062,14 +1070,12 @@ static cell AMX_NATIVE_CALL menu_cancel(AMX *amx, cell *params) return 0; } - int menu = player->newmenu; - if (menu < 0 || menu >= (int)g_NewMenus.size() || !g_NewMenus[menu]) - return 0; + if (Menu *pMenu = get_menu_by_id(player->newmenu)) { + pMenu->Close(player->index); + return 1; + } - Menu *pMenu = g_NewMenus[menu]; - pMenu->Close(player->index); - - return 1; + return 0; } static cell AMX_NATIVE_CALL menu_destroy(AMX *amx, cell *params) diff --git a/amxmodx/newmenus.h b/amxmodx/newmenus.h index e90b807e..c92e0069 100755 --- a/amxmodx/newmenus.h +++ b/amxmodx/newmenus.h @@ -151,6 +151,7 @@ public: }; void ClearMenus(); +Menu *get_menu_by_id(int id); extern CVector g_NewMenus; extern AMX_NATIVE_INFO g_NewMenuNatives[];