core/logic/smn_functions.cpp
author Nicholas Hastings <skamonkey@gmail.com>
Wed Apr 23 21:40:50 2014 -0400 (2014-04-23)
changeset 4332 75c299312bec
parent 4235 ca11e4c5aa43
permissions -rw-r--r--
Update CS:GO gamedata.
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>
kyle@4162
    36
#include <ISourceMod.h>
kyle@4162
    37
#include <am-utility.h>
ds@621
    38
ds@621
    39
HandleType_t g_GlobalFwdType = 0;
ds@621
    40
HandleType_t g_PrivateFwdType = 0;
ds@621
    41
ds@621
    42
static bool s_CallStarted = false;
ds@621
    43
static ICallable *s_pCallable = NULL;
ds@621
    44
static IPluginFunction *s_pFunction = NULL;
ds@621
    45
static IForward *s_pForward = NULL;
ds@621
    46
ds@621
    47
class ForwardNativeHelpers : 
ds@621
    48
	public SMGlobalClass,
ds@621
    49
	public IHandleTypeDispatch
ds@621
    50
{
ds@621
    51
public:
ds@621
    52
	void OnSourceModAllInitialized()
ds@621
    53
	{
ds@621
    54
		HandleAccess sec;
ds@621
    55
ds@621
    56
		/* Set GlobalFwd handle access security */
dvander@2730
    57
		handlesys->InitAccessDefaults(NULL, &sec);
ds@621
    58
		sec.access[HandleAccess_Read] = 0;
ds@621
    59
		sec.access[HandleAccess_Clone] = HANDLE_RESTRICT_IDENTITY | HANDLE_RESTRICT_OWNER;
ds@621
    60
ds@621
    61
		/* Create 'GlobalFwd' handle type */
dvander@2730
    62
		g_GlobalFwdType = handlesys->CreateType("GlobalFwd", this, 0, NULL, &sec, g_pCoreIdent, NULL);
ds@621
    63
ds@621
    64
		/* Private forwards are cloneable */
ds@621
    65
		sec.access[HandleAccess_Clone] = 0;
ds@621
    66
ds@621
    67
		/* Create 'PrivateFwd' handle type */
dvander@2730
    68
		g_PrivateFwdType = handlesys->CreateType("PrivateFwd", this, g_GlobalFwdType, NULL, &sec, g_pCoreIdent, NULL);
ds@621
    69
	}
ds@621
    70
ds@621
    71
	void OnSourceModShutdown()
ds@621
    72
	{
dvander@2730
    73
		handlesys->RemoveType(g_PrivateFwdType, g_pCoreIdent);
dvander@2730
    74
		handlesys->RemoveType(g_GlobalFwdType, g_pCoreIdent);
ds@621
    75
	}
ds@621
    76
ds@621
    77
	void OnHandleDestroy(HandleType_t type, void *object)
ds@621
    78
	{
ds@621
    79
		IForward *pForward = static_cast<IForward *>(object);
ds@621
    80
dvander@2730
    81
		forwardsys->ReleaseForward(pForward);
ds@621
    82
	}
dvander@1766
    83
dvander@1766
    84
	bool GetHandleApproxSize(HandleType_t type, void *object, unsigned int *pSize)
dvander@1766
    85
	{
dvander@2730
    86
		*pSize = sizeof(IForward*) + (((IForward *)object)->GetFunctionCount() * 12);
dvander@1766
    87
		return true;
dvander@1766
    88
	}
ds@621
    89
} g_ForwardNativeHelpers;
ds@621
    90
ds@621
    91
ds@621
    92
/* Turn a public index into a function ID */
ds@621
    93
inline funcid_t PublicIndexToFuncId(uint32_t idx)
ds@621
    94
{
ds@621
    95
	return (idx << 1) | (1 << 0);
ds@621
    96
}
ds@621
    97
ds@621
    98
/* Reset global function/forward call variables */
ds@621
    99
inline void ResetCall()
ds@621
   100
{
ds@621
   101
	s_CallStarted = false;
ds@621
   102
	s_pFunction = NULL;
ds@1082
   103
	s_pForward = NULL;
ds@621
   104
	s_pCallable = NULL;
ds@621
   105
}
ds@621
   106
ds@621
   107
