core/UserMessages.h
author Nicholas Hastings <skamonkey@gmail.com>
Tue Jan 22 21:43:12 2013 -0500 (2013-01-22)
changeset 3743 a98686a77974
parent 3099 1b7bf8509625
child 3815 05a9a13701c4
permissions -rw-r--r--
Added initial support for protobuf usermessages on CS:GO (bug 5579, 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 "ShareSys.h"
    36 #include <IUserMessages.h>
    37 #include "sourcemm_api.h"
    38 #include "sm_trie.h"
    39 #include "sm_stringutil.h"
    40 #include "CellRecipientFilter.h"
    41 
    42 using namespace SourceHook;
    43 using namespace SourceMod;
    44 
    45 #if SOURCE_ENGINE == SE_CSGO
    46 #define USE_PROTOBUF_USERMESSAGES
    47 #endif
    48 
    49 #ifdef USE_PROTOBUF_USERMESSAGES
    50 #include <google/protobuf/message.h>
    51 #include <google/protobuf/descriptor.h>
    52 #include <netmessages.pb.h>
    53 
    54 using namespace google;
    55 #else
    56 #include <bitbuf.h>
    57 #endif
    58 
    59 #define INVALID_MESSAGE_ID -1
    60 
    61 struct ListenerInfo
    62 {
    63 #ifdef USE_PROTOBUF_USERMESSAGES
    64 	IProtobufUserMessageListener *Callback;
    65 #else
    66 	IBitBufUserMessageListener *Callback;
    67 #endif
    68 	bool IsHooked;
    69 	bool KillMe;
    70 	bool IsNew;
    71 };
    72 
    73 typedef List<ListenerInfo *> MsgList;
    74 typedef List<ListenerInfo *>::iterator MsgIter;
    75 
    76 class UserMessages : 
    77 	public IUserMessages,
    78 	public SMGlobalClass
    79 {
    80 public:
    81 	UserMessages();
    82 	~UserMessages();
    83 public: //SMGlobalClass
    84 	void OnSourceModStartup(bool late);
    85 	void OnSourceModAllInitialized();
    86 	void OnSourceModAllShutdown();
    87 public: //IUserMessages
    88 	int GetMessageIndex(const char *msg);
    89 	bool GetMessageName(int msgid, char *buffer, size_t maxlength) const;
    90 	bool HookUserMessage(int msg_id, IUserMessageListener *pListener, bool intercept=false);
    91 	bool UnhookUserMessage(int msg_id, IUserMessageListener *pListener, bool intercept=false);
    92 	bf_write *StartBitBufMessage(int msg_id, const cell_t players[], unsigned int playersNum, int flags);
    93 	google::protobuf::Message *StartProtobufMessage(int msg_id, const cell_t players[], unsigned int playersNum, int flags);
    94 	bool EndMessage();
    95 	bool HookUserMessage2(int msg_id,
    96 		IUserMessageListener *pListener,
    97 		bool intercept=false);
    98 	bool UnhookUserMessage2(int msg_id,
    99 		IUserMessageListener *pListener,
   100 		bool intercept=false);
   101 	UserMessageType GetUserMessageType() const;
   102 public:
   103 #if SOURCE_ENGINE == SE_CSGO
   104 	void OnSendUserMessage_Pre(IRecipientFilter &filter, int msg_type, const protobuf::Message &msg);
   105 	void OnSendUserMessage_Post(IRecipientFilter &filter, int msg_type, const protobuf::Message &msg);
   106 #endif
   107 
   108 #if SOURCE_ENGINE == SE_CSGO
   109 	protobuf::Message *OnStartMessage_Pre(IRecipientFilter *filter, int msg_type, const char *msg_name);
   110 	protobuf::Message *OnStartMessage_Post(IRecipientFilter *filter, int msg_type, const char *msg_name);
   111 #elif SOURCE_ENGINE >= SE_LEFT4DEAD
   112 	bf_write *OnStartMessage_Pre(IRecipientFilter *filter, int msg_type, const char *msg_name);
   113 	bf_write *OnStartMessage_Post(IRecipientFilter *filter, int msg_type, const char *msg_name);
   114 #else
   115 	bf_write *OnStartMessage_Pre(IRecipientFilter *filter, int msg_type);
   116 	bf_write *OnStartMessage_Post(IRecipientFilter *filter, int msg_type);
   117 #endif
   118 	void OnMessageEnd_Pre();
   119 	void OnMessageEnd_Post();
   120 private:
   121 #ifdef USE_PROTOBUF_USERMESSAGES
   122 	bool InternalHook(int msg_id, IProtobufUserMessageListener *pListener, bool intercept, bool isNew);
   123 	bool InternalUnhook(int msg_id, IProtobufUserMessageListener *pListener, bool intercept, bool isNew);
   124 #else
   125 	bool InternalHook(int msg_id, IBitBufUserMessageListener *pListener, bool intercept, bool isNew);
   126 	bool InternalUnhook(int msg_id, IBitBufUserMessageListener *pListener, bool intercept, bool isNew);
   127 #endif
   128 	void _DecRefCounter();
   129 private:
   130 	List<ListenerInfo *> m_msgHooks[255];
   131 	List<ListenerInfo *> m_msgIntercepts[255];
   132 	CStack<ListenerInfo *> m_FreeListeners;
   133 	unsigned char m_pBase[2500];
   134 	IRecipientFilter *m_CurRecFilter;
   135 #ifndef USE_PROTOBUF_USERMESSAGES
   136 	bf_write m_InterceptBuffer;
   137 	bf_write *m_OrigBuffer;
   138 	bf_read m_ReadBuffer;
   139 #else
   140 	// The engine used to provide this. Now we track it.
   141 	protobuf::Message *m_OrigBuffer;
   142 	protobuf::Message *m_FakeEngineBuffer;
   143 	META_RES m_FakeMetaRes;
   144 
   145 	protobuf::Message *m_InterceptBuffer;
   146 #endif
   147 	size_t m_HookCount;
   148 	bool m_InHook;
   149 	bool m_BlockEndPost;
   150 #ifndef USE_PROTOBUF_USERMESSAGES
   151 	bool m_FallbackSearch;
   152 
   153 	Trie *m_Names;
   154 #endif
   155 	CellRecipientFilter m_CellRecFilter;
   156 	bool m_InExec;
   157 	int m_CurFlags;
   158 	int m_CurId;
   159 };
   160 
   161 extern UserMessages g_UserMsgs;
   162 
   163 #endif //_INCLUDE_SOURCEMOD_CUSERMESSAGES_H_