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;
82 static cell_t sm_GetFunctionByName(IPluginContext *pContext, const cell_t *params)
84 Handle_t hndl = static_cast<Handle_t>(params[1]);
92 pPlugin = g_PluginSys.FindPluginByContext(pContext->GetContext());
94 pPlugin = g_PluginSys.PluginFromHandle(hndl, &err);
98 return pContext->ThrowNativeError("Plugin handle %x is invalid (error %d)", hndl, err);
102 pContext->LocalToString(params[2], &name);
104 /* Get public function index */
105 if (pPlugin->GetBaseContext()->FindPublicByName(name, &idx) == SP_ERROR_NOT_FOUND)
107 /* Return INVALID_FUNCTION if not found */
111 /* Return function ID */
112 return PublicIndexToFuncId(idx);
115 static cell_t sm_CreateGlobalForward(IPluginContext *pContext, const cell_t *params)
117 cell_t count = params[0];
119 ParamType forwardParams[SP_MAX_EXEC_PARAMS];
121 if (count - 2 > SP_MAX_EXEC_PARAMS)
123 return pContext->ThrowNativeErrorEx(SP_ERROR_PARAMS_MAX, NULL);
126 pContext->LocalToString(params[1], &name);
129 for (int i = 3; i <= count; i++)
131 pContext->LocalToPhysAddr(params[i], &addr);
132 forwardParams[i - 3] = static_cast<ParamType>(*addr);
135 IForward *pForward = g_Forwards.CreateForward(name, static_cast<ExecType>(params[2]), count - 2, forwardParams);
137 return g_HandleSys.CreateHandle(g_GlobalFwdType, pForward, pContext->GetIdentity(), g_pCoreIdent, NULL);
140 static cell_t sm_CreateForward(IPluginContext *pContext, const cell_t *params)
142 cell_t count = params[0];
143 ParamType forwardParams[SP_MAX_EXEC_PARAMS];
145 if (count - 1 > SP_MAX_EXEC_PARAMS)
147 return pContext->ThrowNativeErrorEx(SP_ERROR_PARAMS_MAX, NULL);
151 for (int i = 2; i <= count; i++)
153 pContext->LocalToPhysAddr(params[i], &addr);
154 forwardParams[i - 2] = static_cast<ParamType>(*addr);
157 IChangeableForward *pForward = g_Forwards.CreateForwardEx(NULL, static_cast<ExecType>(params[1]), count - 1, forwardParams);
159 return g_HandleSys.CreateHandle(g_PrivateFwdType, pForward, pContext->GetIdentity(), g_pCoreIdent, NULL);
162 static cell_t sm_GetForwardFunctionCount(IPluginContext *pContext, const cell_t *params)
164 Handle_t hndl = static_cast<Handle_t>(params[1]);
168 if ((err=g_HandleSys.ReadHandle(hndl, g_GlobalFwdType, NULL, (void **)&pForward))
171 return pContext->ThrowNativeError("Invalid forward handle %x (error %d)", hndl, err);
174 return pForward->GetFunctionCount();
177 static cell_t sm_AddToForward(IPluginContext *pContext, const cell_t *params)
179 Handle_t fwdHandle = static_cast<Handle_t>(params[1]);
180 Handle_t plHandle = static_cast<Handle_t>(params[2]);
182 IChangeableForward *pForward;
185 if ((err=g_HandleSys.ReadHandle(fwdHandle, g_PrivateFwdType, NULL, (void **)&pForward))
188 return pContext->ThrowNativeError("Invalid private forward handle %x (error %d)", fwdHandle, err);
193 pPlugin = g_PluginSys.FindPluginByContext(pContext->GetContext());
195 pPlugin = g_PluginSys.PluginFromHandle(plHandle, &err);
199 return pContext->ThrowNativeError("Plugin handle %x is invalid (error %d)", plHandle, err);
203 IPluginFunction *pFunction = pPlugin->GetBaseContext()->GetFunctionById(params[3]);
207 return pContext->ThrowNativeError("Invalid function id (%X)", params[3]);
210 return pForward->AddFunction(pFunction);
213 static cell_t sm_RemoveFromForward(IPluginContext *pContext, const cell_t *params)
215 Handle_t fwdHandle = static_cast<Handle_t>(params[1]);
216 Handle_t plHandle = static_cast<Handle_t>(params[2]);
218 IChangeableForward *pForward;
221 if ((err=g_HandleSys.ReadHandle(fwdHandle, g_PrivateFwdType, NULL, (void **)&pForward))
224 return pContext->ThrowNativeError("Invalid private forward handle %x (error %d)", fwdHandle, err);
229 pPlugin = g_PluginSys.FindPluginByContext(pContext->GetContext());
231 pPlugin = g_PluginSys.PluginFromHandle(plHandle, &err);
235 return pContext->ThrowNativeError("Plugin handle %x is invalid (error %d)", plHandle, err);
239 IPluginFunction *pFunction = pPlugin->GetBaseContext()->GetFunctionById(params[3]);
243 return pContext->ThrowNativeError("Invalid function id (%X)", params[3]);
246 return pForward->RemoveFunction(pFunction);
249 static cell_t sm_RemoveAllFromForward(IPluginContext *pContext, const cell_t *params)
251 Handle_t fwdHandle = static_cast<Handle_t>(params[1]);
252 Handle_t plHandle = static_cast<Handle_t>(params[2]);
254 IChangeableForward *pForward;
257 if ((err=g_HandleSys.ReadHandle(fwdHandle, g_PrivateFwdType, NULL, (void **)&pForward))
260 return pContext->ThrowNativeError("Invalid private forward handle %x (error %d)", fwdHandle, err);
265 pPlugin = g_PluginSys.FindPluginByContext(pContext->GetContext());
267 pPlugin = g_PluginSys.PluginFromHandle(plHandle, &err);
271 return pContext->ThrowNativeError("Plugin handle %x is invalid (error %d)", plHandle, err);
275 return pForward->RemoveFunctionsOfPlugin(pPlugin);
278 static cell_t sm_CallStartFunction(IPluginContext *pContext, const cell_t *params)
286 return pContext->ThrowNativeError("Cannot start a call while one is already in progress");
289 hndl = static_cast<Handle_t>(params[1]);
293 pPlugin = g_PluginSys.FindPluginByContext(pContext->GetContext());
295 pPlugin = g_PluginSys.PluginFromHandle(hndl, &err);
299 return pContext->ThrowNativeError("Plugin handle %x is invalid (error %d)", hndl, err);
303 s_pFunction = pPlugin->GetBaseContext()->GetFunctionById(params[2]);
307 return pContext->ThrowNativeError("Invalid function id (%X)", params[2]);
310 s_pCallable = static_cast<ICallable *>(s_pFunction);
312 s_CallStarted = true;
317 static cell_t sm_CallStartForward(IPluginContext *pContext, const cell_t *params)
325 return pContext->ThrowNativeError("Cannot start a call while one is already in progress");
328 hndl = static_cast<Handle_t>(params[1]);
330 if ((err=g_HandleSys.ReadHandle(hndl, g_GlobalFwdType, NULL, (void **)&pForward))
333 return pContext->ThrowNativeError("Invalid forward handle %x (error %d)", hndl, err);
336 s_pForward = pForward;
338 s_pCallable = static_cast<ICallable *>(pForward);
340 s_CallStarted = true;
345 static cell_t sm_CallPushCell(IPluginContext *pContext, const cell_t *params)
351 return pContext->ThrowNativeError("Cannot push parameters when there is no call in progress");
354 err = s_pCallable->PushCell(params[1]);
358 s_pCallable->Cancel();
360 return pContext->ThrowNativeErrorEx(err, NULL);
366 static cell_t sm_CallPushCellRef(IPluginContext *pContext, const cell_t *params)
373 return pContext->ThrowNativeError("Cannot push parameters when there is no call in progress");
376 pContext->LocalToPhysAddr(params[1], &addr);
378 err = s_pCallable->PushCellByRef(addr);
382 s_pCallable->Cancel();
384 return pContext->ThrowNativeErrorEx(err, NULL);
390 static cell_t sm_CallPushFloat(IPluginContext *pContext, const cell_t *params)
396 return pContext->ThrowNativeError("Cannot push parameters when there is no call in progress");
399 err = s_pCallable->PushFloat(sp_ctof(params[1]));
403 s_pCallable->Cancel();
405 return pContext->ThrowNativeErrorEx(err, NULL);
411 static cell_t sm_CallPushFloatRef(IPluginContext *pContext, const cell_t *params)
418 return pContext->ThrowNativeError("Cannot push parameters when there is no call in progress");
421 pContext->LocalToPhysAddr(params[1], &addr);
423 err = s_pCallable->PushFloatByRef(reinterpret_cast<float *>(addr));
427 s_pCallable->Cancel();
429 return pContext->ThrowNativeErrorEx(err, NULL);
435 static cell_t sm_CallPushArray(IPluginContext *pContext, const cell_t *params)
442 return pContext->ThrowNativeError("Cannot push parameters when there is no call in progress");
445 pContext->LocalToPhysAddr(params[1], &addr);
447 err = s_pCallable->PushArray(addr, params[2]);
451 s_pCallable->Cancel();
453 return pContext->ThrowNativeErrorEx(err, NULL);
459 static cell_t sm_CallPushArrayEx(IPluginContext *pContext, const cell_t *params)
466 return pContext->ThrowNativeError("Cannot push parameters when there is no call in progress");
469 pContext->LocalToPhysAddr(params[1], &addr);
471 err = s_pCallable->PushArray(addr, params[2], params[3]);
475 s_pCallable->Cancel();
477 return pContext->ThrowNativeErrorEx(err, NULL);
483 static cell_t sm_CallPushString(IPluginContext *pContext, const cell_t *params)
490 return pContext->ThrowNativeError("Cannot push parameters when there is no call in progress");
493 pContext->LocalToString(params[1], &value);
495 err = s_pCallable->PushString(value);
499 s_pCallable->Cancel();
501 return pContext->ThrowNativeErrorEx(err, NULL);
507 static cell_t sm_CallPushStringEx(IPluginContext *pContext, const cell_t *params)
514 return pContext->ThrowNativeError("Cannot push parameters when there is no call in progress");
517 pContext->LocalToString(params[1], &value);
519 err = s_pCallable->PushStringEx(value, params[2], params[3], params[4]);
523 s_pCallable->Cancel();
525 return pContext->ThrowNativeErrorEx(err, NULL);
531 static cell_t sm_CallFinish(IPluginContext *pContext, const cell_t *params)
533 int err = SP_ERROR_NOT_RUNNABLE;
538 return pContext->ThrowNativeError("Cannot finish call when there is no call in progress");
541 pContext->LocalToPhysAddr(params[1], &result);
545 IPluginFunction *pFunction = s_pFunction;
547 err = pFunction->Execute(result);
548 } else if (s_pForward) {
549 IForward *pForward = s_pForward;
551 err = pForward->Execute(result, NULL);
557 static cell_t sm_CallCancel(IPluginContext *pContext, const cell_t *params)
561 return pContext->ThrowNativeError("Cannot cancel call when there is no call in progress");
564 s_pCallable->Cancel();
570 REGISTER_NATIVES(functionNatives)
572 {"GetFunctionByName", sm_GetFunctionByName},
573 {"CreateGlobalForward", sm_CreateGlobalForward},
574 {"CreateForward", sm_CreateForward},
575 {"GetForwardFunctionCount", sm_GetForwardFunctionCount},
576 {"AddToForward", sm_AddToForward},
577 {"RemoveFromForward", sm_RemoveFromForward},
578 {"RemoveAllFromForward", sm_RemoveAllFromForward},
579 {"Call_StartFunction", sm_CallStartFunction},
580 {"Call_StartForward", sm_CallStartForward},
581 {"Call_PushCell", sm_CallPushCell},
582 {"Call_PushCellRef", sm_CallPushCellRef},
583 {"Call_PushFloat", sm_CallPushFloat},
584 {"Call_PushFloatRef", sm_CallPushFloatRef},
585 {"Call_PushArray", sm_CallPushArray},
586 {"Call_PushArrayEx", sm_CallPushArrayEx},
587 {"Call_PushString", sm_CallPushString},
588 {"Call_PushStringEx", sm_CallPushStringEx},
589 {"Call_Finish", sm_CallFinish},
590 {"Call_Cancel", sm_CallCancel},