core/smn_usermsgs.cpp
author David Anderson <dvander@alliedmods.net>
Tue Oct 02 01:50:36 2007 +0000 (2007-10-02)
changeset 1519 c325cd4b060a
parent 1397 c105f054c72e
child 1541 e46ad208b9b4
permissions -rw-r--r--
added reply-to stuff for the C++ API
faluco@505
     1
/**
dvander@600
     2
 * vim: set ts=4 :
ds@1330
     3
 * =============================================================================
dvander@1231
     4
 * SourceMod
dvander@1231
     5
 * Copyright (C) 2004-2007 AlliedModders LLC.  All rights reserved.
ds@1330
     6
 * =============================================================================
dvander@600
     7
 *
ds@1330
     8
 * This program is free software; you can redistribute it and/or modify it under
ds@1330
     9
 * the terms of the GNU General Public License, version 3.0, as published by the
ds@1330
    10
 * Free Software Foundation.
dvander@1231
    11
 * 
ds@1330
    12
 * This program is distributed in the hope that it will be useful, but WITHOUT
ds@1330
    13
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
ds@1330
    14
 * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
ds@1330
    15
 * details.
dvander@1231
    16
 *
ds@1330
    17
 * You should have received a copy of the GNU General Public License along with
ds@1330
    18
 * this program.  If not, see <http://www.gnu.org/licenses/>.
dvander@1231
    19
 *
ds@1330
    20
 * As a special exception, AlliedModders LLC gives you permission to link the
ds@1330
    21
 * code of this program (as well as its derivative works) to "Half-Life 2," the
ds@1330
    22
 * "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
ds@1330
    23
 * by the Valve Corporation.  You must obey the GNU General Public License in
ds@1330
    24
 * all respects for all other code used.  Additionally, AlliedModders LLC grants
ds@1330
    25
 * this exception to all derivative works.  AlliedModders LLC defines further
ds@1330
    26
 * exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
ds@1330
    27
 * or <http://www.sourcemod.net/license.php>.
dvander@600
    28
 *
dvander@600
    29
 * Version: $Id$
dvander@600
    30
 */
faluco@505
    31
faluco@505
    32
#include "HandleSys.h"
faluco@552
    33
#include "PluginSys.h"
dvander@592
    34
#include "UserMessages.h"
dvander@590
    35
#include "smn_usermsgs.h"
faluco@505
    36
faluco@505
    37
HandleType_t g_WrBitBufType;
faluco@598
    38
HandleType_t g_RdBitBufType;
faluco@505
    39
Handle_t g_CurMsgHandle;
faluco@598
    40
Handle_t g_ReadBufHandle;
faluco@598
    41
bf_read g_ReadBitBuf;
faluco@598
    42
faluco@552
    43
int g_MsgPlayers[256];
faluco@505
    44
bool g_IsMsgInExec = false;
faluco@505
    45
faluco@593
    46
typedef List<MsgListenerWrapper *> MsgWrapperList;
faluco@593
    47
typedef List<MsgListenerWrapper *>::iterator MsgWrapperIter;
faluco@593
    48
faluco@505
    49
class UsrMessageNatives :
faluco@505
    50
	public SMGlobalClass,
faluco@552
    51
	public IHandleTypeDispatch,
faluco@552
    52
	public IPluginsListener
faluco@505
    53
{
faluco@641
    54
public:
faluco@641
    55
	~UsrMessageNatives();
faluco@552
    56
public: //SMGlobalClass, IHandleTypeDispatch, IPluginListener
faluco@552
    57
	void OnSourceModAllInitialized();
faluco@552
    58
	void OnSourceModShutdown();
faluco@552
    59
	void OnHandleDestroy(HandleType_t type, void *object);
faluco@552
    60
	void OnPluginUnloaded(IPlugin *plugin);
faluco@505
    61
public:
faluco@593
    62
	MsgListenerWrapper *CreateListener(IPluginContext *pCtx);
faluco@593
    63
	MsgWrapperIter FindListener(int msgid, IPluginContext *pCtx, IPluginFunction *pHook, bool intercept);
faluco@593
    64
	bool DeleteListener(IPluginContext *pCtx, MsgWrapperIter iter);
faluco@552
    65
private:
faluco@552
    66
	CStack<MsgListenerWrapper *> m_FreeListeners;
faluco@552
    67
};
faluco@552
    68