static cell_t sm_GetFunctionByName(IPluginContext *pContext, const cell_t *params)
ds@621
   108
{
ds@621
   109
	Handle_t hndl = static_cast<Handle_t>(params[1]);
ds@621
   110
	HandleError err;
ds@621
   111
	char *name;
ds@621
   112
	uint32_t idx;
ds@621
   113
	IPlugin *pPlugin;
ds@621
   114
ds@621
   115
	if (hndl == 0)
ds@621
   116
	{
dvander@2730
   117
		pPlugin = pluginsys->FindPluginByContext(pContext->GetContext());
ds@621
   118
	} else {
dvander@2730
   119
		pPlugin = pluginsys->PluginFromHandle(hndl, &err);
ds@621
   120
ds@621
   121
		if (!pPlugin)
ds@621
   122
		{
ds@621
   123
			return pContext->ThrowNativeError("Plugin handle %x is invalid (error %d)", hndl, err);
ds@621
   124
		}
ds@621
   125
	}
ds@621
   126
ds@621
   127
	pContext->LocalToString(params[2], &name);
ds@621
   128
ds@621
   129
	/* Get public function index */
ds@621
   130
	if (pPlugin->GetBaseContext()->FindPublicByName(name, &idx) == SP_ERROR_NOT_FOUND)
ds@621
   131
	{
ds@621
   132
		/* Return INVALID_FUNCTION if not found */
ds@621
   133
		return -1;
ds@621
   134
	}
ds@621
   135
ds@621
   136
	/* Return function ID */
ds@621
   137
	return PublicIndexToFuncId(idx);
ds@621
   138
}
ds@621
   139
ds@621
   140
static cell_t sm_CreateGlobalForward(IPluginContext *pContext, const cell_t *params)
ds@621
   141
{
ds@621
   142
	cell_t count = params[0];
ds@621
   143
	char *name;
ds@621
   144
	ParamType forwardParams[SP_MAX_EXEC_PARAMS];
ds@621
   145
ds@621
   146
	if (count - 2 > SP_MAX_EXEC_PARAMS)
ds@621
   147
	{
ds@621
   148
		return pContext->ThrowNativeErrorEx(SP_ERROR_PARAMS_MAX, NULL);
ds@621
   149
	}
ds@621
   150
	
ds@621
   151
	pContext->LocalToString(params[1], &name);
ds@621
   152
ds@621
   153
	cell_t *addr;
ds@621
   154
	for (int i = 3; i <= count; i++)
ds@621
   155
	{
ds@621
   156
		pContext->LocalToPhysAddr(params[i], &addr);
ds@621
   157
		forwardParams[i - 3] = static_cast<ParamType>(*addr);
ds@621
   158
	}
ds@621
   159
dvander@2730
   160
	IForward *pForward = forwardsys->CreateForward(name, static_cast<ExecType>(params[2]), count - 2, forwardParams);
ds@621
   161
dvander@2730
   162
	return handlesys->CreateHandle(g_GlobalFwdType, pForward, pContext->GetIdentity(), g_pCoreIdent, NULL);
ds@621
   163
}
ds@621
   164
ds@621
   165
static cell_t sm_CreateForward(IPluginContext *pContext, const cell_t *params)
ds@621
   166
{
ds@621
   167
	cell_t count = params[0];
ds@621
   168
	ParamType forwardParams[SP_MAX_EXEC_PARAMS];
ds@621
   169
ds@621
   170
	if (count - 1 > SP_MAX_EXEC_PARAMS)
ds@621
   171
	{
ds@621
   172
		return pContext->ThrowNativeErrorEx(SP_ERROR_PARAMS_MAX, NULL);
ds@621
   173
	}
ds@621
   174
ds@621
   175
	cell_t *addr;
ds@621
   176
	for (int i = 2; i <= count; i++)
ds@621
   177
	{
ds@621
   178
		pContext->LocalToPhysAddr(params[i], &addr);
ds@621
   179
		forwardParams[i - 2] = static_cast<ParamType>(*addr);
ds@621
   180
	}
ds@621
   181
dvander@2730
   182
	IChangeableForward *pForward = forwardsys->CreateForwardEx(NULL, static_cast<ExecType>(params[1]), count - 1, forwardParams);
ds@621
   183
dvander@2730
   184
	return handlesys->CreateHandle(g_PrivateFwdType, pForward, pContext->GetIdentity(), g_pCoreIdent, NULL);
ds@621
   185
}
ds@621
   186
