ds@457: /** ds@457: * =============================================================== ds@457: * SourceMod (C)2004-2007 AlliedModders LLC. All rights reserved. ds@457: * =============================================================== ds@457: * ds@457: * This file is not open source and may not be copied without explicit ds@457: * written permission of AlliedModders LLC. This file may not be redistributed ds@457: * in whole or significant part. ds@457: * For information, see LICENSE.txt or http://www.sourcemod.net/license.php ds@457: * ds@457: * Version: $Id$ ds@457: */ ds@457: ds@457: #include "CConVarManager.h" ds@457: #include "CLogger.h" ds@457: #include "PluginSys.h" ds@457: #include "sm_srvcmds.h" ds@457: ds@457: CConVarManager g_ConVarManager; ds@457: ds@457: CConVarManager::CConVarManager() ds@457: { ds@457: m_ConVarType = 0; ds@457: m_CvarCache = sm_trie_create(); ds@457: } ds@457: ds@457: CConVarManager::~CConVarManager() ds@457: { ds@457: sm_trie_destroy(m_CvarCache); ds@457: } ds@457: ds@457: void CConVarManager::OnSourceModAllInitialized() ds@457: { ds@457: HandleAccess sec; ds@457: ds@457: sec.access[HandleAccess_Read] = 0; ds@457: sec.access[HandleAccess_Delete] = HANDLE_RESTRICT_IDENTITY|HANDLE_RESTRICT_OWNER; ds@457: sec.access[HandleAccess_Clone] = HANDLE_RESTRICT_IDENTITY|HANDLE_RESTRICT_OWNER; ds@457: ds@457: m_ConVarType = g_HandleSys.CreateType("ConVar", this, 0, NULL, &sec, g_pCoreIdent, NULL); ds@457: ds@457: g_RootMenu.AddRootConsoleCommand("cvars", "View convars associated with a plugin", this); ds@457: } ds@457: ds@457: void CConVarManager::OnSourceModShutdown() ds@457: { ds@457: g_RootMenu.RemoveRootConsoleCommand("cvars", this); ds@457: ds@457: g_HandleSys.RemoveType(m_ConVarType, g_pCoreIdent); ds@457: } ds@457: ds@457: void CConVarManager::OnHandleDestroy(HandleType_t type, void *object) ds@457: { ds@457: g_SMAPI->UnregisterConCmdBase(g_PLAPI, static_cast(object)); ds@457: } ds@457: ds@457: void CConVarManager::OnRootConsoleCommand(const char *command, unsigned int argcount) ds@457: { ds@457: if (argcount >= 3) ds@457: { ds@457: int id = 1; ds@457: ds@457: int num = atoi(g_RootMenu.GetArgument(2)); ds@457: ds@457: if (num < 1 || num > (int)g_PluginSys.GetPluginCount()) ds@457: { ds@457: g_RootMenu.ConsolePrint("[SM] Plugin index not found."); ds@457: return; ds@457: } ds@457: ds@457: CPlugin *pl = g_PluginSys.GetPluginByOrder(num); ds@457: int convarnum = pl->GetConVarCount(); ds@457: ds@457: if (convarnum == 0) ds@457: { ds@457: g_RootMenu.ConsolePrint("[SM] No convars for \"%s\"", pl->GetPublicInfo()->name); ds@457: return; ds@457: } ds@457: ds@457: g_RootMenu.ConsolePrint("[SM] Displaying convars for \"%s\":", pl->GetPublicInfo()->name); ds@457: ds@457: for (int i = 0; i < convarnum; i++, id++) ds@457: { ds@457: ConVar *cvar = pl->GetConVarByIndex(i); ds@457: g_RootMenu.ConsolePrint(" %02d \"%s\" = \"%s\"", id, cvar->GetName(), cvar->GetString()); ds@457: } ds@457: ds@457: return; ds@457: } ds@457: ds@457: g_RootMenu.ConsolePrint("[SM] Usage: sm cvars <#>"); ds@457: } ds@457: ds@457: 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) ds@457: { ds@457: ConVar *cvar = NULL; ds@457: void *temp = NULL; ds@457: Handle_t hndl = 0; ds@457: ds@457: // Find out if the convar exists already ds@457: cvar = icvar->FindVar(name); ds@457: ds@457: // If the convar already exists... ds@457: if (cvar != NULL) ds@457: { ds@457: IPlugin *pl = g_PluginSys.FindPluginByContext(pContext->GetContext()); ds@457: ds@457: // This isn't a fatal error because we can handle it, but user should be warned anyways ds@457: g_Logger.LogError("[SM] Warning: Plugin \"%s\" has attempted to create already existing convar \"%s\"", pl->GetFilename(), name); ds@457: ds@457: // First check if we already have a handle to it ds@457: if (sm_trie_retrieve(m_CvarCache, name, &temp)) ds@457: { ds@457: // If we do, then return that handle ds@457: return reinterpret_cast(temp); ds@457: } else { ds@457: // If we don't, then create a new handle from the convar and return it ds@457: return g_HandleSys.CreateHandle(m_ConVarType, cvar, NULL, g_pCoreIdent, NULL); ds@457: } ds@457: } ds@457: ds@457: // Since we didn't find an existing convar, now we can create it ds@457: cvar = new ConVar(name, defaultVal, flags, helpText, hasMin, min, hasMax, max); ds@457: ds@457: // Add new convar to plugin's list ds@457: g_PluginSys.GetPluginByCtx(pContext->GetContext())->AddConVar(cvar); ds@457: ds@457: // Create a new handle from the convar ds@457: hndl = g_HandleSys.CreateHandle(m_ConVarType, cvar, NULL, g_pCoreIdent, NULL); ds@457: ds@457: // Insert the handle into our cache ds@457: sm_trie_insert(m_CvarCache, name, reinterpret_cast(hndl)); ds@457: ds@457: return hndl; ds@457: } ds@457: ds@457: Handle_t CConVarManager::FindConVar(const char *name) ds@457: { ds@457: ConVar *cvar = NULL; ds@457: void *temp = NULL; ds@457: ds@457: // Search for convar ds@457: cvar = icvar->FindVar(name); ds@457: ds@457: // If it doesn't exist, then return an invalid handle ds@457: if (cvar == NULL) ds@457: { ds@457: return BAD_HANDLE; ds@457: } ds@457: ds@457: // At this point, convar exists, so find out if we already have a handle for it ds@457: if (sm_trie_retrieve(m_CvarCache, name, &temp)) ds@457: { ds@457: // If we do, then return that handle ds@457: return reinterpret_cast(temp); ds@457: } ds@457: ds@457: // If we don't, then create a new handle from the convar and return it ds@457: return g_HandleSys.CreateHandle(m_ConVarType, cvar, NULL, g_pCoreIdent, NULL); ds@457: }