core/ConVarManager.cpp
changeset 696 d5cc8d0fa899
parent 653 ef60870067ff
child 729 110c14c99545
     1.1 --- a/core/ConVarManager.cpp	Sat Apr 14 04:19:58 2007 +0000
     1.2 +++ b/core/ConVarManager.cpp	Sat Apr 14 04:27:47 2007 +0000
     1.3 @@ -13,6 +13,7 @@
     1.4   */
     1.5  
     1.6  #include "ConVarManager.h"
     1.7 +#include "HalfLife2.h"
     1.8  #include "PluginSys.h"
     1.9  #include "ForwardSys.h"
    1.10  #include "HandleSys.h"
    1.11 @@ -22,10 +23,13 @@
    1.12  
    1.13  ConVarManager g_ConVarManager;
    1.14  
    1.15 +SH_DECL_HOOK5_void(IServerGameDLL, OnQueryCvarValueFinished, SH_NOATTRIB, 0, QueryCvarCookie_t, edict_t *, EQueryCvarValueStatus, const char *, const char *);
    1.16 +SH_DECL_HOOK5_void(IServerPluginCallbacks, OnQueryCvarValueFinished, SH_NOATTRIB, 0, QueryCvarCookie_t, edict_t *, EQueryCvarValueStatus, const char *, const char *);
    1.17 +
    1.18  const ParamType CONVARCHANGE_PARAMS[] = {Param_Cell, Param_String, Param_String};
    1.19  typedef List<const ConVar *> ConVarList;
    1.20  
    1.21 -ConVarManager::ConVarManager() : m_ConVarType(0)
    1.22 +ConVarManager::ConVarManager() : m_ConVarType(0), m_VSPIface(NULL), m_CanQueryConVars(false)
    1.23  {
    1.24  	/* Create a convar lookup trie */
    1.25  	m_ConVarCache = sm_trie_create();
    1.26 @@ -49,6 +53,13 @@
    1.27  
    1.28  void ConVarManager::OnSourceModAllInitialized()
    1.29  {
    1.30 +	/* Only valid with ServerGameDLL006 or greater */
    1.31 +	if (g_SMAPI->GetGameDLLVersion() >= 6)
    1.32 +	{
    1.33 +		SH_ADD_HOOK_MEMFUNC(IServerGameDLL, OnQueryCvarValueFinished, gamedll, this, &ConVarManager::OnQueryCvarValueFinished, false);
    1.34 +		m_CanQueryConVars = true;
    1.35 +	}
    1.36 +
    1.37  	HandleAccess sec;
    1.38  
    1.39  	/* Set up access rights for the 'ConVar' handle type */
    1.40 @@ -65,13 +76,19 @@
    1.41  
    1.42  void ConVarManager::OnSourceModShutdown()
    1.43  {
    1.44 +	if (m_CanQueryConVars)
    1.45 +	{
    1.46 +		SH_REMOVE_HOOK_MEMFUNC(IServerGameDLL, OnQueryCvarValueFinished, gamedll, this, &ConVarManager::OnQueryCvarValueFinished, false);
    1.47 +		SH_REMOVE_HOOK_MEMFUNC(IServerPluginCallbacks, OnQueryCvarValueFinished, m_VSPIface, this, &ConVarManager::OnQueryCvarValueFinished, false);
    1.48 +	}
    1.49 +
    1.50  	IChangeableForward *fwd;
    1.51  	List<ConVarInfo *>::iterator i;
    1.52  
    1.53  	/* Iterate list of ConVarInfo structures */
    1.54  	for (i = m_ConVars.begin(); i != m_ConVars.end(); i++)
    1.55  	{
    1.56 -		fwd = (*i)->changeForward;
    1.57 +		fwd = (*i)->pChangeForward;
    1.58  
    1.59  		/* Free any convar-change forwards that still exist */
    1.60  		if (fwd)
    1.61 @@ -87,6 +104,29 @@
    1.62  	g_HandleSys.RemoveType(m_ConVarType, g_pCoreIdent);
    1.63  }
    1.64  
    1.65 +void ConVarManager::OnSourceModVSPReceived(IServerPluginCallbacks *iface)
    1.66 +{
    1.67 +	/* This will be called after OnSourceModAllInitialized(), so...
    1.68 +	 *
    1.69 +	 * If we haven't been able to hook the IServerGameDLL version at this point,
    1.70 +	 * then use hook IServerPluginCallbacks version from the engine.
    1.71 +	 */
    1.72 +
    1.73 +	/* The Ship currently only supports ServerPluginCallbacks001, but we need 002 */
    1.74 +	if (g_IsOriginalEngine)
    1.75 +	{
    1.76 +		return;
    1.77 +	}
    1.78 +
    1.79 +	if (!m_CanQueryConVars)
    1.80 +	{
    1.81 +		m_VSPIface = iface;
    1.82 +
    1.83 +		SH_ADD_HOOK_MEMFUNC(IServerPluginCallbacks, OnQueryCvarValueFinished, iface, this, &ConVarManager::OnQueryCvarValueFinished, false);
    1.84 +		m_CanQueryConVars = true;
    1.85 +	}
    1.86 +}
    1.87 +
    1.88  void ConVarManager::OnPluginUnloaded(IPlugin *plugin)
    1.89  {
    1.90  	ConVarList *pConVarList;
    1.91 @@ -190,7 +230,7 @@
    1.92  			pInfo = new ConVarInfo();
    1.93  			pInfo->handle = hndl;
    1.94  			pInfo->sourceMod = false;
    1.95 -			pInfo->changeForward = NULL;
    1.96 +			pInfo->pChangeForward = NULL;
    1.97  			pInfo->origCallback = pConVar->GetCallback();
    1.98  
    1.99  			/* Insert struct into caches */
   1.100 @@ -201,7 +241,7 @@
   1.101  		}
   1.102  	}
   1.103  
   1.104 -	// To prevent creating a convar that has the same name as a console command... ugh
   1.105 +	/* To prevent creating a convar that has the same name as a console command... ugh */
   1.106  	ConCommandBase *pBase = icvar->GetCommands();
   1.107  
   1.108  	while (pBase)
   1.109 @@ -227,7 +267,7 @@
   1.110  	pInfo = new ConVarInfo();
   1.111  	pInfo->handle = hndl;
   1.112  	pInfo->sourceMod = true;
   1.113 -	pInfo->changeForward = NULL;
   1.114 +	pInfo->pChangeForward = NULL;
   1.115  	pInfo->origCallback = NULL;
   1.116  
   1.117  	/* Insert struct into caches */
   1.118 @@ -265,7 +305,7 @@
   1.119  	pInfo = new ConVarInfo();
   1.120  	pInfo->handle = hndl;
   1.121  	pInfo->sourceMod = false;
   1.122 -	pInfo->changeForward = NULL;
   1.123 +	pInfo->pChangeForward = NULL;
   1.124  	pInfo->origCallback = pConVar->GetCallback();
   1.125  
   1.126  	/* Insert struct into our caches */
   1.127 @@ -284,13 +324,13 @@
   1.128  	if (sm_trie_retrieve(m_ConVarCache, pConVar->GetName(), (void **)&pInfo))
   1.129  	{
   1.130  		/* Get the forward */
   1.131 -		pForward = pInfo->changeForward;
   1.132 +		pForward = pInfo->pChangeForward;
   1.133  
   1.134  		/* If forward does not exist, create it */
   1.135  		if (!pForward)
   1.136  		{
   1.137  			pForward = g_Forwards.CreateForwardEx(NULL, ET_Ignore, 3, CONVARCHANGE_PARAMS);
   1.138 -			pInfo->changeForward = pForward;
   1.139 +			pInfo->pChangeForward = pForward;
   1.140  
   1.141  			/* Install our own callback */
   1.142  			pConVar->InstallChangeCallback(OnConVarChanged);
   1.143 @@ -311,7 +351,7 @@
   1.144  	if (sm_trie_retrieve(m_ConVarCache, pConVar->GetName(), (void **)&pInfo))
   1.145  	{
   1.146  		/* Get the forward */
   1.147 -		pForward = pInfo->changeForward;
   1.148 +		pForward = pInfo->pChangeForward;
   1.149  
   1.150  		/* If the forward doesn't exist, we can't unhook anything */
   1.151  		if (!pForward)
   1.152 @@ -332,7 +372,7 @@
   1.153  		{
   1.154  			/* Free this forward */
   1.155  			g_Forwards.ReleaseForward(pForward);
   1.156 -			pInfo->changeForward = NULL;
   1.157 +			pInfo->pChangeForward = NULL;
   1.158  
   1.159  			/* Put back the original convar callback */
   1.160  			pConVar->InstallChangeCallback(pInfo->origCallback);
   1.161 @@ -340,6 +380,24 @@
   1.162  	}
   1.163  }
   1.164  
   1.165 +QueryCvarCookie_t ConVarManager::QueryClientConVar(edict_t *pPlayer, const char *name, IPluginFunction *pCallback, Handle_t hndl)
   1.166 +{
   1.167 +	QueryCvarCookie_t cookie;
   1.168 +
   1.169 +	/* Call StartQueryCvarValue() in either the IVEngineServer or IServerPluginHelpers depending on situation */
   1.170 +	if (!m_VSPIface)
   1.171 +	{
   1.172 +		cookie = engine->StartQueryCvarValue(pPlayer, name);	
   1.173 +	} else {
   1.174 +		cookie = serverpluginhelpers->StartQueryCvarValue(pPlayer, name);
   1.175 +	}
   1.176 +
   1.177 +	ConVarQuery query = {cookie, pCallback, hndl};
   1.178 +	m_ConVarQueries.push_back(query);
   1.179 +
   1.180 +	return cookie;
   1.181 +}
   1.182 +
   1.183  void ConVarManager::AddConVarToPluginList(IPluginContext *pContext, const ConVar *pConVar)
   1.184  {
   1.185  	ConVarList *pConVarList;
   1.186 @@ -388,7 +446,7 @@
   1.187  	sm_trie_retrieve(pCache, pConVar->GetName(), (void **)&pInfo);
   1.188  
   1.189  	FnChangeCallback origCallback = pInfo->origCallback;
   1.190 -	IChangeableForward *pForward = pInfo->changeForward;
   1.191 +	IChangeableForward *pForward = pInfo->pChangeForward;
   1.192  
   1.193  	/* If there was a change callback installed previously, call it */
   1.194  	if (origCallback)
   1.195 @@ -403,6 +461,49 @@
   1.196  	pForward->Execute(NULL);
   1.197  }
   1.198  
   1.199 +void ConVarManager::OnQueryCvarValueFinished(QueryCvarCookie_t cookie, edict_t *pPlayer, EQueryCvarValueStatus result, const char *cvarName, const char *cvarValue)
   1.200 +{
   1.201 +	IPluginFunction *pCallback = NULL;
   1.202 +	cell_t value = 0;
   1.203 +	List<ConVarQuery>::iterator iter;
   1.204 +
   1.205 +	for (iter = m_ConVarQueries.begin(); iter != m_ConVarQueries.end(); iter++)
   1.206 +	{
   1.207 +		ConVarQuery &query = (*iter);
   1.208 +		
   1.209 +		if (query.cookie == cookie)
   1.210 +		{
   1.211 +			pCallback = query.pCallback;
   1.212 +			value = query.value;
   1.213 +
   1.214 +			break;
   1.215 +		}
   1.216 +	}
   1.217 +
   1.218 +	if (pCallback)
   1.219 +	{
   1.220 +		cell_t ret;
   1.221 +
   1.222 +		pCallback->PushCell(cookie);
   1.223 +		pCallback->PushCell(engine->IndexOfEdict(pPlayer));
   1.224 +		pCallback->PushCell(result);
   1.225 +		pCallback->PushString(cvarName);
   1.226 +
   1.227 +		if (result == eQueryCvarValueStatus_ValueIntact)
   1.228 +		{
   1.229 +			pCallback->PushString(cvarValue);
   1.230 +		} else {
   1.231 +			pCallback->PushString("\0");
   1.232 +		}
   1.233 +
   1.234 +		pCallback->PushCell(value);
   1.235 +		pCallback->Execute(&ret);
   1.236 +
   1.237 +		m_ConVarQueries.erase(iter);
   1.238 +	}
   1.239 +}
   1.240 +
   1.241 +
   1.242  static int s_YamagramState = 0;
   1.243  
   1.244  void _YamagramPrinterTwoPointOhOh(int yamagram)