ds@621
   187
static cell_t sm_GetForwardFunctionCount(IPluginContext *pContext, const cell_t *params)
ds@621
   188
{
ds@621
   189
	Handle_t hndl = static_cast<Handle_t>(params[1]);
ds@621
   190
	HandleError err;
ds@621
   191
	IForward *pForward;
kyle@3955
   192
	HandleSecurity sec(pContext->GetIdentity(), g_pCoreIdent);
ds@621
   193
kyle@3955
   194
	if ((err=handlesys->ReadHandle(hndl, g_GlobalFwdType, &sec, (void **)&pForward))
ds@621
   195
		!= HandleError_None)
ds@621
   196
	{
ds@621
   197
		return pContext->ThrowNativeError("Invalid forward handle %x (error %d)", hndl, err);
ds@621
   198
	}
ds@621
   199
ds@621
   200
	return pForward->GetFunctionCount();
ds@621
   201
}
ds@621
   202
ds@621
   203
static cell_t sm_AddToForward(IPluginContext *pContext, const cell_t *params)
ds@621
   204
{
ds@621
   205
	Handle_t fwdHandle = static_cast<Handle_t>(params[1]);
ds@621
   206
	Handle_t plHandle = static_cast<Handle_t>(params[2]);
ds@621
   207
	HandleError err;
ds@621
   208
	IChangeableForward *pForward;
ds@621
   209
	IPlugin *pPlugin;
kyle@3955
   210
	HandleSecurity sec(pContext->GetIdentity(), g_pCoreIdent);
ds@621
   211
kyle@3955
   212
	if ((err=handlesys->ReadHandle(fwdHandle, g_PrivateFwdType, &sec, (void **)&pForward))
ds@621
   213
		!= HandleError_None)
ds@621
   214
	{
ds@621
   215
		return pContext->ThrowNativeError("Invalid private forward handle %x (error %d)", fwdHandle, err);
ds@621
   216
	}
ds@621
   217
ds@621
   218
	if (plHandle == 0)
ds@621
   219
	{
dvander@2730
   220
		pPlugin = pluginsys->FindPluginByContext(pContext->GetContext());
ds@621
   221
	} else {
dvander@2730
   222
		pPlugin = pluginsys->PluginFromHandle(plHandle, &err);
ds@621
   223
ds@621
   224
		if (!pPlugin)
ds@621
   225
		{
ds@621
   226
			return pContext->ThrowNativeError("Plugin handle %x is invalid (error %d)", plHandle, err);
ds@621
   227
		}
ds@621
   228
	}
ds@621
   229
ds@621
   230
	IPluginFunction *pFunction = pPlugin->GetBaseContext()->GetFunctionById(params[3]);
ds@621
   231
ds@621
   232
	if (!pFunction)
ds@621
   233
	{
ds@621
   234
		return pContext->ThrowNativeError("Invalid function id (%X)", params[3]);
ds@621
   235
	}
ds@621
   236
ds@621
   237
	return pForward->AddFunction(pFunction);
ds@621
   238
}
ds@621
   239
ds@621
   240
