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