core/UserMessages.h
author Kyle Sanderson <kyle.leet@gmail.com>
Mon Aug 12 00:44:22 2013 +0100 (2013-08-12)
changeset 3995 e94dfd569130
parent 3872 4c93f74cae51
child 4073 56283c919b7b
permissions -rw-r--r--
Fixed various memory issues. (bug 5766, r=asherkin)
     1 /**
     2  * vim: set ts=4 :
     3  * =============================================================================
     4  * SourceMod
     5  * Copyright (C) 2004-2010 AlliedModders LLC.  All rights reserved.
     6  * =============================================================================
     7  *
     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.
    11  * 
    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
    15  * details.
    16  *
    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/>.
    19  *
    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>.
    28  *
    29  * Version: $Id$
    30  */
    31 
    32 #ifndef _INCLUDE_SOURCEMOD_CUSERMESSAGES_H_
    33 #define _INCLUDE_SOURCEMOD_CUSERMESSAGES_H_
    34 
    35 #include <IUserMessages.h>
    36 #include "sourcemm_api.h"
    37 #include "sm_trie.h"
    38 #include "sm_stringutil.h"
    39 #include "CellRecipientFilter.h"
    40 #include "sm_globals.h"
    41 #include <sh_list.h>
    42 #include <sh_stack.h>
    43 
    44 using namespace SourceHook;
    45 using namespace SourceMod;
    46 
    47 #if SOURCE_ENGINE == SE_CSGO || SOURCE_ENGINE == SE_DOTA
    48 #define USE_PROTOBUF_USERMESSAGES
    49 #endif
    50 
    51 #ifdef USE_PROTOBUF_USERMESSAGES
    52 #include <google/protobuf/message.h>
    53 #include <google/protobuf/descriptor.h>
    54 #include <netmessages.pb.h>
    55 
    56 using namespace google;
    57 #else
    58 #include <bitbuf.h>
    59 #endif
    60 
    61 #define INVALID_MESSAGE_ID -1
    62 
    63 struct ListenerInfo
    64 {
    65 #ifdef USE_PROTOBUF_USERMESSAGES
    66 	IProtobufUserMessageListener *Callback;
    67 #else
    68 	IBitBufUserMessageListener *Callback;
    69 #endif
    70 	bool IsHooked;
    71 	bool KillMe;
    72 	bool IsNew;
    73 };
    74 
    75 typedef List<ListenerInfo *> MsgList;
    76 typedef List<ListenerInfo *>::iterator MsgIter;
    77 
    78 class UserMessages : 
    79 	public IUserMessages,
    80 	public SMGlobalClass
    81 {
    82 public:
    83 	UserMessages();
    84 	~UserMessages();
    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);
    96 	bool EndMessage();
    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;
   104 public:
   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);
   108 #endif
   109 
   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);
   116 #else
   117 	bf_write *OnStartMessage_Pre(IRecipientFilter *filter, int msg_type);
   118 	bf_write *OnStartMessage_Post(IRecipientFilter *filter, int msg_type);
   119 #endif
   120 	void OnMessageEnd_Pre();
   121 	void OnMessageEnd_Post();
   122 private:
   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);
   127 #else
   128 	bool InternalHook(int msg_id, IBitBufUserMessageListener *pListener, bool intercept, bool isNew);
   129 	bool InternalUnhook(int msg_id, IBitBufUserMessageListener *pListener, bool intercept, bool isNew);
   130 #endif
   131 	void _DecRefCounter();
   132 private:
   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;
   142 #else
   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;
   147 
   148 	protobuf::Message *m_InterceptBuffer;
   149 #endif
   150 	size_t m_HookCount;
   151 	bool m_InHook;
   152 	bool m_BlockEndPost;
   153 #ifndef USE_PROTOBUF_USERMESSAGES
   154 	bool m_FallbackSearch;
   155 
   156 	Trie *m_Names;
   157 #endif
   158 	CellRecipientFilter m_CellRecFilter;
   159 	bool m_InExec;
   160 	int m_CurFlags;
   161 	int m_CurId;
   162 };
   163 
   164 extern UserMessages g_UserMsgs;
   165 
   166 #endif //_INCLUDE_SOURCEMOD_CUSERMESSAGES_H_