Added missing CS:GO GameRulesProxy gamedata.
3 * =============================================================================
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>.
33 #include "sm_stringutil.h"
34 #include "MenuStyle_Base.h"
35 #include "PlayerManager.h"
36 #include "MenuManager.h"
37 #include "HandleSys.h"
38 #include "CellRecipientFilter.h"
39 #if defined MENU_DEBUG
43 BaseMenuStyle::BaseMenuStyle() : m_WatchList(256), m_hHandle(BAD_HANDLE)
47 Handle_t BaseMenuStyle::GetHandle()
49 /* Don't create the handle until we need it */
50 if (m_hHandle == BAD_HANDLE)
52 m_hHandle = g_Menus.CreateStyleHandle(this);
58 void BaseMenuStyle::AddClientToWatch(int client)
60 #if defined MENU_DEBUG
61 g_Logger.LogMessage("[SM_MENU] AddClientToWatch(%d)", client);
63 m_WatchList.push_back(client);
66 void BaseMenuStyle::RemoveClientFromWatch(int client)
68 #if defined MENU_DEBUG
69 g_Logger.LogMessage("[SM_MENU] RemoveClientFromWatch(%d)", client);
71 m_WatchList.remove(client);
74 void BaseMenuStyle::_CancelClientMenu(int client, MenuCancelReason reason, bool bAutoIgnore/* =false */)
76 #if defined MENU_DEBUG
77 g_Logger.LogMessage("[SM_MENU] _CancelClientMenu() (client %d) (bAutoIgnore %d) (reason %d)", client, bAutoIgnore, reason);
79 CBaseMenuPlayer *player = GetMenuPlayer(client);
80 menu_states_t &states = player->states;
82 bool bOldIgnore = player->bAutoIgnore;
85 player->bAutoIgnore = true;
89 IMenuHandler *mh = states.mh;
90 IBaseMenu *menu = states.menu;
93 player->bInMenu = false;
94 if (player->menuHoldTime)
96 RemoveClientFromWatch(client);
100 mh->OnMenuCancel(menu, client, reason);
102 /* Only fire end if there's a valid menu */
105 mh->OnMenuEnd(menu, MenuEnd_Cancelled);
110 player->bAutoIgnore = bOldIgnore;
114 void BaseMenuStyle::CancelMenu(CBaseMenu *menu)
116 #if defined MENU_DEBUG
117 g_Logger.LogMessage("[SM_MENU] CancelMenu() (menu %p)", menu);
119 int maxClients = g_Players.GetMaxClients();
120 for (int i=1; i<=maxClients; i++)
122 CBaseMenuPlayer *player = GetMenuPlayer(i);
125 menu_states_t &states = player->states;
126 if (states.menu == menu)
128 _CancelClientMenu(i, MenuCancel_Interrupted);
134 bool BaseMenuStyle::CancelClientMenu(int client, bool autoIgnore)
136 #if defined MENU_DEBUG
137 g_Logger.LogMessage("[SM_MENU] CancelClientMenu() (client %d) (bAutoIgnore %d)", client, autoIgnore);
139 if (client < 1 || client > g_Players.MaxClients())
144 CBaseMenuPlayer *player = GetMenuPlayer(client);
145 if (!player->bInMenu)
150 _CancelClientMenu(client, MenuCancel_Interrupted, autoIgnore);
155 MenuSource BaseMenuStyle::GetClientMenu(int client, void **object)
157 if (client < 1 || client > g_Players.GetMaxClients())
159 return MenuSource_None;
162 CBaseMenuPlayer *player = GetMenuPlayer(client);
167 if ((menu=player->states.menu) != NULL)
173 return MenuSource_BaseMenu;
176 return MenuSource_Display;
177 } else if (player->bInExternMenu) {
178 if (player->menuHoldTime != 0
179 && (gpGlobals->curtime > player->menuStartTime + player->menuHoldTime))
181 player->bInExternMenu = false;
182 return MenuSource_None;
184 return MenuSource_External;
187 return MenuSource_None;
190 void BaseMenuStyle::OnClientDisconnected(int client)
192 #if defined MENU_DEBUG
193 g_Logger.LogMessage("[SM_MENU] OnClientDisconnected(%d)", client);
195 CBaseMenuPlayer *player = GetMenuPlayer(client);
196 if (!player->bInMenu)
201 _CancelClientMenu(client, MenuCancel_Disconnected, true);
203 player->bInMenu = false;
204 player->bInExternMenu = false;
207 static int do_lookup[256];
208 void BaseMenuStyle::ProcessWatchList()
210 if (!m_WatchList.size())
215 #if defined MENU_DEBUG
216 g_Logger.LogMessage("BaseMenuStyle::ProcessWatchList(%d,%d,%d,%d,%d,%p)",
218 m_WatchList.m_FirstLink,
219 m_WatchList.m_FreeNodes,
220 m_WatchList.m_LastLink,
221 m_WatchList.m_MaxSize,
222 m_WatchList.m_Nodes);
225 unsigned int total = 0;
226 for (FastLink<int>::iterator iter=m_WatchList.begin(); iter!=m_WatchList.end(); ++iter)
228 do_lookup[total++] = (*iter);
231 #if defined MENU_DEBUG
234 g_Logger.LogMessage("[SM_MENU] ProcessWatchList() found %d clients", total);
239 CBaseMenuPlayer *player;
240 float curTime = gpGlobals->curtime;
241 for (unsigned int i=0; i<total; i++)
243 client = do_lookup[i];
244 player = GetMenuPlayer(client);
245 #if defined MENU_DEBUG
246 g_Logger.LogMessage("[SM_MENU] ProcessWatchList() (client %d) (bInMenu %d) (menuHoldTime %d) (curTime %f) (menuStartTime %f)",
249 player->menuHoldTime,
251 player->menuStartTime);
253 if (!player->bInMenu || !player->menuHoldTime)
255 #if defined MENU_DEBUG
256 g_Logger.LogMessage("[SM_MENU] ProcessWatchList() removing client %d", client);
258 m_WatchList.remove(client);
261 if (curTime > player->menuStartTime + player->menuHoldTime)
263 _CancelClientMenu(client, MenuCancel_Timeout, false);
268 void BaseMenuStyle::ClientPressedKey(int client, unsigned int key_press)
270 #if defined MENU_DEBUG
271 g_Logger.LogMessage("[SM_MENU] ClientPressedKey() (client %d) (key_press %d)", client, key_press);
273 CBaseMenuPlayer *player = GetMenuPlayer(client);
275 /* First question: Are we in a menu? */
276 if (!player->bInMenu)
282 unsigned int item = 0;
283 MenuCancelReason reason = MenuCancel_Exit;
284 MenuEndReason end_reason = MenuEnd_Selected;
285 menu_states_t &states = player->states;
288 IMenuHandler *mh = states.mh;
289 IBaseMenu *menu = states.menu;
291 unsigned int item_on_page = states.item_on_page;
298 } else if (key_press < 1 || key_press > GetMaxPageItems()) {
301 ItemSelection type = states.slots[key_press].type;
303 /* Check if we should play a sound about the type */
304 if (g_Menus.MenuSoundsEnabled() &&
305 (!menu || (menu->GetMenuOptionFlags() & MENUFLAG_NO_SOUND) != MENUFLAG_NO_SOUND))
307 CellRecipientFilter filter;
311 filter.Initialize(clients, 1);
313 const char *sound = g_Menus.GetMenuSound(type);
317 edict_t *pEdict = PEntityOfEntIndex(client);
320 ICollideable *pCollideable = pEdict->GetCollideable();
324 const Vector & pos = pCollideable->GetCollisionOrigin();
325 enginesound->EmitSound(filter,
328 #if SOURCE_ENGINE >= SE_PORTAL2
335 #if SOURCE_ENGINE >= SE_PORTAL2
346 /* For navigational items, we're going to redisplay */
347 if (type == ItemSel_Back)
349 if (!RedoClientMenu(client, ItemOrder_Descending))
352 reason = MenuCancel_NoDisplay;
353 end_reason = MenuEnd_Cancelled;
357 } else if (type == ItemSel_Next) {
358 if (!RedoClientMenu(client, ItemOrder_Ascending))
360 cancel = true; /* I like Saltines. */
361 reason = MenuCancel_NoDisplay;
362 end_reason = MenuEnd_Cancelled;
366 } else if (type == ItemSel_Exit || type == ItemSel_None) {
368 reason = MenuCancel_Exit;
369 end_reason = MenuEnd_Exit;
370 } else if (type == ItemSel_ExitBack) {
372 reason = MenuCancel_ExitBack;
373 end_reason = MenuEnd_ExitBack;
375 item = states.slots[key_press].item;
380 player->bInMenu = false;
381 if (player->menuHoldTime)
383 RemoveClientFromWatch(client);
386 Handle_t hndl = menu ? menu->GetHandle() : BAD_HANDLE;
387 AutoHandleRooter ahr(hndl);
391 mh->OnMenuCancel(menu, client, reason);
393 mh->OnMenuSelect(menu, client, item);
394 if (mh->GetMenuAPIVersion2() >= 13)
396 mh->OnMenuSelect2(menu, client, item, item_on_page);
400 /* Only fire end for valid menus */
403 mh->OnMenuEnd(menu, end_reason);
407 bool BaseMenuStyle::DoClientMenu(int client, IMenuPanel *menu, IMenuHandler *mh, unsigned int time)
409 #if defined MENU_DEBUG
410 g_Logger.LogMessage("[SM_MENU] DoClientMenu() (client %d) (panel %p) (mh %p) (time %d)",
416 CPlayer *pPlayer = g_Players.GetPlayerByIndex(client);
417 if (!pPlayer || pPlayer->IsFakeClient() || !pPlayer->IsInGame())
422 CBaseMenuPlayer *player = GetMenuPlayer(client);
423 if (player->bAutoIgnore)
428 /* For the duration of this, we are going to totally ignore whether
429 * the player is already in a menu or not (except to cancel the old one).
430 * Instead, we are simply going to ignore any further menu displays, so
431 * this display can't be interrupted.
433 player->bAutoIgnore = true;
435 /* Cancel any old menus */
436 menu_states_t &states = player->states;
439 _CancelClientMenu(client, MenuCancel_Interrupted, true);
442 states.firstItem = 0;
446 states.apiVers = SMINTERFACE_MENUMANAGER_VERSION;
447 player->bInMenu = true;
448 player->bInExternMenu = false;
449 player->menuStartTime = gpGlobals->curtime;
450 player->menuHoldTime = time;
454 AddClientToWatch(client);
457 /* Draw the display */
458 SendDisplay(client, menu);
460 /* We can be interrupted again! */
461 player->bAutoIgnore = false;
466 bool BaseMenuStyle::DoClientMenu(int client,
468 unsigned int first_item,
472 #if defined MENU_DEBUG
473 g_Logger.LogMessage("[SM_MENU] DoClientMenu() (client %d) (menu %p) (mh %p) (time %d)",
479 mh->OnMenuStart(menu);
481 CPlayer *pPlayer = g_Players.GetPlayerByIndex(client);
482 if (!pPlayer || pPlayer->IsFakeClient() || !pPlayer->IsInGame())
484 #if defined MENU_DEBUG
485 g_Logger.LogMessage("[SM_MENU] DoClientMenu(): Failed to display to client %d", client);
487 mh->OnMenuCancel(menu, client, MenuCancel_NoDisplay);
488 mh->OnMenuEnd(menu, MenuEnd_Cancelled);
492 CBaseMenuPlayer *player = GetMenuPlayer(client);
493 if (player->bAutoIgnore)
495 #if defined MENU_DEBUG
496 g_Logger.LogMessage("[SM_MENU] DoClientMenu(): Client %d is autoIgnoring", client);
498 mh->OnMenuCancel(menu, client, MenuCancel_NoDisplay);
499 mh->OnMenuEnd(menu, MenuEnd_Cancelled);
503 /* For the duration of this, we are going to totally ignore whether
504 * the player is already in a menu or not (except to cancel the old one).
505 * Instead, we are simply going to ignore any further menu displays, so
506 * this display can't be interrupted.
508 player->bAutoIgnore = true;
510 /* Cancel any old menus */
511 menu_states_t &states = player->states;
514 #if defined MENU_DEBUG
515 g_Logger.LogMessage("[SM_MENU] DoClientMenu(): Cancelling old menu to client %d", client);
517 _CancelClientMenu(client, MenuCancel_Interrupted, true);
520 states.firstItem = 0;
521 states.lastItem = first_item;
524 states.apiVers = SMINTERFACE_MENUMANAGER_VERSION;
526 IMenuPanel *display = g_Menus.RenderMenu(client, states, ItemOrder_Ascending);
529 #if defined MENU_DEBUG
530 g_Logger.LogMessage("[SM_MENU] DoClientMenu(): Failed to render to client %d", client);
532 player->bAutoIgnore = false;
533 player->bInMenu = false;
534 mh->OnMenuCancel(menu, client, MenuCancel_NoDisplay);
535 mh->OnMenuEnd(menu, MenuEnd_Cancelled);
539 /* Finally, set our states */
540 player->bInMenu = true;
541 player->bInExternMenu = false;
542 player->menuStartTime = gpGlobals->curtime;
543 player->menuHoldTime = time;
547 AddClientToWatch(client);
550 /* Draw the display */
551 SendDisplay(client, display);
553 /* Free the display pointer */
554 display->DeleteThis();
556 /* We can be interrupted again! */
557 player->bAutoIgnore = false;
559 #if defined MENU_DEBUG
560 g_Logger.LogMessage("[SM_MENU] DoClientMenu() finished successfully (client %d)", client);
566 bool BaseMenuStyle::RedoClientMenu(int client, ItemOrder order)
568 #if defined MENU_DEBUG
569 g_Logger.LogMessage("[SM_MENU] RedoClientMenu() (client %d) (order %d)", client, order);
571 CBaseMenuPlayer *player = GetMenuPlayer(client);
572 menu_states_t &states = player->states;
574 player->bAutoIgnore = true;
575 IMenuPanel *display = g_Menus.RenderMenu(client, states, order);
578 #if defined MENU_DEBUG
579 g_Logger.LogMessage("[SM_MENU] RedoClientMenu(): Failed to render menu");
581 if (player->menuHoldTime)
583 RemoveClientFromWatch(client);
585 player->bAutoIgnore = false;
589 SendDisplay(client, display);
591 display->DeleteThis();
593 player->bAutoIgnore = false;
595 #if defined MENU_DEBUG
596 g_Logger.LogMessage("[SM_MENU] RedoClientMenu(): Succeeded to client %d", client);
602 CBaseMenu::CBaseMenu(IMenuHandler *pHandler, IMenuStyle *pStyle, IdentityToken_t *pOwner) :
603 m_pStyle(pStyle), m_Strings(512), m_Pagination(7), m_bShouldDelete(false), m_bCancelling(false),
604 m_pOwner(pOwner ? pOwner : g_pCoreIdent), m_bDeleting(false), m_bWillFreeHandle(false),
605 m_hHandle(BAD_HANDLE), m_pHandler(pHandler), m_nFlags(MENUFLAG_BUTTON_EXIT)
609 CBaseMenu::~CBaseMenu()
613 Handle_t CBaseMenu::GetHandle()
617 m_hHandle = g_Menus.CreateMenuHandle(this, m_pOwner);
623 bool CBaseMenu::AppendItem(const char *info, const ItemDrawInfo &draw)
625 if (m_Pagination == (unsigned)MENU_NO_PAGINATION
626 && m_items.size() >= m_pStyle->GetMaxPageItems())
633 item.infoString = m_Strings.AddString(info);
636 item.displayString = m_Strings.AddString(draw.display);
638 item.style = draw.style;
640 m_items.push_back(item);
645 bool CBaseMenu::InsertItem(unsigned int position, const char *info, const ItemDrawInfo &draw)
647 if (m_Pagination == (unsigned)MENU_NO_PAGINATION
648 && m_items.size() >= m_pStyle->GetMaxPageItems())
653 if (position >= m_items.size())
659 item.infoString = m_Strings.AddString(info);
662 item.displayString = m_Strings.AddString(draw.display);
664 item.style = draw.style;
666 CVector<CItem>::iterator iter = m_items.iterAt(position);
667 m_items.insert(iter, item);
672 bool CBaseMenu::RemoveItem(unsigned int position)
674 if (position >= m_items.size())
679 m_items.erase(m_items.iterAt(position));
681 if (m_items.size() == 0)
689 void CBaseMenu::RemoveAllItems()
695 const char *CBaseMenu::GetItemInfo(unsigned int position, ItemDrawInfo *draw/* =NULL */)
697 if (position >= m_items.size())
704 draw->display = m_Strings.GetString(m_items[position].displayString);
705 draw->style = m_items[position].style;
708 return m_Strings.GetString(m_items[position].infoString);
711 unsigned int CBaseMenu::GetItemCount()
713 return m_items.size();
716 bool CBaseMenu::SetPagination(unsigned int itemsPerPage)
718 if (itemsPerPage > 7 || itemsPerPage == 1)
723 if (itemsPerPage == MENU_NO_PAGINATION
724 && m_Pagination != MENU_NO_PAGINATION)
726 m_nFlags &= ~MENUFLAG_BUTTON_EXIT;
729 m_Pagination = itemsPerPage;
734 unsigned int CBaseMenu::GetPagination()
739 IMenuStyle *CBaseMenu::GetDrawStyle()
744 void CBaseMenu::SetDefaultTitle(const char *message)
746 m_Title.assign(message);
749 const char *CBaseMenu::GetDefaultTitle()
751 return m_Title.c_str();
754 void CBaseMenu::Cancel()
761 #if defined MENU_DEBUG
762 g_Logger.LogMessage("[SM_MENU] CBaseMenu::Cancel(%p) (m_bShouldDelete %d)",
767 m_bCancelling = true;
769 m_bCancelling = false;
777 void CBaseMenu::Destroy(bool releaseHandle)
779 /* Check if we shouldn't be here */
785 #if defined MENU_DEBUG
786 g_Logger.LogMessage("[SM_MENU] CBaseMenu::Destroy(%p) (release %d) (m_bCancelling %d) (m_bShouldDelete %d)",
793 /* Save the destruction hint about our handle */
794 m_bWillFreeHandle = releaseHandle;
796 /* Now actually do stuff */
797 if (!m_bCancelling || m_bShouldDelete)
802 m_bShouldDelete = true;
806 void CBaseMenu::InternalDelete()
808 if (m_bWillFreeHandle && m_hHandle != BAD_HANDLE)
810 Handle_t hndl = m_hHandle;
813 sec.pOwner = m_pOwner;
814 sec.pIdentity = g_pCoreIdent;
816 m_hHandle = BAD_HANDLE;
818 g_HandleSys.FreeHandle(hndl, &sec);
821 m_pHandler->OnMenuDestroy(this);
826 unsigned int CBaseMenu::GetMenuOptionFlags()
831 void CBaseMenu::SetMenuOptionFlags(unsigned int flags)
836 IMenuHandler *CBaseMenu::GetHandler()
841 unsigned int CBaseMenu::GetBaseMemUsage()
843 return m_Title.size()
844 + m_Strings.GetMemTable()->GetMemUsage()
845 + (m_items.size() * sizeof(CItem));