core/smn_functions.cpp
author Phong Dang <teame06@gmail.com>
Wed Aug 01 07:29:40 2007 +0000 (2007-08-01)
changeset 1241 2e8046793bc9
parent 1231 17badaf66ce4
child 1330 ab94316bb52b
permissions -rw-r--r--
Added check to make sure player are alive to perform sm_slap, sm_burn, sm_slay.
ds@621
     1
/**
ds@653
     2
 * vim: set ts=4 :
dvander@1231
     3
 * ================================================================
dvander@1231
     4
 * SourceMod
dvander@1231
     5
 * Copyright (C) 2004-2007 AlliedModders LLC.  All rights reserved.
dvander@1231
     6
 * ================================================================
dvander@646
     7
 *
dvander@1231
     8
 * This program is free software; you can redistribute it and/or 
dvander@1231
     9
 * modify it under the terms of the GNU General Public License, 
dvander@1231
    10
 * version 3.0, as published by the Free Software Foundation.
dvander@1231
    11
 * 
dvander@1231
    12
 * This program is distributed in the hope that it will be useful,
dvander@1231
    13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
dvander@1231
    14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
dvander@1231
    15
 * GNU General Public License for more details.
dvander@1231
    16
 *
dvander@1231
    17
 * You should have received a copy of the GNU General Public License
dvander@1231
    18
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
dvander@1231
    19
 *
dvander@1231
    20
 * As a special exception, AlliedModders LLC gives you permission to 
dvander@1231
    21
 * link the code of this program (as well as its derivative works) to 
dvander@1231
    22
 * "Half-Life 2," the "Source Engine," the "SourcePawn JIT," and any 
dvander@1231
    23
 * Game MODs that run on software by the Valve Corporation.  You must 
dvander@1231
    24
 * obey the GNU General Public License in all respects for all other 
dvander@1231
    25
 * code used. Additionally, AlliedModders LLC grants this exception 
dvander@1231
    26
 * to all derivative works. AlliedModders LLC defines further 
dvander@1231
    27
 * exceptions, found in LICENSE.txt (as of this writing, version 
dvander@1231
    28
 * JULY-31-2007), or <http://www.sourcemod.net/license.php>.
dvander@646
    29
 *
dvander@646
    30
 * Version: $Id$
dvander@646
    31
 */
ds@621
    32
ds@621
    33
#include "sm_globals.h"
ds@621
    34
#include "PluginSys.h"
ds@621
    35
#include "ForwardSys.h"
ds@621
    36
#include "HandleSys.h"
ds@621
    37
ds@621
    38
HandleType_t g_GlobalFwdType = 0;
ds@621
    39
HandleType_t g_PrivateFwdType = 0;
ds@621
    40
ds@621
    41
static bool s_CallStarted = false;
ds@621
    42
static ICallable *s_pCallable = NULL;
ds@621
    43
static IPluginFunction *s_pFunction = NULL;
ds@621
    44
static IForward *s_pForward = NULL;
ds@621
    45
ds@621
    46
class ForwardNativeHelpers : 
ds@621
    47
	public SMGlobalClass,
ds@621
    48
	public IHandleTypeDispatch
