2 * vim: set ts=4 sw=4 tw=99 noet :
3 * =============================================================================
5 * Copyright (C) 2004-2010 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 #ifndef _INCLUDE_SOURCEMOD_CUSERMESSAGES_H_
33 #define _INCLUDE_SOURCEMOD_CUSERMESSAGES_H_
35 #include <IUserMessages.h>
36 #include "sourcemm_api.h"
37 #include <sm_stringhashmap.h>
38 #include "sm_stringutil.h"
39 #include "CellRecipientFilter.h"
40 #include "sm_globals.h"
44 using namespace SourceHook;
45 using namespace SourceMod;
47 #if SOURCE_ENGINE == SE_CSGO || SOURCE_ENGINE == SE_DOTA
48 #define USE_PROTOBUF_USERMESSAGES
51 #ifdef USE_PROTOBUF_USERMESSAGES
52 #include <google/protobuf/message.h>
53 #include <google/protobuf/descriptor.h>
54 #include <netmessages.pb.h>
56 using namespace google;
61 #define INVALID_MESSAGE_ID -1
65 #ifdef USE_PROTOBUF_USERMESSAGES
66 IProtobufUserMessageListener *Callback;
68 IBitBufUserMessageListener *Callback;
75 typedef List<ListenerInfo *> MsgList;
76 typedef List<ListenerInfo *>::iterator MsgIter;
85 public: //SMGlobalClass
86 void OnSourceModStartup(bool late);
87 void OnSourceModAllInitialized();
88 void OnSourceModAllShutdown();
89 public: //IUserMessages
90 int GetMessageIndex(const char *msg);
91 bool GetMessageName(int msgid, char *buffer, size_t maxlength) const;
92 bool HookUserMessage(int msg_id, IUserMessageListener *pListener, bool intercept=false);
93 bool UnhookUserMessage(int msg_id, IUserMessageListener *pListener, bool intercept=false);
94 bf_write *StartBitBufMessage(int msg_id, const cell_t players[], unsigned int playersNum, int flags);
95 google::protobuf::Message *StartProtobufMessage(int msg_id, const cell_t players[], unsigned int playersNum, int flags);
97 bool HookUserMessage2(int msg_id,
98 IUserMessageListener *pListener,
99 bool intercept=false);
100 bool UnhookUserMessage2(int msg_id,
101 IUserMessageListener *pListener,
102 bool intercept=false);
103 UserMessageType GetUserMessageType() const;
105 #if SOURCE_ENGINE == SE_CSGO || SOURCE_ENGINE == SE_DOTA
106 void OnSendUserMessage_Pre(IRecipientFilter &filter, int msg_type, const protobuf::Message &msg);
107 void OnSendUserMessage_Post(IRecipientFilter &filter, int msg_type, const protobuf::Message &msg);
110 #if SOURCE_ENGINE == SE_CSGO || SOURCE_ENGINE == SE_DOTA
111 protobuf::Message *OnStartMessage_Pre(IRecipientFilter *filter, int msg_type, const char *msg_name);
112 protobuf::Message *OnStartMessage_Post(IRecipientFilter *filter, int msg_type, const char *msg_name);
113 #elif SOURCE_ENGINE >= SE_LEFT4DEAD
114 bf_write *OnStartMessage_Pre(IRecipientFilter *filter, int msg_type, const char *msg_name);
115 bf_write *OnStartMessage_Post(IRecipientFilter *filter, int msg_type, const char *msg_name);
117 bf_write *OnStartMessage_Pre(IRecipientFilter *filter, int msg_type);
118 bf_write *OnStartMessage_Post(IRecipientFilter *filter, int msg_type);
120 void OnMessageEnd_Pre();
121 void OnMessageEnd_Post();
123 #ifdef USE_PROTOBUF_USERMESSAGES
124 const protobuf::Message *GetMessagePrototype(int msg_type);
125 bool InternalHook(int msg_id, IProtobufUserMessageListener *pListener, bool intercept, bool isNew);
126 bool InternalUnhook(int msg_id, IProtobufUserMessageListener *pListener, bool intercept, bool isNew);
128 bool InternalHook(int msg_id, IBitBufUserMessageListener *pListener, bool intercept, bool isNew);
129 bool InternalUnhook(int msg_id, IBitBufUserMessageListener *pListener, bool intercept, bool isNew);
131 void _DecRefCounter();
133 List<ListenerInfo *> m_msgHooks[255];
134 List<ListenerInfo *> m_msgIntercepts[255];
135 CStack<ListenerInfo *> m_FreeListeners;
136 IRecipientFilter *m_CurRecFilter;
137 #ifndef USE_PROTOBUF_USERMESSAGES
138 unsigned char m_pBase[2500];
139 bf_write m_InterceptBuffer;
140 bf_write *m_OrigBuffer;
141 bf_read m_ReadBuffer;
143 // The engine used to provide this. Now we track it.
144 protobuf::Message *m_OrigBuffer;
145 protobuf::Message *m_FakeEngineBuffer;
146 META_RES m_FakeMetaRes;
148 protobuf::Message *m_InterceptBuffer;
153 #ifndef USE_PROTOBUF_USERMESSAGES
154 bool m_FallbackSearch;
156 StringHashMap<int> m_Names;
158 CellRecipientFilter m_CellRecFilter;
164 extern UserMessages g_UserMsgs;
166 #endif //_INCLUDE_SOURCEMOD_CUSERMESSAGES_H_