faluco@641
    69
UsrMessageNatives::~UsrMessageNatives()
faluco@641
    70
{
faluco@641
    71
	CStack<MsgListenerWrapper *>::iterator iter;
faluco@641
    72
	for (iter=m_FreeListeners.begin(); iter!=m_FreeListeners.end(); iter++)
faluco@641
    73
	{
faluco@641
    74
		delete (*iter);
faluco@641
    75
	}
faluco@641
    76
	m_FreeListeners.popall();
faluco@641
    77
}
faluco@641
    78
faluco@552
    79
void UsrMessageNatives::OnSourceModAllInitialized()
faluco@552
    80
{
faluco@598
    81
	HandleAccess sec;
faluco@1397
    82
faluco@1397
    83
	g_HandleSys.InitAccessDefaults(NULL, &sec);
faluco@1397
    84
	sec.access[HandleAccess_Delete] = HANDLE_RESTRICT_IDENTITY;
faluco@598
    85
faluco@552
    86
	g_WrBitBufType = g_HandleSys.CreateType("BitBufWriter", this, 0, NULL, NULL, g_pCoreIdent, NULL);
faluco@598
    87
	g_RdBitBufType = g_HandleSys.CreateType("BitBufReader", this, 0, NULL, &sec, g_pCoreIdent, NULL);
faluco@598
    88
faluco@598
    89
	g_ReadBufHandle = g_HandleSys.CreateHandle(g_RdBitBufType, &g_ReadBitBuf, NULL, g_pCoreIdent, NULL);
faluco@598
    90
faluco@552
    91
	g_PluginSys.AddPluginsListener(this);
faluco@552
    92
}
faluco@552
    93
faluco@552
    94
void UsrMessageNatives::OnSourceModShutdown()
faluco@552
    95
{
faluco@598
    96
	HandleSecurity sec;
faluco@598
    97
	sec.pIdentity = g_pCoreIdent;
faluco@598
    98
faluco@598
    99
	g_HandleSys.FreeHandle(g_ReadBufHandle, &sec);
faluco@598
   100
faluco@552
   101
	g_HandleSys.RemoveType(g_WrBitBufType, g_pCoreIdent);
faluco@598
   102
	g_HandleSys.RemoveType(g_RdBitBufType, g_pCoreIdent);
faluco@598
   103
faluco@552
   104
	g_WrBitBufType = 0;
faluco@598
   105
	g_RdBitBufType = 0;
faluco@552
   106
}
faluco@553
   107
faluco@552
   108
void UsrMessageNatives::OnHandleDestroy(HandleType_t type, void *object)
faluco@552
   109
{
faluco@552
   110
}
faluco@552
   111
faluco@552
   112
void UsrMessageNatives::OnPluginUnloaded(IPlugin *plugin)
faluco@552
   113
{
faluco@593
   114
	MsgWrapperList *pList;
faluco@552
   115
faluco@593
   116
	if (plugin->GetProperty("MsgListeners", reinterpret_cast<void **>(&pList), true))
faluco@505
   117
	{
faluco@593
   118
		MsgWrapperIter iter;
faluco@593
   119
		MsgListenerWrapper *pListener;
faluco@552
   120
faluco@593
   121
		for (iter=pList->begin(); iter!=pList->end(); iter++)
faluco@552
   122
		{
faluco@593
   123
			pListener = (*iter);
faluco@593
   124
			if (g_UserMsgs.UnhookUserMessage(pListener->GetMessageId(), pListener, pListener->IsInterceptHook()))
faluco@552
   125
			{
faluco@593
   126
				m_FreeListeners.push(pListener);
faluco@552
   127
			}
faluco@552
   128
		}
faluco@552
   129
faluco@593
   130
		delete pList;
faluco@552
   131
	}
faluco@552
   132
}
faluco@552
   133