static cell_t sm_RemoveFromForward(IPluginContext *pContext, const cell_t *params)
ds@621
   241
{
ds@621
   242
	Handle_t fwdHandle = static_cast<Handle_t>(params[1]);
ds@621
   243
	Handle_t plHandle = static_cast<Handle_t>(params[2]);
ds@621
   244
	HandleError err;
ds@621
   245
	IChangeableForward *pForward;
ds@621
   246
	IPlugin *pPlugin;
kyle@3955
   247
	HandleSecurity sec(pContext->GetIdentity(), g_pCoreIdent);
ds@621
   248
kyle@3955
   249
	if ((err=handlesys->ReadHandle(fwdHandle, g_PrivateFwdType, &sec, (void **)&pForward))
ds@621
   250
		!= HandleError_None)
ds@621
   251
	{
ds@621
   252
		return pContext->ThrowNativeError("Invalid private forward handle %x (error %d)", fwdHandle, err);
ds@621
   253
	}
ds@621
   254
ds@621
   255
	if (plHandle == 0)
ds@621
   256
	{
dvander@2730
   257
		pPlugin = pluginsys->FindPluginByContext(pContext->GetContext());
ds@621
   258
	} else {
dvander@2730
   259
		pPlugin = pluginsys->PluginFromHandle(plHandle, &err);
ds@621
   260
ds@621
   261
		if (!pPlugin)
ds@621
   262
		{
ds@621
   263
			return pContext->ThrowNativeError("Plugin handle %x is invalid (error %d)", plHandle, err);
ds@621
   264
		}
ds@621
   265
	}
ds@621
   266
ds@621
   267
	IPluginFunction *pFunction = pPlugin->GetBaseContext()->GetFunctionById(params[3]);
ds@621
   268
ds@621
   269
	if (!pFunction)
ds@621
   270
	{
ds@621
   271
		return pContext->ThrowNativeError("Invalid function id (%X)", params[3]);
ds@621
   272
	}
ds@621
   273
ds@621
   274
	return pForward->RemoveFunction(pFunction);
ds@621
   275
}
ds@621
   276
ds@621
   277
static cell_t sm_RemoveAllFromForward(IPluginContext *pContext, const cell_t *params)
ds@621
   278
{
ds@621
   279
	Handle_t fwdHandle = static_cast<Handle_t>(params[1]);
ds@621
   280
	Handle_t plHandle = static_cast<Handle_t>(params[2]);
ds@621
   281
	HandleError err;
ds@621
   282
	IChangeableForward *pForward;
ds@621
   283
	IPlugin *pPlugin;
kyle@3955
   284
	HandleSecurity sec(pContext->GetIdentity(), g_pCoreIdent);
ds@621
   285
kyle@3955
   286
	if ((err=handlesys->ReadHandle(fwdHandle, g_PrivateFwdType, &sec, (void **)&pForward))
ds@621
   287
		!= HandleError_None)
ds@621
   288
	{
ds@621
   289
		return pContext->ThrowNativeError("Invalid private forward handle %x (error %d)", fwdHandle, err);
ds@621
   290
	}
ds@621
   291
ds@621
   292
	if (plHandle == 0)
ds@621
   293
	{
dvander@2730
   294
		pPlugin = pluginsys->FindPluginByContext(pContext->GetContext());
ds@621
   295
	} else {
dvander@2730
   296
		pPlugin = pluginsys->PluginFromHandle(plHandle, &err);
ds@621
   297
ds@621
   298
		if (!pPlugin)
ds@621
   299
		{
ds@621
   300
			return pContext->ThrowNativeError("Plugin handle %x is invalid (error %d)", plHandle, err);
ds@621
   301
		}
ds@621
   302
	}
ds@621
   303
ds@621
   304
	return pForward->RemoveFunctionsOfPlugin(pPlugin);
ds@621
   305
}
ds@621
   306
ds@621
   307
static cell_t sm_CallStartFunction(IPluginContext *pContext, const cell_t *params)
ds@621
   308
{
ds@621
   309
	Handle_t hndl;
ds@621
   310
	HandleError err;
ds@621
   311
	IPlugin *pPlugin;
ds@621
   312
dvander@2161
   313
	ResetCall();
ds@621
   314
ds@621
   315
	hndl = static_cast<Handle_t>(params[1]);
ds@621
   316
ds@621
   317
	if (hndl == 0)
ds@621
   318
	{
dvander@2730
   319
		pPlugin = pluginsys->FindPluginByContext(pContext->GetContext());
ds@621
   320
	} else {
dvander@2730
   321
		pPlugin = pluginsys->PluginFromHandle(hndl, &err);
ds@621
   322
ds@621
   323
		if (!pPlugin)
ds@621
   324
		{
ds@621
   325
			return pContext->ThrowNativeError("Plugin handle %x is invalid (error %d)", hndl, err);
ds@621
   326
		}
ds@621
   327
	}
ds@621
   328
ds@621
   329
	s_pFunction = pPlugin->GetBaseContext()->GetFunctionById(params[2]);
ds@621
   330
ds@621
   331
	if (!s_pFunction)
ds@621
   332
	{
ds@621
   333
		return pContext->ThrowNativeError("Invalid function id (%X)", params[2]);
ds@621
   334
	}
ds@621
   335
ds@621
   336
	s_pCallable = static_cast<ICallable *>(s_pFunction);
ds@621
   337
ds@621
   338
	s_CallStarted = true;
ds@621
   339
ds@621
   340
	return 1;
ds@621
   341
}
ds@621
   342
