From 2e333caf772399059b03c643d620dfa517500022 Mon Sep 17 00:00:00 2001 From: David Anderson Date: Mon, 22 Oct 2007 06:56:17 +0000 Subject: [PATCH] - added amb1050 - topmenu info strings (similar to menus) - added removal detection to topmenus --HG-- extra : convert_revision : svn%3A39bc706e-5318-0410-9160-8a85361fbb7c/trunk%401640 --- extensions/topmenus/TopMenu.cpp | 33 ++++++++++++++-- extensions/topmenus/TopMenu.h | 12 ++++++ extensions/topmenus/smn_topmenus.cpp | 58 +++++++++++++++++++++++----- plugins/include/topmenus.inc | 25 +++++++++++- public/extensions/ITopMenus.h | 37 +++++++++++++++++- 5 files changed, 151 insertions(+), 14 deletions(-) diff --git a/extensions/topmenus/TopMenu.cpp b/extensions/topmenus/TopMenu.cpp index 1d9581d9..e55c0c9c 100644 --- a/extensions/topmenus/TopMenu.cpp +++ b/extensions/topmenus/TopMenu.cpp @@ -124,6 +124,18 @@ unsigned int TopMenu::AddToMenu(const char *name, const char *cmdname, FlagBits flags, unsigned int parent) +{ + return AddToMenu2(name, type, callbacks, owner, cmdname, flags, parent, NULL); +} + +unsigned int TopMenu::AddToMenu2(const char *name, + TopMenuObjectType type, + ITopMenuObjectCallbacks *callbacks, + IdentityToken_t *owner, + const char *cmdname, + FlagBits flags, + unsigned int parent, + const char *info_string) { /* Sanity checks */ if (type == TopMenuObject_Category && parent != 0) @@ -202,6 +214,7 @@ unsigned int TopMenu::AddToMenu(const char *name, obj->parent = parent_obj; strncopy(obj->name, name, sizeof(obj->name)); strncopy(obj->cmdname, cmdname ? cmdname : "", sizeof(obj->cmdname)); + strncopy(obj->info, info_string ? info_string : "", sizeof(obj->info)); if (obj->type == TopMenuObject_Category) { @@ -239,6 +252,20 @@ unsigned int TopMenu::AddToMenu(const char *name, return obj->object_id; } +const char *TopMenu::GetObjectInfoString(unsigned int object_id) +{ + if (object_id == 0 + || object_id > m_Objects.size() + || m_Objects[object_id - 1]->is_free) + { + return NULL; + } + + topmenu_object_t *obj = m_Objects[object_id - 1]; + + return obj->info; +} + void TopMenu::RemoveFromMenu(unsigned int object_id) { if (object_id == 0 @@ -320,11 +347,11 @@ void TopMenu::RemoveFromMenu(unsigned int object_id) } } - /* Finally, mark the object as free. */ - obj->is_free = true; - /* The callbacks pointer is still valid, so fire away! */ obj->callbacks->OnTopMenuObjectRemoved(this, object_id); + + /* Finally, mark the object as free. */ + obj->is_free = true; } bool TopMenu::DisplayMenu(int client, unsigned int hold_time, TopMenuPosition position) diff --git a/extensions/topmenus/TopMenu.h b/extensions/topmenus/TopMenu.h index 9404398e..d9139f7b 100644 --- a/extensions/topmenus/TopMenu.h +++ b/extensions/topmenus/TopMenu.h @@ -68,6 +68,7 @@ struct topmenu_object_t topmenu_object_t *parent; /** Parent, if any */ TopMenuObjectType type; /** Object Type */ bool is_free; /** Free or not? */ + char info[255]; /** Info string */ }; struct topmenu_category_t @@ -115,12 +116,21 @@ public: //ITopMenu const char *cmdname, FlagBits flags, unsigned int parent); + unsigned int AddToMenu2(const char *name, + TopMenuObjectType type, + ITopMenuObjectCallbacks *callbacks, + IdentityToken_t *owner, + const char *cmdname, + FlagBits flags, + unsigned int parent, + const char *info_string); virtual void RemoveFromMenu(unsigned int object_id); virtual bool DisplayMenu(int client, unsigned int hold_time, TopMenuPosition position); virtual bool LoadConfiguration(const char *file, char *error, size_t maxlength); virtual unsigned int FindCategory(const char *name); + const char *GetObjectInfoString(unsigned int object_id); public: //IMenuHandler virtual void OnMenuSelect2(IBaseMenu *menu, int client, unsigned int item, unsigned int item_on_page); virtual void OnMenuDrawItem(IBaseMenu *menu, int client, unsigned int item, unsigned int &style); @@ -166,4 +176,6 @@ private: bool m_bCatsNeedResort; /* True if categories need a resort */ }; +unsigned int strncopy(char *dest, const char *src, size_t count); + #endif //_INCLUDE_SOURCEMOD_TOP_MENU_H_ diff --git a/extensions/topmenus/smn_topmenus.cpp b/extensions/topmenus/smn_topmenus.cpp index 50146afb..b9645607 100644 --- a/extensions/topmenus/smn_topmenus.cpp +++ b/extensions/topmenus/smn_topmenus.cpp @@ -67,6 +67,7 @@ enum TopMenuAction TopMenuAction_DisplayTitle = 1, TopMenuAction_SelectOption = 2, TopMenuAction_DrawOption = 3, + TopMenuAction_RemoveObject = 4, }; class TopMenuCallbacks : public ITopMenuObjectCallbacks @@ -138,6 +139,14 @@ public: void OnTopMenuObjectRemoved(ITopMenu *menu, unsigned int object_id) { + m_pFunction->PushCell(m_hMenuHandle); + m_pFunction->PushCell(TopMenuAction_RemoveObject); + m_pFunction->PushCell(object_id); + m_pFunction->PushCell(0); + m_pFunction->PushString(""); + m_pFunction->PushCell(0); + m_pFunction->Execute(NULL); + delete this; } @@ -221,20 +230,26 @@ static cell_t AddToTopMenu(IPluginContext *pContext, const cell_t *params) TopMenuCallbacks *cb = new TopMenuCallbacks(func); - char *name, *cmdname; + char *name, *cmdname, *info_string; pContext->LocalToString(params[2], &name); pContext->LocalToString(params[6], &cmdname); + if (params[0] >= 8) + { + pContext->LocalToString(params[8], &info_string); + } + TopMenuObjectType obj_type = (TopMenuObjectType)params[3]; unsigned int object_id; - if ((object_id = pMenu->AddToMenu(name, + if ((object_id = pMenu->AddToMenu2(name, obj_type, cb, pContext->GetIdentity(), cmdname, params[7], - params[5])) == 0) + params[5], + info_string)) == 0) { delete cb; return 0; @@ -306,13 +321,38 @@ static cell_t DisplayTopMenu(IPluginContext *pContext, const cell_t *params) return pMenu->DisplayMenu(client, 0, (TopMenuPosition)params[3]); } +static cell_t GetTopMenuInfoString(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); + } + + const char *str; + if ((str = pMenu->GetObjectInfoString(params[2])) == NULL) + { + return pContext->ThrowNativeError("Invalid menu object %d", params[2]); + } + + char *buffer; + pContext->LocalToString(params[3], &buffer); + + return strncopy(buffer, str, params[4]); +} + sp_nativeinfo_t g_TopMenuNatives[] = { - {"AddToTopMenu", AddToTopMenu}, - {"CreateTopMenu", CreateTopMenu}, - {"DisplayTopMenu", DisplayTopMenu}, - {"LoadTopMenuConfig", LoadTopMenuConfig}, - {"RemoveFromTopMenu", RemoveFromTopMenu}, - {"FindTopMenuCategory", FindTopMenuCategory}, + {"AddToTopMenu", AddToTopMenu}, + {"CreateTopMenu", CreateTopMenu}, + {"DisplayTopMenu", DisplayTopMenu}, + {"LoadTopMenuConfig", LoadTopMenuConfig}, + {"RemoveFromTopMenu", RemoveFromTopMenu}, + {"FindTopMenuCategory", FindTopMenuCategory}, + {"GetTopMenuInfoString", GetTopMenuInfoString}, {NULL, NULL}, }; diff --git a/plugins/include/topmenus.inc b/plugins/include/topmenus.inc index a71192cf..e3dc823f 100644 --- a/plugins/include/topmenus.inc +++ b/plugins/include/topmenus.inc @@ -79,6 +79,14 @@ enum TopMenuAction * ITEMDRAW_DEFAULT. */ TopMenuAction_DrawOption = 3, + + /** + * Called when an object is being removed from the menu. + * This can be used to clean up data stored in the info string. + * + * INPUT : TopMenu Handle, object ID. + */ + TopMenuAction_RemoveObject = 4, }; /** @@ -175,6 +183,7 @@ native bool:LoadTopMenuConfig(Handle:topmenu, const String:file[], String:error[ * @param parent Parent object ID, or INVALID_TOPMENUOBJECT for none. * Items must have a category parent. * Categories must not have a parent. + * @param info_string Arbitrary storage (max 255 bytes). * @return A new TopMenuObject ID, or INVALID_TOPMENUOBJECT on * failure. * @error Invalid TopMenu Handle. @@ -185,7 +194,21 @@ native TopMenuObject:AddToTopMenu(Handle:topmenu, TopMenuHandler:handler, TopMenuObject:parent, const String:cmdname[]="", - flags=0); + flags=0, + const String:info_string[]=""); + +/** + * Retrieves the info string of a top menu item. + * + * @param topmenu TopMenu Handle. + * @param object TopMenuObject ID. + * @param buffer Buffer to store info string. + * @param maxlength Maximum size of info string. + * @return Number of bytes written, not including the + * null terminator. + * @error Invalid TopMenu Handle or TopMenuObject ID. + */ +native GetTopMenuInfoString(Handle:topmenu, TopMenuObject:parent, String:buffer[], maxlength); /** * Removes an object from a TopMenu. diff --git a/public/extensions/ITopMenus.h b/public/extensions/ITopMenus.h index 02f4bd35..97cba05f 100644 --- a/public/extensions/ITopMenus.h +++ b/public/extensions/ITopMenus.h @@ -43,7 +43,7 @@ */ #define SMINTERFACE_TOPMENUS_NAME "ITopMenus" -#define SMINTERFACE_TOPMENUS_VERSION 2 +#define SMINTERFACE_TOPMENUS_VERSION 3 namespace SourceMod { @@ -233,6 +233,41 @@ namespace SourceMod * @return Object ID of the category, or 0 if none. */ virtual unsigned int FindCategory(const char *name) =0; + + /** + * @brief Creates and adds an object type type to the top menu. + * + * @param name Unique, string name to give the object. + * @param type Object type. + * @param callbacks ITopMenuObjectCallbacks pointer. + * @param owner IdentityToken_t owner of the object. + * @param cmdname Command name used for override access checks. + * If NULL or empty, access will not be Checked. + * @param flags Default flag(s) to use for access checks. + * @param parent Parent object, or 0 if none. + * Currently, categories cannot have a parent, + * and items must have a category parent. + * @param info_string Optional info string to attach to the object. + * Only 255 bytes of the string (including null + * terminator) will be stored. + * @return An object ID, or 0 on failure. + */ + virtual unsigned int AddToMenu2(const char *name, + TopMenuObjectType type, + ITopMenuObjectCallbacks *callbacks, + IdentityToken_t *owner, + const char *cmdname, + FlagBits flags, + unsigned int parent, + const char *info_string) =0; + + /** + * @brief Returns an object's info string. + * + * @param object_id Object ID. + * @return Object's info string, or NULL if none. + */ + virtual const char *GetObjectInfoString(unsigned int object_id) =0; }; /**