1 /* ======== SourceMM ========
2 * Copyright (C) 2004-2005 Metamod:Source Development Team
3 * No warranties of any kind
7 * Author(s): David "BAILOPAN" Anderson
8 * ============================
12 #include "concommands.h"
17 * @brief Console Command Implementations
18 * @file concommands.cpp
21 CAlwaysRegisterableCommand g_EternalCommand;
22 SMConVarAccessor g_SMConVarAccessor;
24 bool SMConVarAccessor::RegisterConCommandBase(ConCommandBase *pCommand)
26 // Add the FCVAR_GAMEDLL flag
27 // => No crash on exit!
28 // UPDATE: Do _not_ add the FCVAR_GAMEDLL flag here, as it
29 // causes the command to be unusable on listenservers until you load a map
30 // We will set the FCVAR_GAMEDLL flag on all commands we have registered once we are being unloaded
31 //pCommand->AddFlags(FCVAR_GAMEDLL);
32 m_RegisteredCommands.push_back(pCommand);
34 pCommand->SetNext( NULL );
35 g_Engine.icvar->RegisterConCommandBase(pCommand);
40 bool SMConVarAccessor::Register(ConCommandBase *pCommand)
42 //simple, don't mark as part of sourcemm!
43 pCommand->SetNext( NULL );
44 g_Engine.icvar->RegisterConCommandBase(pCommand);
49 void SMConVarAccessor::MarkCommandsAsGameDLL()
51 for (std::list<ConCommandBase*>::iterator iter = m_RegisteredCommands.begin();
52 iter != m_RegisteredCommands.end(); ++iter)
54 (*iter)->AddFlags(FCVAR_GAMEDLL);
58 void SMConVarAccessor::Unregister(ConCommandBase *pCommand)
60 ICvar *cv = g_Engine.icvar;
61 ConCommandBase *ptr = cv->GetCommands();
66 g_EternalCommand.BringToFront();
67 g_EternalCommand.SetNext(const_cast<ConCommandBase *>(pCommand->GetNext()));
69 //find us and unregister us
70 ConCommandBase *pPrev = NULL;
76 ptr = const_cast<ConCommandBase *>(ptr->GetNext());
78 if (pPrev && ptr == pCommand)
80 pPrev->SetNext(const_cast<ConCommandBase *>(pCommand->GetNext()));
85 ConVar metamod_version("metamod_version", SOURCEMM_VERSION, FCVAR_REPLICATED | FCVAR_SPONLY | FCVAR_NOTIFY, "Metamod:Source Version");
87 CON_COMMAND(meta, "Metamod:Source Menu")
89 IVEngineServer *e = g_Engine.engine;
91 int args = e->Cmd_Argc();
95 const char *command = e->Cmd_Argv(1);
96 if (strcmp(command, "credits") == 0)
98 Msg("Metamod:Source was developed by:\n");
99 Msg(" SourceHook: Pavol \"PM OnoTo\" Marko\n");
100 Msg(" GameDLL/Plugins: David \"BAILOPAN\" Anderson\n");
101 Msg(" GameDLL: Scott \"Damaged Soul\" Ehlert\n");
102 Msg("For more information, see the official website\n");
103 Msg("http://www.sourcemm.net/\n");
106 } else if (strcmp(command, "version") == 0) {
107 Msg("Metamod:Source version %s\n", SOURCEMM_VERSION);
108 Msg("Compiled on: %s\n", SOURCEMM_DATE);
109 Msg("Plugin interface version: %d:%d\n", PLAPI_VERSION, PLAPI_MIN_VERSION);
110 Msg("SourceHook version: %d:%d\n", g_SourceHook.GetIfaceVersion(), g_SourceHook.GetImplVersion());
111 Msg("http://www.sourcemm.net/\n");
114 } else if (strcmp(command, "game") == 0) {
115 Msg("GameDLL Information\n");
116 Msg(" Mod path: %s\n", g_ModPath.c_str());
117 Msg(" Dll path: %s\n", g_BinPath.c_str());
120 } else if (strcmp(command, "refresh") == 0) {
122 #if defined WIN32 || defined _WIN32
123 snprintf(full_path, sizeof(full_path)-1, "%s\\addons\\metamod\\%s", g_ModPath.c_str(), "metaplugins.ini");
125 snprintf(full_path, sizeof(full_path)-1, "%s/addons/metamod/%s", g_ModPath.c_str(), "metaplugins.ini");
127 LoadPluginsFromFile(full_path);
130 } else if (strcmp(command, "list") == 0) {
131 SourceMM::CPluginManager::CPlugin *pl;
133 const char *status="";
134 const char *version=NULL;
135 const char *name=NULL;
136 const char *author=NULL;
138 Msg("-Id- %-16.15s %-8.7s %-12.11s %-8.7s\n", "Name", "Version", "Author", "Status");
139 for (i=g_PluginMngr._begin(); i!=g_PluginMngr._end(); i++)
144 if (pl->m_Status == Pl_Paused)
147 } else if (pl->m_Status == Pl_Running) {
149 } else if (pl->m_Status == Pl_Refused) {
151 } else if (pl->m_Status == Pl_Error) {
153 } else if (pl->m_Status == Pl_NotFound) {
159 version = pl->m_API->GetVersion();
160 author = pl->m_API->GetAuthor();
161 name = pl->m_API->GetName();
173 name = pl->m_File.c_str();
176 Msg("[%02d] %-16.15s %-8.7s %-12.11s %-8.7s\n", pl->m_Id, name, version, author, status);
182 } else if (strcmp(command, "cmds") == 0) {
185 int id = atoi(e->Cmd_Argv(2));
186 SourceMM::CPluginManager::CPlugin *pl = g_PluginMngr.FindById(id);
190 Msg("Plugin %d not found.\n", id);
196 Msg("Plugin %d is not loaded.\n", id);
198 Msg("Console commands for %s:\n", pl->m_API->GetName());
199 std::list<ConCommandBase *>::iterator ci;
202 for (ci=pl->m_Cmds.begin(); ci!=pl->m_Cmds.end(); ci++)
205 Msg(" [%5d] %-s\n", count, (*ci)->GetName());
209 Msg("Usage: meta cmds <id>\n");
213 } else if (strcmp(command, "cvars") == 0) {
216 int id = atoi(e->Cmd_Argv(2));
217 SourceMM::CPluginManager::CPlugin *pl = g_PluginMngr.FindById(id);
221 Msg("Plugin %d not found.\n", id);
227 Msg("Plugin %d is not loaded.\n", id);
229 Msg("Registered cvars for %s:\n", pl->m_API->GetName());
230 std::list<ConCommandBase *>::iterator ci;
233 for (ci=pl->m_Cvars.begin(); ci!=pl->m_Cvars.end(); ci++)
236 Msg(" [%5d] %-s\n", count, (*ci)->GetName());
240 Msg("Usage: meta cvars <id>\n");
244 } else if (strcmp(command, "info") == 0) {
247 int id = atoi(e->Cmd_Argv(2));
248 SourceMM::CPluginManager::CPlugin *pl = g_PluginMngr.FindById(id);
251 Msg("Plugin %d not found.\n", id);
257 Msg("Plugin %d is not loaded.\n", id);
259 if (pl->m_Status == Pl_Paused)
261 Msg("Plugin %d is paused.\n", id);
262 } else if (pl->m_Status == Pl_Running) {
263 Msg("Plugin %d is running.\n", id);
265 Msg(" Name: \"%s\" by %s\n", pl->m_API->GetName(), pl->m_API->GetAuthor());
266 Msg(" Version: %s\n", pl->m_API->GetVersion());
267 Msg(" Description: %s\n", pl->m_API->GetDescription());
268 Msg(" License: %s\n", pl->m_API->GetLicense());
269 Msg(" URL: %s\n", pl->m_API->GetURL());
270 Msg(" Details: API %03d, Date: %s\n", pl->m_API->GetApiVersion(), pl->m_API->GetDate());
272 Msg("File: %s\n\n", pl->m_File.c_str());
276 Msg("Usage: meta info <id>\n");
280 } else if (strcmp(command, "pause") == 0) {
283 int id = atoi(e->Cmd_Argv(2));
287 if (!g_PluginMngr.Pause(id, error, sizeof(error)-1))
289 Msg("Pause failed: %s\n", error);
293 Msg("Plugin %d has been paused.\n", id);
297 Msg("Usage: meta pause <id>\n");
301 } else if (strcmp(command, "unpause") == 0) {
304 int id = atoi(e->Cmd_Argv(2));
307 if (!g_PluginMngr.Unpause(id, error, sizeof(error)-1))
309 Msg("Unpause failed: %s\n", error);
313 Msg("Plugin %d has been unpaused.\n", id);
317 Msg("Usage: meta unpause <id>\n");
321 } else if (strcmp(command, "load") == 0) {
324 const char *file = e->Cmd_Argv(2);
327 if (file[0] == '/' || strcmp(&(file[1]), ":\\") == 0)
329 snprintf(full_path, sizeof(full_path)-1, "%s", file);
331 const char *ext = UTIL_GetExtension(file);
332 #if defined WIN32 || defined _WIN32
333 ext = ext ? "" : ".dll";
334 snprintf(full_path, sizeof(full_path)-1, "%s\\%s%s", g_ModPath.c_str(), file, ext);
336 ext = ext ? "" : "_i486.so";
337 snprintf(full_path, sizeof(full_path)-1, "%s/%s%s", g_ModPath.c_str(), file, ext);
343 SourceMM::CPluginManager::CPlugin *pl;
345 PluginId id = g_PluginMngr.Load(full_path, Pl_Console, already, error, sizeof(error)-1);
346 pl = g_PluginMngr.FindById(id);
347 if (!pl || id < Pl_MinId || (pl->m_Status < Pl_Paused))
349 Msg("Failed to load plugin %s (%s).\n", file, error);
355 Msg("Plugin \"%s\" loaded with id %d.\n", pl->m_API->GetName(), pl->m_Id);
357 Msg("Plugin \"%s\" is already loaded as %d.\n", pl->m_API->GetName(), pl->m_Id);
362 Msg("Usage: meta load <path>\n");
366 } else if (strcmp(command, "unload") == 0) {
369 int id = atoi(e->Cmd_Argv(2));
372 if (!g_PluginMngr.Unload(id, false, error, sizeof(error)-1))
374 Msg("Unload failed: %s\n", error);
378 Msg("Plugin %d unloaded.\n", id);
382 Msg("Usage: meta unload <id>\n");
386 } else if (strcmp(command, "force_unload") == 0) {
389 int id = atoi(e->Cmd_Argv(2));
392 if (!g_PluginMngr.Unload(id, false, error, sizeof(error)-1))
394 Msg("Force unload failed: %s\n", error);
398 Msg("Plugin %d force unloaded.\n", id);
402 Msg("Usage: meta force_unload <id>\n");
406 } else if (strcmp(command, "clear") == 0) {
407 if (!g_PluginMngr.UnloadAll())
409 Msg("One or more plugins resisted removal (cleaned anyway).\n");
413 Msg("All plugins unloaded.\n");
416 } else if (strcmp(command, "retry") == 0) {
419 int id = atoi(e->Cmd_Argv(2));
422 if (!g_PluginMngr.Retry(id, error, sizeof(error)-1))
424 Msg("Error reloading plugin: %s\n", error);
428 Msg("Plugin %d successfully reloaded.\n", id);
432 Msg("Usage: meta retry <id>\n");
439 Msg("Metamod:Source Menu\n");
440 Msg("usage: meta <command> [arguments]\n");
441 Msg(" clear - Unload all plugins forcefully\n");
442 Msg(" cmds - Show plugin commands\n");
443 Msg(" cvars - Show plugin cvars\n");
444 Msg(" credits - About Metamod:Source\n");
445 Msg(" force_unload - Forcefully unload a plugin\n");
446 Msg(" game - Information about GameDLL\n");
447 Msg(" info - Information about a plugin\n");
448 Msg(" list - List plugins\n");
449 Msg(" load - Load a plugin\n");
450 Msg(" pause - Pause a running plugin\n");
451 Msg(" refresh - Reparse plugins file\n");
452 Msg(" retry - Attempt to reload a plugin\n");
453 Msg(" unload - Unload a loaded plugin\n");
454 Msg(" unpause - Unpause a paused plugin\n");
455 Msg(" version - Version information\n");
458 int UTIL_CmpNocase(const std::string &s1, const std::string &s2)
460 std::string::const_iterator p1 = s1.begin();
461 std::string::const_iterator p2 = s2.begin();
463 while (p1 != s1.end() && p2 != s2.end())
465 if(toupper(*p1) != toupper(*p2))
466 return (toupper(*p1)<toupper(*p2)) ? -1 : 1;
471 return (s2.size() == s1.size()) ? 0 : (s1.size() < s2.size()) ? -1 : 1; // size is unsigned
474 CAlwaysRegisterableCommand::CAlwaysRegisterableCommand()
476 Create("", NULL, FCVAR_UNREGISTERED|FCVAR_GAMEDLL);
480 bool CAlwaysRegisterableCommand::IsRegistered( void ) const
485 void CAlwaysRegisterableCommand::BringToFront()
488 m_pICvar = g_Engine.icvar;
490 // First, let's try to find us!
491 ConCommandBase *pPtr = m_pICvar->GetCommands();
495 // We are already at the beginning; Nothing to do
501 if (pPtr == this && pPtr->IsCommand() && UTIL_CmpNocase(GetName(), pPtr->GetName()) == 0)
503 ConCommandBase *pPrev = NULL;
509 pPtr = const_cast<ConCommandBase*>(pPtr->GetNext());
511 if (pPrev && pPtr == this)
513 pPrev->SetNext(m_pNext); // Remove us from the list
516 m_pICvar->RegisterConCommandBase(this);