1 /* ======== SourceMM ========
2 * Copyright (C) 2004-2007 Metamod:Source Development Team
3 * No warranties of any kind
7 * Author(s): David "BAILOPAN" Anderson
8 * ============================
13 #include "concommands.h"
15 #include "sh_string.h"
18 using namespace SourceMM;
19 using namespace SourceHook;
22 * @brief Console Command Implementations
23 * @file concommands.cpp
26 CAlwaysRegisterableCommand g_EternalCommand;
27 SMConVarAccessor g_SMConVarAccessor;
29 bool SMConVarAccessor::RegisterConCommandBase(ConCommandBase *pCommand)
31 // Add the FCVAR_GAMEDLL flag
32 // => No crash on exit!
33 // UPDATE: Do _not_ add the FCVAR_GAMEDLL flag here, as it
34 // causes the command to be unusable on listenservers until you load a map
35 // We will set the FCVAR_GAMEDLL flag on all commands we have registered once we are being unloaded
36 //pCommand->AddFlags(FCVAR_GAMEDLL);
37 m_RegisteredCommands.push_back(pCommand);
39 pCommand->SetNext( NULL );
40 g_Engine.icvar->RegisterConCommandBase(pCommand);
45 bool SMConVarAccessor::Register(ConCommandBase *pCommand)
47 //simple, don't mark as part of sourcemm!
48 pCommand->SetNext( NULL );
49 g_Engine.icvar->RegisterConCommandBase(pCommand);
54 void SMConVarAccessor::MarkCommandsAsGameDLL()
56 for (List<ConCommandBase*>::iterator iter = m_RegisteredCommands.begin();
57 iter != m_RegisteredCommands.end(); ++iter)
59 (*iter)->AddFlags(FCVAR_GAMEDLL);
63 void SMConVarAccessor::Unregister(PluginId id, ConCommandBase *pCommand)
65 /* Notify via IMetamodListener */
67 CPluginManager::CPlugin *pPlugin;
68 List<IMetamodListener *>::iterator event;
69 IMetamodListener *pML;
70 for (iter=g_PluginMngr._begin(); iter!=g_PluginMngr._end(); iter++)
73 if (pPlugin->m_Status < Pl_Paused)
77 /* Only valid for plugins >= 12 (v1:6, SourceMM 1.5) */
78 if (pPlugin->m_API->GetApiVersion() < 12)
82 for (event=pPlugin->m_Events.begin();
83 event!=pPlugin->m_Events.end();
87 pML->OnUnlinkConCommandBase(id, pCommand);
91 ICvar *cv = g_Engine.icvar;
92 ConCommandBase *ptr = cv->GetCommands();
97 g_EternalCommand.BringToFront();
98 g_EternalCommand.SetNext(const_cast<ConCommandBase *>(pCommand->GetNext()));
100 /* Find us and unregister us */
101 ConCommandBase *pPrev = NULL;
107 ptr = const_cast<ConCommandBase *>(ptr->GetNext());
109 if (pPrev && ptr == pCommand)
111 pPrev->SetNext(const_cast<ConCommandBase *>(pCommand->GetNext()));
116 void SMConVarAccessor::UnregisterGameDLLCommands()
118 ConCommandBase *begin = g_Engine.icvar->GetCommands();
119 ConCommandBase *iter = begin;
120 ConCommandBase *prev = NULL;
123 /* Watch out for the ETERNAL COMMAND! */
124 if (iter != &g_EternalCommand && iter->IsBitSet(FCVAR_GAMEDLL))
129 g_EternalCommand.BringToFront();
130 iter = const_cast<ConCommandBase*>(iter->GetNext());
131 g_EternalCommand.SetNext(iter);
132 prev = &g_EternalCommand;
137 iter = const_cast<ConCommandBase*>(iter->GetNext());
143 iter = const_cast<ConCommandBase*>(iter->GetNext());
147 ConVar metamod_version("metamod_version", SOURCEMM_VERSION, FCVAR_REPLICATED | FCVAR_SPONLY | FCVAR_NOTIFY, "Metamod:Source Version");
148 #if defined WIN32 || defined _WIN32
149 ConVar mm_pluginsfile("mm_pluginsfile", "addons\\metamod\\metaplugins.ini", FCVAR_SPONLY, "Metamod:Source Plugins File");
151 ConVar mm_pluginsfile("mm_pluginsfile", "addons/metamod/metaplugins.ini", FCVAR_SPONLY, "Metamod:Source Plugins File");
154 CON_COMMAND(meta, "Metamod:Source Menu")
156 IVEngineServer *e = g_Engine.engine;
158 int args = e->Cmd_Argc();
162 const char *command = e->Cmd_Argv(1);
163 if (strcmp(command, "credits") == 0)
165 CONMSG("Metamod:Source was developed by:\n");
166 CONMSG(" SourceHook: Pavol \"PM OnoTo\" Marko\n");
167 CONMSG(" GameDLL/Plugins: David \"BAILOPAN\" Anderson\n");
168 CONMSG(" GameDLL: Scott \"Damaged Soul\" Ehlert\n");
169 CONMSG("For more information, see the official website\n");
170 CONMSG("http://www.sourcemm.net/\n");
173 } else if (strcmp(command, "version") == 0) {
174 CONMSG("Metamod:Source version %s\n", SOURCEMM_VERSION);
175 CONMSG("Compiled on: %s\n", SOURCEMM_DATE);
176 CONMSG("Plugin interface version: %d:%d\n", PLAPI_VERSION, PLAPI_MIN_VERSION);
177 CONMSG("SourceHook version: %d:%d\n", g_SourceHook.GetIfaceVersion(), g_SourceHook.GetImplVersion());
178 CONMSG("http://www.sourcemm.net/\n");
181 } else if (strcmp(command, "game") == 0) {
182 CONMSG("GameDLL Information\n");
183 CONMSG(" Description: %s\n", g_GameDll.pGameDLL->GetGameDescription());
184 CONMSG(" Mod Path: %s\n", g_ModPath.c_str());
185 CONMSG(" DLL Path: %s\n", g_BinPath.c_str());
186 CONMSG(" Interface: ServerGameDLL%03d, ServerGameClients%03d\n", g_GameDllVersion, g_GameClientsVersion);
188 // Display user messages
189 if (g_SmmAPI.MsgCacheSuccessful())
193 int msgcount = g_SmmAPI.GetUserMessageCount();
197 CONMSG(" User Messages: %-32.31s %-5s %-5s\n", "Name", "Index", "Size");
199 for (int i = 0; i < msgcount; i++)
201 msgname = g_SmmAPI.GetUserMessage(i, &msgsize);
203 CONMSG(" %-32.31s %-5d %-5d\n", msgname, i, msgsize);
206 CONMSG(" %d user message%s in total\n", msgcount, (msgcount > 1) ? "s" : "");
208 CONMSG(" User Messages: None\n");
211 CONMSG(" User Messages: Failed to get list of user messages\n");
215 } else if (strcmp(command, "refresh") == 0) {
217 g_SmmAPI.PathFormat(full_path, sizeof(full_path), "%s/%s", g_ModPath.c_str(), GetPluginsFile());
219 LoadPluginsFromFile(full_path);
222 } else if (strcmp(command, "list") == 0) {
223 CPluginManager::CPlugin *pl;
229 int plnum = g_PluginMngr.GetPluginCount();
233 CONMSG("No plugins loaded.\n");
236 CONMSG("Listing %d plugin%s:\n", plnum, (plnum > 1) ? "s" : "");
239 for (i = g_PluginMngr._begin(); i != g_PluginMngr._end(); i++)
249 if (pl->m_Status != Pl_Running)
251 len += UTIL_Format(buffer, sizeof(buffer), " [%02d] <%s>", pl->m_Id, g_PluginMngr.GetStatusText(pl));
253 len += UTIL_Format(buffer, sizeof(buffer), " [%02d]", pl->m_Id);
256 if ((plapi = pl->m_API))
258 plname = IS_STR_FILLED(plapi->GetName()) ? plapi->GetName() : pl->m_File.c_str();
259 len += UTIL_Format(&buffer[len], sizeof(buffer)-len, " %s", plname);
261 if (IS_STR_FILLED(plapi->GetVersion()))
263 len += UTIL_Format(&buffer[len], sizeof(buffer)-len, " (%s)", plapi->GetVersion());
265 if (IS_STR_FILLED(plapi->GetAuthor()))
267 UTIL_Format(&buffer[len], sizeof(buffer)-len, " by %s", plapi->GetAuthor());
271 CONMSG("%s\n", buffer);
275 } else if (strcmp(command, "cmds") == 0) {
278 int id = atoi(e->Cmd_Argv(2));
279 CPluginManager::CPlugin *pl = g_PluginMngr.FindById(id);
283 CONMSG("Plugin %d not found.\n", id);
289 CONMSG("Plugin %d is not loaded.\n", id);
291 CONMSG("Console commands for %s:\n", pl->m_API->GetName());
292 List<ConCommandBase *>::iterator ci;
295 for (ci=pl->m_Cmds.begin(); ci!=pl->m_Cmds.end(); ci++)
298 CONMSG(" [%5d] %-s\n", count, (*ci)->GetName());
302 CONMSG("Usage: meta cmds <id>\n");
306 } else if (strcmp(command, "cvars") == 0) {
309 int id = atoi(e->Cmd_Argv(2));
310 CPluginManager::CPlugin *pl = g_PluginMngr.FindById(id);
314 CONMSG("Plugin %d not found.\n", id);
320 CONMSG("Plugin %d is not loaded.\n", id);
322 CONMSG("Registered cvars for %s:\n", pl->m_API->GetName());
323 List<ConCommandBase *>::iterator ci;
326 for (ci=pl->m_Cvars.begin(); ci!=pl->m_Cvars.end(); ci++)
329 CONMSG(" [%5d] %-s\n", count, (*ci)->GetName());
333 CONMSG("Usage: meta cvars <id>\n");
337 } else if (strcmp(command, "info") == 0) {
340 int id = atoi(e->Cmd_Argv(2));
341 CPluginManager::CPlugin *pl = g_PluginMngr.FindById(id);
344 CONMSG("Plugin %d not found.\n", id);
350 CONMSG("Plugin %d is not loaded.\n", id);
352 if (pl->m_Status == Pl_Paused)
354 CONMSG("Plugin %d is paused.\n", id);
355 } else if (pl->m_Status == Pl_Running) {
358 if (pl->m_API && pl->m_API->QueryRunning(run_msg, sizeof(run_msg)-1))
362 CONMSG("Plugin %d is running.\n", id);
364 CONMSG("Plugin %d is stopped: %s\n", id, run_msg);
367 CONMSG(" Name: \"%s\" by %s\n", pl->m_API->GetName(), pl->m_API->GetAuthor());
368 CONMSG(" Version: %s\n", pl->m_API->GetVersion());
369 CONMSG(" Description: %s\n", pl->m_API->GetDescription());
370 CONMSG(" License: %s\n", pl->m_API->GetLicense());
371 CONMSG(" URL: %s\n", pl->m_API->GetURL());
372 CONMSG(" Details: API %03d, Date: %s\n", pl->m_API->GetApiVersion(), pl->m_API->GetDate());
374 CONMSG("File: %s\n\n", pl->m_File.c_str());
378 CONMSG("Usage: meta info <id>\n");
382 } else if (strcmp(command, "pause") == 0) {
385 int id = atoi(e->Cmd_Argv(2));
389 if (!g_PluginMngr.Pause(id, error, sizeof(error)))
391 CONMSG("Pause failed: %s\n", error);
395 CONMSG("Plugin %d has been paused.\n", id);
399 CONMSG("Usage: meta pause <id>\n");
403 } else if (strcmp(command, "unpause") == 0) {
406 int id = atoi(e->Cmd_Argv(2));
409 if (!g_PluginMngr.Unpause(id, error, sizeof(error)))
411 CONMSG("Unpause failed: %s\n", error);
415 CONMSG("Plugin %d has been unpaused.\n", id);
419 CONMSG("Usage: meta unpause <id>\n");
423 } else if (strcmp(command, "load") == 0) {
426 const char *file = e->Cmd_Argv(2);
429 const char *alias = g_PluginMngr.LookupAlias(file);
435 if (file[0] == '/' || strcmp(&(file[1]), ":\\") == 0)
437 g_SmmAPI.PathFormat(full_path, sizeof(full_path), "%s", file);
439 const char *ext = UTIL_GetExtension(file);
440 #if defined WIN32 || defined _WIN32
441 ext = ext ? "" : ".dll";
443 ext = ext ? "" : "_i486.so";
445 g_SmmAPI.PathFormat(full_path, sizeof(full_path), "%s/%s%s", g_ModPath.c_str(), file, ext);
450 CPluginManager::CPlugin *pl;
452 PluginId id = g_PluginMngr.Load(full_path, Pl_Console, already, error, sizeof(error));
453 pl = g_PluginMngr.FindById(id);
454 if (!pl || id < Pl_MinId || (pl->m_Status < Pl_Paused))
456 CONMSG("Failed to load plugin %s (%s).\n", file, error);
462 CONMSG("Plugin \"%s\" loaded with id %d.\n", pl->m_API->GetName(), pl->m_Id);
464 CONMSG("Plugin \"%s\" is already loaded as %d.\n", pl->m_API->GetName(), pl->m_Id);
469 CONMSG("Usage: meta load <path>\n");
473 } else if ( (strcmp(command, "alias") == 0) ||
474 (strcmp(command, "aliases") == 0) ) {
477 const char *alias = e->Cmd_Argv(2);
478 const char *value = e->Cmd_Argv(3);
480 g_PluginMngr.SetAlias(alias, value);
481 if (value[0] == '\0')
483 CONMSG("Deleted alias: %s.\n", alias);
485 CONMSG("Set alias \"%s\" to: %s\n", alias, value);
487 } else if (args >= 3) {
488 const char *alias = e->Cmd_Argv(2);
489 const char *value = g_PluginMngr.LookupAlias(alias);
492 CONMSG("Alias \"%s\" is set to: %s\n", alias, value);
494 CONMSG("Alias \"%s\" was not found.\n", alias);
497 List<CNameAlias *>::iterator iter, end;
500 iter = g_PluginMngr._alias_begin();
501 end = g_PluginMngr._alias_end();
505 CONMSG("%-10.9s %s\n", "Alias", "File");
507 for (; iter!=end; iter++)
510 CONMSG("%-10.9s %s\n", p->alias.c_str(), p->value.c_str());
514 CONMSG("%d aliases total.\n", total);
516 CONMSG("No aliases found.\n");
520 } else if (strcmp(command, "unload") == 0) {
523 const char *file = e->Cmd_Argv(2);
527 if (id == 0 && isalpha(file[0]))
530 const char *alias = g_PluginMngr.LookupAlias(file);
537 /* first check if it's a known filename */
538 if (file[0] == '/' || strcmp(&(file[1]), ":\\") == 0)
540 g_SmmAPI.PathFormat(full_path, sizeof(full_path), "%s", file);
542 const char *ext = UTIL_GetExtension(file);
543 #if defined WIN32 || defined _WIN32
544 ext = ext ? "" : ".dll";
546 ext = ext ? "" : "_i486.so";
548 g_SmmAPI.PathFormat(full_path, sizeof(full_path), "%s/%s%s", g_ModPath.c_str(), file, ext);
551 List<CPluginManager::CPlugin *>::iterator iter, end;
552 CPluginManager::CPlugin *pl;
553 iter = g_PluginMngr._begin();
554 end = g_PluginMngr._end();
555 for (; iter!=end; iter++)
558 if (strcmp(pl->m_File.c_str(), full_path) == 0)
566 CONMSG("Plugin \"%s\" not found.\n", full_path);
571 if (!g_PluginMngr.Unload(id, false, error, sizeof(error)))
573 CONMSG("Unload failed: %s\n", error);
577 CONMSG("Plugin %d unloaded.\n", id);
579 CONMSG("Usage: meta unload <id>\n");
582 } else if (strcmp(command, "force_unload") == 0) {
585 int id = atoi(e->Cmd_Argv(2));
588 if (!g_PluginMngr.Unload(id, false, error, sizeof(error)))
590 CONMSG("Force unload failed: %s\n", error);
594 CONMSG("Plugin %d force unloaded.\n", id);
598 CONMSG("Usage: meta force_unload <id>\n");
602 } else if (strcmp(command, "clear") == 0) {
603 if (!g_PluginMngr.UnloadAll())
605 CONMSG("One or more plugins resisted removal (cleaned anyway).\n");
609 CONMSG("All plugins unloaded.\n");
612 } else if (strcmp(command, "retry") == 0) {
615 int id = atoi(e->Cmd_Argv(2));
618 if (!g_PluginMngr.Retry(id, error, sizeof(error)))
620 CONMSG("Error reloading plugin: %s\n", error);
624 CONMSG("Plugin %d successfully reloaded.\n", id);
628 CONMSG("Usage: meta retry <id>\n");
635 CONMSG("Metamod:Source Menu\n");
636 CONMSG("usage: meta <command> [arguments]\n");
637 CONMSG(" alias - List or set an alias\n");
638 CONMSG(" clear - Unload all plugins forcefully\n");
639 CONMSG(" cmds - Show plugin commands\n");
640 CONMSG(" cvars - Show plugin cvars\n");
641 CONMSG(" credits - About Metamod:Source\n");
642 CONMSG(" force_unload - Forcefully unload a plugin\n");
643 CONMSG(" game - Information about GameDLL\n");
644 CONMSG(" info - Information about a plugin\n");
645 CONMSG(" list - List plugins\n");
646 CONMSG(" load - Load a plugin\n");
647 CONMSG(" pause - Pause a running plugin\n");
648 CONMSG(" refresh - Reparse plugins file\n");
649 CONMSG(" retry - Attempt to reload a plugin\n");
650 CONMSG(" unload - Unload a loaded plugin\n");
651 CONMSG(" unpause - Unpause a paused plugin\n");
652 CONMSG(" version - Version information\n");
655 CAlwaysRegisterableCommand::CAlwaysRegisterableCommand()
657 Create("", NULL, FCVAR_UNREGISTERED|FCVAR_GAMEDLL);
661 bool CAlwaysRegisterableCommand::IsRegistered( void ) const
666 void CAlwaysRegisterableCommand::BringToFront()
669 m_pICvar = g_Engine.icvar;
671 // First, let's try to find us!
672 ConCommandBase *pPtr = m_pICvar->GetCommands();
676 // We are already at the beginning; Nothing to do
682 if (pPtr == this && pPtr->IsCommand() && stricmp(GetName(), pPtr->GetName()) == 0)
684 ConCommandBase *pPrev = NULL;
690 pPtr = const_cast<ConCommandBase*>(pPtr->GetNext());
692 if (pPrev && pPtr == this)
694 pPrev->SetNext(m_pNext); // Remove us from the list
698 m_pICvar->RegisterConCommandBase(this);
702 void ClientCommand_handler(edict_t *client)
704 IVEngineServer *e = g_Engine.engine;
705 const char *cmd = e->Cmd_Argv(0);
707 if (strcmp(cmd, "meta") == 0)
709 int args = e->Cmd_Argc();
712 const char *subcmd = e->Cmd_Argv(1);
714 if (strcmp(subcmd, "credits") == 0)
716 CLIENT_CONMSG(client, "Metamod:Source was developed by:\n");
717 CLIENT_CONMSG(client, " SourceHook: Pavol \"PM OnoTo\" Marko\n");
718 CLIENT_CONMSG(client, " GameDLL/Plugins: David \"BAILOPAN\" Anderson\n");
719 CLIENT_CONMSG(client, " GameDLL: Scott \"Damaged Soul\" Ehlert\n");
720 CLIENT_CONMSG(client, "For more information, see the official website\n");
721 CLIENT_CONMSG(client, "http://www.sourcemm.net/\n");
723 RETURN_META(MRES_SUPERCEDE);
724 } else if(strcmp(subcmd, "version") == 0) {
725 CLIENT_CONMSG(client, "Metamod:Source version %s\n", SOURCEMM_VERSION);
726 CLIENT_CONMSG(client, "Compiled on: %s\n", SOURCEMM_DATE);
727 CLIENT_CONMSG(client, "Plugin interface version: %d:%d\n", PLAPI_VERSION, PLAPI_MIN_VERSION);
728 CLIENT_CONMSG(client, "SourceHook version: %d:%d\n", g_SourceHook.GetIfaceVersion(), g_SourceHook.GetImplVersion());
729 CLIENT_CONMSG(client, "http://www.sourcemm.net/\n");
731 RETURN_META(MRES_SUPERCEDE);
732 } else if(strcmp(subcmd, "list") == 0) {
733 CPluginManager::CPlugin *pl;
741 for (i = g_PluginMngr._begin(); i != g_PluginMngr._end(); i++, len=0)
744 if (pl && pl->m_Status == Pl_Running)
747 if (!plapi || !plapi->QueryRunning(NULL, 0))
753 len += UTIL_Format(buffer, sizeof(buffer), " [%02d]", plnum);
755 plname = IS_STR_FILLED(plapi->GetName()) ? plapi->GetName() : pl->m_File.c_str();
756 len += UTIL_Format(&buffer[len], sizeof(buffer)-len, " %s", plname);
758 if (IS_STR_FILLED(plapi->GetVersion()))
760 len += UTIL_Format(&buffer[len], sizeof(buffer)-len, " (%s)", plapi->GetVersion());
762 if (IS_STR_FILLED(plapi->GetAuthor()))
764 UTIL_Format(&buffer[len], sizeof(buffer)-len, " by %s", plapi->GetAuthor());
767 CLIENT_CONMSG(client, "%s\n", buffer);
773 CLIENT_CONMSG(client, "No active plugins loaded.\n");
776 RETURN_META(MRES_SUPERCEDE);
780 CLIENT_CONMSG(client, "Metamod:Source Menu\n");
781 CLIENT_CONMSG(client, "usage: meta <command>\n");
782 CLIENT_CONMSG(client, " credits - About Metamod:Source\n");
783 CLIENT_CONMSG(client, " list - List plugins\n");
784 CLIENT_CONMSG(client, " version - Version information\n");
786 RETURN_META(MRES_SUPERCEDE);
789 RETURN_META(MRES_IGNORED);
792 const char *GetPluginsFile()
794 return mm_pluginsfile.GetString();