User message functions now fallback on IServerGameDLL::GetUserMessageInfo() if SourceMM was unable to get the message list for some reason.
--HG-- extra : convert_revision : svn%3A39bc706e-5318-0410-9160-8a85361fbb7c/trunk%40771
This commit is contained in:
		
							parent
							
								
									47e359d195
								
							
						
					
					
						commit
						a9abbed7dc
					
				@ -13,6 +13,7 @@
 | 
				
			|||||||
 */
 | 
					 */
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#include "UserMessages.h"
 | 
					#include "UserMessages.h"
 | 
				
			||||||
 | 
					#include "sm_stringutil.h"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
UserMessages g_UserMsgs;
 | 
					UserMessages g_UserMsgs;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -41,6 +42,12 @@ UserMessages::~UserMessages()
 | 
				
			|||||||
	m_FreeListeners.popall();
 | 
						m_FreeListeners.popall();
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					void UserMessages::OnSourceModStartup(bool late)
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
						/* -1 means SourceMM was unable to get the user message list */
 | 
				
			||||||
 | 
						m_FallbackSearch = (g_SMAPI->GetUserMessageCount() == -1);
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
void UserMessages::OnSourceModAllInitialized()
 | 
					void UserMessages::OnSourceModAllInitialized()
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
	g_ShareSys.AddInterface(NULL, this);
 | 
						g_ShareSys.AddInterface(NULL, this);
 | 
				
			||||||
