3 * =============================================================================
4 * SourceMod TopMenus Extension
5 * Copyright (C) 2004-2008 AlliedModders LLC. All rights reserved.
6 * =============================================================================
8 * This program is free software; you can redistribute it and/or modify it under
9 * the terms of the GNU General Public License, version 3.0, as published by the
10 * Free Software Foundation.
12 * This program is distributed in the hope that it will be useful, but WITHOUT
13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
17 * You should have received a copy of the GNU General Public License along with
18 * this program. If not, see <http://www.gnu.org/licenses/>.
20 * As a special exception, AlliedModders LLC gives you permission to link the
21 * code of this program (as well as its derivative works) to "Half-Life 2," the
22 * "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
23 * by the Valve Corporation. You must obey the GNU General Public License in
24 * all respects for all other code used. Additionally, AlliedModders LLC grants
25 * this exception to all derivative works. AlliedModders LLC defines further
26 * exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
27 * or <http://www.sourcemod.net/license.php>.
38 unsigned int obj_index;
42 int _SortObjectNamesDescending(const void *ptr1, const void *ptr2);
43 unsigned int strncopy(char *dest, const char *src, size_t count);
44 size_t UTIL_Format(char *buffer, size_t maxlength, const char *fmt, ...);
46 TopMenu::TopMenu(ITopMenuObjectCallbacks *callbacks)
53 if (playerhelpers->IsServerActivated())
55 CreatePlayers(playerhelpers->GetMaxClients());
61 /* Delete all categories */
62 while (m_Categories.size())
64 RemoveFromMenu(m_Categories[0]->obj->object_id);
67 /* Remove all objects */
68 for (size_t i = 0; i < m_Objects.size(); i++)
70 assert(m_Objects[i]->is_free == true);
74 m_pTitle->OnTopMenuObjectRemoved(this, 0);
76 /* Delete all cached config entries */
77 for (size_t i = 0; i < m_Config.cats.size(); i++)
79 delete m_Config.cats[i];
83 for (size_t i = 0; i <= (size_t)m_max_clients; i++)
85 TearDownClient(&m_clients[i]);
90 unsigned int TopMenu::CalcMemUsage()
92 unsigned int size = sizeof(TopMenu);
94 size += m_Config.strings.GetMemTable()->MemUsage();
95 size += (m_Config.cats.size() * sizeof(int));
96 size += (sizeof(topmenu_player_t) * (ABSOLUTE_PLAYER_LIMIT + 1));
97 size += (m_SortedCats.size() * sizeof(unsigned int));
98 size += (m_UnsortedCats.size() * sizeof(unsigned int));
99 size += (m_Categories.size() * (sizeof(topmenu_category_t *) + sizeof(topmenu_category_t)));
100 size += (m_Objects.size() * (sizeof(topmenu_object_t *) + sizeof(topmenu_object_t)));
101 size += m_ObjLookup.mem_usage();
103 for (size_t i = 0; i < m_Categories.size(); i++)
105 size += m_Categories[i]->obj_list.size() * sizeof(topmenu_object_t *);
106 size += m_Categories[i]->sorted.size() * sizeof(topmenu_object_t *);
107 size += m_Categories[i]->unsorted.size() * sizeof(topmenu_object_t *);
113 void TopMenu::OnClientConnected(int client)
115 if (m_clients == NULL)
120 topmenu_player_t *player = &m_clients[client];
121 TearDownClient(player);
124 void TopMenu::OnClientDisconnected(int client)
126 if (m_clients == NULL)
131 topmenu_player_t *player = &m_clients[client];
132 TearDownClient(player);
135 void TopMenu::OnServerActivated(int max_clients)
137 if (m_clients == NULL)
139 CreatePlayers(max_clients);
143 unsigned int TopMenu::AddToMenu(const char *name,
144 TopMenuObjectType type,
145 ITopMenuObjectCallbacks *callbacks,
146 IdentityToken_t *owner,
151 return AddToMenu2(name, type, callbacks, owner, cmdname, flags, parent, NULL);
154 unsigned int TopMenu::AddToMenu2(const char *name,
155 TopMenuObjectType type,
156 ITopMenuObjectCallbacks *callbacks,
157 IdentityToken_t *owner,
161 const char *info_string)
164 if (type == TopMenuObject_Category && parent != 0)
168 else if (type == TopMenuObject_Item && parent == 0)
172 else if (m_ObjLookup.retrieve(name) != NULL)
176 else if (type != TopMenuObject_Item && type != TopMenuObject_Category)
181 /* If we're adding an item, make sure the parent is valid,
182 * and that the parent is a category.
184 topmenu_object_t *parent_obj = NULL;
185 topmenu_category_t *parent_cat = NULL;
186 if (type == TopMenuObject_Item)
188 /* Check parent index. Note it will be >= 1 here. */
189 if (parent > m_Objects.size() || m_Objects[parent - 1]->is_free)
193 parent_obj = m_Objects[parent - 1];
195 /* Find an equivalent pointer in the category array. */
196 for (size_t i = 0; i < m_Categories.size(); i++)
198 if (m_Categories[i]->obj == parent_obj)
200 parent_cat = m_Categories[i];
205 /* If none was found, leave. */
206 if (parent_cat == NULL)
212 /* Re-use an old object pointer if we can. */
213 topmenu_object_t *obj = NULL;
214 for (size_t i = 0; i < m_Objects.size(); i++)
216 if (m_Objects[i]->is_free == true)
223 /* Otherwise, allocate a new one. */
226 obj = new topmenu_object_t;
227 obj->object_id = ((unsigned int)m_Objects.size()) + 1;
228 m_Objects.push_back(obj);
231 /* Initialize the object's properties. */
232 obj->callbacks = callbacks;
236 obj->is_free = false;
237 obj->parent = parent_obj;
238 strncopy(obj->name, name, sizeof(obj->name));
239 strncopy(obj->cmdname, cmdname ? cmdname : "", sizeof(obj->cmdname));
240 strncopy(obj->info, info_string ? info_string : "", sizeof(obj->info));
242 if (obj->type == TopMenuObject_Category)
244 /* Create a new category entry */
245 topmenu_category_t *cat = new topmenu_category_t;
247 cat->reorder = false;
250 /* Add it, then update our serial change number. */
251 m_Categories.push_back(cat);
254 /* Updating sorting info */
255 m_bCatsNeedResort = true;
257 else if (obj->type == TopMenuObject_Item)
259 /* Update the category, mark it as needing changes */
260 parent_cat->obj_list.push_back(obj);
261 parent_cat->reorder = true;
262 parent_cat->serial++;
264 /* If the category just went from 0 to 1 items, mark it as
265 * changed, so clients get the category drawn.
267 if (parent_cat->obj_list.size() == 1)
273 m_ObjLookup.insert(name, obj);
275 return obj->object_id;
278 const char *TopMenu::GetObjectInfoString(unsigned int object_id)
281 || object_id > m_Objects.size()
282 || m_Objects[object_id - 1]->is_free)
287 topmenu_object_t *obj = m_Objects[object_id - 1];
292 const char *TopMenu::GetObjectName(unsigned int object_id)
295 || object_id > m_Objects.size()
296 || m_Objects[object_id - 1]->is_free)
301 topmenu_object_t *obj = m_Objects[object_id - 1];
306 void TopMenu::RemoveFromMenu(unsigned int object_id)
309 || object_id > m_Objects.size()
310 || m_Objects[object_id - 1]->is_free)
315 topmenu_object_t *obj = m_Objects[object_id - 1];
317 m_ObjLookup.remove(obj->name);
319 if (obj->type == TopMenuObject_Category)
321 /* Find it in the category list. */
322 for (size_t i = 0; i < m_Categories.size(); i++)
324 if (m_Categories[i]->obj == obj)
326 /* Mark all children as removed + free. Note we could
327 * call into RemoveMenuItem() for this, but it'd be very
330 topmenu_category_t *cat = m_Categories[i];
331 for (size_t j = 0; j < m_Categories[i]->obj_list.size(); j++)
333 m_ObjLookup.remove(cat->obj_list[j]->name);
334 cat->obj_list[j]->callbacks->OnTopMenuObjectRemoved(this, cat->obj_list[j]->object_id);
335 cat->obj_list[j]->is_free = true;
338 /* Remove the category from the list, then delete it. */
339 m_Categories.erase(m_Categories.iterAt(i));
345 /* Update the root as changed. */
347 m_bCatsNeedResort = true;
349 else if (obj->type == TopMenuObject_Item)
351 /* Find the category this item is in. */
352 topmenu_category_t *parent_cat = NULL;
353 for (size_t i = 0; i < m_Categories.size(); i++)
355 if (m_Categories[i]->obj == obj->parent)
357 parent_cat = m_Categories[i];
362 /* Erase it from the category's lists. */
365 for (size_t i = 0; i < parent_cat->obj_list.size(); i++)
367 if (parent_cat->obj_list[i] == obj)
369 parent_cat->obj_list.erase(parent_cat->obj_list.iterAt(i));
371 /* If this category now has no items, mark root as changed
372 * so clients won't get the category drawn anymore.
374 if (parent_cat->obj_list.size() == 0)
382 /* Update the category as changed. */
383 parent_cat->reorder = true;
384 parent_cat->serial++;
388 /* The callbacks pointer is still valid, so fire away! */
389 obj->callbacks->OnTopMenuObjectRemoved(this, object_id);
391 /* Finally, mark the object as free. */
395 bool TopMenu::DisplayMenu(int client, unsigned int hold_time, TopMenuPosition position)
397 if (m_clients == NULL)
402 IGamePlayer *pPlayer = playerhelpers->GetGamePlayer(client);
403 if (!pPlayer->IsInGame())
408 UpdateClientRoot(client, pPlayer);
410 topmenu_player_t *pClient = &m_clients[client];
411 if (pClient->root == NULL)
416 bool return_value = false;
418 if (position == TopMenuPosition_LastCategory &&
419 pClient->last_category < m_Categories.size())
421 return_value = DisplayCategory(client, pClient->last_category, hold_time, true);
424 return_value = pClient->root->DisplayAtItem(client, hold_time, pClient->last_root_pos);
427 else if (position == TopMenuPosition_LastRoot)
429 pClient->root->DisplayAtItem(client, hold_time, pClient->last_root_pos);
431 else if (position == TopMenuPosition_Start)
433 pClient->last_position = 0;
434 pClient->last_category = 0;
435 return_value = pClient->root->Display(client, hold_time);
441 bool TopMenu::DisplayCategory(int client, unsigned int category, unsigned int hold_time, bool last_position)
443 UpdateClientCategory(client, category);
445 topmenu_player_t *pClient = &m_clients[client];
446 if (category >= pClient->cat_count
447 || pClient->cats[category].menu == NULL)
452 bool return_value = false;
454 topmenu_player_category_t *player_cat = &(pClient->cats[category]);
456 pClient->last_category = category;
459 return_value = player_cat->menu->DisplayAtItem(client, hold_time, pClient->last_position);
463 return_value = player_cat->menu->Display(client, hold_time);
469 void TopMenu::OnMenuSelect2(IBaseMenu *menu, int client, unsigned int item, unsigned int item_on_page)
471 const char *item_name = menu->GetItemInfo(item, NULL);
477 topmenu_object_t *obj;
478 topmenu_player_t *pClient = &m_clients[client];
479 topmenu_object_t **pObject = m_ObjLookup.retrieve(item_name);
487 /* We now have the object... what do we do with it? */
488 if (obj->type == TopMenuObject_Category)
490 /* If it's a category, the user wants to view it.. */
491 for (size_t i = 0; i < m_Categories.size(); i++)
493 if (m_Categories[i]->obj == obj)
495 pClient->last_root_pos = item_on_page;
496 if (!DisplayCategory(client, (unsigned int)i, MENU_TIME_FOREVER, false))
498 /* If we can't display the category, re-display the root menu */
499 DisplayMenu(client, MENU_TIME_FOREVER, TopMenuPosition_LastRoot);
507 pClient->last_position = item_on_page;
509 /* Re-check access in case this user had their credentials revoked */
510 if (obj->cmdname[0] != '\0' && !adminsys->CheckAccess(client, obj->cmdname, obj->flags, false))
512 DisplayMenu(client, 0, TopMenuPosition_LastCategory);
516 /* Pass the information on to the callback */
517 obj->callbacks->OnTopMenuSelectOption(this, client, obj->object_id);
521 void TopMenu::OnMenuDrawItem(IBaseMenu *menu, int client, unsigned int item, unsigned int &style)
523 const char *item_name = menu->GetItemInfo(item, NULL);
529 topmenu_object_t *obj;
530 topmenu_object_t **pObject = m_ObjLookup.retrieve(item_name);
538 style = obj->callbacks->OnTopMenuDrawOption(this, client, obj->object_id);
539 if (style != ITEMDRAW_DEFAULT)
544 if (obj->cmdname[0] == '\0')
549 if (!adminsys->CheckAccess(client, obj->cmdname, obj->flags, false))
551 style = ITEMDRAW_IGNORE;
555 unsigned int TopMenu::OnMenuDisplayItem(IBaseMenu *menu,
559 const ItemDrawInfo &dr)
561 const char *item_name = menu->GetItemInfo(item, NULL);
567 topmenu_object_t *obj;
568 topmenu_object_t **pObject = m_ObjLookup.retrieve(item_name);
576 /* Ask the object to render the text for this client */
578 obj->callbacks->OnTopMenuDisplayOption(this, client, obj->object_id, renderbuf, sizeof(renderbuf));
580 /* Build the new draw info */
581 ItemDrawInfo new_dr = dr;
582 new_dr.display = renderbuf;
584 /* Man I love the menu API. Ask the panel to draw the item and give the position
585 * back to Core's renderer. This way we don't have to worry about keeping the
586 * render buffer static!
588 return panel->DrawItem(new_dr);
591 void TopMenu::OnMenuCancel(IBaseMenu *menu, int client, MenuCancelReason reason)
593 if (reason == MenuCancel_ExitBack)
595 /* If the client chose exit back, they were on a category menu, and we can
596 * now display the root menu from the last known position.
598 DisplayMenu(client, 0, TopMenuPosition_LastRoot);
602 void TopMenu::UpdateClientRoot(int client, IGamePlayer *pGamePlayer)
604 topmenu_player_t *pClient = &m_clients[client];
605 IGamePlayer *pPlayer = pGamePlayer ? pGamePlayer : playerhelpers->GetGamePlayer(client);
607 /* Determine if an update is necessary */
608 bool is_update_needed = false;
609 if (pClient->menu_serial != m_SerialNo)
611 is_update_needed = true;
613 else if (pPlayer->GetUserId() != pClient->user_id)
615 is_update_needed = true;
618 /* If no update is needed at the root level, just leave now */
619 if (!is_update_needed)
624 /* First we need to flush the cache... */
625 TearDownClient(pClient);
627 /* Now, rebuild the category list, but don't create menus */
628 if (m_Categories.size() == 0)
630 pClient->cat_count = 0;
631 pClient->cats = NULL;
635 pClient->cat_count = (unsigned int)m_Categories.size();
636 pClient->cats = new topmenu_player_category_t[pClient->cat_count];
637 memset(pClient->cats, 0, sizeof(topmenu_player_category_t) * pClient->cat_count);
640 /* Re-sort the root categories if needed */
641 SortCategoriesIfNeeded();
643 /* Build the root menu */
644 IBaseMenu *root_menu = menus->GetDefaultStyle()->CreateMenu(this, myself->GetIdentity());
646 /* Add the sorted items */
647 for (size_t i = 0; i < m_SortedCats.size(); i++)
649 if (m_Categories[m_SortedCats[i]]->obj_list.size() == 0)
653 root_menu->AppendItem(m_Categories[m_SortedCats[i]]->obj->name, ItemDrawInfo(""));
656 /* Now we need to handle un-sorted items. This is slightly trickier, as we need to
657 * pre-render each category name, and cache those names. Phew!
659 if (m_UnsortedCats.size())
661 obj_by_name_t *item_list = new obj_by_name_t[m_UnsortedCats.size()];
662 for (size_t i = 0; i < m_UnsortedCats.size(); i++)
664 obj_by_name_t *temp_obj = &item_list[i];
665 topmenu_object_t *obj = m_Categories[m_UnsortedCats[i]]->obj;
666 obj->callbacks->OnTopMenuDisplayOption(this,
670 sizeof(temp_obj->name));
671 temp_obj->obj_index = m_UnsortedCats[i];
674 /* Sort our temp list */
675 qsort(item_list, m_UnsortedCats.size(), sizeof(obj_by_name_t), _SortObjectNamesDescending);
677 /* Add the new sorted categories */
678 for (size_t i = 0; i < m_UnsortedCats.size(); i++)
680 if (m_Categories[item_list[i].obj_index]->obj_list.size() == 0)
684 root_menu->AppendItem(m_Categories[item_list[i].obj_index]->obj->name, ItemDrawInfo(""));
690 /* Set the menu's title */
692 m_pTitle->OnTopMenuDisplayTitle(this, client, 0, renderbuf, sizeof(renderbuf));
693 root_menu->SetDefaultTitle(renderbuf);
695 /* The client is now fully updated */
696 pClient->root = root_menu;
697 pClient->user_id = pPlayer->GetUserId();
698 pClient->menu_serial = m_SerialNo;
699 pClient->last_position = 0;
700 pClient->last_category = 0;
701 pClient->last_root_pos = 0;
704 void TopMenu::UpdateClientCategory(int client, unsigned int category)
706 /* Update the client's root menu just in case it needs it. This
707 * will validate that we have both a valid client and a valid
708 * category structure for that client.
710 UpdateClientRoot(client);
712 /* Now it's guaranteed that our category tables will be usable */
713 topmenu_player_t *pClient = &m_clients[client];
714 topmenu_category_t *cat = m_Categories[category];
715 topmenu_player_category_t *player_cat = &(pClient->cats[category]);
717 /* Does the category actually need updating? */
718 if (player_cat->serial == cat->serial)
723 /* Destroy any existing menu */
724 if (player_cat->menu)
726 player_cat->menu->Destroy();
727 player_cat->menu = NULL;
730 if (pClient->last_category == category)
732 pClient->last_position = 0;
735 IBaseMenu *cat_menu = menus->GetDefaultStyle()->CreateMenu(this, myself->GetIdentity());
737 /* Categories get an "exit back" button */
738 cat_menu->SetMenuOptionFlags(cat_menu->GetMenuOptionFlags() | MENUFLAG_BUTTON_EXITBACK);
740 /* Re-sort the category if needed */
741 SortCategoryIfNeeded(category);
743 /* Build the menu with the sorted items first */
744 for (size_t i = 0; i < cat->sorted.size(); i++)
746 cat_menu->AppendItem(cat->sorted[i]->name, ItemDrawInfo(""));
749 /* Now handle unsorted items */
750 if (cat->unsorted.size())
752 /* Build a list of the item names */
753 obj_by_name_t *item_list = new obj_by_name_t[cat->unsorted.size()];
754 for (size_t i = 0; i < cat->unsorted.size(); i++)
756 obj_by_name_t *item = &item_list[i];
757 topmenu_object_t *obj = cat->unsorted[i];
758 obj->callbacks->OnTopMenuDisplayOption(this,
763 item->obj_index = (unsigned int)i;
767 qsort(item_list, cat->unsorted.size(), sizeof(obj_by_name_t), _SortObjectNamesDescending);
769 /* Add to the menu */
770 for (size_t i = 0; i < cat->unsorted.size(); i++)
772 cat_menu->AppendItem(cat->unsorted[item_list[i].obj_index]->name, ItemDrawInfo(""));
778 /* Set the menu's title */
780 cat->obj->callbacks->OnTopMenuDisplayTitle(this,
785 cat_menu->SetDefaultTitle(renderbuf);
788 player_cat->menu = cat_menu;
789 player_cat->serial = cat->serial;
792 void TopMenu::SortCategoryIfNeeded(unsigned int category)
794 topmenu_category_t *cat = m_Categories[category];
801 cat->unsorted.clear();
803 if (cat->obj_list.size() == 0)
805 cat->reorder = false;
809 CVector<unsigned int> to_sort;
810 for (size_t i = 0; i < cat->obj_list.size(); i++)
812 to_sort.push_back(i);
815 /* Find a matching category in the configs */
816 config_category_t *config_cat = NULL;
817 for (size_t i = 0; i < m_Config.cats.size(); i++)
819 if (strcmp(m_Config.strings.GetString(m_Config.cats[i]->name), cat->obj->name) == 0)
821 config_cat = m_Config.cats[i];
826 /* If there is a matching category, build a pre-sorted item list */
827 if (config_cat != NULL)
829 /* Find matching objects in this category */
830 for (size_t i = 0; i < config_cat->commands.size(); i++)
832 const char *config_name = m_Config.strings.GetString(config_cat->commands[i]);
833 for (size_t j = 0; j < to_sort.size(); j++)
835 if (strcmp(config_name, cat->obj_list[to_sort[j]]->name) == 0)
837 /* Place in the final list, then remove from the temporary list */
838 cat->sorted.push_back(cat->obj_list[to_sort[j]]);
839 to_sort.erase(to_sort.iterAt(j));
846 /* Push any remaining items onto the unsorted list */
847 for (size_t i = 0; i < to_sort.size(); i++)
849 cat->unsorted.push_back(cat->obj_list[to_sort[i]]);
852 cat->reorder = false;
855 void TopMenu::SortCategoriesIfNeeded()
857 if (!m_bCatsNeedResort)
862 /* Clear sort results */
863 m_SortedCats.clear();
864 m_UnsortedCats.clear();
866 if (m_Categories.size() == 0)
868 m_bCatsNeedResort = false;
872 CVector<unsigned int> to_sort;
873 for (unsigned int i = 0; i < (unsigned int)m_Categories.size(); i++)
875 to_sort.push_back(i);
878 /* If we have any predefined categories, add them in as they appear. */
879 for (size_t i= 0; i < m_Config.cats.size(); i++)
881 /* Find this category and map it in if we can */
882 for (size_t j = 0; j < to_sort.size(); j++)
884 if (strcmp(m_Config.strings.GetString(m_Config.cats[i]->name),
885 m_Categories[to_sort[j]]->obj->name) == 0)
887 /* Add to the real list and remove from the temporary */
888 m_SortedCats.push_back(to_sort[j]);
889 to_sort.erase(to_sort.iterAt(j));
895 /* Push any remaining items onto the unsorted list */
896 for (size_t i = 0; i < to_sort.size(); i++)
898 m_UnsortedCats.push_back(to_sort[i]);
901 m_bCatsNeedResort = false;
904 void TopMenu::CreatePlayers(int max_clients)
906 m_max_clients = max_clients;
907 m_clients = (topmenu_player_t *)malloc(sizeof(topmenu_player_t) * (ABSOLUTE_PLAYER_LIMIT + 1));
908 memset(m_clients, 0, sizeof(topmenu_player_t) * (ABSOLUTE_PLAYER_LIMIT + 1));
911 void TopMenu::TearDownClient(topmenu_player_t *player)
913 if (player->cats != NULL)
915 for (unsigned int i = 0; i < player->cat_count; i++)
917 topmenu_player_category_t *player_cat = &(player->cats[i]);
918 if (player_cat->menu != NULL)
920 player_cat->menu->Destroy();
923 delete [] player->cats;
926 if (player->root != NULL)
928 player->root->Destroy();
931 memset(player, 0, sizeof(topmenu_player_t));
934 bool TopMenu::LoadConfiguration(const char *file, char *error, size_t maxlength)
939 if ((err = textparsers->ParseFile_SMC(file, this, &states))
942 const char *err_string = textparsers->GetSMCErrorString(err);
945 err_string = "Unknown";
948 UTIL_Format(error, maxlength, "%s", err_string);
956 bool TopMenu::OnIdentityRemoval(IdentityToken_t *owner)
958 /* First sweep the categories owned by us */
959 CVector<unsigned int> obj_list;
960 for (size_t i = 0; i < m_Categories.size(); i++)
962 if (m_Categories[i]->obj->owner == owner)
964 obj_list.push_back(m_Categories[i]->obj->object_id);
968 for (size_t i = 0; i < obj_list.size(); i++)
970 RemoveFromMenu(obj_list[i]);
973 /* Now we can look for actual items */
974 for (size_t i = 0; i < m_Objects.size(); i++)
976 if (m_Objects[i]->is_free)
980 if (m_Objects[i]->owner == owner)
982 assert(m_Objects[i]->type != TopMenuObject_Category);
983 RemoveFromMenu(m_Objects[i]->object_id);
990 #define PARSE_STATE_NONE 0
991 #define PARSE_STATE_MAIN 1
992 #define PARSE_STATE_CATEGORY 2
993 unsigned int ignore_parse_level = 0;
994 unsigned int current_parse_state = 0;
995 config_category_t *cur_cat = NULL;
997 void TopMenu::ReadSMC_ParseStart()
999 current_parse_state = PARSE_STATE_NONE;
1000 ignore_parse_level = 0;
1003 /* Reset the old config */
1004 m_Config.strings.Reset();
1005 for (size_t i = 0; i < m_Config.cats.size(); i++)
1007 delete m_Config.cats[i];
1009 m_Config.cats.clear();
1012 SMCResult TopMenu::ReadSMC_NewSection(const SMCStates *states, const char *name)
1014 if (ignore_parse_level)
1016 ignore_parse_level++;
1020 if (current_parse_state == PARSE_STATE_NONE)
1022 if (strcmp(name, "Menu") == 0)
1024 current_parse_state = PARSE_STATE_MAIN;
1028 ignore_parse_level = 1;
1031 else if (current_parse_state == PARSE_STATE_MAIN)
1033 cur_cat = new config_category_t;
1034 cur_cat->name = m_Config.strings.AddString(name);
1035 m_Config.cats.push_back(cur_cat);
1036 current_parse_state = PARSE_STATE_CATEGORY;
1040 ignore_parse_level = 1;
1044 return SMCResult_Continue;
1047 SMCResult TopMenu::ReadSMC_KeyValue(const SMCStates *states, const char *key, const char *value)
1049 if (ignore_parse_level > 0
1050 || current_parse_state != PARSE_STATE_CATEGORY
1053 return SMCResult_Continue;
1056 if (strcmp(key, "item") == 0)
1058 cur_cat->commands.push_back(m_Config.strings.AddString(value));
1061 return SMCResult_Continue;
1064 SMCResult TopMenu::ReadSMC_LeavingSection(const SMCStates *states)
1066 if (ignore_parse_level)
1068 ignore_parse_level--;
1072 if (current_parse_state == PARSE_STATE_CATEGORY)
1075 current_parse_state = PARSE_STATE_MAIN;
1077 else if (current_parse_state == PARSE_STATE_MAIN)
1079 current_parse_state = PARSE_STATE_NONE;
1083 return SMCResult_Continue;
1086 unsigned int TopMenu::FindCategory(const char *name)
1088 topmenu_object_t **p_obj = m_ObjLookup.retrieve(name);
1094 topmenu_object_t *obj = *p_obj;
1095 if (obj->type != TopMenuObject_Category)
1100 return obj->object_id;
1103 void TopMenu::OnMaxPlayersChanged( int newvalue )
1105 m_max_clients = newvalue;
1108 int _SortObjectNamesDescending(const void *ptr1, const void *ptr2)
1110 obj_by_name_t *obj1 = (obj_by_name_t *)ptr1;
1111 obj_by_name_t *obj2 = (obj_by_name_t *)ptr2;
1112 return strcmp(obj1->name, obj2->name);
1115 unsigned int strncopy(char *dest, const char *src, size_t count)
1123 while ((*src) && (--count))
1129 return (dest - start);
1132 size_t UTIL_Format(char *buffer, size_t maxlength, const char *fmt, ...)
1136 size_t len = vsnprintf(buffer, maxlength, fmt, ap);
1139 if (len >= maxlength)
1141 buffer[maxlength - 1] = '\0';
1142 return (maxlength - 1);