ds@621
   343
static cell_t sm_CallStartForward(IPluginContext *pContext, const cell_t *params)
ds@621
   344
{
ds@621
   345
	Handle_t hndl;
ds@621
   346
	HandleError err;
ds@621
   347
	IForward *pForward;
kyle@3955
   348
	HandleSecurity sec(pContext->GetIdentity(), g_pCoreIdent);
ds@621
   349
dvander@2161
   350
	ResetCall();
ds@621
   351
ds@621
   352
	hndl = static_cast<Handle_t>(params[1]);
ds@621
   353
kyle@3955
   354
	if ((err=handlesys->ReadHandle(hndl, g_GlobalFwdType, &sec, (void **)&pForward))
ds@621
   355
		!= HandleError_None)
ds@621
   356
	{
ds@621
   357
		return pContext->ThrowNativeError("Invalid forward handle %x (error %d)", hndl, err);
ds@621
   358
	}
ds@621
   359
ds@621
   360
	s_pForward = pForward;
ds@621
   361
ds@621
   362
	s_pCallable = static_cast<ICallable *>(pForward);
ds@621
   363
ds@621
   364
	s_CallStarted = true;
ds@621
   365
ds@621
   366
	return 1;
ds@621
   367
}
ds@621
   368
ds@621
   369
static cell_t sm_CallPushCell(IPluginContext *pContext, const cell_t *params)
ds@621
   370
{
ds@621
   371
	int err;
ds@621
   372
ds@621
   373
	if (!s_CallStarted)
ds@621
   374
	{
ds@621
   375
		return pContext->ThrowNativeError("Cannot push parameters when there is no call in progress");
ds@621
   376
	}
ds@621
   377
ds@621
   378
	err = s_pCallable->PushCell(params[1]);
ds@621
   379
ds@621
   380
	if (err)
ds@621
   381
	{
ds@621
   382
		s_pCallable->Cancel();
ds@621
   383
		ResetCall();
ds@621
   384
		return pContext->ThrowNativeErrorEx(err, NULL);
ds@621
   385
	}
ds@621
   386
ds@621
   387
	return 1;
ds@621
   388
}
ds@621
   389
ds@621
   390
static cell_t sm_CallPushCellRef(IPluginContext *pContext, const cell_t *params)
ds@621
   391
{
ds@621
   392
	int err;
ds@621
   393
	cell_t *addr;
ds@621
   394
ds@621
   395
	if (!s_CallStarted)
ds@621
   396
	{
ds@621
   397
		return pContext->ThrowNativeError("Cannot push parameters when there is no call in progress");
ds@621
   398
	}
ds@621
   399
ds@621
   400
	pContext->LocalToPhysAddr(params[1], &addr);
ds@621
   401
ds@621
   402
	err = s_pCallable->PushCellByRef(addr);
ds@621
   403
ds@621
   404
	if (err)
ds@621
   405
	{
ds@621
   406
		s_pCallable->Cancel();
ds@621
   407
		ResetCall();
ds@621
   408
		return pContext->ThrowNativeErrorEx(err, NULL);
ds@621
   409
	}
ds@621
   410
ds@621
   411
	return 1;
ds@621
   412
}
ds@621
   413
ds@621
   414
static cell_t sm_CallPushFloat(IPluginContext *pContext, const cell_t *params)
ds@621
   415
{
ds@621
   416
	int err;
ds@621
   417
ds@621
   418
	if (!s_CallStarted)
ds@621
   419
	{
ds@621
   420
		return pContext->ThrowNativeError("Cannot push parameters when there is no call in progress");
ds@621
   421
	}
ds@621
   422
ds@621
   423
	err = s_pCallable->PushFloat(sp_ctof(params[1]));
ds@621
   424
ds@621
   425
	if (err)
ds@621
   426
	{
ds@621
   427
		s_pCallable->Cancel();
ds@621
   428
		ResetCall();
ds@621
   429
		return pContext->ThrowNativeErrorEx(err, NULL);
ds@621
   430
	}
ds@621
   431
ds@621
   432
	return 1;
ds@621
   433
}
ds@621
   434
