Update 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 "sm_srvcmds.h"
33 #include "sm_stringutil.h"
34 #include <sh_vector.h>
35 #include <sm_namehashset.h>
36 #include "logic_bridge.h"
38 ConVarManager g_ConVarManager;
40 #if SOURCE_ENGINE >= SE_ORANGEBOX
41 SH_DECL_HOOK3_void(ICvar, CallGlobalChangeCallbacks, SH_NOATTRIB, false, ConVar *, const char *, float);
43 SH_DECL_HOOK2_void(ICvar, CallGlobalChangeCallback, SH_NOATTRIB, false, ConVar *, const char *);
46 #if SOURCE_ENGINE == SE_DOTA
47 SH_DECL_HOOK5_void(IServerGameDLL, OnQueryCvarValueFinished, SH_NOATTRIB, 0, QueryCvarCookie_t, CEntityIndex, EQueryCvarValueStatus, const char *, const char *);
48 SH_DECL_HOOK5_void(IServerPluginCallbacks, OnQueryCvarValueFinished, SH_NOATTRIB, 0, QueryCvarCookie_t, CEntityIndex, EQueryCvarValueStatus, const char *, const char *);
49 #elif SOURCE_ENGINE != SE_DARKMESSIAH
50 SH_DECL_HOOK5_void(IServerGameDLL, OnQueryCvarValueFinished, SH_NOATTRIB, 0, QueryCvarCookie_t, edict_t *, EQueryCvarValueStatus, const char *, const char *);
51 SH_DECL_HOOK5_void(IServerPluginCallbacks, OnQueryCvarValueFinished, SH_NOATTRIB, 0, QueryCvarCookie_t, edict_t *, EQueryCvarValueStatus, const char *, const char *);
54 const ParamType CONVARCHANGE_PARAMS[] = {Param_Cell, Param_String, Param_String};
55 typedef List<const ConVar *> ConVarList;
56 NameHashSet<ConVarInfo *> convar_cache;
58 class ConVarReentrancyGuard
61 ConVarReentrancyGuard *up;
63 static ConVarReentrancyGuard *chain;
65 ConVarReentrancyGuard(ConVar *cvar)
66 : cvar(cvar), up(chain)
71 ~ConVarReentrancyGuard()
73 assert(chain == this);
77 static bool IsCvarInChain(ConVar *cvar)
79 ConVarReentrancyGuard *guard = chain;
82 if (guard->cvar == cvar)
90 ConVarReentrancyGuard *ConVarReentrancyGuard::chain = NULL;
92 ConVarManager::ConVarManager() : m_ConVarType(0), m_bIsDLLQueryHooked(false), m_bIsVSPQueryHooked(false)
96 ConVarManager::~ConVarManager()
100 void ConVarManager::OnSourceModStartup(bool late)
104 /* Set up access rights for the 'ConVar' handle type */
105 sec.access[HandleAccess_Read] = 0;
106 sec.access[HandleAccess_Delete] = HANDLE_RESTRICT_IDENTITY | HANDLE_RESTRICT_OWNER;
107 sec.access[HandleAccess_Clone] = HANDLE_RESTRICT_IDENTITY | HANDLE_RESTRICT_OWNER;
109 /* Create the 'ConVar' handle type */
110 m_ConVarType = handlesys->CreateType("ConVar", this, 0, NULL, &sec, g_pCoreIdent, NULL);
113 void ConVarManager::OnSourceModAllInitialized()
116 * Episode 2 has this function by default, but the older versions do not.
118 #if SOURCE_ENGINE == SE_EPISODEONE
119 if (g_SMAPI->GetGameDLLVersion() >= 6)
121 SH_ADD_HOOK(IServerGameDLL, OnQueryCvarValueFinished, gamedll, SH_MEMBER(this, &ConVarManager::OnQueryCvarValueFinished), false);
122 m_bIsDLLQueryHooked = true;
126 g_Players.AddClientListener(this);
128 #if SOURCE_ENGINE >= SE_ORANGEBOX
129 SH_ADD_HOOK(ICvar, CallGlobalChangeCallbacks, icvar, SH_STATIC(OnConVarChanged), false);
131 SH_ADD_HOOK(ICvar, CallGlobalChangeCallback, icvar, SH_STATIC(OnConVarChanged), false);
134 scripts->AddPluginsListener(this);
136 /* Add the 'convars' option to the 'sm' console command */
137 g_RootMenu.AddRootConsoleCommand("cvars", "View convars created by a plugin", this);
140 void ConVarManager::OnSourceModShutdown()
142 List<ConVarInfo *>::iterator iter = m_ConVars.begin();
143 HandleSecurity sec(NULL, g_pCoreIdent);
145 /* Iterate list of ConVarInfo structures, remove every one of them */
146 while (iter != m_ConVars.end())
148 ConVarInfo *pInfo = (*iter);
150 iter = m_ConVars.erase(iter);
152 handlesys->FreeHandle(pInfo->handle, &sec);
153 if (pInfo->pChangeForward != NULL)
155 forwardsys->ReleaseForward(pInfo->pChangeForward);
157 if (pInfo->sourceMod)
159 /* If we created it, we won't be tracking it, therefore it is
160 * safe to remove everything in one go.
162 META_UNREGCVAR(pInfo->pVar);
163 delete [] pInfo->pVar->GetName();
164 delete [] pInfo->pVar->GetHelpText();
165 delete [] pInfo->pVar->GetDefault();
170 /* If we didn't create it, we might be tracking it. Also,
171 * it could be unreadable.
173 UntrackConCommandBase(pInfo->pVar, this);
176 /* It's not safe to read the name here, so we simply delete the
177 * the info struct and clear the lookup cache at the end.
181 convar_cache.clear();
183 #if SOURCE_ENGINE != SE_DARKMESSIAH
185 if (m_bIsDLLQueryHooked)
187 SH_REMOVE_HOOK(IServerGameDLL, OnQueryCvarValueFinished, gamedll, SH_MEMBER(this, &ConVarManager::OnQueryCvarValueFinished), false);
188 m_bIsDLLQueryHooked = false;
190 else if (m_bIsVSPQueryHooked)
192 #if SOURCE_ENGINE != SE_DOTA
193 SH_REMOVE_HOOK(IServerPluginCallbacks, OnQueryCvarValueFinished, vsp_interface, SH_MEMBER(this, &ConVarManager::OnQueryCvarValueFinished), false);
195 m_bIsVSPQueryHooked = false;
199 g_Players.RemoveClientListener(this);
201 #if SOURCE_ENGINE >= SE_ORANGEBOX
202 SH_REMOVE_HOOK(ICvar, CallGlobalChangeCallbacks, icvar, SH_STATIC(OnConVarChanged), false);
204 SH_REMOVE_HOOK(ICvar, CallGlobalChangeCallback, icvar, SH_STATIC(OnConVarChanged), false);
207 /* Remove the 'convars' option from the 'sm' console command */
208 g_RootMenu.RemoveRootConsoleCommand("cvars", this);
210 scripts->RemovePluginsListener(this);
212 /* Remove the 'ConVar' handle type */
213 handlesys->RemoveType(m_ConVarType, g_pCoreIdent);
217 * Orange Box will never use this.
219 void ConVarManager::OnSourceModVSPReceived()
222 * Don't bother if the DLL is already hooked.
224 if (m_bIsDLLQueryHooked)
229 /* For later MM:S versions, use the updated API, since it's cleaner. */
230 #if defined METAMOD_PLAPI_VERSION || PLAPI_VERSION >= 11
231 int engine = g_SMAPI->GetSourceEngineBuild();
232 if (engine == SOURCE_ENGINE_ORIGINAL || vsp_version < 2)
237 if (g_HL2.IsOriginalEngine() || vsp_version < 2)
243 #if SOURCE_ENGINE != SE_DARKMESSIAH && SOURCE_ENGINE != SE_DOTA
244 SH_ADD_HOOK(IServerPluginCallbacks, OnQueryCvarValueFinished, vsp_interface, SH_MEMBER(this, &ConVarManager::OnQueryCvarValueFinished), false);
245 m_bIsVSPQueryHooked = true;
249 bool convar_cache_lookup(const char *name, ConVarInfo **pVar)
251 return convar_cache.retrieve(name, pVar);
254 void ConVarManager::OnUnlinkConCommandBase(ConCommandBase *pBase, const char *name, bool is_read_safe)
256 /* Only check convars that have not been created by SourceMod's core */
258 if (!convar_cache_lookup(name, &pInfo))
263 HandleSecurity sec(NULL, g_pCoreIdent);
265 /* Remove it from our cache */
266 m_ConVars.remove(pInfo);
267 convar_cache.remove(name);
269 /* Now make sure no plugins are referring to this pointer */
270 IPluginIterator *pl_iter = scripts->GetPluginIterator();
271 while (pl_iter->MorePlugins())
273 IPlugin *pl = pl_iter->GetPlugin();
275 ConVarList *pConVarList;
276 if (pl->GetProperty("ConVarList", (void **)&pConVarList, true)
277 && pConVarList != NULL)
279 pConVarList->remove(pInfo->pVar);
282 pl_iter->NextPlugin();
286 handlesys->FreeHandle(pInfo->handle, &sec);
290 void ConVarManager::OnPluginUnloaded(IPlugin *plugin)
292 ConVarList *pConVarList;
293 List<ConVarQuery>::iterator iter;
295 /* If plugin has a convar list, free its memory */
296 if (plugin->GetProperty("ConVarList", (void **)&pConVarList, true))
301 const IPluginRuntime * pRuntime = plugin->GetRuntime();
303 /* Remove convar queries for this plugin that haven't returned results yet */
304 for (iter = m_ConVarQueries.begin(); iter != m_ConVarQueries.end();)
306 ConVarQuery &query = (*iter);
307 if (query.pCallback->GetParentRuntime() == pRuntime)
309 iter = m_ConVarQueries.erase(iter);
317 void ConVarManager::OnClientDisconnected(int client)
319 /* Remove convar queries for this client that haven't returned results yet */
320 for (List<ConVarQuery>::iterator iter = m_ConVarQueries.begin(); iter != m_ConVarQueries.end();)
322 ConVarQuery &query = (*iter);
323 if (query.client == client)
325 iter = m_ConVarQueries.erase(iter);
333 void ConVarManager::OnHandleDestroy(HandleType_t type, void *object)
337 bool ConVarManager::GetHandleApproxSize(HandleType_t type, void *object, unsigned int *pSize)
339 *pSize = sizeof(ConVar) + sizeof(ConVarInfo);
343 void ConVarManager::OnRootConsoleCommand(const char *cmdname, const CCommand &command)
345 int argcount = command.ArgC();
348 bool wantReset = false;
350 /* Get plugin index that was passed */
351 const char *arg = command.Arg(2);
352 if (argcount >= 4 && strcmp(arg, "reset") == 0)
355 arg = command.Arg(3);
358 /* Get plugin object */
359 IPlugin *plugin = scripts->FindPluginByConsoleArg(arg);
363 g_RootMenu.ConsolePrint("[SM] Plugin \"%s\" was not found.", arg);
367 /* Get plugin name */
368 const sm_plugininfo_t *plinfo = plugin->GetPublicInfo();
369 const char *plname = IS_STR_FILLED(plinfo->name) ? plinfo->name : plugin->GetFilename();
371 ConVarList *pConVarList;
372 ConVarList::iterator iter;
374 /* If no convar list... */
375 if (!plugin->GetProperty("ConVarList", (void **)&pConVarList))
377 g_RootMenu.ConsolePrint("[SM] No convars found for: %s", plname);
383 g_RootMenu.ConsolePrint("[SM] Listing %d convars for: %s", pConVarList->size(), plname);
384 g_RootMenu.ConsolePrint(" %-32.31s %s", "[Name]", "[Value]");
387 /* Iterate convar list and display/reset each one */
388 for (iter = pConVarList->begin(); iter != pConVarList->end(); iter++)
390 /*const */ConVar *pConVar = const_cast<ConVar *>(*iter);
393 g_RootMenu.ConsolePrint(" %-32.31s %s", pConVar->GetName(), pConVar->GetString());
401 g_RootMenu.ConsolePrint("[SM] Reset %d convars for: %s", pConVarList->size(), plname);
407 /* Display usage of subcommand */
408 g_RootMenu.ConsolePrint("[SM] Usage: sm cvars [reset] <plugin #>");
411 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)
413 ConVar *pConVar = NULL;
414 ConVarInfo *pInfo = NULL;
417 /* Find out if the convar exists already */
418 pConVar = icvar->FindVar(name);
420 /* If the convar already exists... */
423 /* Add convar to plugin's list */
424 AddConVarToPluginList(pContext, pConVar);
426 /* First find out if we already have a handle to it */
427 if (convar_cache_lookup(name, &pInfo))
429 return pInfo->handle;
433 /* Create and initialize ConVarInfo structure */
434 pInfo = new ConVarInfo();
435 pInfo->sourceMod = false;
436 pInfo->pChangeForward = NULL;
437 pInfo->pVar = pConVar;
439 /* If we don't, then create a new handle from the convar */
440 hndl = handlesys->CreateHandle(m_ConVarType, pInfo, NULL, g_pCoreIdent, NULL);
441 if (hndl == BAD_HANDLE)
447 pInfo->handle = hndl;
449 /* Insert struct into caches */
450 m_ConVars.push_back(pInfo);
451 convar_cache.insert(name, pInfo);
452 TrackConCommandBase(pConVar, this);
458 /* Prevent creating a convar that has the same name as a console command */
459 if (FindCommand(name))
464 /* Create and initialize ConVarInfo structure */
465 pInfo = new ConVarInfo();
466 pInfo->handle = hndl;
467 pInfo->sourceMod = true;
468 pInfo->pChangeForward = NULL;
470 /* Create a handle from the new convar */
471 hndl = handlesys->CreateHandle(m_ConVarType, pInfo, NULL, g_pCoreIdent, NULL);
472 if (hndl == BAD_HANDLE)
478 pInfo->handle = hndl;
480 /* Since an existing convar (or concmd with the same name) was not found , now we can finally create it */
481 pConVar = new ConVar(sm_strdup(name), sm_strdup(defaultVal), flags, sm_strdup(description), hasMin, min, hasMax, max);
482 pInfo->pVar = pConVar;
484 /* Add convar to plugin's list */
485 AddConVarToPluginList(pContext, pConVar);
487 /* Insert struct into caches */
488 m_ConVars.push_back(pInfo);
489 convar_cache.insert(name, pInfo);
494 Handle_t ConVarManager::FindConVar(const char *name)
496 ConVar *pConVar = NULL;
500 /* Search for convar */
501 pConVar = icvar->FindVar(name);
503 /* If it doesn't exist, then return an invalid handle */
509 /* At this point, the convar exists. So, find out if we already have a handle */
510 if (convar_cache_lookup(name, &pInfo))
512 return pInfo->handle;
515 /* Create and initialize ConVarInfo structure */
516 pInfo = new ConVarInfo();
517 pInfo->sourceMod = false;
518 pInfo->pChangeForward = NULL;
519 pInfo->pVar = pConVar;
521 /* If we don't have a handle, then create a new one */
522 hndl = handlesys->CreateHandle(m_ConVarType, pInfo, NULL, g_pCoreIdent, NULL);
523 if (hndl == BAD_HANDLE)
529 pInfo->handle = hndl;
531 /* Insert struct into our caches */
532 m_ConVars.push_back(pInfo);
533 convar_cache.insert(name, pInfo);
534 TrackConCommandBase(pConVar, this);
539 void ConVarManager::AddConVarChangeListener(const char *name, IConVarChangeListener *pListener)
543 if (FindConVar(name) == BAD_HANDLE)
548 /* Find the convar in the lookup trie */
549 if (convar_cache_lookup(name, &pInfo))
551 pInfo->changeListeners.push_back(pListener);
555 void ConVarManager::RemoveConVarChangeListener(const char *name, IConVarChangeListener *pListener)
559 /* Find the convar in the lookup trie */
560 if (convar_cache_lookup(name, &pInfo))
562 pInfo->changeListeners.remove(pListener);
566 void ConVarManager::HookConVarChange(ConVar *pConVar, IPluginFunction *pFunction)
569 IChangeableForward *pForward;
571 /* Find the convar in the lookup trie */
572 if (convar_cache_lookup(pConVar->GetName(), &pInfo))
574 /* Get the forward */
575 pForward = pInfo->pChangeForward;
577 /* If forward does not exist, create it */
580 pForward = forwardsys->CreateForwardEx(NULL, ET_Ignore, 3, CONVARCHANGE_PARAMS);
581 pInfo->pChangeForward = pForward;
584 /* Add function to forward's list */
585 pForward->AddFunction(pFunction);
589 void ConVarManager::UnhookConVarChange(ConVar *pConVar, IPluginFunction *pFunction)
592 IChangeableForward *pForward;
593 IPluginContext *pContext = pFunction->GetParentContext();
595 /* Find the convar in the lookup trie */
596 if (convar_cache_lookup(pConVar->GetName(), &pInfo))
598 /* Get the forward */
599 pForward = pInfo->pChangeForward;
601 /* If the forward doesn't exist, we can't unhook anything */
604 pContext->ThrowNativeError("Convar \"%s\" has no active hook", pConVar->GetName());
608 /* Remove the function from the forward's list */
609 if (!pForward->RemoveFunction(pFunction))
611 pContext->ThrowNativeError("Invalid hook callback specified for convar \"%s\"", pConVar->GetName());
615 /* If the forward now has 0 functions in it... */
616 if (pForward->GetFunctionCount() == 0 &&
617 !ConVarReentrancyGuard::IsCvarInChain(pConVar))
619 /* Free this forward */
620 forwardsys->ReleaseForward(pForward);
621 pInfo->pChangeForward = NULL;
626 QueryCvarCookie_t ConVarManager::QueryClientConVar(edict_t *pPlayer, const char *name, IPluginFunction *pCallback, Handle_t hndl)
628 QueryCvarCookie_t cookie = 0;
630 #if SOURCE_ENGINE != SE_DARKMESSIAH
631 /* Call StartQueryCvarValue() in either the IVEngineServer or IServerPluginHelpers depending on situation */
632 if (m_bIsDLLQueryHooked)
634 #if SOURCE_ENGINE == SE_DOTA
635 cookie = engine->StartQueryCvarValue(CEntityIndex(IndexOfEdict(pPlayer)), name);
637 cookie = engine->StartQueryCvarValue(pPlayer, name);
640 #if SOURCE_ENGINE != SE_DOTA
641 else if (m_bIsVSPQueryHooked)
643 cookie = serverpluginhelpers->StartQueryCvarValue(pPlayer, name);
648 return InvalidQueryCvarCookie;
651 ConVarQuery query = {cookie, pCallback, (cell_t)hndl, IndexOfEdict(pPlayer)};
652 m_ConVarQueries.push_back(query);
658 void ConVarManager::AddConVarToPluginList(IPluginContext *pContext, const ConVar *pConVar)
660 ConVarList *pConVarList;
661 ConVarList::iterator iter;
662 bool inserted = false;
663 const char *orig = pConVar->GetName();
665 IPlugin *plugin = scripts->FindPluginByContext(pContext->GetContext());
667 /* Check plugin for an existing convar list */
668 if (!plugin->GetProperty("ConVarList", (void **)&pConVarList))
670 pConVarList = new ConVarList();
671 plugin->SetProperty("ConVarList", pConVarList);
673 else if (pConVarList->find(pConVar) != pConVarList->end())
675 /* If convar is already in list, then don't add it */
679 /* Insert convar into list which is sorted alphabetically */
680 for (iter = pConVarList->begin(); iter != pConVarList->end(); iter++)
682 if (strcmp(orig, (*iter)->GetName()) < 0)
684 pConVarList->insert(iter, pConVar);
692 pConVarList->push_back(pConVar);
696 #if SOURCE_ENGINE >= SE_ORANGEBOX
697 void ConVarManager::OnConVarChanged(ConVar *pConVar, const char *oldValue, float flOldValue)
699 void ConVarManager::OnConVarChanged(ConVar *pConVar, const char *oldValue)
702 /* If the values are the same, exit early in order to not trigger callbacks */
703 if (strcmp(pConVar->GetString(), oldValue) == 0)
710 /* Find the convar in the lookup trie */
711 if (!convar_cache_lookup(pConVar->GetName(), &pInfo))
716 IChangeableForward *pForward = pInfo->pChangeForward;
718 if (pInfo->changeListeners.size() != 0)
720 for (List<IConVarChangeListener *>::iterator i = pInfo->changeListeners.begin();
721 i != pInfo->changeListeners.end();
724 #if SOURCE_ENGINE >= SE_ORANGEBOX
725 (*i)->OnConVarChanged(pConVar, oldValue, flOldValue);
727 (*i)->OnConVarChanged(pConVar, oldValue, atof(oldValue));
732 if (pForward != NULL)
734 ConVarReentrancyGuard guard(pConVar);
736 /* Now call forwards in plugins that have hooked this */
737 pForward->PushCell(pInfo->handle);
738 pForward->PushString(oldValue);
739 pForward->PushString(pConVar->GetString());
740 pForward->Execute(NULL);
744 bool ConVarManager::IsQueryingSupported()
746 return (m_bIsDLLQueryHooked || m_bIsVSPQueryHooked);
749 #if SOURCE_ENGINE != SE_DARKMESSIAH
750 #if SOURCE_ENGINE == SE_DOTA
751 void ConVarManager::OnQueryCvarValueFinished(QueryCvarCookie_t cookie, CEntityIndex player, EQueryCvarValueStatus result, const char *cvarName, const char *cvarValue)
753 void ConVarManager::OnQueryCvarValueFinished(QueryCvarCookie_t cookie, edict_t *pPlayer, EQueryCvarValueStatus result, const char *cvarName, const char *cvarValue)
756 IPluginFunction *pCallback = NULL;
758 List<ConVarQuery>::iterator iter;
760 for (iter = m_ConVarQueries.begin(); iter != m_ConVarQueries.end(); iter++)
762 ConVarQuery &query = (*iter);
763 if (query.cookie == cookie)
765 pCallback = query.pCallback;
775 pCallback->PushCell(cookie);
776 #if SOURCE_ENGINE == SE_DOTA
777 pCallback->PushCell(player.Get());
779 pCallback->PushCell(IndexOfEdict(pPlayer));
781 pCallback->PushCell(result);
782 pCallback->PushString(cvarName);
784 if (result == eQueryCvarValueStatus_ValueIntact)
786 pCallback->PushString(cvarValue);
790 pCallback->PushString("\0");
793 pCallback->PushCell(value);
794 pCallback->Execute(&ret);
796 m_ConVarQueries.erase(iter);
801 HandleError ConVarManager::ReadConVarHandle(Handle_t hndl, ConVar **pVar)
806 if ((error = handlesys->ReadHandle(hndl, m_ConVarType, NULL, (void **)&pInfo)) != HandleError_None)