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@465: #include "sm_stringutil.h" ds@457: ds@457: CConVarManager g_ConVarManager; ds@457: ds@465: CConVarManager::CConVarManager() : m_ConVarType(0) ds@457: { ds@465: // Create a convar lookup trie ds@465: m_ConVarCache = sm_trie_create(); ds@457: } ds@457: ds@457: CConVarManager::~CConVarManager() ds@457: { ds@465: List::iterator i; ds@465: ds@465: // Destroy our convar lookup trie ds@465: sm_trie_destroy(m_ConVarCache); ds@465: ds@465: // Destroy all the ConVarInfo structures ds@465: for (i = m_ConVars.begin(); i != m_ConVars.end(); i++) ds@465: { ds@465: delete (*i); ds@465: } ds@465: ds@465: m_ConVars.clear(); ds@457: } ds@457: ds@457: void CConVarManager::OnSourceModAllInitialized() ds@457: { ds@457: HandleAccess sec; ds@457: ds@465: // Set up access rights for the 'ConVar' handle type ds@457: sec.access[HandleAccess_Read] = 0; ds@465: sec.access[HandleAccess_Delete] = HANDLE_RESTRICT_IDENTITY | HANDLE_RESTRICT_OWNER; ds@465: sec.access[HandleAccess_Clone] = HANDLE_RESTRICT_IDENTITY | HANDLE_RESTRICT_OWNER; ds@457: ds@465: // Create the 'ConVar' handle type ds@457: m_ConVarType = g_HandleSys.CreateType("ConVar", this, 0, NULL, &sec, g_pCoreIdent, NULL); ds@457: ds@465: // Add the 'cvars' option to the 'sm' console command ds@465: g_RootMenu.AddRootConsoleCommand("cvars", "View convars created by a plugin", this); ds@457: } ds@457: ds@457: void CConVarManager::OnSourceModShutdown() ds@457: { ds@465: IChangeableForward *fwd; ds@465: List::iterator i; ds@465: ds@465: // Iterate list of ConVarInfo structures ds@465: for (i = m_ConVars.begin(); i != m_ConVars.end(); i++) ds@465: { ds@465: fwd = (*i)->changeForward; ds@465: ds@465: // Free any convar-change forwards that still exist ds@465: if (fwd) ds@465: { ds@465: g_Forwards.ReleaseForward(fwd); ds@465: } ds@465: } ds@465: ds@465: // Remove the 'cvars' option from the 'sm' console command ds@457: g_RootMenu.RemoveRootConsoleCommand("cvars", this); ds@457: ds@465: // Remove the 'ConVar' handle type 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@465: ConVarInfo *info; ds@465: ConCommandBase *cvar = static_cast(object); ds@465: ds@465: // Find convar in lookup trie ds@465: sm_trie_retrieve(m_ConVarCache, cvar->GetName(), reinterpret_cast(&info)); ds@465: ds@465: // If convar was created by SourceMod plugin... ds@465: if (info->sourceMod) ds@465: { ds@465: // Then unregister it ds@465: g_SMAPI->UnregisterConCmdBase(g_PLAPI, cvar); ds@465: } 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@465: // Get plugin index that was passed ds@457: int num = atoi(g_RootMenu.GetArgument(2)); ds@457: ds@465: // If invalid plugin index... 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@465: // Get plugin object ds@457: CPlugin *pl = g_PluginSys.GetPluginByOrder(num); ds@465: ds@465: // Get number of convars created by plugin 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@465: // Iterate convar list and display each one 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@465: // Display usage of subcommand 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@465: ConVarInfo *info = 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: // First check if we already have a handle to it ds@465: if (sm_trie_retrieve(m_ConVarCache, name, reinterpret_cast(&info))) ds@457: { ds@457: // If we do, then return that handle ds@465: return info->handle; ds@457: } else { ds@465: // If we don't, then create a new handle from the convar ds@465: hndl = g_HandleSys.CreateHandle(m_ConVarType, cvar, NULL, g_pCoreIdent, NULL); ds@465: ds@465: info = new ConVarInfo; ds@465: info->handle = hndl; ds@465: info->sourceMod = false; ds@465: info->changeForward = NULL; ds@465: info->origCallback = cvar->GetCallback(); ds@465: ds@465: m_ConVars.push_back(info); ds@465: ds@465: return hndl; ds@457: } ds@457: } ds@457: ds@465: // To prevent creating a convar that has the same name as a console command... ugh ds@465: ConCommandBase *pBase = icvar->GetCommands(); ds@465: ds@465: while (pBase) ds@465: { ds@465: if (strcmp(pBase->GetName(), name) == 0) ds@465: { ds@465: return pContext->ThrowNativeError("Convar \"%s\" was not created. A console command with the same name already exists.", name); ds@465: } ds@465: ds@465: pBase = const_cast(pBase->GetNext()); ds@465: } ds@465: ds@465: // Since we didn't find an existing convar (or concmd with the same name), now we can finally 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@465: // Create a handle from the new convar ds@457: hndl = g_HandleSys.CreateHandle(m_ConVarType, cvar, NULL, g_pCoreIdent, NULL); ds@457: ds@465: info = new ConVarInfo; ds@465: info->handle = hndl; ds@465: info->sourceMod = true; ds@465: info->changeForward = NULL; ds@465: info->origCallback = NULL; ds@465: ds@465: m_ConVars.push_back(info); ds@465: ds@465: // Insert the handle into our lookup trie ds@465: sm_trie_insert(m_ConVarCache, name, info); 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@465: ConVarInfo *info = NULL; ds@465: Handle_t hndl = 0; 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@465: if (sm_trie_retrieve(m_ConVarCache, name, reinterpret_cast(&info))) ds@457: { ds@457: // If we do, then return that handle ds@465: return info->handle; ds@465: } ds@465: ds@465: // If we don't, then create a new handle from the convar ds@465: hndl = g_HandleSys.CreateHandle(m_ConVarType, cvar, NULL, g_pCoreIdent, NULL); ds@465: ds@465: info = new ConVarInfo; ds@465: info->handle = hndl; ds@465: info->sourceMod = false; ds@465: info->changeForward = NULL; ds@465: info->origCallback = cvar->GetCallback(); ds@465: ds@465: m_ConVars.push_back(info); ds@465: ds@465: // Insert the handle into our cache ds@465: sm_trie_insert(m_ConVarCache, name, info); ds@465: ds@465: return hndl; ds@465: } ds@465: ds@465: void CConVarManager::HookConVarChange(IPluginContext *pContext, ConVar *cvar, funcid_t funcid) ds@465: { ds@465: IPluginFunction *func = pContext->GetFunctionById(funcid); ds@465: IChangeableForward *fwd = NULL; ds@465: char fwdName[64]; ds@465: ConVarInfo *info = NULL; ds@465: ds@465: // This shouldn't happen... ds@465: if (func == NULL) ds@465: { ds@465: pContext->ThrowNativeError("Invalid function: %d", funcid); ds@465: return; ds@465: } ds@465: ds@465: // Create a forward name ds@465: UTIL_Format(fwdName, sizeof(fwdName), "ConVar.%s", cvar->GetName()); ds@465: ds@465: // First find out if the forward already exists ds@465: g_Forwards.FindForward(fwdName, &fwd); ds@465: ds@465: // If the forward doesn't exist... ds@465: if (fwd == NULL) ds@465: { ds@465: // This is the forward's parameter type list ds@465: ParamType p[] = {Param_Cell, Param_String, Param_String}; ds@465: ds@465: // Create the forward ds@465: fwd = g_Forwards.CreateForwardEx(fwdName, ET_Ignore, 3, p); ds@465: ds@465: // Find the convar in the lookup trie ds@465: if (sm_trie_retrieve(m_ConVarCache, cvar->GetName(), reinterpret_cast(&info))) ds@465: { ds@465: // Set the convar's forward to the newly created one ds@465: info->changeForward = fwd; ds@465: ds@465: // Set the convar's callback to our static one ds@465: cvar->InstallChangeCallback(OnConVarChanged); ds@465: } ds@457: } ds@457: ds@465: // Add the function to the forward's list ds@465: fwd->AddFunction(func); ds@465: } ds@465: ds@465: void CConVarManager::UnhookConVarChange(IPluginContext *pContext, ConVar *cvar, funcid_t funcid) ds@465: { ds@465: IPluginFunction *func = pContext->GetFunctionById(funcid); ds@465: IChangeableForward *fwd = NULL; ds@465: ConVarInfo *info = NULL; ds@465: ds@465: // This shouldn't happen... ds@465: if (func == NULL) ds@465: { ds@465: pContext->ThrowNativeError("Invalid function: %d", funcid); ds@465: return; ds@465: } ds@465: ds@465: // Find the convar in the lookup trie ds@465: if (sm_trie_retrieve(m_ConVarCache, cvar->GetName(), reinterpret_cast(&info))) ds@465: { ds@465: // Get the forward ds@465: fwd = info->changeForward; ds@465: ds@465: // If the forward doesn't exist, we can't unhook anything ds@465: if (fwd == NULL) ds@465: { ds@465: pContext->ThrowNativeError("Convar \"%s\" has no active hook.", cvar->GetName()); ds@465: return; ds@465: } ds@465: ds@465: // Remove the function from the forward's list ds@465: if (!fwd->RemoveFunction(func)) ds@465: { ds@465: pContext->ThrowNativeError("Function %d is not a valid hook callback for convar \"%s\"", funcid, cvar->GetName()); ds@465: return; ds@465: } ds@465: ds@465: // If the forward now has 0 functions in it... ds@465: if (fwd->GetFunctionCount() == 0) ds@465: { ds@465: // Free this forward ds@465: g_Forwards.ReleaseForward(fwd); ds@465: info->changeForward = NULL; ds@465: ds@465: // Put the back the original convar callback ds@465: cvar->InstallChangeCallback(info->origCallback); ds@465: } ds@465: } ds@465: } ds@465: ds@465: void CConVarManager::OnConVarChanged(ConVar *cvar, const char *oldValue) ds@465: { ds@465: Trie *cache = g_ConVarManager.GetConVarCache(); ds@465: ConVarInfo *info; ds@465: ds@465: // Find the convar in the lookup trie ds@465: sm_trie_retrieve(cache, cvar->GetName(), reinterpret_cast(&info)); ds@465: ds@465: FnChangeCallback origCallback = info->origCallback; ds@465: IChangeableForward *fwd = info->changeForward; ds@465: ds@465: // If there was a change callback installed previously, call it ds@465: if (origCallback) ds@465: { ds@465: origCallback(cvar, oldValue); ds@465: } ds@465: ds@465: // Now call forwards in plugins that have hooked this ds@465: fwd->PushCell(info->handle); ds@465: fwd->PushString(cvar->GetString()); ds@465: fwd->PushString(oldValue); ds@465: fwd->Execute(NULL); ds@457: }