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"
19 * @brief Console Command Implementations
20 * @file concommands.cpp
23 CAlwaysRegisterableCommand g_EternalCommand;
24 SMConVarAccessor g_SMConVarAccessor;
26 bool SMConVarAccessor::RegisterConCommandBase(ConCommandBase *pCommand)
28 // Add the FCVAR_GAMEDLL flag
29 // => No crash on exit!
30 // UPDATE: Do _not_ add the FCVAR_GAMEDLL flag here, as it
31 // causes the command to be unusable on listenservers until you load a map
32 // We will set the FCVAR_GAMEDLL flag on all commands we have registered once we are being unloaded
33 //pCommand->AddFlags(FCVAR_GAMEDLL);
34 m_RegisteredCommands.push_back(pCommand);
36 pCommand->SetNext( NULL );
37 g_Engine.icvar->RegisterConCommandBase(pCommand);
42 bool SMConVarAccessor::Register(ConCommandBase *pCommand)
44 //simple, don't mark as part of sourcemm!
45 pCommand->SetNext( NULL );
46 g_Engine.icvar->RegisterConCommandBase(pCommand);
51 void SMConVarAccessor::MarkCommandsAsGameDLL()
53 for (SourceHook::List<ConCommandBase*>::iterator iter = m_RegisteredCommands.begin();
54 iter != m_RegisteredCommands.end(); ++iter)
56 (*iter)->AddFlags(FCVAR_GAMEDLL);
60 void SMConVarAccessor::Unregister(ConCommandBase *pCommand)
62 ICvar *cv = g_Engine.icvar;
63 ConCommandBase *ptr = cv->GetCommands();
68 g_EternalCommand.BringToFront();
69 g_EternalCommand.SetNext(const_cast<ConCommandBase *>(pCommand->GetNext()));
71 //find us and unregister us
72 ConCommandBase *pPrev = NULL;
78 ptr = const_cast<ConCommandBase *>(ptr->GetNext());
80 if (pPrev && ptr == pCommand)
82 pPrev->SetNext(const_cast<ConCommandBase *>(pCommand->GetNext()));
87 void SMConVarAccessor::UnregisterGameDLLCommands()
89 ConCommandBase *begin = g_Engine.icvar->GetCommands();
90 ConCommandBase *iter = begin;
91 ConCommandBase *prev = NULL;
94 // watch out for the ETERNAL COMMAND!
95 if (iter != &g_EternalCommand && iter->IsBitSet(FCVAR_GAMEDLL))
100 g_EternalCommand.BringToFront();
101 iter = const_cast<ConCommandBase*>(iter->GetNext());
102 g_EternalCommand.SetNext(iter);
103 prev = &g_EternalCommand;
108 iter = const_cast<ConCommandBase*>(iter->GetNext());
114 iter = const_cast<ConCommandBase*>(iter->GetNext());
118 ConVar metamod_version("metamod_version", SOURCEMM_VERSION, FCVAR_REPLICATED | FCVAR_SPONLY | FCVAR_NOTIFY, "Metamod:Source Version");
119 #if defined WIN32 || defined _WIN32
120 ConVar mm_pluginsfile("mm_pluginsfile", "addons\\metamod\\metaplugins.ini", FCVAR_SPONLY, "Metamod:Source Plugins File");
122 ConVar mm_pluginsfile("mm_pluginsfile", "addons/metamod/metaplugins.ini", FCVAR_SPONLY, "Metamod:Source Plugins File");
125 CON_COMMAND(meta, "Metamod:Source Menu")
127 IVEngineServer *e = g_Engine.engine;
129 int args = e->Cmd_Argc();
133 const char *command = e->Cmd_Argv(1);
134 if (strcmp(command, "credits") == 0)
136 CONMSG("Metamod:Source was developed by:\n");
137 CONMSG(" SourceHook: Pavol \"PM OnoTo\" Marko\n");
138 CONMSG(" GameDLL/Plugins: David \"BAILOPAN\" Anderson\n");
139 CONMSG(" GameDLL: Scott \"Damaged Soul\" Ehlert\n");
140 CONMSG("For more information, see the official website\n");
141 CONMSG("http://www.sourcemm.net/\n");
144 } else if (strcmp(command, "version") == 0) {
145 CONMSG("Metamod:Source version %s\n", SOURCEMM_VERSION);
146 CONMSG("Compiled on: %s\n", SOURCEMM_DATE);
147 CONMSG("Plugin interface version: %d:%d\n", PLAPI_VERSION, PLAPI_MIN_VERSION);
148 CONMSG("SourceHook version: %d:%d\n", g_SourceHook.GetIfaceVersion(), g_SourceHook.GetImplVersion());
149 CONMSG("http://www.sourcemm.net/\n");
152 } else if (strcmp(command, "game") == 0) {
153 CONMSG("GameDLL Information\n");
154 CONMSG(" Description: %s\n", g_GameDll.pGameDLL->GetGameDescription());
155 CONMSG(" Mod Path: %s\n", g_ModPath.c_str());
156 CONMSG(" DLL Path: %s\n", g_BinPath.c_str());
157 CONMSG(" Interface: ServerGameDLL%03d, ServerGameClients%03d\n", g_GameDllVersion, g_GameClientsVersion);
159 // Display user messages
160 if (g_SmmAPI.MsgCacheSuccessful())
164 int msgcount = g_SmmAPI.GetUserMessageCount();
168 CONMSG(" User Messages: %-32.31s %-5s %-5s\n", "Name", "Index", "Size");
170 for (int i = 0; i < msgcount; i++)
172 msgname = g_SmmAPI.GetUserMessage(i, &msgsize);
174 CONMSG(" %-32.31s %-5d %-5d\n", msgname, i, msgsize);
177 CONMSG(" %d user message%s in total\n", msgcount, (msgcount > 1) ? "s" : "");
179 CONMSG(" User Messages: None\n");
182 CONMSG(" User Messages: Failed to get list of user messages\n");
186 } else if (strcmp(command, "refresh") == 0) {
188 g_SmmAPI.PathFormat(full_path, sizeof(full_path), "%s/%s", g_ModPath.c_str(), GetPluginsFile());
190 LoadPluginsFromFile(full_path);
193 } else if (strcmp(command, "list") == 0) {
194 SourceMM::CPluginManager::CPlugin *pl;
196 const char *status="";
197 const char *version=NULL;
198 const char *name=NULL;
199 const char *author=NULL;
201 CONMSG("-Id- %-20.19s %-10.9s %-20.19s %-8.7s\n", "Name", "Version", "Author", "Status");
202 for (i=g_PluginMngr._begin(); i!=g_PluginMngr._end(); i++)
207 if (pl->m_Status == Pl_Paused)
210 } else if (pl->m_Status == Pl_Running) {
211 if (pl->m_API && pl->m_API->QueryRunning(NULL, 0))
215 } else if (pl->m_Status == Pl_Refused) {
217 } else if (pl->m_Status == Pl_Error) {
219 } else if (pl->m_Status == Pl_NotFound) {
225 version = pl->m_API->GetVersion();
226 author = pl->m_API->GetAuthor();
227 name = pl->m_API->GetName();
239 name = pl->m_File.c_str();
242 CONMSG("[%02d] %-20.19s %-10.9s %-20.19s %-8.7s\n", pl->m_Id, name, version, author, status);
248 } else if (strcmp(command, "cmds") == 0) {
251 int id = atoi(e->Cmd_Argv(2));
252 SourceMM::CPluginManager::CPlugin *pl = g_PluginMngr.FindById(id);
256 CONMSG("Plugin %d not found.\n", id);
262 CONMSG("Plugin %d is not loaded.\n", id);
264 CONMSG("Console commands for %s:\n", pl->m_API->GetName());
265 SourceHook::List<ConCommandBase *>::iterator ci;
268 for (ci=pl->m_Cmds.begin(); ci!=pl->m_Cmds.end(); ci++)
271 CONMSG(" [%5d] %-s\n", count, (*ci)->GetName());
275 CONMSG("Usage: meta cmds <id>\n");
279 } else if (strcmp(command, "cvars") == 0) {
282 int id = atoi(e->Cmd_Argv(2));
283 SourceMM::CPluginManager::CPlugin *pl = g_PluginMngr.FindById(id);
287 CONMSG("Plugin %d not found.\n", id);
293 CONMSG("Plugin %d is not loaded.\n", id);
295 CONMSG("Registered cvars for %s:\n", pl->m_API->GetName());
296 SourceHook::List<ConCommandBase *>::iterator ci;
299 for (ci=pl->m_Cvars.begin(); ci!=pl->m_Cvars.end(); ci++)
302 CONMSG(" [%5d] %-s\n", count, (*ci)->GetName());
306 CONMSG("Usage: meta cvars <id>\n");
310 } else if (strcmp(command, "info") == 0) {
313 int id = atoi(e->Cmd_Argv(2));
314 SourceMM::CPluginManager::CPlugin *pl = g_PluginMngr.FindById(id);
317 CONMSG("Plugin %d not found.\n", id);
323 CONMSG("Plugin %d is not loaded.\n", id);
325 if (pl->m_Status == Pl_Paused)
327 CONMSG("Plugin %d is paused.\n", id);
328 } else if (pl->m_Status == Pl_Running) {
331 if (pl->m_API && pl->m_API->QueryRunning(run_msg, sizeof(run_msg)-1))
335 CONMSG("Plugin %d is running.\n", id);
337 CONMSG("Plugin %d is stopped: %s\n", id, run_msg);
340 CONMSG(" Name: \"%s\" by %s\n", pl->m_API->GetName(), pl->m_API->GetAuthor());
341 CONMSG(" Version: %s\n", pl->m_API->GetVersion());
342 CONMSG(" Description: %s\n", pl->m_API->GetDescription());
343 CONMSG(" License: %s\n", pl->m_API->GetLicense());
344 CONMSG(" URL: %s\n", pl->m_API->GetURL());
345 CONMSG(" Details: API %03d, Date: %s\n", pl->m_API->GetApiVersion(), pl->m_API->GetDate());
347 CONMSG("File: %s\n\n", pl->m_File.c_str());
351 CONMSG("Usage: meta info <id>\n");
355 } else if (strcmp(command, "pause") == 0) {
358 int id = atoi(e->Cmd_Argv(2));
362 if (!g_PluginMngr.Pause(id, error, sizeof(error)))
364 CONMSG("Pause failed: %s\n", error);
368 CONMSG("Plugin %d has been paused.\n", id);
372 CONMSG("Usage: meta pause <id>\n");
376 } else if (strcmp(command, "unpause") == 0) {
379 int id = atoi(e->Cmd_Argv(2));
382 if (!g_PluginMngr.Unpause(id, error, sizeof(error)))
384 CONMSG("Unpause failed: %s\n", error);
388 CONMSG("Plugin %d has been unpaused.\n", id);
392 CONMSG("Usage: meta unpause <id>\n");
396 } else if (strcmp(command, "load") == 0) {
399 const char *file = e->Cmd_Argv(2);
402 const char *alias = g_PluginMngr.LookupAlias(file);
408 if (file[0] == '/' || strcmp(&(file[1]), ":\\") == 0)
410 g_SmmAPI.PathFormat(full_path, sizeof(full_path), "%s", file);
412 const char *ext = UTIL_GetExtension(file);
413 #if defined WIN32 || defined _WIN32
414 ext = ext ? "" : ".dll";
416 ext = ext ? "" : "_i486.so";
418 g_SmmAPI.PathFormat(full_path, sizeof(full_path), "%s/%s%s", g_ModPath.c_str(), file, ext);
423 SourceMM::CPluginManager::CPlugin *pl;
425 PluginId id = g_PluginMngr.Load(full_path, Pl_Console, already, error, sizeof(error));
426 pl = g_PluginMngr.FindById(id);
427 if (!pl || id < Pl_MinId || (pl->m_Status < Pl_Paused))
429 CONMSG("Failed to load plugin %s (%s).\n", file, error);
435 CONMSG("Plugin \"%s\" loaded with id %d.\n", pl->m_API->GetName(), pl->m_Id);
437 CONMSG("Plugin \"%s\" is already loaded as %d.\n", pl->m_API->GetName(), pl->m_Id);
442 CONMSG("Usage: meta load <path>\n");
446 } else if ( (strcmp(command, "alias") == 0) ||
447 (strcmp(command, "aliases") == 0) ) {
450 const char *alias = e->Cmd_Argv(2);
451 const char *value = e->Cmd_Argv(3);
453 g_PluginMngr.SetAlias(alias, value);
454 if (value[0] == '\0')
456 CONMSG("Deleted alias: %s.\n", alias);
458 CONMSG("Set alias \"%s\" to: %s\n", alias, value);
460 } else if (args >= 3) {
461 const char *alias = e->Cmd_Argv(2);
462 const char *value = g_PluginMngr.LookupAlias(alias);
465 CONMSG("Alias \"%s\" is set to: %s\n", alias, value);
467 CONMSG("Alias \"%s\" was not found.\n", alias);
470 SourceHook::List<SourceMM::CNameAlias *>::iterator iter, end;
471 SourceMM::CNameAlias *p;
473 iter = g_PluginMngr._alias_begin();
474 end = g_PluginMngr._alias_end();
478 CONMSG("%-10.9s %s\n", "Alias", "File");
480 for (; iter!=end; iter++)
483 CONMSG("%-10.9s %s\n", p->alias.c_str(), p->value.c_str());
487 CONMSG("%d aliases total.\n", total);
489 CONMSG("No aliases found.\n");
493 } else if (strcmp(command, "unload") == 0) {
496 const char *file = e->Cmd_Argv(2);
500 if (id == 0 && isalpha(file[0]))
503 const char *alias = g_PluginMngr.LookupAlias(file);
510 /* first check if it's a known filename */
511 if (file[0] == '/' || strcmp(&(file[1]), ":\\") == 0)
513 g_SmmAPI.PathFormat(full_path, sizeof(full_path), "%s", file);
515 const char *ext = UTIL_GetExtension(file);
516 #if defined WIN32 || defined _WIN32
517 ext = ext ? "" : ".dll";
519 ext = ext ? "" : "_i486.so";
521 g_SmmAPI.PathFormat(full_path, sizeof(full_path), "%s/%s%s", g_ModPath.c_str(), file, ext);
524 SourceHook::List<SourceMM::CPluginManager::CPlugin *>::iterator iter, end;
525 SourceMM::CPluginManager::CPlugin *pl;
526 iter = g_PluginMngr._begin();
527 end = g_PluginMngr._end();
528 for (; iter!=end; iter++)
531 if (strcmp(pl->m_File.c_str(), full_path) == 0)
539 CONMSG("Plugin \"%s\" not found.\n", full_path);
544 if (!g_PluginMngr.Unload(id, false, error, sizeof(error)))
546 CONMSG("Unload failed: %s\n", error);
550 CONMSG("Plugin %d unloaded.\n", id);
552 CONMSG("Usage: meta unload <id>\n");
555 } else if (strcmp(command, "force_unload") == 0) {
558 int id = atoi(e->Cmd_Argv(2));
561 if (!g_PluginMngr.Unload(id, false, error, sizeof(error)))
563 CONMSG("Force unload failed: %s\n", error);
567 CONMSG("Plugin %d force unloaded.\n", id);
571 CONMSG("Usage: meta force_unload <id>\n");
575 } else if (strcmp(command, "clear") == 0) {
576 if (!g_PluginMngr.UnloadAll())
578 CONMSG("One or more plugins resisted removal (cleaned anyway).\n");
582 CONMSG("All plugins unloaded.\n");
585 } else if (strcmp(command, "retry") == 0) {
588 int id = atoi(e->Cmd_Argv(2));
591 if (!g_PluginMngr.Retry(id, error, sizeof(error)))
593 CONMSG("Error reloading plugin: %s\n", error);
597 CONMSG("Plugin %d successfully reloaded.\n", id);
601 CONMSG("Usage: meta retry <id>\n");
608 CONMSG("Metamod:Source Menu\n");
609 CONMSG("usage: meta <command> [arguments]\n");
610 CONMSG(" alias - List or set an alias\n");
611 CONMSG(" clear - Unload all plugins forcefully\n");
612 CONMSG(" cmds - Show plugin commands\n");
613 CONMSG(" cvars - Show plugin cvars\n");
614 CONMSG(" credits - About Metamod:Source\n");
615 CONMSG(" force_unload - Forcefully unload a plugin\n");
616 CONMSG(" game - Information about GameDLL\n");
617 CONMSG(" info - Information about a plugin\n");
618 CONMSG(" list - List plugins\n");
619 CONMSG(" load - Load a plugin\n");
620 CONMSG(" pause - Pause a running plugin\n");
621 CONMSG(" refresh - Reparse plugins file\n");
622 CONMSG(" retry - Attempt to reload a plugin\n");
623 CONMSG(" unload - Unload a loaded plugin\n");
624 CONMSG(" unpause - Unpause a paused plugin\n");
625 CONMSG(" version - Version information\n");
628 CAlwaysRegisterableCommand::CAlwaysRegisterableCommand()
630 Create("", NULL, FCVAR_UNREGISTERED|FCVAR_GAMEDLL);
634 bool CAlwaysRegisterableCommand::IsRegistered( void ) const
639 void CAlwaysRegisterableCommand::BringToFront()
642 m_pICvar = g_Engine.icvar;
644 // First, let's try to find us!
645 ConCommandBase *pPtr = m_pICvar->GetCommands();
649 // We are already at the beginning; Nothing to do
655 if (pPtr == this && pPtr->IsCommand() && stricmp(GetName(), pPtr->GetName()) == 0)
657 ConCommandBase *pPrev = NULL;
663 pPtr = const_cast<ConCommandBase*>(pPtr->GetNext());
665 if (pPrev && pPtr == this)
667 pPrev->SetNext(m_pNext); // Remove us from the list
671 m_pICvar->RegisterConCommandBase(this);
675 void ClientCommand_handler(edict_t *client)
677 IVEngineServer *e = g_Engine.engine;
678 const char *cmd = e->Cmd_Argv(0);
680 if (strcmp(cmd, "meta") == 0)
682 int args = e->Cmd_Argc();
685 const char *subcmd = e->Cmd_Argv(1);
687 if (strcmp(subcmd, "credits") == 0)
689 CLIENT_CONMSG(client, "Metamod:Source was developed by:\n");
690 CLIENT_CONMSG(client, " SourceHook: Pavol \"PM OnoTo\" Marko\n");
691 CLIENT_CONMSG(client, " GameDLL/Plugins: David \"BAILOPAN\" Anderson\n");
692 CLIENT_CONMSG(client, " GameDLL: Scott \"Damaged Soul\" Ehlert\n");
693 CLIENT_CONMSG(client, "For more information, see the official website\n");
694 CLIENT_CONMSG(client, "http://www.sourcemm.net/\n");
696 RETURN_META(MRES_SUPERCEDE);
697 } else if(strcmp(subcmd, "version") == 0) {
698 CLIENT_CONMSG(client, "Metamod:Source version %s\n", SOURCEMM_VERSION);
699 CLIENT_CONMSG(client, "Compiled on: %s\n", SOURCEMM_DATE);
700 CLIENT_CONMSG(client, "Plugin interface version: %d:%d\n", PLAPI_VERSION, PLAPI_MIN_VERSION);
701 CLIENT_CONMSG(client, "SourceHook version: %d:%d\n", g_SourceHook.GetIfaceVersion(), g_SourceHook.GetImplVersion());
702 CLIENT_CONMSG(client, "http://www.sourcemm.net/\n");
704 RETURN_META(MRES_SUPERCEDE);
705 } else if(strcmp(subcmd, "list") == 0) {
706 SourceMM::CPluginManager::CPlugin *pl;
709 const char *version = NULL;
710 const char *name = NULL;
711 const char *author = NULL;
712 const char *status = NULL;
714 CLIENT_CONMSG(client, "-Id- %-20.19s %-10.9s %-20.19s %6s\n", "Name", "Version", "Author", "Status");
716 for (i=g_PluginMngr._begin(); i!=g_PluginMngr._end(); i++)
724 /* Only show plugins that are running or paused */
725 if (pl->m_API && (st == Pl_Running || st == Pl_Paused))
727 version = pl->m_API->GetVersion();
728 author = pl->m_API->GetAuthor();
729 name = pl->m_API->GetName();
731 if (st == Pl_Running && pl->m_API->QueryRunning(NULL, 0))
738 if (!version || !author || !name)
741 CLIENT_CONMSG(client, "[%02d] %-20.19s %-10.9s %-20.19s %6s\n", pl->m_Id, name, version, author, status);
745 RETURN_META(MRES_SUPERCEDE);
749 CLIENT_CONMSG(client, "Metamod:Source Menu\n");
750 CLIENT_CONMSG(client, "usage: meta <command>\n");
751 CLIENT_CONMSG(client, " credits - About Metamod:Source\n");
752 CLIENT_CONMSG(client, " list - List plugins\n");
753 CLIENT_CONMSG(client, " version - Version information\n");
755 RETURN_META(MRES_SUPERCEDE);
758 RETURN_META(MRES_IGNORED);
761 const char *GetPluginsFile()
763 return mm_pluginsfile.GetString();