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