core/smn_functions.cpp
author David Anderson <dvander@alliedmods.net>
Wed Feb 27 13:49:57 2008 +0000 (2008-02-27)
changeset 1900 457329ae65bd
parent 1766 d30c252089be
child 1947 e73a1e51833b
permissions -rw-r--r--
fixed amb1483 - GetEntPropString() rejected FIELD_MODELNAME/FIELD_SOUNDNAME
     1 /**
     2  * vim: set ts=4 :
     3  * =============================================================================
     4  * SourceMod
     5  * Copyright (C) 2004-2007 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 "sm_globals.h"
    33 #include "PluginSys.h"
    34 #include "ForwardSys.h"
    35 #include "HandleSys.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 		g_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 = g_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 = g_HandleSys.CreateType("PrivateFwd", this, g_GlobalFwdType, NULL, &sec, g_pCoreIdent, NULL);
    67 	}
    68 
    69 	void OnSourceModShutdown()
    70 	{
    71 		g_HandleSys.RemoveType(g_PrivateFwdType, g_pCoreIdent);
    72 		g_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 		g_Forwards.ReleaseForward(pForward);
    80 	}
    81 
    82 	bool GetHandleApproxSize(HandleType_t type, void *object, unsigned int *pSize)
    83 	{
    84 		*pSize = sizeof(CForward) + (((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 = g_PluginSys.FindPluginByContext(pContext->GetContext());
   116 	} else {
   117 		pPlugin = g_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 = g_Forwards.CreateForward(name, static_cast<ExecType>(params[2]), count - 2, forwardParams);
   159 
   160 	return g_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 = g_Forwards.CreateForwardEx(NULL, static_cast<ExecType>(params[1]), count - 1, forwardParams);
   181 
   182 	return g_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 
   191 	if ((err=g_HandleSys.ReadHandle(hndl, g_GlobalFwdType, NULL, (void **)&pForward))
   192 		!= HandleError_None)
   193 	{
   194 		return pContext->ThrowNativeError("Invalid forward handle %x (error %d)", hndl, err);
   195 	}
   196 
   197 	return pForward->GetFunctionCount();
   198 }
   199 
   200 static cell_t sm_AddToForward(IPluginContext *pContext, const cell_t *params)
   201 {
   202 	Handle_t fwdHandle = static_cast<Handle_t>(params[1]);
   203 	Handle_t plHandle = static_cast<Handle_t>(params[2]);
   204 	HandleError err;
   205 	IChangeableForward *pForward;
   206 	IPlugin *pPlugin;
   207 
   208 	if ((err=g_HandleSys.ReadHandle(fwdHandle, g_PrivateFwdType, NULL, (void **)&pForward))
   209 		!= HandleError_None)
   210 	{
   211 		return pContext->ThrowNativeError("Invalid private forward handle %x (error %d)", fwdHandle, err);
   212 	}
   213 
   214 	if (plHandle == 0)
   215 	{
   216 		pPlugin = g_PluginSys.FindPluginByContext(pContext->GetContext());
   217 	} else {
   218 		pPlugin = g_PluginSys.PluginFromHandle(plHandle, &err);
   219 
   220 		if (!pPlugin)
   221 		{
   222 			return pContext->ThrowNativeError("Plugin handle %x is invalid (error %d)", plHandle, err);
   223 		}
   224 	}
   225 
   226 	IPluginFunction *pFunction = pPlugin->GetBaseContext()->GetFunctionById(params[3]);
   227 
   228 	if (!pFunction)
   229 	{
   230 		return pContext->ThrowNativeError("Invalid function id (%X)", params[3]);
   231 	}
   232 
   233 	return pForward->AddFunction(pFunction);
   234 }
   235 
   236 static cell_t sm_RemoveFromForward(IPluginContext *pContext, const cell_t *params)
   237 {
   238 	Handle_t fwdHandle = static_cast<Handle_t>(params[1]);
   239 	Handle_t plHandle = static_cast<Handle_t>(params[2]);
   240 	HandleError err;
   241 	IChangeableForward *pForward;
   242 	IPlugin *pPlugin;
   243 
   244 	if ((err=g_HandleSys.ReadHandle(fwdHandle, g_PrivateFwdType, NULL, (void **)&pForward))
   245 		!= HandleError_None)
   246 	{
   247 		return pContext->ThrowNativeError("Invalid private forward handle %x (error %d)", fwdHandle, err);
   248 	}
   249 
   250 	if (plHandle == 0)
   251 	{
   252 		pPlugin = g_PluginSys.FindPluginByContext(pContext->GetContext());
   253 	} else {
   254 		pPlugin = g_PluginSys.PluginFromHandle(plHandle, &err);
   255 
   256 		if (!pPlugin)
   257 		{
   258 			return pContext->ThrowNativeError("Plugin handle %x is invalid (error %d)", plHandle, err);
   259 		}
   260 	}
   261 
   262 	IPluginFunction *pFunction = pPlugin->GetBaseContext()->GetFunctionById(params[3]);
   263 
   264 	if (!pFunction)
   265 	{
   266 		return pContext->ThrowNativeError("Invalid function id (%X)", params[3]);
   267 	}
   268 
   269 	return pForward->RemoveFunction(pFunction);
   270 }
   271 
   272 static cell_t sm_RemoveAllFromForward(IPluginContext *pContext, const cell_t *params)
   273 {
   274 	Handle_t fwdHandle = static_cast<Handle_t>(params[1]);
   275 	Handle_t plHandle = static_cast<Handle_t>(params[2]);
   276 	HandleError err;
   277 	IChangeableForward *pForward;
   278 	IPlugin *pPlugin;
   279 
   280 	if ((err=g_HandleSys.ReadHandle(fwdHandle, g_PrivateFwdType, NULL, (void **)&pForward))
   281 		!= HandleError_None)
   282 	{
   283 		return pContext->ThrowNativeError("Invalid private forward handle %x (error %d)", fwdHandle, err);
   284 	}
   285 
   286 	if (plHandle == 0)
   287 	{
   288 		pPlugin = g_PluginSys.FindPluginByContext(pContext->GetContext());
   289 	} else {
   290 		pPlugin = g_PluginSys.PluginFromHandle(plHandle, &err);
   291 
   292 		if (!pPlugin)
   293 		{
   294 			return pContext->ThrowNativeError("Plugin handle %x is invalid (error %d)", plHandle, err);
   295 		}
   296 	}
   297 
   298 	return pForward->RemoveFunctionsOfPlugin(pPlugin);
   299 }
   300 
   301 static cell_t sm_CallStartFunction(IPluginContext *pContext, const cell_t *params)
   302 {
   303 	Handle_t hndl;
   304 	HandleError err;
   305 	IPlugin *pPlugin;
   306 
   307 	if (s_CallStarted)
   308 	{
   309 		return pContext->ThrowNativeError("Cannot start a call while one is already in progress");
   310 	}
   311 
   312 	hndl = static_cast<Handle_t>(params[1]);
   313 
   314 	if (hndl == 0)
   315 	{
   316 		pPlugin = g_PluginSys.FindPluginByContext(pContext->GetContext());
   317 	} else {
   318 		pPlugin = g_PluginSys.PluginFromHandle(hndl, &err);
   319 
   320 		if (!pPlugin)
   321 		{
   322 			return pContext->ThrowNativeError("Plugin handle %x is invalid (error %d)", hndl, err);
   323 		}
   324 	}
   325 
   326 	s_pFunction = pPlugin->GetBaseContext()->GetFunctionById(params[2]);
   327 
   328 	if (!s_pFunction)
   329 	{
   330 		return pContext->ThrowNativeError("Invalid function id (%X)", params[2]);
   331 	}
   332 
   333 	s_pCallable = static_cast<ICallable *>(s_pFunction);
   334 
   335 	s_CallStarted = true;
   336 
   337 	return 1;
   338 }
   339 
   340 static cell_t sm_CallStartForward(IPluginContext *pContext, const cell_t *params)
   341 {
   342 	Handle_t hndl;
   343 	HandleError err;
   344 	IForward *pForward;
   345 
   346 	if (s_CallStarted)
   347 	{
   348 		return pContext->ThrowNativeError("Cannot start a call while one is already in progress");
   349 	}
   350 
   351 	hndl = static_cast<Handle_t>(params[1]);
   352 
   353 	if ((err=g_HandleSys.ReadHandle(hndl, g_GlobalFwdType, NULL, (void **)&pForward))
   354 		!= HandleError_None)
   355 	{
   356 		return pContext->ThrowNativeError("Invalid forward handle %x (error %d)", hndl, err);
   357 	}
   358 
   359 	s_pForward = pForward;
   360 
   361 	s_pCallable = static_cast<ICallable *>(pForward);
   362 
   363 	s_CallStarted = true;
   364 
   365 	return 1;
   366 }
   367 
   368 static cell_t sm_CallPushCell(IPluginContext *pContext, const cell_t *params)
   369 {
   370 	int err;
   371 
   372 	if (!s_CallStarted)
   373 	{
   374 		return pContext->ThrowNativeError("Cannot push parameters when there is no call in progress");
   375 	}
   376 
   377 	err = s_pCallable->PushCell(params[1]);
   378 
   379 	if (err)
   380 	{
   381 		s_pCallable->Cancel();
   382 		ResetCall();
   383 		return pContext->ThrowNativeErrorEx(err, NULL);
   384 	}
   385 
   386 	return 1;
   387 }
   388 
   389 static cell_t sm_CallPushCellRef(IPluginContext *pContext, const cell_t *params)
   390 {
   391 	int err;
   392 	cell_t *addr;
   393 
   394 	if (!s_CallStarted)
   395 	{
   396 		return pContext->ThrowNativeError("Cannot push parameters when there is no call in progress");
   397 	}
   398 
   399 	pContext->LocalToPhysAddr(params[1], &addr);
   400 
   401 	err = s_pCallable->PushCellByRef(addr);
   402 
   403 	if (err)
   404 	{
   405 		s_pCallable->Cancel();
   406 		ResetCall();
   407 		return pContext->ThrowNativeErrorEx(err, NULL);
   408 	}
   409 
   410 	return 1;
   411 }
   412 
   413 static cell_t sm_CallPushFloat(IPluginContext *pContext, const cell_t *params)
   414 {
   415 	int err;
   416 
   417 	if (!s_CallStarted)
   418 	{
   419 		return pContext->ThrowNativeError("Cannot push parameters when there is no call in progress");
   420 	}
   421 
   422 	err = s_pCallable->PushFloat(sp_ctof(params[1]));
   423 
   424 	if (err)
   425 	{
   426 		s_pCallable->Cancel();
   427 		ResetCall();
   428 		return pContext->ThrowNativeErrorEx(err, NULL);
   429 	}
   430 
   431 	return 1;
   432 }
   433 
   434 static cell_t sm_CallPushFloatRef(IPluginContext *pContext, const cell_t *params)
   435 {
   436 	int err;
   437 	cell_t *addr;
   438 
   439 	if (!s_CallStarted)
   440 	{
   441 		return pContext->ThrowNativeError("Cannot push parameters when there is no call in progress");
   442 	}
   443 
   444 	pContext->LocalToPhysAddr(params[1], &addr);
   445 
   446 	err = s_pCallable->PushFloatByRef(reinterpret_cast<float *>(addr));
   447 
   448 	if (err)
   449 	{
   450 		s_pCallable->Cancel();
   451 		ResetCall();
   452 		return pContext->ThrowNativeErrorEx(err, NULL);
   453 	}
   454 
   455 	return 1;
   456 }
   457 
   458 static cell_t sm_CallPushArray(IPluginContext *pContext, const cell_t *params)
   459 {
   460 	int err;
   461 	cell_t *addr;
   462 
   463 	if (!s_CallStarted)
   464 	{
   465 		return pContext->ThrowNativeError("Cannot push parameters when there is no call in progress");
   466 	}
   467 
   468 	pContext->LocalToPhysAddr(params[1], &addr);
   469 
   470 	err = s_pCallable->PushArray(addr, params[2]);
   471 
   472 	if (err)
   473 	{
   474 		s_pCallable->Cancel();
   475 		ResetCall();
   476 		return pContext->ThrowNativeErrorEx(err, NULL);
   477 	}
   478 
   479 	return 1;
   480 }
   481 
   482 static cell_t sm_CallPushArrayEx(IPluginContext *pContext, const cell_t *params)
   483 {
   484 	int err;
   485 	cell_t *addr;
   486 
   487 	if (!s_CallStarted)
   488 	{
   489 		return pContext->ThrowNativeError("Cannot push parameters when there is no call in progress");
   490 	}
   491 
   492 	pContext->LocalToPhysAddr(params[1], &addr);
   493 
   494 	err = s_pCallable->PushArray(addr, params[2], params[3]);
   495 
   496 	if (err)
   497 	{
   498 		s_pCallable->Cancel();
   499 		ResetCall();
   500 		return pContext->ThrowNativeErrorEx(err, NULL);
   501 	}
   502 
   503 	return 1;
   504 }
   505 
   506 static cell_t sm_CallPushString(IPluginContext *pContext, const cell_t *params)
   507 {
   508 	int err;
   509 	char *value;
   510 
   511 	if (!s_CallStarted)
   512 	{
   513 		return pContext->ThrowNativeError("Cannot push parameters when there is no call in progress");
   514 	}
   515 
   516 	pContext->LocalToString(params[1], &value);
   517 
   518 	err = s_pCallable->PushString(value);
   519 
   520 	if (err)
   521 	{
   522 		s_pCallable->Cancel();
   523 		ResetCall();
   524 		return pContext->ThrowNativeErrorEx(err, NULL);
   525 	}
   526 
   527 	return 1;
   528 }
   529 
   530 static cell_t sm_CallPushStringEx(IPluginContext *pContext, const cell_t *params)
   531 {
   532 	int err;
   533 	char *value;
   534 
   535 	if (!s_CallStarted)
   536 	{
   537 		return pContext->ThrowNativeError("Cannot push parameters when there is no call in progress");
   538 	}
   539 
   540 	pContext->LocalToString(params[1], &value);
   541 
   542 	err = s_pCallable->PushStringEx(value, params[2], params[3], params[4]);
   543 
   544 	if (err)
   545 	{
   546 		s_pCallable->Cancel();
   547 		ResetCall();
   548 		return pContext->ThrowNativeErrorEx(err, NULL);
   549 	}
   550 
   551 	return 1;
   552 }
   553 
   554 static cell_t sm_CallFinish(IPluginContext *pContext, const cell_t *params)
   555 {
   556 	int err = SP_ERROR_NOT_RUNNABLE;
   557 	cell_t *result;
   558 
   559 	if (!s_CallStarted)
   560 	{
   561 		return pContext->ThrowNativeError("Cannot finish call when there is no call in progress");
   562 	}
   563 
   564 	pContext->LocalToPhysAddr(params[1], &result);
   565 
   566 	if (s_pFunction)
   567 	{
   568 		IPluginFunction *pFunction = s_pFunction;
   569 		ResetCall();
   570 		err = pFunction->Execute(result);
   571 	} else if (s_pForward) {
   572 		IForward *pForward = s_pForward;
   573 		ResetCall();
   574 		err = pForward->Execute(result, NULL);
   575 	}
   576 
   577 	return err;
   578 }
   579 
   580 static cell_t sm_CallCancel(IPluginContext *pContext, const cell_t *params)
   581 {
   582 	if (!s_CallStarted)
   583 	{
   584 		return pContext->ThrowNativeError("Cannot cancel call when there is no call in progress");
   585 	}
   586 
   587 	s_pCallable->Cancel();
   588 	ResetCall();
   589 
   590 	return 1;
   591 }
   592 
   593 REGISTER_NATIVES(functionNatives)
   594 {
   595 	{"GetFunctionByName",		sm_GetFunctionByName},
   596 	{"CreateGlobalForward",		sm_CreateGlobalForward},
   597 	{"CreateForward",			sm_CreateForward},
   598 	{"GetForwardFunctionCount",	sm_GetForwardFunctionCount},
   599 	{"AddToForward",			sm_AddToForward},
   600 	{"RemoveFromForward",		sm_RemoveFromForward},
   601 	{"RemoveAllFromForward",	sm_RemoveAllFromForward},
   602 	{"Call_StartFunction",		sm_CallStartFunction},
   603 	{"Call_StartForward",		sm_CallStartForward},
   604 	{"Call_PushCell",			sm_CallPushCell},
   605 	{"Call_PushCellRef",		sm_CallPushCellRef},
   606 	{"Call_PushFloat",			sm_CallPushFloat},
   607 	{"Call_PushFloatRef",		sm_CallPushFloatRef},
   608 	{"Call_PushArray",			sm_CallPushArray},
   609 	{"Call_PushArrayEx",		sm_CallPushArrayEx},
   610 	{"Call_PushString",			sm_CallPushString},
   611 	{"Call_PushStringEx",		sm_CallPushStringEx},
   612 	{"Call_Finish",				sm_CallFinish},
   613 	{"Call_Cancel",				sm_CallCancel},
   614 	{NULL,						NULL},
   615 };