sourcemm/convar.cpp
changeset 232 a86d52ff721c
parent 7 8ef11f6189e4
     1.1 --- a/sourcemm/convar.cpp	Wed Aug 09 01:52:49 2006 +0000
     1.2 +++ b/sourcemm/convar.cpp	Wed Aug 09 02:05:57 2006 +0000
     1.3 @@ -6,28 +6,216 @@
     1.4  //
     1.5  //=============================================================================//
     1.6  
     1.7 +#ifdef _XBOX
     1.8 +#include "xbox/xbox_platform.h"
     1.9 +#include "xbox/xbox_win32stubs.h"
    1.10 +#include "xbox/xbox_core.h"
    1.11 +#endif
    1.12  #include <stdio.h>
    1.13  #include <stdlib.h>
    1.14  #include <string.h>
    1.15  #include "basetypes.h"
    1.16  #include "convar.h"
    1.17  #include "vstdlib/strtools.h"
    1.18 +#include "icvar.h"
    1.19  #include "tier0/dbg.h"
    1.20 +#ifdef _XBOX
    1.21 +#include "vstdlib/ICommandLine.h"
    1.22 +#endif
    1.23  #include "tier0/memdbgon.h"
    1.24  
    1.25 -
    1.26  ConCommandBase			*ConCommandBase::s_pConCommandBases = NULL;
    1.27  IConCommandBaseAccessor	*ConCommandBase::s_pAccessor = NULL;
    1.28  
    1.29 +#ifdef _XBOX
    1.30 +static char const* GetCommandLineValue( char const *pVariableName )
    1.31 +{
    1.32 +	int nLen = Q_strlen(pVariableName);
    1.33 +	char *pSearch = (char*)stackalloc(nLen + 2);
    1.34 +	pSearch[0] = '+';
    1.35 +	memcpy(&pSearch[1], pVariableName, nLen + 1);
    1.36 +	return CommandLine()->ParmValue(pSearch);
    1.37 +}
    1.38 +#endif
    1.39 +
    1.40 +#if defined( _XBOX ) && !defined( _RETAIL )
    1.41 +void ConCommandBaseMgr::PublishCommands( bool bForce )
    1.42 +{
    1.43 +	ConCommandBase	*pCur;
    1.44 +	const char		*commands[2048];
    1.45 +	const char		*helptext[2048];
    1.46 +	int				numCommands = 0;
    1.47 +
    1.48 +	if ( bForce )
    1.49 +	{
    1.50 +		for ( pCur=ConCommandBase::s_pConCommandBases; pCur; pCur=pCur->m_pNext )
    1.51 +		{
    1.52 +			pCur->m_bRegistered = false;
    1.53 +		}
    1.54 +	}
    1.55 +
    1.56 +	// iterate and publish commands to the remote console
    1.57 +	for ( pCur=ConCommandBase::s_pConCommandBases; pCur; pCur=pCur->m_pNext )
    1.58 +	{
    1.59 +		if ( !pCur->m_bRegistered )
    1.60 +		{
    1.61 +			// add unregistered commands to list
    1.62 +			if ( numCommands < sizeof(commands)/sizeof(commands[0]) )
    1.63 +			{
    1.64 +				commands[numCommands] = pCur->m_pszName;
    1.65 +				helptext[numCommands] = pCur->m_pszHelpString;
    1.66 +				numCommands++;
    1.67 +			}
    1.68 +
    1.69 +			// mark as registered
    1.70 +			pCur->m_bRegistered = true;
    1.71 +		}
    1.72 +	}
    1.73 +	if ( numCommands )
    1.74 +	{
    1.75 +		XBX_rAddCommands( numCommands, commands, helptext );
    1.76 +	}
    1.77 +}
    1.78 +#endif
    1.79 +
    1.80 +#ifdef _XBOX
    1.81 +bool ConCommandBaseMgr::Fixup(ConCommandBase* pConCommand)
    1.82 +{
    1.83 +	ConCommandBase	*pCur;
    1.84 +	ConCommandBase	*pPrev2;
    1.85 +	ConCommandBase	*pCur2;
    1.86 +	ConCommandBase	*pNext2;
    1.87 +	const char		*name;
    1.88 +	static int		initCount = 0;
    1.89 +
    1.90 +	// xboxissue - cvars and its class hierarchy could not be made to instance per subsystem
    1.91 +	// without massive mangling and re-arranging, instead...
    1.92 +	// there is only a single chain and therefore single /init/fixup
    1.93 +	// missing: need to identify which subsystem
    1.94 +	// could pass as part of declaration in constructor, but how to hide parameter for pc
    1.95 +	// the accessors (aka callbacks to subsystems) to register with engine 
    1.96 +	// cannot be invoked as their unlink logic expect private lists
    1.97 +	// so this just mimics the expected end result
    1.98 +	// must handle early and late constructors
    1.99 +	// late constructors are usually function scoped static
   1.100 +	if (!pConCommand)
   1.101 +	{
   1.102 +		// the caller is one-time-init 
   1.103 +		if (++initCount > 1)
   1.104 +		{
   1.105 +			// the list has already been fixed
   1.106 +			return true;
   1.107 +		}
   1.108 +	}
   1.109 +	else
   1.110 +	{
   1.111 +		// the caller is a console command constructor
   1.112 +		if (!initCount)
   1.113 +		{
   1.114 +			// the list has not been fixed yet 
   1.115 +			// no special behavior
   1.116 +			return false;
   1.117 +		}
   1.118 +		else
   1.119 +		{
   1.120 +			// the list has already been fixed
   1.121 +			// the console command is a late constructor
   1.122 +			// add in to fixed list 
   1.123 +			bool hasParent = false;
   1.124 +			if (!pConCommand->IsCommand())
   1.125 +			{
   1.126 +				pCur  = ConCommandBase::s_pConCommandBases;
   1.127 +				while (pCur)
   1.128 +				{
   1.129 +					if (pCur->IsCommand() && !stricmp(pCur->m_pszName, pConCommand->m_pszName))
   1.130 +					{
   1.131 +						// set its parent
   1.132 +						((ConVar*)pConCommand)->m_pParent = ((ConVar*)pCur)->m_pParent;
   1.133 +						hasParent = true;
   1.134 +						break;
   1.135 +					}
   1.136 +					pCur = pCur->m_pNext;
   1.137 +				}
   1.138 +			}
   1.139 +			if (!hasParent)
   1.140 +			{
   1.141 +				// add to head of list
   1.142 +				pConCommand->m_pNext = ConCommandBase::s_pConCommandBases;
   1.143 +				ConCommandBase::s_pConCommandBases = pConCommand;
   1.144 +			}
   1.145 +			else
   1.146 +				return true;
   1.147 +		}
   1.148 +	}
   1.149 +
   1.150 +	if (initCount == 1)
   1.151 +	{
   1.152 +		// iterate the cvars and set their possible proxy parents
   1.153 +		// skip over registered (fixed) entries
   1.154 +		pCur = ConCommandBase::s_pConCommandBases;
   1.155 +		while (pCur)
   1.156 +		{
   1.157 +			if (!pCur->IsCommand() && !pCur->m_bRegistered)
   1.158 +			{
   1.159 +				// iterate from the next node until end of list
   1.160 +				name   = pCur->m_pszName; 
   1.161 +				pPrev2 = pCur;		
   1.162 +				pCur2  = pCur->m_pNext;
   1.163 +				while (pCur2)
   1.164 +				{
   1.165 +					pNext2 = pCur2->m_pNext;
   1.166 +					if (!pCur2->IsCommand() && !stricmp(pCur2->m_pszName, name))
   1.167 +					{
   1.168 +						// found duplicate
   1.169 +						// unlink and fixup
   1.170 +						pCur2->m_pNext  = NULL;
   1.171 +						pPrev2->m_pNext = pNext2;
   1.172 +
   1.173 +						// set its parent
   1.174 +						((ConVar*)pCur2)->m_pParent = ((ConVar*)pCur)->m_pParent;
   1.175 +					}
   1.176 +					else
   1.177 +					{
   1.178 +						// no unlink, advance to next node
   1.179 +						pPrev2 = pCur2;
   1.180 +					}
   1.181 +
   1.182 +					pCur2 = pNext2;
   1.183 +				}
   1.184 +
   1.185 +				char const *pValue = GetCommandLineValue(name);
   1.186 +				if (pValue)
   1.187 +					((ConVar*)pCur)->SetValue(pValue);
   1.188 +			}
   1.189 +			pCur = pCur->m_pNext;
   1.190 +		}
   1.191 +	}
   1.192 +
   1.193 +#if !defined( _RETAIL )
   1.194 +	XBX_rTimeStampLog( Plat_FloatTime(), "xbx PublishCommands:Start" );
   1.195 +
   1.196 +	PublishCommands( false );
   1.197 +
   1.198 +	XBX_rTimeStampLog( Plat_FloatTime(), "xbx PublishCommands:Done" );
   1.199 +#endif
   1.200 +
   1.201 +	// fixup has been performed
   1.202 +	return true;
   1.203 +}
   1.204 +#endif
   1.205 +
   1.206  // ----------------------------------------------------------------------------- //
   1.207  // ConCommandBaseMgr.
   1.208  // ----------------------------------------------------------------------------- //
   1.209  void ConCommandBaseMgr::OneTimeInit( IConCommandBaseAccessor *pAccessor )
   1.210  {
   1.211 +#ifdef _XBOX
   1.212 +	// fixup the list
   1.213 +	ConCommandBaseMgr::Fixup(NULL);
   1.214 +#else
   1.215  	ConCommandBase *pCur, *pNext;
   1.216  
   1.217  	ConCommandBase::s_pAccessor = pAccessor;
   1.218 -
   1.219  	pCur = ConCommandBase::s_pConCommandBases;
   1.220  	while ( pCur )
   1.221  	{
   1.222 @@ -35,6 +223,7 @@
   1.223  		pCur->Init();
   1.224  		pCur = pNext;
   1.225  	}
   1.226 +#endif
   1.227  }
   1.228  
   1.229  //-----------------------------------------------------------------------------
   1.230 @@ -42,12 +231,12 @@
   1.231  //-----------------------------------------------------------------------------
   1.232  ConCommandBase::ConCommandBase( void )
   1.233  {
   1.234 -	m_bRegistered			= false;
   1.235 -	m_pszName				= NULL;
   1.236 -	m_pszHelpString			= NULL;
   1.237 +	m_bRegistered   = false;
   1.238 +	m_pszName       = NULL;
   1.239 +	m_pszHelpString = NULL;
   1.240  
   1.241 -	m_nFlags				= 0;
   1.242 -	m_pNext					= NULL;
   1.243 +	m_nFlags = 0;
   1.244 +	m_pNext  = NULL;
   1.245  }
   1.246  
   1.247  //-----------------------------------------------------------------------------
   1.248 @@ -89,24 +278,55 @@
   1.249  {
   1.250  	static char *empty_string = "";
   1.251  
   1.252 -	m_bRegistered		= false;
   1.253 +	m_bRegistered = false;
   1.254  
   1.255  	// Name should be static data
   1.256  	Assert( pName );
   1.257 -	m_pszName			= pName;
   1.258 -	m_pszHelpString		= pHelpString ? pHelpString : empty_string;
   1.259 +	m_pszName = pName;
   1.260 +	m_pszHelpString = pHelpString ? pHelpString : empty_string;
   1.261  
   1.262  	m_nFlags = flags;
   1.263  
   1.264 +#ifdef _XBOX
   1.265 +	if (ConCommandBaseMgr::Fixup(this))
   1.266 +		return;
   1.267 +#endif
   1.268 +
   1.269  	if ( !( m_nFlags & FCVAR_UNREGISTERED ) )
   1.270  	{
   1.271 -		m_pNext		= s_pConCommandBases;
   1.272 -		s_pConCommandBases	= this;
   1.273 +#ifndef _XBOX
   1.274 +		m_pNext = s_pConCommandBases;
   1.275 +		s_pConCommandBases = this;
   1.276 +#else
   1.277 +		// xboxissue - engine cvars should be at head of list to
   1.278 +		// ensure they are set as the cvar master/parent during fixup
   1.279 +		if (!s_pConCommandBases || !(flags & FCVAR_NON_ENGINE))
   1.280 +		{
   1.281 +			// engine cvars, place at head of list
   1.282 +			m_pNext = s_pConCommandBases;
   1.283 +			s_pConCommandBases = this;
   1.284 +		}
   1.285 +		else
   1.286 +		{
   1.287 +			// non-engine cvars, place at end of list
   1.288 +			ConCommandBase *cur = s_pConCommandBases;
   1.289 +			while (1)
   1.290 +			{
   1.291 +				if (!cur->m_pNext)
   1.292 +				{
   1.293 +					cur->m_pNext = this;
   1.294 +					m_pNext      = NULL;
   1.295 +					break;
   1.296 +				}
   1.297 +				cur = cur->m_pNext;
   1.298 +			}
   1.299 +		}
   1.300 +#endif
   1.301  	}
   1.302  	else
   1.303  	{
   1.304  		// It's unregistered
   1.305 -		m_pNext		= NULL;
   1.306 +		m_pNext = NULL;
   1.307  	}
   1.308  
   1.309  	// If s_pAccessor is already set (this ConVar is not a global variable),
   1.310 @@ -329,7 +549,6 @@
   1.311  //-----------------------------------------------------------------------------
   1.312  ConCommand::~ConCommand( void )
   1.313  {
   1.314 -
   1.315  }
   1.316  
   1.317  //-----------------------------------------------------------------------------
   1.318 @@ -565,12 +784,8 @@
   1.319  {
   1.320  	Assert( !( m_nFlags & FCVAR_NEVER_AS_STRING ) );
   1.321  
   1.322 -	char* pszOldValue = (char*)stackalloc( m_StringLength );
   1.323 -
   1.324 -	if ( m_fnChangeCallback )
   1.325 -	{
   1.326 -		memcpy( pszOldValue, m_pszString, m_StringLength );
   1.327 -	}
   1.328 + 	char* pszOldValue = (char*)stackalloc( m_StringLength );
   1.329 +	memcpy( pszOldValue, m_pszString, m_StringLength );
   1.330  	
   1.331  	int len = Q_strlen(tempVal) + 1;
   1.332  
   1.333 @@ -591,6 +806,8 @@
   1.334  		m_fnChangeCallback( this, pszOldValue );
   1.335  	}
   1.336  
   1.337 +	GetCVarIF()->CallGlobalChangeCallback( this, pszOldValue );
   1.338 +
   1.339  	stackfree( pszOldValue );
   1.340  }
   1.341