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