core/smn_usermsgs.cpp
author David Anderson <dvander@alliedmods.net>
Sun Aug 19 18:34:36 2007 +0000 (2007-08-19)
changeset 1361 591f6d2fd11d
parent 1330 ab94316bb52b
child 1397 c105f054c72e
permissions -rw-r--r--
added helper function for panel text
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@598
    82
	sec.access[HandleAccess_Delete] |= HANDLE_RESTRICT_IDENTITY;
faluco@598
    83
faluco@552
    84
	g_WrBitBufType = g_HandleSys.CreateType("BitBufWriter", this, 0, NULL, NULL, g_pCoreIdent, NULL);
faluco@598
    85
	g_RdBitBufType = g_HandleSys.CreateType("BitBufReader", this, 0, NULL, &sec, g_pCoreIdent, NULL);
faluco@598
    86
faluco@598
    87
	g_ReadBufHandle = g_HandleSys.CreateHandle(g_RdBitBufType, &g_ReadBitBuf, NULL, g_pCoreIdent, NULL);
faluco@598
    88
faluco@552
    89
	g_PluginSys.AddPluginsListener(this);
faluco@552
    90
}
faluco@552
    91
faluco@552
    92
void UsrMessageNatives::OnSourceModShutdown()
faluco@552
    93
{
faluco@598
    94
	HandleSecurity sec;
faluco@598
    95
	sec.pIdentity = g_pCoreIdent;
faluco@598
    96
faluco@598
    97
	g_HandleSys.FreeHandle(g_ReadBufHandle, &sec);
faluco@598
    98
faluco@552
    99
	g_HandleSys.RemoveType(g_WrBitBufType, g_pCoreIdent);
faluco@598
   100
	g_HandleSys.RemoveType(g_RdBitBufType, g_pCoreIdent);
faluco@598
   101
faluco@552
   102
	g_WrBitBufType = 0;
faluco@598
   103
	g_RdBitBufType = 0;
faluco@552
   104
}
faluco@553
   105
faluco@552
   106
void UsrMessageNatives::OnHandleDestroy(HandleType_t type, void *object)
faluco@552
   107
{
faluco@552
   108
}
faluco@552
   109
faluco@552
   110
void UsrMessageNatives::OnPluginUnloaded(IPlugin *plugin)
faluco@552
   111
{
faluco@593
   112
	MsgWrapperList *pList;
faluco@552
   113
faluco@593
   114
	if (plugin->GetProperty("MsgListeners", reinterpret_cast<void **>(&pList), true))
faluco@505
   115
	{
faluco@593
   116
		MsgWrapperIter iter;
faluco@593
   117
		MsgListenerWrapper *pListener;
faluco@552
   118
faluco@593
   119
		for (iter=pList->begin(); iter!=pList->end(); iter++)
faluco@552
   120
		{
faluco@593
   121
			pListener = (*iter);
faluco@593
   122
			if (g_UserMsgs.UnhookUserMessage(pListener->GetMessageId(), pListener, pListener->IsInterceptHook()))
faluco@552
   123
			{
faluco@593
   124
				m_FreeListeners.push(pListener);
faluco@552
   125
			}
faluco@552
   126
		}
faluco@552
   127
faluco@593
   128
		delete pList;
faluco@552
   129
	}
faluco@552
   130
}
faluco@552
   131
faluco@593
   132
MsgListenerWrapper *UsrMessageNatives::CreateListener(IPluginContext *pCtx)
faluco@552
   133
{
faluco@593
   134
	MsgWrapperList *pList;
faluco@593
   135
	MsgListenerWrapper *pListener;
faluco@593
   136
	IPlugin *pl = g_PluginSys.FindPluginByContext(pCtx->GetContext());
faluco@552
   137
faluco@552
   138
	if (m_FreeListeners.empty())
faluco@552
   139
	{
faluco@593
   140
		pListener = new MsgListenerWrapper;
faluco@552
   141
	} else {
faluco@593
   142
		pListener = m_FreeListeners.front();
faluco@552
   143
		m_FreeListeners.pop();
faluco@505
   144
	}
faluco@552
   145
faluco@593
   146
	if (!pl->GetProperty("MsgListeners", reinterpret_cast<void **>(&pList)))
faluco@505
   147
	{
faluco@593
   148
		pList = new List<MsgListenerWrapper *>;
faluco@593
   149
		pl->SetProperty("MsgListeners", pList);
faluco@552
   150
	}
faluco@552
   151
faluco@593
   152
	pList->push_back(pListener);
faluco@552
   153
faluco@593
   154
	return pListener;
faluco@552
   155
}
faluco@552
   156
