ds@472: /** ds@472: * =============================================================== ds@472: * SourceMod (C)2004-2007 AlliedModders LLC. All rights reserved. ds@472: * =============================================================== ds@472: * ds@472: * This file is not open source and may not be copied without explicit ds@472: * written permission of AlliedModders LLC. This file may not be redistributed ds@472: * in whole or significant part. ds@472: * For information, see LICENSE.txt or http://www.sourcemod.net/license.php ds@472: * ds@472: * Version: $Id$ ds@472: */ ds@472: ds@472: #include "CConVarManager.h" ds@472: #include "PluginSys.h" ds@487: #include "ForwardSys.h" ds@487: #include "HandleSys.h" ds@472: #include "sm_srvcmds.h" ds@472: #include "sm_stringutil.h" ds@487: #include ds@472: ds@472: CConVarManager g_ConVarManager; ds@472: ds@472: CConVarManager::CConVarManager() : m_ConVarType(0) ds@472: { ds@472: // Create a convar lookup trie ds@472: m_ConVarCache = sm_trie_create(); ds@472: } ds@472: ds@472: CConVarManager::~CConVarManager() ds@472: { ds@472: List::iterator i; ds@472: ds@472: // Destroy our convar lookup trie ds@472: sm_trie_destroy(m_ConVarCache); ds@472: ds@472: // Destroy all the ConVarInfo structures ds@472: for (i = m_ConVars.begin(); i != m_ConVars.end(); i++) ds@472: { ds@472: delete (*i); ds@472: } ds@472: ds@472: m_ConVars.clear(); ds@472: } ds@472: ds@472: void CConVarManager::OnSourceModAllInitialized() ds@472: { ds@472: HandleAccess sec; ds@472: ds@472: // Set up access rights for the 'ConVar' handle type ds@472: sec.access[HandleAccess_Read] = 0; ds@472: sec.access[HandleAccess_Delete] = HANDLE_RESTRICT_IDENTITY | HANDLE_RESTRICT_OWNER; ds@472: sec.access[HandleAccess_Clone] = HANDLE_RESTRICT_IDENTITY | HANDLE_RESTRICT_OWNER; ds@472: ds@472: // Create the 'ConVar' handle type ds@472: m_ConVarType = g_HandleSys.CreateType("ConVar", this, 0, NULL, &sec, g_pCoreIdent, NULL); ds@472: ds@472: // Add the 'cvars' option to the 'sm' console command ds@482: g_RootMenu.AddRootConsoleCommand("convars", "View convars created by a plugin", this); ds@472: } ds@472: ds@472: void CConVarManager::OnSourceModShutdown() ds@472: { ds@472: IChangeableForward *fwd; ds@472: List::iterator i; ds@472: ds@472: // Iterate list of ConVarInfo structures ds@472: for (i = m_ConVars.begin(); i != m_ConVars.end(); i++) ds@472: { ds@472: fwd = (*i)->changeForward; ds@472: ds@472: // Free any convar-change forwards that still exist ds@472: if (fwd) ds@472: { ds@472: g_Forwards.ReleaseForward(fwd); ds@472: } ds@472: } ds@472: ds@472: // Remove the 'cvars' option from the 'sm' console command ds@482: g_RootMenu.RemoveRootConsoleCommand("convars", this); ds@472: ds@472: // Remove the 'ConVar' handle type ds@472: g_HandleSys.RemoveType(m_ConVarType, g_pCoreIdent); ds@472: } ds@472: ds@487: void CConVarManager::OnPluginDestroyed(IPlugin *plugin) ds@487: { ds@487: CVector *cvarList; ds@487: ds@487: // If plugin has a convar list, free its memory ds@487: if (plugin->GetProperty("ConVar", reinterpret_cast(&cvarList), true)) ds@487: { ds@487: delete cvarList; ds@487: } ds@487: } ds@487: ds@472: void CConVarManager::OnHandleDestroy(HandleType_t type, void *object) ds@472: { ds@472: ConVarInfo *info; ds@472: ConCommandBase *cvar = static_cast(object); ds@472: ds@472: // Find convar in lookup trie ds@472: sm_trie_retrieve(m_ConVarCache, cvar->GetName(), reinterpret_cast(&info)); ds@472: ds@472: // If convar was created by SourceMod plugin... ds@472: if (info->sourceMod) ds@472: { ds@472: // Then unregister it ds@472: g_SMAPI->UnregisterConCmdBase(g_PLAPI, cvar); ds@472: } ds@472: } ds@472: ds@472: void CConVarManager::OnRootConsoleCommand(const char *command, unsigned int argcount) ds@472: { ds@472: if (argcount >= 3) ds@472: { ds@472: int id = 1; ds@472: ds@472: // Get plugin index that was passed ds@472: int num = atoi(g_RootMenu.GetArgument(2)); ds@472: ds@472: // If invalid plugin index... ds@472: if (num < 1 || num > (int)g_PluginSys.GetPluginCount()) ds@472: { dvander@499: g_RootMenu.ConsolePrint("[SM] Plugin index %d not found.", num); ds@472: return; ds@472: } ds@472: ds@472: // Get plugin object ds@472: CPlugin *pl = g_PluginSys.GetPluginByOrder(num); ds@472: ds@487: CVector *cvarList = NULL; ds@472: ds@487: // Get convar list from 'ConVar' property ds@487: pl->GetProperty("ConVar", reinterpret_cast(&cvarList)); ds@487: ds@487: // If no cvar list... ds@487: if (cvarList == NULL) ds@472: { ds@472: g_RootMenu.ConsolePrint("[SM] No convars for \"%s\"", pl->GetPublicInfo()->name); ds@472: return; ds@472: } ds@472: ds@482: g_RootMenu.ConsolePrint("[SM] Displaying convars for \"%s\"", pl->GetPublicInfo()->name); ds@472: ds@472: // Iterate convar list and display each one ds@487: for (size_t i = 0; i < cvarList->size(); i++, id++) ds@472: { ds@487: ConVar *cvar = (*cvarList)[i]; ds@472: g_RootMenu.ConsolePrint(" %02d \"%s\" = \"%s\"", id, cvar->GetName(), cvar->GetString()); ds@472: } ds@472: ds@472: return; ds@472: } ds@472: ds@472: // Display usage of subcommand dvander@499: g_RootMenu.ConsolePrint("[SM] Usage: sm convars "); ds@472: } ds@472: ds@472: 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@472: { ds@472: ConVar *cvar = NULL; ds@472: ConVarInfo *info = NULL; ds@487: CVector *cvarList = NULL; ds@472: Handle_t hndl = 0; ds@472: ds@472: // Find out if the convar exists already ds@472: cvar = icvar->FindVar(name); ds@472: ds@472: // If the convar already exists... ds@472: if (cvar != NULL) ds@472: { ds@472: // First check if we already have a handle to it ds@472: if (sm_trie_retrieve(m_ConVarCache, name, reinterpret_cast(&info))) ds@472: { ds@472: // If we do, then return that handle ds@472: return info->handle; ds@472: } else { ds@472: // If we don't, then create a new handle from the convar ds@472: hndl = g_HandleSys.CreateHandle(m_ConVarType, cvar, NULL, g_pCoreIdent, NULL); ds@472: ds@472: info = new ConVarInfo; ds@472: info->handle = hndl; ds@472: info->sourceMod = false; ds@472: info->changeForward = NULL; ds@472: info->origCallback = cvar->GetCallback(); ds@472: ds@472: m_ConVars.push_back(info); ds@472: ds@472: return hndl; ds@472: } ds@472: } ds@472: ds@472: // To prevent creating a convar that has the same name as a console command... ugh ds@472: ConCommandBase *pBase = icvar->GetCommands(); ds@472: ds@472: while (pBase) ds@472: { ds@472: if (strcmp(pBase->GetName(), name) == 0) ds@472: { ds@472: return pContext->ThrowNativeError("Convar \"%s\" was not created. A console command with the same name already exists.", name); ds@472: } ds@472: ds@472: pBase = const_cast(pBase->GetNext()); ds@472: } ds@472: ds@472: // Since we didn't find an existing convar (or concmd with the same name), now we can finally create it! ds@472: cvar = new ConVar(name, defaultVal, flags, helpText, hasMin, min, hasMax, max); ds@472: ds@487: // Find plugin creating convar ds@487: IPlugin *pl = g_PluginSys.FindPluginByContext(pContext->GetContext()); ds@487: ds@487: // Get convar list from 'ConVar' property of plugin ds@487: pl->GetProperty("ConVar", reinterpret_cast(&cvarList)); ds@487: ds@487: // If 'ConVar' property doesn't exist... ds@487: if (cvarList == NULL) ds@487: { ds@487: // Then create it ds@487: cvarList = new CVector; ds@487: pl->SetProperty("ConVar", cvarList); ds@487: } ds@487: ds@472: // Add new convar to plugin's list ds@487: cvarList->push_back(cvar); ds@472: ds@472: // Create a handle from the new convar ds@472: hndl = g_HandleSys.CreateHandle(m_ConVarType, cvar, NULL, g_pCoreIdent, NULL); ds@472: ds@472: info = new ConVarInfo; ds@472: info->handle = hndl; ds@472: info->sourceMod = true; ds@472: info->changeForward = NULL; ds@472: info->origCallback = NULL; ds@472: ds@472: m_ConVars.push_back(info); ds@472: ds@472: // Insert the handle into our lookup trie ds@472: sm_trie_insert(m_ConVarCache, name, info); ds@472: ds@472: return hndl; ds@472: } ds@472: ds@472: Handle_t CConVarManager::FindConVar(const char *name) ds@472: { ds@472: ConVar *cvar = NULL; ds@472: ConVarInfo *info = NULL; ds@472: Handle_t hndl = 0; ds@472: ds@472: // Search for convar ds@472: cvar = icvar->FindVar(name); ds@472: ds@472: // If it doesn't exist, then return an invalid handle ds@472: if (cvar == NULL) ds@472: { ds@472: return BAD_HANDLE; ds@472: } ds@472: ds@472: // At this point, convar exists, so find out if we already have a handle for it ds@472: if (sm_trie_retrieve(m_ConVarCache, name, reinterpret_cast(&info))) ds@472: { ds@472: // If we do, then return that handle ds@472: return info->handle; ds@472: } ds@472: ds@472: // If we don't, then create a new handle from the convar ds@472: hndl = g_HandleSys.CreateHandle(m_ConVarType, cvar, NULL, g_pCoreIdent, NULL); ds@472: ds@472: info = new ConVarInfo; ds@472: info->handle = hndl; ds@472: info->sourceMod = false; ds@472: info->changeForward = NULL; ds@472: info->origCallback = cvar->GetCallback(); ds@472: ds@472: m_ConVars.push_back(info); ds@472: ds@472: // Insert the handle into our cache ds@472: sm_trie_insert(m_ConVarCache, name, info); ds@472: ds@472: return hndl; ds@472: } ds@472: ds@472: void CConVarManager::HookConVarChange(IPluginContext *pContext, ConVar *cvar, funcid_t funcid) ds@472: { ds@472: IPluginFunction *func = pContext->GetFunctionById(funcid); ds@472: IChangeableForward *fwd = NULL; ds@472: char fwdName[64]; ds@472: ConVarInfo *info = NULL; ds@472: ds@472: // This shouldn't happen... ds@472: if (func == NULL) ds@472: { ds@472: pContext->ThrowNativeError("Invalid function specified"); ds@472: return; ds@472: } ds@472: ds@472: // Create a forward name ds@472: UTIL_Format(fwdName, sizeof(fwdName), "ConVar.%s", cvar->GetName()); ds@472: ds@472: // First find out if the forward already exists ds@472: g_Forwards.FindForward(fwdName, &fwd); ds@472: ds@472: // If the forward doesn't exist... ds@472: if (fwd == NULL) ds@472: { ds@472: // This is the forward's parameter type list ds@472: ParamType p[] = {Param_Cell, Param_String, Param_String}; ds@472: ds@472: // Create the forward ds@472: fwd = g_Forwards.CreateForwardEx(fwdName, ET_Ignore, 3, p); ds@472: ds@472: // Find the convar in the lookup trie ds@472: if (sm_trie_retrieve(m_ConVarCache, cvar->GetName(), reinterpret_cast(&info))) ds@472: { ds@472: // Set the convar's forward to the newly created one ds@472: info->changeForward = fwd; ds@472: ds@472: // Set the convar's callback to our static one ds@472: cvar->InstallChangeCallback(OnConVarChanged); ds@472: } ds@472: } ds@472: ds@472: // Add the function to the forward's list ds@472: fwd->AddFunction(func); ds@472: } ds@472: ds@472: void CConVarManager::UnhookConVarChange(IPluginContext *pContext, ConVar *cvar, funcid_t funcid) ds@472: { ds@472: IPluginFunction *func = pContext->GetFunctionById(funcid); ds@472: IChangeableForward *fwd = NULL; ds@472: ConVarInfo *info = NULL; ds@472: ds@472: // This shouldn't happen... ds@472: if (func == NULL) ds@472: { ds@472: pContext->ThrowNativeError("Invalid function specified"); ds@472: return; ds@472: } ds@472: ds@472: // Find the convar in the lookup trie ds@472: if (sm_trie_retrieve(m_ConVarCache, cvar->GetName(), reinterpret_cast(&info))) ds@472: { ds@472: // Get the forward ds@472: fwd = info->changeForward; ds@472: ds@472: // If the forward doesn't exist, we can't unhook anything ds@472: if (fwd == NULL) ds@472: { ds@472: pContext->ThrowNativeError("Convar \"%s\" has no active hook", cvar->GetName()); ds@472: return; ds@472: } ds@472: ds@472: // Remove the function from the forward's list ds@472: if (!fwd->RemoveFunction(func)) ds@472: { ds@472: pContext->ThrowNativeError("Invalid hook callback specified for convar \"%s\"", cvar->GetName()); ds@472: return; ds@472: } ds@472: ds@472: // If the forward now has 0 functions in it... ds@472: if (fwd->GetFunctionCount() == 0) ds@472: { ds@472: // Free this forward ds@472: g_Forwards.ReleaseForward(fwd); ds@472: info->changeForward = NULL; ds@472: ds@472: // Put the back the original convar callback ds@472: cvar->InstallChangeCallback(info->origCallback); ds@472: } ds@472: } ds@472: } ds@472: ds@472: void CConVarManager::OnConVarChanged(ConVar *cvar, const char *oldValue) ds@472: { ds@494: // If the values are the same... ds@494: if (strcmp(cvar->GetString(), oldValue) == 0) ds@494: { ds@494: // Exit early in order to not trigger callbacks ds@494: return; ds@494: } ds@494: ds@472: Trie *cache = g_ConVarManager.GetConVarCache(); ds@472: ConVarInfo *info; ds@472: ds@472: // Find the convar in the lookup trie ds@472: sm_trie_retrieve(cache, cvar->GetName(), reinterpret_cast(&info)); ds@472: ds@472: FnChangeCallback origCallback = info->origCallback; ds@472: IChangeableForward *fwd = info->changeForward; ds@472: ds@472: // If there was a change callback installed previously, call it ds@472: if (origCallback) ds@472: { ds@472: origCallback(cvar, oldValue); ds@472: } ds@472: ds@472: // Now call forwards in plugins that have hooked this ds@472: fwd->PushCell(info->handle); ds@472: fwd->PushString(cvar->GetString()); ds@472: fwd->PushString(oldValue); ds@472: fwd->Execute(NULL); ds@472: }