ds@621
    49
{
ds@621
    50
public:
ds@621
    51
	void OnSourceModAllInitialized()
ds@621
    52
	{
ds@621
    53
		HandleAccess sec;
ds@621
    54
ds@621
    55
		/* Set GlobalFwd handle access security */
ds@621
    56
		g_HandleSys.InitAccessDefaults(NULL, &sec);
ds@621
    57
		sec.access[HandleAccess_Read] = 0;
ds@621
    58
		sec.access[HandleAccess_Clone] = HANDLE_RESTRICT_IDENTITY | HANDLE_RESTRICT_OWNER;
ds@621
    59
ds@621
    60
		/* Create 'GlobalFwd' handle type */
ds@621
    61
		g_GlobalFwdType = g_HandleSys.CreateType("GlobalFwd", this, 0, NULL, &sec, g_pCoreIdent, NULL);
ds@621
    62
ds@621
    63
		/* Private forwards are cloneable */
ds@621
    64
		sec.access[HandleAccess_Clone] = 0;
ds@621
    65
ds@621
    66
		/* Create 'PrivateFwd' handle type */
ds@621
    67
		g_PrivateFwdType = g_HandleSys.CreateType("PrivateFwd", this, g_GlobalFwdType, NULL, &sec, g_pCoreIdent, NULL);
ds@621
    68
	}
ds@621
    69
ds@621
    70
	void OnSourceModShutdown()
ds@621
    71
	{
ds@621
    72
		g_HandleSys.RemoveType(g_PrivateFwdType, g_pCoreIdent);
ds@621
    73
		g_HandleSys.RemoveType(g_GlobalFwdType, g_pCoreIdent);
ds@621
    74
	}
ds@621
    75
ds@621
    76
	void OnHandleDestroy(HandleType_t type, void *object)
ds@621
    77
	{
ds@621
    78
		IForward *pForward = static_cast<IForward *>(object);
ds@621
    79
ds@621
    80
		g_Forwards.ReleaseForward(pForward);
ds@621
    81
	}
ds@621
    82
} g_ForwardNativeHelpers;
ds@621
    83
ds@621
    84
ds@621
    85
/* Turn a public index into a function ID */
ds@621
    86
inline funcid_t PublicIndexToFuncId(uint32_t idx)
ds@621
    87
{
ds@621
    88
	return (idx << 1) | (1 << 0);
ds@621
    89
}
ds@621
    90
ds@621
    91
/* Reset global function/forward call variables */
ds@621
    92
inline void ResetCall()
ds@621
    93
{
ds@621
    94
	s_CallStarted = false;
ds@621
    95
	s_pFunction = NULL;
ds@1082
    96
	s_pForward = NULL;
ds@621
    97
	s_pCallable = NULL;
ds@621
    98
}
ds@621
    99
ds@621
   100
static cell_t sm_GetFunctionByName(IPluginContext *pContext, const cell_t *params)
ds@621
   101
{
ds@621
   102
	Handle_t hndl = static_cast<Handle_t>(params[1]);
ds@621
   103
	HandleError err;
ds@621
   104
	char *name;
ds@621
   105
	uint32_t idx;
ds@621
   106
	IPlugin *pPlugin;
ds@621
   107
ds@621
   108
	if (hndl == 0)
ds@621
   109
	{
ds@621
   110
		pPlugin = g_PluginSys.FindPluginByContext(pContext->GetContext());
ds@621
   111
	} else {
ds@621
   112
		pPlugin = g_PluginSys.PluginFromHandle(hndl, &err);
ds@621
   113
ds@621
   114
		if (!pPlugin)
ds@621
   115
		{
ds@621
   116
			return pContext->ThrowNativeError("Plugin handle %x is invalid (error %d)", hndl, err);
ds@621
   117
		}
ds@621
   118
	}
ds@621
   119
ds@621
   120
	pContext->LocalToString(params[2], &name);
ds@621
   121
ds@621
   122
	/* Get public function index */
ds@621
   123
	if (pPlugin->GetBaseContext()->FindPublicByName(name, &idx) == SP_ERROR_NOT_FOUND)
ds@621
   124
	{
ds@621
   125
		/* Return INVALID_FUNCTION if not found */
ds@621
   126
		return -1;
ds@621
   127
	}
ds@621
   128
ds@621
   129
	/* Return function ID */
ds@621
   130
	return PublicIndexToFuncId(idx);
ds@621
   131
}
ds@621
   132
ds@621
   133