faluco@593
   157
MsgWrapperIter UsrMessageNatives::FindListener(int msgid, IPluginContext *pCtx, IPluginFunction *pHook, bool intercept)
faluco@552
   158
{
faluco@593
   159
	MsgWrapperList *pList;
faluco@593
   160
	MsgWrapperIter iter;
faluco@593
   161
	MsgListenerWrapper *pListener;
faluco@552
   162
	IPlugin *pl = g_PluginSys.FindPluginByContext(pCtx->GetContext());
faluco@552
   163
faluco@593
   164
	if (!pl->GetProperty("MsgListeners", reinterpret_cast<void **>(&pList)))
faluco@552
   165
	{
faluco@552
   166
		return NULL;
faluco@505
   167
	}
faluco@552
   168
faluco@593
   169
	for (iter=pList->begin(); iter!=pList->end(); iter++)
faluco@505
   170
	{
faluco@593
   171
		pListener = (*iter);
faluco@593
   172
		if ((msgid == pListener->GetMessageId()) 
faluco@593
   173
			&& (intercept == pListener->IsInterceptHook()) 
faluco@593
   174
			&& (pHook == pListener->GetHookedFunction()))
faluco@552
   175
		{
faluco@593
   176
			return iter;
faluco@552
   177
		}
faluco@505
   178
	}
faluco@552
   179
faluco@593
   180
	return NULL;
faluco@552
   181
}
faluco@553
   182
faluco@593
   183
bool UsrMessageNatives::DeleteListener(IPluginContext *pCtx, MsgWrapperIter iter)
faluco@552
   184
{
faluco@593
   185
	MsgWrapperList *pList;
faluco@593
   186
	MsgListenerWrapper *pListener;
faluco@552
   187
	IPlugin *pl = g_PluginSys.FindPluginByContext(pCtx->GetContext());
faluco@552
   188
faluco@593
   189
	if (!pl->GetProperty("MsgListeners", reinterpret_cast<void **>(&pList)))
faluco@552
   190
	{
faluco@552
   191
		return false;
faluco@552
   192
	}
faluco@552
   193
faluco@593
   194
	pListener = (*iter);
faluco@593
   195
	pList->erase(iter);
faluco@593
   196
	m_FreeListeners.push(pListener);
faluco@552
   197
faluco@552
   198
	return true;
faluco@552
   199
}
faluco@552
   200
dvander@590
   201
/***************************************
dvander@590
   202
 *                                     *
dvander@590
   203
 * USER MESSAGE WRAPPER IMPLEMENTATION *
dvander@590
   204
 *                                     *
dvander@590
   205
 ***************************************/
dvander@590
   206
faluco@593
   207
void MsgListenerWrapper::Initialize(int msgid, IPluginFunction *hook, IPluginFunction *notify, bool intercept)
faluco@593
   208
{
faluco@593
   209
	if (intercept)
faluco@593
   210
	{
faluco@593
   211
		m_Intercept = hook;
faluco@593
   212
		m_Hook = NULL;
faluco@593
   213
	} else {
faluco@593
   214
		m_Hook = hook;
faluco@593
   215
		m_Intercept = NULL;
faluco@593
   216
	}
faluco@593
   217
faluco@593
   218
	if (notify)
faluco@593
   219
	{
faluco@593
   220
		m_Notify = notify;
faluco@593
   221
	} else {
faluco@593
   222
		m_Notify = NULL;
faluco@593
   223
	}
faluco@593
   224
faluco@593
   225
	m_MsgId = msgid;
faluco@593
   226
	m_IsInterceptHook = intercept;
faluco@593
   227
}
faluco@593
   228
faluco@593
   229
size_t MsgListenerWrapper::_FillInPlayers(int *pl_array, IRecipientFilter *pFilter)
dvander@590
   230
{
dvander@590
   231
	size_t size = static_cast<size_t>(pFilter->GetRecipientCount());
dvander@590
   232
dvander@590
   233
	for (size_t i=0; i<size; i++)
dvander@590
   234
	{
dvander@590
   235
		pl_array[i] = pFilter->GetRecipientIndex(i);
dvander@590
   236
	}
dvander@590
   237
dvander@590
   238
	return size;
dvander@590
   239
}
dvander@590
   240
dvander@590
   241
bool MsgListenerWrapper::IsInterceptHook() const
dvander@590
   242
{
dvander@590
   243
	return m_IsInterceptHook;
dvander@590
   244
}
dvander@590
   245
dvander@590
   246
int MsgListenerWrapper::GetMessageId() const
dvander@590
   247
{
dvander@590
   248
	return m_MsgId;
dvander@590
   249
}
dvander@590
   250
