core/UserMessages.h
author David Anderson <dvander@alliedmods.net>
Sun Aug 26 17:41:02 2007 +0000 (2007-08-26)
changeset 1369 bd9ccced1b1a
parent 1330 ab94316bb52b
child 1690 7cc4f79a0416
permissions -rw-r--r--
fixed amb834 - command callbacks could potentially occur on invalid clients in very rare cases
dvander@592
     1
/**
ds@653
     2
 * vim: set ts=4 :
ds@1330
     3
 * =============================================================================
dvander@1231
     4
 * SourceMod
dvander@1231
     5
 * Copyright (C) 2004-2007 AlliedModders LLC.  All rights reserved.
ds@1330
     6
 * =============================================================================
ds@653
     7
 *
ds@1330
     8
 * This program is free software; you can redistribute it and/or modify it under
ds@1330
     9
 * the terms of the GNU General Public License, version 3.0, as published by the
ds@1330
    10
 * Free Software Foundation.
dvander@1231
    11
 * 
ds@1330
    12
 * This program is distributed in the hope that it will be useful, but WITHOUT
ds@1330
    13
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
ds@1330
    14
 * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
ds@1330
    15
 * details.
dvander@1231
    16
 *
ds@1330
    17
 * You should have received a copy of the GNU General Public License along with
ds@1330
    18
 * this program.  If not, see <http://www.gnu.org/licenses/>.
dvander@1231
    19
 *
ds@1330
    20
 * As a special exception, AlliedModders LLC gives you permission to link the
ds@1330
    21
 * code of this program (as well as its derivative works) to "Half-Life 2," the
ds@1330
    22
 * "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
ds@1330
    23
 * by the Valve Corporation.  You must obey the GNU General Public License in
ds@1330
    24
 * all respects for all other code used.  Additionally, AlliedModders LLC grants
ds@1330
    25
 * this exception to all derivative works.  AlliedModders LLC defines further
ds@1330
    26
 * exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
ds@1330
    27
 * or <http://www.sourcemod.net/license.php>.
ds@653
    28
 *
ds@653
    29
 * Version: $Id$
ds@653
    30
 */
dvander@592
    31
dvander@592
    32
#ifndef _INCLUDE_SOURCEMOD_CUSERMESSAGES_H_
dvander@592
    33
#define _INCLUDE_SOURCEMOD_CUSERMESSAGES_H_
dvander@592
    34
dvander@592
    35
#include "ShareSys.h"
dvander@592
    36
#include <IUserMessages.h>
dvander@592
    37
#include "sourcemm_api.h"
dvander@592
    38
#include "sm_trie.h"
dvander@592
    39
#include "CellRecipientFilter.h"
dvander@592
    40
dvander@592
    41
using namespace SourceHook;
dvander@592
    42
using namespace SourceMod;
dvander@592
    43
dvander@592
    44
#define INVALID_MESSAGE_ID -1
dvander@592
    45
faluco@593
    46
struct ListenerInfo
faluco@593
    47
{
faluco@593
    48
	IUserMessageListener *Callback;
faluco@593
    49
	bool IsHooked;
faluco@593
    50
	bool KillMe;
faluco@593
    51
};
faluco@593
    52
faluco@593
    53
typedef List<ListenerInfo *> MsgList;
faluco@593
    54
typedef List<ListenerInfo *>::iterator MsgIter;
dvander@592
    55
dvander@616
    56
class UserMessages : 
dvander@592
    57
	public IUserMessages,
dvander@592
    58
	public SMGlobalClass
dvander@592
    59
{
dvander@592
    60
public:
dvander@616
    61
	UserMessages();
dvander@616
    62
	~UserMessages();
dvander@592
    63
public: //SMGlobalClass
ds@765
    64
	void OnSourceModStartup(bool late);
dvander@592
    65
	void OnSourceModAllInitialized();
dvander@592
    66
	void OnSourceModAllShutdown();
dvander@592
    67
public: //IUserMessages
dvander@592
    68
	int GetMessageIndex(const char *msg);
ds@765
    69
	bool GetMessageName(int msgid, char *buffer, size_t maxlength) const;
dvander@592
    70
	bool HookUserMessage(int msg_id, IUserMessageListener *pListener, bool intercept=false);
dvander@592
    71
	bool UnhookUserMessage(int msg_id, IUserMessageListener *pListener, bool intercept=false);
dvander@592
    72
	bf_write *StartMessage(int msg_id, cell_t players[], unsigned int playersNum, int flags);
dvander@592
    73
	bool EndMessage();
dvander@592
    74
public:
dvander@592
    75
	bf_write *OnStartMessage_Pre(IRecipientFilter *filter, int msg_type);
dvander@592
    76
	bf_write *OnStartMessage_Post(IRecipientFilter *filter, int msg_type);
dvander@592
    77
	void OnMessageEnd_Pre();
dvander@592
    78
	void OnMessageEnd_Post();
dvander@592
    79
private:
faluco@593
    80
	void _DecRefCounter();
faluco@593
    81
private:
faluco@593
    82
	List<ListenerInfo *> m_msgHooks[255];
faluco@593
    83
	List<ListenerInfo *> m_msgIntercepts[255];
faluco@593
    84
	CStack<ListenerInfo *> m_FreeListeners;
dvander@592
    85
	unsigned char m_pBase[2500];
dvander@592
    86
	IRecipientFilter *m_CurRecFilter;
dvander@592
    87
	bf_write m_InterceptBuffer;
dvander@592
    88
	bf_write *m_OrigBuffer;
dvander@592
    89
	bf_read m_ReadBuffer;
dvander@592
    90
	size_t m_HookCount;
dvander@592
    91
	bool m_InHook;
faluco@597
    92
	bool m_BlockEndPost;
ds@765
    93
	bool m_FallbackSearch;
dvander@592
    94
dvander@592
    95
	Trie *m_Names;
dvander@592
    96
	CellRecipientFilter m_CellRecFilter;
dvander@592
    97
	bool m_InExec;
dvander@592
    98
	int m_CurFlags;
dvander@592
    99
	int m_CurId;
dvander@592
   100
};
dvander@592
   101
dvander@616
   102
extern UserMessages g_UserMsgs;
dvander@592
   103
dvander@592
   104
#endif //_INCLUDE_SOURCEMOD_CUSERMESSAGES_H_