core/logic/smn_functions.cpp
author Asher Baker <asherkin@gmail.com>
Thu May 16 14:58:14 2013 +0100 (2013-05-16)
changeset 3920 e4fa6b583893
parent 2730 a2ff0a7a1c53
child 3955 763d59fe6d77
permissions -rw-r--r--
Fixed copy-paste error in 116256b519b5.
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;
ds@621
   190
dvander@2730
   191
	if ((err=handlesys->ReadHandle(hndl, g_GlobalFwdType, NULL, (void **)&pForward))
ds@621
   192
		!= HandleError_None)
ds@621
   193
	{
ds@621
   194
		return pContext->ThrowNativeError("Invalid forward handle %x (error %d)", hndl, err);
ds@621
   195
	}
ds@621
   196
ds@621
   197
	return pForward->GetFunctionCount();
ds@621
   198
}
ds@621
   199
ds@621
   200
static cell_t sm_AddToForward(IPluginContext *pContext, const cell_t *params)
ds@621
   201
{
ds@621
   202
	Handle_t fwdHandle = static_cast<Handle_t>(params[1]);
ds@621
   203
	Handle_t plHandle = static_cast<Handle_t>(params[2]);
ds@621
   204
	HandleError err;
ds@621
   205
	IChangeableForward *pForward;
ds@621
   206
	IPlugin *pPlugin;
ds@621
   207
dvander@2730
   208
	if ((err=handlesys->ReadHandle(fwdHandle, g_PrivateFwdType, NULL, (void **)&pForward))
ds@621
   209
		!= HandleError_None)
ds@621
   210
	{
ds@621
   211
		return pContext->ThrowNativeError("Invalid private forward handle %x (error %d)", fwdHandle, err);
ds@621
   212
	}
ds@621
   213
ds@621
   214
	if (plHandle == 0)
ds@621
   215
	{
dvander@2730
   216
		pPlugin = pluginsys->FindPluginByContext(pContext->GetContext());
ds@621
   217
	} else {
dvander@2730
   218
		pPlugin = pluginsys->PluginFromHandle(plHandle, &err);
ds@621
   219
ds@621
   220
		if (!pPlugin)
ds@621
   221
		{
ds@621
   222
			return pContext->ThrowNativeError("Plugin handle %x is invalid (error %d)", plHandle, err);
ds@621
   223
		}
ds@621
   224
	}
ds@621
   225
ds@621
   226
	IPluginFunction *pFunction = pPlugin->GetBaseContext()->GetFunctionById(params[3]);
ds@621
   227
ds@621
   228
	if (!pFunction)
ds@621
   229
	{
ds@621
   230
		return pContext->ThrowNativeError("Invalid function id (%X)", params[3]);
ds@621
   231
	}
ds@621
   232
ds@621
   233
	return pForward->AddFunction(pFunction);
ds@621
   234
}
ds@621
   235
ds@621
   236
static cell_t sm_RemoveFromForward(IPluginContext *pContext, const cell_t *params)
ds@621
   237
{
ds@621
   238
	Handle_t fwdHandle = static_cast<Handle_t>(params[1]);
ds@621
   239
	Handle_t plHandle = static_cast<Handle_t>(params[2]);
ds@621
   240
	HandleError err;
ds@621
   241
	IChangeableForward *pForward;
ds@621
   242
	IPlugin *pPlugin;
ds@621
   243
dvander@2730
   244
	if ((err=handlesys->ReadHandle(fwdHandle, g_PrivateFwdType, NULL, (void **)&pForward))
ds@621
   245
		!= HandleError_None)
ds@621
   246
	{
ds@621
   247
		return pContext->ThrowNativeError("Invalid private forward handle %x (error %d)", fwdHandle, err);
ds@621
   248
	}
ds@621
   249
ds@621
   250
	if (plHandle == 0)
ds@621
   251
	{
dvander@2730
   252
		pPlugin = pluginsys->FindPluginByContext(pContext->GetContext());
ds@621
   253
	} else {
dvander@2730
   254
		pPlugin = pluginsys->PluginFromHandle(plHandle, &err);
ds@621
   255
ds@621
   256
		if (!pPlugin)
ds@621
   257
		{
ds@621
   258
			return pContext->ThrowNativeError("Plugin handle %x is invalid (error %d)", plHandle, err);
ds@621
   259
		}
ds@621
   260
	}
ds@621
   261
ds@621
   262
	IPluginFunction *pFunction = pPlugin->GetBaseContext()->GetFunctionById(params[3]);
ds@621
   263
ds@621
   264
	if (!pFunction)
ds@621
   265
	{
ds@621
   266
		return pContext->ThrowNativeError("Invalid function id (%X)", params[3]);
ds@621
   267
	}
ds@621
   268
ds@621
   269
	return pForward->RemoveFunction(pFunction);
ds@621
   270
}
ds@621
   271