faluco@593
   134
MsgListenerWrapper *UsrMessageNatives::CreateListener(IPluginContext *pCtx)
faluco@552
   135
{
faluco@593
   136
	MsgWrapperList *pList;
faluco@593
   137
	MsgListenerWrapper *pListener;
faluco@593
   138
	IPlugin *pl = g_PluginSys.FindPluginByContext(pCtx->GetContext());
faluco@552
   139
faluco@552
   140
	if (m_FreeListeners.empty())
faluco@552
   141
	{
faluco@593
   142
		pListener = new MsgListenerWrapper;
faluco@552
   143
	} else {
faluco@593
   144
		pListener = m_FreeListeners.front();
faluco@552
   145
		m_FreeListeners.pop();
faluco@505
   146
	}
faluco@552
   147
faluco@593
   148
	if (!pl->GetProperty("MsgListeners", reinterpret_cast<void **>(&pList)))
faluco@505
   149
	{
faluco@593
   150
		pList = new List<MsgListenerWrapper *>;
faluco@593
   151
		pl->SetProperty("MsgListeners", pList);
faluco@552
   152
	}
faluco@552
   153
faluco@593
   154
	pList->push_back(pListener);
faluco@552
   155
faluco@593
   156
	return pListener;
faluco@552
   157
}
faluco@552
   158
faluco@593
   159
MsgWrapperIter UsrMessageNatives::FindListener(int msgid, IPluginContext *pCtx, IPluginFunction *pHook, bool intercept)
faluco@552
   160
{
faluco@593
   161
	MsgWrapperList *pList;
faluco@593
   162
	MsgWrapperIter iter;
faluco@593
   163
	MsgListenerWrapper *pListener;
faluco@552
   164
	IPlugin *pl = g_PluginSys.FindPluginByContext(pCtx->GetContext());
faluco@552
   165
faluco@593
   166
	if (!pl->GetProperty("MsgListeners", reinterpret_cast<void **>(&pList)))
faluco@552
   167
	{
faluco@552
   168
		return NULL;
faluco@505
   169
	}
faluco@552
   170
faluco@593
   171
	for (iter=pList->begin(); iter!=pList->end(); iter++)
faluco@505
   172
	{
faluco@593
   173
		pListener = (*iter);
faluco@593
   174
		if ((msgid == pListener->GetMessageId()) 
faluco@593
   175
			&& (intercept == pListener->IsInterceptHook()) 
faluco@593
   176
			&& (pHook == pListener->GetHookedFunction()))
faluco@552
   177
		{
faluco@593
   178
			return iter;
faluco@552
   179
		}
faluco@505
   180
	}
faluco@552
   181
faluco@593
   182
	return NULL;
faluco@552
   183
}
faluco@553
   184
faluco@593
   185
bool UsrMessageNatives::DeleteListener(IPluginContext *pCtx, MsgWrapperIter iter)
faluco@552
   186
{
faluco@593
   187
	MsgWrapperList *pList;
faluco@593
   188
	MsgListenerWrapper *pListener;
faluco@552
   189
	IPlugin *pl = g_PluginSys.FindPluginByContext(pCtx->GetContext());
faluco@552
   190
faluco@593
   191
	if (!pl->GetProperty("MsgListeners", reinterpret_cast<void **>(&pList)))
faluco@552
   192
	{
faluco@552
   193
		return false;
faluco@552
   194
	}
faluco@552
   195
faluco@593
   196
	pListener = (*iter);
faluco@593
   197
	pList->erase(iter);
faluco@593
   198
	m_FreeListeners.push(pListener);
faluco@552
   199
faluco@552
   200
	return true;
faluco@552
   201
}
faluco@552
   202
dvander@590
   203
