core/smn_functions.cpp
author David Anderson <dvander@alliedmods.net>
Thu Jul 19 03:22:10 2007 +0000 (2007-07-19)
changeset 1134 68e8b8d386ff
parent 1082 bb923439d570
child 1231 17badaf66ce4
permissions -rw-r--r--
fixed another translator cache issue
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@1082
    78
	s_pForward = NULL;
ds@621
    79
	s_pCallable = NULL;
ds@621
    80
}
ds@621
    81
ds@621
    82
static cell_t sm_GetFunctionByName(IPluginContext *pContext, const cell_t *params)
ds@621
    83
{
ds@621
    84
	Handle_t hndl = static_cast<Handle_t>(params[1]);
ds@621
    85
	HandleError err;
ds@621
    86
	char *name;
ds@621
    87
	uint32_t idx;
ds@621
    88
	IPlugin *pPlugin;
ds@621
    89
ds@621
    90
	if (hndl == 0)
ds@621
    91
	{
ds@621
    92
		pPlugin = g_PluginSys.FindPluginByContext(pContext->GetContext());
ds@621
    93
	} else {
ds@621
    94
		pPlugin = g_PluginSys.PluginFromHandle(hndl, &err);
ds@621
    95
ds@621
    96
		if (!pPlugin)
ds@621
    97
		{
ds@621
    98
			return pContext->ThrowNativeError("Plugin handle %x is invalid (error %d)", hndl, err);
ds@621
    99
		}
ds@621
   100
	}
ds@621
   101
ds@621
   102
	pContext->LocalToString(params[2], &name);
ds@621
   103
ds@621
   104
	/* Get public function index */
ds@621
   105
	if (pPlugin->GetBaseContext()->FindPublicByName(name, &idx) == SP_ERROR_NOT_FOUND)
ds@621
   106
	{
ds@621
   107
		/* Return INVALID_FUNCTION if not found */
ds@621
   108
		return -1;
ds@621
   109
	}
ds@621
   110
ds@621
   111
	/* Return function ID */
ds@621
   112
	return PublicIndexToFuncId(idx);
ds@621
   113
}
ds@621
   114
ds@621
   115
static cell_t sm_CreateGlobalForward(IPluginContext *pContext, const cell_t *params)
ds@621
   116
{
ds@621
   117
	cell_t count = params[0];
ds@621
   118
	char *name;
ds@621
   119
	ParamType forwardParams[SP_MAX_EXEC_PARAMS];
ds@621
   120
ds@621
   121
	if (count - 2 > SP_MAX_EXEC_PARAMS)
ds@621
   122
	{
ds@621
   123
		return pContext->ThrowNativeErrorEx(SP_ERROR_PARAMS_MAX, NULL);
ds@621
   124
	}
ds@621
   125
	
ds@621
   126
	pContext->LocalToString(params[1], &name);
ds@621
   127
ds@621
   128
	cell_t *addr;
ds@621
   129
	for (int i = 3; i <= count; i++)
ds@621
   130
	{
ds@621
   131
		pContext->LocalToPhysAddr(params[i], &addr);
ds@621
   132
		forwardParams[i - 3] = static_cast<ParamType>(*addr);
ds@621
   133
	}
ds@621
   134
ds@621
   135
	IForward *pForward = g_Forwards.CreateForward(name, static_cast<ExecType>(params[2]), count - 2, forwardParams);
ds@621
   136
ds@621
   137
	return g_HandleSys.CreateHandle(g_GlobalFwdType, pForward, pContext->GetIdentity(), g_pCoreIdent, NULL);
ds@621
   138
}
ds@621
   139
ds@621
   140
static cell_t sm_CreateForward(IPluginContext *pContext, const cell_t *params)
ds@621
   141
{
ds@621
   142
	cell_t count = params[0];
ds@621
   143
	ParamType forwardParams[SP_MAX_EXEC_PARAMS];
ds@621
   144
ds@621
   145
	if (count - 1 > SP_MAX_EXEC_PARAMS)
ds@621
   146
	{
ds@621
   147
		return pContext->ThrowNativeErrorEx(SP_ERROR_PARAMS_MAX, NULL);
ds@621
   148
	}
ds@621
   149
ds@621
   150
	cell_t *addr;
ds@621
   151
	for (int i = 2; i <= count; i++)
ds@621
   152
	{
ds@621
   153
		pContext->LocalToPhysAddr(params[i], &addr);
ds@621
   154
		forwardParams[i - 2] = static_cast<ParamType>(*addr);
ds@621
   155
	}
ds@621
   156
ds@621
   157
	IChangeableForward *pForward = g_Forwards.CreateForwardEx(NULL, static_cast<ExecType>(params[1]), count - 1, forwardParams);
ds@621
   158
ds@621
   159
	return g_HandleSys.CreateHandle(g_PrivateFwdType, pForward, pContext->GetIdentity(), g_pCoreIdent, NULL);
ds@621
   160
}
ds@621
   161