ds@621
   272
static cell_t sm_RemoveAllFromForward(IPluginContext *pContext, const cell_t *params)
ds@621
   273
{
ds@621
   274
	Handle_t fwdHandle = static_cast<Handle_t>(params[1]);
ds@621
   275
	Handle_t plHandle = static_cast<Handle_t>(params[2]);
ds@621
   276
	HandleError err;
ds@621
   277
	IChangeableForward *pForward;
ds@621
   278
	IPlugin *pPlugin;
ds@621
   279
dvander@2730
   280
	if ((err=handlesys->ReadHandle(fwdHandle, g_PrivateFwdType, NULL, (void **)&pForward))
ds@621
   281
		!= HandleError_None)
ds@621
   282
	{
ds@621
   283
		return pContext->ThrowNativeError("Invalid private forward handle %x (error %d)", fwdHandle, err);
ds@621
   284
	}
ds@621
   285
ds@621
   286
	if (plHandle == 0)
ds@621
   287
	{
dvander@2730
   288
		pPlugin = pluginsys->FindPluginByContext(pContext->GetContext());
ds@621
   289
	} else {
dvander@2730
   290
		pPlugin = pluginsys->PluginFromHandle(plHandle, &err);
ds@621
   291
ds@621
   292
		if (!pPlugin)
ds@621
   293
		{
ds@621
   294
			return pContext->ThrowNativeError("Plugin handle %x is invalid (error %d)", plHandle, err);
ds@621
   295
		}
ds@621
   296
	}
ds@621
   297
ds@621
   298
	return pForward->RemoveFunctionsOfPlugin(pPlugin);
ds@621
   299
}
ds@621
   300
ds@621
   301
static cell_t sm_CallStartFunction(IPluginContext *pContext, const cell_t *params)
ds@621
   302
{
ds@621
   303
	Handle_t hndl;
ds@621
   304
	HandleError err;
ds@621
   305
	IPlugin *pPlugin;
ds@621
   306
dvander@2161
   307
	ResetCall();
ds@621
   308
ds@621
   309
	hndl = static_cast<Handle_t>(params[1]);
ds@621
   310
ds@621
   311
	if (hndl == 0)
ds@621
   312
	{
dvander@2730
   313
		pPlugin = pluginsys->FindPluginByContext(pContext->GetContext());
ds@621
   314
	} else {
dvander@2730
   315
		pPlugin = pluginsys->PluginFromHandle(hndl, &err);
ds@621
   316
ds@621
   317
		if (!pPlugin)
ds@621
   318
		{
ds@621
   319
			return pContext->ThrowNativeError("Plugin handle %x is invalid (error %d)", hndl, err);
ds@621
   320
		}
ds@621
   321
	}
ds@621
   322
ds@621
   323
	s_pFunction = pPlugin->GetBaseContext()->GetFunctionById(params[2]);
ds@621
   324
ds@621
   325
	if (!s_pFunction)
ds@621
   326
	{
ds@621
   327
		return pContext->ThrowNativeError("Invalid function id (%X)", params[2]);
ds@621
   328
	}
ds@621
   329
ds@621
   330
	s_pCallable = static_cast<ICallable *>(s_pFunction);
ds@621
   331
ds@621
   332
	s_CallStarted = true;
ds@621
   333
ds@621
   334
	return 1;
ds@621
   335
}
ds@621
   336
ds@621
   337
static cell_t sm_CallStartForward(IPluginContext *pContext, const cell_t *params)
ds@621
   338
{
ds@621
   339
	Handle_t hndl;
ds@621
   340
	HandleError err;
ds@621
   341
	IForward *pForward;
ds@621
   342
dvander@2161
   343
	ResetCall();
ds@621
   344
ds@621
   345
	hndl = static_cast<Handle_t>(params[1]);
ds@621
   346
dvander@2730
   347
	if ((err=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
};