3 * ===============================================================
4 * SourceMod (C)2004-2007 AlliedModders LLC. All rights reserved.
5 * ===============================================================
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
15 #include "sm_globals.h"
16 #include "PluginSys.h"
17 #include "ForwardSys.h"
18 #include "HandleSys.h"
20 HandleType_t g_GlobalFwdType = 0;
21 HandleType_t g_PrivateFwdType = 0;
23 static bool s_CallStarted = false;
24 static ICallable *s_pCallable = NULL;
25 static IPluginFunction *s_pFunction = NULL;
26 static IForward *s_pForward = NULL;
28 class ForwardNativeHelpers :
30 public IHandleTypeDispatch
33 void OnSourceModAllInitialized()
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;
42 /* Create 'GlobalFwd' handle type */
43 g_GlobalFwdType = g_HandleSys.CreateType("GlobalFwd", this, 0, NULL, &sec, g_pCoreIdent, NULL);
45 /* Private forwards are cloneable */
46 sec.access[HandleAccess_Clone] = 0;
48 /* Create 'PrivateFwd' handle type */
49 g_PrivateFwdType = g_HandleSys.CreateType("PrivateFwd", this, g_GlobalFwdType, NULL, &sec, g_pCoreIdent, NULL);
52 void OnSourceModShutdown()
54 g_HandleSys.RemoveType(g_PrivateFwdType, g_pCoreIdent);
55 g_HandleSys.RemoveType(g_GlobalFwdType, g_pCoreIdent);
58 void OnHandleDestroy(HandleType_t type, void *object)
60 IForward *pForward = static_cast<IForward *>(object);
62 g_Forwards.ReleaseForward(pForward);
64 } g_ForwardNativeHelpers;
67 /* Turn a public index into a function ID */
68 inline funcid_t PublicIndexToFuncId(uint32_t idx)
70 return (idx << 1) | (1 << 0);
73 /* Reset global function/forward call variables */
74 inline void ResetCall()
76 s_CallStarted = false;
81 static cell_t sm_GetFunctionByName(IPluginContext *pContext, const cell_t *params)
83 Handle_t hndl = static_cast<Handle_t>(params[1]);
91 pPlugin = g_PluginSys.FindPluginByContext(pContext->GetContext());
93 pPlugin = g_PluginSys.PluginFromHandle(hndl, &err);
97 return pContext->ThrowNativeError("Plugin handle %x is invalid (error %d)", hndl, err);
101 pContext->LocalToString(params[2], &name);
103 /* Get public function index */
104 if (pPlugin->GetBaseContext()->FindPublicByName(name, &idx) == SP_ERROR_NOT_FOUND)
106 /* Return INVALID_FUNCTION if not found */
110 /* Return function ID */
111 return PublicIndexToFuncId(idx);
114 static cell_t sm_CreateGlobalForward(IPluginContext *pContext, const cell_t *params)
116 cell_t count = params[0];
118 ParamType forwardParams[SP_MAX_EXEC_PARAMS];
120 if (count - 2 > SP_MAX_EXEC_PARAMS)
122 return pContext->ThrowNativeErrorEx(SP_ERROR_PARAMS_MAX, NULL);
125 pContext->LocalToString(params[1], &name);
128 for (int i = 3; i <= count; i++)
130 pContext->LocalToPhysAddr(params[i], &addr);
131 forwardParams[i - 3] = static_cast<ParamType>(*addr);
134 IForward *pForward = g_Forwards.CreateForward(name, static_cast<ExecType>(params[2]), count - 2, forwardParams);
136 return g_HandleSys.CreateHandle(g_GlobalFwdType, pForward, pContext->GetIdentity(), g_pCoreIdent, NULL);
139 static cell_t sm_CreateForward(IPluginContext *pContext, const cell_t *params)
141 cell_t count = params[0];
142 ParamType forwardParams[SP_MAX_EXEC_PARAMS];
144 if (count - 1 > SP_MAX_EXEC_PARAMS)
146 return pContext->ThrowNativeErrorEx(SP_ERROR_PARAMS_MAX, NULL);
150 for (int i = 2; i <= count; i++)
152 pContext->LocalToPhysAddr(params[i], &addr);
153 forwardParams[i - 2] = static_cast<ParamType>(*addr);
156 IChangeableForward *pForward = g_Forwards.CreateForwardEx(NULL, static_cast<ExecType>(params[1]), count - 1, forwardParams);
158 return g_HandleSys.CreateHandle(g_PrivateFwdType, pForward, pContext->GetIdentity(), g_pCoreIdent, NULL);
161 static cell_t sm_GetForwardFunctionCount(IPluginContext *pContext, const cell_t *params)
163 Handle_t hndl = static_cast<Handle_t>(params[1]);
167 if ((err=g_HandleSys.ReadHandle(hndl, g_GlobalFwdType, NULL, (void **)&pForward))
170 return pContext->ThrowNativeError("Invalid forward handle %x (error %d)", hndl, err);
173 return pForward->GetFunctionCount();
176 static cell_t sm_AddToForward(IPluginContext *pContext, const cell_t *params)
178 Handle_t fwdHandle = static_cast<Handle_t>(params[1]);
179 Handle_t plHandle = static_cast<Handle_t>(params[2]);
181 IChangeableForward *pForward;
184 if ((err=g_HandleSys.ReadHandle(fwdHandle, g_PrivateFwdType, NULL, (void **)&pForward))
187 return pContext->ThrowNativeError("Invalid private forward handle %x (error %d)", fwdHandle, err);
192 pPlugin = g_PluginSys.FindPluginByContext(pContext->GetContext());
194 pPlugin = g_PluginSys.PluginFromHandle(plHandle, &err);
198 return pContext->ThrowNativeError("Plugin handle %x is invalid (error %d)", plHandle, err);
202 IPluginFunction *pFunction = pPlugin->GetBaseContext()->GetFunctionById(params[3]);
206 return pContext->ThrowNativeError("Invalid function id (%X)", params[3]);
209 return pForward->AddFunction(pFunction);
212 static cell_t sm_RemoveFromForward(IPluginContext *pContext, const cell_t *params)
214 Handle_t fwdHandle = static_cast<Handle_t>(params[1]);
215 Handle_t plHandle = static_cast<Handle_t>(params[2]);
217 IChangeableForward *pForward;
220 if ((err=g_HandleSys.ReadHandle(fwdHandle, g_PrivateFwdType, NULL, (void **)&pForward))
223 return pContext->ThrowNativeError("Invalid private forward handle %x (error %d)", fwdHandle, err);
228 pPlugin = g_PluginSys.FindPluginByContext(pContext->GetContext());
230 pPlugin = g_PluginSys.PluginFromHandle(plHandle, &err);
234 return pContext->ThrowNativeError("Plugin handle %x is invalid (error %d)", plHandle, err);
238 IPluginFunction *pFunction = pPlugin->GetBaseContext()->GetFunctionById(params[3]);
242 return pContext->ThrowNativeError("Invalid function id (%X)", params[3]);
245 return pForward->RemoveFunction(pFunction);
248 static cell_t sm_RemoveAllFromForward(IPluginContext *pContext, const cell_t *params)
250 Handle_t fwdHandle = static_cast<Handle_t>(params[1]);
251 Handle_t plHandle = static_cast<Handle_t>(params[2]);
253 IChangeableForward *pForward;
256 if ((err=g_HandleSys.ReadHandle(fwdHandle, g_PrivateFwdType, NULL, (void **)&pForward))
259 return pContext->ThrowNativeError("Invalid private forward handle %x (error %d)", fwdHandle, err);
264 pPlugin = g_PluginSys.FindPluginByContext(pContext->GetContext());
266 pPlugin = g_PluginSys.PluginFromHandle(plHandle, &err);
270 return pContext->ThrowNativeError("Plugin handle %x is invalid (error %d)", plHandle, err);
274 return pForward->RemoveFunctionsOfPlugin(pPlugin);
277 static cell_t sm_CallStartFunction(IPluginContext *pContext, const cell_t *params)
285 return pContext->ThrowNativeError("Cannot start a call while one is already in progress");
288 hndl = static_cast<Handle_t>(params[1]);
292 pPlugin = g_PluginSys.FindPluginByContext(pContext->GetContext());
294 pPlugin = g_PluginSys.PluginFromHandle(hndl, &err);
298 return pContext->ThrowNativeError("Plugin handle %x is invalid (error %d)", hndl, err);
302 s_pFunction = pPlugin->GetBaseContext()->GetFunctionById(params[2]);
306 return pContext->ThrowNativeError("Invalid function id (%X)", params[2]);
309 s_pCallable = static_cast<ICallable *>(s_pFunction);
311 s_CallStarted = true;
316 static cell_t sm_CallStartForward(IPluginContext *pContext, const cell_t *params)
324 return pContext->ThrowNativeError("Cannot start a call while one is already in progress");
327 hndl = static_cast<Handle_t>(params[1]);
329 if ((err=g_HandleSys.ReadHandle(hndl, g_GlobalFwdType, NULL, (void **)&pForward))
332 return pContext->ThrowNativeError("Invalid forward handle %x (error %d)", hndl, err);
335 s_pForward = pForward;
337 s_pCallable = static_cast<ICallable *>(pForward);
339 s_CallStarted = true;
344 static cell_t sm_CallPushCell(IPluginContext *pContext, const cell_t *params)
350 return pContext->ThrowNativeError("Cannot push parameters when there is no call in progress");
353 err = s_pCallable->PushCell(params[1]);
357 s_pCallable->Cancel();
359 return pContext->ThrowNativeErrorEx(err, NULL);
365 static cell_t sm_CallPushCellRef(IPluginContext *pContext, const cell_t *params)
372 return pContext->ThrowNativeError("Cannot push parameters when there is no call in progress");
375 pContext->LocalToPhysAddr(params[1], &addr);
377 err = s_pCallable->PushCellByRef(addr);
381 s_pCallable->Cancel();
383 return pContext->ThrowNativeErrorEx(err, NULL);
389 static cell_t sm_CallPushFloat(IPluginContext *pContext, const cell_t *params)
395 return pContext->ThrowNativeError("Cannot push parameters when there is no call in progress");
398 err = s_pCallable->PushFloat(sp_ctof(params[1]));
402 s_pCallable->Cancel();
404 return pContext->ThrowNativeErrorEx(err, NULL);
410 static cell_t sm_CallPushFloatRef(IPluginContext *pContext, const cell_t *params)
417 return pContext->ThrowNativeError("Cannot push parameters when there is no call in progress");
420 pContext->LocalToPhysAddr(params[1], &addr);
422 err = s_pCallable->PushFloatByRef(reinterpret_cast<float *>(addr));
426 s_pCallable->Cancel();
428 return pContext->ThrowNativeErrorEx(err, NULL);
434 static cell_t sm_CallPushArray(IPluginContext *pContext, const cell_t *params)
441 return pContext->ThrowNativeError("Cannot push parameters when there is no call in progress");
444 pContext->LocalToPhysAddr(params[1], &addr);
446 err = s_pCallable->PushArray(addr, params[2]);
450 s_pCallable->Cancel();
452 return pContext->ThrowNativeErrorEx(err, NULL);
458 static cell_t sm_CallPushArrayEx(IPluginContext *pContext, const cell_t *params)
465 return pContext->ThrowNativeError("Cannot push parameters when there is no call in progress");
468 pContext->LocalToPhysAddr(params[1], &addr);
470 err = s_pCallable->PushArray(addr, params[2], params[3]);
474 s_pCallable->Cancel();
476 return pContext->ThrowNativeErrorEx(err, NULL);
482 static cell_t sm_CallPushString(IPluginContext *pContext, const cell_t *params)
489 return pContext->ThrowNativeError("Cannot push parameters when there is no call in progress");
492 pContext->LocalToString(params[1], &value);
494 err = s_pCallable->PushString(value);
498 s_pCallable->Cancel();
500 return pContext->ThrowNativeErrorEx(err, NULL);
506 static cell_t sm_CallPushStringEx(IPluginContext *pContext, const cell_t *params)
513 return pContext->ThrowNativeError("Cannot push parameters when there is no call in progress");
516 pContext->LocalToString(params[1], &value);
518 err = s_pCallable->PushStringEx(value, params[2], params[3], params[4]);
522 s_pCallable->Cancel();
524 return pContext->ThrowNativeErrorEx(err, NULL);
530 static cell_t sm_CallFinish(IPluginContext *pContext, const cell_t *params)
532 int err = SP_ERROR_NOT_RUNNABLE;
537 return pContext->ThrowNativeError("Cannot finish call when there is no call in progress");
540 pContext->LocalToPhysAddr(params[1], &result);
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);
556 static cell_t sm_CallCancel(IPluginContext *pContext, const cell_t *params)
560 return pContext->ThrowNativeError("Cannot cancel call when there is no call in progress");
563 s_pCallable->Cancel();
569 REGISTER_NATIVES(functionNatives)
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},