1 /* ======== SourceMM ========
2 * Copyright (C) 2004-2005 Metamod:Source Development Team
3 * No warranties of any kind
7 * Author(s): David "BAILOPAN" Anderson
8 * ============================
13 #include "concommands.h"
18 * @brief Console Command Implementations
19 * @file concommands.cpp
22 CAlwaysRegisterableCommand g_EternalCommand;
23 SMConVarAccessor g_SMConVarAccessor;
25 bool SMConVarAccessor::RegisterConCommandBase(ConCommandBase *pCommand)
27 // Add the FCVAR_GAMEDLL flag
28 // => No crash on exit!
29 // UPDATE: Do _not_ add the FCVAR_GAMEDLL flag here, as it
30 // causes the command to be unusable on listenservers until you load a map
31 // We will set the FCVAR_GAMEDLL flag on all commands we have registered once we are being unloaded
32 //pCommand->AddFlags(FCVAR_GAMEDLL);
33 m_RegisteredCommands.push_back(pCommand);
35 pCommand->SetNext( NULL );
36 g_Engine.icvar->RegisterConCommandBase(pCommand);
41 bool SMConVarAccessor::Register(ConCommandBase *pCommand)
43 //simple, don't mark as part of sourcemm!
44 pCommand->SetNext( NULL );
45 g_Engine.icvar->RegisterConCommandBase(pCommand);
50 void SMConVarAccessor::MarkCommandsAsGameDLL()
52 for (std::list<ConCommandBase*>::iterator iter = m_RegisteredCommands.begin();
53 iter != m_RegisteredCommands.end(); ++iter)
55 (*iter)->AddFlags(FCVAR_GAMEDLL);
59 void SMConVarAccessor::Unregister(ConCommandBase *pCommand)
61 ICvar *cv = g_Engine.icvar;
62 ConCommandBase *ptr = cv->GetCommands();
67 g_EternalCommand.BringToFront();
68 g_EternalCommand.SetNext(const_cast<ConCommandBase *>(pCommand->GetNext()));
70 //find us and unregister us
71 ConCommandBase *pPrev = NULL;
77 ptr = const_cast<ConCommandBase *>(ptr->GetNext());
79 if (pPrev && ptr == pCommand)
81 pPrev->SetNext(const_cast<ConCommandBase *>(pCommand->GetNext()));
86 ConVar metamod_version("metamod_version", SOURCEMM_VERSION, FCVAR_REPLICATED | FCVAR_SPONLY | FCVAR_NOTIFY, "Metamod:Source Version");
87 #if defined WIN32 || defined _WIN32
88 ConVar mm_pluginsfile("mm_pluginsfile", "addons\\metamod\\metaplugins.ini", FCVAR_SPONLY, "Metamod:Source Plugins File");
90 ConVar mm_pluginsfile("mm_pluginsfile", "addons/metamod/metaplugins.ini", FCVAR_SPONLY, "Metamod:Source Plugins File");
93 CON_COMMAND(meta, "Metamod:Source Menu")
95 IVEngineServer *e = g_Engine.engine;
97 int args = e->Cmd_Argc();
101 const char *command = e->Cmd_Argv(1);
102 if (strcmp(command, "credits") == 0)
104 CONMSG("Metamod:Source was developed by:\n");
105 CONMSG(" SourceHook: Pavol \"PM OnoTo\" Marko\n");
106 CONMSG(" GameDLL/Plugins: David \"BAILOPAN\" Anderson\n");
107 CONMSG(" GameDLL: Scott \"Damaged Soul\" Ehlert\n");
108 CONMSG("For more information, see the official website\n");
109 CONMSG("http://www.sourcemm.net/\n");
112 } else if (strcmp(command, "version") == 0) {
113 CONMSG("Metamod:Source version %s\n", SOURCEMM_VERSION);
114 CONMSG("Compiled on: %s\n", SOURCEMM_DATE);
115 CONMSG("Plugin interface version: %d:%d\n", PLAPI_VERSION, PLAPI_MIN_VERSION);
116 CONMSG("SourceHook version: %d:%d\n", g_SourceHook.GetIfaceVersion(), g_SourceHook.GetImplVersion());
117 CONMSG("http://www.sourcemm.net/\n");
120 } else if (strcmp(command, "game") == 0) {
121 CONMSG("GameDLL Information\n");
122 CONMSG(" Mod path: %s\n", g_ModPath.c_str());
123 CONMSG(" Dll path: %s\n", g_BinPath.c_str());
126 } else if (strcmp(command, "refresh") == 0) {
128 #if defined WIN32 || defined _WIN32
129 snprintf(full_path, sizeof(full_path)-1, "%s\\addons\\metamod\\%s", g_ModPath.c_str(), "metaplugins.ini");
131 snprintf(full_path, sizeof(full_path)-1, "%s/addons/metamod/%s", g_ModPath.c_str(), "metaplugins.ini");
133 LoadPluginsFromFile(full_path);
136 } else if (strcmp(command, "list") == 0) {
137 SourceMM::CPluginManager::CPlugin *pl;
139 const char *status="";
140 const char *version=NULL;
141 const char *name=NULL;
142 const char *author=NULL;
144 CONMSG("-Id- %-16.15s %-8.7s %-12.11s %-8.7s\n", "Name", "Version", "Author", "Status");
145 for (i=g_PluginMngr._begin(); i!=g_PluginMngr._end(); i++)
150 if (pl->m_Status == Pl_Paused)
153 } else if (pl->m_Status == Pl_Running) {
155 } else if (pl->m_Status == Pl_Refused) {
157 } else if (pl->m_Status == Pl_Error) {
159 } else if (pl->m_Status == Pl_NotFound) {
165 version = pl->m_API->GetVersion();
166 author = pl->m_API->GetAuthor();
167 name = pl->m_API->GetName();
179 name = pl->m_File.c_str();
182 CONMSG("[%02d] %-16.15s %-8.7s %-12.11s %-8.7s\n", pl->m_Id, name, version, author, status);
188 } else if (strcmp(command, "cmds") == 0) {
191 int id = atoi(e->Cmd_Argv(2));
192 SourceMM::CPluginManager::CPlugin *pl = g_PluginMngr.FindById(id);
196 CONMSG("Plugin %d not found.\n", id);
202 CONMSG("Plugin %d is not loaded.\n", id);
204 CONMSG("Console commands for %s:\n", pl->m_API->GetName());
205 std::list<ConCommandBase *>::iterator ci;
208 for (ci=pl->m_Cmds.begin(); ci!=pl->m_Cmds.end(); ci++)
211 CONMSG(" [%5d] %-s\n", count, (*ci)->GetName());
215 CONMSG("Usage: meta cmds <id>\n");
219 } else if (strcmp(command, "cvars") == 0) {
222 int id = atoi(e->Cmd_Argv(2));
223 SourceMM::CPluginManager::CPlugin *pl = g_PluginMngr.FindById(id);
227 CONMSG("Plugin %d not found.\n", id);
233 CONMSG("Plugin %d is not loaded.\n", id);
235 CONMSG("Registered cvars for %s:\n", pl->m_API->GetName());
236 std::list<ConCommandBase *>::iterator ci;
239 for (ci=pl->m_Cvars.begin(); ci!=pl->m_Cvars.end(); ci++)
242 CONMSG(" [%5d] %-s\n", count, (*ci)->GetName());
246 CONMSG("Usage: meta cvars <id>\n");
250 } else if (strcmp(command, "info") == 0) {
253 int id = atoi(e->Cmd_Argv(2));
254 SourceMM::CPluginManager::CPlugin *pl = g_PluginMngr.FindById(id);
257 CONMSG("Plugin %d not found.\n", id);
263 CONMSG("Plugin %d is not loaded.\n", id);
265 if (pl->m_Status == Pl_Paused)
267 CONMSG("Plugin %d is paused.\n", id);
268 } else if (pl->m_Status == Pl_Running) {
269 CONMSG("Plugin %d is running.\n", id);
271 CONMSG(" Name: \"%s\" by %s\n", pl->m_API->GetName(), pl->m_API->GetAuthor());
272 CONMSG(" Version: %s\n", pl->m_API->GetVersion());
273 CONMSG(" Description: %s\n", pl->m_API->GetDescription());
274 CONMSG(" License: %s\n", pl->m_API->GetLicense());
275 CONMSG(" URL: %s\n", pl->m_API->GetURL());
276 CONMSG(" Details: API %03d, Date: %s\n", pl->m_API->GetApiVersion(), pl->m_API->GetDate());
278 CONMSG("File: %s\n\n", pl->m_File.c_str());
282 CONMSG("Usage: meta info <id>\n");
286 } else if (strcmp(command, "pause") == 0) {
289 int id = atoi(e->Cmd_Argv(2));
293 if (!g_PluginMngr.Pause(id, error, sizeof(error)-1))
295 CONMSG("Pause failed: %s\n", error);
299 CONMSG("Plugin %d has been paused.\n", id);
303 CONMSG("Usage: meta pause <id>\n");
307 } else if (strcmp(command, "unpause") == 0) {
310 int id = atoi(e->Cmd_Argv(2));
313 if (!g_PluginMngr.Unpause(id, error, sizeof(error)-1))
315 CONMSG("Unpause failed: %s\n", error);
319 CONMSG("Plugin %d has been unpaused.\n", id);
323 CONMSG("Usage: meta unpause <id>\n");
327 } else if (strcmp(command, "load") == 0) {
330 const char *file = e->Cmd_Argv(2);
333 if (file[0] == '/' || strcmp(&(file[1]), ":\\") == 0)
335 snprintf(full_path, sizeof(full_path)-1, "%s", file);
337 const char *ext = UTIL_GetExtension(file);
338 #if defined WIN32 || defined _WIN32
339 ext = ext ? "" : ".dll";
340 snprintf(full_path, sizeof(full_path)-1, "%s\\%s%s", g_ModPath.c_str(), file, ext);
342 ext = ext ? "" : "_i486.so";
343 snprintf(full_path, sizeof(full_path)-1, "%s/%s%s", g_ModPath.c_str(), file, ext);
349 SourceMM::CPluginManager::CPlugin *pl;
351 PluginId id = g_PluginMngr.Load(full_path, Pl_Console, already, error, sizeof(error)-1);
352 pl = g_PluginMngr.FindById(id);
353 if (!pl || id < Pl_MinId || (pl->m_Status < Pl_Paused))
355 CONMSG("Failed to load plugin %s (%s).\n", file, error);
361 CONMSG("Plugin \"%s\" loaded with id %d.\n", pl->m_API->GetName(), pl->m_Id);
363 CONMSG("Plugin \"%s\" is already loaded as %d.\n", pl->m_API->GetName(), pl->m_Id);
368 CONMSG("Usage: meta load <path>\n");
372 } else if (strcmp(command, "unload") == 0) {
375 int id = atoi(e->Cmd_Argv(2));
378 if (!g_PluginMngr.Unload(id, false, error, sizeof(error)-1))
380 CONMSG("Unload failed: %s\n", error);
384 CONMSG("Plugin %d unloaded.\n", id);
388 CONMSG("Usage: meta unload <id>\n");
392 } else if (strcmp(command, "force_unload") == 0) {
395 int id = atoi(e->Cmd_Argv(2));
398 if (!g_PluginMngr.Unload(id, false, error, sizeof(error)-1))
400 CONMSG("Force unload failed: %s\n", error);
404 CONMSG("Plugin %d force unloaded.\n", id);
408 CONMSG("Usage: meta force_unload <id>\n");
412 } else if (strcmp(command, "clear") == 0) {
413 if (!g_PluginMngr.UnloadAll())
415 CONMSG("One or more plugins resisted removal (cleaned anyway).\n");
419 CONMSG("All plugins unloaded.\n");
422 } else if (strcmp(command, "retry") == 0) {
425 int id = atoi(e->Cmd_Argv(2));
428 if (!g_PluginMngr.Retry(id, error, sizeof(error)-1))
430 CONMSG("Error reloading plugin: %s\n", error);
434 CONMSG("Plugin %d successfully reloaded.\n", id);
438 CONMSG("Usage: meta retry <id>\n");
445 CONMSG("Metamod:Source Menu\n");
446 CONMSG("usage: meta <command> [arguments]\n");
447 CONMSG(" clear - Unload all plugins forcefully\n");
448 CONMSG(" cmds - Show plugin commands\n");
449 CONMSG(" cvars - Show plugin cvars\n");
450 CONMSG(" credits - About Metamod:Source\n");
451 CONMSG(" force_unload - Forcefully unload a plugin\n");
452 CONMSG(" game - Information about GameDLL\n");
453 CONMSG(" info - Information about a plugin\n");
454 CONMSG(" list - List plugins\n");
455 CONMSG(" load - Load a plugin\n");
456 CONMSG(" pause - Pause a running plugin\n");
457 CONMSG(" refresh - Reparse plugins file\n");
458 CONMSG(" retry - Attempt to reload a plugin\n");
459 CONMSG(" unload - Unload a loaded plugin\n");
460 CONMSG(" unpause - Unpause a paused plugin\n");
461 CONMSG(" version - Version information\n");
464 int UTIL_CmpNocase(const std::string &s1, const std::string &s2)
466 std::string::const_iterator p1 = s1.begin();
467 std::string::const_iterator p2 = s2.begin();
469 while (p1 != s1.end() && p2 != s2.end())
471 if(toupper(*p1) != toupper(*p2))
472 return (toupper(*p1)<toupper(*p2)) ? -1 : 1;
477 return (s2.size() == s1.size()) ? 0 : (s1.size() < s2.size()) ? -1 : 1; // size is unsigned
480 CAlwaysRegisterableCommand::CAlwaysRegisterableCommand()
482 Create("", NULL, FCVAR_UNREGISTERED|FCVAR_GAMEDLL);
486 bool CAlwaysRegisterableCommand::IsRegistered( void ) const
491 void CAlwaysRegisterableCommand::BringToFront()
494 m_pICvar = g_Engine.icvar;
496 // First, let's try to find us!
497 ConCommandBase *pPtr = m_pICvar->GetCommands();
501 // We are already at the beginning; Nothing to do
507 if (pPtr == this && pPtr->IsCommand() && UTIL_CmpNocase(GetName(), pPtr->GetName()) == 0)
509 ConCommandBase *pPrev = NULL;
515 pPtr = const_cast<ConCommandBase*>(pPtr->GetNext());
517 if (pPrev && pPtr == this)
519 pPrev->SetNext(m_pNext); // Remove us from the list
522 m_pICvar->RegisterConCommandBase(this);
526 const char *GetPluginsFile()
528 return mm_pluginsfile.GetString();