Updated some CS:GO gamedata.
2 * vim: set ts=4 sw=4 tw=99 noet :
3 * =============================================================================
5 * Copyright (C) 2004-2009 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>.
30 #include "ConVarManager.h"
31 #include "HalfLife2.h"
32 #include "ForwardSys.h"
33 #include "sm_srvcmds.h"
34 #include "sm_stringutil.h"
35 #include <sh_vector.h>
36 #include <sm_trie_tpl.h>
37 #include "logic_bridge.h"
39 ConVarManager g_ConVarManager;
41 #if SOURCE_ENGINE >= SE_ORANGEBOX
42 SH_DECL_HOOK3_void(ICvar, CallGlobalChangeCallbacks, SH_NOATTRIB, false, ConVar *, const char *, float);
44 SH_DECL_HOOK2_void(ICvar, CallGlobalChangeCallback, SH_NOATTRIB, false, ConVar *, const char *);
47 #if SOURCE_ENGINE == SE_DOTA
48 SH_DECL_HOOK5_void(IServerGameDLL, OnQueryCvarValueFinished, SH_NOATTRIB, 0, QueryCvarCookie_t, CEntityIndex, EQueryCvarValueStatus, const char *, const char *);
49 SH_DECL_HOOK5_void(IServerPluginCallbacks, OnQueryCvarValueFinished, SH_NOATTRIB, 0, QueryCvarCookie_t, CEntityIndex, EQueryCvarValueStatus, const char *, const char *);
50 #elif SOURCE_ENGINE != SE_DARKMESSIAH
51 SH_DECL_HOOK5_void(IServerGameDLL, OnQueryCvarValueFinished, SH_NOATTRIB, 0, QueryCvarCookie_t, edict_t *, EQueryCvarValueStatus, const char *, const char *);
52 SH_DECL_HOOK5_void(IServerPluginCallbacks, OnQueryCvarValueFinished, SH_NOATTRIB, 0, QueryCvarCookie_t, edict_t *, EQueryCvarValueStatus, const char *, const char *);
55 const ParamType CONVARCHANGE_PARAMS[] = {Param_Cell, Param_String, Param_String};
56 typedef List<const ConVar *> ConVarList;
57 KTrie<ConVarInfo *> convar_cache;
59 class ConVarReentrancyGuard
62 ConVarReentrancyGuard *up;
64 static ConVarReentrancyGuard *chain;
66 ConVarReentrancyGuard(ConVar *cvar)
67 : cvar(cvar), up(chain)
72 ~ConVarReentrancyGuard()
74 assert(chain == this);
78 static bool IsCvarInChain(ConVar *cvar)
80 ConVarReentrancyGuard *guard = chain;
83 if (guard->cvar == cvar)
91 ConVarReentrancyGuard *ConVarReentrancyGuard::chain = NULL;
93 ConVarManager::ConVarManager() : m_ConVarType(0), m_bIsDLLQueryHooked(false), m_bIsVSPQueryHooked(false)
97 ConVarManager::~ConVarManager()
101 void ConVarManager::OnSourceModStartup(bool late)
105 /* Set up access rights for the 'ConVar' handle type */
106 sec.access[HandleAccess_Read] = 0;
107 sec.access[HandleAccess_Delete] = HANDLE_RESTRICT_IDENTITY | HANDLE_RESTRICT_OWNER;
108 sec.access[HandleAccess_Clone] = HANDLE_RESTRICT_IDENTITY | HANDLE_RESTRICT_OWNER;
110 /* Create the 'ConVar' handle type */
111 m_ConVarType = handlesys->CreateType("ConVar", this, 0, NULL, &sec, g_pCoreIdent, NULL);
114 void ConVarManager::OnSourceModAllInitialized()
117 * Episode 2 has this function by default, but the older versions do not.
119 #if SOURCE_ENGINE == SE_EPISODEONE
120 if (g_SMAPI->GetGameDLLVersion() >= 6)
122 SH_ADD_HOOK(IServerGameDLL, OnQueryCvarValueFinished, gamedll, SH_MEMBER(this, &ConVarManager::OnQueryCvarValueFinished), false);
123 m_bIsDLLQueryHooked = true;
127 #if SOURCE_ENGINE >= SE_ORANGEBOX
128 SH_ADD_HOOK(ICvar, CallGlobalChangeCallbacks, icvar, SH_STATIC(OnConVarChanged), false);
130 SH_ADD_HOOK(ICvar, CallGlobalChangeCallback, icvar, SH_STATIC(OnConVarChanged), false);
133 scripts->AddPluginsListener(this);
135 /* Add the 'convars' option to the 'sm' console command */
136 g_RootMenu.AddRootConsoleCommand("cvars", "View convars created by a plugin", this);
139 void ConVarManager::OnSourceModShutdown()
141 List<ConVarInfo *>::iterator iter = m_ConVars.begin();
142 HandleSecurity sec(NULL, g_pCoreIdent);
144 /* Iterate list of ConVarInfo structures, remove every one of them */
145 while (iter != m_ConVars.end())
147 ConVarInfo *pInfo = (*iter);
149 iter = m_ConVars.erase(iter);
151 handlesys->FreeHandle(pInfo->handle, &sec);
152 if (pInfo->pChangeForward != NULL)
154 g_Forwards.ReleaseForward(pInfo->pChangeForward);
156 if (pInfo->sourceMod)
158 /* If we created it, we won't be tracking it, therefore it is
159 * safe to remove everything in one go.
161 META_UNREGCVAR(pInfo->pVar);
162 delete [] pInfo->pVar->GetName();
163 delete [] pInfo->pVar->GetHelpText();
164 delete [] pInfo->pVar->GetDefault();
169 /* If we didn't create it, we might be tracking it. Also,
170 * it could be unreadable.
172 UntrackConCommandBase(pInfo->pVar, this);
175 /* It's not safe to read the name here, so we simply delete the
176 * the info struct and clear the lookup cache at the end.
180 convar_cache.clear();
182 #if SOURCE_ENGINE != SE_DARKMESSIAH
184 if (m_bIsDLLQueryHooked)
186 SH_REMOVE_HOOK(IServerGameDLL, OnQueryCvarValueFinished, gamedll, SH_MEMBER(this, &ConVarManager::OnQueryCvarValueFinished), false);
187 m_bIsDLLQueryHooked = false;
189 else if (m_bIsVSPQueryHooked)
191 #if SOURCE_ENGINE != SE_DOTA
192 SH_REMOVE_HOOK(IServerPluginCallbacks, OnQueryCvarValueFinished, vsp_interface, SH_MEMBER(this, &ConVarManager::OnQueryCvarValueFinished), false);
194 m_bIsVSPQueryHooked = false;
198 #if SOURCE_ENGINE >= SE_ORANGEBOX
199 SH_REMOVE_HOOK(ICvar, CallGlobalChangeCallbacks, icvar, SH_STATIC(OnConVarChanged), false);
201 SH_REMOVE_HOOK(ICvar, CallGlobalChangeCallback, icvar, SH_STATIC(OnConVarChanged), false);
204 /* Remove the 'convars' option from the 'sm' console command */
205 g_RootMenu.RemoveRootConsoleCommand("cvars", this);
207 scripts->RemovePluginsListener(this);
209 /* Remove the 'ConVar' handle type */
210 handlesys->RemoveType(m_ConVarType, g_pCoreIdent);
214 * Orange Box will never use this.
216 void ConVarManager::OnSourceModVSPReceived()
219 * Don't bother if the DLL is already hooked.
221 if (m_bIsDLLQueryHooked)
226 /* For later MM:S versions, use the updated API, since it's cleaner. */
227 #if defined METAMOD_PLAPI_VERSION || PLAPI_VERSION >= 11
228 int engine = g_SMAPI->GetSourceEngineBuild();
229 if (engine == SOURCE_ENGINE_ORIGINAL || vsp_version < 2)
234 if (g_HL2.IsOriginalEngine() || vsp_version < 2)
240 #if SOURCE_ENGINE != SE_DARKMESSIAH && SOURCE_ENGINE != SE_DOTA
241 SH_ADD_HOOK(IServerPluginCallbacks, OnQueryCvarValueFinished, vsp_interface, SH_MEMBER(this, &ConVarManager::OnQueryCvarValueFinished), false);
242 m_bIsVSPQueryHooked = true;
246 bool convar_cache_lookup(const char *name, ConVarInfo **pVar)
248 ConVarInfo **pLookup = convar_cache.retrieve(name);
260 void ConVarManager::OnUnlinkConCommandBase(ConCommandBase *pBase, const char *name, bool is_read_safe)
262 /* Only check convars that have not been created by SourceMod's core */
264 if (!convar_cache_lookup(name, &pInfo))
269 HandleSecurity sec(NULL, g_pCoreIdent);
271 /* Remove it from our cache */
272 m_ConVars.remove(pInfo);
273 convar_cache.remove(name);
275 /* Now make sure no plugins are referring to this pointer */
276 IPluginIterator *pl_iter = scripts->GetPluginIterator();
277 while (pl_iter->MorePlugins())
279 IPlugin *pl = pl_iter->GetPlugin();
281 ConVarList *pConVarList;
282 if (pl->GetProperty("ConVarList", (void **)&pConVarList, true)
283 && pConVarList != NULL)
285 pConVarList->remove(pInfo->pVar);
288 pl_iter->NextPlugin();
292 handlesys->FreeHandle(pInfo->handle, &sec);
296 void ConVarManager::OnPluginUnloaded(IPlugin *plugin)
298 ConVarList *pConVarList;
299 List<ConVarQuery>::iterator iter;
301 /* If plugin has a convar list, free its memory */
302 if (plugin->GetProperty("ConVarList", (void **)&pConVarList, true))
307 const IPluginRuntime * pRuntime = plugin->GetRuntime();
309 /* Remove convar queries for this plugin that haven't returned results yet */
310 for (iter = m_ConVarQueries.begin(); iter != m_ConVarQueries.end();)
312 ConVarQuery &query = (*iter);
313 if (query.pCallback->GetParentRuntime() == pRuntime)
315 iter = m_ConVarQueries.erase(iter);
323 void ConVarManager::OnHandleDestroy(HandleType_t type, void *object)
327 bool ConVarManager::GetHandleApproxSize(HandleType_t type, void *object, unsigned int *pSize)
329 *pSize = sizeof(ConVar) + sizeof(ConVarInfo);
333 void ConVarManager::OnRootConsoleCommand(const char *cmdname, const CCommand &command)
335 int argcount = command.ArgC();
338 bool wantReset = false;
340 /* Get plugin index that was passed */
341 const char *arg = command.Arg(2);
342 if (argcount >= 4 && strcmp(arg, "reset") == 0)
345 arg = command.Arg(3);
348 /* Get plugin object */
349 IPlugin *plugin = scripts->FindPluginByConsoleArg(arg);
353 g_RootMenu.ConsolePrint("[SM] Plugin \"%s\" was not found.", arg);
357 /* Get plugin name */
358 const sm_plugininfo_t *plinfo = plugin->GetPublicInfo();
359 const char *plname = IS_STR_FILLED(plinfo->name) ? plinfo->name : plugin->GetFilename();
361 ConVarList *pConVarList;
362 ConVarList::iterator iter;
364 /* If no convar list... */
365 if (!plugin->GetProperty("ConVarList", (void **)&pConVarList))
367 g_RootMenu.ConsolePrint("[SM] No convars found for: %s", plname);
373 g_RootMenu.ConsolePrint("[SM] Listing %d convars for: %s", pConVarList->size(), plname);
374 g_RootMenu.ConsolePrint(" %-32.31s %s", "[Name]", "[Value]");
377 /* Iterate convar list and display/reset each one */
378 for (iter = pConVarList->begin(); iter != pConVarList->end(); iter++)
380 /*const */ConVar *pConVar = const_cast<ConVar *>(*iter);
383 g_RootMenu.ConsolePrint(" %-32.31s %s", pConVar->GetName(), pConVar->GetString());
391 g_RootMenu.ConsolePrint("[SM] Reset %d convars for: %s", pConVarList->size(), plname);
397 /* Display usage of subcommand */
398 g_RootMenu.ConsolePrint("[SM] Usage: sm cvars [reset] <plugin #>");
401 Handle_t ConVarManager::CreateConVar(IPluginContext *pContext, const char *name, const char *defaultVal, const char *description, int flags, bool hasMin, float min, bool hasMax, float max)
403 ConVar *pConVar = NULL;
404 ConVarInfo *pInfo = NULL;
407 /* Find out if the convar exists already */
408 pConVar = icvar->FindVar(name);
410 /* If the convar already exists... */
413 /* Add convar to plugin's list */
414 AddConVarToPluginList(pContext, pConVar);
416 /* First find out if we already have a handle to it */
417 if (convar_cache_lookup(name, &pInfo))
419 return pInfo->handle;
423 /* Create and initialize ConVarInfo structure */
424 pInfo = new ConVarInfo();
425 pInfo->sourceMod = false;
426 pInfo->pChangeForward = NULL;
427 pInfo->pVar = pConVar;
429 /* If we don't, then create a new handle from the convar */
430 hndl = handlesys->CreateHandle(m_ConVarType, pInfo, NULL, g_pCoreIdent, NULL);
431 if (hndl == BAD_HANDLE)
437 pInfo->handle = hndl;
439 /* Insert struct into caches */
440 m_ConVars.push_back(pInfo);
441 convar_cache.insert(name, pInfo);
442 TrackConCommandBase(pConVar, this);
448 /* Prevent creating a convar that has the same name as a console command */
449 if (FindCommand(name))
454 /* Create and initialize ConVarInfo structure */
455 pInfo = new ConVarInfo();
456 pInfo->handle = hndl;
457 pInfo->sourceMod = true;
458 pInfo->pChangeForward = NULL;
460 /* Create a handle from the new convar */
461 hndl = handlesys->CreateHandle(m_ConVarType, pInfo, NULL, g_pCoreIdent, NULL);
462 if (hndl == BAD_HANDLE)
468 pInfo->handle = hndl;
470 /* Since an existing convar (or concmd with the same name) was not found , now we can finally create it */
471 pConVar = new ConVar(sm_strdup(name), sm_strdup(defaultVal), flags, sm_strdup(description), hasMin, min, hasMax, max);
472 pInfo->pVar = pConVar;
474 /* Add convar to plugin's list */
475 AddConVarToPluginList(pContext, pConVar);
477 /* Insert struct into caches */
478 m_ConVars.push_back(pInfo);
479 convar_cache.insert(name, pInfo);
484 Handle_t ConVarManager::FindConVar(const char *name)
486 ConVar *pConVar = NULL;
490 /* Search for convar */
491 pConVar = icvar->FindVar(name);
493 /* If it doesn't exist, then return an invalid handle */
499 /* At this point, the convar exists. So, find out if we already have a handle */
500 if (convar_cache_lookup(name, &pInfo))
502 return pInfo->handle;
505 /* Create and initialize ConVarInfo structure */
506 pInfo = new ConVarInfo();
507 pInfo->sourceMod = false;
508 pInfo->pChangeForward = NULL;
509 pInfo->pVar = pConVar;
511 /* If we don't have a handle, then create a new one */
512 hndl = handlesys->CreateHandle(m_ConVarType, pInfo, NULL, g_pCoreIdent, NULL);
513 if (hndl == BAD_HANDLE)
519 pInfo->handle = hndl;
521 /* Insert struct into our caches */
522 m_ConVars.push_back(pInfo);
523 convar_cache.insert(name, pInfo);
524 TrackConCommandBase(pConVar, this);
529 void ConVarManager::AddConVarChangeListener(const char *name, IConVarChangeListener *pListener)
533 if (FindConVar(name) == BAD_HANDLE)
538 /* Find the convar in the lookup trie */
539 if (convar_cache_lookup(name, &pInfo))
541 pInfo->changeListeners.push_back(pListener);
545 void ConVarManager::RemoveConVarChangeListener(const char *name, IConVarChangeListener *pListener)
549 /* Find the convar in the lookup trie */
550 if (convar_cache_lookup(name, &pInfo))
552 pInfo->changeListeners.remove(pListener);
556 void ConVarManager::HookConVarChange(ConVar *pConVar, IPluginFunction *pFunction)
559 IChangeableForward *pForward;
561 /* Find the convar in the lookup trie */
562 if (convar_cache_lookup(pConVar->GetName(), &pInfo))
564 /* Get the forward */
565 pForward = pInfo->pChangeForward;
567 /* If forward does not exist, create it */
570 pForward = g_Forwards.CreateForwardEx(NULL, ET_Ignore, 3, CONVARCHANGE_PARAMS);
571 pInfo->pChangeForward = pForward;
574 /* Add function to forward's list */
575 pForward->AddFunction(pFunction);
579 void ConVarManager::UnhookConVarChange(ConVar *pConVar, IPluginFunction *pFunction)
582 IChangeableForward *pForward;
583 IPluginContext *pContext = pFunction->GetParentContext();
585 /* Find the convar in the lookup trie */
586 if (convar_cache_lookup(pConVar->GetName(), &pInfo))
588 /* Get the forward */
589 pForward = pInfo->pChangeForward;
591 /* If the forward doesn't exist, we can't unhook anything */
594 pContext->ThrowNativeError("Convar \"%s\" has no active hook", pConVar->GetName());
598 /* Remove the function from the forward's list */
599 if (!pForward->RemoveFunction(pFunction))
601 pContext->ThrowNativeError("Invalid hook callback specified for convar \"%s\"", pConVar->GetName());
605 /* If the forward now has 0 functions in it... */
606 if (pForward->GetFunctionCount() == 0 &&
607 !ConVarReentrancyGuard::IsCvarInChain(pConVar))
609 /* Free this forward */
610 g_Forwards.ReleaseForward(pForward);
611 pInfo->pChangeForward = NULL;
616 QueryCvarCookie_t ConVarManager::QueryClientConVar(edict_t *pPlayer, const char *name, IPluginFunction *pCallback, Handle_t hndl)
618 QueryCvarCookie_t cookie = 0;
620 #if SOURCE_ENGINE != SE_DARKMESSIAH
621 /* Call StartQueryCvarValue() in either the IVEngineServer or IServerPluginHelpers depending on situation */
622 if (m_bIsDLLQueryHooked)
624 #if SOURCE_ENGINE == SE_DOTA
625 cookie = engine->StartQueryCvarValue(CEntityIndex(IndexOfEdict(pPlayer)), name);
627 cookie = engine->StartQueryCvarValue(pPlayer, name);
630 #if SOURCE_ENGINE != SE_DOTA
631 else if (m_bIsVSPQueryHooked)
633 cookie = serverpluginhelpers->StartQueryCvarValue(pPlayer, name);
638 return InvalidQueryCvarCookie;
641 ConVarQuery query = {cookie, pCallback, hndl};
642 m_ConVarQueries.push_back(query);
648 void ConVarManager::AddConVarToPluginList(IPluginContext *pContext, const ConVar *pConVar)
650 ConVarList *pConVarList;
651 ConVarList::iterator iter;
652 bool inserted = false;
653 const char *orig = pConVar->GetName();
655 IPlugin *plugin = scripts->FindPluginByContext(pContext->GetContext());
657 /* Check plugin for an existing convar list */
658 if (!plugin->GetProperty("ConVarList", (void **)&pConVarList))
660 pConVarList = new ConVarList();
661 plugin->SetProperty("ConVarList", pConVarList);
663 else if (pConVarList->find(pConVar) != pConVarList->end())
665 /* If convar is already in list, then don't add it */
669 /* Insert convar into list which is sorted alphabetically */
670 for (iter = pConVarList->begin(); iter != pConVarList->end(); iter++)
672 if (strcmp(orig, (*iter)->GetName()) < 0)
674 pConVarList->insert(iter, pConVar);
682 pConVarList->push_back(pConVar);
686 #if SOURCE_ENGINE >= SE_ORANGEBOX
687 void ConVarManager::OnConVarChanged(ConVar *pConVar, const char *oldValue, float flOldValue)
689 void ConVarManager::OnConVarChanged(ConVar *pConVar, const char *oldValue)
692 /* If the values are the same, exit early in order to not trigger callbacks */
693 if (strcmp(pConVar->GetString(), oldValue) == 0)
700 /* Find the convar in the lookup trie */
701 if (!convar_cache_lookup(pConVar->GetName(), &pInfo))
706 IChangeableForward *pForward = pInfo->pChangeForward;
708 if (pInfo->changeListeners.size() != 0)
710 for (List<IConVarChangeListener *>::iterator i = pInfo->changeListeners.begin();
711 i != pInfo->changeListeners.end();
714 #if SOURCE_ENGINE >= SE_ORANGEBOX
715 (*i)->OnConVarChanged(pConVar, oldValue, flOldValue);
717 (*i)->OnConVarChanged(pConVar, oldValue, atof(oldValue));
722 if (pForward != NULL)
724 ConVarReentrancyGuard guard(pConVar);
726 /* Now call forwards in plugins that have hooked this */
727 pForward->PushCell(pInfo->handle);
728 pForward->PushString(oldValue);
729 pForward->PushString(pConVar->GetString());
730 pForward->Execute(NULL);
734 bool ConVarManager::IsQueryingSupported()
736 return (m_bIsDLLQueryHooked || m_bIsVSPQueryHooked);
739 #if SOURCE_ENGINE != SE_DARKMESSIAH
740 #if SOURCE_ENGINE == SE_DOTA
741 void ConVarManager::OnQueryCvarValueFinished(QueryCvarCookie_t cookie, CEntityIndex player, EQueryCvarValueStatus result, const char *cvarName, const char *cvarValue)
743 void ConVarManager::OnQueryCvarValueFinished(QueryCvarCookie_t cookie, edict_t *pPlayer, EQueryCvarValueStatus result, const char *cvarName, const char *cvarValue)
746 IPluginFunction *pCallback = NULL;
748 List<ConVarQuery>::iterator iter;
750 for (iter = m_ConVarQueries.begin(); iter != m_ConVarQueries.end(); iter++)
752 ConVarQuery &query = (*iter);
753 if (query.cookie == cookie)
755 pCallback = query.pCallback;
765 pCallback->PushCell(cookie);
766 #if SOURCE_ENGINE == SE_DOTA
767 pCallback->PushCell(player.Get());
769 pCallback->PushCell(IndexOfEdict(pPlayer));
771 pCallback->PushCell(result);
772 pCallback->PushString(cvarName);
774 if (result == eQueryCvarValueStatus_ValueIntact)
776 pCallback->PushString(cvarValue);
780 pCallback->PushString("\0");
783 pCallback->PushCell(value);
784 pCallback->Execute(&ret);
786 m_ConVarQueries.erase(iter);
791 HandleError ConVarManager::ReadConVarHandle(Handle_t hndl, ConVar **pVar)
796 if ((error = handlesys->ReadHandle(hndl, m_ConVarType, NULL, (void **)&pInfo)) != HandleError_None)