Newmenus: Add get_menu_by_id and use it everywhere

This commit is contained in:
Valentin Grünbacher 2014-05-27 10:33:53 +02:00
parent c91d67912b
commit a6ffae72f5
5 changed files with 26 additions and 38 deletions

View File

@ -65,11 +65,8 @@ void CPlayer::Disconnect()
authorized = false; authorized = false;
teamIdsInitialized = false; teamIdsInitialized = false;
if (newmenu >= 0 && newmenu < (int)g_NewMenus.size() && g_NewMenus[newmenu]) if (Menu *pMenu = get_menu_by_id(newmenu))
{
Menu *pMenu = g_NewMenus[newmenu];
pMenu->Close(index); pMenu->Close(index);
}
List<ClientCvarQuery_Info *>::iterator iter, end=queries.end(); List<ClientCvarQuery_Info *>::iterator iter, end=queries.end();
for (iter=queries.begin(); iter!=end; iter++) for (iter=queries.begin(); iter!=end; iter++)

View File

@ -1294,12 +1294,8 @@ static cell AMX_NATIVE_CALL show_menu(AMX *amx, cell *params) /* 3 param */
pPlayer->menu = 0; pPlayer->menu = 0;
// Fire newmenu callback so closing it can be handled by the plugin // Fire newmenu callback so closing it can be handled by the plugin
int menu = pPlayer->newmenu; if (Menu *pMenu = get_menu_by_id(pPlayer->newmenu))
if (menu >= 0 && menu < (int)g_NewMenus.size() && g_NewMenus[menu])
{
Menu *pMenu = g_NewMenus[menu];
pMenu->Close(pPlayer->index); pMenu->Close(pPlayer->index);
}
UTIL_FakeClientCommand(pPlayer->pEdict, "menuselect", "10", 0); 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; pPlayer->menu = 0;
// Fire newmenu callback so closing it can be handled by the plugin // Fire newmenu callback so closing it can be handled by the plugin
int menu = pPlayer->newmenu; if (Menu *pMenu = get_menu_by_id(pPlayer->newmenu))
if (menu >= 0 && menu < (int)g_NewMenus.size() && g_NewMenus[menu])
{
Menu *pMenu = g_NewMenus[menu];
pMenu->Close(pPlayer->index); pMenu->Close(pPlayer->index);
}
UTIL_FakeClientCommand(pPlayer->pEdict, "menuselect", "10", 0); 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) if (gpGlobals->time > pPlayer->menuexpire)
{ {
int menu = pPlayer->newmenu; if (Menu *pMenu = get_menu_by_id(pPlayer->newmenu))
if (menu >= 0 && menu < (int)g_NewMenus.size() && g_NewMenus[menu])
{
Menu *pMenu = g_NewMenus[menu];
pMenu->Close(pPlayer->index); pMenu->Close(pPlayer->index);
}
else else
pPlayer->menu = 0; pPlayer->menu = 0;

View File

@ -963,10 +963,8 @@ void C_ClientCommand(edict_t *pEntity)
{ {
if (gpGlobals->time > pPlayer->menuexpire) if (gpGlobals->time > pPlayer->menuexpire)
{ {
int menu = pPlayer->newmenu; if (Menu *pMenu = get_menu_by_id(pPlayer->newmenu))
if (menu >= 0 && menu < (int)g_NewMenus.size() && g_NewMenus[menu])
{ {
Menu *pMenu = g_NewMenus[menu];
pMenu->Close(pPlayer->index); pMenu->Close(pPlayer->index);
RETURN_META(MRES_SUPERCEDE); RETURN_META(MRES_SUPERCEDE);
@ -989,10 +987,8 @@ void C_ClientCommand(edict_t *pEntity)
{ {
int menu = pPlayer->newmenu; int menu = pPlayer->newmenu;
pPlayer->newmenu = -1; pPlayer->newmenu = -1;
if (Menu *pMenu = get_menu_by_id(menu))
if (menu >= 0 && menu < (int)g_NewMenus.size() && g_NewMenus[menu])
{ {
Menu *pMenu = g_NewMenus[menu];
int item = pMenu->PagekeyToItem(pPlayer->page, pressed_key+1); int item = pMenu->PagekeyToItem(pPlayer->page, pressed_key+1);
if (item == MENU_BACK) if (item == MENU_BACK)
{ {

View File

@ -79,6 +79,14 @@ void validate_menu_text(char *str)
*(str-offs) = '\0'; *(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"), 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(); 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()); \ LogError(amx, AMX_ERR_NATIVE, "Invalid menu id %d(%d)", p, g_NewMenus.size()); \
return 0; } \ return 0; }
Menu *pMenu = g_NewMenus[p];
//Makes a new menu handle (-1 for failure) //Makes a new menu handle (-1 for failure)
//native csdm_makemenu(title[]); //native csdm_makemenu(title[]);
@ -1040,10 +1048,10 @@ static cell AMX_NATIVE_CALL menu_setprop(AMX *amx, cell *params)
return 1; 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()); \ LogError(amx, AMX_ERR_NATIVE, "Invalid menu id %d(%d)", p, g_NewMenus.size()); \
return 0; } \ return 0; }
Menu *pMenu = g_NewMenus[p];
static cell AMX_NATIVE_CALL menu_cancel(AMX *amx, cell *params) 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; return 0;
} }
int menu = player->newmenu; if (Menu *pMenu = get_menu_by_id(player->newmenu)) {
if (menu < 0 || menu >= (int)g_NewMenus.size() || !g_NewMenus[menu]) pMenu->Close(player->index);
return 0; return 1;
}
Menu *pMenu = g_NewMenus[menu]; return 0;
pMenu->Close(player->index);
return 1;
} }
static cell AMX_NATIVE_CALL menu_destroy(AMX *amx, cell *params) static cell AMX_NATIVE_CALL menu_destroy(AMX *amx, cell *params)

View File

@ -151,6 +151,7 @@ public:
}; };
void ClearMenus(); void ClearMenus();
Menu *get_menu_by_id(int id);
extern CVector<Menu *> g_NewMenus; extern CVector<Menu *> g_NewMenus;
extern AMX_NATIVE_INFO g_NewMenuNatives[]; extern AMX_NATIVE_INFO g_NewMenuNatives[];