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
17 #include "ForwardSys.h"
18 #include "PluginSys.h"
21 CForwardManager g_Forwards;
24 * Gensis turns to its source, reduction occurs stepwise although the essence is all one.
25 * End of line. FTL system check.
27 * :TODO: WHAT NEEDS TO BE TESTED IN THIS BEAST
29 * - Pushing no varargs
32 // :TODO: IMPORTANT!!! The result pointer arg in the execute function maybe invalid if the forward fails
33 // so later evaluation of this result may cause problems on higher levels of abstraction. DOCUMENT OR FIX ALL FORWARDS!
35 void CForwardManager::OnSourceModAllInitialized()
37 g_PluginSys.AddPluginsListener(this);
38 g_ShareSys.AddInterface(NULL, this);
41 void CForwardManager::OnSourceModShutdown()
43 g_PluginSys.RemovePluginsListener(this);
46 IForward *CForwardManager::CreateForward(const char *name, ExecType et, unsigned int num_params, ParamType *types, ...)
52 fwd = CForward::CreateForward(name, et, num_params, types, ap);
58 m_managed.push_back(fwd);
64 IChangeableForward *CForwardManager::CreateForwardEx(const char *name, ExecType et, int num_params, ParamType *types, ...)
70 fwd = CForward::CreateForward(name, et, num_params, types, ap);
76 m_unmanaged.push_back(fwd);
82 void CForwardManager::OnPluginLoaded(IPlugin *plugin)
84 /* Attach any globally managed forwards */
85 List<CForward *>::iterator iter;
88 for (iter=m_managed.begin(); iter!=m_managed.end(); iter++)
91 IPluginFunction *pFunc = plugin->GetBaseContext()->GetFunctionByName(fwd->GetForwardName());
94 fwd->AddFunction(pFunc);
99 void CForwardManager::OnPluginUnloaded(IPlugin *plugin)
101 List<CForward *>::iterator iter;
104 for (iter=m_managed.begin(); iter!=m_managed.end(); iter++)
107 fwd->RemoveFunctionsOfPlugin(plugin);
110 for (iter=m_unmanaged.begin(); iter!=m_unmanaged.end(); iter++)
113 fwd->RemoveFunctionsOfPlugin(plugin);
117 IForward *CForwardManager::FindForward(const char *name, IChangeableForward **ifchng)
119 List<CForward *>::iterator iter;
122 for (iter=m_managed.begin(); iter!=m_managed.end(); iter++)
125 if (strcmp(fwd->GetForwardName(), name) == 0)
135 for (iter=m_unmanaged.begin(); iter!=m_unmanaged.end(); iter++)
138 if (strcmp(fwd->GetForwardName(), name) == 0)
156 void CForwardManager::ReleaseForward(IForward *forward)
158 ForwardFree(static_cast<CForward *>(forward));
161 void CForwardManager::ForwardFree(CForward *fwd)
163 m_FreeForwards.push(fwd);
164 m_managed.remove(fwd);
167 CForward *CForwardManager::ForwardMake()
170 if (m_FreeForwards.empty())
174 fwd = m_FreeForwards.front();
175 m_FreeForwards.pop();
180 /*************************************
181 * ACTUAL FORWARD API IMPLEMENTATION *
182 *************************************/
184 CForward *CForward::CreateForward(const char *name, ExecType et, unsigned int num_params, ParamType *types, va_list ap)
186 ParamType _types[SP_MAX_EXEC_PARAMS];
188 if (num_params > SP_MAX_EXEC_PARAMS)
193 if (types == NULL && num_params)
195 for (unsigned int i=0; i<num_params; i++)
197 _types[i] = (ParamType)va_arg(ap, int);
198 if (_types[i] == Param_VarArgs && (i != num_params - 1))
205 for (unsigned int i=0; i<num_params; i++)
207 if (types[i] == Param_VarArgs && (i != num_params - 1))
214 /* First parameter can never be varargs */
215 if (types[0] == Param_VarArgs)
220 CForward *pForward = g_Forwards.ForwardMake();
221 pForward->m_curparam = 0;
222 pForward->m_ExecType = et;
223 snprintf(pForward->m_name, FORWARDS_NAME_MAX, "%s", name ? name : "");
225 for (unsigned int i=0; i<num_params; i++)
227 pForward->m_types[i] = types[i];
230 if (num_params && types[num_params-1] == Param_VarArgs)
232 pForward->m_varargs = num_params--;
234 pForward->m_varargs = false;
237 pForward->m_numparams = num_params;
238 pForward->m_errstate = SP_ERROR_NONE;
243 int CForward::Execute(cell_t *result, IForwardFilter *filter)
247 int err = m_errstate;
254 filter->OnExecuteBegin();
257 FuncIter iter = m_functions.begin();
258 IPluginFunction *func;
259 cell_t cur_result = 0;
260 cell_t high_result = 0;
262 unsigned int failed=0, success=0;
263 unsigned int num_params = m_curparam;
264 FwdParamInfo temp_info[SP_MAX_EXEC_PARAMS];
268 /* Save local, reset */
269 memcpy(temp_info, m_params, sizeof(m_params));
272 for (iter=m_functions.begin(); iter!=m_functions.end(); iter++)
276 if (!func->GetParentContext()->IsRunnable())
281 for (unsigned int i=0; i<num_params; i++)
283 param = &temp_info[i];
284 if (i >= m_numparams || m_types[i] == Param_Any)
286 type = param->pushedas;
290 if ((i >= m_numparams) || (type & SP_PARAMFLAG_BYREF))
292 /* If we're byref or we're vararg, we always push everything by ref.
293 * Even if they're byval, we must push them byref.
295 if (type == Param_String)
297 func->PushStringEx((char *)param->byref.orig_addr, param->byref.cells, param->byref.sz_flags, param->byref.flags);
298 } else if (type == Param_Float || type == Param_Cell) {
299 func->PushCellByRef(¶m->val, 0);
301 func->PushArray(param->byref.orig_addr, param->byref.cells, NULL, param->byref.flags);
302 assert(type == Param_Array || type == Param_FloatByRef || type == Param_CellByRef);
305 /* If we're not byref or not vararg, our job is a bit easier. */
306 assert(type == Param_Cell || type == Param_Float);
307 func->PushCell(param->val);
311 /* Call the function and deal with the return value. */
312 if ((err=func->Execute(&cur_result)) != SP_ERROR_NONE)
314 bool handled = false;
317 handled = filter->OnErrorReport(this, func, err);
321 /* :TODO: invoke global error reporting here */
330 if (cur_result > high_result)
332 high_result = cur_result;
338 if (cur_result > high_result)
340 high_result = cur_result;
341 if ((ResultType)high_result == Pl_Stop)
343 iter = m_functions.end();
353 if (filter->OnFunctionReturn(this, func, &cur_result) == Pl_Stop)
355 iter = m_functions.end();
368 if (m_ExecType == ET_Event || m_ExecType == ET_Hook)
370 cur_result = high_result;
371 } else if (m_ExecType == ET_Ignore) {
375 *result = cur_result;
379 filter->OnExecuteEnd(&cur_result, success, failed);
382 return SP_ERROR_NONE;
385 int CForward::PushCell(cell_t cell)
387 if (m_curparam < m_numparams)
389 if (m_types[m_curparam] == Param_Any)
391 m_params[m_curparam].pushedas = Param_Cell;
392 } else if (m_types[m_curparam] != Param_Cell) {
393 return SetError(SP_ERROR_PARAM);
396 if (!m_varargs || m_numparams > SP_MAX_EXEC_PARAMS)
398 return SetError(SP_ERROR_PARAMS_MAX);
400 m_params[m_curparam].pushedas = Param_Cell;
403 m_params[m_curparam++].val = cell;
405 return SP_ERROR_NONE;
408 int CForward::PushFloat(float number)
410 if (m_curparam < m_numparams)
412 if (m_types[m_curparam] == Param_Any)
414 m_params[m_curparam].pushedas = Param_Float;
415 } else if (m_types[m_curparam] != Param_Float) {
416 return SetError(SP_ERROR_PARAM);
419 if (!m_varargs || m_numparams > SP_MAX_EXEC_PARAMS)
421 return SetError(SP_ERROR_PARAMS_MAX);
423 m_params[m_curparam].pushedas = Param_Float;
426 m_params[m_curparam++].val = *(cell_t *)&number;
428 return SP_ERROR_NONE;
431 int CForward::PushCellByRef(cell_t *cell, int flags)
433 if (m_curparam < m_numparams)
435 if (m_types[m_curparam] == Param_Any)
437 m_params[m_curparam].pushedas = Param_CellByRef;
438 } else if (m_types[m_curparam] != Param_CellByRef) {
439 return SetError(SP_ERROR_PARAM);
442 if (!m_varargs || m_numparams > SP_MAX_EXEC_PARAMS)
444 return SetError(SP_ERROR_PARAMS_MAX);
446 m_params[m_curparam].pushedas = Param_CellByRef;
449 _Int_PushArray(cell, 1, flags);
452 return SP_ERROR_NONE;
455 int CForward::PushFloatByRef(float *num, int flags)
457 if (m_curparam < m_numparams)
459 if (m_types[m_curparam] == Param_Any)
461 m_params[m_curparam].pushedas = Param_FloatByRef;
462 } else if (m_types[m_curparam] != Param_FloatByRef) {
463 return SetError(SP_ERROR_PARAM);
466 if (!m_varargs || m_numparams > SP_MAX_EXEC_PARAMS)
468 return SetError(SP_ERROR_PARAMS_MAX);
470 m_params[m_curparam].pushedas = Param_FloatByRef;
473 _Int_PushArray((cell_t *)num, 1, flags);
476 return SP_ERROR_NONE;
479 void CForward::_Int_PushArray(cell_t *inarray, unsigned int cells, int flags)
481 m_params[m_curparam].byref.cells = cells;
482 m_params[m_curparam].byref.flags = flags;
483 m_params[m_curparam].byref.orig_addr = inarray;
486 int CForward::PushArray(cell_t *inarray, unsigned int cells, cell_t **phys_addr, int flags)
488 /* We don't allow this here */
491 return SetError(SP_ERROR_PARAM);
494 if (m_curparam < m_numparams)
496 if (m_types[m_curparam] == Param_Any)
498 m_params[m_curparam].pushedas = Param_Array;
499 } else if (m_types[m_curparam] != Param_Array) {
500 return SetError(SP_ERROR_PARAM);
503 if (!m_varargs || m_curparam > SP_MAX_EXEC_PARAMS)
505 return SetError(SP_ERROR_PARAMS_MAX);
507 m_params[m_curparam].pushedas = Param_Array;
515 _Int_PushArray(inarray, cells, flags);
519 return SP_ERROR_NONE;
522 void CForward::_Int_PushString(cell_t *inarray, unsigned int cells, int sz_flags, int cp_flags)
524 m_params[m_curparam].byref.cells = cells; /* Notice this contains the char count not cell count */
525 m_params[m_curparam].byref.flags = cp_flags;
526 m_params[m_curparam].byref.orig_addr = inarray;
527 m_params[m_curparam].byref.sz_flags = sz_flags;
530 int CForward::PushString(const char *string)
532 if (m_curparam < m_numparams)
534 if (m_types[m_curparam] == Param_Any)
536 m_params[m_curparam].pushedas = Param_String;
537 } else if (m_types[m_curparam] != Param_String) {
538 return SetError(SP_ERROR_PARAM);
541 if (!m_varargs || m_curparam > SP_MAX_EXEC_PARAMS)
543 return SetError(SP_ERROR_PARAMS_MAX);
545 m_params[m_curparam].pushedas = Param_String;
548 _Int_PushString((cell_t *)string, strlen(string)+1, SM_PARAM_STRING_COPY, 0);
551 return SP_ERROR_NONE;
554 int CForward::PushStringEx(char *buffer, size_t length, int sz_flags, int cp_flags)
556 if (m_curparam < m_numparams)
558 if (m_types[m_curparam] == Param_Any)
560 m_params[m_curparam].pushedas = Param_String;
561 } else if (m_types[m_curparam] != Param_String) {
562 return SetError(SP_ERROR_PARAM);
565 if (!m_varargs || m_curparam > SP_MAX_EXEC_PARAMS)
567 return SetError(SP_ERROR_PARAMS_MAX);
569 m_params[m_curparam].pushedas = Param_String;
572 _Int_PushString((cell_t *)buffer, length, sz_flags, cp_flags);
575 return SP_ERROR_NONE;
578 void CForward::Cancel()
586 m_errstate = SP_ERROR_NONE;
589 bool CForward::AddFunction(IPluginContext *pContext, funcid_t index)
591 IPluginFunction *pFunc = pContext->GetFunctionById(index);
598 return AddFunction(pFunc);
601 bool CForward::RemoveFunction(IPluginFunction *func)
606 for (iter=m_functions.begin(); iter!=m_functions.end();)
611 /* If this iterator is being used, swap in a new one !*/
612 iter = m_functions.erase(iter);
618 /* Cancel a call, if any */
619 if (found || m_curparam)
627 unsigned int CForward::RemoveFunctionsOfPlugin(IPlugin *plugin)
630 IPluginFunction *func;
631 unsigned int removed = 0;
632 IPluginContext *pContext = plugin->GetBaseContext();
633 for (iter=m_functions.begin(); iter!=m_functions.end();)
636 if (func->GetParentContext() == pContext)
638 iter = m_functions.erase(iter);
648 bool CForward::AddFunction(IPluginFunction *func)
655 //:IDEA: eventually we will tell the plugin we're using it [?]
656 m_functions.push_back(func);
661 const char *CForward::GetForwardName()
666 unsigned int CForward::GetFunctionCount()
668 return m_functions.size();
671 ExecType CForward::GetExecType()