From a9abbed7dcf5f35f4149fc4b8173a484663278f9 Mon Sep 17 00:00:00 2001 From: Scott Ehlert Date: Sat, 12 May 2007 04:47:58 +0000 Subject: [PATCH] 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 --- core/UserMessages.cpp | 44 +++++++++++++++++++++++++++++++++++++++++++ core/UserMessages.h | 3 +++ core/smn_usermsgs.cpp | 11 +++-------- 3 files changed, 50 insertions(+), 8 deletions(-) diff --git a/core/UserMessages.cpp b/core/UserMessages.cpp index 32b0d4ca..53e786a8 100644 --- a/core/UserMessages.cpp +++ b/core/UserMessages.cpp @@ -13,6 +13,7 @@ */ #include "UserMessages.h" +#include "sm_stringutil.h" UserMessages g_UserMsgs; @@ -41,6 +42,12 @@ UserMessages::~UserMessages() 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() { g_ShareSys.AddInterface(NULL, this); @@ -64,6 +71,23 @@ int UserMessages::GetMessageIndex(const char *msg) if (!sm_trie_retrieve(m_Names, msg, reinterpret_cast(&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(msgid)); + return msgid; + } + msgid++; + } + } + msgid = g_SMAPI->FindUserMessage(msg); if (msgid != INVALID_MESSAGE_ID) @@ -75,6 +99,26 @@ int UserMessages::GetMessageIndex(const char *msg) 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 *buffer; diff --git a/core/UserMessages.h b/core/UserMessages.h index 17571479..222f2a25 100644 --- a/core/UserMessages.h +++ b/core/UserMessages.h @@ -44,10 +44,12 @@ public: UserMessages(); ~UserMessages(); public: //SMGlobalClass + void OnSourceModStartup(bool late); void OnSourceModAllInitialized(); void OnSourceModAllShutdown(); public: //IUserMessages 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 UnhookUserMessage(int msg_id, IUserMessageListener *pListener, bool intercept=false); bf_write *StartMessage(int msg_id, cell_t players[], unsigned int playersNum, int flags); @@ -71,6 +73,7 @@ private: size_t m_HookCount; bool m_InHook; bool m_BlockEndPost; + bool m_FallbackSearch; Trie *m_Names; CellRecipientFilter m_CellRecFilter; diff --git a/core/smn_usermsgs.cpp b/core/smn_usermsgs.cpp index f7570802..5e45f17e 100644 --- a/core/smn_usermsgs.cpp +++ b/core/smn_usermsgs.cpp @@ -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) { - const char *msgname = g_SMAPI->GetUserMessage(params[1]); + char *msgname; - if (msgname == NULL) - { - return 0; - } + pCtx->LocalToPhysAddr(params[2], (cell_t **)&msgname); - pCtx->StringToLocalUTF8(params[2], params[3], msgname, NULL); - - return 1; + return g_UserMsgs.GetMessageName(params[1], msgname, params[3]); } static cell_t smn_StartMessage(IPluginContext *pCtx, const cell_t *params)