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 void CForwardManager::OnPluginPauseChange(IPlugin *plugin, bool paused)
185 /*************************************
186 * ACTUAL FORWARD API IMPLEMENTATION *
187 *************************************/
189 CForward *CForward::CreateForward(const char *name, ExecType et, unsigned int num_params, ParamType *types, va_list ap)
191 ParamType _types[SP_MAX_EXEC_PARAMS];
193 if (num_params > SP_MAX_EXEC_PARAMS)
198 if (types == NULL && num_params)
200 for (unsigned int i=0; i<num_params; i++)
202 _types[i] = (ParamType)va_arg(ap, int);
203 if (_types[i] == Param_VarArgs && (i != num_params - 1))
210 for (unsigned int i=0; i<num_params; i++)
212 if (types[i] == Param_VarArgs && (i != num_params - 1))
219 /* First parameter can never be varargs */
220 if (types[0] == Param_VarArgs)
225 CForward *pForward = g_Forwards.ForwardMake();
226 pForward->m_curparam = 0;
227 pForward->m_ExecType = et;
228 snprintf(pForward->m_name, FORWARDS_NAME_MAX, "%s", name ? name : "");
230 for (unsigned int i=0; i<num_params; i++)
232 pForward->m_types[i] = types[i];
235 if (num_params && types[num_params-1] == Param_VarArgs)
237 pForward->m_varargs = num_params--;
239 pForward->m_varargs = false;
242 pForward->m_numparams = num_params;
243 pForward->m_errstate = SP_ERROR_NONE;
248 int CForward::Execute(cell_t *result, IForwardFilter *filter)
252 int err = m_errstate;
259 filter->OnExecuteBegin();
262 FuncIter iter = m_functions.begin();
263 IPluginFunction *func;
264 cell_t cur_result = 0;
265 cell_t high_result = 0;
267 unsigned int failed=0, success=0;
268 unsigned int num_params = m_curparam;
269 FwdParamInfo temp_info[SP_MAX_EXEC_PARAMS];
273 /* Save local, reset */
274 memcpy(temp_info, m_params, sizeof(m_params));
277 for (iter=m_functions.begin(); iter!=m_functions.end(); iter++)
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();
370 if (m_ExecType == ET_Event || m_ExecType == ET_Hook)
372 cur_result = high_result;
373 } else if (m_ExecType == ET_Ignore) {
378 *result = cur_result;
384 filter->OnExecuteEnd(&cur_result, success, failed);
387 return SP_ERROR_NONE;
390 int CForward::PushCell(cell_t cell)
392 if (m_curparam < m_numparams)
394 if (m_types[m_curparam] == Param_Any)
396 m_params[m_curparam].pushedas = Param_Cell;
397 } else if (m_types[m_curparam] != Param_Cell) {
398 return SetError(SP_ERROR_PARAM);
401 if (!m_varargs || m_numparams > SP_MAX_EXEC_PARAMS)
403 return SetError(SP_ERROR_PARAMS_MAX);
405 m_params[m_curparam].pushedas = Param_Cell;
408 m_params[m_curparam++].val = cell;
410 return SP_ERROR_NONE;
413 int CForward::PushFloat(float number)
415 if (m_curparam < m_numparams)
417 if (m_types[m_curparam] == Param_Any)
419 m_params[m_curparam].pushedas = Param_Float;
420 } else if (m_types[m_curparam] != Param_Float) {
421 return SetError(SP_ERROR_PARAM);
424 if (!m_varargs || m_numparams > SP_MAX_EXEC_PARAMS)
426 return SetError(SP_ERROR_PARAMS_MAX);
428 m_params[m_curparam].pushedas = Param_Float;
431 m_params[m_curparam++].val = *(cell_t *)&number;
433 return SP_ERROR_NONE;
436 int CForward::PushCellByRef(cell_t *cell, int flags)
438 if (m_curparam < m_numparams)
440 if (m_types[m_curparam] == Param_Any)
442 m_params[m_curparam].pushedas = Param_CellByRef;
443 } else if (m_types[m_curparam] != Param_CellByRef) {
444 return SetError(SP_ERROR_PARAM);
447 if (!m_varargs || m_numparams > SP_MAX_EXEC_PARAMS)
449 return SetError(SP_ERROR_PARAMS_MAX);
451 m_params[m_curparam].pushedas = Param_CellByRef;
454 _Int_PushArray(cell, 1, flags);
457 return SP_ERROR_NONE;
460 int CForward::PushFloatByRef(float *num, int flags)
462 if (m_curparam < m_numparams)
464 if (m_types[m_curparam] == Param_Any)
466 m_params[m_curparam].pushedas = Param_FloatByRef;
467 } else if (m_types[m_curparam] != Param_FloatByRef) {
468 return SetError(SP_ERROR_PARAM);
471 if (!m_varargs || m_numparams > SP_MAX_EXEC_PARAMS)
473 return SetError(SP_ERROR_PARAMS_MAX);
475 m_params[m_curparam].pushedas = Param_FloatByRef;
478 _Int_PushArray((cell_t *)num, 1, flags);
481 return SP_ERROR_NONE;
484 void CForward::_Int_PushArray(cell_t *inarray, unsigned int cells, int flags)
486 m_params[m_curparam].byref.cells = cells;
487 m_params[m_curparam].byref.flags = flags;
488 m_params[m_curparam].byref.orig_addr = inarray;
491 int CForward::PushArray(cell_t *inarray, unsigned int cells, cell_t **phys_addr, int flags)
493 /* We don't allow this here */
496 return SetError(SP_ERROR_PARAM);
499 if (m_curparam < m_numparams)
501 if (m_types[m_curparam] == Param_Any)
503 m_params[m_curparam].pushedas = Param_Array;
504 } else if (m_types[m_curparam] != Param_Array) {
505 return SetError(SP_ERROR_PARAM);
508 if (!m_varargs || m_curparam > SP_MAX_EXEC_PARAMS)
510 return SetError(SP_ERROR_PARAMS_MAX);
512 m_params[m_curparam].pushedas = Param_Array;
520 _Int_PushArray(inarray, cells, flags);
524 return SP_ERROR_NONE;
527 void CForward::_Int_PushString(cell_t *inarray, unsigned int cells, int sz_flags, int cp_flags)
529 m_params[m_curparam].byref.cells = cells; /* Notice this contains the char count not cell count */
530 m_params[m_curparam].byref.flags = cp_flags;
531 m_params[m_curparam].byref.orig_addr = inarray;
532 m_params[m_curparam].byref.sz_flags = sz_flags;
535 int CForward::PushString(const char *string)
537 if (m_curparam < m_numparams)
539 if (m_types[m_curparam] == Param_Any)
541 m_params[m_curparam].pushedas = Param_String;
542 } else if (m_types[m_curparam] != Param_String) {
543 return SetError(SP_ERROR_PARAM);
546 if (!m_varargs || m_curparam > SP_MAX_EXEC_PARAMS)
548 return SetError(SP_ERROR_PARAMS_MAX);
550 m_params[m_curparam].pushedas = Param_String;
553 _Int_PushString((cell_t *)string, strlen(string)+1, SM_PARAM_STRING_COPY, 0);
556 return SP_ERROR_NONE;
559 int CForward::PushStringEx(char *buffer, size_t length, int sz_flags, int cp_flags)
561 if (m_curparam < m_numparams)
563 if (m_types[m_curparam] == Param_Any)
565 m_params[m_curparam].pushedas = Param_String;
566 } else if (m_types[m_curparam] != Param_String) {
567 return SetError(SP_ERROR_PARAM);
570 if (!m_varargs || m_curparam > SP_MAX_EXEC_PARAMS)
572 return SetError(SP_ERROR_PARAMS_MAX);
574 m_params[m_curparam].pushedas = Param_String;
577 _Int_PushString((cell_t *)buffer, length, sz_flags, cp_flags);
580 return SP_ERROR_NONE;
583 void CForward::Cancel()
591 m_errstate = SP_ERROR_NONE;
594 bool CForward::AddFunction(IPluginContext *pContext, funcid_t index)
596 IPluginFunction *pFunc = pContext->GetFunctionById(index);
603 return AddFunction(pFunc);
606 bool CForward::RemoveFunction(IPluginFunction *func)
610 List<IPluginFunction *> *lst;
612 if (func->IsRunnable())
619 for (iter=m_functions.begin(); iter!=m_functions.end(); iter++)
629 /* Cancel a call, if any */
630 if (found || m_curparam)
638 unsigned int CForward::RemoveFunctionsOfPlugin(IPlugin *plugin)
641 IPluginFunction *func;
642 unsigned int removed = 0;
643 IPluginContext *pContext = plugin->GetBaseContext();
644 for (iter=m_functions.begin(); iter!=m_functions.end();)
647 if (func->GetParentContext() == pContext)
649 iter = m_functions.erase(iter);
659 bool CForward::AddFunction(IPluginFunction *func)
666 if (func->IsRunnable())
668 m_functions.push_back(func);
670 m_paused.push_back(func);
676 const char *CForward::GetForwardName() const
681 unsigned int CForward::GetFunctionCount() const
683 return m_functions.size();
686 ExecType CForward::GetExecType() const