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