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