1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/core/CConVarManager.cpp Thu Feb 08 21:41:28 2007 +0000
1.3 @@ -0,0 +1,160 @@
1.4 +/**
1.5 +* ===============================================================
1.6 +* SourceMod (C)2004-2007 AlliedModders LLC. All rights reserved.
1.7 +* ===============================================================
1.8 +*
1.9 +* This file is not open source and may not be copied without explicit
1.10 +* written permission of AlliedModders LLC. This file may not be redistributed
1.11 +* in whole or significant part.
1.12 +* For information, see LICENSE.txt or http://www.sourcemod.net/license.php
1.13 +*
1.14 +* Version: $Id$
1.15 +*/
1.16 +
1.17 +#include "CConVarManager.h"
1.18 +#include "CLogger.h"
1.19 +#include "PluginSys.h"
1.20 +#include "sm_srvcmds.h"
1.21 +
1.22 +CConVarManager g_ConVarManager;
1.23 +
1.24 +CConVarManager::CConVarManager()
1.25 +{
1.26 + m_ConVarType = 0;
1.27 + m_CvarCache = sm_trie_create();
1.28 +}
1.29 +
1.30 +CConVarManager::~CConVarManager()
1.31 +{
1.32 + sm_trie_destroy(m_CvarCache);
1.33 +}
1.34 +
1.35 +void CConVarManager::OnSourceModAllInitialized()
1.36 +{
1.37 + HandleAccess sec;
1.38 +
1.39 + sec.access[HandleAccess_Read] = 0;
1.40 + sec.access[HandleAccess_Delete] = HANDLE_RESTRICT_IDENTITY|HANDLE_RESTRICT_OWNER;
1.41 + sec.access[HandleAccess_Clone] = HANDLE_RESTRICT_IDENTITY|HANDLE_RESTRICT_OWNER;
1.42 +
1.43 + m_ConVarType = g_HandleSys.CreateType("ConVar", this, 0, NULL, &sec, g_pCoreIdent, NULL);
1.44 +
1.45 + g_RootMenu.AddRootConsoleCommand("cvars", "View convars associated with a plugin", this);
1.46 +}
1.47 +
1.48 +void CConVarManager::OnSourceModShutdown()
1.49 +{
1.50 + g_RootMenu.RemoveRootConsoleCommand("cvars", this);
1.51 +
1.52 + g_HandleSys.RemoveType(m_ConVarType, g_pCoreIdent);
1.53 +}
1.54 +
1.55 +void CConVarManager::OnHandleDestroy(HandleType_t type, void *object)
1.56 +{
1.57 + g_SMAPI->UnregisterConCmdBase(g_PLAPI, static_cast<ConCommandBase *>(object));
1.58 +}
1.59 +
1.60 +void CConVarManager::OnRootConsoleCommand(const char *command, unsigned int argcount)
1.61 +{
1.62 + if (argcount >= 3)
1.63 + {
1.64 + int id = 1;
1.65 +
1.66 + int num = atoi(g_RootMenu.GetArgument(2));
1.67 +
1.68 + if (num < 1 || num > (int)g_PluginSys.GetPluginCount())
1.69 + {
1.70 + g_RootMenu.ConsolePrint("[SM] Plugin index not found.");
1.71 + return;
1.72 + }
1.73 +
1.74 + CPlugin *pl = g_PluginSys.GetPluginByOrder(num);
1.75 + int convarnum = pl->GetConVarCount();
1.76 +
1.77 + if (convarnum == 0)
1.78 + {
1.79 + g_RootMenu.ConsolePrint("[SM] No convars for \"%s\"", pl->GetPublicInfo()->name);
1.80 + return;
1.81 + }
1.82 +
1.83 + g_RootMenu.ConsolePrint("[SM] Displaying convars for \"%s\":", pl->GetPublicInfo()->name);
1.84 +
1.85 + for (int i = 0; i < convarnum; i++, id++)
1.86 + {
1.87 + ConVar *cvar = pl->GetConVarByIndex(i);
1.88 + g_RootMenu.ConsolePrint(" %02d \"%s\" = \"%s\"", id, cvar->GetName(), cvar->GetString());
1.89 + }
1.90 +
1.91 + return;
1.92 + }
1.93 +
1.94 + g_RootMenu.ConsolePrint("[SM] Usage: sm cvars <#>");
1.95 +}
1.96 +
1.97 +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)
1.98 +{
1.99 + ConVar *cvar = NULL;
1.100 + void *temp = NULL;
1.101 + Handle_t hndl = 0;
1.102 +
1.103 + // Find out if the convar exists already
1.104 + cvar = icvar->FindVar(name);
1.105 +
1.106 + // If the convar already exists...
1.107 + if (cvar != NULL)
1.108 + {
1.109 + IPlugin *pl = g_PluginSys.FindPluginByContext(pContext->GetContext());
1.110 +
1.111 + // This isn't a fatal error because we can handle it, but user should be warned anyways
1.112 + g_Logger.LogError("[SM] Warning: Plugin \"%s\" has attempted to create already existing convar \"%s\"", pl->GetFilename(), name);
1.113 +
1.114 + // First check if we already have a handle to it
1.115 + if (sm_trie_retrieve(m_CvarCache, name, &temp))
1.116 + {
1.117 + // If we do, then return that handle
1.118 + return reinterpret_cast<Handle_t>(temp);
1.119 + } else {
1.120 + // If we don't, then create a new handle from the convar and return it
1.121 + return g_HandleSys.CreateHandle(m_ConVarType, cvar, NULL, g_pCoreIdent, NULL);
1.122 + }
1.123 + }
1.124 +
1.125 + // Since we didn't find an existing convar, now we can create it
1.126 + cvar = new ConVar(name, defaultVal, flags, helpText, hasMin, min, hasMax, max);
1.127 +
1.128 + // Add new convar to plugin's list
1.129 + g_PluginSys.GetPluginByCtx(pContext->GetContext())->AddConVar(cvar);
1.130 +
1.131 + // Create a new handle from the convar
1.132 + hndl = g_HandleSys.CreateHandle(m_ConVarType, cvar, NULL, g_pCoreIdent, NULL);
1.133 +
1.134 + // Insert the handle into our cache
1.135 + sm_trie_insert(m_CvarCache, name, reinterpret_cast<void *>(hndl));
1.136 +
1.137 + return hndl;
1.138 +}
1.139 +
1.140 +Handle_t CConVarManager::FindConVar(const char *name)
1.141 +{
1.142 + ConVar *cvar = NULL;
1.143 + void *temp = NULL;
1.144 +
1.145 + // Search for convar
1.146 + cvar = icvar->FindVar(name);
1.147 +
1.148 + // If it doesn't exist, then return an invalid handle
1.149 + if (cvar == NULL)
1.150 + {
1.151 + return BAD_HANDLE;
1.152 + }
1.153 +
1.154 + // At this point, convar exists, so find out if we already have a handle for it
1.155 + if (sm_trie_retrieve(m_CvarCache, name, &temp))
1.156 + {
1.157 + // If we do, then return that handle
1.158 + return reinterpret_cast<Handle_t>(temp);
1.159 + }
1.160 +
1.161 + // If we don't, then create a new handle from the convar and return it
1.162 + return g_HandleSys.CreateHandle(m_ConVarType, cvar, NULL, g_pCoreIdent, NULL);
1.163 +}
1.164 \ No newline at end of file