@ -64,6 +71,23 @@ int UserMessages::GetMessageIndex(const char *msg)
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
	if (!sm_trie_retrieve(m_Names, msg, reinterpret_cast<void **>(&msgid)))
 | 
						if (!sm_trie_retrieve(m_Names, msg, reinterpret_cast<void **>(&msgid)))
 | 
				
			||||||
	{
 | 
						{
 | 
				
			||||||
 | 
							if (m_FallbackSearch)
 | 
				
			||||||
 | 
							{
 | 
				
			||||||
 | 
								char msgbuf[64];
 | 
				
			||||||
 | 
								int size;
 | 
				
			||||||
 | 
								msgid = 0;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
								while (gamedll->GetUserMessageInfo(msgid, msgbuf, sizeof(msgbuf), size))
 | 
				
			||||||
 | 
								{
 | 
				
			||||||
 | 
									if (strcmp(msgbuf, msg) == 0)
 | 
				
			||||||
 | 
									{
 | 
				
			||||||
 | 
										sm_trie_insert(m_Names, msg, reinterpret_cast<void *>(msgid));
 | 
				
			||||||
 | 
										return msgid;
 | 
				
			||||||
 | 
									}
 | 
				
			||||||
 | 
									msgid++;
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		msgid = g_SMAPI->FindUserMessage(msg);
 | 
							msgid = g_SMAPI->FindUserMessage(msg);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		if (msgid != INVALID_MESSAGE_ID)
 | 
							if (msgid != INVALID_MESSAGE_ID)
 | 
				
			||||||
@ -75,6 +99,26 @@ int UserMessages::GetMessageIndex(const char *msg)
 | 
				
			|||||||
	return msgid;
 | 
						return msgid;
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					bool UserMessages::GetMessageName(int msgid, char *buffer, size_t maxlength) const
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
						if (m_FallbackSearch)
 | 
				
			||||||
 | 
						{
 | 
				
			||||||
 | 
							printf("hello");
 | 
				
			||||||
 | 
							int size;
 | 
				
			||||||
 | 
							return gamedll->GetUserMessageInfo(msgid, buffer, maxlength, size);
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						printf("hello2");
 | 
				
			||||||
 | 
						const char *msg = g_SMAPI->GetUserMessage(msgid);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						if (msg)
 | 
				
			||||||
 | 
						{
 | 
				
			||||||
 | 
							strncopy(buffer, msg, maxlength);
 | 
				
			||||||
 | 
							return true;
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						return false;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
bf_write *UserMessages::StartMessage(int msg_id, cell_t players[], unsigned int playersNum, int flags)
 | 
					bf_write *UserMessages::StartMessage(int msg_id, cell_t players[], unsigned int playersNum, int flags)
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
	bf_write *buffer;
 | 
						bf_write *buffer;
 | 
				
			||||||
 | 
				
			|||||||
@ -44,10 +44,12 @@ public:
 | 
				
			|||||||
	UserMessages();
 | 
						UserMessages();
 | 
				
			||||||
	~UserMessages();
 | 
						~UserMessages();
 | 
				
			||||||
public: //SMGlobalClass
 | 
					public: //SMGlobalClass
 | 
				
			||||||
 | 
						void OnSourceModStartup(bool late);
 | 
				
			||||||
	void OnSourceModAllInitialized();
 | 
						void OnSourceModAllInitialized();
 | 
				
			||||||
	void OnSourceModAllShutdown();
 | 
						void OnSourceModAllShutdown();
 | 
				
			||||||
public: //IUserMessages
 | 
					public: //IUserMessages
 | 
				
			||||||
	int GetMessageIndex(const char *msg);
 | 
						int GetMessageIndex(const char *msg);
 | 
				
			||||||
 | 
						bool GetMessageName(int msgid, char *buffer, size_t maxlength) const;
 | 
				
			||||||
	bool HookUserMessage(int msg_id, IUserMessageListener *pListener, bool intercept=false);
 | 
						bool HookUserMessage(int msg_id, IUserMessageListener *pListener, bool intercept=false);
 | 
				
			||||||
	bool UnhookUserMessage(int msg_id, IUserMessageListener *pListener, bool intercept=false);
 | 
						bool UnhookUserMessage(int msg_id, IUserMessageListener *pListener, bool intercept=false);
 | 
				
			||||||
	bf_write *StartMessage(int msg_id, cell_t players[], unsigned int playersNum, int flags);
 | 
						bf_write *StartMessage(int msg_id, cell_t players[], unsigned int playersNum, int flags);
 | 
				
			||||||
@ -71,6 +73,7 @@ private:
 | 
				
			|||||||
	size_t m_HookCount;
 | 
						size_t m_HookCount;
 | 
				
			||||||
	bool m_InHook;
 | 
						bool m_InHook;
 | 
				
			||||||
	bool m_BlockEndPost;
 | 
						bool m_BlockEndPost;
 | 
				
			||||||
 | 
						bool m_FallbackSearch;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	Trie *m_Names;
 | 
						Trie *m_Names;
 | 
				
			||||||
	CellRecipientFilter m_CellRecFilter;
 | 
						CellRecipientFilter m_CellRecFilter;
 | 
				
			||||||
 | 
				
			|||||||
@ -310,16 +310,11 @@ static cell_t smn_GetUserMessageId(IPluginContext *pCtx, const cell_t *params)
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
static cell_t smn_GetUserMessageName(IPluginContext *pCtx, const cell_t *params)
 | 
					static cell_t smn_GetUserMessageName(IPluginContext *pCtx, const cell_t *params)
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
	const char *msgname = g_SMAPI->GetUserMessage(params[1]);
 | 
						char *msgname;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	if (msgname == NULL)
 | 
						pCtx->LocalToPhysAddr(params[2], (cell_t **)&msgname);
 | 
				
			||||||
	{
 | 
					 | 
				
			||||||
		return 0;
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
	pCtx->StringToLocalUTF8(params[2], params[3], msgname, NULL);
 | 
						return g_UserMsgs.GetMessageName(params[1], msgname, params[3]);
 | 
				
			||||||
 | 
					 | 
				
			||||||
	return 1;
 | 
					 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
static cell_t smn_StartMessage(IPluginContext *pCtx, const cell_t *params)
 | 
					static cell_t smn_StartMessage(IPluginContext *pCtx, const cell_t *params)
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
		Reference in New Issue
	
	Block a user