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
18 #include "ForwardSys.h"
19 #include "PluginSys.h"
22 CForwardManager g_Forwards;
25 * Genesis turns to its source, reduction occurs stepwise although the essence is all one.
26 * End of line. FTL system check.
28 * :TODO: WHAT NEEDS TO BE TESTED IN THIS BEAST
30 * - Pushing no varargs
33 // :TODO: IMPORTANT!!! The result pointer arg in the execute function maybe invalid if the forward fails
34 // so later evaluation of this result may cause problems on higher levels of abstraction. DOCUMENT OR FIX ALL FORWARDS!
36 CForwardManager::~CForwardManager()
38 CStack<CForward *>::iterator iter;
39 for (iter=m_FreeForwards.begin(); iter!=m_FreeForwards.end(); iter++)
43 m_FreeForwards.popall();
46 void CForwardManager::OnSourceModAllInitialized()
48 g_PluginSys.AddPluginsListener(this);
49 g_ShareSys.AddInterface(NULL, this);
52 IForward *CForwardManager::CreateForward(const char *name, ExecType et, unsigned int num_params, const ParamType *types, ...)
58 fwd = CForward::CreateForward(name, et, num_params, types, ap);
64 g_PluginSys.AddFunctionsToForward(name, fwd);
66 m_managed.push_back(fwd);
72 IChangeableForward *CForwardManager::CreateForwardEx(const char *name, ExecType et, int num_params, const ParamType *types, ...)
78 fwd = CForward::CreateForward(name, et, num_params, types, ap);
84 m_unmanaged.push_back(fwd);
90 void CForwardManager::OnPluginLoaded(IPlugin *plugin)
92 /* Attach any globally managed forwards */
93 List<CForward *>::iterator iter;
96 for (iter=m_managed.begin(); iter!=m_managed.end(); iter++)
99 IPluginFunction *pFunc = plugin->GetBaseContext()->GetFunctionByName(fwd->GetForwardName());
102 fwd->AddFunction(pFunc);
107 void CForwardManager::OnPluginUnloaded(IPlugin *plugin)
109 List<CForward *>::iterator iter;
112 for (iter=m_managed.begin(); iter!=m_managed.end(); iter++)
115 fwd->RemoveFunctionsOfPlugin(plugin);
118 for (iter=m_unmanaged.begin(); iter!=m_unmanaged.end(); iter++)
121 fwd->RemoveFunctionsOfPlugin(plugin);
125 IForward *CForwardManager::FindForward(const char *name, IChangeableForward **ifchng)
127 List<CForward *>::iterator iter;
130 for (iter=m_managed.begin(); iter!=m_managed.end(); iter++)
133 if (strcmp(fwd->GetForwardName(), name) == 0)
143 for (iter=m_unmanaged.begin(); iter!=m_unmanaged.end(); iter++)
146 if (strcmp(fwd->GetForwardName(), name) == 0)
164 void CForwardManager::ReleaseForward(IForward *forward)
166 ForwardFree(static_cast<CForward *>(forward));
169 void CForwardManager::ForwardFree(CForward *fwd)
171 m_FreeForwards.push(fwd);
172 m_managed.remove(fwd);
175 CForward *CForwardManager::ForwardMake()
178 if (m_FreeForwards.empty())
182 fwd = m_FreeForwards.front();
183 m_FreeForwards.pop();
188 void CForwardManager::OnPluginPauseChange(IPlugin *plugin, bool paused)
193 /*************************************
194 * ACTUAL FORWARD API IMPLEMENTATION *
195 *************************************/
197 CForward *CForward::CreateForward(const char *name, ExecType et, unsigned int num_params, const ParamType *types, va_list ap)
199 ParamType _types[SP_MAX_EXEC_PARAMS];
201 if (num_params > SP_MAX_EXEC_PARAMS)
206 if (types == NULL && num_params)
208 for (unsigned int i=0; i<num_params; i++)
210 _types[i] = (ParamType)va_arg(ap, int);
211 if (_types[i] == Param_VarArgs && (i != num_params - 1))
218 for (unsigned int i=0; i<num_params; i++)
220 _types[i] = types[i];
221 if (types[i] == Param_VarArgs && (i != num_params - 1))
228 /* First parameter can never be varargs */
229 if (num_params && _types[0] == Param_VarArgs)
234 CForward *pForward = g_Forwards.ForwardMake();
235 pForward->m_curparam = 0;
236 pForward->m_ExecType = et;
237 snprintf(pForward->m_name, FORWARDS_NAME_MAX, "%s", name ? name : "");
239 for (unsigned int i=0; i<num_params; i++)
241 pForward->m_types[i] = _types[i];
244 if (num_params && _types[num_params-1] == Param_VarArgs)
246 pForward->m_varargs = num_params--;
248 pForward->m_varargs = false;
251 pForward->m_numparams = num_params;
252 pForward->m_errstate = SP_ERROR_NONE;
257 int CForward::Execute(cell_t *result, IForwardFilter *filter)
261 int err = m_errstate;
268 filter->OnExecuteBegin();
271 FuncIter iter = m_functions.begin();
272 IPluginFunction *func;
273 cell_t cur_result = 0;
274 cell_t high_result = 0;
276 unsigned int failed=0, success=0;
277 unsigned int num_params = m_curparam;
278 FwdParamInfo temp_info[SP_MAX_EXEC_PARAMS];
282 /* Save local, reset */
283 memcpy(temp_info, m_params, sizeof(m_params));
286 for (iter=m_functions.begin(); iter!=m_functions.end(); iter++)
290 for (unsigned int i=0; i<num_params; i++)
292 param = &temp_info[i];
293 if (i >= m_numparams || m_types[i] == Param_Any)
295 type = param->pushedas;
299 if ((i >= m_numparams) || (type & SP_PARAMFLAG_BYREF))
301 /* If we're byref or we're vararg, we always push everything by ref.
302 * Even if they're byval, we must push them byref.
304 if (type == Param_String)
306 func->PushStringEx((char *)param->byref.orig_addr, param->byref.cells, param->byref.sz_flags, param->byref.flags);
307 } else if (type == Param_Float || type == Param_Cell) {
308 func->PushCellByRef(¶m->val);
310 func->PushArray(param->byref.orig_addr, param->byref.cells, param->byref.flags);
311 assert(type == Param_Array || type == Param_FloatByRef || type == Param_CellByRef);
314 /* If we're not byref or not vararg, our job is a bit easier. */
315 assert(type == Param_Cell || type == Param_Float);
316 func->PushCell(param->val);
320 /* Call the function and deal with the return value. */
321 if ((err=func->Execute(&cur_result)) != SP_ERROR_NONE)
323 bool handled = false;
326 handled = filter->OnErrorReport(this, func, err);
330 /* :TODO: invoke global error reporting here */
339 if (cur_result > high_result)
341 high_result = cur_result;
347 if (cur_result > high_result)
349 high_result = cur_result;
350 if ((ResultType)high_result == Pl_Stop)
352 iter = m_functions.end();
362 if (filter->OnFunctionReturn(this, func, &cur_result) == Pl_Stop)
364 iter = m_functions.end();
379 if (m_ExecType == ET_Event || m_ExecType == ET_Hook)
381 cur_result = high_result;
382 } else if (m_ExecType == ET_Ignore) {
387 *result = cur_result;
393 filter->OnExecuteEnd(&cur_result, success, failed);
396 return SP_ERROR_NONE;
399 int CForward::PushCell(cell_t cell)
401 if (m_curparam < m_numparams)
403 if (m_types[m_curparam] == Param_Any)
405 m_params[m_curparam].pushedas = Param_Cell;
406 } else if (m_types[m_curparam] != Param_Cell) {
407 return SetError(SP_ERROR_PARAM);
410 if (!m_varargs || m_numparams > SP_MAX_EXEC_PARAMS)
412 return SetError(SP_ERROR_PARAMS_MAX);
414 m_params[m_curparam].pushedas = Param_Cell;
417 m_params[m_curparam++].val = cell;
419 return SP_ERROR_NONE;
422 int CForward::PushFloat(float number)
424 if (m_curparam < m_numparams)
426 if (m_types[m_curparam] == Param_Any)
428 m_params[m_curparam].pushedas = Param_Float;
429 } else if (m_types[m_curparam] != Param_Float) {
430 return SetError(SP_ERROR_PARAM);
433 if (!m_varargs || m_numparams > SP_MAX_EXEC_PARAMS)
435 return SetError(SP_ERROR_PARAMS_MAX);
437 m_params[m_curparam].pushedas = Param_Float;
440 m_params[m_curparam++].val = *(cell_t *)&number;
442 return SP_ERROR_NONE;
445 int CForward::PushCellByRef(cell_t *cell, int flags)
447 if (m_curparam < m_numparams)
449 if (m_types[m_curparam] == Param_Any)
451 m_params[m_curparam].pushedas = Param_CellByRef;
452 } else if (m_types[m_curparam] != Param_CellByRef) {
453 return SetError(SP_ERROR_PARAM);
456 if (!m_varargs || m_numparams > SP_MAX_EXEC_PARAMS)
458 return SetError(SP_ERROR_PARAMS_MAX);
460 m_params[m_curparam].pushedas = Param_CellByRef;
463 _Int_PushArray(cell, 1, flags);
466 return SP_ERROR_NONE;
469 int CForward::PushFloatByRef(float *num, int flags)
471 if (m_curparam < m_numparams)
473 if (m_types[m_curparam] == Param_Any)
475 m_params[m_curparam].pushedas = Param_FloatByRef;
476 } else if (m_types[m_curparam] != Param_FloatByRef) {
477 return SetError(SP_ERROR_PARAM);
480 if (!m_varargs || m_numparams > SP_MAX_EXEC_PARAMS)
482 return SetError(SP_ERROR_PARAMS_MAX);
484 m_params[m_curparam].pushedas = Param_FloatByRef;
487 _Int_PushArray((cell_t *)num, 1, flags);
490 return SP_ERROR_NONE;
493 void CForward::_Int_PushArray(cell_t *inarray, unsigned int cells, int flags)
495 m_params[m_curparam].byref.cells = cells;
496 m_params[m_curparam].byref.flags = flags;
497 m_params[m_curparam].byref.orig_addr = inarray;
500 int CForward::PushArray(cell_t *inarray, unsigned int cells, int flags)
502 /* We don't allow this here */
505 return SetError(SP_ERROR_PARAM);
508 if (m_curparam < m_numparams)
510 if (m_types[m_curparam] == Param_Any)
512 m_params[m_curparam].pushedas = Param_Array;
513 } else if (m_types[m_curparam] != Param_Array) {
514 return SetError(SP_ERROR_PARAM);
517 if (!m_varargs || m_curparam > SP_MAX_EXEC_PARAMS)
519 return SetError(SP_ERROR_PARAMS_MAX);
521 m_params[m_curparam].pushedas = Param_Array;
524 _Int_PushArray(inarray, cells, flags);
528 return SP_ERROR_NONE;
531 void CForward::_Int_PushString(cell_t *inarray, unsigned int cells, int sz_flags, int cp_flags)
533 m_params[m_curparam].byref.cells = cells; /* Notice this contains the char count not cell count */
534 m_params[m_curparam].byref.flags = cp_flags;
535 m_params[m_curparam].byref.orig_addr = inarray;
536 m_params[m_curparam].byref.sz_flags = sz_flags;
539 int CForward::PushString(const char *string)
541 if (m_curparam < m_numparams)
543 if (m_types[m_curparam] == Param_Any)
545 m_params[m_curparam].pushedas = Param_String;
546 } else if (m_types[m_curparam] != Param_String) {
547 return SetError(SP_ERROR_PARAM);
550 if (!m_varargs || m_curparam > SP_MAX_EXEC_PARAMS)
552 return SetError(SP_ERROR_PARAMS_MAX);
554 m_params[m_curparam].pushedas = Param_String;
557 _Int_PushString((cell_t *)string, strlen(string)+1, SM_PARAM_STRING_COPY, 0);
560 return SP_ERROR_NONE;
563 int CForward::PushStringEx(char *buffer, size_t length, int sz_flags, int cp_flags)
565 if (m_curparam < m_numparams)
567 if (m_types[m_curparam] == Param_Any)
569 m_params[m_curparam].pushedas = Param_String;
570 } else if (m_types[m_curparam] != Param_String) {
571 return SetError(SP_ERROR_PARAM);
574 if (!m_varargs || m_curparam > SP_MAX_EXEC_PARAMS)
576 return SetError(SP_ERROR_PARAMS_MAX);
578 m_params[m_curparam].pushedas = Param_String;
581 _Int_PushString((cell_t *)buffer, length, sz_flags, cp_flags);
584 return SP_ERROR_NONE;
587 void CForward::Cancel()
595 m_errstate = SP_ERROR_NONE;
598 bool CForward::AddFunction(IPluginContext *pContext, funcid_t index)
600 IPluginFunction *pFunc = pContext->GetFunctionById(index);
607 return AddFunction(pFunc);
610 bool CForward::RemoveFunction(IPluginContext *pContext, funcid_t index)
612 IPluginFunction *pFunc = pContext->GetFunctionById(index);
619 return RemoveFunction(pFunc);
622 bool CForward::RemoveFunction(IPluginFunction *func)
626 List<IPluginFunction *> *lst;
628 if (func->IsRunnable())
635 for (iter=m_functions.begin(); iter!=m_functions.end(); iter++)
645 /* Cancel a call, if any */
646 if (found || m_curparam)
654 unsigned int CForward::RemoveFunctionsOfPlugin(IPlugin *plugin)
657 IPluginFunction *func;
658 unsigned int removed = 0;
659 IPluginContext *pContext = plugin->GetBaseContext();
660 for (iter=m_functions.begin(); iter!=m_functions.end();)
663 if (func->GetParentContext() == pContext)
665 iter = m_functions.erase(iter);
675 bool CForward::AddFunction(IPluginFunction *func)
682 if (func->IsRunnable())
684 m_functions.push_back(func);
686 m_paused.push_back(func);
692 const char *CForward::GetForwardName()
697 unsigned int CForward::GetFunctionCount()
699 return m_functions.size();
702 ExecType CForward::GetExecType()