reworked antiflood. it now has some logic in core to take care of loading order nastiness, and to fully prevent trigger spamming
3 * =============================================================================
5 * Copyright (C) 2004-2007 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 "GameConfigs.h"
36 #include "PlayerManager.h"
37 #if defined MENU_DEBUG
41 extern const char *g_RadioNumTable[];
42 CRadioStyle g_RadioMenuStyle;
43 int g_ShowMenuId = -1;
44 bool g_bRadioInit = false;
45 unsigned int g_RadioMenuTimeout = 0;
47 CRadioStyle::CRadioStyle()
49 m_players = new CRadioMenuPlayer[256+1];
50 for (size_t i = 0; i < 256+1; i++)
52 m_players[i].Radio_SetIndex(i);
56 void CRadioStyle::OnSourceModAllInitialized()
58 g_Players.AddClientListener(this);
61 void CRadioStyle::OnSourceModLevelChange(const char *mapName)
69 const char *msg = g_pGameConf->GetKeyValue("HudRadioMenuMsg");
70 if (!msg || msg[0] == '\0')
75 g_ShowMenuId = g_UserMsgs.GetMessageIndex(msg);
82 const char *val = g_pGameConf->GetKeyValue("RadioMenuTimeout");
85 g_RadioMenuTimeout = atoi(val);
89 g_RadioMenuTimeout = 0;
92 g_Menus.AddStyle(this);
93 g_Menus.SetDefaultStyle(this);
95 g_UserMsgs.HookUserMessage(g_ShowMenuId, this, false);
98 void CRadioStyle::OnSourceModShutdown()
100 g_Players.RemoveClientListener(this);
101 g_UserMsgs.UnhookUserMessage(g_ShowMenuId, this, false);
103 while (!m_FreeDisplays.empty())
105 delete m_FreeDisplays.front();
106 m_FreeDisplays.pop();
110 bool CRadioStyle::IsSupported()
112 return (g_ShowMenuId != -1);
115 bool CRadioStyle::OnClientCommand(int client, const char *cmdname, const CCommand &cmd)
117 if (strcmp(cmdname, "menuselect") == 0)
119 if (!m_players[client].bInMenu)
121 m_players[client].bInExternMenu = false;
125 int arg = atoi(cmd.Arg(1));
126 ClientPressedKey(client, arg);
133 static unsigned int g_last_holdtime = 0;
134 static unsigned int g_last_client_count = 0;
135 static int g_last_clients[256];
137 void CRadioStyle::OnUserMessage(int msg_id, bf_write *bf, IRecipientFilter *pFilter)
139 int count = pFilter->GetRecipientCount();
140 bf_read br(bf->GetBasePointer(), 3);
143 int c = br.ReadChar();
145 g_last_holdtime = (c == -1) ? 0 : (unsigned)c;
147 for (int i=0; i<count; i++)
149 g_last_clients[g_last_client_count++] = pFilter->GetRecipientIndex(i);
153 void CRadioStyle::OnUserMessageSent(int msg_id)
155 for (unsigned int i=0; i<g_last_client_count; i++)
157 int client = g_last_clients[i];
158 #if defined MENU_DEBUG
159 g_Logger.LogMessage("[SM_MENU] CRadioStyle got ShowMenu (client %d) (bInMenu %d)",
161 m_players[client].bInExternMenu);
163 if (m_players[client].bInMenu)
165 _CancelClientMenu(client, MenuCancel_Interrupted, true);
167 m_players[client].bInExternMenu = true;
168 m_players[client].menuHoldTime = g_last_holdtime;
170 g_last_client_count = 0;
173 void CRadioStyle::SendDisplay(int client, IMenuPanel *display)
175 CRadioDisplay *rDisplay = (CRadioDisplay *)display;
176 rDisplay->SendRawDisplay(client, m_players[client].menuHoldTime);
179 IMenuPanel *CRadioStyle::CreatePanel()
181 return g_RadioMenuStyle.MakeRadioDisplay();
184 IBaseMenu *CRadioStyle::CreateMenu(IMenuHandler *pHandler, IdentityToken_t *pOwner)
186 return new CRadioMenu(pHandler, pOwner);
189 unsigned int CRadioStyle::GetMaxPageItems()
194 const char *CRadioStyle::GetStyleName()
199 CBaseMenuPlayer *CRadioStyle::GetMenuPlayer(int client)
201 return &m_players[client];
204 CRadioDisplay *CRadioStyle::MakeRadioDisplay(CRadioMenu *menu)
206 CRadioDisplay *display;
207 if (m_FreeDisplays.empty())
209 display = new CRadioDisplay();
213 display = m_FreeDisplays.front();
214 m_FreeDisplays.pop();
220 IMenuPanel *CRadioStyle::MakeRadioDisplay(const char *str, int keys)
222 CRadioDisplay *pPanel = MakeRadioDisplay(NULL);
224 pPanel->DirectSet(str, keys);
229 void CRadioStyle::FreeRadioDisplay(CRadioDisplay *display)
231 m_FreeDisplays.push(display);
234 CRadioMenuPlayer *CRadioStyle::GetRadioMenuPlayer(int client)
236 return &m_players[client];
239 void CRadioStyle::ProcessWatchList()
241 if (!g_RadioMenuTimeout)
243 BaseMenuStyle::ProcessWatchList();
247 BaseMenuStyle::ProcessWatchList();
249 CRadioMenuPlayer *pPlayer;
250 unsigned int max_clients = g_Players.GetMaxClients();
251 for (unsigned int i = 1; i <= max_clients; i++)
253 pPlayer = GetRadioMenuPlayer(i);
254 if (!pPlayer->bInMenu || pPlayer->bInExternMenu)
258 if (pPlayer->Radio_NeedsRefresh())
260 pPlayer->Radio_Refresh();
265 unsigned int CRadioStyle::GetApproxMemUsage()
267 return sizeof(CRadioStyle) + (sizeof(CRadioMenuPlayer) * 257);
270 CRadioDisplay::CRadioDisplay()
275 CRadioDisplay::CRadioDisplay(CRadioMenu *menu)
280 bool CRadioDisplay::DrawRawLine(const char *rawline)
282 m_BufferText.append(rawline);
283 m_BufferText.append("\n");
287 void CRadioDisplay::Reset()
289 m_BufferText.assign("");
295 void CRadioDisplay::DirectSet(const char *str, int keymap)
298 m_BufferText.assign(str);
302 unsigned int CRadioDisplay::GetCurrentKey()
307 bool CRadioDisplay::SetCurrentKey(unsigned int key)
309 if (key < m_NextPos || m_NextPos > 10)
319 bool CRadioDisplay::SendDisplay(int client, IMenuHandler *handler, unsigned int time)
321 return g_RadioMenuStyle.DoClientMenu(client, this, handler, time);
324 bool CRadioDisplay::SetExtOption(MenuOption option, const void *valuePtr)
329 IMenuStyle *CRadioDisplay::GetParentStyle()
331 return &g_RadioMenuStyle;
334 void CRadioDisplay::DrawTitle(const char *text, bool onlyIfEmpty/* =false */)
336 if (m_Title.size() != 0 && onlyIfEmpty)
340 m_Title.assign(text);
343 unsigned int CRadioDisplay::DrawItem(const ItemDrawInfo &item)
345 if (m_NextPos > 10 || !CanDrawItem(item.style))
350 if (item.style & ITEMDRAW_RAWLINE)
352 if (item.style & ITEMDRAW_SPACER)
354 m_BufferText.append(" \n");
356 m_BufferText.append(item.display);
357 m_BufferText.append("\n");
360 } else if (item.style & ITEMDRAW_SPACER) {
361 m_BufferText.append(" \n");
363 } else if (item.style & ITEMDRAW_NOTEXT) {
367 if (item.style & ITEMDRAW_DISABLED)
369 m_BufferText.append(g_RadioNumTable[m_NextPos]);
370 m_BufferText.append(item.display);
371 m_BufferText.append("\n");
373 m_BufferText.append("->");
374 m_BufferText.append(g_RadioNumTable[m_NextPos]);
375 m_BufferText.append(item.display);
376 m_BufferText.append("\n");
377 keys |= (1<<(m_NextPos-1));
383 bool CRadioDisplay::CanDrawItem(unsigned int drawFlags)
385 if ((drawFlags & ITEMDRAW_IGNORE) == ITEMDRAW_IGNORE)
390 if ((drawFlags & ITEMDRAW_DISABLED) && (drawFlags & ITEMDRAW_CONTROL))
398 void CRadioDisplay::SendRawDisplay(int client, unsigned int time)
400 int _sel_keys = (keys == 0) ? (1<<9) : keys;
401 CRadioMenuPlayer *pPlayer = g_RadioMenuStyle.GetRadioMenuPlayer(client);
402 pPlayer->Radio_Init(_sel_keys, m_Title.c_str(), m_BufferText.c_str());
403 pPlayer->Radio_Refresh();
406 void CRadioMenuPlayer::Radio_SetIndex(unsigned int index)
411 bool CRadioMenuPlayer::Radio_NeedsRefresh()
413 return (gpGlobals->curtime - display_last_refresh >= g_RadioMenuTimeout);
416 void CRadioMenuPlayer::Radio_Init(int keys, const char *title, const char *text)
418 if (title[0] != '\0')
420 display_len = UTIL_Format(display_pkt,
428 display_len = UTIL_Format(display_pkt,
436 void CRadioMenuPlayer::Radio_Refresh()
438 cell_t players[1] = {m_index};
439 char *ptr = display_pkt;
441 size_t len = display_len;
444 /* Compute the new time */
445 if (menuHoldTime == 0)
451 time = menuHoldTime - (unsigned int)(gpGlobals->curtime - menuStartTime);
461 bf_write *buffer = g_UserMsgs.StartMessage(g_ShowMenuId, players, 1, USERMSG_BLOCKHOOKS);
462 buffer->WriteWord(display_keys);
463 buffer->WriteChar(time ? time : -1);
464 buffer->WriteByte( (len > 240) ? 1 : 0 );
465 buffer->WriteString(ptr);
466 g_UserMsgs.EndMessage();
479 display_last_refresh = gpGlobals->curtime;
482 int CRadioDisplay::GetAmountRemaining()
484 size_t amt = m_Title.size() + 1 + m_BufferText.size();
489 return (int)(511 - amt);
492 void CRadioDisplay::DeleteThis()
497 bool CRadioDisplay::SetSelectableKeys(unsigned int keymap)
499 keys = (signed)keymap;
503 unsigned int CRadioDisplay::GetApproxMemUsage()
505 return sizeof(CRadioDisplay)
506 + m_BufferText.size()
510 CRadioMenu::CRadioMenu(IMenuHandler *pHandler, IdentityToken_t *pOwner) :
511 CBaseMenu(pHandler, &g_RadioMenuStyle, pOwner)
515 bool CRadioMenu::SetExtOption(MenuOption option, const void *valuePtr)
520 IMenuPanel *CRadioMenu::CreatePanel()
522 return g_RadioMenuStyle.MakeRadioDisplay(this);
525 bool CRadioMenu::Display(int client, unsigned int time, IMenuHandler *alt_handler)
527 return DisplayAtItem(client, time, 0, alt_handler);
530 bool CRadioMenu::DisplayAtItem(int client,
532 unsigned int start_item,
533 IMenuHandler *alt_handler)
535 #if defined MENU_DEBUG
536 g_Logger.LogMessage("[SM_MENU] CRadioMenu::Display(%p) (client %d) (time %d)",
546 return g_RadioMenuStyle.DoClientMenu(client,
549 alt_handler ? alt_handler : m_pHandler,
553 void CRadioMenu::Cancel_Finally()
555 g_RadioMenuStyle.CancelMenu(this);
558 unsigned int CRadioMenu::GetApproxMemUsage()
560 return sizeof(CRadioMenu) + GetBaseMemUsage();
563 const char *g_RadioNumTable[11] =
565 "0. ", "1. ", "2. ", "3. ", "4. ", "5. ", "6. ", "7. ", "8. ", "9. ", "0. "