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