ds@621
   162
static cell_t sm_GetForwardFunctionCount(IPluginContext *pContext, const cell_t *params)
ds@621
   163
{
ds@621
   164
	Handle_t hndl = static_cast<Handle_t>(params[1]);
ds@621
   165
	HandleError err;
ds@621
   166
	IForward *pForward;
ds@621
   167
ds@621
   168
	if ((err=g_HandleSys.ReadHandle(hndl, g_GlobalFwdType, NULL, (void **)&pForward))
ds@621
   169
		!= HandleError_None)
ds@621
   170
	{
ds@621
   171
		return pContext->ThrowNativeError("Invalid forward handle %x (error %d)", hndl, err);
ds@621
   172
	}
ds@621
   173
ds@621
   174
	return pForward->GetFunctionCount();
ds@621
   175
}
ds@621
   176
ds@621
   177
static cell_t sm_AddToForward(IPluginContext *pContext, const cell_t *params)
ds@621
   178
{
ds@621
   179
	Handle_t fwdHandle = static_cast<Handle_t>(params[1]);
ds@621
   180
	Handle_t plHandle = static_cast<Handle_t>(params[2]);
ds@621
   181
	HandleError err;
ds@621
   182
	IChangeableForward *pForward;
ds@621
   183
	IPlugin *pPlugin;
ds@621
   184
ds@621
   185
	if ((err=g_HandleSys.ReadHandle(fwdHandle, g_PrivateFwdType, NULL, (void **)&pForward))
ds@621
   186
		!= HandleError_None)
ds@621
   187
	{
ds@621
   188
		return pContext->ThrowNativeError("Invalid private forward handle %x (error %d)", fwdHandle, err);
ds@621
   189
	}
ds@621
   190
ds@621
   191
	if (plHandle == 0)
ds@621
   192
	{
ds@621
   193
		pPlugin = g_PluginSys.FindPluginByContext(pContext->GetContext());
ds@621
   194
	} else {
ds@621
   195
		pPlugin = g_PluginSys.PluginFromHandle(plHandle, &err);
ds@621
   196
ds@621
   197
		if (!pPlugin)
ds@621
   198
		{
ds@621
   199
			return pContext->ThrowNativeError("Plugin handle %x is invalid (error %d)", plHandle, err);
ds@621
   200
		}
ds@621
   201
	}
ds@621
   202
ds@621
   203
	IPluginFunction *pFunction = pPlugin->GetBaseContext()->GetFunctionById(params[3]);
ds@621
   204
ds@621
   205
	if (!pFunction)
ds@621
   206
	{
ds@621
   207
		return pContext->ThrowNativeError("Invalid function id (%X)", params[3]);
ds@621
   208
	}
ds@621
   209
ds@621
   210
	return pForward->AddFunction(pFunction);
ds@621
   211
}
ds@621
   212
ds@621
   213
