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