ds@580: /** ds@580: * =============================================================== ds@580: * SourceMod (C)2004-2007 AlliedModders LLC. All rights reserved. ds@580: * =============================================================== ds@580: * ds@580: * This file is not open source and may not be copied without explicit ds@580: * written permission of AlliedModders LLC. This file may not be redistributed ds@580: * in whole or significant part. ds@580: * For information, see LICENSE.txt or http://www.sourcemod.net/license.php ds@580: * ds@580: * Version: $Id$ ds@580: */ ds@580: ds@580: #include "ConVarManager.h" ds@580: #include "PluginSys.h" ds@580: #include "ForwardSys.h" ds@580: #include "HandleSys.h" ds@580: #include "sm_srvcmds.h" ds@580: #include "sm_stringutil.h" ds@580: #include ds@580: ds@580: ConVarManager g_ConVarManager; ds@580: ds@582: const ParamType CONVARCHANGE_PARAMS[] = {Param_Cell, Param_String, Param_String}; ds@582: typedef List ConVarList; ds@582: ds@580: ConVarManager::ConVarManager() : m_ConVarType(0) ds@580: { ds@582: /* Create a convar lookup trie */ ds@580: m_ConVarCache = sm_trie_create(); ds@580: } ds@580: ds@580: ConVarManager::~ConVarManager() ds@580: { ds@580: List::iterator iter; ds@580: ds@582: /* Destroy our convar lookup trie */ ds@580: sm_trie_destroy(m_ConVarCache); ds@580: ds@582: /* Destroy all the ConVarInfo structures */ ds@580: for (iter = m_ConVars.begin(); iter != m_ConVars.end(); iter++) ds@580: { ds@580: delete (*iter); ds@580: } ds@580: ds@580: m_ConVars.clear(); ds@580: } ds@580: ds@580: void ConVarManager::OnSourceModAllInitialized() ds@580: { ds@580: HandleAccess sec; ds@580: ds@582: /* Set up access rights for the 'ConVar' handle type */ ds@580: sec.access[HandleAccess_Read] = 0; ds@580: sec.access[HandleAccess_Delete] = HANDLE_RESTRICT_IDENTITY | HANDLE_RESTRICT_OWNER; ds@580: sec.access[HandleAccess_Clone] = HANDLE_RESTRICT_IDENTITY | HANDLE_RESTRICT_OWNER; ds@580: ds@582: /* Create the 'ConVar' handle type */ ds@580: m_ConVarType = g_HandleSys.CreateType("ConVar", this, 0, NULL, &sec, g_pCoreIdent, NULL); ds@580: ds@582: /* Add the 'convars' option to the 'sm' console command */ ds@580: g_RootMenu.AddRootConsoleCommand("convars", "View convars created by a plugin", this); ds@580: } ds@580: ds@580: void ConVarManager::OnSourceModShutdown() ds@580: { ds@580: IChangeableForward *fwd; ds@580: List::iterator i; ds@580: ds@582: /* Iterate list of ConVarInfo structures */ ds@580: for (i = m_ConVars.begin(); i != m_ConVars.end(); i++) ds@580: { ds@580: fwd = (*i)->changeForward; ds@580: ds@582: /* Free any convar-change forwards that still exist */ ds@580: if (fwd) ds@580: { ds@580: g_Forwards.ReleaseForward(fwd); ds@580: } ds@580: } ds@580: ds@582: /* Remove the 'convars' option from the 'sm' console command */ ds@580: g_RootMenu.RemoveRootConsoleCommand("convars", this); ds@580: ds@582: /* Remove the 'ConVar' handle type */ ds@580: g_HandleSys.RemoveType(m_ConVarType, g_pCoreIdent); ds@580: } ds@580: ds@582: void ConVarManager::OnPluginUnloaded(IPlugin *plugin) ds@580: { ds@582: ConVarList *pConVarList; ds@580: ds@582: /* If plugin has a convar list, free its memory */ ds@582: if (plugin->GetProperty("ConVarList", (void **)&pConVarList, true)) ds@580: { ds@582: delete pConVarList; ds@580: } ds@580: } ds@580: ds@580: void ConVarManager::OnHandleDestroy(HandleType_t type, void *object) ds@580: { ds@580: ConVarInfo *info; ds@580: ConVar *pConVar = static_cast(object); ds@580: ds@582: /* Find convar in lookup trie */ ds@580: sm_trie_retrieve(m_ConVarCache, pConVar->GetName(), reinterpret_cast(&info)); ds@580: ds@582: /* If convar was created by SourceMod plugin... */ ds@580: if (info->sourceMod) ds@580: { ds@582: /* Delete string allocations */ ds@580: delete [] pConVar->GetName(); ds@580: delete [] pConVar->GetDefault(); ds@580: delete [] pConVar->GetHelpText(); ds@580: ds@582: /* Then unlink it from SourceMM */ ds@580: g_SMAPI->UnregisterConCmdBase(g_PLAPI, pConVar); ds@580: } ds@580: } ds@580: ds@580: void ConVarManager::OnRootConsoleCommand(const char *command, unsigned int argcount) ds@580: { ds@580: if (argcount >= 3) ds@580: { ds@582: /* Get plugin index that was passed */ ds@582: int id = atoi(g_RootMenu.GetArgument(2)); ds@580: ds@582: /* Get plugin object */ ds@582: CPlugin *plugin = g_PluginSys.GetPluginByOrder(id); ds@582: ds@582: if (!plugin) ds@580: { ds@582: g_RootMenu.ConsolePrint("[SM] Plugin index %d not found.", id); ds@580: return; ds@580: } ds@580: ds@582: /* Get plugin name */ ds@582: const sm_plugininfo_t *plinfo = plugin->GetPublicInfo(); ds@582: const char *plname = IS_STR_FILLED(plinfo->name) ? plinfo->name : plugin->GetFilename(); ds@580: ds@582: ConVarList *pConVarList; ds@582: ConVarList::iterator iter; ds@580: ds@582: /* If no convar list... */ ds@582: if (!plugin->GetProperty("ConVarList", (void **)&pConVarList)) ds@580: { ds@582: g_RootMenu.ConsolePrint("[SM] No convars found for: %s", plname); ds@580: return; ds@580: } ds@580: ds@582: g_RootMenu.ConsolePrint("[SM] Listing %d convars for: %s", pConVarList->size(), plname); ds@582: g_RootMenu.ConsolePrint(" %-32.31s %s", "[Name]", "[Value]"); ds@580: ds@582: /* Iterate convar list and display each one */ ds@582: for (iter = pConVarList->begin(); iter != pConVarList->end(); iter++) ds@580: { ds@582: const ConVar *pConVar = (*iter); ds@582: g_RootMenu.ConsolePrint(" %-32.31s %s", pConVar->GetName(), pConVar->GetString()); ds@580: } ds@580: ds@580: return; ds@580: } ds@580: ds@582: /* Display usage of subcommand */ ds@580: g_RootMenu.ConsolePrint("[SM] Usage: sm convars "); ds@580: } ds@580: ds@582: 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) ds@580: { ds@580: ConVar *pConVar = NULL; ds@582: ConVarInfo *pInfo = NULL; ds@580: Handle_t hndl = 0; ds@580: ds@582: /* Find out if the convar exists already */ ds@580: pConVar = icvar->FindVar(name); ds@580: ds@582: /* If the convar already exists... */ ds@582: if (pConVar) ds@580: { ds@582: /* First find out if we already have a handle to it */ ds@582: if (sm_trie_retrieve(m_ConVarCache, name, (void **)&pInfo)) ds@580: { ds@582: return pInfo->handle; ds@580: } else { ds@582: /* If we don't, then create a new handle from the convar */ ds@580: hndl = g_HandleSys.CreateHandle(m_ConVarType, pConVar, NULL, g_pCoreIdent, NULL); ds@580: ds@582: /* Create and initialize ConVarInfo structure */ ds@582: pInfo = new ConVarInfo(); ds@582: pInfo->handle = hndl; ds@582: pInfo->sourceMod = false; ds@582: pInfo->changeForward = NULL; ds@582: pInfo->origCallback = pConVar->GetCallback(); ds@580: ds@582: /* Insert struct into caches */ ds@582: m_ConVars.push_back(pInfo); ds@582: sm_trie_insert(m_ConVarCache, name, pInfo); ds@580: ds@580: return hndl; ds@580: } ds@580: } ds@580: ds@580: // To prevent creating a convar that has the same name as a console command... ugh ds@580: ConCommandBase *pBase = icvar->GetCommands(); ds@580: ds@580: while (pBase) ds@580: { ds@582: if (pBase->IsCommand() && strcmp(pBase->GetName(), name) == 0) ds@580: { ds@582: return BAD_HANDLE; ds@580: } ds@580: ds@580: pBase = const_cast(pBase->GetNext()); ds@580: } ds@580: ds@582: /* Since an existing convar (or concmd with the same name) was not found , now we can finally create it */ ds@582: pConVar = new ConVar(sm_strdup(name), sm_strdup(defaultVal), flags, sm_strdup(description), hasMin, min, hasMax, max); ds@580: ds@582: /* Add convar to plugin's list */ ds@582: AddConVarToPluginList(pContext, pConVar); ds@580: ds@582: /* Create a handle from the new convar */ ds@580: hndl = g_HandleSys.CreateHandle(m_ConVarType, pConVar, NULL, g_pCoreIdent, NULL); ds@580: ds@582: /* Create and initialize ConVarInfo structure */ ds@582: pInfo = new ConVarInfo(); ds@582: pInfo->handle = hndl; ds@582: pInfo->sourceMod = true; ds@582: pInfo->changeForward = NULL; ds@582: pInfo->origCallback = NULL; ds@580: ds@582: /* Insert struct into caches */ ds@582: m_ConVars.push_back(pInfo); ds@582: sm_trie_insert(m_ConVarCache, name, pInfo); ds@580: ds@580: return hndl; ds@580: } ds@580: ds@580: Handle_t ConVarManager::FindConVar(const char *name) ds@580: { ds@580: ConVar *pConVar = NULL; ds@582: ConVarInfo *pInfo; ds@582: Handle_t hndl; ds@580: ds@582: /* Search for convar */ ds@580: pConVar = icvar->FindVar(name); ds@580: ds@582: /* If it doesn't exist, then return an invalid handle */ ds@582: if (!pConVar) ds@580: { ds@580: return BAD_HANDLE; ds@580: } ds@580: ds@582: /* At this point, the convar exists. So, find out if we already have a handle */ ds@582: if (sm_trie_retrieve(m_ConVarCache, name, (void **)&pInfo)) ds@580: { ds@582: return pInfo->handle; ds@580: } ds@580: ds@582: /* If we don't have a handle, then create a new one */ ds@580: hndl = g_HandleSys.CreateHandle(m_ConVarType, pConVar, NULL, g_pCoreIdent, NULL); ds@580: ds@582: /* Create and initilize ConVarInfo structure */ ds@582: pInfo = new ConVarInfo(); ds@582: pInfo->handle = hndl; ds@582: pInfo->sourceMod = false; ds@582: pInfo->changeForward = NULL; ds@582: pInfo->origCallback = pConVar->GetCallback(); ds@580: ds@582: /* Insert struct into our caches */ ds@582: m_ConVars.push_back(pInfo); ds@582: sm_trie_insert(m_ConVarCache, name, pInfo); ds@580: ds@580: return hndl; ds@580: } ds@580: ds@582: void ConVarManager::HookConVarChange(ConVar *pConVar, IPluginFunction *pFunction) ds@580: { ds@582: ConVarInfo *pInfo; ds@582: IChangeableForward *pForward; ds@580: ds@582: /* Find the convar in the lookup trie */ ds@582: if (sm_trie_retrieve(m_ConVarCache, pConVar->GetName(), (void **)&pInfo)) ds@580: { ds@582: /* Get the forward */ ds@582: pForward = pInfo->changeForward; ds@580: ds@582: /* If forward does not exist, create it */ ds@582: if (!pForward) ds@582: { ds@582: pForward = g_Forwards.CreateForwardEx(NULL, ET_Ignore, 3, CONVARCHANGE_PARAMS); ds@582: pInfo->changeForward = pForward; ds@580: ds@582: /* Install our own callback */ ds@580: pConVar->InstallChangeCallback(OnConVarChanged); ds@580: } ds@582: ds@582: /* Add function to forward's list */ ds@582: pForward->AddFunction(pFunction); ds@580: } ds@580: } ds@580: ds@582: void ConVarManager::UnhookConVarChange(ConVar *pConVar, IPluginFunction *pFunction) ds@580: { ds@582: ConVarInfo *pInfo; ds@582: IChangeableForward *pForward; ds@582: IPluginContext *pContext = pFunction->GetParentContext(); ds@580: ds@582: /* Find the convar in the lookup trie */ ds@582: if (sm_trie_retrieve(m_ConVarCache, pConVar->GetName(), (void **)&pInfo)) ds@580: { ds@582: /* Get the forward */ ds@582: pForward = pInfo->changeForward; ds@580: ds@582: /* If the forward doesn't exist, we can't unhook anything */ ds@582: if (!pForward) ds@580: { ds@580: pContext->ThrowNativeError("Convar \"%s\" has no active hook", pConVar->GetName()); ds@580: return; ds@580: } ds@580: ds@582: /* Remove the function from the forward's list */ ds@582: if (!pForward->RemoveFunction(pFunction)) ds@580: { ds@580: pContext->ThrowNativeError("Invalid hook callback specified for convar \"%s\"", pConVar->GetName()); ds@580: return; ds@580: } ds@580: ds@582: /* If the forward now has 0 functions in it... */ ds@582: if (pForward->GetFunctionCount() == 0) ds@580: { ds@582: /* Free this forward */ ds@582: g_Forwards.ReleaseForward(pForward); ds@582: pInfo->changeForward = NULL; ds@582: ds@582: /* Put back the original convar callback */ ds@582: pConVar->InstallChangeCallback(pInfo->origCallback); ds@582: } ds@582: } ds@582: } ds@582: ds@582: void ConVarManager::AddConVarToPluginList(IPluginContext *pContext, const ConVar *pConVar) ds@582: { ds@582: ConVarList *pConVarList; ds@582: ConVarList::iterator iter; ds@582: bool inserted = false; ds@582: const char *orig = pConVar->GetName(); ds@582: ds@582: IPlugin *plugin = g_PluginSys.FindPluginByContext(pContext->GetContext()); ds@580: ds@582: /* Check plugin for an existing convar list */ ds@582: if (!plugin->GetProperty("ConVarList", (void **)&pConVarList)) ds@582: { ds@582: pConVarList = new ConVarList(); ds@582: plugin->SetProperty("ConVarList", pConVarList); ds@582: } ds@582: ds@582: /* Insert convar into list which is sorted alphabetically */ ds@582: for (iter = pConVarList->begin(); iter != pConVarList->end(); iter++) ds@582: { ds@582: if (strcmp(orig, (*iter)->GetName()) < 0) ds@582: { ds@582: pConVarList->insert(iter, pConVar); ds@582: inserted = true; ds@582: break; ds@580: } ds@580: } ds@582: ds@582: if (!inserted) ds@582: { ds@582: pConVarList->push_back(pConVar); ds@582: } ds@580: } ds@580: ds@580: void ConVarManager::OnConVarChanged(ConVar *pConVar, const char *oldValue) ds@580: { ds@582: /* If the values are the same, exit early in order to not trigger callbacks */ ds@580: if (strcmp(pConVar->GetString(), oldValue) == 0) ds@580: { ds@580: return; ds@580: } ds@580: ds@582: Trie *pCache = g_ConVarManager.GetConVarCache(); ds@582: ConVarInfo *pInfo; ds@580: ds@582: /* Find the convar in the lookup trie */ ds@582: sm_trie_retrieve(pCache, pConVar->GetName(), (void **)&pInfo); ds@580: ds@582: FnChangeCallback origCallback = pInfo->origCallback; ds@582: IChangeableForward *pForward = pInfo->changeForward; ds@580: ds@582: /* If there was a change callback installed previously, call it */ ds@580: if (origCallback) ds@580: { ds@580: origCallback(pConVar, oldValue); ds@580: } ds@580: ds@582: /* Now call forwards in plugins that have hooked this */ ds@582: pForward->PushCell(pInfo->handle); ds@582: pForward->PushString(oldValue); ds@582: pForward->PushString(pConVar->GetString()); ds@582: pForward->Execute(NULL); ds@580: }