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