dvander@590
   251
IPluginFunction *MsgListenerWrapper::GetHookedFunction() const
dvander@590
   252
{
dvander@590
   253
	if (m_Hook)
dvander@590
   254
	{
dvander@590
   255
		return m_Hook;
dvander@590
   256
	} else {
dvander@590
   257
		return m_Intercept;
dvander@590
   258
	}
dvander@590
   259
}
dvander@590
   260
dvander@590
   261
IPluginFunction *MsgListenerWrapper::GetNotifyFunction() const
dvander@590
   262
{
dvander@590
   263
	return m_Notify;
dvander@590
   264
}
dvander@590
   265
dvander@590
   266
void MsgListenerWrapper::OnUserMessage(int msg_id, bf_write *bf, IRecipientFilter *pFilter)
dvander@590
   267
{
dvander@590
   268
	cell_t res;
faluco@593
   269
	size_t size = _FillInPlayers(g_MsgPlayers, pFilter);
dvander@590
   270
faluco@598
   271
	g_ReadBitBuf.StartReading(bf->GetBasePointer(), bf->GetNumBytesWritten());
faluco@598
   272
dvander@590
   273
	m_Hook->PushCell(msg_id);
faluco@598
   274
	m_Hook->PushCell(g_ReadBufHandle);
dvander@590
   275
	m_Hook->PushArray(g_MsgPlayers, size);
dvander@590
   276
	m_Hook->PushCell(size);
dvander@590
   277
	m_Hook->PushCell(pFilter->IsReliable());
dvander@590
   278
	m_Hook->PushCell(pFilter->IsInitMessage());
dvander@590
   279
	m_Hook->Execute(&res);
dvander@590
   280
}
dvander@590
   281
dvander@590
   282
ResultType MsgListenerWrapper::InterceptUserMessage(int msg_id, bf_write *bf, IRecipientFilter *pFilter)
dvander@590
   283
{
dvander@590
   284
	cell_t res = static_cast<cell_t>(Pl_Continue);
faluco@593
   285
	size_t size = _FillInPlayers(g_MsgPlayers, pFilter);
dvander@590
   286
faluco@598
   287
	g_ReadBitBuf.StartReading(bf->GetBasePointer(), bf->GetNumBytesWritten());
faluco@598
   288
dvander@590
   289
	m_Intercept->PushCell(msg_id);
faluco@598
   290
	m_Intercept->PushCell(g_ReadBufHandle);
dvander@590
   291
	m_Intercept->PushArray(g_MsgPlayers, size);
dvander@590
   292
	m_Intercept->PushCell(size);
dvander@590
   293
	m_Intercept->PushCell(pFilter->IsReliable());
dvander@590
   294
	m_Intercept->PushCell(pFilter->IsInitMessage());
dvander@590
   295
	m_Intercept->Execute(&res);
dvander@590
   296
dvander@590
   297
	return static_cast<ResultType>(res);
dvander@590
   298
}
dvander@590
   299
dvander@590
   300
void MsgListenerWrapper::OnUserMessageSent(int msg_id)
dvander@590
   301
{
dvander@590
   302
	if (!m_Notify)
dvander@590
   303
	{
dvander@590
   304
		return;
dvander@590
   305
	}
dvander@590
   306
faluco@593
   307
	cell_t res;
dvander@590
   308
	m_Notify->PushCell(msg_id);
dvander@590
   309
	m_Notify->Execute(&res);
dvander@590
   310
}
dvander@590
   311
dvander@590
   312
/***************************************
dvander@590
   313
 *                                     *
dvander@590
   314
 * USER MESSAGE NATIVE IMPLEMENTATIONS *
dvander@590
   315
 *                                     *
dvander@590
   316
 ***************************************/
faluco@552
   317
faluco@552
   318
static UsrMessageNatives s_UsrMessageNatives;
faluco@505
   319
faluco@505
   320
static cell_t smn_GetUserMessageId(IPluginContext *pCtx, const cell_t *params)
faluco@505
   321
{
faluco@505
   322
	char *msgname;
ds@698
   323
	pCtx->LocalToString(params[1], &msgname);
faluco@505
   324
faluco@552
   325
	return g_UserMsgs.GetMessageIndex(msgname);
faluco@505
   326
}
faluco@505
   327
faluco@553
   328
