sourcemm/episode1/console.cpp
author David Anderson <dvander@alliedmods.net>
Sat Sep 29 16:16:24 2007 +0000 (2007-09-29)
branchsourcemm-1.6.0
changeset 367 fc76472b980f
parent 365 afdf36dee1a6
child 368 4a937e6e6f11
permissions -rw-r--r--
added file headers and svn props
     1 /**
     2  * vim: set ts=4 :
     3  * ======================================================
     4  * Metamod:Source
     5  * Copyright (C) 2004-2007 AlliedModders LLC and authors.
     6  * All rights reserved.
     7  * ======================================================
     8  *
     9  * This software is provided 'as-is', without any express or implied warranty.
    10  * In no event will the authors be held liable for any damages arising from 
    11  * the use of this software.
    12  * 
    13  * Permission is granted to anyone to use this software for any purpose, 
    14  * including commercial applications, and to alter it and redistribute it 
    15  * freely, subject to the following restrictions:
    16  *
    17  * 1. The origin of this software must not be misrepresented; you must not 
    18  * claim that you wrote the original software. If you use this software in a 
    19  * product, an acknowledgment in the product documentation would be 
    20  * appreciated but is not required.
    21  * 2. Altered source versions must be plainly marked as such, and must not be
    22  * misrepresented as being the original software.
    23  * 3. This notice may not be removed or altered from any source distribution.
    24  *
    25  * Version: $Id$
    26  */
    27 
    28 #include "console.h"
    29 #include "provider_ep1.h"
    30 
    31 using namespace SourceHook;
    32 
    33 SMConVarAccessor g_SMConVarAccessor;
    34 
    35 class CAlwaysRegisterableCommand : public ConCommandBase
    36 {
    37 public:
    38 	CAlwaysRegisterableCommand()
    39 	{
    40 		Create("metamod_eternal", NULL, FCVAR_UNREGISTERED|FCVAR_GAMEDLL);
    41 	}
    42 	bool IsRegistered( void ) const
    43 	{
    44 		return false;
    45 	}
    46 	void BringToFront()
    47 	{
    48 		// First, let's try to find us!
    49 		ConCommandBase *pPtr = icvar->GetCommands();
    50 	
    51 		if (pPtr == this)
    52 		{
    53 			// We are already at the beginning; Nothing to do
    54 			return;
    55 		}
    56 	
    57 		while (pPtr)
    58 		{
    59 			if (pPtr == this)
    60 			{
    61 				break;
    62 			}
    63 			ConCommandBase *pPrev = NULL;
    64 			while (pPtr)
    65 			{
    66 				if (pPtr == this)
    67 				{
    68 					break;
    69 				}
    70 				pPrev = pPtr;
    71 				pPtr = const_cast<ConCommandBase*>(pPtr->GetNext());
    72 			}
    73 			if (pPrev && pPtr == this)
    74 			{
    75 				pPrev->SetNext(m_pNext);		// Remove us from the list
    76 			}
    77 			// Now, register us
    78 			SetNext(NULL);
    79 			icvar->RegisterConCommandBase(this);
    80 		}	
    81 	}
    82 } s_EternalCommand;
    83 
    84 bool SMConVarAccessor::RegisterConCommandBase(ConCommandBase *pCommand)
    85 {
    86 	m_RegisteredCommands.push_back(pCommand);
    87 	pCommand->SetNext(NULL);
    88 	icvar->RegisterConCommandBase(pCommand);
    89 
    90 	return true;
    91 }
    92 
    93 bool SMConVarAccessor::Register(ConCommandBase *pCommand)
    94 {
    95 	pCommand->SetNext(NULL);
    96 	icvar->RegisterConCommandBase(pCommand);
    97 
    98 	return true;
    99 }
   100 
   101 void SMConVarAccessor::MarkCommandsAsGameDLL()
   102 {
   103 	for (List<ConCommandBase*>::iterator iter = m_RegisteredCommands.begin();
   104 		iter != m_RegisteredCommands.end(); ++iter)
   105 	{
   106 		(*iter)->AddFlags(FCVAR_GAMEDLL);
   107 	}
   108 }
   109 
   110 void SMConVarAccessor::UnregisterGameDLLCommands()
   111 {
   112 	ConCommandBase *begin = icvar->GetCommands();
   113 	ConCommandBase *iter = begin;
   114 	ConCommandBase *prev = NULL;
   115 	while (iter)
   116 	{
   117 		/* Watch out for the ETERNAL COMMAND! */
   118 		if (iter != &s_EternalCommand && iter->IsBitSet(FCVAR_GAMEDLL))
   119 		{
   120 			/* Remove it! */
   121 			if (iter == begin)
   122 			{
   123 				s_EternalCommand.BringToFront();
   124 				iter = const_cast<ConCommandBase*>(iter->GetNext());
   125 				s_EternalCommand.SetNext(iter);
   126 				prev = &s_EternalCommand;
   127 				continue;
   128 			}
   129 			else
   130 			{
   131 				iter = const_cast<ConCommandBase*>(iter->GetNext());
   132 				prev->SetNext(iter);
   133 				continue;
   134 			}
   135 		}
   136 		prev = iter;
   137 		iter = const_cast<ConCommandBase*>(iter->GetNext());
   138 	}
   139 }
   140 
   141 void SMConVarAccessor::Unregister(ConCommandBase *pCommand)
   142 {
   143 	ConCommandBase *ptr = icvar->GetCommands();
   144 
   145 	if (ptr == pCommand)
   146 	{
   147 		s_EternalCommand.BringToFront();
   148 		s_EternalCommand.SetNext(const_cast<ConCommandBase *>(pCommand->GetNext()));
   149 	}
   150 	else
   151 	{
   152 		/* Find us and unregister us */
   153 		ConCommandBase *pPrev = NULL;
   154 		while (ptr)
   155 		{
   156 			if (ptr == pCommand)
   157 			{
   158 				break;
   159 			}
   160 			pPrev = ptr;
   161 			ptr = const_cast<ConCommandBase *>(ptr->GetNext());
   162 		}
   163 		if (pPrev && ptr == pCommand)
   164 		{
   165 			pPrev->SetNext(const_cast<ConCommandBase *>(pCommand->GetNext()));
   166 		}		
   167 	}
   168 }
   169