ds@621
   435
static cell_t sm_CallPushFloatRef(IPluginContext *pContext, const cell_t *params)
ds@621
   436
{
ds@621
   437
	int err;
ds@621
   438
	cell_t *addr;
ds@621
   439
ds@621
   440
	if (!s_CallStarted)
ds@621
   441
	{
ds@621
   442
		return pContext->ThrowNativeError("Cannot push parameters when there is no call in progress");
ds@621
   443
	}
ds@621
   444
ds@621
   445
	pContext->LocalToPhysAddr(params[1], &addr);
ds@621
   446
ds@621
   447
	err = s_pCallable->PushFloatByRef(reinterpret_cast<float *>(addr));
ds@621
   448
ds@621
   449
	if (err)
ds@621
   450
	{
ds@621
   451
		s_pCallable->Cancel();
ds@621
   452
		ResetCall();
ds@621
   453
		return pContext->ThrowNativeErrorEx(err, NULL);
ds@621
   454
	}
ds@621
   455
ds@621
   456
	return 1;
ds@621
   457
}
ds@621
   458
ds@621
   459
static cell_t sm_CallPushArray(IPluginContext *pContext, const cell_t *params)
ds@621
   460
{
ds@621
   461
	int err;
ds@621
   462
	cell_t *addr;
ds@621
   463
ds@621
   464
	if (!s_CallStarted)
ds@621
   465
	{
ds@621
   466
		return pContext->ThrowNativeError("Cannot push parameters when there is no call in progress");
ds@621
   467
	}
ds@621
   468
ds@621
   469
	pContext->LocalToPhysAddr(params[1], &addr);
ds@621
   470
faluco@629
   471
	err = s_pCallable->PushArray(addr, params[2]);
ds@621
   472
ds@621
   473
	if (err)
ds@621
   474
	{
ds@621
   475
		s_pCallable->Cancel();
ds@621
   476
		ResetCall();
ds@621
   477
		return pContext->ThrowNativeErrorEx(err, NULL);
ds@621
   478
	}
ds@621
   479
ds@621
   480
	return 1;
ds@621
   481
}
ds@621
   482
ds@621
   483
static cell_t sm_CallPushArrayEx(IPluginContext *pContext, const cell_t *params)
ds@621
   484
{
ds@621
   485
	int err;
ds@621
   486
	cell_t *addr;
ds@621
   487
ds@621
   488
	if (!s_CallStarted)
ds@621
   489
	{
ds@621
   490
		return pContext->ThrowNativeError("Cannot push parameters when there is no call in progress");
ds@621
   491
	}
ds@621
   492
ds@621
   493
	pContext->LocalToPhysAddr(params[1], &addr);
ds@621
   494
ds@621
   495
	err = s_pCallable->PushArray(addr, params[2], params[3]);
ds@621
   496
ds@621
   497
	if (err)
ds@621
   498
	{
ds@621
   499
		s_pCallable->Cancel();
ds@621
   500
		ResetCall();
ds@621
   501
		return pContext->ThrowNativeErrorEx(err, NULL);
ds@621
   502
	}
ds@621
   503
ds@621
   504
	return 1;
ds@621
   505
}
ds@621
   506
ds@621
   507
static cell_t sm_CallPushString(IPluginContext *pContext, const cell_t *params)
ds@621
   508
{
ds@621
   509
	int err;
ds@621
   510
	char *value;
ds@621
   511
ds@621
   512
	if (!s_CallStarted)
ds@621
   513
	{
ds@621
   514
		return pContext->ThrowNativeError("Cannot push parameters when there is no call in progress");
ds@621
   515
	}
ds@621
   516
ds@621
   517
	pContext->LocalToString(params[1], &value);
ds@621
   518
ds@621
   519
	err = s_pCallable->PushString(value);
ds@621
   520
ds@621
   521
	if (err)
ds@621
   522
	{
ds@621
   523
		s_pCallable->Cancel();
ds@621
   524
		ResetCall();
ds@621
   525
		return pContext->ThrowNativeErrorEx(err, NULL);
ds@621
   526
	}
ds@621
   527
ds@621
   528
	return 1;
ds@621
   529
}
ds@621
   530
