core/smn_usermsgs.cpp
author David Anderson <dvander@alliedmods.net>
Mon Oct 15 00:38:25 2007 +0000 (2007-10-15)
changeset 1568 ecbaee393b0b
parent 1541 e46ad208b9b4
child 1766 d30c252089be
permissions -rw-r--r--
extensions can now be built for multiple engines, and will autoload from auto.X.Y folders
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@1541
    63
	bool FindListener(int msgid, IPluginContext *pCtx, IPluginFunction *pHook, bool intercept, MsgWrapperIter *iter);
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@1541
   159
bool UsrMessageNatives::FindListener(int msgid, IPluginContext *pCtx, IPluginFunction *pHook, bool intercept, MsgWrapperIter *iter)
faluco@552
   160
{
faluco@593
   161
	MsgWrapperList *pList;
faluco@1541
   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@1541
   168
		return false;
faluco@505
   169
	}
faluco@552
   170
faluco@1541
   171
	for (_iter=pList->begin(); _iter!=pList->end(); _iter++)
faluco@505
   172
	{
faluco@1541
   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@1541
   178
			*iter = _iter;
faluco@1541
   179
			return true;
faluco@552
   180
		}
faluco@505
   181
	}
faluco@552
   182
faluco@1541
   183
	return false;
faluco@552
   184
}
faluco@553
   185
faluco@593
   186
bool UsrMessageNatives::DeleteListener(IPluginContext *pCtx, MsgWrapperIter iter)
faluco@552
   187
{
faluco@593
   188
	MsgWrapperList *pList;
faluco@593
   189
	MsgListenerWrapper *pListener;
faluco@552
   190
	IPlugin *pl = g_PluginSys.FindPluginByContext(pCtx->GetContext());
faluco@552
   191
faluco@593
   192
	if (!pl->GetProperty("MsgListeners", reinterpret_cast<void **>(&pList)))
faluco@552
   193
	{
faluco@552
   194
		return false;
faluco@552
   195
	}
faluco@552
   196
faluco@593
   197
	pListener = (*iter);
faluco@593
   198
	pList->erase(iter);
faluco@593
   199
	m_FreeListeners.push(pListener);
faluco@552
   200
faluco@552
   201
	return true;
faluco@552
   202
}
faluco@552
   203
dvander@590
   204
/***************************************
dvander@590
   205
 *                                     *
dvander@590
   206
 * USER MESSAGE WRAPPER IMPLEMENTATION *
dvander@590
   207
 *                                     *
dvander@590
   208
 ***************************************/
dvander@590
   209
faluco@593
   210
void MsgListenerWrapper::Initialize(int msgid, IPluginFunction *hook, IPluginFunction *notify, bool intercept)
faluco@593
   211
{
faluco@593
   212
	if (intercept)
faluco@593
   213
	{
faluco@593
   214
		m_Intercept = hook;
faluco@593
   215
		m_Hook = NULL;
faluco@593
   216
	} else {
faluco@593
   217
		m_Hook = hook;
faluco@593
   218
		m_Intercept = NULL;
faluco@593
   219
	}
faluco@593
   220
faluco@593
   221
	if (notify)
faluco@593
   222
	{
faluco@593
   223
		m_Notify = notify;
faluco@593
   224
	} else {
faluco@593
   225
		m_Notify = NULL;
faluco@593
   226
	}
faluco@593
   227
faluco@593
   228
	m_MsgId = msgid;
faluco@593
   229
	m_IsInterceptHook = intercept;
faluco@593
   230
}
faluco@593
   231
faluco@593
   232
size_t MsgListenerWrapper::_FillInPlayers(int *pl_array, IRecipientFilter *pFilter)
dvander@590
   233
{
dvander@590
   234
	size_t size = static_cast<size_t>(pFilter->GetRecipientCount());
dvander@590
   235
dvander@590
   236
	for (size_t i=0; i<size; i++)
dvander@590
   237
	{
dvander@590
   238
		pl_array[i] = pFilter->GetRecipientIndex(i);
dvander@590
   239
	}
dvander@590
   240
dvander@590
   241
	return size;
dvander@590
   242
}
dvander@590
   243
dvander@590
   244
