Triggering build against new mac libprotobuf.
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>.
32 #include "MenuStyle_Radio.h"
33 #include "sm_stringutil.h"
34 #include "UserMessages.h"
35 #include <IGameConfigs.h>
36 #include "PlayerManager.h"
37 #if defined MENU_DEBUG
40 #include "logic_bridge.h"
42 #ifdef USE_PROTOBUF_USERMESSAGES
43 #include <google/protobuf/descriptor.h>
46 #if SOURCE_ENGINE == SE_CSGO
47 #include <game/shared/csgo/protobuf/cstrike15_usermessages.pb.h>
50 extern const char *g_RadioNumTable[];
51 CRadioStyle g_RadioMenuStyle;
52 int g_ShowMenuId = -1;
53 bool g_bRadioInit = false;
54 unsigned int g_RadioMenuTimeout = 0;
57 #define MAX_PAGINATION_OPTIONS 3
59 #define MAX_MENUSLOT_KEYS 10
61 static unsigned int s_RadioMaxPageItems = MAX_MENUSLOT_KEYS;
64 CRadioStyle::CRadioStyle()
66 m_players = new CRadioMenuPlayer[256+1];
67 for (size_t i = 0; i < 256+1; i++)
69 m_players[i].Radio_SetIndex(i);
73 void CRadioStyle::OnSourceModAllInitialized()
75 g_Players.AddClientListener(this);
78 void CRadioStyle::OnSourceModLevelChange(const char *mapName)
86 const char *msg = g_pGameConf->GetKeyValue("HudRadioMenuMsg");
87 if (!msg || msg[0] == '\0')
92 g_ShowMenuId = g_UserMsgs.GetMessageIndex(msg);
99 const char *val = g_pGameConf->GetKeyValue("RadioMenuTimeout");
102 g_RadioMenuTimeout = atoi(val);
106 g_RadioMenuTimeout = 0;
109 const char *items = g_pGameConf->GetKeyValue("RadioMenuMaxPageItems");
112 int value = atoi(items);
114 // Only override the mostly-safe default if it's a sane value
115 if (value > MAX_PAGINATION_OPTIONS && value <= MAX_MENUSLOT_KEYS)
117 s_RadioMaxPageItems = value;
121 g_Menus.AddStyle(this);
122 g_Menus.SetDefaultStyle(this);
124 g_UserMsgs.HookUserMessage(g_ShowMenuId, this, false);
127 void CRadioStyle::OnSourceModShutdown()
129 g_Players.RemoveClientListener(this);
130 g_UserMsgs.UnhookUserMessage(g_ShowMenuId, this, false);
132 while (!m_FreeDisplays.empty())
134 delete m_FreeDisplays.front();
135 m_FreeDisplays.pop();
139 bool CRadioStyle::IsSupported()
141 return (g_ShowMenuId != -1);
144 bool CRadioStyle::OnClientCommand(int client, const char *cmdname, const CCommand &cmd)
146 if (strcmp(cmdname, "menuselect") == 0)
148 if (!m_players[client].bInMenu)
150 m_players[client].bInExternMenu = false;
154 int arg = atoi(cmd.Arg(1));
155 ClientPressedKey(client, arg);
162 static unsigned int g_last_holdtime = 0;
163 static unsigned int g_last_client_count = 0;
164 static int g_last_clients[256];
166 #ifdef USE_PROTOBUF_USERMESSAGES
167 void CRadioStyle::OnUserMessage(int msg_id, protobuf::Message &msg, IRecipientFilter *pFilter)
169 void CRadioStyle::OnUserMessage(int msg_id, bf_write *bf, IRecipientFilter *pFilter)
172 int count = pFilter->GetRecipientCount();
174 #ifdef USE_PROTOBUF_USERMESSAGES
175 int c = ((CCSUsrMsg_ShowMenu &)msg).display_time();
177 bf_read br(bf->GetBasePointer(), 3);
180 int c = br.ReadChar();
183 g_last_holdtime = (c == -1) ? 0 : (unsigned)c;
185 for (int i=0; i<count; i++)
187 g_last_clients[g_last_client_count++] = pFilter->GetRecipientIndex(i);
191 void CRadioStyle::OnUserMessageSent(int msg_id)
193 for (unsigned int i=0; i<g_last_client_count; i++)
195 int client = g_last_clients[i];
196 #if defined MENU_DEBUG
197 g_Logger.LogMessage("[SM_MENU] CRadioStyle got ShowMenu (client %d) (bInMenu %d)",
199 m_players[client].bInExternMenu);
201 if (m_players[client].bInMenu)
203 _CancelClientMenu(client, MenuCancel_Interrupted, true);
205 m_players[client].bInExternMenu = true;
206 m_players[client].menuHoldTime = g_last_holdtime;
208 g_last_client_count = 0;
211 void CRadioStyle::SendDisplay(int client, IMenuPanel *display)
213 CRadioDisplay *rDisplay = (CRadioDisplay *)display;
214 rDisplay->SendRawDisplay(client, m_players[client].menuHoldTime);
217 IMenuPanel *CRadioStyle::CreatePanel()
219 return g_RadioMenuStyle.MakeRadioDisplay();
222 IBaseMenu *CRadioStyle::CreateMenu(IMenuHandler *pHandler, IdentityToken_t *pOwner)
224 return new CRadioMenu(pHandler, pOwner);
227 unsigned int CRadioStyle::GetMaxPageItems()
229 return s_RadioMaxPageItems;
232 const char *CRadioStyle::GetStyleName()
237 CBaseMenuPlayer *CRadioStyle::GetMenuPlayer(int client)
239 return &m_players[client];
242 CRadioDisplay *CRadioStyle::MakeRadioDisplay(CRadioMenu *menu)
244 CRadioDisplay *display;
245 if (m_FreeDisplays.empty())
247 display = new CRadioDisplay();
251 display = m_FreeDisplays.front();
252 m_FreeDisplays.pop();
258 IMenuPanel *CRadioStyle::MakeRadioDisplay(const char *str, int keys)
260 CRadioDisplay *pPanel = MakeRadioDisplay(NULL);
262 pPanel->DirectSet(str, keys);
267 void CRadioStyle::FreeRadioDisplay(CRadioDisplay *display)
269 m_FreeDisplays.push(display);
272 CRadioMenuPlayer *CRadioStyle::GetRadioMenuPlayer(int client)
274 return &m_players[client];
277 void CRadioStyle::ProcessWatchList()
279 if (!g_RadioMenuTimeout)
281 BaseMenuStyle::ProcessWatchList();
285 BaseMenuStyle::ProcessWatchList();
287 CRadioMenuPlayer *pPlayer;
288 unsigned int max_clients = g_Players.GetMaxClients();
289 for (unsigned int i = 1; i <= max_clients; i++)
291 pPlayer = GetRadioMenuPlayer(i);
292 if (!pPlayer->bInMenu || pPlayer->bInExternMenu)
296 if (pPlayer->Radio_NeedsRefresh())
298 pPlayer->Radio_Refresh();
303 unsigned int CRadioStyle::GetApproxMemUsage()
305 return sizeof(CRadioStyle) + (sizeof(CRadioMenuPlayer) * 257);
308 CRadioDisplay::CRadioDisplay()
313 CRadioDisplay::CRadioDisplay(CRadioMenu *menu)
318 bool CRadioDisplay::DrawRawLine(const char *rawline)
320 m_BufferText.append(rawline);
321 m_BufferText.append("\n");
325 void CRadioDisplay::Reset()
327 m_BufferText.assign("");
333 void CRadioDisplay::DirectSet(const char *str, int keymap)
336 m_BufferText.assign(str);
340 unsigned int CRadioDisplay::GetCurrentKey()
345 bool CRadioDisplay::SetCurrentKey(unsigned int key)
347 if (key < m_NextPos || m_NextPos > s_RadioMaxPageItems)
357 bool CRadioDisplay::SendDisplay(int client, IMenuHandler *handler, unsigned int time)
359 return g_RadioMenuStyle.DoClientMenu(client, this, handler, time);
362 bool CRadioDisplay::SetExtOption(MenuOption option, const void *valuePtr)
367 IMenuStyle *CRadioDisplay::GetParentStyle()
369 return &g_RadioMenuStyle;
372 void CRadioDisplay::DrawTitle(const char *text, bool onlyIfEmpty/* =false */)
374 if (m_Title.size() != 0 && onlyIfEmpty)
378 m_Title.assign(text);
381 unsigned int CRadioDisplay::DrawItem(const ItemDrawInfo &item)
383 if (m_NextPos > s_RadioMaxPageItems || !CanDrawItem(item.style))
388 if (item.style & ITEMDRAW_RAWLINE)
390 if (item.style & ITEMDRAW_SPACER)
392 m_BufferText.append(" \n");
394 m_BufferText.append(item.display);
395 m_BufferText.append("\n");
398 } else if (item.style & ITEMDRAW_SPACER) {
399 m_BufferText.append(" \n");
401 } else if (item.style & ITEMDRAW_NOTEXT) {
405 if (item.style & ITEMDRAW_DISABLED)
407 m_BufferText.append(g_RadioNumTable[m_NextPos]);
408 m_BufferText.append(item.display);
409 m_BufferText.append("\n");
411 m_BufferText.append("->");
412 m_BufferText.append(g_RadioNumTable[m_NextPos]);
413 m_BufferText.append(item.display);
414 m_BufferText.append("\n");
415 keys |= (1<<(m_NextPos-1));
421 bool CRadioDisplay::CanDrawItem(unsigned int drawFlags)
423 if ((drawFlags & ITEMDRAW_IGNORE) == ITEMDRAW_IGNORE)
428 if ((drawFlags & ITEMDRAW_DISABLED) && (drawFlags & ITEMDRAW_CONTROL))
436 void CRadioDisplay::SendRawDisplay(int client, unsigned int time)
438 int _sel_keys = (keys == 0) ? (1<<9) : keys;
439 CRadioMenuPlayer *pPlayer = g_RadioMenuStyle.GetRadioMenuPlayer(client);
440 pPlayer->Radio_Init(_sel_keys, m_Title.c_str(), m_BufferText.c_str());
441 pPlayer->Radio_Refresh();
444 void CRadioMenuPlayer::Radio_SetIndex(unsigned int index)
449 bool CRadioMenuPlayer::Radio_NeedsRefresh()
451 return (gpGlobals->curtime - display_last_refresh >= g_RadioMenuTimeout);
454 void CRadioMenuPlayer::Radio_Init(int keys, const char *title, const char *text)
456 if (title[0] != '\0')
458 display_len = UTIL_Format(display_pkt,
466 display_len = UTIL_Format(display_pkt,
474 void CRadioMenuPlayer::Radio_Refresh()
476 cell_t players[1] = {m_index};
477 char *ptr = display_pkt;
479 size_t len = display_len;
482 /* Compute the new time */
483 if (menuHoldTime == 0)
489 time = menuHoldTime - (unsigned int)(gpGlobals->curtime - menuStartTime);
492 #ifdef USE_PROTOBUF_USERMESSAGES
493 // If or when we need to support multiple games per engine with this, we can switch to reflection
494 // TODO: find what happens past 240 on CS:GO
495 CCSUsrMsg_ShowMenu *msg = (CCSUsrMsg_ShowMenu *)g_UserMsgs.StartProtobufMessage(g_ShowMenuId, players, 1, USERMSG_BLOCKHOOKS);
496 msg->set_bits_valid_slots(display_keys);
497 msg->set_display_time(time);
498 msg->set_menu_string(ptr);
499 g_UserMsgs.EndMessage();
509 bf_write *buffer = g_UserMsgs.StartBitBufMessage(g_ShowMenuId, players, 1, USERMSG_BLOCKHOOKS);
510 buffer->WriteWord(display_keys);
511 buffer->WriteChar(time ? time : -1);
512 buffer->WriteByte( (len > 240) ? 1 : 0 );
513 buffer->WriteString(ptr);
514 g_UserMsgs.EndMessage();
528 display_last_refresh = gpGlobals->curtime;
531 int CRadioDisplay::GetAmountRemaining()
533 size_t amt = m_Title.size() + 1 + m_BufferText.size();
538 return (int)(511 - amt);
541 void CRadioDisplay::DeleteThis()
546 bool CRadioDisplay::SetSelectableKeys(unsigned int keymap)
548 keys = (signed)keymap;
552 unsigned int CRadioDisplay::GetApproxMemUsage()
554 return sizeof(CRadioDisplay)
555 + m_BufferText.size()
559 CRadioMenu::CRadioMenu(IMenuHandler *pHandler, IdentityToken_t *pOwner) :
560 CBaseMenu(pHandler, &g_RadioMenuStyle, pOwner)
562 m_Pagination = s_RadioMaxPageItems - MAX_PAGINATION_OPTIONS;
565 bool CRadioMenu::SetExtOption(MenuOption option, const void *valuePtr)
570 IMenuPanel *CRadioMenu::CreatePanel()
572 return g_RadioMenuStyle.MakeRadioDisplay(this);
575 bool CRadioMenu::Display(int client, unsigned int time, IMenuHandler *alt_handler)
577 return DisplayAtItem(client, time, 0, alt_handler);
580 bool CRadioMenu::DisplayAtItem(int client,
582 unsigned int start_item,
583 IMenuHandler *alt_handler)
585 #if defined MENU_DEBUG
586 g_Logger.LogMessage("[SM_MENU] CRadioMenu::Display(%p) (client %d) (time %d)",
596 return g_RadioMenuStyle.DoClientMenu(client,
599 alt_handler ? alt_handler : m_pHandler,
603 bool CRadioMenu::SetPagination(unsigned int itemsPerPage)
605 const unsigned int maxPerPage = s_RadioMaxPageItems - MAX_PAGINATION_OPTIONS;
606 if (itemsPerPage > maxPerPage)
611 return CBaseMenu::SetPagination(itemsPerPage);
614 void CRadioMenu::Cancel_Finally()
616 g_RadioMenuStyle.CancelMenu(this);
619 unsigned int CRadioMenu::GetApproxMemUsage()
621 return sizeof(CRadioMenu) + GetBaseMemUsage();
624 const char *g_RadioNumTable[11] =
626 "0. ", "1. ", "2. ", "3. ", "4. ", "5. ", "6. ", "7. ", "8. ", "9. ", "0. "