core/smn_functions.cpp
author Scott Ehlert <ds@alliedmods.net>
Fri Jun 01 06:33:54 2007 +0000 (2007-06-01)
changeset 873 a2136b3be10d
parent 653 ef60870067ff
child 1082 bb923439d570
permissions -rw-r--r--
- Added natives: PrintToChat() and PrintCenterText()
- Bit of reorganization with moving some natives from sourcemod.inc to halflife.inc
     1 /**
     2  * vim: set ts=4 :
     3  * ===============================================================
     4  * SourceMod (C)2004-2007 AlliedModders LLC.  All rights reserved.
     5  * ===============================================================
     6  *
     7  * This file is not open source and may not be copied without explicit
     8  * written permission of AlliedModders LLC.  This file may not be redistributed 
     9  * in whole or significant part.
    10  * For information, see LICENSE.txt or http://www.sourcemod.net/license.php
    11  *
    12  * Version: $Id$
    13  */
    14 
    15 #include "sm_globals.h"
    16 #include "PluginSys.h"
    17 #include "ForwardSys.h"
    18 #include "HandleSys.h"
    19 
    20 HandleType_t g_GlobalFwdType = 0;
    21 HandleType_t g_PrivateFwdType = 0;
    22 
    23 static bool s_CallStarted = false;
    24 static ICallable *s_pCallable = NULL;
    25 static IPluginFunction *s_pFunction = NULL;
    26 static IForward *s_pForward = NULL;
    27 
    28 class ForwardNativeHelpers : 
    29 	public SMGlobalClass,
    30 	public IHandleTypeDispatch
    31 {
    32 public:
    33 	void OnSourceModAllInitialized()
    34 	{
    35 		HandleAccess sec;
    36 
    37 		/* Set GlobalFwd handle access security */
    38 		g_HandleSys.InitAccessDefaults(NULL, &sec);
    39 		sec.access[HandleAccess_Read] = 0;
    40 		sec.access[HandleAccess_Clone] = HANDLE_RESTRICT_IDENTITY | HANDLE_RESTRICT_OWNER;
    41 
    42 		/* Create 'GlobalFwd' handle type */
    43 		g_GlobalFwdType = g_HandleSys.CreateType("GlobalFwd", this, 0, NULL, &sec, g_pCoreIdent, NULL);
    44 
    45 		/* Private forwards are cloneable */
    46 		sec.access[HandleAccess_Clone] = 0;
    47 
    48 		/* Create 'PrivateFwd' handle type */
    49 		g_PrivateFwdType = g_HandleSys.CreateType("PrivateFwd", this, g_GlobalFwdType, NULL, &sec, g_pCoreIdent, NULL);
    50 	}
    51 
    52 	void OnSourceModShutdown()
    53 	{
    54 		g_HandleSys.RemoveType(g_PrivateFwdType, g_pCoreIdent);
    55 		g_HandleSys.RemoveType(g_GlobalFwdType, g_pCoreIdent);
    56 	}
    57 
    58 	void OnHandleDestroy(HandleType_t type, void *object)
    59 	{
    60 		IForward *pForward = static_cast<IForward *>(object);
    61 
    62 		g_Forwards.ReleaseForward(pForward);
    63 	}
    64 } g_ForwardNativeHelpers;
    65 
    66 
    67 /* Turn a public index into a function ID */
    68 inline funcid_t PublicIndexToFuncId(uint32_t idx)
    69 {
    70 	return (idx << 1) | (1 << 0);
    71 }
    72 
    73 /* Reset global function/forward call variables */
    74 inline void ResetCall()
    75 {
    76 	s_CallStarted = false;
    77 	s_pFunction = NULL;
    78 	s_pCallable = NULL;
    79 }
    80 
    81 static cell_t sm_GetFunctionByName(IPluginContext *pContext, const cell_t *params)
    82 {
    83 	Handle_t hndl = static_cast<Handle_t>(params[1]);
    84 	HandleError err;
    85 	char *name;
    86 	uint32_t idx;
    87 	IPlugin *pPlugin;
    88 
    89 	if (hndl == 0)
    90 	{
    91 		pPlugin = g_PluginSys.FindPluginByContext(pContext->GetContext());
    92 	} else {
    93 		pPlugin = g_PluginSys.PluginFromHandle(hndl, &err);
    94 
    95 		if (!pPlugin)
    96 		{
    97 			return pContext->ThrowNativeError("Plugin handle %x is invalid (error %d)", hndl, err);
    98 		}
    99 	}
   100 
   101 	pContext->LocalToString(params[2], &name);
   102 
   103 	/* Get public function index */
   104 	if (pPlugin->GetBaseContext()->FindPublicByName(name, &idx) == SP_ERROR_NOT_FOUND)
   105 	{
   106 		/* Return INVALID_FUNCTION if not found */
   107 		return -1;
   108 	}
   109 
   110 	/* Return function ID */
   111 	return PublicIndexToFuncId(idx);
   112 }
   113 
   114 static cell_t sm_CreateGlobalForward(IPluginContext *pContext, const cell_t *params)
   115 {
   116 	cell_t count = params[0];
   117 	char *name;
   118 	ParamType forwardParams[SP_MAX_EXEC_PARAMS];
   119 
   120 	if (count - 2 > SP_MAX_EXEC_PARAMS)
   121 	{
   122 		return pContext->ThrowNativeErrorEx(SP_ERROR_PARAMS_MAX, NULL);
   123 	}
   124 	
   125 	pContext->LocalToString(params[1], &name);
   126 
   127 	cell_t *addr;
   128 	for (int i = 3; i <= count; i++)
   129 	{
   130 		pContext->LocalToPhysAddr(params[i], &addr);
   131 		forwardParams[i - 3] = static_cast<ParamType>(*addr);
   132 	}
   133 
   134 	IForward *pForward = g_Forwards.CreateForward(name, static_cast<ExecType>(params[2]), count - 2, forwardParams);
   135 
   136 	return g_HandleSys.CreateHandle(g_GlobalFwdType, pForward, pContext->GetIdentity(), g_pCoreIdent, NULL);
   137 }
   138 
   139 static cell_t sm_CreateForward(IPluginContext *pContext, const cell_t *params)
   140 {
   141 	cell_t count = params[0];
   142 	ParamType forwardParams[SP_MAX_EXEC_PARAMS];
   143 
   144 	if (count - 1 > SP_MAX_EXEC_PARAMS)
   145 	{
   146 		return pContext->ThrowNativeErrorEx(SP_ERROR_PARAMS_MAX, NULL);
   147 	}
   148 
   149 	cell_t *addr;
   150 	for (int i = 2; i <= count; i++)
   151 	{
   152 		pContext->LocalToPhysAddr(params[i], &addr);
   153 		forwardParams[i - 2] = static_cast<ParamType>(*addr);
   154 	}
   155 
   156 	IChangeableForward *pForward = g_Forwards.CreateForwardEx(NULL, static_cast<ExecType>(params[1]), count - 1, forwardParams);
   157 
   158 	return g_HandleSys.CreateHandle(g_PrivateFwdType, pForward, pContext->GetIdentity(), g_pCoreIdent, NULL);
   159 }
   160 
   161 static cell_t sm_GetForwardFunctionCount(IPluginContext *pContext, const cell_t *params)
   162 {
   163 	Handle_t hndl = static_cast<Handle_t>(params[1]);
   164 	HandleError err;
   165 	IForward *pForward;
   166 
   167 	if ((err=g_HandleSys.ReadHandle(hndl, g_GlobalFwdType, NULL, (void **)&pForward))
   168 		!= HandleError_None)
   169 	{
   170 		return pContext->ThrowNativeError("Invalid forward handle %x (error %d)", hndl, err);
   171 	}
   172 
   173 	return pForward->GetFunctionCount();
   174 }
   175 
   176 static cell_t sm_AddToForward(IPluginContext *pContext, const cell_t *params)
   177 {
   178 	Handle_t fwdHandle = static_cast<Handle_t>(params[1]);
   179 	Handle_t plHandle = static_cast<Handle_t>(params[2]);
   180 	HandleError err;
   181 	IChangeableForward *pForward;
   182 	IPlugin *pPlugin;
   183 
   184 	if ((err=g_HandleSys.ReadHandle(fwdHandle, g_PrivateFwdType, NULL, (void **)&pForward))
   185 		!= HandleError_None)
   186 	{
   187 		return pContext->ThrowNativeError("Invalid private forward handle %x (error %d)", fwdHandle, err);
   188 	}
   189 
   190 	if (plHandle == 0)
   191 	{
   192 		pPlugin = g_PluginSys.FindPluginByContext(pContext->GetContext());
   193 	} else {
   194 		pPlugin = g_PluginSys.PluginFromHandle(plHandle, &err);
   195 
   196 		if (!pPlugin)
   197 		{
   198 			return pContext->ThrowNativeError("Plugin handle %x is invalid (error %d)", plHandle, err);
   199 		}
   200 	}
   201 
   202 	IPluginFunction *pFunction = pPlugin->GetBaseContext()->GetFunctionById(params[3]);
   203 
   204 	if (!pFunction)
   205 	{
   206 		return pContext->ThrowNativeError("Invalid function id (%X)", params[3]);
   207 	}
   208 
   209 	return pForward->AddFunction(pFunction);
   210 }
   211 
   212 static cell_t sm_RemoveFromForward(IPluginContext *pContext, const cell_t *params)
   213 {
   214 	Handle_t fwdHandle = static_cast<Handle_t>(params[1]);
   215 	Handle_t plHandle = static_cast<Handle_t>(params[2]);
   216 	HandleError err;
   217 	IChangeableForward *pForward;
   218 	IPlugin *pPlugin;
   219 
   220 	if ((err=g_HandleSys.ReadHandle(fwdHandle, g_PrivateFwdType, NULL, (void **)&pForward))
   221 		!= HandleError_None)
   222 	{
   223 		return pContext->ThrowNativeError("Invalid private forward handle %x (error %d)", fwdHandle, err);
   224 	}
   225 
   226 	if (plHandle == 0)
   227 	{
   228 		pPlugin = g_PluginSys.FindPluginByContext(pContext->GetContext());
   229 	} else {
   230 		pPlugin = g_PluginSys.PluginFromHandle(plHandle, &err);
   231 
   232 		if (!pPlugin)
   233 		{
   234 			return pContext->ThrowNativeError("Plugin handle %x is invalid (error %d)", plHandle, err);
   235 		}
   236 	}
   237 
   238 	IPluginFunction *pFunction = pPlugin->GetBaseContext()->GetFunctionById(params[3]);
   239 
   240 	if (!pFunction)
   241 	{
   242 		return pContext->ThrowNativeError("Invalid function id (%X)", params[3]);
   243 	}
   244 
   245 	return pForward->RemoveFunction(pFunction);
   246 }
   247 
   248 static cell_t sm_RemoveAllFromForward(IPluginContext *pContext, const cell_t *params)
   249 {
   250 	Handle_t fwdHandle = static_cast<Handle_t>(params[1]);
   251 	Handle_t plHandle = static_cast<Handle_t>(params[2]);
   252 	HandleError err;
   253 	IChangeableForward *pForward;
   254 	IPlugin *pPlugin;
   255 
   256 	if ((err=g_HandleSys.ReadHandle(fwdHandle, g_PrivateFwdType, NULL, (void **)&pForward))
   257 		!= HandleError_None)
   258 	{
   259 		return pContext->ThrowNativeError("Invalid private forward handle %x (error %d)", fwdHandle, err);
   260 	}
   261 
   262 	if (plHandle == 0)
   263 	{
   264 		pPlugin = g_PluginSys.FindPluginByContext(pContext->GetContext());
   265 	} else {
   266 		pPlugin = g_PluginSys.PluginFromHandle(plHandle, &err);
   267 
   268 		if (!pPlugin)
   269 		{
   270 			return pContext->ThrowNativeError("Plugin handle %x is invalid (error %d)", plHandle, err);
   271 		}
   272 	}
   273 
   274 	return pForward->RemoveFunctionsOfPlugin(pPlugin);
   275 }
   276 
   277 static cell_t sm_CallStartFunction(IPluginContext *pContext, const cell_t *params)
   278 {
   279 	Handle_t hndl;
   280 	HandleError err;
   281 	IPlugin *pPlugin;
   282 
   283 	if (s_CallStarted)
   284 	{
   285 		return pContext->ThrowNativeError("Cannot start a call while one is already in progress");
   286 	}
   287 
   288 	hndl = static_cast<Handle_t>(params[1]);
   289 
   290 	if (hndl == 0)
   291 	{
   292 		pPlugin = g_PluginSys.FindPluginByContext(pContext->GetContext());
   293 	} else {
   294 		pPlugin = g_PluginSys.PluginFromHandle(hndl, &err);
   295 
   296 		if (!pPlugin)
   297 		{
   298 			return pContext->ThrowNativeError("Plugin handle %x is invalid (error %d)", hndl, err);
   299 		}
   300 	}
   301 
   302 	s_pFunction = pPlugin->GetBaseContext()->GetFunctionById(params[2]);
   303 
   304 	if (!s_pFunction)
   305 	{
   306 		return pContext->ThrowNativeError("Invalid function id (%X)", params[2]);
   307 	}
   308 
   309 	s_pCallable = static_cast<ICallable *>(s_pFunction);
   310 
   311 	s_CallStarted = true;
   312 
   313 	return 1;
   314 }
   315 
   316 static cell_t sm_CallStartForward(IPluginContext *pContext, const cell_t *params)
   317 {
   318 	Handle_t hndl;
   319 	HandleError err;
   320 	IForward *pForward;
   321 
   322 	if (s_CallStarted)
   323 	{
   324 		return pContext->ThrowNativeError("Cannot start a call while one is already in progress");
   325 	}
   326 
   327 	hndl = static_cast<Handle_t>(params[1]);
   328 
   329 	if ((err=g_HandleSys.ReadHandle(hndl, g_GlobalFwdType, NULL, (void **)&pForward))
   330 		!= HandleError_None)
   331 	{
   332 		return pContext->ThrowNativeError("Invalid forward handle %x (error %d)", hndl, err);
   333 	}
   334 
   335 	s_pForward = pForward;
   336 
   337 	s_pCallable = static_cast<ICallable *>(pForward);
   338 
   339 	s_CallStarted = true;
   340 
   341 	return 1;
   342 }
   343 
   344 static cell_t sm_CallPushCell(IPluginContext *pContext, const cell_t *params)
   345 {
   346 	int err;
   347 
   348 	if (!s_CallStarted)
   349 	{
   350 		return pContext->ThrowNativeError("Cannot push parameters when there is no call in progress");
   351 	}
   352 
   353 	err = s_pCallable->PushCell(params[1]);
   354 
   355 	if (err)
   356 	{
   357 		s_pCallable->Cancel();
   358 		ResetCall();
   359 		return pContext->ThrowNativeErrorEx(err, NULL);
   360 	}
   361 
   362 	return 1;
   363 }
   364 
   365 static cell_t sm_CallPushCellRef(IPluginContext *pContext, const cell_t *params)
   366 {
   367 	int err;
   368 	cell_t *addr;
   369 
   370 	if (!s_CallStarted)
   371 	{
   372 		return pContext->ThrowNativeError("Cannot push parameters when there is no call in progress");
   373 	}
   374 
   375 	pContext->LocalToPhysAddr(params[1], &addr);
   376 
   377 	err = s_pCallable->PushCellByRef(addr);
   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_CallPushFloat(IPluginContext *pContext, const cell_t *params)
   390 {
   391 	int err;
   392 
   393 	if (!s_CallStarted)
   394 	{
   395 		return pContext->ThrowNativeError("Cannot push parameters when there is no call in progress");
   396 	}
   397 
   398 	err = s_pCallable->PushFloat(sp_ctof(params[1]));
   399 
   400 	if (err)
   401 	{
   402 		s_pCallable->Cancel();
   403 		ResetCall();
   404 		return pContext->ThrowNativeErrorEx(err, NULL);
   405 	}
   406 
   407 	return 1;
   408 }
   409 
   410 static cell_t sm_CallPushFloatRef(IPluginContext *pContext, const cell_t *params)
   411 {
   412 	int err;
   413 	cell_t *addr;
   414 
   415 	if (!s_CallStarted)
   416 	{
   417 		return pContext->ThrowNativeError("Cannot push parameters when there is no call in progress");
   418 	}
   419 
   420 	pContext->LocalToPhysAddr(params[1], &addr);
   421 
   422 	err = s_pCallable->PushFloatByRef(reinterpret_cast<float *>(addr));
   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_CallPushArray(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->PushArray(addr, params[2]);
   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_CallPushArrayEx(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], params[3]);
   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_CallPushString(IPluginContext *pContext, const cell_t *params)
   483 {
   484 	int err;
   485 	char *value;
   486 
   487 	if (!s_CallStarted)
   488 	{
   489 		return pContext->ThrowNativeError("Cannot push parameters when there is no call in progress");
   490 	}
   491 
   492 	pContext->LocalToString(params[1], &value);
   493 
   494 	err = s_pCallable->PushString(value);
   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_CallPushStringEx(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->PushStringEx(value, params[2], params[3], params[4]);
   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_CallFinish(IPluginContext *pContext, const cell_t *params)
   531 {
   532 	int err = SP_ERROR_NOT_RUNNABLE;
   533 	cell_t *result;
   534 
   535 	if (!s_CallStarted)
   536 	{
   537 		return pContext->ThrowNativeError("Cannot finish call when there is no call in progress");
   538 	}
   539 
   540 	pContext->LocalToPhysAddr(params[1], &result);
   541 
   542 	if (s_pFunction)
   543 	{
   544 		IPluginFunction *pFunction = s_pFunction;
   545 		err = pFunction->Execute(result);
   546 	} else if (s_pForward) {
   547 		IForward *pForward = s_pForward;
   548 		err = pForward->Execute(result, NULL);
   549 	}
   550 
   551 	ResetCall();
   552 
   553 	return err;
   554 }
   555 
   556 static cell_t sm_CallCancel(IPluginContext *pContext, const cell_t *params)
   557 {
   558 	if (!s_CallStarted)
   559 	{
   560 		return pContext->ThrowNativeError("Cannot cancel call when there is no call in progress");
   561 	}
   562 
   563 	s_pCallable->Cancel();
   564 	ResetCall();
   565 
   566 	return 1;
   567 }
   568 
   569 REGISTER_NATIVES(functionNatives)
   570 {
   571 	{"GetFunctionByName",		sm_GetFunctionByName},
   572 	{"CreateGlobalForward",		sm_CreateGlobalForward},
   573 	{"CreateForward",			sm_CreateForward},
   574 	{"GetForwardFunctionCount",	sm_GetForwardFunctionCount},
   575 	{"AddToForward",			sm_AddToForward},
   576 	{"RemoveFromForward",		sm_RemoveFromForward},
   577 	{"RemoveAllFromForward",	sm_RemoveAllFromForward},
   578 	{"Call_StartFunction",		sm_CallStartFunction},
   579 	{"Call_StartForward",		sm_CallStartForward},
   580 	{"Call_PushCell",			sm_CallPushCell},
   581 	{"Call_PushCellRef",		sm_CallPushCellRef},
   582 	{"Call_PushFloat",			sm_CallPushFloat},
   583 	{"Call_PushFloatRef",		sm_CallPushFloatRef},
   584 	{"Call_PushArray",			sm_CallPushArray},
   585 	{"Call_PushArrayEx",		sm_CallPushArrayEx},
   586 	{"Call_PushString",			sm_CallPushString},
   587 	{"Call_PushStringEx",		sm_CallPushStringEx},
   588 	{"Call_Finish",				sm_CallFinish},
   589 	{"Call_Cancel",				sm_CallCancel},
   590 	{NULL,						NULL},
   591 };