bool MsgListenerWrapper::IsInterceptHook() const
dvander@590
   245
{
dvander@590
   246
	return m_IsInterceptHook;
dvander@590
   247
}
dvander@590
   248
dvander@590
   249
int MsgListenerWrapper::GetMessageId() const
dvander@590
   250
{
dvander@590
   251
	return m_MsgId;
dvander@590
   252
}
dvander@590
   253
dvander@590
   254
IPluginFunction *MsgListenerWrapper::GetHookedFunction() const
dvander@590
   255
{
dvander@590
   256
	if (m_Hook)
dvander@590
   257
	{
dvander@590
   258
		return m_Hook;
dvander@590
   259
	} else {
dvander@590
   260
		return m_Intercept;
dvander@590
   261
	}
dvander@590
   262
}
dvander@590
   263
dvander@590
   264
IPluginFunction *MsgListenerWrapper::GetNotifyFunction() const
dvander@590
   265
{
dvander@590
   266
	return m_Notify;
dvander@590
   267
}
dvander@590
   268
dvander@590
   269
void MsgListenerWrapper::OnUserMessage(int msg_id, bf_write *bf, IRecipientFilter *pFilter)
dvander@590
   270
{
dvander@590
   271
	cell_t res;
faluco@593
   272
	size_t size = _FillInPlayers(g_MsgPlayers, pFilter);
dvander@590
   273
faluco@598
   274
	g_ReadBitBuf.StartReading(bf->GetBasePointer(), bf->GetNumBytesWritten());
faluco@598
   275
dvander@590
   276
	m_Hook->PushCell(msg_id);
faluco@598
   277
	m_Hook->PushCell(g_ReadBufHandle);
dvander@590
   278
	m_Hook->PushArray(g_MsgPlayers, size);
dvander@590
   279
	m_Hook->PushCell(size);
dvander@590
   280
	m_Hook->PushCell(pFilter->IsReliable());
dvander@590
   281
	m_Hook->PushCell(pFilter->IsInitMessage());
dvander@590
   282
	m_Hook->Execute(&res);
dvander@590
   283
}
dvander@590
   284
dvander@590
   285
ResultType MsgListenerWrapper::InterceptUserMessage(int msg_id, bf_write *bf, IRecipientFilter *pFilter)
dvander@590
   286
{
dvander@590
   287
	cell_t res = static_cast<cell_t>(Pl_Continue);
faluco@593
   288
	size_t size = _FillInPlayers(g_MsgPlayers, pFilter);
dvander@590
   289
faluco@598
   290
	g_ReadBitBuf.StartReading(bf->GetBasePointer(), bf->GetNumBytesWritten());
faluco@598
   291
dvander@590
   292
	m_Intercept->PushCell(msg_id);
faluco@598
   293
	m_Intercept->PushCell(g_ReadBufHandle);
dvander@590
   294
	m_Intercept->PushArray(g_MsgPlayers, size);
dvander@590
   295
	m_Intercept->PushCell(size);
dvander@590
   296
	m_Intercept->PushCell(pFilter->IsReliable());
dvander@590
   297
	m_Intercept->PushCell(pFilter->IsInitMessage());
dvander@590
   298
	m_Intercept->Execute(&res);
dvander@590
   299
dvander@590
   300
	return static_cast<ResultType>(res);
dvander@590
   301
}
dvander@590
   302
dvander@590
   303
void MsgListenerWrapper::OnUserMessageSent(int msg_id)
dvander@590
   304
{
dvander@590
   305
	if (!m_Notify)
dvander@590
   306
	{
dvander@590
   307
		return;
dvander@590
   308
	}
dvander@590
   309
faluco@593
   310
	cell_t res;
dvander@590
   311
	m_Notify->PushCell(msg_id);
dvander@590
   312
	m_Notify->Execute(&res);
dvander@590
   313
}
dvander@590
   314
dvander@590
   315
/***************************************
dvander@590
   316
 *                                     *
dvander@590
   317
 * USER MESSAGE NATIVE IMPLEMENTATIONS *
dvander@590
   318
 *                                     *
dvander@590
   319
 ***************************************/
faluco@552
   320
faluco@552
   321
static UsrMessageNatives s_UsrMessageNatives;
faluco@505
   322