static cell_t sm_RemoveFromForward(IPluginContext *pContext, const cell_t *params)
ds@621
   214
{
ds@621
   215
	Handle_t fwdHandle = static_cast<Handle_t>(params[1]);
ds@621
   216
	Handle_t plHandle = static_cast<Handle_t>(params[2]);
ds@621
   217
	HandleError err;
ds@621
   218
	IChangeableForward *pForward;
ds@621
   219
	IPlugin *pPlugin;
ds@621
   220
ds@621
   221
	if ((err=g_HandleSys.ReadHandle(fwdHandle, g_PrivateFwdType, NULL, (void **)&pForward))
ds@621
   222
		!= HandleError_None)
ds@621
   223
	{
ds@621
   224
		return pContext->ThrowNativeError("Invalid private forward handle %x (error %d)", fwdHandle, err);
ds@621
   225
	}
ds@621
   226
ds@621
   227
	if (plHandle == 0)
ds@621
   228
	{
ds@621
   229
		pPlugin = g_PluginSys.FindPluginByContext(pContext->GetContext());
ds@621
   230
	} else {
ds@621
   231
		pPlugin = g_PluginSys.PluginFromHandle(plHandle, &err);
ds@621
   232
ds@621
   233
		if (!pPlugin)
ds@621
   234
		{
ds@621
   235
			return pContext->ThrowNativeError("Plugin handle %x is invalid (error %d)", plHandle, err);
ds@621
   236
		}
ds@621
   237
	}
ds@621
   238
ds@621
   239
	IPluginFunction *pFunction = pPlugin->GetBaseContext()->GetFunctionById(params[3]);
ds@621
   240
ds@621
   241
	if (!pFunction)
ds@621
   242
	{
ds@621
   243
		return pContext->ThrowNativeError("Invalid function id (%X)", params[3]);
ds@621
   244
	}
ds@621
   245
ds@621
   246
	return pForward->RemoveFunction(pFunction);
ds@621
   247
}
ds@621
   248
ds@621
   249
static cell_t sm_RemoveAllFromForward(IPluginContext *pContext, const cell_t *params)
ds@621
   250
{
ds@621
   251
	Handle_t fwdHandle = static_cast<Handle_t>(params[1]);
ds@621
   252
	Handle_t plHandle = static_cast<Handle_t>(params[2]);
ds@621
   253
	HandleError err;
ds@621
   254
	IChangeableForward *pForward;
ds@621
   255
	IPlugin *pPlugin;
ds@621
   256
ds@621
   257
	if ((err=g_HandleSys.ReadHandle(fwdHandle, g_PrivateFwdType, NULL, (void **)&pForward))
ds@621
   258
		!= HandleError_None)
ds@621
   259
	{
ds@621
   260
		return pContext->ThrowNativeError("Invalid private forward handle %x (error %d)", fwdHandle, err);
ds@621
   261
	}
ds@621
   262
ds@621
   263
	if (plHandle == 0)
ds@621
   264
	{
ds@621
   265
		pPlugin = g_PluginSys.FindPluginByContext(pContext->GetContext());
ds@621
   266
	} else {
ds@621
   267
		pPlugin = g_PluginSys.PluginFromHandle(plHandle, &err);
ds@621
   268
ds@621
   269
		if (!pPlugin)
ds@621
   270
		{
ds@621
   271
			return pContext->ThrowNativeError("Plugin handle %x is invalid (error %d)", plHandle, err);
ds@621
   272
		}
ds@621
   273
	}
ds@621
   274
ds@621
   275
	return pForward->RemoveFunctionsOfPlugin(pPlugin);
ds@621
   276
}
ds@621
   277
ds@621
   278
static cell_t sm_CallStartFunction(IPluginContext *pContext, const cell_t *params)
ds@621
   279
{
ds@621
   280
	Handle_t hndl;
ds@621
   281
	HandleError err;
ds@621
   282
	IPlugin *pPlugin;
ds@621
   283
ds@621
   284
	if (s_CallStarted)
ds@621
   285
	{
ds@621
   286
		return pContext->ThrowNativeError("Cannot start a call while one is already in progress");
ds@621
   287
	}
ds@621
   288
ds@621
   289
	hndl = static_cast<Handle_t>(params[1]);
ds@621
   290
ds@621
   291
	if (hndl == 0)
ds@621
   292
	{
ds@621
   293
		pPlugin = g_PluginSys.FindPluginByContext(pContext->GetContext());
ds@621
   294
	} else {
ds@621
   295
		pPlugin = g_PluginSys.PluginFromHandle(hndl, &err);
ds@621
   296
ds@621
   297
		if (!pPlugin)
ds@621
   298
		{
ds@621
   299
			return pContext->ThrowNativeError("Plugin handle %x is invalid (error %d)", hndl, err);
ds@621
   300
		}
ds@621
   301
	}
ds@621
   302
ds@621
   303
	s_pFunction = pPlugin->GetBaseContext()->GetFunctionById(params[2]);
ds@621
   304
ds@621
   305
	if (!s_pFunction)
ds@621
   306
	{
ds@621
   307
		return pContext->ThrowNativeError("Invalid function id (%X)", params[2]);
ds@621
   308
	}
ds@621
   309
ds@621
   310
	s_pCallable = static_cast<ICallable *>(s_pFunction);
ds@621
   311
ds@621
   312
	s_CallStarted = true;
ds@621
   313
ds@621
   314
	return 1;
ds@621
   315
}
ds@621
   316
