sourcemm/episode1/console.cpp
changeset 371 751179604513
child 523 6f1630519b81
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/sourcemm/episode1/console.cpp	Thu Oct 04 20:15:25 2007 +0000
     1.3 @@ -0,0 +1,169 @@
     1.4 +/**
     1.5 + * vim: set ts=4 :
     1.6 + * ======================================================
     1.7 + * Metamod:Source
     1.8 + * Copyright (C) 2004-2007 AlliedModders LLC and authors.
     1.9 + * All rights reserved.
    1.10 + * ======================================================
    1.11 + *
    1.12 + * This software is provided 'as-is', without any express or implied warranty.
    1.13 + * In no event will the authors be held liable for any damages arising from 
    1.14 + * the use of this software.
    1.15 + * 
    1.16 + * Permission is granted to anyone to use this software for any purpose, 
    1.17 + * including commercial applications, and to alter it and redistribute it 
    1.18 + * freely, subject to the following restrictions:
    1.19 + *
    1.20 + * 1. The origin of this software must not be misrepresented; you must not 
    1.21 + * claim that you wrote the original software. If you use this software in a 
    1.22 + * product, an acknowledgment in the product documentation would be 
    1.23 + * appreciated but is not required.
    1.24 + * 2. Altered source versions must be plainly marked as such, and must not be
    1.25 + * misrepresented as being the original software.
    1.26 + * 3. This notice may not be removed or altered from any source distribution.
    1.27 + *
    1.28 + * Version: $Id$
    1.29 + */
    1.30 +
    1.31 +#include "console.h"
    1.32 +#include "provider_ep1.h"
    1.33 +
    1.34 +using namespace SourceHook;
    1.35 +
    1.36 +SMConVarAccessor g_SMConVarAccessor;
    1.37 +
    1.38 +class CAlwaysRegisterableCommand : public ConCommandBase
    1.39 +{
    1.40 +public:
    1.41 +	CAlwaysRegisterableCommand()
    1.42 +	{
    1.43 +		Create("metamod_eternal", NULL, FCVAR_UNREGISTERED|FCVAR_GAMEDLL);
    1.44 +	}
    1.45 +	bool IsRegistered( void ) const
    1.46 +	{
    1.47 +		return false;
    1.48 +	}
    1.49 +	void BringToFront()
    1.50 +	{
    1.51 +		// First, let's try to find us!
    1.52 +		ConCommandBase *pPtr = icvar->GetCommands();
    1.53 +	
    1.54 +		if (pPtr == this)
    1.55 +		{
    1.56 +			// We are already at the beginning; Nothing to do
    1.57 +			return;
    1.58 +		}
    1.59 +	
    1.60 +		while (pPtr)
    1.61 +		{
    1.62 +			if (pPtr == this)
    1.63 +			{
    1.64 +				break;
    1.65 +			}
    1.66 +			ConCommandBase *pPrev = NULL;
    1.67 +			while (pPtr)
    1.68 +			{
    1.69 +				if (pPtr == this)
    1.70 +				{
    1.71 +					break;
    1.72 +				}
    1.73 +				pPrev = pPtr;
    1.74 +				pPtr = const_cast<ConCommandBase*>(pPtr->GetNext());
    1.75 +			}
    1.76 +			if (pPrev && pPtr == this)
    1.77 +			{
    1.78 +				pPrev->SetNext(m_pNext);		// Remove us from the list
    1.79 +			}
    1.80 +			// Now, register us
    1.81 +			SetNext(NULL);
    1.82 +			icvar->RegisterConCommandBase(this);
    1.83 +		}	
    1.84 +	}
    1.85 +} s_EternalCommand;
    1.86 +
    1.87 +bool SMConVarAccessor::RegisterConCommandBase(ConCommandBase *pCommand)
    1.88 +{
    1.89 +	m_RegisteredCommands.push_back(pCommand);
    1.90 +	pCommand->SetNext(NULL);
    1.91 +	icvar->RegisterConCommandBase(pCommand);
    1.92 +
    1.93 +	return true;
    1.94 +}
    1.95 +
    1.96 +bool SMConVarAccessor::Register(ConCommandBase *pCommand)
    1.97 +{
    1.98 +	pCommand->SetNext(NULL);
    1.99 +	icvar->RegisterConCommandBase(pCommand);
   1.100 +
   1.101 +	return true;
   1.102 +}
   1.103 +
   1.104 +void SMConVarAccessor::MarkCommandsAsGameDLL()
   1.105 +{
   1.106 +	for (List<ConCommandBase*>::iterator iter = m_RegisteredCommands.begin();
   1.107 +		iter != m_RegisteredCommands.end(); ++iter)
   1.108 +	{
   1.109 +		(*iter)->AddFlags(FCVAR_GAMEDLL);
   1.110 +	}
   1.111 +}
   1.112 +
   1.113 +void SMConVarAccessor::UnregisterGameDLLCommands()
   1.114 +{
   1.115 +	ConCommandBase *begin = icvar->GetCommands();
   1.116 +	ConCommandBase *iter = begin;
   1.117 +	ConCommandBase *prev = NULL;
   1.118 +	while (iter)
   1.119 +	{
   1.120 +		/* Watch out for the ETERNAL COMMAND! */
   1.121 +		if (iter != &s_EternalCommand && iter->IsBitSet(FCVAR_GAMEDLL))
   1.122 +		{
   1.123 +			/* Remove it! */
   1.124 +			if (iter == begin)
   1.125 +			{
   1.126 +				s_EternalCommand.BringToFront();
   1.127 +				iter = const_cast<ConCommandBase*>(iter->GetNext());
   1.128 +				s_EternalCommand.SetNext(iter);
   1.129 +				prev = &s_EternalCommand;
   1.130 +				continue;
   1.131 +			}
   1.132 +			else
   1.133 +			{
   1.134 +				iter = const_cast<ConCommandBase*>(iter->GetNext());
   1.135 +				prev->SetNext(iter);
   1.136 +				continue;
   1.137 +			}
   1.138 +		}
   1.139 +		prev = iter;
   1.140 +		iter = const_cast<ConCommandBase*>(iter->GetNext());
   1.141 +	}
   1.142 +}
   1.143 +
   1.144 +void SMConVarAccessor::Unregister(ConCommandBase *pCommand)
   1.145 +{
   1.146 +	ConCommandBase *ptr = icvar->GetCommands();
   1.147 +
   1.148 +	if (ptr == pCommand)
   1.149 +	{
   1.150 +		s_EternalCommand.BringToFront();
   1.151 +		s_EternalCommand.SetNext(const_cast<ConCommandBase *>(pCommand->GetNext()));
   1.152 +	}
   1.153 +	else
   1.154 +	{
   1.155 +		/* Find us and unregister us */
   1.156 +		ConCommandBase *pPrev = NULL;
   1.157 +		while (ptr)
   1.158 +		{
   1.159 +			if (ptr == pCommand)
   1.160 +			{
   1.161 +				break;
   1.162 +			}
   1.163 +			pPrev = ptr;
   1.164 +			ptr = const_cast<ConCommandBase *>(ptr->GetNext());
   1.165 +		}
   1.166 +		if (pPrev && ptr == pCommand)
   1.167 +		{
   1.168 +			pPrev->SetNext(const_cast<ConCommandBase *>(pCommand->GetNext()));
   1.169 +		}		
   1.170 +	}
   1.171 +}
   1.172 +