/***************************************
dvander@590
   204
 *                                     *
dvander@590
   205
 * USER MESSAGE WRAPPER IMPLEMENTATION *
dvander@590
   206
 *                                     *
dvander@590
   207
 ***************************************/
dvander@590
   208
faluco@593
   209
void MsgListenerWrapper::Initialize(int msgid, IPluginFunction *hook, IPluginFunction *notify, bool intercept)
faluco@593
   210
{
faluco@593
   211
	if (intercept)
faluco@593
   212
	{
faluco@593
   213
		m_Intercept = hook;
faluco@593
   214
		m_Hook = NULL;
faluco@593
   215
	} else {
faluco@593
   216
		m_Hook = hook;
faluco@593
   217
		m_Intercept = NULL;
faluco@593
   218
	}
faluco@593
   219
faluco@593
   220
	if (notify)
faluco@593
   221
	{
faluco@593
   222
		m_Notify = notify;
faluco@593
   223
	} else {
faluco@593
   224
		m_Notify = NULL;
faluco@593
   225
	}
faluco@593
   226
faluco@593
   227
	m_MsgId = msgid;
faluco@593
   228
	m_IsInterceptHook = intercept;
faluco@593
   229
}
faluco@593
   230
faluco@593
   231
size_t MsgListenerWrapper::_FillInPlayers(int *pl_array, IRecipientFilter *pFilter)
dvander@590
   232
{
dvander@590
   233
	size_t size = static_cast<size_t>(pFilter->GetRecipientCount());
dvander@590
   234
dvander@590
   235
	for (size_t i=0; i<size; i++)
dvander@590
   236
	{
dvander@590
   237
		pl_array[i] = pFilter->GetRecipientIndex(i);
dvander@590
   238
	}
dvander@590
   239
dvander@590
   240
	return size;
dvander@590
   241
}
dvander@590
   242
dvander@590
   243
bool MsgListenerWrapper::IsInterceptHook() const
dvander@590
   244
{
dvander@590
   245
	return m_IsInterceptHook;
dvander@590
   246
}
dvander@590
   247
dvander@590
   248
int MsgListenerWrapper::GetMessageId() const
dvander@590
   249
{
dvander@590
   250
	return m_MsgId;
dvander@590
   251
}
dvander@590
   252
dvander@590
   253
IPluginFunction *MsgListenerWrapper::GetHookedFunction() const
dvander@590
   254
{
dvander@590
   255
	if (m_Hook)
dvander@590
   256
	{
dvander@590
   257
		return m_Hook;
dvander@590
   258
	} else {
dvander@590
   259
		return m_Intercept;
dvander@590
   260
	}
dvander@590
   261
}
dvander@590
   262
dvander@590
   263
IPluginFunction *MsgListenerWrapper::GetNotifyFunction() const
dvander@590
   264
{
dvander@590
   265
	return m_Notify;
dvander@590
   266
}
dvander@590
   267
dvander@590
   268
void MsgListenerWrapper::OnUserMessage(int msg_id, bf_write *bf, IRecipientFilter *pFilter)
dvander@590
   269
{
dvander@590
   270
	cell_t res;
faluco@593
   271
	size_t size = _FillInPlayers(g_MsgPlayers, pFilter);
dvander@590
   272
faluco@598
   273
	g_ReadBitBuf.StartReading(bf->GetBasePointer(), bf->GetNumBytesWritten());
faluco@598
   274
dvander@590
   275
	m_Hook->PushCell(msg_id);
faluco@598
   276
	m_Hook->PushCell(g_ReadBufHandle);
dvander@590
   277
	m_Hook->PushArray(g_MsgPlayers, size);
dvander@590
   278
	m_Hook->PushCell(size);
dvander@590
   279
	m_Hook->PushCell(pFilter->IsReliable());
dvander@590
   280
	m_Hook->PushCell(pFilter->IsInitMessage());
dvander@590
   281
	m_Hook->Execute(&res);
dvander@590
   282
}
dvander@590
   283
dvander@590
   284
