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"
19 CConVarManager g_ConVarManager;
21 CConVarManager::CConVarManager()
24 m_CvarCache = sm_trie_create();
27 CConVarManager::~CConVarManager()
29 sm_trie_destroy(m_CvarCache);
32 void CConVarManager::OnSourceModAllInitialized()
36 sec.access[HandleAccess_Read] = 0;
37 sec.access[HandleAccess_Delete] = HANDLE_RESTRICT_IDENTITY|HANDLE_RESTRICT_OWNER;
38 sec.access[HandleAccess_Clone] = HANDLE_RESTRICT_IDENTITY|HANDLE_RESTRICT_OWNER;
40 m_ConVarType = g_HandleSys.CreateType("ConVar", this, 0, NULL, &sec, g_pCoreIdent, NULL);
42 g_RootMenu.AddRootConsoleCommand("cvars", "View convars associated with a plugin", this);
45 void CConVarManager::OnSourceModShutdown()
47 g_RootMenu.RemoveRootConsoleCommand("cvars", this);
49 g_HandleSys.RemoveType(m_ConVarType, g_pCoreIdent);
52 void CConVarManager::OnHandleDestroy(HandleType_t type, void *object)
54 g_SMAPI->UnregisterConCmdBase(g_PLAPI, static_cast<ConCommandBase *>(object));
57 void CConVarManager::OnRootConsoleCommand(const char *command, unsigned int argcount)
63 int num = atoi(g_RootMenu.GetArgument(2));
65 if (num < 1 || num > (int)g_PluginSys.GetPluginCount())
67 g_RootMenu.ConsolePrint("[SM] Plugin index not found.");
71 CPlugin *pl = g_PluginSys.GetPluginByOrder(num);
72 int convarnum = pl->GetConVarCount();
76 g_RootMenu.ConsolePrint("[SM] No convars for \"%s\"", pl->GetPublicInfo()->name);
80 g_RootMenu.ConsolePrint("[SM] Displaying convars for \"%s\":", pl->GetPublicInfo()->name);
82 for (int i = 0; i < convarnum; i++, id++)
84 ConVar *cvar = pl->GetConVarByIndex(i);
85 g_RootMenu.ConsolePrint(" %02d \"%s\" = \"%s\"", id, cvar->GetName(), cvar->GetString());
91 g_RootMenu.ConsolePrint("[SM] Usage: sm cvars <#>");
94 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)
100 // Find out if the convar exists already
101 cvar = icvar->FindVar(name);
103 // If the convar already exists...
106 IPlugin *pl = g_PluginSys.FindPluginByContext(pContext->GetContext());
108 // This isn't a fatal error because we can handle it, but user should be warned anyways
109 g_Logger.LogError("[SM] Warning: Plugin \"%s\" has attempted to create already existing convar \"%s\"", pl->GetFilename(), name);
111 // First check if we already have a handle to it
112 if (sm_trie_retrieve(m_CvarCache, name, &temp))
114 // If we do, then return that handle
115 return reinterpret_cast<Handle_t>(temp);
117 // If we don't, then create a new handle from the convar and return it
118 return g_HandleSys.CreateHandle(m_ConVarType, cvar, NULL, g_pCoreIdent, NULL);
122 // Since we didn't find an existing convar, now we can create it
123 cvar = new ConVar(name, defaultVal, flags, helpText, hasMin, min, hasMax, max);
125 // Add new convar to plugin's list
126 g_PluginSys.GetPluginByCtx(pContext->GetContext())->AddConVar(cvar);
128 // Create a new handle from the convar
129 hndl = g_HandleSys.CreateHandle(m_ConVarType, cvar, NULL, g_pCoreIdent, NULL);
131 // Insert the handle into our cache
132 sm_trie_insert(m_CvarCache, name, reinterpret_cast<void *>(hndl));
137 Handle_t CConVarManager::FindConVar(const char *name)
143 cvar = icvar->FindVar(name);
145 // If it doesn't exist, then return an invalid handle
151 // At this point, convar exists, so find out if we already have a handle for it
152 if (sm_trie_retrieve(m_CvarCache, name, &temp))
154 // If we do, then return that handle
155 return reinterpret_cast<Handle_t>(temp);
158 // If we don't, then create a new handle from the convar and return it
159 return g_HandleSys.CreateHandle(m_ConVarType, cvar, NULL, g_pCoreIdent, NULL);