static cell_t sm_CreateGlobalForward(IPluginContext *pContext, const cell_t *params)
ds@621
   134
{
ds@621
   135
	cell_t count = params[0];
ds@621
   136
	char *name;
ds@621
   137
	ParamType forwardParams[SP_MAX_EXEC_PARAMS];
ds@621
   138
ds@621
   139
	if (count - 2 > SP_MAX_EXEC_PARAMS)
ds@621
   140
	{
ds@621
   141
		return pContext->ThrowNativeErrorEx(SP_ERROR_PARAMS_MAX, NULL);
ds@621
   142
	}
ds@621
   143
	
ds@621
   144
	pContext->LocalToString(params[1], &name);
ds@621
   145
ds@621
   146
	cell_t *addr;
ds@621
   147
	for (int i = 3; i <= count; i++)
ds@621
   148
	{
ds@621
   149
		pContext->LocalToPhysAddr(params[i], &addr);
ds@621
   150
		forwardParams[i - 3] = static_cast<ParamType>(*addr);
ds@621
   151
	}
ds@621
   152
ds@621
   153
	IForward *pForward = g_Forwards.CreateForward(name, static_cast<ExecType>(params[2]), count - 2, forwardParams);
ds@621
   154
ds@621
   155
	return g_HandleSys.CreateHandle(g_GlobalFwdType, pForward, pContext->GetIdentity(), g_pCoreIdent, NULL);
ds@621
   156
}
ds@621
   157
ds@621
   158
static cell_t sm_CreateForward(IPluginContext *pContext, const cell_t *params)
ds@621
   159
{
ds@621
   160
	cell_t count = params[0];
ds@621
   161
	ParamType forwardParams[SP_MAX_EXEC_PARAMS];
ds@621
   162
ds@621
   163
	if (count - 1 > SP_MAX_EXEC_PARAMS)
ds@621
   164
	{
ds@621
   165
		return pContext->ThrowNativeErrorEx(SP_ERROR_PARAMS_MAX, NULL);
ds@621
   166
	}
ds@621
   167
ds@621
   168
	cell_t *addr;
ds@621
   169
	for (int i = 2; i <= count; i++)
ds@621
   170
	{
ds@621
   171
		pContext->LocalToPhysAddr(params[i], &addr);
ds@621
   172
		forwardParams[i - 2] = static_cast<ParamType>(*addr);
ds@621
   173
	}
ds@621
   174
ds@621
   175
	IChangeableForward *pForward = g_Forwards.CreateForwardEx(NULL, static_cast<ExecType>(params[1]), count - 1, forwardParams);
ds@621
   176
ds@621
   177
	return g_HandleSys.CreateHandle(g_PrivateFwdType, pForward, pContext->GetIdentity(), g_pCoreIdent, NULL);
ds@621
   178
}
ds@621
   179
ds@621
   180
static cell_t sm_GetForwardFunctionCount(IPluginContext *pContext, const cell_t *params)
ds@621
   181
{
ds@621
   182
	Handle_t hndl = static_cast<Handle_t>(params[1]);
ds@621
   183
	HandleError err;
ds@621
   184
	IForward *pForward;
ds@621
   185
ds@621
   186
	if ((err=g_HandleSys.ReadHandle(hndl, g_GlobalFwdType, NULL, (void **)&pForward))
ds@621
   187
		!= HandleError_None)
ds@621
   188
	{
ds@621
   189
		return pContext->ThrowNativeError("Invalid forward handle %x (error %d)", hndl, err);
ds@621
   190
	}
ds@621
   191
ds@621
   192
	return pForward->GetFunctionCount();
ds@621
   193
}
ds@621
   194
ds@621
   195