ResultType MsgListenerWrapper::InterceptUserMessage(int msg_id, bf_write *bf, IRecipientFilter *pFilter)
dvander@590
   285
{
dvander@590
   286
	cell_t res = static_cast<cell_t>(Pl_Continue);
faluco@593
   287
	size_t size = _FillInPlayers(g_MsgPlayers, pFilter);
dvander@590
   288
faluco@598
   289
	g_ReadBitBuf.StartReading(bf->GetBasePointer(), bf->GetNumBytesWritten());
faluco@598
   290
dvander@590
   291
	m_Intercept->PushCell(msg_id);
faluco@598
   292
	m_Intercept->PushCell(g_ReadBufHandle);
dvander@590
   293
	m_Intercept->PushArray(g_MsgPlayers, size);
dvander@590
   294
	m_Intercept->PushCell(size);
dvander@590
   295
	m_Intercept->PushCell(pFilter->IsReliable());
dvander@590
   296
	m_Intercept->PushCell(pFilter->IsInitMessage());
dvander@590
   297
	m_Intercept->Execute(&res);
dvander@590
   298
dvander@590
   299
	return static_cast<ResultType>(res);
dvander@590
   300
}
dvander@590
   301
dvander@590
   302
void MsgListenerWrapper::OnUserMessageSent(int msg_id)
dvander@590
   303
{
dvander@590
   304
	if (!m_Notify)
dvander@590
   305
	{
dvander@590
   306
		return;
dvander@590
   307
	}
dvander@590
   308
faluco@593
   309
	cell_t res;
dvander@590
   310
	m_Notify->PushCell(msg_id);
dvander@590
   311
	m_Notify->Execute(&res);
dvander@590
   312
}
dvander@590
   313
dvander@590
   314
/***************************************
dvander@590
   315
 *                                     *
dvander@590
   316
 * USER MESSAGE NATIVE IMPLEMENTATIONS *
dvander@590
   317
 *                                     *
dvander@590
   318
 ***************************************/
faluco@552
   319
faluco@552
   320
static UsrMessageNatives s_UsrMessageNatives;
faluco@505
   321
faluco@505
   322
static cell_t smn_GetUserMessageId(IPluginContext *pCtx, const cell_t *params)
faluco@505
   323
{
faluco@505
   324
	char *msgname;
ds@698
   325
	pCtx->LocalToString(params[1], &msgname);
faluco@505
   326
faluco@552
   327
	return g_UserMsgs.GetMessageIndex(msgname);
faluco@505
   328
}
faluco@505
   329
faluco@553
   330
static cell_t smn_GetUserMessageName(IPluginContext *pCtx, const cell_t *params)
faluco@553
   331
{
ds@765
   332
	char *msgname;
faluco@553
   333
ds@765
   334
	pCtx->LocalToPhysAddr(params[2], (cell_t **)&msgname);
faluco@553
   335
ds@765
   336
	return g_UserMsgs.GetMessageName(params[1], msgname, params[3]);
faluco@553
   337
}
faluco@553
   338
faluco@505
   339
static cell_t smn_StartMessage(IPluginContext *pCtx, const cell_t *params)
faluco@505
   340
{
faluco@505
   341
	char *msgname;
faluco@505
   342
	cell_t *cl_array;
ds@698
   343
	int msgid;
faluco@505
   344
	bf_write *pBitBuf;
faluco@505
   345
faluco@505
   346
	if (g_IsMsgInExec)
faluco@505
   347
	{
faluco@505
   348
		return pCtx->ThrowNativeError("Unable to execute a new message, there is already one in progress");
faluco@505
   349
	}
faluco@505
   350
ds@698
   351
	pCtx->LocalToString(params[1], &msgname);
faluco@505
   352
faluco@552
   353
	if ((msgid=g_UserMsgs.GetMessageIndex(msgname)) == INVALID_MESSAGE_ID)
faluco@505
   354
	{
faluco@505
   355
		return pCtx->ThrowNativeError("Invalid message name: \"%s\"", msgname);
faluco@505
   356
	}
faluco@505
   357
faluco@505
   358
	pCtx->LocalToPhysAddr(params[2], &cl_array);
faluco@505
   359
faluco@552
   360
	pBitBuf = g_UserMsgs.StartMessage(msgid, cl_array, params[3], params[4]);
faluco@593
   361
	if (!pBitBuf)
faluco@593
   362
	{
faluco@593
   363
		return pCtx->ThrowNativeError("Unable to execute a new message while in hook");
faluco@593
   364
	}
faluco@505
   365
faluco@505
   366
	g_CurMsgHandle = g_HandleSys.CreateHandle(g_WrBitBufType, pBitBuf, pCtx->GetIdentity(), g_pCoreIdent, NULL);
faluco@505
   367
	g_IsMsgInExec = true;
faluco@505
   368
faluco@505
   369
	return g_CurMsgHandle;
faluco@505
   370
}
faluco@505
   371
