2 * vim: set ts=4 sw=4 tw=99 noet :
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)
194 m_FreeForwards.push(fwd);
195 m_managed.remove(fwd);
198 CForward *CForwardManager::ForwardMake()
201 if (m_FreeForwards.empty())
205 fwd = m_FreeForwards.front();
206 m_FreeForwards.pop();
211 void CForwardManager::OnPluginPauseChange(IPlugin *plugin, bool paused)
216 /*************************************
217 * ACTUAL FORWARD API IMPLEMENTATION *
218 *************************************/
220 CForward *CForward::CreateForward(const char *name, ExecType et, unsigned int num_params, const ParamType *types, va_list ap)
222 ParamType _types[SP_MAX_EXEC_PARAMS];
224 if (num_params > SP_MAX_EXEC_PARAMS)
229 if (types == NULL && num_params)
231 for (unsigned int i=0; i<num_params; i++)
233 _types[i] = (ParamType)va_arg(ap, int);
234 if (_types[i] == Param_VarArgs && (i != num_params - 1))
241 for (unsigned int i=0; i<num_params; i++)
243 _types[i] = types[i];
244 if (types[i] == Param_VarArgs && (i != num_params - 1))
251 /* First parameter can never be varargs */
252 if (num_params && _types[0] == Param_VarArgs)
257 CForward *pForward = g_Forwards.ForwardMake();
258 pForward->m_IterGuard = NULL;
259 pForward->m_curparam = 0;
260 pForward->m_ExecType = et;
261 snprintf(pForward->m_name, FORWARDS_NAME_MAX, "%s", name ? name : "");
263 for (unsigned int i=0; i<num_params; i++)
265 pForward->m_types[i] = _types[i];
268 if (num_params && _types[num_params-1] == Param_VarArgs)
270 pForward->m_varargs = num_params--;
272 pForward->m_varargs = false;
275 pForward->m_numparams = num_params;
276 pForward->m_errstate = SP_ERROR_NONE;
278 pForward->m_functions.clear();
283 int CForward::Execute(cell_t *result, IForwardFilter *filter)
287 int err = m_errstate;
292 FuncIter iter = m_functions.begin();
293 IPluginFunction *func;
294 cell_t cur_result = 0;
295 cell_t high_result = 0;
296 cell_t low_result = 0;
298 unsigned int success=0;
299 unsigned int num_params = m_curparam;
300 FwdParamInfo temp_info[SP_MAX_EXEC_PARAMS];
304 /* Save local, reset */
305 memcpy(temp_info, m_params, sizeof(m_params));
308 FuncIteratorGuard guard(&m_IterGuard, &iter);
310 while (iter != m_functions.end())
315 filter->Preprocess(func, temp_info);
317 for (unsigned int i=0; i<num_params; i++)
319 int err = SP_ERROR_PARAM;
320 param = &temp_info[i];
322 if (i >= m_numparams || m_types[i] == Param_Any)
324 type = param->pushedas;
331 if ((i >= m_numparams) || (type & SP_PARAMFLAG_BYREF))
333 /* If we're byref or we're vararg, we always push everything by ref.
334 * Even if they're byval, we must push them byref.
336 if (type == Param_String)
338 err = func->PushStringEx((char *)param->byref.orig_addr, param->byref.cells, param->byref.sz_flags, param->byref.flags);
340 else if (type == Param_Float || type == Param_Cell)
342 err = func->PushCellByRef(¶m->val);
346 err = func->PushArray(param->byref.orig_addr, param->byref.cells, param->byref.flags);
347 assert(type == Param_Array || type == Param_FloatByRef || type == Param_CellByRef);
352 /* If we're not byref or not vararg, our job is a bit easier. */
353 assert(type == Param_Cell || type == Param_Float);
354 err = func->PushCell(param->val);
357 if (err != SP_ERROR_NONE)
359 g_DbgReporter.GenerateError(func->GetParentContext(),
360 func->GetFunctionID(),
362 "Failed to push parameter while executing forward");
367 /* Call the function and deal with the return value. */
368 if ((err=func->Execute(&cur_result)) == SP_ERROR_NONE)
375 if (cur_result > high_result)
377 high_result = cur_result;
383 if (cur_result > high_result)
385 high_result = cur_result;
386 if ((ResultType)high_result == Pl_Stop)
395 /* Check if the current result is the lowest so far (or if it's the first result) */
396 if (cur_result < low_result || success == 1)
398 low_result = cur_result;
409 if (!guard.Triggered())
427 cur_result = high_result;
432 cur_result = low_result;
443 *result = cur_result;
447 return SP_ERROR_NONE;
450 int CForward::PushCell(cell_t cell)
452 if (m_curparam < m_numparams)
454 if (m_types[m_curparam] == Param_Any)
456 m_params[m_curparam].pushedas = Param_Cell;
457 } else if (m_types[m_curparam] != Param_Cell) {
458 return SetError(SP_ERROR_PARAM);
461 if (!m_varargs || m_numparams > SP_MAX_EXEC_PARAMS)
463 return SetError(SP_ERROR_PARAMS_MAX);
465 m_params[m_curparam].pushedas = Param_Cell;
468 m_params[m_curparam++].val = cell;
470 return SP_ERROR_NONE;
473 int CForward::PushFloat(float number)
475 if (m_curparam < m_numparams)
477 if (m_types[m_curparam] == Param_Any)
479 m_params[m_curparam].pushedas = Param_Float;
480 } else if (m_types[m_curparam] != Param_Float) {
481 return SetError(SP_ERROR_PARAM);
484 if (!m_varargs || m_numparams > SP_MAX_EXEC_PARAMS)
486 return SetError(SP_ERROR_PARAMS_MAX);
488 m_params[m_curparam].pushedas = Param_Float;
491 m_params[m_curparam++].val = *(cell_t *)&number;
493 return SP_ERROR_NONE;
496 int CForward::PushCellByRef(cell_t *cell, int flags)
498 if (m_curparam < m_numparams)
500 if (m_types[m_curparam] == Param_Any)
502 m_params[m_curparam].pushedas = Param_CellByRef;
503 } else if (m_types[m_curparam] != Param_CellByRef) {
504 return SetError(SP_ERROR_PARAM);
507 if (!m_varargs || m_numparams > SP_MAX_EXEC_PARAMS)
509 return SetError(SP_ERROR_PARAMS_MAX);
511 m_params[m_curparam].pushedas = Param_CellByRef;
514 _Int_PushArray(cell, 1, flags);
517 return SP_ERROR_NONE;
520 int CForward::PushFloatByRef(float *num, int flags)
522 if (m_curparam < m_numparams)
524 if (m_types[m_curparam] == Param_Any)
526 m_params[m_curparam].pushedas = Param_FloatByRef;
527 } else if (m_types[m_curparam] != Param_FloatByRef) {
528 return SetError(SP_ERROR_PARAM);
531 if (!m_varargs || m_numparams > SP_MAX_EXEC_PARAMS)
533 return SetError(SP_ERROR_PARAMS_MAX);
535 m_params[m_curparam].pushedas = Param_FloatByRef;
538 _Int_PushArray((cell_t *)num, 1, flags);
541 return SP_ERROR_NONE;
544 void CForward::_Int_PushArray(cell_t *inarray, unsigned int cells, int flags)
546 m_params[m_curparam].byref.cells = cells;
547 m_params[m_curparam].byref.flags = flags;
548 m_params[m_curparam].byref.orig_addr = inarray;
551 int CForward::PushArray(cell_t *inarray, unsigned int cells, int flags)
553 /* We don't allow this here */
556 return SetError(SP_ERROR_PARAM);
559 if (m_curparam < m_numparams)
561 if (m_types[m_curparam] == Param_Any)
563 m_params[m_curparam].pushedas = Param_Array;
564 } else if (m_types[m_curparam] != Param_Array) {
565 return SetError(SP_ERROR_PARAM);
568 if (!m_varargs || m_curparam > SP_MAX_EXEC_PARAMS)
570 return SetError(SP_ERROR_PARAMS_MAX);
572 m_params[m_curparam].pushedas = Param_Array;
575 _Int_PushArray(inarray, cells, flags);
579 return SP_ERROR_NONE;
582 void CForward::_Int_PushString(cell_t *inarray, unsigned int cells, int sz_flags, int cp_flags)
584 m_params[m_curparam].byref.cells = cells; /* Notice this contains the char count not cell count */
585 m_params[m_curparam].byref.flags = cp_flags;
586 m_params[m_curparam].byref.orig_addr = inarray;
587 m_params[m_curparam].byref.sz_flags = sz_flags;
590 int CForward::PushString(const char *string)
592 if (m_curparam < m_numparams)
594 if (m_types[m_curparam] == Param_Any)
596 m_params[m_curparam].pushedas = Param_String;
597 } else if (m_types[m_curparam] != Param_String) {
598 return SetError(SP_ERROR_PARAM);
601 if (!m_varargs || m_curparam > SP_MAX_EXEC_PARAMS)
603 return SetError(SP_ERROR_PARAMS_MAX);
605 m_params[m_curparam].pushedas = Param_String;
608 _Int_PushString((cell_t *)string, strlen(string)+1, SM_PARAM_STRING_COPY, 0);
611 return SP_ERROR_NONE;
614 int CForward::PushStringEx(char *buffer, size_t length, int sz_flags, int cp_flags)
616 if (m_curparam < m_numparams)
618 if (m_types[m_curparam] == Param_Any)
620 m_params[m_curparam].pushedas = Param_String;
621 } else if (m_types[m_curparam] != Param_String) {
622 return SetError(SP_ERROR_PARAM);
625 if (!m_varargs || m_curparam > SP_MAX_EXEC_PARAMS)
627 return SetError(SP_ERROR_PARAMS_MAX);
629 m_params[m_curparam].pushedas = Param_String;
632 _Int_PushString((cell_t *)buffer, length, sz_flags, cp_flags);
635 return SP_ERROR_NONE;
638 void CForward::Cancel()
646 m_errstate = SP_ERROR_NONE;
649 bool CForward::AddFunction(IPluginContext *pContext, funcid_t index)
651 IPluginFunction *pFunc = pContext->GetFunctionById(index);
658 return AddFunction(pFunc);
661 bool CForward::RemoveFunction(IPluginContext *pContext, funcid_t index)
663 IPluginFunction *pFunc = pContext->GetFunctionById(index);
670 return RemoveFunction(pFunc);
673 bool CForward::RemoveFunction(IPluginFunction *func)
677 List<IPluginFunction *> *lst;
679 if (func->IsRunnable())
686 for (iter=m_functions.begin(); iter!=m_functions.end(); iter++)
690 m_IterGuard->FixIteratorChain(iter);
697 /* Cancel a call, if any */
698 if (found || m_curparam)
706 unsigned int CForward::RemoveFunctionsOfPlugin(IPlugin *plugin)
709 IPluginFunction *func;
710 unsigned int removed = 0;
711 IPluginContext *pContext = plugin->GetBaseContext();
712 for (iter=m_functions.begin(); iter!=m_functions.end();)
715 if (func->GetParentContext() == pContext)
717 iter = m_functions.erase(iter);
727 bool CForward::AddFunction(IPluginFunction *func)
734 if (func->IsRunnable())
736 m_functions.push_back(func);
738 m_paused.push_back(func);
744 const char *CForward::GetForwardName()
749 unsigned int CForward::GetFunctionCount()
751 return m_functions.size();
754 ExecType CForward::GetExecType()