ds@621
   531
static cell_t sm_CallPushStringEx(IPluginContext *pContext, const cell_t *params)
ds@621
   532
{
ds@621
   533
	int err;
ds@621
   534
	char *value;
ds@621
   535
ds@621
   536
	if (!s_CallStarted)
ds@621
   537
	{
ds@621
   538
		return pContext->ThrowNativeError("Cannot push parameters when there is no call in progress");
ds@621
   539
	}
ds@621
   540
ds@621
   541
	pContext->LocalToString(params[1], &value);
ds@621
   542
ds@621
   543
	err = s_pCallable->PushStringEx(value, params[2], params[3], params[4]);
ds@621
   544
ds@621
   545
	if (err)
ds@621
   546
	{
ds@621
   547
		s_pCallable->Cancel();
ds@621
   548
		ResetCall();
ds@621
   549
		return pContext->ThrowNativeErrorEx(err, NULL);
ds@621
   550
	}
ds@621
   551
ds@621
   552
	return 1;
ds@621
   553
}
ds@621
   554
ds@621
   555
static cell_t sm_CallFinish(IPluginContext *pContext, const cell_t *params)
ds@621
   556
{
faluco@629
   557
	int err = SP_ERROR_NOT_RUNNABLE;
ds@621
   558
	cell_t *result;
ds@621
   559
ds@621
   560
	if (!s_CallStarted)
ds@621
   561
	{
ds@621
   562
		return pContext->ThrowNativeError("Cannot finish call when there is no call in progress");
ds@621
   563
	}
ds@621
   564
ds@621
   565
	pContext->LocalToPhysAddr(params[1], &result);
ds@621
   566
ds@621
   567
	if (s_pFunction)
ds@621
   568
	{
ds@621
   569
		IPluginFunction *pFunction = s_pFunction;
ds@1082
   570
		ResetCall();
ds@621
   571
		err = pFunction->Execute(result);
ds@621
   572
	} else if (s_pForward) {
ds@621
   573
		IForward *pForward = s_pForward;
ds@1082
   574
		ResetCall();
ds@621
   575
		err = pForward->Execute(result, NULL);
ds@621
   576
	}
ds@621
   577
ds@621
   578
	return err;
ds@621
   579
}
ds@621
   580
ds@621
   581
static cell_t sm_CallCancel(IPluginContext *pContext, const cell_t *params)
ds@621
   582
{
ds@621
   583
	if (!s_CallStarted)
ds@621
   584
	{
ds@621
   585
		return pContext->ThrowNativeError("Cannot cancel call when there is no call in progress");
ds@621
   586
	}
ds@621
   587
ds@621
   588
	s_pCallable->Cancel();
ds@621
   589
	ResetCall();
ds@621
   590
ds@621
   591
	return 1;
ds@621
   592
}
ds@621
   593
kyle@4162
   594
struct SMFrameActionData
kyle@4162
   595
{
kyle@4162
   596
	SMFrameActionData(Handle_t handle, Handle_t ownerhandle, cell_t data) : handle(handle), ownerhandle(ownerhandle), data(data)
kyle@4162
   597
	{
kyle@4162
   598
	};
kyle@4162
   599
	Handle_t handle;
kyle@4162
   600
	Handle_t ownerhandle;
kyle@4162
   601
	cell_t data;
kyle@4162
   602
};
kyle@4162
   603
kyle@4162
   604
static void PawnFrameAction(void *pData)
kyle@4162
   605
{
kyle@4162
   606
	ke::AutoPtr<SMFrameActionData> frame(reinterpret_cast<SMFrameActionData *>(pData));
kyle@4162
   607
	IPlugin *pPlugin = pluginsys->PluginFromHandle(frame->ownerhandle, NULL);
kyle@4162
   608
	if (!pPlugin)
kyle@4162
   609
	{
kyle@4162
   610
		return;
kyle@4162
   611
	}
kyle@4162
   612
kyle@4162
   613
	IChangeableForward *pForward;
kyle@4162
   614
	HandleSecurity sec(pPlugin->GetIdentity(), g_pCoreIdent);
kyle@4162
   615
	if (handlesys->ReadHandle(frame->handle, g_PrivateFwdType, &sec, (void **)&pForward) != HandleError_None)
kyle@4162
   616
	{
kyle@4162
   617
		return;
kyle@4162
   618
	}
kyle@4162
   619
kyle@4162
   620
	pForward->PushCell(frame->data);
kyle@4162
   621
	pForward->Execute(NULL);
kyle@4162
   622
kyle@4162
   623
	handlesys->FreeHandle(frame->handle, &sec);
kyle@4162
   624
}
kyle@4162
   625