faluco@505
   372
static cell_t smn_StartMessageEx(IPluginContext *pCtx, const cell_t *params)
faluco@505
   373
{
faluco@505
   374
	cell_t *cl_array;
faluco@505
   375
	bf_write *pBitBuf;
faluco@505
   376
	int msgid = params[1];
faluco@505
   377
faluco@505
   378
	if (g_IsMsgInExec)
faluco@505
   379
	{
faluco@505
   380
		return pCtx->ThrowNativeError("Unable to execute a new message, there is already one in progress");
faluco@505
   381
	}
faluco@505
   382
faluco@552
   383
	if (msgid < 0 || msgid >= 255)
faluco@505
   384
	{
faluco@505
   385
		return pCtx->ThrowNativeError("Invalid message id supplied (%d)", msgid);
faluco@505
   386
	}
faluco@505
   387
faluco@505
   388
	pCtx->LocalToPhysAddr(params[2], &cl_array);
faluco@505
   389
faluco@552
   390
	pBitBuf = g_UserMsgs.StartMessage(msgid, cl_array, params[3], params[4]);
faluco@593
   391
	if (!pBitBuf)
faluco@593
   392
	{
faluco@593
   393
		return pCtx->ThrowNativeError("Unable to execute a new message while in hook");
faluco@593
   394
	}
faluco@505
   395
faluco@505
   396
	g_CurMsgHandle = g_HandleSys.CreateHandle(g_WrBitBufType, pBitBuf, pCtx->GetIdentity(), g_pCoreIdent, NULL);
faluco@505
   397
	g_IsMsgInExec = true;
faluco@505
   398
faluco@505
   399
	return g_CurMsgHandle;
faluco@505
   400
}
faluco@505
   401
faluco@505
   402
static cell_t smn_EndMessage(IPluginContext *pCtx, const cell_t *params)
faluco@505
   403
{
faluco@505
   404
	HandleSecurity sec;
faluco@505
   405
faluco@505
   406
	if (!g_IsMsgInExec)
faluco@505
   407
	{
faluco@505
   408
		return pCtx->ThrowNativeError("Unable to end message, no message is in progress");
faluco@505
   409
	}
faluco@505
   410
faluco@552
   411
	g_UserMsgs.EndMessage();
faluco@505
   412
faluco@505
   413
	sec.pOwner = pCtx->GetIdentity();
faluco@505
   414
	sec.pIdentity = g_pCoreIdent;
faluco@505
   415
	g_HandleSys.FreeHandle(g_CurMsgHandle, &sec);
faluco@505
   416
faluco@505
   417
	g_IsMsgInExec = false;
faluco@552
   418
faluco@552
   419
	return 1;
faluco@552
   420
}
faluco@552
   421
faluco@552
   422