faluco@505
   323
static cell_t smn_GetUserMessageId(IPluginContext *pCtx, const cell_t *params)
faluco@505
   324
{
faluco@505
   325
	char *msgname;
ds@698
   326
	pCtx->LocalToString(params[1], &msgname);
faluco@505
   327
faluco@552
   328
	return g_UserMsgs.GetMessageIndex(msgname);
faluco@505
   329
}
faluco@505
   330
faluco@553
   331
static cell_t smn_GetUserMessageName(IPluginContext *pCtx, const cell_t *params)
faluco@553
   332
{
ds@765
   333
	char *msgname;
faluco@553
   334
ds@765
   335
	pCtx->LocalToPhysAddr(params[2], (cell_t **)&msgname);
faluco@553
   336
ds@765
   337
	return g_UserMsgs.GetMessageName(params[1], msgname, params[3]);
faluco@553
   338
}
faluco@553
   339
faluco@505
   340
static cell_t smn_StartMessage(IPluginContext *pCtx, const cell_t *params)
faluco@505
   341
{
faluco@505
   342
	char *msgname;
faluco@505
   343
	cell_t *cl_array;
ds@698
   344
	int msgid;
faluco@505
   345
	bf_write *pBitBuf;
faluco@505
   346
faluco@505
   347
	if (g_IsMsgInExec)
faluco@505
   348
	{
faluco@505
   349
		return pCtx->ThrowNativeError("Unable to execute a new message, there is already one in progress");
faluco@505
   350
	}
faluco@505
   351
ds@698
   352
	pCtx->LocalToString(params[1], &msgname);
faluco@505
   353
faluco@552
   354
	if ((msgid=g_UserMsgs.GetMessageIndex(msgname)) == INVALID_MESSAGE_ID)
faluco@505
   355
	{
faluco@505
   356
		return pCtx->ThrowNativeError("Invalid message name: \"%s\"", msgname);
faluco@505
   357
	}
faluco@505
   358
faluco@505
   359
	pCtx->LocalToPhysAddr(params[2], &cl_array);
faluco@505
   360
faluco@552
   361
	pBitBuf = g_UserMsgs.StartMessage(msgid, cl_array, params[3], params[4]);
faluco@593
   362
	if (!pBitBuf)
faluco@593
   363
	{
faluco@593
   364
		return pCtx->ThrowNativeError("Unable to execute a new message while in hook");
faluco@593
   365
	}
faluco@505
   366
faluco@505
   367
	g_CurMsgHandle = g_HandleSys.CreateHandle(g_WrBitBufType, pBitBuf, pCtx->GetIdentity(), g_pCoreIdent, NULL);
faluco@505
   368
	g_IsMsgInExec = true;
faluco@505
   369
faluco@505
   370
	return g_CurMsgHandle;
faluco@505
   371
}
faluco@505
   372
faluco@505
   373
static cell_t smn_StartMessageEx(IPluginContext *pCtx, const cell_t *params)
faluco@505
   374
{
faluco@505
   375
	cell_t *cl_array;
faluco@505
   376
	bf_write *pBitBuf;
faluco@505
   377
	int msgid = params[1];
faluco@505
   378
faluco@505
   379
	if (g_IsMsgInExec)
faluco@505
   380
	{
faluco@505
   381
		return pCtx->ThrowNativeError("Unable to execute a new message, there is already one in progress");
faluco@505
   382
	}
faluco@505
   383
faluco@552
   384
	if (msgid < 0 || msgid >= 255)
faluco@505
   385
	{
faluco@505
   386
		return pCtx->ThrowNativeError("Invalid message id supplied (%d)", msgid);
faluco@505
   387
	}
faluco@505
   388
faluco@505
   389
	pCtx->LocalToPhysAddr(params[2], &cl_array);
faluco@505
   390
faluco@552
   391
	pBitBuf = g_UserMsgs.StartMessage(msgid, cl_array, params[3], params[4]);
faluco@593
   392
	if (!pBitBuf)
faluco@593
   393
	{
faluco@593
   394
		return pCtx->ThrowNativeError("Unable to execute a new message while in hook");
faluco@593
   395
	}
faluco@505
   396
faluco@505
   397
	g_CurMsgHandle = g_HandleSys.CreateHandle(g_WrBitBufType, pBitBuf, pCtx->GetIdentity(), g_pCoreIdent, NULL);
faluco@505
   398
	g_IsMsgInExec = true;
faluco@505
   399
faluco@505
   400
	return g_CurMsgHandle;
faluco@505
   401
}
faluco@505
   402
