sourcemm/concommands.cpp
changeset 67 1753f7a9b2f2
parent 65 27c4d32394a6
child 70 0bfb8a11fd8f
     1.1 --- a/sourcemm/concommands.cpp	Sun May 22 04:33:58 2005 +0000
     1.2 +++ b/sourcemm/concommands.cpp	Thu May 26 08:06:23 2005 +0000
     1.3 @@ -8,6 +8,8 @@
     1.4  * ============================
     1.5  */
     1.6  
     1.7 +#include <string>
     1.8 +#include <ctype.h>
     1.9  #include "concommands.h"
    1.10  #include "CPlugin.h"
    1.11  
    1.12 @@ -16,6 +18,7 @@
    1.13   * @file concommands.cpp
    1.14   */
    1.15  
    1.16 +CAlwaysRegisterableCommand g_EternalCommand;
    1.17  SMConVarAccessor g_SMConVarAccessor;
    1.18  
    1.19  bool SMConVarAccessor::RegisterConCommandBase(ConCommandBase *pCommand)
    1.20 @@ -34,6 +37,15 @@
    1.21  	return true;
    1.22  }
    1.23  
    1.24 +bool SMConVarAccessor::Register(ConCommandBase *pCommand)
    1.25 +{
    1.26 +	//simple, don't mark as part of sourcemm!
    1.27 +	pCommand->SetNext( NULL );
    1.28 +	g_Engine.icvar->RegisterConCommandBase(pCommand);
    1.29 +
    1.30 +	return true;
    1.31 +}
    1.32 +
    1.33  void SMConVarAccessor::MarkCommandsAsGameDLL()
    1.34  {
    1.35  	for (std::list<ConCommandBase*>::iterator iter = m_RegisteredCommands.begin();
    1.36 @@ -43,37 +55,34 @@
    1.37  	}
    1.38  }
    1.39  
    1.40 -void SMConVarAccessor::Unregister(ConCommandBase *pCvar)
    1.41 +void SMConVarAccessor::Unregister(ConCommandBase *pCommand)
    1.42  {
    1.43  	ICvar *cv = g_Engine.icvar;
    1.44 -
    1.45  	ConCommandBase *ptr = cv->GetCommands();
    1.46  
    1.47 -	if (ptr == pCvar && ptr->GetNext())
    1.48 -	{
    1.49 -		//we're at the beginning of the list
    1.50 -		*ptr = *(ptr->GetNext());
    1.51 -		return;
    1.52 -	}
    1.53 -
    1.54 -	while (ptr)
    1.55 +	if (ptr == pCommand)
    1.56  	{
    1.57 -		ConCommandBase *pNext = const_cast<ConCommandBase *>(ptr->GetNext());
    1.58 -		if (pNext == pCvar)
    1.59 -			break;
    1.60 -		ptr = pNext;
    1.61 +		//first in list
    1.62 +		g_EternalCommand.BringToFront();
    1.63 +		g_EternalCommand.SetNext(const_cast<ConCommandBase *>(pCommand->GetNext()));
    1.64 +	} else {
    1.65 +		//find us and unregister us
    1.66 +		ConCommandBase *pPrev = NULL;
    1.67 +		while (ptr)
    1.68 +		{
    1.69 +			if (ptr == pCommand)
    1.70 +				break;
    1.71 +			pPrev = ptr;
    1.72 +			ptr = const_cast<ConCommandBase *>(ptr->GetNext());
    1.73 +		}
    1.74 +		if (pPrev && ptr == pCommand)
    1.75 +		{
    1.76 +			pPrev->SetNext(const_cast<ConCommandBase *>(pCommand->GetNext()));
    1.77 +		}
    1.78  	}
    1.79 -
    1.80 -	if (ptr)
    1.81 -	{
    1.82 -		ptr->SetNext(const_cast<ConCommandBase *>(pCvar->GetNext()));
    1.83 -		pCvar->SetNext(NULL);
    1.84 -	}
    1.85 -
    1.86 -	m_RegisteredCommands.remove(pCvar);
    1.87  }
    1.88  
    1.89 -ConVar metamod_version("metamod_version", SOURCEMM_VERSION, FCVAR_REPLICATED | FCVAR_SPONLY, "Metamod:Source Version");
    1.90 +ConVar metamod_version("metamod_version", SOURCEMM_VERSION, FCVAR_REPLICATED | FCVAR_SPONLY | FCVAR_NOTIFY, "Metamod:Source Version");
    1.91  
    1.92  CON_COMMAND(meta, "Metamod:Source Menu")
    1.93  {
    1.94 @@ -111,9 +120,9 @@
    1.95  		} else if (strcmp(command, "refresh") == 0) {
    1.96  			char full_path[255];
    1.97  #if defined WIN32 || defined _WIN32
    1.98 -			snprintf(full_path, sizeof(full_path)-1, "%s\\%s", g_ModPath.c_str(), "metaplugins.ini");
    1.99 +			snprintf(full_path, sizeof(full_path)-1, "%s\\addons\\metamod\\%s", g_ModPath.c_str(), "metaplugins.ini");
   1.100  #else
   1.101 -			snprintf(full_path, sizeof(full_path)-1, "%s/%s", g_ModPath.c_str(), "metaplugins.ini");
   1.102 +			snprintf(full_path, sizeof(full_path)-1, "%s/addons/metamod/%s", g_ModPath.c_str(), "metaplugins.ini");
   1.103  #endif
   1.104  			LoadPluginsFromFile(full_path);
   1.105  
   1.106 @@ -170,6 +179,68 @@
   1.107  			Msg("\n");
   1.108  
   1.109  			return;
   1.110 +		} else if (strcmp(command, "cmds") == 0) {
   1.111 +			if (args >= 3)
   1.112 +			{
   1.113 +				int id = atoi(e->Cmd_Argv(2));
   1.114 +				SourceMM::CPluginManager::CPlugin *pl = g_PluginMngr.FindById(id);
   1.115 +
   1.116 +				if (!pl)
   1.117 +				{
   1.118 +					Msg("Plugin %d not found.\n", id);
   1.119 +					return;
   1.120 +				}
   1.121 +
   1.122 +				if (!pl->m_API)
   1.123 +				{
   1.124 +					Msg("Plugin %d is not loaded.\n", id);
   1.125 +				} else {
   1.126 +					Msg("Console commands for %s:\n", pl->m_API->GetName());
   1.127 +					std::list<ConCommandBase *>::iterator ci;
   1.128 +					size_t count = 0;
   1.129 +
   1.130 +					for (ci=pl->m_Cmds.begin(); ci!=pl->m_Cmds.end(); ci++)
   1.131 +					{
   1.132 +						count++;
   1.133 +						Msg(" [%5d] %-s\n", count, (*ci)->GetName());
   1.134 +					}
   1.135 +				}
   1.136 +			} else {
   1.137 +				Msg("Usage: meta cmds <id>\n");
   1.138 +			}
   1.139 +
   1.140 +			return;
   1.141 +		} else if (strcmp(command, "cvars") == 0) {
   1.142 +			if (args >= 3)
   1.143 +			{
   1.144 +				int id = atoi(e->Cmd_Argv(2));
   1.145 +				SourceMM::CPluginManager::CPlugin *pl = g_PluginMngr.FindById(id);
   1.146 +
   1.147 +				if (!pl)
   1.148 +				{
   1.149 +					Msg("Plugin %d not found.\n", id);
   1.150 +					return;
   1.151 +				}
   1.152 +
   1.153 +				if (!pl->m_API)
   1.154 +				{
   1.155 +					Msg("Plugin %d is not loaded.\n", id);
   1.156 +				} else {
   1.157 +					Msg("Registered cvars for %s:\n", pl->m_API->GetName());
   1.158 +					std::list<ConCommandBase *>::iterator ci;
   1.159 +					size_t count = 0;
   1.160 +
   1.161 +					for (ci=pl->m_Cvars.begin(); ci!=pl->m_Cvars.end(); ci++)
   1.162 +					{
   1.163 +						count++;
   1.164 +						Msg(" [%5d] %-s\n", count, (*ci)->GetName());
   1.165 +					}
   1.166 +				}
   1.167 +			} else {
   1.168 +				Msg("Usage: meta cvars <id>\n");
   1.169 +			}
   1.170 +
   1.171 +			return;
   1.172  		} else if (strcmp(command, "info") == 0) {
   1.173  			if (args >= 3)
   1.174  			{
   1.175 @@ -368,6 +439,8 @@
   1.176  	Msg("Metamod:Source Menu\n");
   1.177  	Msg("usage: meta <command> [arguments]\n");
   1.178  	Msg("  clear        - Unload all plugins forcefully\n");
   1.179 +	Msg("  cmds         - Show plugin commands\n");
   1.180 +	Msg("  cvars        - Show plugin cvars\n");
   1.181  	Msg("  credits      - About Metamod:Source\n");
   1.182  	Msg("  force_unload - Forcefully unload a plugin\n");
   1.183  	Msg("  game         - Information about GameDLL\n");
   1.184 @@ -382,3 +455,65 @@
   1.185  	Msg("  version      - Version information\n");
   1.186  	Msg("\n");
   1.187  }
   1.188 +
   1.189 +int UTIL_CmpNocase(const std::string &s1, const std::string &s2)
   1.190 +{
   1.191 +	std::string::const_iterator p1 = s1.begin();
   1.192 +	std::string::const_iterator p2 = s2.begin();
   1.193 +
   1.194 +	while (p1 != s1.end() && p2 != s2.end())
   1.195 +	{
   1.196 +		if(toupper(*p1) != toupper(*p2))
   1.197 +			return (toupper(*p1)<toupper(*p2)) ? -1 : 1;
   1.198 +		++p1;
   1.199 +		++p2;
   1.200 +	}
   1.201 +
   1.202 +	return (s2.size() == s1.size()) ? 0 : (s1.size() < s2.size()) ? -1 : 1;	// size is unsigned
   1.203 +}
   1.204 +
   1.205 +CAlwaysRegisterableCommand::CAlwaysRegisterableCommand()
   1.206 +{
   1.207 +	Create("", NULL, FCVAR_UNREGISTERED|FCVAR_GAMEDLL);
   1.208 +	m_pICvar = NULL;
   1.209 +}
   1.210 +
   1.211 +bool CAlwaysRegisterableCommand::IsRegistered( void ) const
   1.212 +{
   1.213 +	return false;
   1.214 +}
   1.215 +
   1.216 +void CAlwaysRegisterableCommand::BringToFront()
   1.217 +{
   1.218 +	if (!m_pICvar)
   1.219 +		m_pICvar = g_Engine.icvar;
   1.220 +
   1.221 +	// First, let's try to find us!
   1.222 +	ConCommandBase *pPtr = m_pICvar->GetCommands();
   1.223 +
   1.224 +	if (pPtr == this)
   1.225 +	{
   1.226 +		// We are already at the beginning; Nothing to do
   1.227 +		return;
   1.228 +	}
   1.229 +
   1.230 +	while (pPtr)
   1.231 +	{
   1.232 +		if (pPtr == this && pPtr->IsCommand() && UTIL_CmpNocase(GetName(), pPtr->GetName()) == 0)
   1.233 +			break;
   1.234 +		ConCommandBase *pPrev = NULL;
   1.235 +		while (pPtr)
   1.236 +		{
   1.237 +			if (pPtr == this)
   1.238 +				break;
   1.239 +			pPrev = pPtr;
   1.240 +			pPtr = const_cast<ConCommandBase*>(pPtr->GetNext());
   1.241 +		}
   1.242 +		if (pPrev && pPtr == this)
   1.243 +		{
   1.244 +			pPrev->SetNext(m_pNext);		// Remove us from the list
   1.245 +		}
   1.246 +		// Now, register us
   1.247 +		m_pICvar->RegisterConCommandBase(this);
   1.248 +	}
   1.249 +}