kyle@4162
   626
static cell_t sm_AddFrameAction(IPluginContext *pContext, const cell_t *params)
kyle@4162
   627
{
kyle@4162
   628
	IPlugin *pPlugin = pluginsys->FindPluginByContext(pContext->GetContext());
kyle@4162
   629
	IPluginFunction *pFunction = pPlugin->GetBaseContext()->GetFunctionById(params[1]);
kyle@4162
   630
	if (!pFunction)
kyle@4162
   631
	{
kyle@4162
   632
		return pContext->ThrowNativeError("Invalid function id (%X)", params[1]);
kyle@4162
   633
	}
kyle@4162
   634
kyle@4162
   635
	IChangeableForward *pForward = forwardsys->CreateForwardEx(NULL, ET_Ignore, 1, NULL, Param_Cell);
kyle@4162
   636
	IdentityToken_t *pIdentity = pContext->GetIdentity();
kyle@4162
   637
	Handle_t Handle = handlesys->CreateHandle(g_PrivateFwdType, pForward, pIdentity, g_pCoreIdent, NULL);
kyle@4162
   638
	if (Handle == BAD_HANDLE)
kyle@4162
   639
	{
kyle@4235
   640
		forwardsys->ReleaseForward(pForward);
kyle@4162
   641
		return 0;
kyle@4162
   642
	}
kyle@4162
   643
kyle@4162
   644
	pForward->AddFunction(pFunction);
kyle@4162
   645
kyle@4162
   646
	SMFrameActionData *pData = new SMFrameActionData(Handle, pPlugin->GetMyHandle(), params[2]);
kyle@4162
   647
	g_pSM->AddFrameAction(PawnFrameAction, pData);
kyle@4162
   648
	return 1;
kyle@4162
   649
}
kyle@4162
   650
ds@621
   651
REGISTER_NATIVES(functionNatives)
ds@621
   652
{
ds@621
   653
	{"GetFunctionByName",		sm_GetFunctionByName},
ds@621
   654
	{"CreateGlobalForward",		sm_CreateGlobalForward},
ds@621
   655
	{"CreateForward",			sm_CreateForward},
ds@621
   656
	{"GetForwardFunctionCount",	sm_GetForwardFunctionCount},
ds@621
   657
	{"AddToForward",			sm_AddToForward},
ds@621
   658
	{"RemoveFromForward",		sm_RemoveFromForward},
ds@621
   659
	{"RemoveAllFromForward",	sm_RemoveAllFromForward},
ds@621
   660
	{"Call_StartFunction",		sm_CallStartFunction},
ds@621
   661
	{"Call_StartForward",		sm_CallStartForward},
ds@621
   662
	{"Call_PushCell",			sm_CallPushCell},
ds@621
   663
	{"Call_PushCellRef",		sm_CallPushCellRef},
ds@621
   664
	{"Call_PushFloat",			sm_CallPushFloat},
ds@621
   665
	{"Call_PushFloatRef",		sm_CallPushFloatRef},
ds@621
   666
	{"Call_PushArray",			sm_CallPushArray},
ds@621
   667
	{"Call_PushArrayEx",		sm_CallPushArrayEx},
ds@621
   668
	{"Call_PushString",			sm_CallPushString},
ds@621
   669
	{"Call_PushStringEx",		sm_CallPushStringEx},
ds@621
   670
	{"Call_Finish",				sm_CallFinish},
ds@621
   671
	{"Call_Cancel",				sm_CallCancel},
kyle@4162
   672
	{"RequestFrame",			sm_AddFrameAction},
ds@621
   673
	{NULL,						NULL},
ds@621
   674
};