2 * ===============================================================
3 * SourceMod (C)2004-2007 AlliedModders LLC. All rights reserved.
4 * ===============================================================
6 * This file is not open source and may not be copied without explicit
7 * written permission of AlliedModders LLC. This file may not be redistributed
8 * in whole or significant part.
9 * For information, see LICENSE.txt or http://www.sourcemod.net/license.php
14 #include "ConVarManager.h"
15 #include "PluginSys.h"
16 #include "ForwardSys.h"
17 #include "HandleSys.h"
18 #include "sm_srvcmds.h"
19 #include "sm_stringutil.h"
20 #include <sh_vector.h>
22 ConVarManager g_ConVarManager;
24 const ParamType CONVARCHANGE_PARAMS[] = {Param_Cell, Param_String, Param_String};
25 typedef List<const ConVar *> ConVarList;
27 ConVarManager::ConVarManager() : m_ConVarType(0)
29 /* Create a convar lookup trie */
30 m_ConVarCache = sm_trie_create();
33 ConVarManager::~ConVarManager()
35 List<ConVarInfo *>::iterator iter;
37 /* Destroy our convar lookup trie */
38 sm_trie_destroy(m_ConVarCache);
40 /* Destroy all the ConVarInfo structures */
41 for (iter = m_ConVars.begin(); iter != m_ConVars.end(); iter++)
49 void ConVarManager::OnSourceModAllInitialized()
53 /* Set up access rights for the 'ConVar' handle type */
54 sec.access[HandleAccess_Read] = 0;
55 sec.access[HandleAccess_Delete] = HANDLE_RESTRICT_IDENTITY | HANDLE_RESTRICT_OWNER;
56 sec.access[HandleAccess_Clone] = HANDLE_RESTRICT_IDENTITY | HANDLE_RESTRICT_OWNER;
58 /* Create the 'ConVar' handle type */
59 m_ConVarType = g_HandleSys.CreateType("ConVar", this, 0, NULL, &sec, g_pCoreIdent, NULL);
61 /* Add the 'convars' option to the 'sm' console command */
62 g_RootMenu.AddRootConsoleCommand("convars", "View convars created by a plugin", this);
65 void ConVarManager::OnSourceModShutdown()
67 IChangeableForward *fwd;
68 List<ConVarInfo *>::iterator i;
70 /* Iterate list of ConVarInfo structures */
71 for (i = m_ConVars.begin(); i != m_ConVars.end(); i++)
73 fwd = (*i)->changeForward;
75 /* Free any convar-change forwards that still exist */
78 g_Forwards.ReleaseForward(fwd);
82 /* Remove the 'convars' option from the 'sm' console command */
83 g_RootMenu.RemoveRootConsoleCommand("convars", this);
85 /* Remove the 'ConVar' handle type */
86 g_HandleSys.RemoveType(m_ConVarType, g_pCoreIdent);
89 void ConVarManager::OnPluginUnloaded(IPlugin *plugin)
91 ConVarList *pConVarList;
93 /* If plugin has a convar list, free its memory */
94 if (plugin->GetProperty("ConVarList", (void **)&pConVarList, true))
100 void ConVarManager::OnHandleDestroy(HandleType_t type, void *object)
103 ConVar *pConVar = static_cast<ConVar *>(object);
105 /* Find convar in lookup trie */
106 sm_trie_retrieve(m_ConVarCache, pConVar->GetName(), reinterpret_cast<void **>(&info));
108 /* If convar was created by SourceMod plugin... */
111 /* Delete string allocations */
112 delete [] pConVar->GetName();
113 delete [] pConVar->GetDefault();
114 delete [] pConVar->GetHelpText();
116 /* Then unlink it from SourceMM */
117 g_SMAPI->UnregisterConCmdBase(g_PLAPI, pConVar);
121 void ConVarManager::OnRootConsoleCommand(const char *command, unsigned int argcount)
125 /* Get plugin index that was passed */
126 int id = atoi(g_RootMenu.GetArgument(2));
128 /* Get plugin object */
129 CPlugin *plugin = g_PluginSys.GetPluginByOrder(id);
133 g_RootMenu.ConsolePrint("[SM] Plugin index %d not found.", id);
137 /* Get plugin name */
138 const sm_plugininfo_t *plinfo = plugin->GetPublicInfo();
139 const char *plname = IS_STR_FILLED(plinfo->name) ? plinfo->name : plugin->GetFilename();
141 ConVarList *pConVarList;
142 ConVarList::iterator iter;
144 /* If no convar list... */
145 if (!plugin->GetProperty("ConVarList", (void **)&pConVarList))
147 g_RootMenu.ConsolePrint("[SM] No convars found for: %s", plname);
151 g_RootMenu.ConsolePrint("[SM] Listing %d convars for: %s", pConVarList->size(), plname);
152 g_RootMenu.ConsolePrint(" %-32.31s %s", "[Name]", "[Value]");
154 /* Iterate convar list and display each one */
155 for (iter = pConVarList->begin(); iter != pConVarList->end(); iter++)
157 const ConVar *pConVar = (*iter);
158 g_RootMenu.ConsolePrint(" %-32.31s %s", pConVar->GetName(), pConVar->GetString());
164 /* Display usage of subcommand */
165 g_RootMenu.ConsolePrint("[SM] Usage: sm convars <plugin #>");
168 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)
170 ConVar *pConVar = NULL;
171 ConVarInfo *pInfo = NULL;
172 ConVarList *pConVarList = NULL;
175 /* Find out if the convar exists already */
176 pConVar = icvar->FindVar(name);
178 /* If the convar already exists... */
181 /* First find out if we already have a handle to it */
182 if (sm_trie_retrieve(m_ConVarCache, name, (void **)&pInfo))
184 return pInfo->handle;
186 /* If we don't, then create a new handle from the convar */
187 hndl = g_HandleSys.CreateHandle(m_ConVarType, pConVar, NULL, g_pCoreIdent, NULL);
189 /* Create and initialize ConVarInfo structure */
190 pInfo = new ConVarInfo();
191 pInfo->handle = hndl;
192 pInfo->sourceMod = false;
193 pInfo->changeForward = NULL;
194 pInfo->origCallback = pConVar->GetCallback();
196 /* Insert struct into caches */
197 m_ConVars.push_back(pInfo);
198 sm_trie_insert(m_ConVarCache, name, pInfo);
204 // To prevent creating a convar that has the same name as a console command... ugh
205 ConCommandBase *pBase = icvar->GetCommands();
209 if (pBase->IsCommand() && strcmp(pBase->GetName(), name) == 0)
214 pBase = const_cast<ConCommandBase *>(pBase->GetNext());
217 /* Since an existing convar (or concmd with the same name) was not found , now we can finally create it */
218 pConVar = new ConVar(sm_strdup(name), sm_strdup(defaultVal), flags, sm_strdup(description), hasMin, min, hasMax, max);
220 /* Add convar to plugin's list */
221 AddConVarToPluginList(pContext, pConVar);
223 /* Create a handle from the new convar */
224 hndl = g_HandleSys.CreateHandle(m_ConVarType, pConVar, NULL, g_pCoreIdent, NULL);
226 /* Create and initialize ConVarInfo structure */
227 pInfo = new ConVarInfo();
228 pInfo->handle = hndl;
229 pInfo->sourceMod = true;
230 pInfo->changeForward = NULL;
231 pInfo->origCallback = NULL;
233 /* Insert struct into caches */
234 m_ConVars.push_back(pInfo);
235 sm_trie_insert(m_ConVarCache, name, pInfo);
240 Handle_t ConVarManager::FindConVar(const char *name)
242 ConVar *pConVar = NULL;
246 /* Search for convar */
247 pConVar = icvar->FindVar(name);
249 /* If it doesn't exist, then return an invalid handle */
255 /* At this point, the convar exists. So, find out if we already have a handle */
256 if (sm_trie_retrieve(m_ConVarCache, name, (void **)&pInfo))
258 return pInfo->handle;
261 /* If we don't have a handle, then create a new one */
262 hndl = g_HandleSys.CreateHandle(m_ConVarType, pConVar, NULL, g_pCoreIdent, NULL);
264 /* Create and initilize ConVarInfo structure */
265 pInfo = new ConVarInfo();
266 pInfo->handle = hndl;
267 pInfo->sourceMod = false;
268 pInfo->changeForward = NULL;
269 pInfo->origCallback = pConVar->GetCallback();
271 /* Insert struct into our caches */
272 m_ConVars.push_back(pInfo);
273 sm_trie_insert(m_ConVarCache, name, pInfo);
278 void ConVarManager::HookConVarChange(ConVar *pConVar, IPluginFunction *pFunction)
281 IChangeableForward *pForward;
283 /* Find the convar in the lookup trie */
284 if (sm_trie_retrieve(m_ConVarCache, pConVar->GetName(), (void **)&pInfo))
286 /* Get the forward */
287 pForward = pInfo->changeForward;
289 /* If forward does not exist, create it */
292 pForward = g_Forwards.CreateForwardEx(NULL, ET_Ignore, 3, CONVARCHANGE_PARAMS);
293 pInfo->changeForward = pForward;
295 /* Install our own callback */
296 pConVar->InstallChangeCallback(OnConVarChanged);
299 /* Add function to forward's list */
300 pForward->AddFunction(pFunction);
304 void ConVarManager::UnhookConVarChange(ConVar *pConVar, IPluginFunction *pFunction)
307 IChangeableForward *pForward;
308 IPluginContext *pContext = pFunction->GetParentContext();
310 /* Find the convar in the lookup trie */
311 if (sm_trie_retrieve(m_ConVarCache, pConVar->GetName(), (void **)&pInfo))
313 /* Get the forward */
314 pForward = pInfo->changeForward;
316 /* If the forward doesn't exist, we can't unhook anything */
319 pContext->ThrowNativeError("Convar \"%s\" has no active hook", pConVar->GetName());
323 /* Remove the function from the forward's list */
324 if (!pForward->RemoveFunction(pFunction))
326 pContext->ThrowNativeError("Invalid hook callback specified for convar \"%s\"", pConVar->GetName());
330 /* If the forward now has 0 functions in it... */
331 if (pForward->GetFunctionCount() == 0)
333 /* Free this forward */
334 g_Forwards.ReleaseForward(pForward);
335 pInfo->changeForward = NULL;
337 /* Put back the original convar callback */
338 pConVar->InstallChangeCallback(pInfo->origCallback);
343 void ConVarManager::AddConVarToPluginList(IPluginContext *pContext, const ConVar *pConVar)
345 ConVarList *pConVarList;
346 ConVarList::iterator iter;
347 bool inserted = false;
348 const char *orig = pConVar->GetName();
350 IPlugin *plugin = g_PluginSys.FindPluginByContext(pContext->GetContext());
352 /* Check plugin for an existing convar list */
353 if (!plugin->GetProperty("ConVarList", (void **)&pConVarList))
355 pConVarList = new ConVarList();
356 plugin->SetProperty("ConVarList", pConVarList);
359 /* Insert convar into list which is sorted alphabetically */
360 for (iter = pConVarList->begin(); iter != pConVarList->end(); iter++)
362 if (strcmp(orig, (*iter)->GetName()) < 0)
364 pConVarList->insert(iter, pConVar);
372 pConVarList->push_back(pConVar);
376 void ConVarManager::OnConVarChanged(ConVar *pConVar, const char *oldValue)
378 /* If the values are the same, exit early in order to not trigger callbacks */
379 if (strcmp(pConVar->GetString(), oldValue) == 0)
384 Trie *pCache = g_ConVarManager.GetConVarCache();
387 /* Find the convar in the lookup trie */
388 sm_trie_retrieve(pCache, pConVar->GetName(), (void **)&pInfo);
390 FnChangeCallback origCallback = pInfo->origCallback;
391 IChangeableForward *pForward = pInfo->changeForward;
393 /* If there was a change callback installed previously, call it */
396 origCallback(pConVar, oldValue);
399 /* Now call forwards in plugins that have hooked this */
400 pForward->PushCell(pInfo->handle);
401 pForward->PushString(oldValue);
402 pForward->PushString(pConVar->GetString());
403 pForward->Execute(NULL);