static cell_t sm_AddToForward(IPluginContext *pContext, const cell_t *params)
ds@621
   196
{
ds@621
   197
	Handle_t fwdHandle = static_cast<Handle_t>(params[1]);
ds@621
   198
	Handle_t plHandle = static_cast<Handle_t>(params[2]);
ds@621
   199
	HandleError err;
ds@621
   200
	IChangeableForward *pForward;
ds@621
   201
	IPlugin *pPlugin;
ds@621
   202
ds@621
   203
	if ((err=g_HandleSys.ReadHandle(fwdHandle, g_PrivateFwdType, NULL, (void **)&pForward))
ds@621
   204
		!= HandleError_None)
ds@621
   205
	{
ds@621
   206
		return pContext->ThrowNativeError("Invalid private forward handle %x (error %d)", fwdHandle, err);
ds@621
   207
	}
ds@621
   208
ds@621
   209
	if (plHandle == 0)
ds@621
   210
	{
ds@621
   211
		pPlugin = g_PluginSys.FindPluginByContext(pContext->GetContext());
ds@621
   212
	} else {
ds@621
   213
		pPlugin = g_PluginSys.PluginFromHandle(plHandle, &err);
ds@621
   214
ds@621
   215
		if (!pPlugin)
ds@621
   216
		{
ds@621
   217
			return pContext->ThrowNativeError("Plugin handle %x is invalid (error %d)", plHandle, err);
ds@621
   218
		}
ds@621
   219
	}
ds@621
   220
ds@621
   221
	IPluginFunction *pFunction = pPlugin->GetBaseContext()->GetFunctionById(params[3]);
ds@621
   222
ds@621
   223
	if (!pFunction)
ds@621
   224
	{
ds@621
   225
		return pContext->ThrowNativeError("Invalid function id (%X)", params[3]);
ds@621
   226
	}
ds@621
   227
ds@621
   228
	return pForward->AddFunction(pFunction);
ds@621
   229
}
ds@621
   230
ds@621
   231
static cell_t sm_RemoveFromForward(IPluginContext *pContext, const cell_t *params)
ds@621
   232
{
ds@621
   233
	Handle_t fwdHandle = static_cast<Handle_t>(params[1]);
ds@621
   234
	Handle_t plHandle = static_cast<Handle_t>(params[2]);
ds@621
   235
	HandleError err;
ds@621
   236
	IChangeableForward *pForward;
ds@621
   237
	IPlugin *pPlugin;
ds@621
   238
ds@621
   239
	if ((err=g_HandleSys.ReadHandle(fwdHandle, g_PrivateFwdType, NULL, (void **)&pForward))
ds@621
   240
		!= HandleError_None)
ds@621
   241
	{
ds@621
   242
		return pContext->ThrowNativeError("Invalid private forward handle %x (error %d)", fwdHandle, err);
ds@621
   243
	}
ds@621
   244
ds@621
   245
	if (plHandle == 0)
ds@621
   246
	{
ds@621
   247
		pPlugin = g_PluginSys.FindPluginByContext(pContext->GetContext());
ds@621
   248
	} else {
ds@621
   249
		pPlugin = g_PluginSys.PluginFromHandle(plHandle, &err);
ds@621
   250
ds@621
   251
		if (!pPlugin)
ds@621
   252
		{
ds@621
   253
			return pContext->ThrowNativeError("Plugin handle %x is invalid (error %d)", plHandle, err);
ds@621
   254
		}
ds@621
   255
	}
ds@621
   256
ds@621
   257
	IPluginFunction *pFunction = pPlugin->GetBaseContext()->GetFunctionById(params[3]);
ds@621
   258
ds@621
   259
	if (!pFunction)
ds@621
   260
	{
ds@621
   261
		return pContext->ThrowNativeError("Invalid function id (%X)", params[3]);
ds@621
   262
	}
ds@621
   263
ds@621
   264
	return pForward->RemoveFunction(pFunction);
ds@621
   265
}
ds@621
   266
ds@621
   267