static cell_t smn_GetUserMessageName(IPluginContext *pCtx, const cell_t *params)
faluco@553
   329
{
ds@765
   330
	char *msgname;
faluco@553
   331
ds@765
   332
	pCtx->LocalToPhysAddr(params[2], (cell_t **)&msgname);
faluco@553
   333
ds@765
   334
	return g_UserMsgs.GetMessageName(params[1], msgname, params[3]);
faluco@553
   335
}
faluco@553
   336
faluco@505
   337
static cell_t smn_StartMessage(IPluginContext *pCtx, const cell_t *params)
faluco@505
   338
{
faluco@505
   339
	char *msgname;
faluco@505
   340
	cell_t *cl_array;
ds@698
   341
	int msgid;
faluco@505
   342
	bf_write *pBitBuf;
faluco@505
   343
faluco@505
   344
	if (g_IsMsgInExec)
faluco@505
   345
	{
faluco@505
   346
		return pCtx->ThrowNativeError("Unable to execute a new message, there is already one in progress");
faluco@505
   347
	}
faluco@505
   348
ds@698
   349
	pCtx->LocalToString(params[1], &msgname);
faluco@505
   350
faluco@552
   351
	if ((msgid=g_UserMsgs.GetMessageIndex(msgname)) == INVALID_MESSAGE_ID)
faluco@505
   352
	{
faluco@505
   353
		return pCtx->ThrowNativeError("Invalid message name: \"%s\"", msgname);
faluco@505
   354
	}
faluco@505
   355
faluco@505
   356
	pCtx->LocalToPhysAddr(params[2], &cl_array);
faluco@505
   357
faluco@552
   358
	pBitBuf = g_UserMsgs.StartMessage(msgid, cl_array, params[3], params[4]);
faluco@593
   359
	if (!pBitBuf)
faluco@593
   360
	{
faluco@593
   361
		return pCtx->ThrowNativeError("Unable to execute a new message while in hook");
faluco@593
   362
	}
faluco@505
   363
faluco@505
   364
	g_CurMsgHandle = g_HandleSys.CreateHandle(g_WrBitBufType, pBitBuf, pCtx->GetIdentity(), g_pCoreIdent, NULL);
faluco@505
   365
	g_IsMsgInExec = true;
faluco@505
   366
faluco@505
   367
	return g_CurMsgHandle;
faluco@505
   368
}
faluco@505
   369
faluco@505
   370
static cell_t smn_StartMessageEx(IPluginContext *pCtx, const cell_t *params)
faluco@505
   371
{
faluco@505
   372
	cell_t *cl_array;
faluco@505
   373
	bf_write *pBitBuf;
faluco@505
   374
	int msgid = params[1];
faluco@505
   375
faluco@505
   376
	if (g_IsMsgInExec)
faluco@505
   377
	{
faluco@505
   378
		return pCtx->ThrowNativeError("Unable to execute a new message, there is already one in progress");
faluco@505
   379
	}
faluco@505
   380
faluco@552
   381
	if (msgid < 0 || msgid >= 255)
faluco@505
   382
	{
faluco@505
   383
		return pCtx->ThrowNativeError("Invalid message id supplied (%d)", msgid);
faluco@505
   384
	}
faluco@505
   385
faluco@505
   386
	pCtx->LocalToPhysAddr(params[2], &cl_array);
faluco@505
   387
faluco@552
   388
	pBitBuf = g_UserMsgs.StartMessage(msgid, cl_array, params[3], params[4]);
faluco@593
   389
	if (!pBitBuf)
faluco@593
   390
	{
faluco@593
   391
		return pCtx->ThrowNativeError("Unable to execute a new message while in hook");
faluco@593
   392
	}
faluco@505
   393
faluco@505
   394
	g_CurMsgHandle = g_HandleSys.CreateHandle(g_WrBitBufType, pBitBuf, pCtx->GetIdentity(), g_pCoreIdent, NULL);
faluco@505
   395
	g_IsMsgInExec = true;
faluco@505
   396
faluco@505
   397
	return g_CurMsgHandle;
faluco@505
   398
}
faluco@505
   399
faluco@505
   400
static cell_t smn_EndMessage(IPluginContext *pCtx, const cell_t *params)
faluco@505
   401
{
faluco@505
   402
	HandleSecurity sec;
faluco@505
   403
faluco@505
   404
	if (!g_IsMsgInExec)
faluco@505
   405
	{
faluco@505
   406
		return pCtx->ThrowNativeError("Unable to end message, no message is in progress");
faluco@505
   407
	}
faluco@505
   408
faluco@552
   409
	g_UserMsgs.EndMessage();
faluco@505
   410
faluco@505
   411
	sec.pOwner = pCtx->GetIdentity();
faluco@505
   412
	sec.pIdentity = g_pCoreIdent;
faluco@505
   413
	g_HandleSys.FreeHandle(g_CurMsgHandle, &sec);
faluco@505
   414
faluco@505
   415
	g_IsMsgInExec = false;
faluco@552
   416
faluco@552
   417
	return 1;
faluco@552
   418
}
faluco@552
   419