faluco@505
   403
static cell_t smn_EndMessage(IPluginContext *pCtx, const cell_t *params)
faluco@505
   404
{
faluco@505
   405
	HandleSecurity sec;
faluco@505
   406
faluco@505
   407
	if (!g_IsMsgInExec)
faluco@505
   408
	{
faluco@505
   409
		return pCtx->ThrowNativeError("Unable to end message, no message is in progress");
faluco@505
   410
	}
faluco@505
   411
faluco@552
   412
	g_UserMsgs.EndMessage();
faluco@505
   413
faluco@505
   414
	sec.pOwner = pCtx->GetIdentity();
faluco@505
   415
	sec.pIdentity = g_pCoreIdent;
faluco@505
   416
	g_HandleSys.FreeHandle(g_CurMsgHandle, &sec);
faluco@505
   417
faluco@505
   418
	g_IsMsgInExec = false;
faluco@552
   419
faluco@552
   420
	return 1;
faluco@552
   421
}
faluco@552
   422
faluco@552
   423
static cell_t smn_HookUserMessage(IPluginContext *pCtx, const cell_t *params)
faluco@552
   424
{
faluco@552
   425
	IPluginFunction *pHook, *pNotify;
faluco@593
   426
	MsgListenerWrapper *pListener;
faluco@552
   427
	bool intercept;
faluco@552
   428
	int msgid = params[1];
faluco@552
   429
faluco@552
   430
	if (msgid < 0 || msgid >= 255)
faluco@552
   431
	{
faluco@552
   432
		return pCtx->ThrowNativeError("Invalid message id supplied (%d)", msgid);
faluco@552
   433
	}
faluco@552
   434
faluco@552
   435
	pHook = pCtx->GetFunctionById(params[2]);
faluco@552
   436
	if (!pHook)
faluco@552
   437
	{
faluco@552
   438
		return pCtx->ThrowNativeError("Invalid function id (%X)", params[2]);
faluco@552
   439
	}
faluco@552
   440
	pNotify = pCtx->GetFunctionById(params[4]);
faluco@593
   441
	intercept = (params[3]) ? true : false;
faluco@552
   442
faluco@593
   443
	pListener = s_UsrMessageNatives.CreateListener(pCtx);
faluco@593
   444
	pListener->Initialize(msgid, pHook, pNotify, intercept);
faluco@553
   445
faluco@593
   446
	g_UserMsgs.HookUserMessage(msgid, pListener, intercept);
faluco@505
   447
faluco@505
   448
	return 1;
faluco@505
   449
}
faluco@505
   450
ds@626
   451
static cell_t smn_UnhookUserMessage(IPluginContext *pCtx, const cell_t *params)
faluco@552
   452
{
faluco@552
   453
	IPluginFunction *pFunc;
faluco@593
   454
	MsgListenerWrapper *pListener;
faluco@593
   455
	MsgWrapperIter iter;
faluco@552
   456
	bool intercept;
faluco@552
   457
	int msgid = params[1];
faluco@552
   458
faluco@552
   459
	if (msgid < 0 || msgid >= 255)
faluco@552
   460
	{
faluco@552
   461
		return pCtx->ThrowNativeError("Invalid message id supplied (%d)", msgid);
faluco@552
   462
	}
faluco@552
   463
faluco@552
   464
	pFunc = pCtx->GetFunctionById(params[2]);
faluco@552
   465
	if (!pFunc)
faluco@552
   466
	{
faluco@552
   467
		return pCtx->ThrowNativeError("Invalid function id (%X)", params[2]);
faluco@552
   468
	}
faluco@593
   469
	intercept = (params[3]) ? true : false;
faluco@552
   470
faluco@1541
   471
	if (!s_UsrMessageNatives.FindListener(msgid, pCtx, pFunc, intercept, &iter))
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
};