ds@621
   317
static cell_t sm_CallStartForward(IPluginContext *pContext, const cell_t *params)
ds@621
   318
{
ds@621
   319
	Handle_t hndl;
ds@621
   320
	HandleError err;
ds@621
   321
	IForward *pForward;
ds@621
   322
ds@621
   323
	if (s_CallStarted)
ds@621
   324
	{
ds@621
   325
		return pContext->ThrowNativeError("Cannot start a call while one is already in progress");
ds@621
   326
	}
ds@621
   327
ds@621
   328
	hndl = static_cast<Handle_t>(params[1]);
ds@621
   329
ds@621
   330
	if ((err=g_HandleSys.ReadHandle(hndl, g_GlobalFwdType, NULL, (void **)&pForward))
ds@621
   331
		!= HandleError_None)
ds@621
   332
	{
ds@621
   333
		return pContext->ThrowNativeError("Invalid forward handle %x (error %d)", hndl, err);
ds@621
   334
	}
ds@621
   335
ds@621
   336
	s_pForward = pForward;
ds@621
   337
ds@621
   338
	s_pCallable = static_cast<ICallable *>(pForward);
ds@621
   339
ds@621
   340
	s_CallStarted = true;
ds@621
   341
ds@621
   342
	return 1;
ds@621
   343
}
ds@621
   344
ds@621
   345
static cell_t sm_CallPushCell(IPluginContext *pContext, const cell_t *params)
ds@621
   346
{
ds@621
   347
	int err;
ds@621
   348
ds@621
   349
	if (!s_CallStarted)
ds@621
   350
	{
ds@621
   351
		return pContext->ThrowNativeError("Cannot push parameters when there is no call in progress");
ds@621
   352
	}
ds@621
   353
ds@621
   354
	err = s_pCallable->PushCell(params[1]);
ds@621
   355
ds@621
   356
	if (err)
ds@621
   357
	{
ds@621
   358
		s_pCallable->Cancel();
ds@621
   359
		ResetCall();
ds@621
   360
		return pContext->ThrowNativeErrorEx(err, NULL);
ds@621
   361
	}
ds@621
   362
ds@621
   363
	return 1;
ds@621
   364
}
ds@621
   365
ds@621
   366
static cell_t sm_CallPushCellRef(IPluginContext *pContext, const cell_t *params)
ds@621
   367
{
ds@621
   368
	int err;
ds@621
   369
	cell_t *addr;
ds@621
   370
ds@621
   371
	if (!s_CallStarted)
ds@621
   372
	{
ds@621
   373
		return pContext->ThrowNativeError("Cannot push parameters when there is no call in progress");
ds@621
   374
	}
ds@621
   375
ds@621
   376
	pContext->LocalToPhysAddr(params[1], &addr);
ds@621
   377
ds@621
   378
	err = s_pCallable->PushCellByRef(addr);
ds@621
   379
ds@621
   380
	if (err)
ds@621
   381
	{
ds@621
   382
		s_pCallable->Cancel();
ds@621
   383
		ResetCall();
ds@621
   384
		return pContext->ThrowNativeErrorEx(err, NULL);
ds@621
   385
	}
ds@621
   386
ds@621
   387
	return 1;
ds@621
   388
}
ds@621
   389
ds@621
   390
static cell_t sm_CallPushFloat(IPluginContext *pContext, const cell_t *params)
ds@621
   391
{
ds@621
   392
	int err;
ds@621
   393
ds@621
   394
	if (!s_CallStarted)
ds@621
   395
	{
ds@621
   396
		return pContext->ThrowNativeError("Cannot push parameters when there is no call in progress");
ds@621
   397
	}
ds@621
   398
ds@621
   399
	err = s_pCallable->PushFloat(sp_ctof(params[1]));
ds@621
   400
ds@621
   401
	if (err)
ds@621
   402
	{
ds@621
   403
		s_pCallable->Cancel();
ds@621
   404
		ResetCall();
ds@621
   405
		return pContext->ThrowNativeErrorEx(err, NULL);
ds@621
   406
	}
ds@621
   407
ds@621
   408
	return 1;
ds@621
   409
}
ds@621
   410