faluco@552
   420
static cell_t smn_HookUserMessage(IPluginContext *pCtx, const cell_t *params)
faluco@552
   421
{
faluco@552
   422
	IPluginFunction *pHook, *pNotify;
faluco@593
   423
	MsgListenerWrapper *pListener;
faluco@552
   424
	bool intercept;
faluco@552
   425
	int msgid = params[1];
faluco@552
   426
faluco@552
   427
	if (msgid < 0 || msgid >= 255)
faluco@552
   428
	{
faluco@552
   429
		return pCtx->ThrowNativeError("Invalid message id supplied (%d)", msgid);
faluco@552
   430
	}
faluco@552
   431
faluco@552
   432
	pHook = pCtx->GetFunctionById(params[2]);
faluco@552
   433
	if (!pHook)
faluco@552
   434
	{
faluco@552
   435
		return pCtx->ThrowNativeError("Invalid function id (%X)", params[2]);
faluco@552
   436
	}
faluco@552
   437
	pNotify = pCtx->GetFunctionById(params[4]);
faluco@593
   438
	intercept = (params[3]) ? true : false;
faluco@552
   439
faluco@593
   440
	pListener = s_UsrMessageNatives.CreateListener(pCtx);
faluco@593
   441
	pListener->Initialize(msgid, pHook, pNotify, intercept);
faluco@553
   442
faluco@593
   443
	g_UserMsgs.HookUserMessage(msgid, pListener, intercept);
faluco@505
   444
faluco@505
   445
	return 1;
faluco@505
   446
}
faluco@505
   447
ds@626
   448
static cell_t smn_UnhookUserMessage(IPluginContext *pCtx, const cell_t *params)
faluco@552
   449
{
faluco@552
   450
	IPluginFunction *pFunc;
faluco@593
   451
	MsgListenerWrapper *pListener;
faluco@593
   452
	MsgWrapperIter iter;
faluco@552
   453
	bool intercept;
faluco@552
   454
	int msgid = params[1];
faluco@552
   455
faluco@552
   456
	if (msgid < 0 || msgid >= 255)
faluco@552
   457
	{
faluco@552
   458
		return pCtx->ThrowNativeError("Invalid message id supplied (%d)", msgid);
faluco@552
   459
	}
faluco@552
   460
faluco@552
   461
	pFunc = pCtx->GetFunctionById(params[2]);
faluco@552
   462
	if (!pFunc)
faluco@552
   463
	{
faluco@552
   464
		return pCtx->ThrowNativeError("Invalid function id (%X)", params[2]);
faluco@552
   465
	}
faluco@593
   466
	intercept = (params[3]) ? true : false;
faluco@552
   467
faluco@593
   468
	iter = s_UsrMessageNatives.FindListener(msgid, pCtx, pFunc, intercept);
faluco@593
   469
	if (iter == NULL)
faluco@552
   470
	{
faluco@552
   471
		return pCtx->ThrowNativeError("Unable to unhook the current user message");
faluco@552
   472
	}
faluco@552
   473
faluco@593
   474
	pListener = (*iter);
faluco@593
   475
	if (!g_UserMsgs.UnhookUserMessage(msgid, pListener, intercept))
faluco@552
   476
	{
faluco@552
   477
		return pCtx->ThrowNativeError("Unable to unhook the current user message");
faluco@552
   478
	}
faluco@552
   479
faluco@593
   480
	s_UsrMessageNatives.DeleteListener(pCtx, iter);
faluco@552
   481
faluco@552
   482
	return 1;
faluco@552
   483
}
faluco@505
   484
faluco@505
   485
REGISTER_NATIVES(usrmsgnatives)
faluco@505
   486
{
faluco@505
   487
	{"GetUserMessageId",			smn_GetUserMessageId},
faluco@553
   488
	{"GetUserMessageName",			smn_GetUserMessageName},
faluco@505
   489
	{"StartMessage",				smn_StartMessage},
faluco@505
   490
	{"StartMessageEx",				smn_StartMessageEx},
faluco@505
   491
	{"EndMessage",					smn_EndMessage},
faluco@552
   492
	{"HookUserMessage",				smn_HookUserMessage},
ds@626
   493
	{"UnhookUserMessage",			smn_UnhookUserMessage},
faluco@505
   494
	{NULL,							NULL}
faluco@505
   495
};