static cell_t smn_HookUserMessage(IPluginContext *pCtx, const cell_t *params)
faluco@552
   423
{
faluco@552
   424
	IPluginFunction *pHook, *pNotify;
faluco@593
   425
	MsgListenerWrapper *pListener;
faluco@552
   426
	bool intercept;
faluco@552
   427
	int msgid = params[1];
faluco@552
   428
faluco@552
   429
	if (msgid < 0 || msgid >= 255)
faluco@552
   430
	{
faluco@552
   431
		return pCtx->ThrowNativeError("Invalid message id supplied (%d)", msgid);
faluco@552
   432
	}
faluco@552
   433
faluco@552
   434
	pHook = pCtx->GetFunctionById(params[2]);
faluco@552
   435
	if (!pHook)
faluco@552
   436
	{
faluco@552
   437
		return pCtx->ThrowNativeError("Invalid function id (%X)", params[2]);
faluco@552
   438
	}
faluco@552
   439
	pNotify = pCtx->GetFunctionById(params[4]);
faluco@593
   440
	intercept = (params[3]) ? true : false;
faluco@552
   441
faluco@593
   442
	pListener = s_UsrMessageNatives.CreateListener(pCtx);
faluco@593
   443
	pListener->Initialize(msgid, pHook, pNotify, intercept);
faluco@553
   444
faluco@593
   445
	g_UserMsgs.HookUserMessage(msgid, pListener, intercept);
faluco@505
   446
faluco@505
   447
	return 1;
faluco@505
   448
}
faluco@505
   449
ds@626
   450
static cell_t smn_UnhookUserMessage(IPluginContext *pCtx, const cell_t *params)
faluco@552
   451
{
faluco@552
   452
	IPluginFunction *pFunc;
faluco@593
   453
	MsgListenerWrapper *pListener;
faluco@593
   454
	MsgWrapperIter iter;
faluco@552
   455
	bool intercept;
faluco@552
   456
	int msgid = params[1];
faluco@552
   457
faluco@552
   458
	if (msgid < 0 || msgid >= 255)
faluco@552
   459
	{
faluco@552
   460
		return pCtx->ThrowNativeError("Invalid message id supplied (%d)", msgid);
faluco@552
   461
	}
faluco@552
   462
faluco@552
   463
	pFunc = pCtx->GetFunctionById(params[2]);
faluco@552
   464
	if (!pFunc)
faluco@552
   465
	{
faluco@552
   466
		return pCtx->ThrowNativeError("Invalid function id (%X)", params[2]);
faluco@552
   467
	}
faluco@593
   468
	intercept = (params[3]) ? true : false;
faluco@552
   469
faluco@593
   470
	iter = s_UsrMessageNatives.FindListener(msgid, pCtx, pFunc, intercept);
faluco@593
   471
	if (iter == NULL)
faluco@552
   472
	{
faluco@552
   473
		return pCtx->ThrowNativeError("Unable to unhook the current user message");
faluco@552
   474
	}
faluco@552
   475
faluco@593
   476
	pListener = (*iter);
faluco@593
   477
	if (!g_UserMsgs.UnhookUserMessage(msgid, pListener, intercept))
faluco@552
   478
	{
faluco@552
   479
		return pCtx->ThrowNativeError("Unable to unhook the current user message");
faluco@552
   480
	}
faluco@552
   481
faluco@593
   482
	s_UsrMessageNatives.DeleteListener(pCtx, iter);
faluco@552
   483
faluco@552
   484
	return 1;
faluco@552
   485
}
faluco@505
   486
faluco@505
   487
REGISTER_NATIVES(usrmsgnatives)
faluco@505
   488
{
faluco@505
   489
	{"GetUserMessageId",			smn_GetUserMessageId},
faluco@553
   490
	{"GetUserMessageName",			smn_GetUserMessageName},
faluco@505
   491
	{"StartMessage",				smn_StartMessage},
faluco@505
   492
	{"StartMessageEx",				smn_StartMessageEx},
faluco@505
   493
	{"EndMessage",					smn_EndMessage},
faluco@552
   494
	{"HookUserMessage",				smn_HookUserMessage},
ds@626
   495
	{"UnhookUserMessage",			smn_UnhookUserMessage},
faluco@505
   496
	{NULL,							NULL}
faluco@505
   497
};