Add ability to disable TopMenu Title Caching (bug 6034, r=dvander).
Add ability to disable TopMenu Title Caching (
bug 6034, r=dvander).
1.1 --- a/extensions/topmenus/TopMenu.cpp Fri May 09 10:36:58 2014 -0700
1.2 +++ b/extensions/topmenus/TopMenu.cpp Fri May 09 12:30:52 2014 -0700
1.3 @@ -49,6 +49,7 @@
1.4 m_SerialNo = 1;
1.5 m_pTitle = callbacks;
1.6 m_max_clients = 0;
1.7 + m_bCacheTitles = true;
1.8
1.9 if (playerhelpers->IsServerActivated())
1.10 {
1.11 @@ -421,6 +422,13 @@
1.12 return false;
1.13 }
1.14
1.15 + if (!m_bCacheTitles)
1.16 + {
1.17 + char renderbuf[128];
1.18 + m_pTitle->OnTopMenuDisplayTitle(this, client, 0, renderbuf, sizeof(renderbuf));
1.19 + pClient->root->SetDefaultTitle(renderbuf);
1.20 + }
1.21 +
1.22 bool return_value = false;
1.23
1.24 if (position == TopMenuPosition_LastCategory &&
1.25 @@ -460,6 +468,18 @@
1.26
1.27 topmenu_player_category_t *player_cat = &(pClient->cats[category]);
1.28
1.29 + // Refresh the title if the topmenu wants that.
1.30 + if (!m_bCacheTitles)
1.31 + {
1.32 + char renderbuf[128];
1.33 + m_Categories[category]->obj->callbacks->OnTopMenuDisplayTitle(this,
1.34 + client,
1.35 + m_Categories[category]->obj->object_id,
1.36 + renderbuf,
1.37 + sizeof(renderbuf));
1.38 + player_cat->menu->SetDefaultTitle(renderbuf);
1.39 + }
1.40 +
1.41 pClient->last_category = category;
1.42 if (last_position)
1.43 {
1.44 @@ -473,6 +493,11 @@
1.45 return return_value;
1.46 }
1.47
1.48 +void TopMenu::SetTitleCaching(bool cache_titles)
1.49 +{
1.50 + m_bCacheTitles = cache_titles;
1.51 +}
1.52 +
1.53 void TopMenu::OnMenuSelect2(IBaseMenu *menu, int client, unsigned int item, unsigned int item_on_page)
1.54 {
1.55 const char *item_name = menu->GetItemInfo(item, NULL);
1.56 @@ -691,9 +716,12 @@
1.57 }
1.58
1.59 /* Set the menu's title */
1.60 - char renderbuf[128];
1.61 - m_pTitle->OnTopMenuDisplayTitle(this, client, 0, renderbuf, sizeof(renderbuf));
1.62 - root_menu->SetDefaultTitle(renderbuf);
1.63 + if (m_bCacheTitles)
1.64 + {
1.65 + char renderbuf[128];
1.66 + m_pTitle->OnTopMenuDisplayTitle(this, client, 0, renderbuf, sizeof(renderbuf));
1.67 + root_menu->SetDefaultTitle(renderbuf);
1.68 + }
1.69
1.70 /* The client is now fully updated */
1.71 pClient->root = root_menu;
1.72 @@ -810,13 +838,16 @@
1.73 }
1.74
1.75 /* Set the menu's title */
1.76 - char renderbuf[128];
1.77 - cat->obj->callbacks->OnTopMenuDisplayTitle(this,
1.78 - client,
1.79 - cat->obj->object_id,
1.80 - renderbuf,
1.81 - sizeof(renderbuf));
1.82 - cat_menu->SetDefaultTitle(renderbuf);
1.83 + if (m_bCacheTitles)
1.84 + {
1.85 + char renderbuf[128];
1.86 + cat->obj->callbacks->OnTopMenuDisplayTitle(this,
1.87 + client,
1.88 + cat->obj->object_id,
1.89 + renderbuf,
1.90 + sizeof(renderbuf));
1.91 + cat_menu->SetDefaultTitle(renderbuf);
1.92 + }
1.93
1.94 /* We're done! */
1.95 player_cat->menu = cat_menu;
2.1 --- a/extensions/topmenus/TopMenu.h Fri May 09 10:36:58 2014 -0700
2.2 +++ b/extensions/topmenus/TopMenu.h Fri May 09 12:30:52 2014 -0700
2.3 @@ -154,6 +154,7 @@
2.4 SMCResult ReadSMC_LeavingSection(const SMCStates *states);
2.5 public:
2.6 unsigned int CalcMemUsage();
2.7 + void SetTitleCaching(bool cache_titles);
2.8 private:
2.9 void SortCategoriesIfNeeded();
2.10 void SortCategoryIfNeeded(unsigned int category);
2.11 @@ -181,6 +182,7 @@
2.12 ITopMenuObjectCallbacks *m_pTitle; /* Title callbacks */
2.13 int m_max_clients; /* Maximum number of clients */
2.14 bool m_bCatsNeedResort; /* True if categories need a resort */
2.15 + bool m_bCacheTitles; /* True if the categorie titles should be cached */
2.16 };
2.17
2.18 unsigned int strncopy(char *dest, const char *src, size_t count);
3.1 --- a/extensions/topmenus/smn_topmenus.cpp Fri May 09 10:36:58 2014 -0700
3.2 +++ b/extensions/topmenus/smn_topmenus.cpp Fri May 09 12:30:52 2014 -0700
3.3 @@ -374,6 +374,23 @@
3.4 return strncopy(buffer, str, params[4]);
3.5 }
3.6
3.7 +static cell_t SetTopMenuTitleCaching(IPluginContext *pContext, const cell_t *params)
3.8 +{
3.9 + HandleError err;
3.10 + TopMenu *pMenu;
3.11 + HandleSecurity sec(pContext->GetIdentity(), myself->GetIdentity());
3.12 +
3.13 + if ((err = handlesys->ReadHandle(params[1], hTopMenuType, &sec, (void **)&pMenu))
3.14 + != HandleError_None)
3.15 + {
3.16 + return pContext->ThrowNativeError("Invalid Handle %x (error: %d)", params[1], err);
3.17 + }
3.18 +
3.19 + bool cache_titles = params[2]==1?true:false;
3.20 + pMenu->SetTitleCaching(cache_titles);
3.21 + return 0;
3.22 +}
3.23 +
3.24 sp_nativeinfo_t g_TopMenuNatives[] =
3.25 {
3.26 {"AddToTopMenu", AddToTopMenu},
3.27 @@ -384,5 +401,6 @@
3.28 {"FindTopMenuCategory", FindTopMenuCategory},
3.29 {"GetTopMenuInfoString", GetTopMenuInfoString},
3.30 {"GetTopMenuObjName", GetTopMenuName},
3.31 + {"SetTopMenuTitleCaching", SetTopMenuTitleCaching},
3.32 {NULL, NULL},
3.33 };
4.1 --- a/plugins/include/topmenus.inc Fri May 09 10:36:58 2014 -0700
4.2 +++ b/plugins/include/topmenus.inc Fri May 09 12:30:52 2014 -0700
4.3 @@ -259,6 +259,17 @@
4.4 native TopMenuObject:FindTopMenuCategory(Handle:topmenu, const String:name[]);
4.5
4.6 /**
4.7 + * Change the menu title caching behaviour of the TopMenu. By default the titles are cached to reduce overhead.
4.8 + * If you need dynamic menu titles, which can change everytime the menu is displayed to a user, set this to false.
4.9 + *
4.10 + * @param topmenu TopMenu Handle.
4.11 + * @param cache_titles Cache the menu titles and don't call the handler with TopMenuAction_DisplayTitle everytime the menu is drawn?
4.12 + * @noreturn
4.13 + * @error Invalid TopMenu Handle
4.14 + */
4.15 +native SetTopMenuTitleCaching(Handle:topmenu, bool:cache_titles);
4.16 +
4.17 +/**
4.18 * Do not edit below this line!
4.19 */
4.20 public Extension:__ext_topmenus =
4.21 @@ -286,5 +297,6 @@
4.22 MarkNativeAsOptional("RemoveFromTopMenu");
4.23 MarkNativeAsOptional("DisplayTopMenu");
4.24 MarkNativeAsOptional("FindTopMenuCategory");
4.25 + MarkNativeAsOptional("SetTopMenuTitleCaching");
4.26 }
4.27 #endif