ds@621
   411
static cell_t sm_CallPushFloatRef(IPluginContext *pContext, const cell_t *params)
ds@621
   412
{
ds@621
   413
	int err;
ds@621
   414
	cell_t *addr;
ds@621
   415
ds@621
   416
	if (!s_CallStarted)
ds@621
   417
	{
ds@621
   418
		return pContext->ThrowNativeError("Cannot push parameters when there is no call in progress");
ds@621
   419
	}
ds@621
   420
ds@621
   421
	pContext->LocalToPhysAddr(params[1], &addr);
ds@621
   422
ds@621
   423
	err = s_pCallable->PushFloatByRef(reinterpret_cast<float *>(addr));
ds@621
   424
ds@621
   425
	if (err)
ds@621
   426
	{
ds@621
   427
		s_pCallable->Cancel();
ds@621
   428
		ResetCall();
ds@621
   429
		return pContext->ThrowNativeErrorEx(err, NULL);
ds@621
   430
	}
ds@621
   431
ds@621
   432
	return 1;
ds@621
   433
}
ds@621
   434
ds@621
   435
static cell_t sm_CallPushArray(IPluginContext *pContext, const cell_t *params)
ds@621
   436
{
ds@621
   437
	int err;
ds@621
   438
	cell_t *addr;
ds@621
   439
ds@621
   440
	if (!s_CallStarted)
ds@621
   441
	{
ds@621
   442
		return pContext->ThrowNativeError("Cannot push parameters when there is no call in progress");
ds@621
   443
	}
ds@621
   444
ds@621
   445
	pContext->LocalToPhysAddr(params[1], &addr);
ds@621
   446
faluco@629
   447
	err = s_pCallable->PushArray(addr, params[2]);
ds@621
   448
ds@621
   449
	if (err)
ds@621
   450
	{
ds@621
   451
		s_pCallable->Cancel();
ds@621
   452
		ResetCall();
ds@621
   453
		return pContext->ThrowNativeErrorEx(err, NULL);
ds@621
   454
	}
ds@621
   455
ds@621
   456
	return 1;
ds@621
   457
}
ds@621
   458
ds@621
   459
static cell_t sm_CallPushArrayEx(IPluginContext *pContext, const cell_t *params)
ds@621
   460
{
ds@621
   461
	int err;
ds@621
   462
	cell_t *addr;
ds@621
   463
ds@621
   464
	if (!s_CallStarted)
ds@621
   465
	{
ds@621
   466
		return pContext->ThrowNativeError("Cannot push parameters when there is no call in progress");
ds@621
   467
	}
ds@621
   468
ds@621
   469
	pContext->LocalToPhysAddr(params[1], &addr);
ds@621
   470
ds@621
   471
	err = s_pCallable->PushArray(addr, params[2], params[3]);
ds@621
   472
ds@621
   473
	if (err)
ds@621
   474
	{
ds@621
   475
		s_pCallable->Cancel();
ds@621
   476
		ResetCall();
ds@621
   477
		return pContext->ThrowNativeErrorEx(err, NULL);
ds@621
   478
	}
ds@621
   479
ds@621
   480
	return 1;
ds@621
   481
}
ds@621
   482
ds@621
   483
static cell_t sm_CallPushString(IPluginContext *pContext, const cell_t *params)
ds@621
   484
{
ds@621
   485
	int err;
ds@621
   486
	char *value;
ds@621
   487
ds@621
   488
	if (!s_CallStarted)
ds@621
   489
	{
ds@621
   490
		return pContext->ThrowNativeError("Cannot push parameters when there is no call in progress");
ds@621
   491
	}
ds@621
   492
ds@621
   493
	pContext->LocalToString(params[1], &value);
ds@621
   494
ds@621
   495
	err = s_pCallable->PushString(value);
ds@621
   496
ds@621
   497
	if (err)
ds@621
   498
	{
ds@621
   499
		s_pCallable->Cancel();
ds@621
   500
		ResetCall();
ds@621
   501
		return pContext->ThrowNativeErrorEx(err, NULL);
ds@621
   502
	}
ds@621
   503
ds@621
   504
	return 1;
ds@621
   505
}
ds@621
   506
ds@621
   507