static cell_t sm_RemoveAllFromForward(IPluginContext *pContext, const cell_t *params)
ds@621
   268
{
ds@621
   269
	Handle_t fwdHandle = static_cast<Handle_t>(params[1]);
ds@621
   270
	Handle_t plHandle = static_cast<Handle_t>(params[2]);
ds@621
   271
	HandleError err;
ds@621
   272
	IChangeableForward *pForward;
ds@621
   273
	IPlugin *pPlugin;
ds@621
   274
ds@621
   275
	if ((err=g_HandleSys.ReadHandle(fwdHandle, g_PrivateFwdType, NULL, (void **)&pForward))
ds@621
   276
		!= HandleError_None)
ds@621
   277
	{
ds@621
   278
		return pContext->ThrowNativeError("Invalid private forward handle %x (error %d)", fwdHandle, err);
ds@621
   279
	}
ds@621
   280
ds@621
   281
	if (plHandle == 0)
ds@621
   282
	{
ds@621
   283
		pPlugin = g_PluginSys.FindPluginByContext(pContext->GetContext());
ds@621
   284
	} else {
ds@621
   285
		pPlugin = g_PluginSys.PluginFromHandle(plHandle, &err);
ds@621
   286
ds@621
   287
		if (!pPlugin)
ds@621
   288
		{
ds@621
   289
			return pContext->ThrowNativeError("Plugin handle %x is invalid (error %d)", plHandle, err);
ds@621
   290
		}
ds@621
   291
	}
ds@621
   292
ds@621
   293
	return pForward->RemoveFunctionsOfPlugin(pPlugin);
ds@621
   294
}
ds@621
   295
ds@621
   296
static cell_t sm_CallStartFunction(IPluginContext *pContext, const cell_t *params)
ds@621
   297
{
ds@621
   298
	Handle_t hndl;
ds@621
   299
	HandleError err;
ds@621
   300
	IPlugin *pPlugin;
ds@621
   301
ds@621
   302
	if (s_CallStarted)
ds@621
   303
	{
ds@621
   304
		return pContext->ThrowNativeError("Cannot start a call while one is already in progress");
ds@621
   305
	}
ds@621
   306
ds@621
   307
	hndl = static_cast<Handle_t>(params[1]);
ds@621
   308
ds@621
   309
	if (hndl == 0)
ds@621
   310
	{
ds@621
   311
		pPlugin = g_PluginSys.FindPluginByContext(pContext->GetContext());
ds@621
   312
	} else {
ds@621
   313
		pPlugin = g_PluginSys.PluginFromHandle(hndl, &err);
ds@621
   314
ds@621
   315
		if (!pPlugin)
ds@621
   316
		{
ds@621
   317
			return pContext->ThrowNativeError("Plugin handle %x is invalid (error %d)", hndl, err);
ds@621
   318
		}
ds@621
   319
	}
ds@621
   320
ds@621
   321
	s_pFunction = pPlugin->GetBaseContext()->GetFunctionById(params[2]);
ds@621
   322
ds@621
   323
	if (!s_pFunction)
ds@621
   324
	{
ds@621
   325
		return pContext->ThrowNativeError("Invalid function id (%X)", params[2]);
ds@621
   326
	}
ds@621
   327
ds@621
   328
	s_pCallable = static_cast<ICallable *>(s_pFunction);
ds@621
   329
ds@621
   330
	s_CallStarted = true;
ds@621
   331
ds@621
   332
	return 1;
ds@621
   333
}
ds@621
   334
ds@621
   335
static cell_t sm_CallStartForward(IPluginContext *pContext, const cell_t *params)
ds@621
   336
{
ds@621
   337
	Handle_t hndl;
ds@621
   338
	HandleError err;
ds@621
   339
	IForward *pForward;
ds@621
   340
ds@621
   341
	if (s_CallStarted)
ds@621
   342
	{
ds@621
   343
		return pContext->ThrowNativeError("Cannot start a call while one is already in progress");
ds@621
   344
	}
ds@621
   345
ds@621
   346
	hndl = static_cast<Handle_t>(params[1]);
ds@621
   347
ds@621
   348
	if ((err=g_HandleSys.ReadHandle(hndl, g_GlobalFwdType, NULL, (void **)&pForward))
ds@621
   349
		!= HandleError_None)
ds@621
   350
	{
ds@621
   351
		return pContext->ThrowNativeError("Invalid forward handle %x (error %d)", hndl, err);
ds@621
   352
	}
ds@621
   353
ds@621
   354
	s_pForward = pForward;
ds@621
   355
ds@621
   356
	s_pCallable = static_cast<ICallable *>(pForward);
ds@621
   357
ds@621
   358
	s_CallStarted = true;
ds@621
   359
ds@621
   360
	return 1;
ds@621
   361
}
ds@621
   362
