ds@1990: /** ds@1990: * vim: set ts=4 : ds@1990: * ============================================================================= ds@1990: * SourceMod TopMenus Extension ds@1990: * Copyright (C) 2004-2008 AlliedModders LLC. All rights reserved. ds@1990: * ============================================================================= ds@1990: * ds@1990: * This program is free software; you can redistribute it and/or modify it under ds@1990: * the terms of the GNU General Public License, version 3.0, as published by the ds@1990: * Free Software Foundation. ds@1990: * ds@1990: * This program is distributed in the hope that it will be useful, but WITHOUT ds@1990: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS ds@1990: * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more ds@1990: * details. ds@1990: * ds@1990: * You should have received a copy of the GNU General Public License along with ds@1990: * this program. If not, see . ds@1990: * ds@1990: * As a special exception, AlliedModders LLC gives you permission to link the ds@1990: * code of this program (as well as its derivative works) to "Half-Life 2," the ds@1990: * "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software ds@1990: * by the Valve Corporation. You must obey the GNU General Public License in ds@1990: * all respects for all other code used. Additionally, AlliedModders LLC grants ds@1990: * this exception to all derivative works. AlliedModders LLC defines further ds@1990: * exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007), ds@1990: * or . ds@1990: * ds@1990: * Version: $Id$ ds@1990: */ ds@1990: ds@1990: #include ds@1990: #include ds@1990: #include "TopMenu.h" ds@1990: ds@1990: struct obj_by_name_t ds@1990: { ds@1990: unsigned int obj_index; ds@1990: char name[64]; ds@1990: }; ds@1990: ds@1990: int _SortObjectNamesDescending(const void *ptr1, const void *ptr2); ds@1990: unsigned int strncopy(char *dest, const char *src, size_t count); ds@1990: size_t UTIL_Format(char *buffer, size_t maxlength, const char *fmt, ...); ds@1990: ds@1990: TopMenu::TopMenu(ITopMenuObjectCallbacks *callbacks) ds@1990: { ds@1990: m_clients = NULL; ds@1990: m_SerialNo = 1; ds@1990: m_pTitle = callbacks; ds@1990: m_max_clients = 0; ds@1990: ds@1990: if (playerhelpers->IsServerActivated()) ds@1990: { ds@1990: CreatePlayers(playerhelpers->GetMaxClients()); ds@1990: } ds@1990: } ds@1990: ds@1990: TopMenu::~TopMenu() ds@1990: { ds@1990: /* Delete all categories */ ds@1990: while (m_Categories.size()) ds@1990: { ds@1990: RemoveFromMenu(m_Categories[0]->obj->object_id); ds@1990: } ds@1990: ds@1990: /* Remove all objects */ ds@1990: for (size_t i = 0; i < m_Objects.size(); i++) ds@1990: { ds@1990: assert(m_Objects[i]->is_free == true); ds@1990: delete m_Objects[i]; ds@1990: } ds@1990: ds@1990: m_pTitle->OnTopMenuObjectRemoved(this, 0); ds@1990: ds@1990: /* Delete all cached config entries */ ds@1990: for (size_t i = 0; i < m_Config.cats.size(); i++) ds@1990: { ds@1990: delete m_Config.cats[i]; ds@1990: } ds@1990: ds@1990: /* Sweep players */ ds@1990: for (size_t i = 0; i <= (size_t)m_max_clients; i++) ds@1990: { ds@1990: TearDownClient(&m_clients[i]); ds@1990: } ds@1990: delete [] m_clients; ds@1990: } ds@1990: ds@1990: unsigned int TopMenu::CalcMemUsage() ds@1990: { ds@1990: unsigned int size = sizeof(TopMenu); ds@1990: ds@1990: size += m_Config.strings.GetMemTable()->MemUsage(); ds@1990: size += (m_Config.cats.size() * sizeof(int)); pred@2323: size += (sizeof(topmenu_player_t) * (ABSOLUTE_PLAYER_LIMIT + 1)); ds@1990: size += (m_SortedCats.size() * sizeof(unsigned int)); ds@1990: size += (m_UnsortedCats.size() * sizeof(unsigned int)); ds@1990: size += (m_Categories.size() * (sizeof(topmenu_category_t *) + sizeof(topmenu_category_t))); ds@1990: size += (m_Objects.size() * (sizeof(topmenu_object_t *) + sizeof(topmenu_object_t))); ds@1990: size += m_ObjLookup.mem_usage(); ds@1990: ds@1990: for (size_t i = 0; i < m_Categories.size(); i++) ds@1990: { ds@1990: size += m_Categories[i]->obj_list.size() * sizeof(topmenu_object_t *); ds@1990: size += m_Categories[i]->sorted.size() * sizeof(topmenu_object_t *); ds@1990: size += m_Categories[i]->unsorted.size() * sizeof(topmenu_object_t *); ds@1990: } ds@1990: ds@1990: return size; ds@1990: } ds@1990: ds@1990: void TopMenu::OnClientConnected(int client) ds@1990: { ds@1990: if (m_clients == NULL) ds@1990: { ds@1990: return; ds@1990: } ds@1990: ds@1990: topmenu_player_t *player = &m_clients[client]; ds@1990: TearDownClient(player); ds@1990: } ds@1990: ds@1990: void TopMenu::OnClientDisconnected(int client) ds@1990: { ds@1990: if (m_clients == NULL) ds@1990: { ds@1990: return; ds@1990: } ds@1990: ds@1990: topmenu_player_t *player = &m_clients[client]; ds@1990: TearDownClient(player); ds@1990: } ds@1990: ds@1990: void TopMenu::OnServerActivated(int max_clients) ds@1990: { ds@1990: if (m_clients == NULL) ds@1990: { ds@1990: CreatePlayers(max_clients); ds@1990: } ds@1990: } ds@1990: ds@1990: unsigned int TopMenu::AddToMenu(const char *name, ds@1990: TopMenuObjectType type, ds@1990: ITopMenuObjectCallbacks *callbacks, ds@1990: IdentityToken_t *owner, ds@1990: const char *cmdname, ds@1990: FlagBits flags, ds@1990: unsigned int parent) ds@1990: { ds@1990: return AddToMenu2(name, type, callbacks, owner, cmdname, flags, parent, NULL); ds@1990: } ds@1990: ds@1990: unsigned int TopMenu::AddToMenu2(const char *name, ds@1990: TopMenuObjectType type, ds@1990: ITopMenuObjectCallbacks *callbacks, ds@1990: IdentityToken_t *owner, ds@1990: const char *cmdname, ds@1990: FlagBits flags, ds@1990: unsigned int parent, ds@1990: const char *info_string) ds@1990: { ds@1990: /* Sanity checks */ ds@1990: if (type == TopMenuObject_Category && parent != 0) ds@1990: { ds@1990: return 0; ds@1990: } ds@1990: else if (type == TopMenuObject_Item && parent == 0) ds@1990: { ds@1990: return 0; ds@1990: } ds@1990: else if (m_ObjLookup.retrieve(name) != NULL) ds@1990: { ds@1990: return 0; ds@1990: } ds@1990: else if (type != TopMenuObject_Item && type != TopMenuObject_Category) ds@1990: { ds@1990: return 0; ds@1990: } ds@1990: ds@1990: /* If we're adding an item, make sure the parent is valid, ds@1990: * and that the parent is a category. ds@1990: */ ds@1990: topmenu_object_t *parent_obj = NULL; ds@1990: topmenu_category_t *parent_cat = NULL; ds@1990: if (type == TopMenuObject_Item) ds@1990: { ds@1990: /* Check parent index. Note it will be >= 1 here. */ ds@1990: if (parent > m_Objects.size() || m_Objects[parent - 1]->is_free) ds@1990: { ds@1990: return 0; ds@1990: } ds@1990: parent_obj = m_Objects[parent - 1]; ds@1990: ds@1990: /* Find an equivalent pointer in the category array. */ ds@1990: for (size_t i = 0; i < m_Categories.size(); i++) ds@1990: { ds@1990: if (m_Categories[i]->obj == parent_obj) ds@1990: { ds@1990: parent_cat = m_Categories[i]; ds@1990: break; ds@1990: } ds@1990: } ds@1990: ds@1990: /* If none was found, leave. */ ds@1990: if (parent_cat == NULL) ds@1990: { ds@1990: return 0; ds@1990: } ds@1990: } ds@1990: ds@1990: /* Re-use an old object pointer if we can. */ ds@1990: topmenu_object_t *obj = NULL; ds@1990: for (size_t i = 0; i < m_Objects.size(); i++) ds@1990: { ds@1990: if (m_Objects[i]->is_free == true) ds@1990: { ds@1990: obj = m_Objects[i]; ds@1990: break; ds@1990: } ds@1990: } ds@1990: ds@1990: /* Otherwise, allocate a new one. */ ds@1990: if (obj == NULL) ds@1990: { ds@1990: obj = new topmenu_object_t; ds@1990: obj->object_id = ((unsigned int)m_Objects.size()) + 1; ds@1990: m_Objects.push_back(obj); ds@1990: } ds@1990: ds@1990: /* Initialize the object's properties. */ ds@1990: obj->callbacks = callbacks; ds@1990: obj->flags = flags; ds@1990: obj->owner = owner; ds@1990: obj->type = type; ds@1990: obj->is_free = false; ds@1990: obj->parent = parent_obj; ds@1990: strncopy(obj->name, name, sizeof(obj->name)); ds@1990: strncopy(obj->cmdname, cmdname ? cmdname : "", sizeof(obj->cmdname)); ds@1990: strncopy(obj->info, info_string ? info_string : "", sizeof(obj->info)); ds@1990: ds@1990: if (obj->type == TopMenuObject_Category) ds@1990: { ds@1990: /* Create a new category entry */ ds@1990: topmenu_category_t *cat = new topmenu_category_t; ds@1990: cat->obj = obj; ds@1990: cat->reorder = false; ds@1990: cat->serial = 1; ds@1990: ds@1990: /* Add it, then update our serial change number. */ dvander@2510: obj->cat_id = m_Categories.size(); ds@1990: m_Categories.push_back(cat); ds@1990: m_SerialNo++; ds@1990: ds@1990: /* Updating sorting info */ ds@1990: m_bCatsNeedResort = true; ds@1990: } ds@1990: else if (obj->type == TopMenuObject_Item) ds@1990: { ds@1990: /* Update the category, mark it as needing changes */ dvander@2510: obj->cat_id = 0; ds@1990: parent_cat->obj_list.push_back(obj); ds@1990: parent_cat->reorder = true; ds@1990: parent_cat->serial++; ds@1990: ds@1990: /* If the category just went from 0 to 1 items, mark it as ds@1990: * changed, so clients get the category drawn. ds@1990: */ ds@1990: if (parent_cat->obj_list.size() == 1) ds@1990: { ds@1990: m_SerialNo++; ds@1990: } ds@1990: } ds@1990: ds@1990: m_ObjLookup.insert(name, obj); ds@1990: ds@1990: return obj->object_id; ds@1990: } ds@1990: ds@1990: const char *TopMenu::GetObjectInfoString(unsigned int object_id) ds@1990: { ds@1990: if (object_id == 0 ds@1990: || object_id > m_Objects.size() ds@1990: || m_Objects[object_id - 1]->is_free) ds@1990: { ds@1990: return NULL; ds@1990: } ds@1990: ds@1990: topmenu_object_t *obj = m_Objects[object_id - 1]; ds@1990: ds@1990: return obj->info; ds@1990: } ds@1990: ds@1990: const char *TopMenu::GetObjectName(unsigned int object_id) ds@1990: { ds@1990: if (object_id == 0 ds@1990: || object_id > m_Objects.size() ds@1990: || m_Objects[object_id - 1]->is_free) ds@1990: { ds@1990: return NULL; ds@1990: } ds@1990: ds@1990: topmenu_object_t *obj = m_Objects[object_id - 1]; ds@1990: ds@1990: return obj->name; ds@1990: } ds@1990: ds@1990: void TopMenu::RemoveFromMenu(unsigned int object_id) ds@1990: { ds@1990: if (object_id == 0 ds@1990: || object_id > m_Objects.size() ds@1990: || m_Objects[object_id - 1]->is_free) ds@1990: { ds@1990: return; ds@1990: } ds@1990: ds@1990: topmenu_object_t *obj = m_Objects[object_id - 1]; ds@1990: ds@1990: m_ObjLookup.remove(obj->name); ds@1990: ds@1990: if (obj->type == TopMenuObject_Category) ds@1990: { ds@1990: /* Find it in the category list. */ ds@1990: for (size_t i = 0; i < m_Categories.size(); i++) ds@1990: { ds@1990: if (m_Categories[i]->obj == obj) ds@1990: { ds@1990: /* Mark all children as removed + free. Note we could ds@1990: * call into RemoveMenuItem() for this, but it'd be very ds@1990: * inefficient! ds@1990: */ ds@1990: topmenu_category_t *cat = m_Categories[i]; ds@1990: for (size_t j = 0; j < m_Categories[i]->obj_list.size(); j++) ds@1990: { dvander@2246: m_ObjLookup.remove(cat->obj_list[j]->name); ds@1990: cat->obj_list[j]->callbacks->OnTopMenuObjectRemoved(this, cat->obj_list[j]->object_id); ds@1990: cat->obj_list[j]->is_free = true; ds@1990: } ds@1990: ds@1990: /* Remove the category from the list, then delete it. */ ds@1990: m_Categories.erase(m_Categories.iterAt(i)); ds@1990: delete cat; ds@1990: break; ds@1990: } ds@1990: } ds@1990: ds@1990: /* Update the root as changed. */ ds@1990: m_SerialNo++; ds@1990: m_bCatsNeedResort = true; ds@1990: } ds@1990: else if (obj->type == TopMenuObject_Item) ds@1990: { ds@1990: /* Find the category this item is in. */ ds@1990: topmenu_category_t *parent_cat = NULL; ds@1990: for (size_t i = 0; i < m_Categories.size(); i++) ds@1990: { ds@1990: if (m_Categories[i]->obj == obj->parent) ds@1990: { ds@1990: parent_cat = m_Categories[i]; ds@1990: break; ds@1990: } ds@1990: } ds@1990: ds@1990: /* Erase it from the category's lists. */ ds@1990: if (parent_cat) ds@1990: { ds@1990: for (size_t i = 0; i < parent_cat->obj_list.size(); i++) ds@1990: { ds@1990: if (parent_cat->obj_list[i] == obj) ds@1990: { ds@1990: parent_cat->obj_list.erase(parent_cat->obj_list.iterAt(i)); ds@1990: ds@1990: /* If this category now has no items, mark root as changed ds@1990: * so clients won't get the category drawn anymore. ds@1990: */ ds@1990: if (parent_cat->obj_list.size() == 0) ds@1990: { ds@1990: m_SerialNo++; ds@1990: } ds@1990: break; ds@1990: } ds@1990: } ds@1990: ds@1990: /* Update the category as changed. */ ds@1990: parent_cat->reorder = true; ds@1990: parent_cat->serial++; ds@1990: } ds@1990: } ds@1990: ds@1990: /* The callbacks pointer is still valid, so fire away! */ ds@1990: obj->callbacks->OnTopMenuObjectRemoved(this, object_id); ds@1990: ds@1990: /* Finally, mark the object as free. */ ds@1990: obj->is_free = true; ds@1990: } ds@1990: ds@1990: bool TopMenu::DisplayMenu(int client, unsigned int hold_time, TopMenuPosition position) ds@1990: { ds@1990: if (m_clients == NULL) ds@1990: { ds@1990: return false; ds@1990: } ds@1990: ds@1990: IGamePlayer *pPlayer = playerhelpers->GetGamePlayer(client); ds@1990: if (!pPlayer->IsInGame()) ds@1990: { ds@1990: return false; ds@1990: } ds@1990: ds@1990: UpdateClientRoot(client, pPlayer); ds@1990: dvander@2510: /* This is unfortunate but it's the best solution. */ dvander@2510: for (size_t i = 0; i < m_Categories.size(); i++) dvander@2510: { dvander@2510: UpdateClientCategory(client, i, true); dvander@2510: } dvander@2510: ds@1990: topmenu_player_t *pClient = &m_clients[client]; ds@1990: if (pClient->root == NULL) ds@1990: { ds@1990: return false; ds@1990: } ds@1990: ds@1990: bool return_value = false; ds@1990: ds@1990: if (position == TopMenuPosition_LastCategory && ds@1990: pClient->last_category < m_Categories.size()) ds@1990: { ds@1990: return_value = DisplayCategory(client, pClient->last_category, hold_time, true); ds@1990: if (!return_value) ds@1990: { ds@1990: return_value = pClient->root->DisplayAtItem(client, hold_time, pClient->last_root_pos); ds@1990: } ds@1990: } ds@1990: else if (position == TopMenuPosition_LastRoot) ds@1990: { ds@1990: pClient->root->DisplayAtItem(client, hold_time, pClient->last_root_pos); ds@1990: } ds@1990: else if (position == TopMenuPosition_Start) ds@1990: { ds@1990: pClient->last_position = 0; ds@1990: pClient->last_category = 0; ds@1990: return_value = pClient->root->Display(client, hold_time); ds@1990: } ds@1990: ds@1990: return return_value; ds@1990: } ds@1990: ds@1990: bool TopMenu::DisplayCategory(int client, unsigned int category, unsigned int hold_time, bool last_position) ds@1990: { ds@1990: UpdateClientCategory(client, category); ds@1990: ds@1990: topmenu_player_t *pClient = &m_clients[client]; dvander@2510: if (category >= pClient->cat_count || pClient->cats[category].menu == NULL) ds@1990: { ds@1990: return false; ds@1990: } ds@1990: ds@1990: bool return_value = false; ds@1990: ds@1990: topmenu_player_category_t *player_cat = &(pClient->cats[category]); ds@1990: ds@1990: pClient->last_category = category; ds@1990: if (last_position) ds@1990: { ds@1990: return_value = player_cat->menu->DisplayAtItem(client, hold_time, pClient->last_position); ds@1990: } ds@1990: else ds@1990: { ds@1990: return_value = player_cat->menu->Display(client, hold_time); ds@1990: } ds@1990: ds@1990: return return_value; ds@1990: } ds@1990: ds@1990: void TopMenu::OnMenuSelect2(IBaseMenu *menu, int client, unsigned int item, unsigned int item_on_page) ds@1990: { ds@1990: const char *item_name = menu->GetItemInfo(item, NULL); ds@1990: if (!item_name) ds@1990: { ds@1990: return; ds@1990: } ds@1990: ds@1990: topmenu_object_t *obj; ds@1990: topmenu_player_t *pClient = &m_clients[client]; ds@1990: topmenu_object_t **pObject = m_ObjLookup.retrieve(item_name); ds@1990: if (pObject == NULL) ds@1990: { ds@1990: return; ds@1990: } ds@1990: ds@1990: obj = *pObject; ds@1990: ds@1990: /* We now have the object... what do we do with it? */ ds@1990: if (obj->type == TopMenuObject_Category) ds@1990: { ds@1990: /* If it's a category, the user wants to view it.. */ dvander@2510: assert(obj->cat_id < m_Categories.size()); dvander@2510: assert(m_Categories[obj->cat_id]->obj == obj); dvander@2510: pClient->last_root_pos = item_on_page; dvander@2510: if (!DisplayCategory(client, obj->cat_id, MENU_TIME_FOREVER, false)) ds@1990: { dvander@2510: /* If we can't display the category, re-display the root menu. dvander@2510: * This code should be dead as of bug 3256, which disables categories dvander@2510: * that cannot be displayed. dvander@2510: */ dvander@2510: DisplayMenu(client, MENU_TIME_FOREVER, TopMenuPosition_LastRoot); ds@1990: } ds@1990: } ds@1990: else ds@1990: { ds@1990: pClient->last_position = item_on_page; ds@1990: ds@1990: /* Re-check access in case this user had their credentials revoked */ ds@1990: if (obj->cmdname[0] != '\0' && !adminsys->CheckAccess(client, obj->cmdname, obj->flags, false)) ds@1990: { ds@1990: DisplayMenu(client, 0, TopMenuPosition_LastCategory); ds@1990: return; ds@1990: } ds@1990: ds@1990: /* Pass the information on to the callback */ ds@1990: obj->callbacks->OnTopMenuSelectOption(this, client, obj->object_id); ds@1990: } ds@1990: } ds@1990: ds@1990: void TopMenu::OnMenuDrawItem(IBaseMenu *menu, int client, unsigned int item, unsigned int &style) ds@1990: { ds@1990: const char *item_name = menu->GetItemInfo(item, NULL); ds@1990: if (!item_name) ds@1990: { ds@1990: return; ds@1990: } ds@1990: ds@1990: topmenu_object_t *obj; ds@1990: topmenu_object_t **pObject = m_ObjLookup.retrieve(item_name); ds@1990: if (pObject == NULL) ds@1990: { ds@1990: return; ds@1990: } ds@1990: ds@1990: obj = *pObject; ds@1990: dvander@2510: /* If the category has nothing to display, disable it. */ dvander@2510: if (obj->type == TopMenuObject_Category) dvander@2510: { dvander@2510: assert(obj->cat_id < m_Categories.size()); dvander@2510: assert(m_Categories[obj->cat_id]->obj == obj); dvander@2510: topmenu_player_t *pClient = &m_clients[client]; dvander@2510: if (obj->cat_id >= pClient->cat_count || pClient->cats[obj->cat_id].menu == NULL) dvander@2510: { dvander@2510: style = ITEMDRAW_IGNORE; dvander@2510: return; dvander@2510: } dvander@2510: } dvander@2510: ds@1990: style = obj->callbacks->OnTopMenuDrawOption(this, client, obj->object_id); ds@1990: if (style != ITEMDRAW_DEFAULT) ds@1990: { ds@1990: return; ds@1990: } ds@1990: ds@1990: if (obj->cmdname[0] == '\0') ds@1990: { ds@1990: return; ds@1990: } ds@1990: ds@1990: if (!adminsys->CheckAccess(client, obj->cmdname, obj->flags, false)) ds@1990: { ds@1990: style = ITEMDRAW_IGNORE; ds@1990: } ds@1990: } ds@1990: ds@1990: unsigned int TopMenu::OnMenuDisplayItem(IBaseMenu *menu, ds@1990: int client, ds@1990: IMenuPanel *panel, ds@1990: unsigned int item, ds@1990: const ItemDrawInfo &dr) ds@1990: { ds@1990: const char *item_name = menu->GetItemInfo(item, NULL); ds@1990: if (!item_name) ds@1990: { ds@1990: return 0; ds@1990: } ds@1990: ds@1990: topmenu_object_t *obj; ds@1990: topmenu_object_t **pObject = m_ObjLookup.retrieve(item_name); ds@1990: if (pObject == NULL) ds@1990: { ds@1990: return 0; ds@1990: } ds@1990: ds@1990: obj = *pObject; ds@1990: ds@1990: /* Ask the object to render the text for this client */ ds@1990: char renderbuf[64]; ds@1990: obj->callbacks->OnTopMenuDisplayOption(this, client, obj->object_id, renderbuf, sizeof(renderbuf)); ds@1990: ds@1990: /* Build the new draw info */ ds@1990: ItemDrawInfo new_dr = dr; ds@1990: new_dr.display = renderbuf; ds@1990: ds@1990: /* Man I love the menu API. Ask the panel to draw the item and give the position ds@1990: * back to Core's renderer. This way we don't have to worry about keeping the ds@1990: * render buffer static! ds@1990: */ ds@1990: return panel->DrawItem(new_dr); ds@1990: } ds@1990: ds@1990: void TopMenu::OnMenuCancel(IBaseMenu *menu, int client, MenuCancelReason reason) ds@1990: { ds@1990: if (reason == MenuCancel_ExitBack) ds@1990: { ds@1990: /* If the client chose exit back, they were on a category menu, and we can ds@1990: * now display the root menu from the last known position. ds@1990: */ ds@1990: DisplayMenu(client, 0, TopMenuPosition_LastRoot); ds@1990: } ds@1990: } ds@1990: ds@1990: void TopMenu::UpdateClientRoot(int client, IGamePlayer *pGamePlayer) ds@1990: { ds@1990: topmenu_player_t *pClient = &m_clients[client]; ds@1990: IGamePlayer *pPlayer = pGamePlayer ? pGamePlayer : playerhelpers->GetGamePlayer(client); ds@1990: ds@1990: /* Determine if an update is necessary */ ds@1990: bool is_update_needed = false; ds@1990: if (pClient->menu_serial != m_SerialNo) ds@1990: { ds@1990: is_update_needed = true; ds@1990: } ds@1990: else if (pPlayer->GetUserId() != pClient->user_id) ds@1990: { ds@1990: is_update_needed = true; ds@1990: } ds@1990: ds@1990: /* If no update is needed at the root level, just leave now */ ds@1990: if (!is_update_needed) ds@1990: { ds@1990: return; ds@1990: } ds@1990: ds@1990: /* First we need to flush the cache... */ ds@1990: TearDownClient(pClient); ds@1990: ds@1990: /* Now, rebuild the category list, but don't create menus */ ds@1990: if (m_Categories.size() == 0) ds@1990: { ds@1990: pClient->cat_count = 0; ds@1990: pClient->cats = NULL; ds@1990: } ds@1990: else ds@1990: { ds@1990: pClient->cat_count = (unsigned int)m_Categories.size(); ds@1990: pClient->cats = new topmenu_player_category_t[pClient->cat_count]; ds@1990: memset(pClient->cats, 0, sizeof(topmenu_player_category_t) * pClient->cat_count); ds@1990: } ds@1990: ds@1990: /* Re-sort the root categories if needed */ ds@1990: SortCategoriesIfNeeded(); ds@1990: ds@1990: /* Build the root menu */ ds@1990: IBaseMenu *root_menu = menus->GetDefaultStyle()->CreateMenu(this, myself->GetIdentity()); ds@1990: ds@1990: /* Add the sorted items */ ds@1990: for (size_t i = 0; i < m_SortedCats.size(); i++) ds@1990: { ds@1990: if (m_Categories[m_SortedCats[i]]->obj_list.size() == 0) ds@1990: { ds@1990: continue; ds@1990: } ds@1990: root_menu->AppendItem(m_Categories[m_SortedCats[i]]->obj->name, ItemDrawInfo("")); ds@1990: } ds@1990: ds@1990: /* Now we need to handle un-sorted items. This is slightly trickier, as we need to ds@1990: * pre-render each category name, and cache those names. Phew! ds@1990: */ ds@1990: if (m_UnsortedCats.size()) ds@1990: { ds@1990: obj_by_name_t *item_list = new obj_by_name_t[m_UnsortedCats.size()]; ds@1990: for (size_t i = 0; i < m_UnsortedCats.size(); i++) ds@1990: { ds@1990: obj_by_name_t *temp_obj = &item_list[i]; ds@1990: topmenu_object_t *obj = m_Categories[m_UnsortedCats[i]]->obj; ds@1990: obj->callbacks->OnTopMenuDisplayOption(this, ds@1990: client, ds@1990: obj->object_id, ds@1990: temp_obj->name, ds@1990: sizeof(temp_obj->name)); ds@1990: temp_obj->obj_index = m_UnsortedCats[i]; ds@1990: } ds@1990: ds@1990: /* Sort our temp list */ ds@1990: qsort(item_list, m_UnsortedCats.size(), sizeof(obj_by_name_t), _SortObjectNamesDescending); ds@1990: ds@1990: /* Add the new sorted categories */ ds@1990: for (size_t i = 0; i < m_UnsortedCats.size(); i++) ds@1990: { ds@1990: if (m_Categories[item_list[i].obj_index]->obj_list.size() == 0) ds@1990: { ds@1990: continue; ds@1990: } ds@1990: root_menu->AppendItem(m_Categories[item_list[i].obj_index]->obj->name, ItemDrawInfo("")); ds@1990: } ds@1990: ds@1990: delete [] item_list; ds@1990: } ds@1990: ds@1990: /* Set the menu's title */ ds@1990: char renderbuf[128]; ds@1990: m_pTitle->OnTopMenuDisplayTitle(this, client, 0, renderbuf, sizeof(renderbuf)); ds@1990: root_menu->SetDefaultTitle(renderbuf); ds@1990: ds@1990: /* The client is now fully updated */ ds@1990: pClient->root = root_menu; ds@1990: pClient->user_id = pPlayer->GetUserId(); ds@1990: pClient->menu_serial = m_SerialNo; ds@1990: pClient->last_position = 0; ds@1990: pClient->last_category = 0; ds@1990: pClient->last_root_pos = 0; ds@1990: } ds@1990: dvander@2510: void TopMenu::UpdateClientCategory(int client, unsigned int category, bool bSkipRootCheck) ds@1990: { dvander@2510: bool has_access = false; dvander@2510: ds@1990: /* Update the client's root menu just in case it needs it. This ds@1990: * will validate that we have both a valid client and a valid ds@1990: * category structure for that client. ds@1990: */ dvander@2510: if (!bSkipRootCheck) dvander@2510: { dvander@2510: UpdateClientRoot(client); dvander@2510: } ds@1990: ds@1990: /* Now it's guaranteed that our category tables will be usable */ ds@1990: topmenu_player_t *pClient = &m_clients[client]; ds@1990: topmenu_category_t *cat = m_Categories[category]; ds@1990: topmenu_player_category_t *player_cat = &(pClient->cats[category]); ds@1990: ds@1990: /* Does the category actually need updating? */ ds@1990: if (player_cat->serial == cat->serial) ds@1990: { ds@1990: return; ds@1990: } ds@1990: ds@1990: /* Destroy any existing menu */ ds@1990: if (player_cat->menu) ds@1990: { ds@1990: player_cat->menu->Destroy(); ds@1990: player_cat->menu = NULL; ds@1990: } ds@1990: ds@1990: if (pClient->last_category == category) ds@1990: { ds@1990: pClient->last_position = 0; ds@1990: } ds@1990: ds@1990: IBaseMenu *cat_menu = menus->GetDefaultStyle()->CreateMenu(this, myself->GetIdentity()); ds@1990: ds@1990: /* Categories get an "exit back" button */ ds@1990: cat_menu->SetMenuOptionFlags(cat_menu->GetMenuOptionFlags() | MENUFLAG_BUTTON_EXITBACK); ds@1990: ds@1990: /* Re-sort the category if needed */ ds@1990: SortCategoryIfNeeded(category); ds@1990: ds@1990: /* Build the menu with the sorted items first */ ds@1990: for (size_t i = 0; i < cat->sorted.size(); i++) ds@1990: { ds@1990: cat_menu->AppendItem(cat->sorted[i]->name, ItemDrawInfo("")); dvander@2510: if (!has_access && adminsys->CheckAccess(client, dvander@2510: cat->sorted[i]->cmdname, dvander@2510: cat->sorted[i]->flags, dvander@2510: false)) dvander@2510: { dvander@2510: has_access = true; dvander@2510: } ds@1990: } ds@1990: ds@1990: /* Now handle unsorted items */ ds@1990: if (cat->unsorted.size()) ds@1990: { ds@1990: /* Build a list of the item names */ ds@1990: obj_by_name_t *item_list = new obj_by_name_t[cat->unsorted.size()]; ds@1990: for (size_t i = 0; i < cat->unsorted.size(); i++) ds@1990: { ds@1990: obj_by_name_t *item = &item_list[i]; ds@1990: topmenu_object_t *obj = cat->unsorted[i]; ds@1990: obj->callbacks->OnTopMenuDisplayOption(this, ds@1990: client, ds@1990: obj->object_id, ds@1990: item->name, ds@1990: sizeof(item->name)); ds@1990: item->obj_index = (unsigned int)i; dvander@2510: if (!has_access && adminsys->CheckAccess(client, obj->cmdname, obj->flags, false)) dvander@2510: { dvander@2510: has_access = true; dvander@2510: } ds@1990: } ds@1990: dvander@2510: if (has_access) dvander@2510: { dvander@2510: /* Sort the names */ dvander@2510: qsort(item_list, dvander@2510: cat->unsorted.size(), dvander@2510: sizeof(obj_by_name_t), dvander@2510: _SortObjectNamesDescending); ds@1990: dvander@2510: /* Add to the menu */ dvander@2510: for (size_t i = 0; i < cat->unsorted.size(); i++) dvander@2510: { dvander@2510: cat_menu->AppendItem(cat->unsorted[item_list[i].obj_index]->name, ItemDrawInfo("")); dvander@2510: } ds@1990: } ds@1990: ds@1990: delete [] item_list; ds@1990: } ds@1990: dvander@2510: /* If the player has access to no items, don't draw a menu. */ dvander@2510: if (!has_access) dvander@2510: { dvander@2510: cat_menu->Destroy(); dvander@2510: player_cat->menu = NULL; dvander@2510: player_cat->serial = cat->serial; dvander@2510: return; dvander@2510: } dvander@2510: ds@1990: /* Set the menu's title */ ds@1990: char renderbuf[128]; ds@1990: cat->obj->callbacks->OnTopMenuDisplayTitle(this, ds@1990: client, ds@1990: cat->obj->object_id, ds@1990: renderbuf, ds@1990: sizeof(renderbuf)); ds@1990: cat_menu->SetDefaultTitle(renderbuf); ds@1990: ds@1990: /* We're done! */ ds@1990: player_cat->menu = cat_menu; ds@1990: player_cat->serial = cat->serial; ds@1990: } ds@1990: ds@1990: void TopMenu::SortCategoryIfNeeded(unsigned int category) ds@1990: { ds@1990: topmenu_category_t *cat = m_Categories[category]; ds@1990: if (!cat->reorder) ds@1990: { ds@1990: return; ds@1990: } ds@1990: ds@1990: cat->sorted.clear(); ds@1990: cat->unsorted.clear(); ds@1990: ds@1990: if (cat->obj_list.size() == 0) ds@1990: { ds@1990: cat->reorder = false; ds@1990: return; ds@1990: } ds@1990: ds@1990: CVector to_sort; ds@1990: for (size_t i = 0; i < cat->obj_list.size(); i++) ds@1990: { ds@1990: to_sort.push_back(i); ds@1990: } ds@1990: ds@1990: /* Find a matching category in the configs */ ds@1990: config_category_t *config_cat = NULL; ds@1990: for (size_t i = 0; i < m_Config.cats.size(); i++) ds@1990: { ds@1990: if (strcmp(m_Config.strings.GetString(m_Config.cats[i]->name), cat->obj->name) == 0) ds@1990: { ds@1990: config_cat = m_Config.cats[i]; ds@1990: break; ds@1990: } ds@1990: } ds@1990: ds@1990: /* If there is a matching category, build a pre-sorted item list */ ds@1990: if (config_cat != NULL) ds@1990: { ds@1990: /* Find matching objects in this category */ ds@1990: for (size_t i = 0; i < config_cat->commands.size(); i++) ds@1990: { ds@1990: const char *config_name = m_Config.strings.GetString(config_cat->commands[i]); ds@1990: for (size_t j = 0; j < to_sort.size(); j++) ds@1990: { ds@1990: if (strcmp(config_name, cat->obj_list[to_sort[j]]->name) == 0) ds@1990: { ds@1990: /* Place in the final list, then remove from the temporary list */ ds@1990: cat->sorted.push_back(cat->obj_list[to_sort[j]]); ds@1990: to_sort.erase(to_sort.iterAt(j)); ds@1990: break; ds@1990: } ds@1990: } ds@1990: } ds@1990: } ds@1990: ds@1990: /* Push any remaining items onto the unsorted list */ ds@1990: for (size_t i = 0; i < to_sort.size(); i++) ds@1990: { ds@1990: cat->unsorted.push_back(cat->obj_list[to_sort[i]]); ds@1990: } ds@1990: ds@1990: cat->reorder = false; ds@1990: } ds@1990: ds@1990: void TopMenu::SortCategoriesIfNeeded() ds@1990: { ds@1990: if (!m_bCatsNeedResort) ds@1990: { ds@1990: return; ds@1990: } ds@1990: ds@1990: /* Clear sort results */ ds@1990: m_SortedCats.clear(); ds@1990: m_UnsortedCats.clear(); ds@1990: ds@1990: if (m_Categories.size() == 0) ds@1990: { ds@1990: m_bCatsNeedResort = false; ds@1990: return; ds@1990: } ds@1990: ds@1990: CVector to_sort; ds@1990: for (unsigned int i = 0; i < (unsigned int)m_Categories.size(); i++) ds@1990: { ds@1990: to_sort.push_back(i); ds@1990: } ds@1990: ds@1990: /* If we have any predefined categories, add them in as they appear. */ ds@1990: for (size_t i= 0; i < m_Config.cats.size(); i++) ds@1990: { ds@1990: /* Find this category and map it in if we can */ ds@1990: for (size_t j = 0; j < to_sort.size(); j++) ds@1990: { ds@1990: if (strcmp(m_Config.strings.GetString(m_Config.cats[i]->name), ds@1990: m_Categories[to_sort[j]]->obj->name) == 0) ds@1990: { ds@1990: /* Add to the real list and remove from the temporary */ ds@1990: m_SortedCats.push_back(to_sort[j]); ds@1990: to_sort.erase(to_sort.iterAt(j)); ds@1990: break; ds@1990: } ds@1990: } ds@1990: } ds@1990: ds@1990: /* Push any remaining items onto the unsorted list */ ds@1990: for (size_t i = 0; i < to_sort.size(); i++) ds@1990: { ds@1990: m_UnsortedCats.push_back(to_sort[i]); ds@1990: } ds@1990: ds@1990: m_bCatsNeedResort = false; ds@1990: } ds@1990: ds@1990: void TopMenu::CreatePlayers(int max_clients) ds@1990: { ds@1990: m_max_clients = max_clients; pred@2323: m_clients = (topmenu_player_t *)malloc(sizeof(topmenu_player_t) * (ABSOLUTE_PLAYER_LIMIT + 1)); pred@2323: memset(m_clients, 0, sizeof(topmenu_player_t) * (ABSOLUTE_PLAYER_LIMIT + 1)); ds@1990: } ds@1990: ds@1990: void TopMenu::TearDownClient(topmenu_player_t *player) ds@1990: { ds@1990: if (player->cats != NULL) ds@1990: { ds@1990: for (unsigned int i = 0; i < player->cat_count; i++) ds@1990: { ds@1990: topmenu_player_category_t *player_cat = &(player->cats[i]); ds@1990: if (player_cat->menu != NULL) ds@1990: { ds@1990: player_cat->menu->Destroy(); ds@1990: } ds@1990: } ds@1990: delete [] player->cats; ds@1990: } ds@1990: ds@1990: if (player->root != NULL) ds@1990: { ds@1990: player->root->Destroy(); ds@1990: } ds@1990: ds@1990: memset(player, 0, sizeof(topmenu_player_t)); ds@1990: } ds@1990: ds@1990: bool TopMenu::LoadConfiguration(const char *file, char *error, size_t maxlength) ds@1990: { ds@1990: SMCError err; ds@1990: SMCStates states; ds@1990: ds@1990: if ((err = textparsers->ParseFile_SMC(file, this, &states)) ds@1990: != SMCError_Okay) ds@1990: { ds@1990: const char *err_string = textparsers->GetSMCErrorString(err); ds@1990: if (!err_string) ds@1990: { ds@1990: err_string = "Unknown"; ds@1990: } ds@1990: ds@1990: UTIL_Format(error, maxlength, "%s", err_string); ds@1990: ds@1990: return false; ds@1990: } ds@1990: ds@1990: return true; ds@1990: } ds@1990: ds@1990: bool TopMenu::OnIdentityRemoval(IdentityToken_t *owner) ds@1990: { ds@1990: /* First sweep the categories owned by us */ ds@1990: CVector obj_list; ds@1990: for (size_t i = 0; i < m_Categories.size(); i++) ds@1990: { ds@1990: if (m_Categories[i]->obj->owner == owner) ds@1990: { ds@1990: obj_list.push_back(m_Categories[i]->obj->object_id); ds@1990: } ds@1990: } ds@1990: ds@1990: for (size_t i = 0; i < obj_list.size(); i++) ds@1990: { ds@1990: RemoveFromMenu(obj_list[i]); ds@1990: } ds@1990: ds@1990: /* Now we can look for actual items */ ds@1990: for (size_t i = 0; i < m_Objects.size(); i++) ds@1990: { ds@1990: if (m_Objects[i]->is_free) ds@1990: { ds@1990: continue; ds@1990: } ds@1990: if (m_Objects[i]->owner == owner) ds@1990: { ds@1990: assert(m_Objects[i]->type != TopMenuObject_Category); ds@1990: RemoveFromMenu(m_Objects[i]->object_id); ds@1990: } ds@1990: } ds@1990: ds@1990: return true; ds@1990: } ds@1990: ds@1990: #define PARSE_STATE_NONE 0 ds@1990: #define PARSE_STATE_MAIN 1 ds@1990: #define PARSE_STATE_CATEGORY 2 ds@1990: unsigned int ignore_parse_level = 0; ds@1990: unsigned int current_parse_state = 0; ds@1990: config_category_t *cur_cat = NULL; ds@1990: ds@1990: void TopMenu::ReadSMC_ParseStart() ds@1990: { ds@1990: current_parse_state = PARSE_STATE_NONE; ds@1990: ignore_parse_level = 0; ds@1990: cur_cat = NULL; ds@1990: ds@1990: /* Reset the old config */ ds@1990: m_Config.strings.Reset(); ds@1990: for (size_t i = 0; i < m_Config.cats.size(); i++) ds@1990: { ds@1990: delete m_Config.cats[i]; ds@1990: } ds@1990: m_Config.cats.clear(); ds@1990: } ds@1990: ds@1990: SMCResult TopMenu::ReadSMC_NewSection(const SMCStates *states, const char *name) ds@1990: { ds@1990: if (ignore_parse_level) ds@1990: { ds@1990: ignore_parse_level++; ds@1990: } ds@1990: else ds@1990: { ds@1990: if (current_parse_state == PARSE_STATE_NONE) ds@1990: { ds@1990: if (strcmp(name, "Menu") == 0) ds@1990: { ds@1990: current_parse_state = PARSE_STATE_MAIN; ds@1990: } ds@1990: else ds@1990: { ds@1990: ignore_parse_level = 1; ds@1990: } ds@1990: } ds@1990: else if (current_parse_state == PARSE_STATE_MAIN) ds@1990: { ds@1990: cur_cat = new config_category_t; ds@1990: cur_cat->name = m_Config.strings.AddString(name); ds@1990: m_Config.cats.push_back(cur_cat); ds@1990: current_parse_state = PARSE_STATE_CATEGORY; ds@1990: } ds@1990: else ds@1990: { ds@1990: ignore_parse_level = 1; ds@1990: } ds@1990: } ds@1990: ds@1990: return SMCResult_Continue; ds@1990: } ds@1990: ds@1990: SMCResult TopMenu::ReadSMC_KeyValue(const SMCStates *states, const char *key, const char *value) ds@1990: { ds@1990: if (ignore_parse_level > 0 ds@1990: || current_parse_state != PARSE_STATE_CATEGORY ds@1990: || cur_cat == NULL) ds@1990: { ds@1990: return SMCResult_Continue; ds@1990: } ds@1990: ds@1990: if (strcmp(key, "item") == 0) ds@1990: { ds@1990: cur_cat->commands.push_back(m_Config.strings.AddString(value)); ds@1990: } ds@1990: ds@1990: return SMCResult_Continue; ds@1990: } ds@1990: ds@1990: SMCResult TopMenu::ReadSMC_LeavingSection(const SMCStates *states) ds@1990: { ds@1990: if (ignore_parse_level) ds@1990: { ds@1990: ignore_parse_level--; ds@1990: } ds@1990: else ds@1990: { ds@1990: if (current_parse_state == PARSE_STATE_CATEGORY) ds@1990: { ds@1990: cur_cat = NULL; ds@1990: current_parse_state = PARSE_STATE_MAIN; ds@1990: } ds@1990: else if (current_parse_state == PARSE_STATE_MAIN) ds@1990: { ds@1990: current_parse_state = PARSE_STATE_NONE; ds@1990: } ds@1990: } ds@1990: ds@1990: return SMCResult_Continue; ds@1990: } ds@1990: ds@1990: unsigned int TopMenu::FindCategory(const char *name) ds@1990: { ds@1990: topmenu_object_t **p_obj = m_ObjLookup.retrieve(name); ds@1990: if (!p_obj) ds@1990: { ds@1990: return 0; ds@1990: } ds@1990: ds@1990: topmenu_object_t *obj = *p_obj; ds@1990: if (obj->type != TopMenuObject_Category) ds@1990: { ds@1990: return 0; ds@1990: } ds@1990: ds@1990: return obj->object_id; ds@1990: } ds@1990: pred@2323: void TopMenu::OnMaxPlayersChanged( int newvalue ) pred@2323: { pred@2323: m_max_clients = newvalue; pred@2323: } pred@2323: ds@1990: int _SortObjectNamesDescending(const void *ptr1, const void *ptr2) ds@1990: { ds@1990: obj_by_name_t *obj1 = (obj_by_name_t *)ptr1; ds@1990: obj_by_name_t *obj2 = (obj_by_name_t *)ptr2; ds@1990: return strcmp(obj1->name, obj2->name); ds@1990: } ds@1990: ds@1990: unsigned int strncopy(char *dest, const char *src, size_t count) ds@1990: { ds@1990: if (!count) ds@1990: { ds@1990: return 0; ds@1990: } ds@1990: ds@1990: char *start = dest; ds@1990: while ((*src) && (--count)) ds@1990: { ds@1990: *dest++ = *src++; ds@1990: } ds@1990: *dest = '\0'; ds@1990: ds@1990: return (dest - start); ds@1990: } ds@1990: ds@1990: size_t UTIL_Format(char *buffer, size_t maxlength, const char *fmt, ...) ds@1990: { ds@1990: va_list ap; ds@1990: va_start(ap, fmt); ds@1990: size_t len = vsnprintf(buffer, maxlength, fmt, ap); ds@1990: va_end(ap); ds@1990: ds@1990: if (len >= maxlength) ds@1990: { ds@1990: buffer[maxlength - 1] = '\0'; ds@1990: return (maxlength - 1); ds@1990: } ds@1990: else ds@1990: { ds@1990: return len; ds@1990: } ds@1990: }