3 * =============================================================================
5 * Copyright (C) 2004-2008 AlliedModders LLC. All rights reserved.
6 * =============================================================================
8 * This program is free software; you can redistribute it and/or modify it under
9 * the terms of the GNU General Public License, version 3.0, as published by the
10 * Free Software Foundation.
12 * This program is distributed in the hope that it will be useful, but WITHOUT
13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
17 * You should have received a copy of the GNU General Public License along with
18 * this program. If not, see <http://www.gnu.org/licenses/>.
20 * As a special exception, AlliedModders LLC gives you permission to link the
21 * code of this program (as well as its derivative works) to "Half-Life 2," the
22 * "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
23 * by the Valve Corporation. You must obey the GNU General Public License in
24 * all respects for all other code used. Additionally, AlliedModders LLC grants
25 * this exception to all derivative works. AlliedModders LLC defines further
26 * exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
27 * or <http://www.sourcemod.net/license.php>.
35 #include "ForwardSys.h"
36 #include "PluginSys.h"
38 #include "DebugReporter.h"
40 CForwardManager g_Forwards;
43 * Genesis turns to its source, reduction occurs stepwise although the essence is all one.
44 * End of line. FTL system check.
46 * :TODO: WHAT NEEDS TO BE TESTED IN THIS BEAST
48 * - Pushing no varargs
51 // :TODO: IMPORTANT!!! The result pointer arg in the execute function maybe invalid if the forward fails
52 // so later evaluation of this result may cause problems on higher levels of abstraction. DOCUMENT OR FIX ALL FORWARDS!
54 CForwardManager::~CForwardManager()
56 CStack<CForward *>::iterator iter;
57 for (iter=m_FreeForwards.begin(); iter!=m_FreeForwards.end(); iter++)
61 m_FreeForwards.popall();
64 void CForwardManager::OnSourceModAllInitialized()
66 g_PluginSys.AddPluginsListener(this);
67 g_ShareSys.AddInterface(NULL, this);
70 IForward *CForwardManager::CreateForward(const char *name, ExecType et, unsigned int num_params, const ParamType *types, ...)
76 fwd = CForward::CreateForward(name, et, num_params, types, ap);
82 g_PluginSys.AddFunctionsToForward(name, fwd);
84 m_managed.push_back(fwd);
90 IChangeableForward *CForwardManager::CreateForwardEx(const char *name, ExecType et, int num_params, const ParamType *types, ...)
96 fwd = CForward::CreateForward(name, et, num_params, types, ap);
102 m_unmanaged.push_back(fwd);
108 void CForwardManager::OnPluginLoaded(IPlugin *plugin)
110 /* Attach any globally managed forwards */
111 List<CForward *>::iterator iter;
114 for (iter=m_managed.begin(); iter!=m_managed.end(); iter++)
117 IPluginFunction *pFunc = plugin->GetBaseContext()->GetFunctionByName(fwd->GetForwardName());
120 fwd->AddFunction(pFunc);
125 void CForwardManager::OnPluginUnloaded(IPlugin *plugin)
127 List<CForward *>::iterator iter;
130 for (iter=m_managed.begin(); iter!=m_managed.end(); iter++)
133 fwd->RemoveFunctionsOfPlugin(plugin);
136 for (iter=m_unmanaged.begin(); iter!=m_unmanaged.end(); iter++)
139 fwd->RemoveFunctionsOfPlugin(plugin);
143 IForward *CForwardManager::FindForward(const char *name, IChangeableForward **ifchng)
145 List<CForward *>::iterator iter;
148 for (iter=m_managed.begin(); iter!=m_managed.end(); iter++)
151 if (strcmp(fwd->GetForwardName(), name) == 0)
161 for (iter=m_unmanaged.begin(); iter!=m_unmanaged.end(); iter++)
164 if (strcmp(fwd->GetForwardName(), name) == 0)
182 void CForwardManager::ReleaseForward(IForward *forward)
184 ForwardFree(static_cast<CForward *>(forward));
187 void CForwardManager::ForwardFree(CForward *fwd)
189 m_FreeForwards.push(fwd);
190 m_managed.remove(fwd);
193 CForward *CForwardManager::ForwardMake()
196 if (m_FreeForwards.empty())
200 fwd = m_FreeForwards.front();
201 m_FreeForwards.pop();
206 void CForwardManager::OnPluginPauseChange(IPlugin *plugin, bool paused)
211 /*************************************
212 * ACTUAL FORWARD API IMPLEMENTATION *
213 *************************************/
215 CForward *CForward::CreateForward(const char *name, ExecType et, unsigned int num_params, const ParamType *types, va_list ap)
217 ParamType _types[SP_MAX_EXEC_PARAMS];
219 if (num_params > SP_MAX_EXEC_PARAMS)
224 if (types == NULL && num_params)
226 for (unsigned int i=0; i<num_params; i++)
228 _types[i] = (ParamType)va_arg(ap, int);
229 if (_types[i] == Param_VarArgs && (i != num_params - 1))
236 for (unsigned int i=0; i<num_params; i++)
238 _types[i] = types[i];
239 if (types[i] == Param_VarArgs && (i != num_params - 1))
246 /* First parameter can never be varargs */
247 if (num_params && _types[0] == Param_VarArgs)
252 CForward *pForward = g_Forwards.ForwardMake();
253 pForward->m_curparam = 0;
254 pForward->m_ExecType = et;
255 snprintf(pForward->m_name, FORWARDS_NAME_MAX, "%s", name ? name : "");
257 for (unsigned int i=0; i<num_params; i++)
259 pForward->m_types[i] = _types[i];
262 if (num_params && _types[num_params-1] == Param_VarArgs)
264 pForward->m_varargs = num_params--;
266 pForward->m_varargs = false;
269 pForward->m_numparams = num_params;
270 pForward->m_errstate = SP_ERROR_NONE;
272 pForward->m_functions.clear();
277 int CForward::Execute(cell_t *result, IForwardFilter *filter)
281 int err = m_errstate;
286 FuncIter iter = m_functions.begin();
287 IPluginFunction *func;
288 cell_t cur_result = 0;
289 cell_t high_result = 0;
290 cell_t low_result = 0;
292 unsigned int failed=0, success=0;
293 unsigned int num_params = m_curparam;
294 FwdParamInfo temp_info[SP_MAX_EXEC_PARAMS];
298 /* Save local, reset */
299 memcpy(temp_info, m_params, sizeof(m_params));
302 for (iter=m_functions.begin(); iter!=m_functions.end(); iter++)
306 for (unsigned int i=0; i<num_params; i++)
308 int err = SP_ERROR_PARAM;
309 param = &temp_info[i];
311 if (i >= m_numparams || m_types[i] == Param_Any)
313 type = param->pushedas;
320 if ((i >= m_numparams) || (type & SP_PARAMFLAG_BYREF))
322 /* If we're byref or we're vararg, we always push everything by ref.
323 * Even if they're byval, we must push them byref.
325 if (type == Param_String)
327 err = func->PushStringEx((char *)param->byref.orig_addr, param->byref.cells, param->byref.sz_flags, param->byref.flags);
329 else if (type == Param_Float || type == Param_Cell)
331 err = func->PushCellByRef(¶m->val);
335 err = func->PushArray(param->byref.orig_addr, param->byref.cells, param->byref.flags);
336 assert(type == Param_Array || type == Param_FloatByRef || type == Param_CellByRef);
341 /* If we're not byref or not vararg, our job is a bit easier. */
342 assert(type == Param_Cell || type == Param_Float);
343 err = func->PushCell(param->val);
346 if (err != SP_ERROR_NONE)
348 g_DbgReporter.GenerateError(func->GetParentContext(),
349 func->GetFunctionID(),
351 "Failed to push parameter while executing forward");
356 /* Call the function and deal with the return value. */
357 if ((err=func->Execute(&cur_result)) != SP_ERROR_NONE)
368 if (cur_result > high_result)
370 high_result = cur_result;
376 if (cur_result > high_result)
378 high_result = cur_result;
379 if ((ResultType)high_result == Pl_Stop)
388 if (cur_result < low_result)
390 low_result = cur_result;
416 cur_result = high_result;
421 cur_result = low_result;
432 *result = cur_result;
436 return SP_ERROR_NONE;
439 int CForward::PushCell(cell_t cell)
441 if (m_curparam < m_numparams)
443 if (m_types[m_curparam] == Param_Any)
445 m_params[m_curparam].pushedas = Param_Cell;
446 } else if (m_types[m_curparam] != Param_Cell) {
447 return SetError(SP_ERROR_PARAM);
450 if (!m_varargs || m_numparams > SP_MAX_EXEC_PARAMS)
452 return SetError(SP_ERROR_PARAMS_MAX);
454 m_params[m_curparam].pushedas = Param_Cell;
457 m_params[m_curparam++].val = cell;
459 return SP_ERROR_NONE;
462 int CForward::PushFloat(float number)
464 if (m_curparam < m_numparams)
466 if (m_types[m_curparam] == Param_Any)
468 m_params[m_curparam].pushedas = Param_Float;
469 } else if (m_types[m_curparam] != Param_Float) {
470 return SetError(SP_ERROR_PARAM);
473 if (!m_varargs || m_numparams > SP_MAX_EXEC_PARAMS)
475 return SetError(SP_ERROR_PARAMS_MAX);
477 m_params[m_curparam].pushedas = Param_Float;
480 m_params[m_curparam++].val = *(cell_t *)&number;
482 return SP_ERROR_NONE;
485 int CForward::PushCellByRef(cell_t *cell, int flags)
487 if (m_curparam < m_numparams)
489 if (m_types[m_curparam] == Param_Any)
491 m_params[m_curparam].pushedas = Param_CellByRef;
492 } else if (m_types[m_curparam] != Param_CellByRef) {
493 return SetError(SP_ERROR_PARAM);
496 if (!m_varargs || m_numparams > SP_MAX_EXEC_PARAMS)
498 return SetError(SP_ERROR_PARAMS_MAX);
500 m_params[m_curparam].pushedas = Param_CellByRef;
503 _Int_PushArray(cell, 1, flags);
506 return SP_ERROR_NONE;
509 int CForward::PushFloatByRef(float *num, int flags)
511 if (m_curparam < m_numparams)
513 if (m_types[m_curparam] == Param_Any)
515 m_params[m_curparam].pushedas = Param_FloatByRef;
516 } else if (m_types[m_curparam] != Param_FloatByRef) {
517 return SetError(SP_ERROR_PARAM);
520 if (!m_varargs || m_numparams > SP_MAX_EXEC_PARAMS)
522 return SetError(SP_ERROR_PARAMS_MAX);
524 m_params[m_curparam].pushedas = Param_FloatByRef;
527 _Int_PushArray((cell_t *)num, 1, flags);
530 return SP_ERROR_NONE;
533 void CForward::_Int_PushArray(cell_t *inarray, unsigned int cells, int flags)
535 m_params[m_curparam].byref.cells = cells;
536 m_params[m_curparam].byref.flags = flags;
537 m_params[m_curparam].byref.orig_addr = inarray;
540 int CForward::PushArray(cell_t *inarray, unsigned int cells, int flags)
542 /* We don't allow this here */
545 return SetError(SP_ERROR_PARAM);
548 if (m_curparam < m_numparams)
550 if (m_types[m_curparam] == Param_Any)
552 m_params[m_curparam].pushedas = Param_Array;
553 } else if (m_types[m_curparam] != Param_Array) {
554 return SetError(SP_ERROR_PARAM);
557 if (!m_varargs || m_curparam > SP_MAX_EXEC_PARAMS)
559 return SetError(SP_ERROR_PARAMS_MAX);
561 m_params[m_curparam].pushedas = Param_Array;
564 _Int_PushArray(inarray, cells, flags);
568 return SP_ERROR_NONE;
571 void CForward::_Int_PushString(cell_t *inarray, unsigned int cells, int sz_flags, int cp_flags)
573 m_params[m_curparam].byref.cells = cells; /* Notice this contains the char count not cell count */
574 m_params[m_curparam].byref.flags = cp_flags;
575 m_params[m_curparam].byref.orig_addr = inarray;
576 m_params[m_curparam].byref.sz_flags = sz_flags;
579 int CForward::PushString(const char *string)
581 if (m_curparam < m_numparams)
583 if (m_types[m_curparam] == Param_Any)
585 m_params[m_curparam].pushedas = Param_String;
586 } else if (m_types[m_curparam] != Param_String) {
587 return SetError(SP_ERROR_PARAM);
590 if (!m_varargs || m_curparam > SP_MAX_EXEC_PARAMS)
592 return SetError(SP_ERROR_PARAMS_MAX);
594 m_params[m_curparam].pushedas = Param_String;
597 _Int_PushString((cell_t *)string, strlen(string)+1, SM_PARAM_STRING_COPY, 0);
600 return SP_ERROR_NONE;
603 int CForward::PushStringEx(char *buffer, size_t length, int sz_flags, int cp_flags)
605 if (m_curparam < m_numparams)
607 if (m_types[m_curparam] == Param_Any)
609 m_params[m_curparam].pushedas = Param_String;
610 } else if (m_types[m_curparam] != Param_String) {
611 return SetError(SP_ERROR_PARAM);
614 if (!m_varargs || m_curparam > SP_MAX_EXEC_PARAMS)
616 return SetError(SP_ERROR_PARAMS_MAX);
618 m_params[m_curparam].pushedas = Param_String;
621 _Int_PushString((cell_t *)buffer, length, sz_flags, cp_flags);
624 return SP_ERROR_NONE;
627 void CForward::Cancel()
635 m_errstate = SP_ERROR_NONE;
638 bool CForward::AddFunction(IPluginContext *pContext, funcid_t index)
640 IPluginFunction *pFunc = pContext->GetFunctionById(index);
647 return AddFunction(pFunc);
650 bool CForward::RemoveFunction(IPluginContext *pContext, funcid_t index)
652 IPluginFunction *pFunc = pContext->GetFunctionById(index);
659 return RemoveFunction(pFunc);
662 bool CForward::RemoveFunction(IPluginFunction *func)
666 List<IPluginFunction *> *lst;
668 if (func->IsRunnable())
675 for (iter=m_functions.begin(); iter!=m_functions.end(); iter++)
685 /* Cancel a call, if any */
686 if (found || m_curparam)
694 unsigned int CForward::RemoveFunctionsOfPlugin(IPlugin *plugin)
697 IPluginFunction *func;
698 unsigned int removed = 0;
699 IPluginContext *pContext = plugin->GetBaseContext();
700 for (iter=m_functions.begin(); iter!=m_functions.end();)
703 if (func->GetParentContext() == pContext)
705 iter = m_functions.erase(iter);
715 bool CForward::AddFunction(IPluginFunction *func)
722 if (func->IsRunnable())
724 m_functions.push_back(func);
726 m_paused.push_back(func);
732 const char *CForward::GetForwardName()
737 unsigned int CForward::GetFunctionCount()
739 return m_functions.size();
742 ExecType CForward::GetExecType()