ds@621
   363
static cell_t sm_CallPushCell(IPluginContext *pContext, const cell_t *params)
ds@621
   364
{
ds@621
   365
	int err;
ds@621
   366
ds@621
   367
	if (!s_CallStarted)
ds@621
   368
	{
ds@621
   369
		return pContext->ThrowNativeError("Cannot push parameters when there is no call in progress");
ds@621
   370
	}
ds@621
   371
ds@621
   372
	err = s_pCallable->PushCell(params[1]);
ds@621
   373
ds@621
   374
	if (err)
ds@621
   375
	{
ds@621
   376
		s_pCallable->Cancel();
ds@621
   377
		ResetCall();
ds@621
   378
		return pContext->ThrowNativeErrorEx(err, NULL);
ds@621
   379
	}
ds@621
   380
ds@621
   381
	return 1;
ds@621
   382
}
ds@621
   383
ds@621
   384
static cell_t sm_CallPushCellRef(IPluginContext *pContext, const cell_t *params)
ds@621
   385
{
ds@621
   386
	int err;
ds@621
   387
	cell_t *addr;
ds@621
   388
ds@621
   389
	if (!s_CallStarted)
ds@621
   390
	{
ds@621
   391
		return pContext->ThrowNativeError("Cannot push parameters when there is no call in progress");
ds@621
   392
	}
ds@621
   393
ds@621
   394
	pContext->LocalToPhysAddr(params[1], &addr);
ds@621
   395
ds@621
   396
	err = s_pCallable->PushCellByRef(addr);
ds@621
   397
ds@621
   398
	if (err)
ds@621
   399
	{
ds@621
   400
		s_pCallable->Cancel();
ds@621
   401
		ResetCall();
ds@621
   402
		return pContext->ThrowNativeErrorEx(err, NULL);
ds@621
   403
	}
ds@621
   404
ds@621
   405
	return 1;
ds@621
   406
}
ds@621
   407
ds@621
   408
static cell_t sm_CallPushFloat(IPluginContext *pContext, const cell_t *params)
ds@621
   409
{
ds@621
   410
	int err;
ds@621
   411
ds@621
   412
	if (!s_CallStarted)
ds@621
   413
	{
ds@621
   414
		return pContext->ThrowNativeError("Cannot push parameters when there is no call in progress");
ds@621
   415
	}
ds@621
   416
ds@621
   417
	err = s_pCallable->PushFloat(sp_ctof(params[1]));
ds@621
   418
ds@621
   419
	if (err)
ds@621
   420
	{
ds@621
   421
		s_pCallable->Cancel();
ds@621
   422
		ResetCall();
ds@621
   423
		return pContext->ThrowNativeErrorEx(err, NULL);
ds@621
   424
	}
ds@621
   425
ds@621
   426
	return 1;
ds@621
   427
}
ds@621
   428
ds@621
   429
static cell_t sm_CallPushFloatRef(IPluginContext *pContext, const cell_t *params)
ds@621
   430
{
ds@621
   431
	int err;
ds@621
   432
	cell_t *addr;
ds@621
   433
ds@621
   434
	if (!s_CallStarted)
ds@621
   435
	{
ds@621
   436
		return pContext->ThrowNativeError("Cannot push parameters when there is no call in progress");
ds@621
   437
	}
ds@621
   438
ds@621
   439
	pContext->LocalToPhysAddr(params[1], &addr);
ds@621
   440
ds@621
   441
	err = s_pCallable->PushFloatByRef(reinterpret_cast<float *>(addr));
ds@621
   442
ds@621
   443
	if (err)
ds@621
   444
	{
ds@621
   445
		s_pCallable->Cancel();
ds@621
   446
		ResetCall();
ds@621
   447
		return pContext->ThrowNativeErrorEx(err, NULL);
ds@621
   448
	}
ds@621
   449
ds@621
   450
	return 1;
ds@621
   451
}
ds@621
   452
