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");
88 CON_COMMAND(meta, "Metamod:Source Menu")
90 IVEngineServer *e = g_Engine.engine;
92 int args = e->Cmd_Argc();
96 const char *command = e->Cmd_Argv(1);
97 if (strcmp(command, "credits") == 0)
99 CONMSG("Metamod:Source was developed by:\n");
100 CONMSG(" SourceHook: Pavol \"PM OnoTo\" Marko\n");
101 CONMSG(" GameDLL/Plugins: David \"BAILOPAN\" Anderson\n");
102 CONMSG(" GameDLL: Scott \"Damaged Soul\" Ehlert\n");
103 CONMSG("For more information, see the official website\n");
104 CONMSG("http://www.sourcemm.net/\n");
107 } else if (strcmp(command, "version") == 0) {
108 CONMSG("Metamod:Source version %s\n", SOURCEMM_VERSION);
109 CONMSG("Compiled on: %s\n", SOURCEMM_DATE);
110 CONMSG("Plugin interface version: %d:%d\n", PLAPI_VERSION, PLAPI_MIN_VERSION);
111 CONMSG("SourceHook version: %d:%d\n", g_SourceHook.GetIfaceVersion(), g_SourceHook.GetImplVersion());
112 CONMSG("http://www.sourcemm.net/\n");
115 } else if (strcmp(command, "game") == 0) {
116 CONMSG("GameDLL Information\n");
117 CONMSG(" Mod path: %s\n", g_ModPath.c_str());
118 CONMSG(" Dll path: %s\n", g_BinPath.c_str());
121 } else if (strcmp(command, "refresh") == 0) {
123 #if defined WIN32 || defined _WIN32
124 snprintf(full_path, sizeof(full_path)-1, "%s\\addons\\metamod\\%s", g_ModPath.c_str(), "metaplugins.ini");
126 snprintf(full_path, sizeof(full_path)-1, "%s/addons/metamod/%s", g_ModPath.c_str(), "metaplugins.ini");
128 LoadPluginsFromFile(full_path);
131 } else if (strcmp(command, "list") == 0) {
132 SourceMM::CPluginManager::CPlugin *pl;
134 const char *status="";
135 const char *version=NULL;
136 const char *name=NULL;
137 const char *author=NULL;
139 CONMSG("-Id- %-16.15s %-8.7s %-12.11s %-8.7s\n", "Name", "Version", "Author", "Status");
140 for (i=g_PluginMngr._begin(); i!=g_PluginMngr._end(); i++)
145 if (pl->m_Status == Pl_Paused)
148 } else if (pl->m_Status == Pl_Running) {
150 } else if (pl->m_Status == Pl_Refused) {
152 } else if (pl->m_Status == Pl_Error) {
154 } else if (pl->m_Status == Pl_NotFound) {
160 version = pl->m_API->GetVersion();
161 author = pl->m_API->GetAuthor();
162 name = pl->m_API->GetName();
174 name = pl->m_File.c_str();
177 CONMSG("[%02d] %-16.15s %-8.7s %-12.11s %-8.7s\n", pl->m_Id, name, version, author, status);
183 } else if (strcmp(command, "cmds") == 0) {
186 int id = atoi(e->Cmd_Argv(2));
187 SourceMM::CPluginManager::CPlugin *pl = g_PluginMngr.FindById(id);
191 CONMSG("Plugin %d not found.\n", id);
197 CONMSG("Plugin %d is not loaded.\n", id);
199 CONMSG("Console commands for %s:\n", pl->m_API->GetName());
200 std::list<ConCommandBase *>::iterator ci;
203 for (ci=pl->m_Cmds.begin(); ci!=pl->m_Cmds.end(); ci++)
206 CONMSG(" [%5d] %-s\n", count, (*ci)->GetName());
210 CONMSG("Usage: meta cmds <id>\n");
214 } else if (strcmp(command, "cvars") == 0) {
217 int id = atoi(e->Cmd_Argv(2));
218 SourceMM::CPluginManager::CPlugin *pl = g_PluginMngr.FindById(id);
222 CONMSG("Plugin %d not found.\n", id);
228 CONMSG("Plugin %d is not loaded.\n", id);
230 CONMSG("Registered cvars for %s:\n", pl->m_API->GetName());
231 std::list<ConCommandBase *>::iterator ci;
234 for (ci=pl->m_Cvars.begin(); ci!=pl->m_Cvars.end(); ci++)
237 CONMSG(" [%5d] %-s\n", count, (*ci)->GetName());
241 CONMSG("Usage: meta cvars <id>\n");
245 } else if (strcmp(command, "info") == 0) {
248 int id = atoi(e->Cmd_Argv(2));
249 SourceMM::CPluginManager::CPlugin *pl = g_PluginMngr.FindById(id);
252 CONMSG("Plugin %d not found.\n", id);
258 CONMSG("Plugin %d is not loaded.\n", id);
260 if (pl->m_Status == Pl_Paused)
262 CONMSG("Plugin %d is paused.\n", id);
263 } else if (pl->m_Status == Pl_Running) {
264 CONMSG("Plugin %d is running.\n", id);
266 CONMSG(" Name: \"%s\" by %s\n", pl->m_API->GetName(), pl->m_API->GetAuthor());
267 CONMSG(" Version: %s\n", pl->m_API->GetVersion());
268 CONMSG(" Description: %s\n", pl->m_API->GetDescription());
269 CONMSG(" License: %s\n", pl->m_API->GetLicense());
270 CONMSG(" URL: %s\n", pl->m_API->GetURL());
271 CONMSG(" Details: API %03d, Date: %s\n", pl->m_API->GetApiVersion(), pl->m_API->GetDate());
273 CONMSG("File: %s\n\n", pl->m_File.c_str());
277 CONMSG("Usage: meta info <id>\n");
281 } else if (strcmp(command, "pause") == 0) {
284 int id = atoi(e->Cmd_Argv(2));
288 if (!g_PluginMngr.Pause(id, error, sizeof(error)-1))
290 CONMSG("Pause failed: %s\n", error);
294 CONMSG("Plugin %d has been paused.\n", id);
298 CONMSG("Usage: meta pause <id>\n");
302 } else if (strcmp(command, "unpause") == 0) {
305 int id = atoi(e->Cmd_Argv(2));
308 if (!g_PluginMngr.Unpause(id, error, sizeof(error)-1))
310 CONMSG("Unpause failed: %s\n", error);
314 CONMSG("Plugin %d has been unpaused.\n", id);
318 CONMSG("Usage: meta unpause <id>\n");
322 } else if (strcmp(command, "load") == 0) {
325 const char *file = e->Cmd_Argv(2);
328 if (file[0] == '/' || strcmp(&(file[1]), ":\\") == 0)
330 snprintf(full_path, sizeof(full_path)-1, "%s", file);
332 const char *ext = UTIL_GetExtension(file);
333 #if defined WIN32 || defined _WIN32
334 ext = ext ? "" : ".dll";
335 snprintf(full_path, sizeof(full_path)-1, "%s\\%s%s", g_ModPath.c_str(), file, ext);
337 ext = ext ? "" : "_i486.so";
338 snprintf(full_path, sizeof(full_path)-1, "%s/%s%s", g_ModPath.c_str(), file, ext);
344 SourceMM::CPluginManager::CPlugin *pl;
346 PluginId id = g_PluginMngr.Load(full_path, Pl_Console, already, error, sizeof(error)-1);
347 pl = g_PluginMngr.FindById(id);
348 if (!pl || id < Pl_MinId || (pl->m_Status < Pl_Paused))
350 CONMSG("Failed to load plugin %s (%s).\n", file, error);
356 CONMSG("Plugin \"%s\" loaded with id %d.\n", pl->m_API->GetName(), pl->m_Id);
358 CONMSG("Plugin \"%s\" is already loaded as %d.\n", pl->m_API->GetName(), pl->m_Id);
363 CONMSG("Usage: meta load <path>\n");
367 } else if (strcmp(command, "unload") == 0) {
370 int id = atoi(e->Cmd_Argv(2));
373 if (!g_PluginMngr.Unload(id, false, error, sizeof(error)-1))
375 CONMSG("Unload failed: %s\n", error);
379 CONMSG("Plugin %d unloaded.\n", id);
383 CONMSG("Usage: meta unload <id>\n");
387 } else if (strcmp(command, "force_unload") == 0) {
390 int id = atoi(e->Cmd_Argv(2));
393 if (!g_PluginMngr.Unload(id, false, error, sizeof(error)-1))
395 CONMSG("Force unload failed: %s\n", error);
399 CONMSG("Plugin %d force unloaded.\n", id);
403 CONMSG("Usage: meta force_unload <id>\n");
407 } else if (strcmp(command, "clear") == 0) {
408 if (!g_PluginMngr.UnloadAll())
410 CONMSG("One or more plugins resisted removal (cleaned anyway).\n");
414 CONMSG("All plugins unloaded.\n");
417 } else if (strcmp(command, "retry") == 0) {
420 int id = atoi(e->Cmd_Argv(2));
423 if (!g_PluginMngr.Retry(id, error, sizeof(error)-1))
425 CONMSG("Error reloading plugin: %s\n", error);
429 CONMSG("Plugin %d successfully reloaded.\n", id);
433 CONMSG("Usage: meta retry <id>\n");
440 CONMSG("Metamod:Source Menu\n");
441 CONMSG("usage: meta <command> [arguments]\n");
442 CONMSG(" clear - Unload all plugins forcefully\n");
443 CONMSG(" cmds - Show plugin commands\n");
444 CONMSG(" cvars - Show plugin cvars\n");
445 CONMSG(" credits - About Metamod:Source\n");
446 CONMSG(" force_unload - Forcefully unload a plugin\n");
447 CONMSG(" game - Information about GameDLL\n");
448 CONMSG(" info - Information about a plugin\n");
449 CONMSG(" list - List plugins\n");
450 CONMSG(" load - Load a plugin\n");
451 CONMSG(" pause - Pause a running plugin\n");
452 CONMSG(" refresh - Reparse plugins file\n");
453 CONMSG(" retry - Attempt to reload a plugin\n");
454 CONMSG(" unload - Unload a loaded plugin\n");
455 CONMSG(" unpause - Unpause a paused plugin\n");
456 CONMSG(" version - Version information\n");
459 int UTIL_CmpNocase(const std::string &s1, const std::string &s2)
461 std::string::const_iterator p1 = s1.begin();
462 std::string::const_iterator p2 = s2.begin();
464 while (p1 != s1.end() && p2 != s2.end())
466 if(toupper(*p1) != toupper(*p2))
467 return (toupper(*p1)<toupper(*p2)) ? -1 : 1;
472 return (s2.size() == s1.size()) ? 0 : (s1.size() < s2.size()) ? -1 : 1; // size is unsigned
475 CAlwaysRegisterableCommand::CAlwaysRegisterableCommand()
477 Create("", NULL, FCVAR_UNREGISTERED|FCVAR_GAMEDLL);
481 bool CAlwaysRegisterableCommand::IsRegistered( void ) const
486 void CAlwaysRegisterableCommand::BringToFront()
489 m_pICvar = g_Engine.icvar;
491 // First, let's try to find us!
492 ConCommandBase *pPtr = m_pICvar->GetCommands();
496 // We are already at the beginning; Nothing to do
502 if (pPtr == this && pPtr->IsCommand() && UTIL_CmpNocase(GetName(), pPtr->GetName()) == 0)
504 ConCommandBase *pPrev = NULL;
510 pPtr = const_cast<ConCommandBase*>(pPtr->GetNext());
512 if (pPrev && pPtr == this)
514 pPrev->SetNext(m_pNext); // Remove us from the list
517 m_pICvar->RegisterConCommandBase(this);