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 "CConVarManager.h"
16 #include "PluginSys.h"
17 #include "sm_srvcmds.h"
18 #include "sm_stringutil.h"
20 CConVarManager g_ConVarManager;
22 CConVarManager::CConVarManager() : m_ConVarType(0)
24 // Create a convar lookup trie
25 m_ConVarCache = sm_trie_create();
28 CConVarManager::~CConVarManager()
30 List<ConVarInfo *>::iterator i;
32 // Destroy our convar lookup trie
33 sm_trie_destroy(m_ConVarCache);
35 // Destroy all the ConVarInfo structures
36 for (i = m_ConVars.begin(); i != m_ConVars.end(); i++)
44 void CConVarManager::OnSourceModAllInitialized()
48 // Set up access rights for the 'ConVar' handle type
49 sec.access[HandleAccess_Read] = 0;
50 sec.access[HandleAccess_Delete] = HANDLE_RESTRICT_IDENTITY | HANDLE_RESTRICT_OWNER;
51 sec.access[HandleAccess_Clone] = HANDLE_RESTRICT_IDENTITY | HANDLE_RESTRICT_OWNER;
53 // Create the 'ConVar' handle type
54 m_ConVarType = g_HandleSys.CreateType("ConVar", this, 0, NULL, &sec, g_pCoreIdent, NULL);
56 // Add the 'cvars' option to the 'sm' console command
57 g_RootMenu.AddRootConsoleCommand("cvars", "View convars created by a plugin", this);
60 void CConVarManager::OnSourceModShutdown()
62 IChangeableForward *fwd;
63 List<ConVarInfo *>::iterator i;
65 // Iterate list of ConVarInfo structures
66 for (i = m_ConVars.begin(); i != m_ConVars.end(); i++)
68 fwd = (*i)->changeForward;
70 // Free any convar-change forwards that still exist
73 g_Forwards.ReleaseForward(fwd);
77 // Remove the 'cvars' option from the 'sm' console command
78 g_RootMenu.RemoveRootConsoleCommand("cvars", this);
80 // Remove the 'ConVar' handle type
81 g_HandleSys.RemoveType(m_ConVarType, g_pCoreIdent);
84 void CConVarManager::OnHandleDestroy(HandleType_t type, void *object)
87 ConCommandBase *cvar = static_cast<ConCommandBase *>(object);
89 // Find convar in lookup trie
90 sm_trie_retrieve(m_ConVarCache, cvar->GetName(), reinterpret_cast<void **>(&info));
92 // If convar was created by SourceMod plugin...
96 g_SMAPI->UnregisterConCmdBase(g_PLAPI, cvar);
100 void CConVarManager::OnRootConsoleCommand(const char *command, unsigned int argcount)
106 // Get plugin index that was passed
107 int num = atoi(g_RootMenu.GetArgument(2));
109 // If invalid plugin index...
110 if (num < 1 || num > (int)g_PluginSys.GetPluginCount())
112 g_RootMenu.ConsolePrint("[SM] Plugin index not found.");
117 CPlugin *pl = g_PluginSys.GetPluginByOrder(num);
119 // Get number of convars created by plugin
120 int convarnum = pl->GetConVarCount();
124 g_RootMenu.ConsolePrint("[SM] No convars for \"%s\"", pl->GetPublicInfo()->name);
128 g_RootMenu.ConsolePrint("[SM] Displaying convars for \"%s\":", pl->GetPublicInfo()->name);
130 // Iterate convar list and display each one
131 for (int i = 0; i < convarnum; i++, id++)
133 ConVar *cvar = pl->GetConVarByIndex(i);
134 g_RootMenu.ConsolePrint(" %02d \"%s\" = \"%s\"", id, cvar->GetName(), cvar->GetString());
140 // Display usage of subcommand
141 g_RootMenu.ConsolePrint("[SM] Usage: sm cvars <#>");
144 Handle_t CConVarManager::CreateConVar(IPluginContext *pContext, const char *name, const char *defaultVal, const char *helpText, int flags, bool hasMin, float min, bool hasMax, float max)
147 ConVarInfo *info = NULL;
150 // Find out if the convar exists already
151 cvar = icvar->FindVar(name);
153 // If the convar already exists...
156 // First check if we already have a handle to it
157 if (sm_trie_retrieve(m_ConVarCache, name, reinterpret_cast<void **>(&info)))
159 // If we do, then return that handle
162 // If we don't, then create a new handle from the convar
163 hndl = g_HandleSys.CreateHandle(m_ConVarType, cvar, NULL, g_pCoreIdent, NULL);
165 info = new ConVarInfo;
167 info->sourceMod = false;
168 info->changeForward = NULL;
169 info->origCallback = cvar->GetCallback();
171 m_ConVars.push_back(info);
177 // To prevent creating a convar that has the same name as a console command... ugh
178 ConCommandBase *pBase = icvar->GetCommands();
182 if (strcmp(pBase->GetName(), name) == 0)
184 return pContext->ThrowNativeError("Convar \"%s\" was not created. A console command with the same name already exists.", name);
187 pBase = const_cast<ConCommandBase *>(pBase->GetNext());
190 // Since we didn't find an existing convar (or concmd with the same name), now we can finally create it!
191 cvar = new ConVar(name, defaultVal, flags, helpText, hasMin, min, hasMax, max);
193 // Add new convar to plugin's list
194 g_PluginSys.GetPluginByCtx(pContext->GetContext())->AddConVar(cvar);
196 // Create a handle from the new convar
197 hndl = g_HandleSys.CreateHandle(m_ConVarType, cvar, NULL, g_pCoreIdent, NULL);
199 info = new ConVarInfo;
201 info->sourceMod = true;
202 info->changeForward = NULL;
203 info->origCallback = NULL;
205 m_ConVars.push_back(info);
207 // Insert the handle into our lookup trie
208 sm_trie_insert(m_ConVarCache, name, info);
213 Handle_t CConVarManager::FindConVar(const char *name)
216 ConVarInfo *info = NULL;
220 cvar = icvar->FindVar(name);
222 // If it doesn't exist, then return an invalid handle
228 // At this point, convar exists, so find out if we already have a handle for it
229 if (sm_trie_retrieve(m_ConVarCache, name, reinterpret_cast<void **>(&info)))
231 // If we do, then return that handle
235 // If we don't, then create a new handle from the convar
236 hndl = g_HandleSys.CreateHandle(m_ConVarType, cvar, NULL, g_pCoreIdent, NULL);
238 info = new ConVarInfo;
240 info->sourceMod = false;
241 info->changeForward = NULL;
242 info->origCallback = cvar->GetCallback();
244 m_ConVars.push_back(info);
246 // Insert the handle into our cache
247 sm_trie_insert(m_ConVarCache, name, info);
252 void CConVarManager::HookConVarChange(IPluginContext *pContext, ConVar *cvar, funcid_t funcid)
254 IPluginFunction *func = pContext->GetFunctionById(funcid);
255 IChangeableForward *fwd = NULL;
257 ConVarInfo *info = NULL;
259 // This shouldn't happen...
262 pContext->ThrowNativeError("Invalid function: %d", funcid);
266 // Create a forward name
267 UTIL_Format(fwdName, sizeof(fwdName), "ConVar.%s", cvar->GetName());
269 // First find out if the forward already exists
270 g_Forwards.FindForward(fwdName, &fwd);
272 // If the forward doesn't exist...
275 // This is the forward's parameter type list
276 ParamType p[] = {Param_Cell, Param_String, Param_String};
278 // Create the forward
279 fwd = g_Forwards.CreateForwardEx(fwdName, ET_Ignore, 3, p);
281 // Find the convar in the lookup trie
282 if (sm_trie_retrieve(m_ConVarCache, cvar->GetName(), reinterpret_cast<void **>(&info)))
284 // Set the convar's forward to the newly created one
285 info->changeForward = fwd;
287 // Set the convar's callback to our static one
288 cvar->InstallChangeCallback(OnConVarChanged);
292 // Add the function to the forward's list
293 fwd->AddFunction(func);
296 void CConVarManager::UnhookConVarChange(IPluginContext *pContext, ConVar *cvar, funcid_t funcid)
298 IPluginFunction *func = pContext->GetFunctionById(funcid);
299 IChangeableForward *fwd = NULL;
300 ConVarInfo *info = NULL;
302 // This shouldn't happen...
305 pContext->ThrowNativeError("Invalid function: %d", funcid);
309 // Find the convar in the lookup trie
310 if (sm_trie_retrieve(m_ConVarCache, cvar->GetName(), reinterpret_cast<void **>(&info)))
313 fwd = info->changeForward;
315 // If the forward doesn't exist, we can't unhook anything
318 pContext->ThrowNativeError("Convar \"%s\" has no active hook.", cvar->GetName());
322 // Remove the function from the forward's list
323 if (!fwd->RemoveFunction(func))
325 pContext->ThrowNativeError("Function %d is not a valid hook callback for convar \"%s\"", funcid, cvar->GetName());
329 // If the forward now has 0 functions in it...
330 if (fwd->GetFunctionCount() == 0)
333 g_Forwards.ReleaseForward(fwd);
334 info->changeForward = NULL;
336 // Put the back the original convar callback
337 cvar->InstallChangeCallback(info->origCallback);
342 void CConVarManager::OnConVarChanged(ConVar *cvar, const char *oldValue)
344 Trie *cache = g_ConVarManager.GetConVarCache();
347 // Find the convar in the lookup trie
348 sm_trie_retrieve(cache, cvar->GetName(), reinterpret_cast<void **>(&info));
350 FnChangeCallback origCallback = info->origCallback;
351 IChangeableForward *fwd = info->changeForward;
353 // If there was a change callback installed previously, call it
356 origCallback(cvar, oldValue);
359 // Now call forwards in plugins that have hooked this
360 fwd->PushCell(info->handle);
361 fwd->PushString(cvar->GetString());
362 fwd->PushString(oldValue);