ds@621
   453
static cell_t sm_CallPushArray(IPluginContext *pContext, const cell_t *params)
ds@621
   454
{
ds@621
   455
	int err;
ds@621
   456
	cell_t *addr;
ds@621
   457
ds@621
   458
	if (!s_CallStarted)
ds@621
   459
	{
ds@621
   460
		return pContext->ThrowNativeError("Cannot push parameters when there is no call in progress");
ds@621
   461
	}
ds@621
   462
ds@621
   463
	pContext->LocalToPhysAddr(params[1], &addr);
ds@621
   464
faluco@629
   465
	err = s_pCallable->PushArray(addr, params[2]);
ds@621
   466
ds@621
   467
	if (err)
ds@621
   468
	{
ds@621
   469
		s_pCallable->Cancel();
ds@621
   470
		ResetCall();
ds@621
   471
		return pContext->ThrowNativeErrorEx(err, NULL);
ds@621
   472
	}
ds@621
   473
ds@621
   474
	return 1;
ds@621
   475
}
ds@621
   476
ds@621
   477
static cell_t sm_CallPushArrayEx(IPluginContext *pContext, const cell_t *params)
ds@621
   478
{
ds@621
   479
	int err;
ds@621
   480
	cell_t *addr;
ds@621
   481
ds@621
   482
	if (!s_CallStarted)
ds@621
   483
	{
ds@621
   484
		return pContext->ThrowNativeError("Cannot push parameters when there is no call in progress");
ds@621
   485
	}
ds@621
   486
ds@621
   487
	pContext->LocalToPhysAddr(params[1], &addr);
ds@621
   488
ds@621
   489
	err = s_pCallable->PushArray(addr, params[2], params[3]);
ds@621
   490
ds@621
   491
	if (err)
ds@621
   492
	{
ds@621
   493
		s_pCallable->Cancel();
ds@621
   494
		ResetCall();
ds@621
   495
		return pContext->ThrowNativeErrorEx(err, NULL);
ds@621
   496
	}
ds@621
   497
ds@621
   498
	return 1;
ds@621
   499
}
ds@621
   500
ds@621
   501
static cell_t sm_CallPushString(IPluginContext *pContext, const cell_t *params)
ds@621
   502
{
ds@621
   503
	int err;
ds@621
   504
	char *value;
ds@621
   505
ds@621
   506
	if (!s_CallStarted)
ds@621
   507
	{
ds@621
   508
		return pContext->ThrowNativeError("Cannot push parameters when there is no call in progress");
ds@621
   509
	}
ds@621
   510
ds@621
   511
	pContext->LocalToString(params[1], &value);
ds@621
   512
ds@621
   513
	err = s_pCallable->PushString(value);
ds@621
   514
ds@621
   515
	if (err)
ds@621
   516
	{
ds@621
   517
		s_pCallable->Cancel();
ds@621
   518
		ResetCall();
ds@621
   519
		return pContext->ThrowNativeErrorEx(err, NULL);
ds@621
   520
	}
ds@621
   521
ds@621
   522
	return 1;
ds@621
   523
}
ds@621
   524
ds@621
   525
static cell_t sm_CallPushStringEx(IPluginContext *pContext, const cell_t *params)
ds@621
   526
{
ds@621
   527
	int err;
ds@621
   528
	char *value;
ds@621
   529
ds@621
   530
	if (!s_CallStarted)
ds@621
   531
	{
ds@621
   532
		return pContext->ThrowNativeError("Cannot push parameters when there is no call in progress");
ds@621
   533
	}
ds@621
   534
ds@621
   535
	pContext->LocalToString(params[1], &value);
ds@621
   536
ds@621
   537
	err = s_pCallable->PushStringEx(value, params[2], params[3], params[4]);
ds@621
   538
ds@621
   539
	if (err)
ds@621
   540
	{
ds@621
   541
		s_pCallable->Cancel();
ds@621
   542
		ResetCall();
ds@621
   543
		return pContext->ThrowNativeErrorEx(err, NULL);
ds@621
   544
	}
ds@621
   545
ds@621
   546
	return 1;
ds@621
   547
}
ds@621
   548
