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:
Scott Ehlert 2007-05-12 04:47:58 +00:00
parent 47e359d195
commit a9abbed7dc
3 changed files with 50 additions and 8 deletions

View File

@ -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<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);
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;

View File

@ -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;

View File

@ -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)