static cell_t sm_CallPushStringEx(IPluginContext *pContext, const cell_t *params)
ds@621
   508
{
ds@621
   509
	int err;
ds@621
   510
	char *value;
ds@621
   511
ds@621
   512
	if (!s_CallStarted)
ds@621
   513
	{
ds@621
   514
		return pContext->ThrowNativeError("Cannot push parameters when there is no call in progress");
ds@621
   515
	}
ds@621
   516
ds@621
   517
	pContext->LocalToString(params[1], &value);
ds@621
   518
ds@621
   519
	err = s_pCallable->PushStringEx(value, params[2], params[3], params[4]);
ds@621
   520
ds@621
   521
	if (err)
ds@621
   522
	{
ds@621
   523
		s_pCallable->Cancel();
ds@621
   524
		ResetCall();
ds@621
   525
		return pContext->ThrowNativeErrorEx(err, NULL);
ds@621
   526
	}
ds@621
   527
ds@621
   528
	return 1;
ds@621
   529
}
ds@621
   530
ds@621
   531
static cell_t sm_CallFinish(IPluginContext *pContext, const cell_t *params)
ds@621
   532
{
faluco@629
   533
	int err = SP_ERROR_NOT_RUNNABLE;
ds@621
   534
	cell_t *result;
ds@621
   535
ds@621
   536
	if (!s_CallStarted)
ds@621
   537
	{
ds@621
   538
		return pContext->ThrowNativeError("Cannot finish call when there is no call in progress");
ds@621
   539
	}
ds@621
   540
ds@621
   541
	pContext->LocalToPhysAddr(params[1], &result);
ds@621
   542
ds@621
   543
	if (s_pFunction)
ds@621
   544
	{
ds@621
   545
		IPluginFunction *pFunction = s_pFunction;
ds@1082
   546
		ResetCall();
ds@621
   547
		err = pFunction->Execute(result);
ds@621
   548
	} else if (s_pForward) {
ds@621
   549
		IForward *pForward = s_pForward;
ds@1082
   550
		ResetCall();
ds@621
   551
		err = pForward->Execute(result, NULL);
ds@621
   552
	}
ds@621
   553
ds@621
   554
	return err;
ds@621
   555
}
ds@621
   556
ds@621
   557
static cell_t sm_CallCancel(IPluginContext *pContext, const cell_t *params)
ds@621
   558
{
ds@621
   559
	if (!s_CallStarted)
ds@621
   560
	{
ds@621
   561
		return pContext->ThrowNativeError("Cannot cancel call when there is no call in progress");
ds@621
   562
	}
ds@621
   563
ds@621
   564
	s_pCallable->Cancel();
ds@621
   565
	ResetCall();
ds@621
   566
ds@621
   567
	return 1;
ds@621
   568
}
ds@621
   569
ds@621
   570
REGISTER_NATIVES(functionNatives)
ds@621
   571
{
ds@621
   572
	{"GetFunctionByName",		sm_GetFunctionByName},
ds@621
   573
	{"CreateGlobalForward",		sm_CreateGlobalForward},
ds@621
   574
	{"CreateForward",			sm_CreateForward},
ds@621
   575
	{"GetForwardFunctionCount",	sm_GetForwardFunctionCount},
ds@621
   576
	{"AddToForward",			sm_AddToForward},
ds@621
   577
	{"RemoveFromForward",		sm_RemoveFromForward},
ds@621
   578
	{"RemoveAllFromForward",	sm_RemoveAllFromForward},
ds@621
   579
	{"Call_StartFunction",		sm_CallStartFunction},
ds@621
   580
	{"Call_StartForward",		sm_CallStartForward},
ds@621
   581
	{"Call_PushCell",			sm_CallPushCell},
ds@621
   582
	{"Call_PushCellRef",		sm_CallPushCellRef},
ds@621
   583
	{"Call_PushFloat",			sm_CallPushFloat},
ds@621
   584
	{"Call_PushFloatRef",		sm_CallPushFloatRef},
ds@621
   585
	{"Call_PushArray",			sm_CallPushArray},
ds@621
   586
	{"Call_PushArrayEx",		sm_CallPushArrayEx},
ds@621
   587
	{"Call_PushString",			sm_CallPushString},
ds@621
   588
	{"Call_PushStringEx",		sm_CallPushStringEx},
ds@621
   589
	{"Call_Finish",				sm_CallFinish},
ds@621
   590
	{"Call_Cancel",				sm_CallCancel},
ds@621
   591
	{NULL,						NULL},
ds@621
   592
};