2 * ===============================================================
3 * SourceMod (C)2004-2007 AlliedModders LLC. All rights reserved.
4 * ===============================================================
6 * This file is not open source and may not be copied without explicit
7 * written permission of AlliedModders LLC. This file may not be redistributed
8 * in whole or significant part.
9 * For information, see LICENSE.txt or http://www.sourcemod.net/license.php
14 #include "sm_globals.h"
15 #include "PluginSys.h"
16 #include "ForwardSys.h"
17 #include "HandleSys.h"
19 HandleType_t g_GlobalFwdType = 0;
20 HandleType_t g_PrivateFwdType = 0;
22 static bool s_CallStarted = false;
23 static ICallable *s_pCallable = NULL;
24 static IPluginFunction *s_pFunction = NULL;
25 static IForward *s_pForward = NULL;
27 class ForwardNativeHelpers :
29 public IHandleTypeDispatch
32 void OnSourceModAllInitialized()
36 /* Set GlobalFwd handle access security */
37 g_HandleSys.InitAccessDefaults(NULL, &sec);
38 sec.access[HandleAccess_Read] = 0;
39 sec.access[HandleAccess_Clone] = HANDLE_RESTRICT_IDENTITY | HANDLE_RESTRICT_OWNER;
41 /* Create 'GlobalFwd' handle type */
42 g_GlobalFwdType = g_HandleSys.CreateType("GlobalFwd", this, 0, NULL, &sec, g_pCoreIdent, NULL);
44 /* Private forwards are cloneable */
45 sec.access[HandleAccess_Clone] = 0;
47 /* Create 'PrivateFwd' handle type */
48 g_PrivateFwdType = g_HandleSys.CreateType("PrivateFwd", this, g_GlobalFwdType, NULL, &sec, g_pCoreIdent, NULL);
51 void OnSourceModShutdown()
53 g_HandleSys.RemoveType(g_PrivateFwdType, g_pCoreIdent);
54 g_HandleSys.RemoveType(g_GlobalFwdType, g_pCoreIdent);
57 void OnHandleDestroy(HandleType_t type, void *object)
59 IForward *pForward = static_cast<IForward *>(object);
61 g_Forwards.ReleaseForward(pForward);
63 } g_ForwardNativeHelpers;
66 /* Turn a public index into a function ID */
67 inline funcid_t PublicIndexToFuncId(uint32_t idx)
69 return (idx << 1) | (1 << 0);
72 /* Reset global function/forward call variables */
73 inline void ResetCall()
75 s_CallStarted = false;
80 static cell_t sm_GetFunctionByName(IPluginContext *pContext, const cell_t *params)
82 Handle_t hndl = static_cast<Handle_t>(params[1]);
90 pPlugin = g_PluginSys.FindPluginByContext(pContext->GetContext());
92 pPlugin = g_PluginSys.PluginFromHandle(hndl, &err);
96 return pContext->ThrowNativeError("Plugin handle %x is invalid (error %d)", hndl, err);
100 pContext->LocalToString(params[2], &name);
102 /* Get public function index */
103 if (pPlugin->GetBaseContext()->FindPublicByName(name, &idx) == SP_ERROR_NOT_FOUND)
105 /* Return INVALID_FUNCTION if not found */
109 /* Return function ID */
110 return PublicIndexToFuncId(idx);
113 static cell_t sm_CreateGlobalForward(IPluginContext *pContext, const cell_t *params)
115 cell_t count = params[0];
117 ParamType forwardParams[SP_MAX_EXEC_PARAMS];
119 if (count - 2 > SP_MAX_EXEC_PARAMS)
121 return pContext->ThrowNativeErrorEx(SP_ERROR_PARAMS_MAX, NULL);
124 pContext->LocalToString(params[1], &name);
127 for (int i = 3; i <= count; i++)
129 pContext->LocalToPhysAddr(params[i], &addr);
130 forwardParams[i - 3] = static_cast<ParamType>(*addr);
133 IForward *pForward = g_Forwards.CreateForward(name, static_cast<ExecType>(params[2]), count - 2, forwardParams);
135 return g_HandleSys.CreateHandle(g_GlobalFwdType, pForward, pContext->GetIdentity(), g_pCoreIdent, NULL);
138 static cell_t sm_CreateForward(IPluginContext *pContext, const cell_t *params)
140 cell_t count = params[0];
141 ParamType forwardParams[SP_MAX_EXEC_PARAMS];
143 if (count - 1 > SP_MAX_EXEC_PARAMS)
145 return pContext->ThrowNativeErrorEx(SP_ERROR_PARAMS_MAX, NULL);
149 for (int i = 2; i <= count; i++)
151 pContext->LocalToPhysAddr(params[i], &addr);
152 forwardParams[i - 2] = static_cast<ParamType>(*addr);
155 IChangeableForward *pForward = g_Forwards.CreateForwardEx(NULL, static_cast<ExecType>(params[1]), count - 1, forwardParams);
157 return g_HandleSys.CreateHandle(g_PrivateFwdType, pForward, pContext->GetIdentity(), g_pCoreIdent, NULL);
160 static cell_t sm_GetForwardFunctionCount(IPluginContext *pContext, const cell_t *params)
162 Handle_t hndl = static_cast<Handle_t>(params[1]);
166 if ((err=g_HandleSys.ReadHandle(hndl, g_GlobalFwdType, NULL, (void **)&pForward))
169 return pContext->ThrowNativeError("Invalid forward handle %x (error %d)", hndl, err);
172 return pForward->GetFunctionCount();
175 static cell_t sm_AddToForward(IPluginContext *pContext, const cell_t *params)
177 Handle_t fwdHandle = static_cast<Handle_t>(params[1]);
178 Handle_t plHandle = static_cast<Handle_t>(params[2]);
180 IChangeableForward *pForward;
183 if ((err=g_HandleSys.ReadHandle(fwdHandle, g_PrivateFwdType, NULL, (void **)&pForward))
186 return pContext->ThrowNativeError("Invalid private forward handle %x (error %d)", fwdHandle, err);
191 pPlugin = g_PluginSys.FindPluginByContext(pContext->GetContext());
193 pPlugin = g_PluginSys.PluginFromHandle(plHandle, &err);
197 return pContext->ThrowNativeError("Plugin handle %x is invalid (error %d)", plHandle, err);
201 IPluginFunction *pFunction = pPlugin->GetBaseContext()->GetFunctionById(params[3]);
205 return pContext->ThrowNativeError("Invalid function id (%X)", params[3]);
208 return pForward->AddFunction(pFunction);
211 static cell_t sm_RemoveFromForward(IPluginContext *pContext, const cell_t *params)
213 Handle_t fwdHandle = static_cast<Handle_t>(params[1]);
214 Handle_t plHandle = static_cast<Handle_t>(params[2]);
216 IChangeableForward *pForward;
219 if ((err=g_HandleSys.ReadHandle(fwdHandle, g_PrivateFwdType, NULL, (void **)&pForward))
222 return pContext->ThrowNativeError("Invalid private forward handle %x (error %d)", fwdHandle, err);
227 pPlugin = g_PluginSys.FindPluginByContext(pContext->GetContext());
229 pPlugin = g_PluginSys.PluginFromHandle(plHandle, &err);
233 return pContext->ThrowNativeError("Plugin handle %x is invalid (error %d)", plHandle, err);
237 IPluginFunction *pFunction = pPlugin->GetBaseContext()->GetFunctionById(params[3]);
241 return pContext->ThrowNativeError("Invalid function id (%X)", params[3]);
244 return pForward->RemoveFunction(pFunction);
247 static cell_t sm_RemoveAllFromForward(IPluginContext *pContext, const cell_t *params)
249 Handle_t fwdHandle = static_cast<Handle_t>(params[1]);
250 Handle_t plHandle = static_cast<Handle_t>(params[2]);
252 IChangeableForward *pForward;
255 if ((err=g_HandleSys.ReadHandle(fwdHandle, g_PrivateFwdType, NULL, (void **)&pForward))
258 return pContext->ThrowNativeError("Invalid private forward handle %x (error %d)", fwdHandle, err);
263 pPlugin = g_PluginSys.FindPluginByContext(pContext->GetContext());
265 pPlugin = g_PluginSys.PluginFromHandle(plHandle, &err);
269 return pContext->ThrowNativeError("Plugin handle %x is invalid (error %d)", plHandle, err);
273 return pForward->RemoveFunctionsOfPlugin(pPlugin);
276 static cell_t sm_CallStartFunction(IPluginContext *pContext, const cell_t *params)
284 return pContext->ThrowNativeError("Cannot start a call while one is already in progress");
287 hndl = static_cast<Handle_t>(params[1]);
291 pPlugin = g_PluginSys.FindPluginByContext(pContext->GetContext());
293 pPlugin = g_PluginSys.PluginFromHandle(hndl, &err);
297 return pContext->ThrowNativeError("Plugin handle %x is invalid (error %d)", hndl, err);
301 s_pFunction = pPlugin->GetBaseContext()->GetFunctionById(params[2]);
305 return pContext->ThrowNativeError("Invalid function id (%X)", params[2]);
308 s_pCallable = static_cast<ICallable *>(s_pFunction);
310 s_CallStarted = true;
315 static cell_t sm_CallStartForward(IPluginContext *pContext, const cell_t *params)
323 return pContext->ThrowNativeError("Cannot start a call while one is already in progress");
326 hndl = static_cast<Handle_t>(params[1]);
328 if ((err=g_HandleSys.ReadHandle(hndl, g_GlobalFwdType, NULL, (void **)&pForward))
331 return pContext->ThrowNativeError("Invalid forward handle %x (error %d)", hndl, err);
334 s_pForward = pForward;
336 s_pCallable = static_cast<ICallable *>(pForward);
338 s_CallStarted = true;
343 static cell_t sm_CallPushCell(IPluginContext *pContext, const cell_t *params)
349 return pContext->ThrowNativeError("Cannot push parameters when there is no call in progress");
352 err = s_pCallable->PushCell(params[1]);
356 s_pCallable->Cancel();
358 return pContext->ThrowNativeErrorEx(err, NULL);
364 static cell_t sm_CallPushCellRef(IPluginContext *pContext, const cell_t *params)
371 return pContext->ThrowNativeError("Cannot push parameters when there is no call in progress");
374 pContext->LocalToPhysAddr(params[1], &addr);
376 err = s_pCallable->PushCellByRef(addr);
380 s_pCallable->Cancel();
382 return pContext->ThrowNativeErrorEx(err, NULL);
388 static cell_t sm_CallPushFloat(IPluginContext *pContext, const cell_t *params)
394 return pContext->ThrowNativeError("Cannot push parameters when there is no call in progress");
397 err = s_pCallable->PushFloat(sp_ctof(params[1]));
401 s_pCallable->Cancel();
403 return pContext->ThrowNativeErrorEx(err, NULL);
409 static cell_t sm_CallPushFloatRef(IPluginContext *pContext, const cell_t *params)
416 return pContext->ThrowNativeError("Cannot push parameters when there is no call in progress");
419 pContext->LocalToPhysAddr(params[1], &addr);
421 err = s_pCallable->PushFloatByRef(reinterpret_cast<float *>(addr));
425 s_pCallable->Cancel();
427 return pContext->ThrowNativeErrorEx(err, NULL);
433 static cell_t sm_CallPushArray(IPluginContext *pContext, const cell_t *params)
440 return pContext->ThrowNativeError("Cannot push parameters when there is no call in progress");
443 pContext->LocalToPhysAddr(params[1], &addr);
445 err = s_pCallable->PushArray(addr, params[2]);
449 s_pCallable->Cancel();
451 return pContext->ThrowNativeErrorEx(err, NULL);
457 static cell_t sm_CallPushArrayEx(IPluginContext *pContext, const cell_t *params)
464 return pContext->ThrowNativeError("Cannot push parameters when there is no call in progress");
467 pContext->LocalToPhysAddr(params[1], &addr);
469 err = s_pCallable->PushArray(addr, params[2], params[3]);
473 s_pCallable->Cancel();
475 return pContext->ThrowNativeErrorEx(err, NULL);
481 static cell_t sm_CallPushString(IPluginContext *pContext, const cell_t *params)
488 return pContext->ThrowNativeError("Cannot push parameters when there is no call in progress");
491 pContext->LocalToString(params[1], &value);
493 err = s_pCallable->PushString(value);
497 s_pCallable->Cancel();
499 return pContext->ThrowNativeErrorEx(err, NULL);
505 static cell_t sm_CallPushStringEx(IPluginContext *pContext, const cell_t *params)
512 return pContext->ThrowNativeError("Cannot push parameters when there is no call in progress");
515 pContext->LocalToString(params[1], &value);
517 err = s_pCallable->PushStringEx(value, params[2], params[3], params[4]);
521 s_pCallable->Cancel();
523 return pContext->ThrowNativeErrorEx(err, NULL);
529 static cell_t sm_CallFinish(IPluginContext *pContext, const cell_t *params)
531 int err = SP_ERROR_NOT_RUNNABLE;
536 return pContext->ThrowNativeError("Cannot finish call when there is no call in progress");
539 pContext->LocalToPhysAddr(params[1], &result);
543 IPluginFunction *pFunction = s_pFunction;
544 err = pFunction->Execute(result);
545 } else if (s_pForward) {
546 IForward *pForward = s_pForward;
547 err = pForward->Execute(result, NULL);
555 static cell_t sm_CallCancel(IPluginContext *pContext, const cell_t *params)
559 return pContext->ThrowNativeError("Cannot cancel call when there is no call in progress");
562 s_pCallable->Cancel();
568 REGISTER_NATIVES(functionNatives)
570 {"GetFunctionByName", sm_GetFunctionByName},
571 {"CreateGlobalForward", sm_CreateGlobalForward},
572 {"CreateForward", sm_CreateForward},
573 {"GetForwardFunctionCount", sm_GetForwardFunctionCount},
574 {"AddToForward", sm_AddToForward},
575 {"RemoveFromForward", sm_RemoveFromForward},
576 {"RemoveAllFromForward", sm_RemoveAllFromForward},
577 {"Call_StartFunction", sm_CallStartFunction},
578 {"Call_StartForward", sm_CallStartForward},
579 {"Call_PushCell", sm_CallPushCell},
580 {"Call_PushCellRef", sm_CallPushCellRef},
581 {"Call_PushFloat", sm_CallPushFloat},
582 {"Call_PushFloatRef", sm_CallPushFloatRef},
583 {"Call_PushArray", sm_CallPushArray},
584 {"Call_PushArrayEx", sm_CallPushArrayEx},
585 {"Call_PushString", sm_CallPushString},
586 {"Call_PushStringEx", sm_CallPushStringEx},
587 {"Call_Finish", sm_CallFinish},
588 {"Call_Cancel", sm_CallCancel},