Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add function to find a TopMenu Item #1944

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions extensions/topmenus/TopMenu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1239,6 +1239,18 @@ unsigned int TopMenu::FindCategory(const char *name)
return obj->object_id;
}

unsigned int TopMenu::FindItem(const char *name)
{
topmenu_object_t *obj;
if (!m_ObjLookup.retrieve(name, &obj))
return 0;

if (obj->type != TopMenuObject_Item)
return 0;

return obj->object_id;
}

void TopMenu::OnMaxPlayersChanged( int newvalue )
{
m_max_clients = newvalue;
Expand Down
1 change: 1 addition & 0 deletions extensions/topmenus/TopMenu.h
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ class TopMenu :
TopMenuPosition position);
virtual bool LoadConfiguration(const char *file, char *error, size_t maxlength);
virtual unsigned int FindCategory(const char *name);
virtual unsigned int FindItem(const char *name);
const char *GetObjectInfoString(unsigned int object_id);
const char *GetObjectName(unsigned int object_id);
public: //IMenuHandler
Expand Down
19 changes: 19 additions & 0 deletions extensions/topmenus/smn_topmenus.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,24 @@ static cell_t FindTopMenuCategory(IPluginContext *pContext, const cell_t *params
return pMenu->FindCategory(name);
}

static cell_t FindTopMenuItem(IPluginContext *pContext, const cell_t *params)
{
HandleError err;
ITopMenu *pMenu;
HandleSecurity sec(pContext->GetIdentity(), myself->GetIdentity());

if ((err = handlesys->ReadHandle(params[1], hTopMenuType, &sec, (void **)&pMenu))
!= HandleError_None)
{
return pContext->ThrowNativeError("Invalid Handle %x (error: %d)", params[1], err);
}

char *name;
pContext->LocalToString(params[2], &name);

return pMenu->FindItem(name);
}

static cell_t DisplayTopMenu(IPluginContext *pContext, const cell_t *params)
{
HandleError err;
Expand Down Expand Up @@ -486,6 +504,7 @@ sp_nativeinfo_t g_TopMenuNatives[] =
{"TopMenu.LoadConfig", LoadTopMenuConfig},
{"TopMenu.Remove", RemoveFromTopMenu},
{"TopMenu.FindCategory", FindTopMenuCategory},
{"TopMenu.FindItem", FindTopMenuItem},
{"TopMenu.GetInfoString", GetTopMenuInfoString},
{"TopMenu.GetObjName", GetTopMenuName},
{"TopMenu.CacheTitles.set", SetTopMenuTitleCaching},
Expand Down
10 changes: 9 additions & 1 deletion plugins/include/topmenus.inc
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,14 @@ methodmap TopMenu < Handle
// @return TopMenuObject ID on success, or
// INVALID_TOPMENUOBJECT on failure.
public native TopMenuObject FindCategory(const char[] name);


// Finds an item's topobj ID in a TopMenu.
//
// @param name Object's unique name.
// @return TopMenuObject ID on success, or
// INVALID_TOPMENUOBJECT on failure.
public native TopMenuObject FindItem(const char[] name);

// Set the menu title caching behavior of the TopMenu. By default titles
// are cached to reduce overhead. If you need dynamic menu titles which
// change each time the menu is displayed to a user, set this to false.
Expand Down Expand Up @@ -441,6 +448,7 @@ public void __ext_topmenus_SetNTVOptional()
MarkNativeAsOptional("TopMenu.Display");
MarkNativeAsOptional("TopMenu.DisplayCategory");
MarkNativeAsOptional("TopMenu.FindCategory");
MarkNativeAsOptional("TopMenu.FindItem");
MarkNativeAsOptional("TopMenu.CacheTitles.set");
}
#endif
Loading