Update CS:GO team score gamedata.
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 "DebugReporter.h"
37 #include "common_logic.h"
39 CForwardManager g_Forwards;
42 * Genesis turns to its source, reduction occurs stepwise although the essence is all one.
43 * End of line. FTL system check.
45 * :TODO: WHAT NEEDS TO BE TESTED IN THIS BEAST
47 * - Pushing no varargs
50 // :TODO: IMPORTANT!!! The result pointer arg in the execute function maybe invalid if the forward fails
51 // so later evaluation of this result may cause problems on higher levels of abstraction. DOCUMENT OR FIX ALL FORWARDS!
53 CForwardManager::~CForwardManager()
55 CStack<CForward *>::iterator iter;
56 for (iter=m_FreeForwards.begin(); iter!=m_FreeForwards.end(); iter++)
60 m_FreeForwards.popall();
63 void CForwardManager::OnSourceModAllInitialized()
65 scripts->AddPluginsListener(this);
66 sharesys->AddInterface(NULL, this);
69 IForward *CForwardManager::CreateForward(const char *name, ExecType et, unsigned int num_params, const ParamType *types, ...)
75 fwd = CForward::CreateForward(name, et, num_params, types, ap);
81 scripts->AddFunctionsToForward(name, fwd);
83 m_managed.push_back(fwd);
89 IChangeableForward *CForwardManager::CreateForwardEx(const char *name, ExecType et, int num_params, const ParamType *types, ...)
95 fwd = CForward::CreateForward(name, et, num_params, types, ap);
101 m_unmanaged.push_back(fwd);
107 void CForwardManager::OnPluginLoaded(IPlugin *plugin)
109 /* Attach any globally managed forwards */
110 List<CForward *>::iterator iter;
113 for (iter=m_managed.begin(); iter!=m_managed.end(); iter++)
116 IPluginFunction *pFunc = plugin->GetBaseContext()->GetFunctionByName(fwd->GetForwardName());
119 fwd->AddFunction(pFunc);
124 void CForwardManager::OnPluginUnloaded(IPlugin *plugin)
126 List<CForward *>::iterator iter;
129 for (iter=m_managed.begin(); iter!=m_managed.end(); iter++)
132 fwd->RemoveFunctionsOfPlugin(plugin);
135 for (iter=m_unmanaged.begin(); iter!=m_unmanaged.end(); iter++)
138 fwd->RemoveFunctionsOfPlugin(plugin);
142 IForward *CForwardManager::FindForward(const char *name, IChangeableForward **ifchng)
144 List<CForward *>::iterator iter;
147 for (iter=m_managed.begin(); iter!=m_managed.end(); iter++)
150 if (strcmp(fwd->GetForwardName(), name) == 0)
160 for (iter=m_unmanaged.begin(); iter!=m_unmanaged.end(); iter++)
163 if (strcmp(fwd->GetForwardName(), name) == 0)
181 void CForwardManager::ReleaseForward(IForward *forward)
183 ForwardFree(static_cast<CForward *>(forward));
186 void CForwardManager::ForwardFree(CForward *fwd)
193 m_FreeForwards.push(fwd);
194 m_managed.remove(fwd);
195 m_unmanaged.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 /* Attach any globally managed forwards */
217 List<CForward *>::iterator iter;
220 for (iter=m_managed.begin(); iter!=m_managed.end(); iter++)
223 IPluginFunction *pFunc = plugin->GetBaseContext()->GetFunctionByName(fwd->GetForwardName());
224 // Only add functions, if they aren't registered yet!
225 if (pFunc && !fwd->IsFunctionRegistered(pFunc))
227 fwd->AddFunction(pFunc);
232 /*************************************
233 * ACTUAL FORWARD API IMPLEMENTATION *
234 *************************************/
236 CForward *CForward::CreateForward(const char *name, ExecType et, unsigned int num_params, const ParamType *types, va_list ap)
238 ParamType _types[SP_MAX_EXEC_PARAMS];
240 if (num_params > SP_MAX_EXEC_PARAMS)
245 if (types == NULL && num_params)
247 for (unsigned int i=0; i<num_params; i++)
249 _types[i] = (ParamType)va_arg(ap, int);
250 if (_types[i] == Param_VarArgs && (i != num_params - 1))
257 for (unsigned int i=0; i<num_params; i++)
259 _types[i] = types[i];
260 if (types[i] == Param_VarArgs && (i != num_params - 1))
267 /* First parameter can never be varargs */
268 if (num_params && _types[0] == Param_VarArgs)
273 CForward *pForward = g_Forwards.ForwardMake();
274 pForward->m_IterGuard = NULL;
275 pForward->m_curparam = 0;
276 pForward->m_ExecType = et;
277 snprintf(pForward->m_name, FORWARDS_NAME_MAX, "%s", name ? name : "");
279 for (unsigned int i=0; i<num_params; i++)
281 pForward->m_types[i] = _types[i];
284 if (num_params && _types[num_params-1] == Param_VarArgs)
286 pForward->m_varargs = num_params--;
288 pForward->m_varargs = false;
291 pForward->m_numparams = num_params;
292 pForward->m_errstate = SP_ERROR_NONE;
294 pForward->m_functions.clear();
299 int CForward::Execute(cell_t *result, IForwardFilter *filter)
303 int err = m_errstate;
308 FuncIter iter = m_functions.begin();
309 IPluginFunction *func;
310 cell_t cur_result = 0;
311 cell_t high_result = 0;
312 cell_t low_result = 0;
314 unsigned int success=0;
315 unsigned int num_params = m_curparam;
316 FwdParamInfo temp_info[SP_MAX_EXEC_PARAMS];
320 /* Save local, reset */
321 memcpy(temp_info, m_params, sizeof(m_params));
324 FuncIteratorGuard guard(&m_IterGuard, &iter);
326 while (iter != m_functions.end())
331 filter->Preprocess(func, temp_info);
333 for (unsigned int i=0; i<num_params; i++)
335 int err = SP_ERROR_PARAM;
336 param = &temp_info[i];
338 if (i >= m_numparams || m_types[i] == Param_Any)
340 type = param->pushedas;
347 if ((i >= m_numparams) || (type & SP_PARAMFLAG_BYREF))
349 /* If we're byref or we're vararg, we always push everything by ref.
350 * Even if they're byval, we must push them byref.
352 if (type == Param_String)
354 err = func->PushStringEx((char *)param->byref.orig_addr, param->byref.cells, param->byref.sz_flags, param->byref.flags);
356 else if (type == Param_Float || type == Param_Cell)
358 err = func->PushCellByRef(¶m->val);
362 err = func->PushArray(param->byref.orig_addr, param->byref.cells, param->byref.flags);
363 assert(type == Param_Array || type == Param_FloatByRef || type == Param_CellByRef);
368 /* If we're not byref or not vararg, our job is a bit easier. */
369 assert(type == Param_Cell || type == Param_Float);
370 err = func->PushCell(param->val);
373 if (err != SP_ERROR_NONE)
375 g_DbgReporter.GenerateError(func->GetParentContext(),
376 func->GetFunctionID(),
378 "Failed to push parameter while executing forward");
383 /* Call the function and deal with the return value. */
384 if ((err=func->Execute(&cur_result)) == SP_ERROR_NONE)
391 if (cur_result > high_result)
393 high_result = cur_result;
399 if (cur_result > high_result)
401 high_result = cur_result;
402 if ((ResultType)high_result == Pl_Stop)
411 /* Check if the current result is the lowest so far (or if it's the first result) */
412 if (cur_result < low_result || success == 1)
414 low_result = cur_result;
425 if (!guard.Triggered())
443 cur_result = high_result;
448 cur_result = low_result;
459 *result = cur_result;
463 return SP_ERROR_NONE;
466 int CForward::PushCell(cell_t cell)
468 if (m_curparam < m_numparams)
470 if (m_types[m_curparam] == Param_Any)
472 m_params[m_curparam].pushedas = Param_Cell;
473 } else if (m_types[m_curparam] != Param_Cell) {
474 return SetError(SP_ERROR_PARAM);
477 if (!m_varargs || m_numparams > SP_MAX_EXEC_PARAMS)
479 return SetError(SP_ERROR_PARAMS_MAX);
481 m_params[m_curparam].pushedas = Param_Cell;
484 m_params[m_curparam++].val = cell;
486 return SP_ERROR_NONE;
489 int CForward::PushFloat(float number)
491 if (m_curparam < m_numparams)
493 if (m_types[m_curparam] == Param_Any)
495 m_params[m_curparam].pushedas = Param_Float;
496 } else if (m_types[m_curparam] != Param_Float) {
497 return SetError(SP_ERROR_PARAM);
500 if (!m_varargs || m_numparams > SP_MAX_EXEC_PARAMS)
502 return SetError(SP_ERROR_PARAMS_MAX);
504 m_params[m_curparam].pushedas = Param_Float;
507 m_params[m_curparam++].val = *(cell_t *)&number;
509 return SP_ERROR_NONE;
512 int CForward::PushCellByRef(cell_t *cell, int flags)
514 if (m_curparam < m_numparams)
516 if (m_types[m_curparam] == Param_Any)
518 m_params[m_curparam].pushedas = Param_CellByRef;
519 } else if (m_types[m_curparam] != Param_CellByRef) {
520 return SetError(SP_ERROR_PARAM);
523 if (!m_varargs || m_numparams > SP_MAX_EXEC_PARAMS)
525 return SetError(SP_ERROR_PARAMS_MAX);
527 m_params[m_curparam].pushedas = Param_CellByRef;
530 _Int_PushArray(cell, 1, flags);
533 return SP_ERROR_NONE;
536 int CForward::PushFloatByRef(float *num, int flags)
538 if (m_curparam < m_numparams)
540 if (m_types[m_curparam] == Param_Any)
542 m_params[m_curparam].pushedas = Param_FloatByRef;
543 } else if (m_types[m_curparam] != Param_FloatByRef) {
544 return SetError(SP_ERROR_PARAM);
547 if (!m_varargs || m_numparams > SP_MAX_EXEC_PARAMS)
549 return SetError(SP_ERROR_PARAMS_MAX);
551 m_params[m_curparam].pushedas = Param_FloatByRef;
554 _Int_PushArray((cell_t *)num, 1, flags);
557 return SP_ERROR_NONE;
560 void CForward::_Int_PushArray(cell_t *inarray, unsigned int cells, int flags)
562 m_params[m_curparam].byref.cells = cells;
563 m_params[m_curparam].byref.flags = flags;
564 m_params[m_curparam].byref.orig_addr = inarray;
567 int CForward::PushArray(cell_t *inarray, unsigned int cells, int flags)
569 /* We don't allow this here */
572 return SetError(SP_ERROR_PARAM);
575 if (m_curparam < m_numparams)
577 if (m_types[m_curparam] == Param_Any)
579 m_params[m_curparam].pushedas = Param_Array;
580 } else if (m_types[m_curparam] != Param_Array) {
581 return SetError(SP_ERROR_PARAM);
584 if (!m_varargs || m_curparam > SP_MAX_EXEC_PARAMS)
586 return SetError(SP_ERROR_PARAMS_MAX);
588 m_params[m_curparam].pushedas = Param_Array;
591 _Int_PushArray(inarray, cells, flags);
595 return SP_ERROR_NONE;
598 void CForward::_Int_PushString(cell_t *inarray, unsigned int cells, int sz_flags, int cp_flags)
600 m_params[m_curparam].byref.cells = cells; /* Notice this contains the char count not cell count */
601 m_params[m_curparam].byref.flags = cp_flags;
602 m_params[m_curparam].byref.orig_addr = inarray;
603 m_params[m_curparam].byref.sz_flags = sz_flags;
606 int CForward::PushString(const char *string)
608 if (m_curparam < m_numparams)
610 if (m_types[m_curparam] == Param_Any)
612 m_params[m_curparam].pushedas = Param_String;
613 } else if (m_types[m_curparam] != Param_String) {
614 return SetError(SP_ERROR_PARAM);
617 if (!m_varargs || m_curparam > SP_MAX_EXEC_PARAMS)
619 return SetError(SP_ERROR_PARAMS_MAX);
621 m_params[m_curparam].pushedas = Param_String;
624 _Int_PushString((cell_t *)string, strlen(string)+1, SM_PARAM_STRING_COPY, 0);
627 return SP_ERROR_NONE;
630 int CForward::PushStringEx(char *buffer, size_t length, int sz_flags, int cp_flags)
632 if (m_curparam < m_numparams)
634 if (m_types[m_curparam] == Param_Any)
636 m_params[m_curparam].pushedas = Param_String;
637 } else if (m_types[m_curparam] != Param_String) {
638 return SetError(SP_ERROR_PARAM);
641 if (!m_varargs || m_curparam > SP_MAX_EXEC_PARAMS)
643 return SetError(SP_ERROR_PARAMS_MAX);
645 m_params[m_curparam].pushedas = Param_String;
648 _Int_PushString((cell_t *)buffer, length, sz_flags, cp_flags);
651 return SP_ERROR_NONE;
654 void CForward::Cancel()
662 m_errstate = SP_ERROR_NONE;
665 bool CForward::AddFunction(IPluginContext *pContext, funcid_t index)
667 IPluginFunction *pFunc = pContext->GetFunctionById(index);
674 return AddFunction(pFunc);
677 bool CForward::RemoveFunction(IPluginContext *pContext, funcid_t index)
679 IPluginFunction *pFunc = pContext->GetFunctionById(index);
686 return RemoveFunction(pFunc);
689 bool CForward::RemoveFunction(IPluginFunction *func)
693 List<IPluginFunction *> *lst;
695 if (func->IsRunnable())
702 for (iter=lst->begin(); iter!=lst->end(); iter++)
706 m_IterGuard->FixIteratorChain(iter);
713 /* Cancel a call, if any */
714 if (found || m_curparam)
722 unsigned int CForward::RemoveFunctionsOfPlugin(IPlugin *plugin)
725 IPluginFunction *func;
726 unsigned int removed = 0;
727 IPluginContext *pContext = plugin->GetBaseContext();
728 for (iter=m_functions.begin(); iter!=m_functions.end();)
731 if (func->GetParentContext() == pContext)
733 iter = m_functions.erase(iter);
743 bool CForward::AddFunction(IPluginFunction *func)
750 if (func->IsRunnable())
752 m_functions.push_back(func);
754 m_paused.push_back(func);
760 bool CForward::IsFunctionRegistered(IPluginFunction *func)
763 List<IPluginFunction *> *lst;
765 if (func->IsRunnable())
772 for (iter=m_functions.begin(); iter!=m_functions.end(); iter++)
782 const char *CForward::GetForwardName()
787 unsigned int CForward::GetFunctionCount()
789 return m_functions.size();
792 ExecType CForward::GetExecType()