ds@621
   549
static cell_t sm_CallFinish(IPluginContext *pContext, const cell_t *params)
ds@621
   550
{
faluco@629
   551
	int err = SP_ERROR_NOT_RUNNABLE;
ds@621
   552
	cell_t *result;
ds@621
   553
ds@621
   554
	if (!s_CallStarted)
ds@621
   555
	{
ds@621
   556
		return pContext->ThrowNativeError("Cannot finish call when there is no call in progress");
ds@621
   557
	}
ds@621
   558
ds@621
   559
	pContext->LocalToPhysAddr(params[1], &result);
ds@621
   560
ds@621
   561
	if (s_pFunction)
ds@621
   562
	{
ds@621
   563
		IPluginFunction *pFunction = s_pFunction;
ds@1082
   564
		ResetCall();
ds@621
   565
		err = pFunction->Execute(result);
ds@621
   566
	} else if (s_pForward) {
ds@621
   567
		IForward *pForward = s_pForward;
ds@1082
   568
		ResetCall();
ds@621
   569
		err = pForward->Execute(result, NULL);
ds@621
   570
	}
ds@621
   571
ds@621
   572
	return err;
ds@621
   573
}
ds@621
   574
ds@621
   575
static cell_t sm_CallCancel(IPluginContext *pContext, const cell_t *params)
ds@621
   576
{
ds@621
   577
	if (!s_CallStarted)
ds@621
   578
	{
ds@621
   579
		return pContext->ThrowNativeError("Cannot cancel call when there is no call in progress");
ds@621
   580
	}
ds@621
   581
ds@621
   582
	s_pCallable->Cancel();
ds@621
   583
	ResetCall();
ds@621
   584
ds@621
   585
	return 1;
ds@621
   586
}
ds@621
   587
ds@621
   588
REGISTER_NATIVES(functionNatives)
ds@621
   589
{
ds@621
   590
	{"GetFunctionByName",		sm_GetFunctionByName},
ds@621
   591
	{"CreateGlobalForward",		sm_CreateGlobalForward},
ds@621
   592
	{"CreateForward",			sm_CreateForward},
ds@621
   593
	{"GetForwardFunctionCount",	sm_GetForwardFunctionCount},
ds@621
   594
	{"AddToForward",			sm_AddToForward},
ds@621
   595
	{"RemoveFromForward",		sm_RemoveFromForward},
ds@621
   596
	{"RemoveAllFromForward",	sm_RemoveAllFromForward},
ds@621
   597
	{"Call_StartFunction",		sm_CallStartFunction},
ds@621
   598
	{"Call_StartForward",		sm_CallStartForward},
ds@621
   599
	{"Call_PushCell",			sm_CallPushCell},
ds@621
   600
	{"Call_PushCellRef",		sm_CallPushCellRef},
ds@621
   601
	{"Call_PushFloat",			sm_CallPushFloat},
ds@621
   602
	{"Call_PushFloatRef",		sm_CallPushFloatRef},
ds@621
   603
	{"Call_PushArray",			sm_CallPushArray},
ds@621
   604
	{"Call_PushArrayEx",		sm_CallPushArrayEx},
ds@621
   605
	{"Call_PushString",			sm_CallPushString},
ds@621
   606
	{"Call_PushStringEx",		sm_CallPushStringEx},
ds@621
   607
	{"Call_Finish",				sm_CallFinish},
ds@621
   608
	{"Call_Cancel",				sm_CallCancel},
ds@621
   609
	{NULL,						NULL},
ds@621
   610
};