3 * ======================================================
5 * Copyright (C) 2004-2008 AlliedModders LLC and authors.
7 * ======================================================
9 * This software is provided 'as-is', without any express or implied warranty.
10 * In no event will the authors be held liable for any damages arising from
11 * the use of this software.
13 * Permission is granted to anyone to use this software for any purpose,
14 * including commercial applications, and to alter it and redistribute it
15 * freely, subject to the following restrictions:
17 * 1. The origin of this software must not be misrepresented; you must not
18 * claim that you wrote the original software. If you use this software in a
19 * product, an acknowledgment in the product documentation would be
20 * appreciated but is not required.
21 * 2. Altered source versions must be plainly marked as such, and must not be
22 * misrepresented as being the original software.
23 * 3. This notice may not be removed or altered from any source distribution.
29 #include "metamod_oslink.h"
32 #include "metamod_util.h"
33 #include "metamod_console.h"
34 #include "metamod_plugins.h"
36 using namespace SourceMM;
37 using namespace SourceHook;
40 * @brief Console Command Implementations
41 * @file concommands.cpp
44 #define CONMSG g_Metamod.ConPrintf
45 #define CLIENT_CONMSG g_Metamod.ClientConPrintf
47 bool Command_Meta(IMetamodSourceCommandInfo *info)
49 unsigned int args = info->GetArgCount();
51 if (mm_IsVspBridged() && !mm_IsVspLoadComplete())
53 CONMSG("You must change the map to activate Metamod:Source.\n");
59 const char *command = info->GetArg(1);
60 if (strcmp(command, "credits") == 0)
62 CONMSG("Metamod:Source was developed by:\n");
63 CONMSG(" SourceHook: Pavol \"PM OnoTo\" Marko\n");
64 CONMSG(" GameDLL/Plugins: David \"BAILOPAN\" Anderson\n");
65 CONMSG(" GameDLL: Scott \"DS\" Ehlert\n");
66 CONMSG("For more information, see the official website\n");
67 CONMSG("http://www.metamodsource.net/\n");
71 else if (strcmp(command, "version") == 0)
73 CONMSG("Metamod:Source version %s\n", MMS_FULL_VERSION);
74 CONMSG("Build ID: %s\n", MMS_BUILD_UNIQUEID);
76 if (g_Metamod.IsLoadedAsGameDLL())
78 CONMSG("Loaded As: GameDLL (gameinfo.txt)\n");
82 CONMSG("Loaded As: Valve Server Plugin\n");
85 CONMSG("Compiled on: %s\n", SOURCEMM_DATE);
86 CONMSG("Plugin interface version: %d:%d\n", METAMOD_PLAPI_VERSION, PLAPI_MIN_VERSION);
87 CONMSG("SourceHook version: %d:%d\n", g_SHPtr->GetIfaceVersion(), g_SHPtr->GetImplVersion());
88 CONMSG("http://www.metamodsource.net/\n");
92 else if (strcmp(command, "game") == 0)
94 CONMSG("GameDLL Information\n");
95 CONMSG(" Description: %s\n", provider->GetGameDescription());
96 CONMSG(" Mod Path: %s\n", g_Metamod.GetBaseDir());
97 CONMSG(" DLL Path: %s\n", g_Metamod.GetGameBinaryPath());
98 CONMSG(" Interface: ServerGameDLL%03d\n", g_Metamod.GetGameDLLVersion());
101 int engine = g_Metamod.GetSourceEngineBuild();
102 if (engine == SOURCE_ENGINE_ORIGINAL)
104 CONMSG(" Engine: Original (pre-Episode 1)\n");
106 else if (engine == SOURCE_ENGINE_EPISODEONE)
108 CONMSG(" Engine: Episode 1 (2004)\n");
110 else if (engine == SOURCE_ENGINE_ORANGEBOX)
112 CONMSG(" Engine: Episode 2 (Orange Box, 2007)\n");
116 #if SOURCE_ENGINE == SE_LEFT4DEAD
117 CONMSG(" Engine: Left 4 Dead (2008)\n");
118 #elif SOURCE_ENGINE == SE_ORANGEBOX
119 CONMSG(" Engine: Episode 2 (Orange Box, 2007)\n");
121 CONMSG(" Engine: Dark Messiah (2006)\n");
124 // Display user messages
125 int messages = g_Metamod.GetUserMessageCount();
133 CONMSG(" User Messages: %-32.31s %-5s %-5s\n", "Name", "Index", "Size");
135 for (int i = 0; i < messages; i++)
137 msgname = g_Metamod.GetUserMessage(i, &msgsize);
139 CONMSG(" %-32.31s %-5d %-5d\n", msgname, i, msgsize);
142 CONMSG(" %d user message%s in total\n", messages, (messages > 1) ? "s" : "");
146 CONMSG(" User Messages: None\n");
151 CONMSG(" User Messages: Failed to get list of user messages\n");
156 else if (strcmp(command, "refresh") == 0)
158 char filepath[PATH_SIZE], vdfpath[PATH_SIZE];
159 g_Metamod.PathFormat(filepath,
162 g_Metamod.GetBaseDir(),
163 g_Metamod.GetPluginsFile());
164 g_Metamod.PathFormat(vdfpath,
167 g_Metamod.GetBaseDir(),
168 g_Metamod.GetVDFDir());
170 mm_LoadPlugins(filepath, vdfpath);
174 else if (strcmp(command, "list") == 0)
176 CPluginManager::CPlugin *pl;
182 int plnum = g_PluginMngr.GetPluginCount();
186 CONMSG("No plugins loaded.\n");
189 CONMSG("Listing %d plugin%s:\n", plnum, (plnum > 1) ? "s" : "");
192 for (i = g_PluginMngr._begin(); i != g_PluginMngr._end(); i++)
202 if (pl->m_Status != Pl_Running)
204 len += UTIL_Format(buffer, sizeof(buffer), " [%02d] <%s>", pl->m_Id, g_PluginMngr.GetStatusText(pl));
206 len += UTIL_Format(buffer, sizeof(buffer), " [%02d]", pl->m_Id);
209 if ((plapi = pl->m_API))
211 plname = IS_STR_FILLED(plapi->GetName()) ? plapi->GetName() : pl->m_File.c_str();
212 len += UTIL_Format(&buffer[len], sizeof(buffer)-len, " %s", plname);
214 if (IS_STR_FILLED(plapi->GetVersion()))
216 len += UTIL_Format(&buffer[len], sizeof(buffer)-len, " (%s)", plapi->GetVersion());
218 if (IS_STR_FILLED(plapi->GetAuthor()))
220 UTIL_Format(&buffer[len], sizeof(buffer)-len, " by %s", plapi->GetAuthor());
224 CONMSG("%s\n", buffer);
229 else if (strcmp(command, "cmds") == 0)
233 int id = atoi(info->GetArg(2));
234 CPluginManager::CPlugin *pl = g_PluginMngr.FindById(id);
238 CONMSG("Plugin %d not found.\n", id);
244 CONMSG("Plugin %d is not loaded.\n", id);
248 CONMSG("Console commands for %s:\n", pl->m_API->GetName());
249 List<ConCommandBase *>::iterator ci;
252 for (ci=pl->m_Cmds.begin(); ci!=pl->m_Cmds.end(); ci++)
255 CONMSG(" [%5d] %-s\n", count, (*ci)->GetName());
261 CONMSG("Usage: meta cmds <id>\n");
266 else if (strcmp(command, "cvars") == 0)
270 int id = atoi(info->GetArg(2));
271 CPluginManager::CPlugin *pl = g_PluginMngr.FindById(id);
275 CONMSG("Plugin %d not found.\n", id);
281 CONMSG("Plugin %d is not loaded.\n", id);
285 CONMSG("Registered cvars for %s:\n", pl->m_API->GetName());
286 List<ConCommandBase *>::iterator ci;
289 for (ci=pl->m_Cvars.begin(); ci!=pl->m_Cvars.end(); ci++)
292 CONMSG(" [%5d] %-s\n", count, (*ci)->GetName());
298 CONMSG("Usage: meta cvars <id>\n");
303 else if (strcmp(command, "info") == 0)
307 int id = atoi(info->GetArg(2));
308 CPluginManager::CPlugin *pl = g_PluginMngr.FindById(id);
311 CONMSG("Plugin %d not found.\n", id);
317 CONMSG("Plugin %d is not loaded.\n", id);
321 if (pl->m_Status == Pl_Paused)
323 CONMSG("Plugin %d is paused.\n", id);
325 else if (pl->m_Status == Pl_Running)
329 if (pl->m_API && pl->m_API->QueryRunning(run_msg, sizeof(run_msg)-1))
335 CONMSG("Plugin %d is running.\n", id);
339 CONMSG("Plugin %d is stopped: %s\n", id, run_msg);
342 CONMSG(" Name: \"%s\" by %s\n", pl->m_API->GetName(), pl->m_API->GetAuthor());
343 CONMSG(" Version: %s\n", pl->m_API->GetVersion());
344 CONMSG(" Description: %s\n", pl->m_API->GetDescription());
345 CONMSG(" License: %s\n", pl->m_API->GetLicense());
346 CONMSG(" URL: %s\n", pl->m_API->GetURL());
347 CONMSG(" Details: API %03d, Date: %s\n", pl->m_API->GetApiVersion(), pl->m_API->GetDate());
349 CONMSG("File: %s\n\n", pl->m_File.c_str());
355 CONMSG("Usage: meta info <id>\n");
360 else if (strcmp(command, "pause") == 0)
364 int id = atoi(info->GetArg(2));
368 if (!g_PluginMngr.Pause(id, error, sizeof(error)))
370 CONMSG("Pause failed: %s\n", error);
374 CONMSG("Plugin %d has been paused.\n", id);
380 CONMSG("Usage: meta pause <id>\n");
385 else if (strcmp(command, "unpause") == 0)
389 int id = atoi(info->GetArg(2));
392 if (!g_PluginMngr.Unpause(id, error, sizeof(error)))
394 CONMSG("Unpause failed: %s\n", error);
398 CONMSG("Plugin %d has been unpaused.\n", id);
404 CONMSG("Usage: meta unpause <id>\n");
409 else if (strcmp(command, "load") == 0)
413 const char *file = info->GetArg(2);
416 const char *alias = g_PluginMngr.LookupAlias(file);
422 if (file[0] == '/' || strcmp(&(file[1]), ":\\") == 0)
424 g_Metamod.PathFormat(full_path, sizeof(full_path), "%s", file);
428 const char *ext = UTIL_GetExtension(file);
429 #if defined WIN32 || defined _WIN32
430 ext = ext ? "" : ".dll";
432 ext = ext ? "" : "_i486.so";
434 g_Metamod.PathFormat(full_path,
437 g_Metamod.GetBaseDir(),
444 CPluginManager::CPlugin *pl;
446 PluginId id = g_PluginMngr.Load(full_path, Pl_Console, already, error, sizeof(error));
447 pl = g_PluginMngr.FindById(id);
448 if (!pl || id < Pl_MinId || (pl->m_Status < Pl_Paused))
450 CONMSG("Failed to load plugin %s (%s).\n", file, error);
456 CONMSG("Plugin \"%s\" loaded with id %d.\n", pl->m_API->GetName(), pl->m_Id);
458 CONMSG("Plugin \"%s\" is already loaded as %d.\n", pl->m_API->GetName(), pl->m_Id);
465 CONMSG("Usage: meta load <path>\n");
470 else if ( (strcmp(command, "alias") == 0) ||
471 (strcmp(command, "aliases") == 0) )
475 const char *alias = info->GetArg(2);
476 const char *value = info->GetArg(3);
478 g_PluginMngr.SetAlias(alias, value);
479 if (value == NULL || value[0] == '\0')
481 CONMSG("Deleted alias: %s.\n", alias);
483 CONMSG("Set alias \"%s\" to: %s\n", alias, value);
488 const char *alias = info->GetArg(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);
499 List<CNameAlias *>::iterator iter, end;
502 iter = g_PluginMngr._alias_begin();
503 end = g_PluginMngr._alias_end();
507 CONMSG("%-10.9s %s\n", "Alias", "File");
509 for (; iter!=end; iter++)
512 CONMSG("%-10.9s %s\n", p->alias.c_str(), p->value.c_str());
516 CONMSG("%d aliases total.\n", total);
520 CONMSG("No aliases found.\n");
525 else if (strcmp(command, "unload") == 0)
529 const char *file = info->GetArg(2);
533 if (id == 0 && isalpha(file[0]))
536 const char *alias = g_PluginMngr.LookupAlias(file);
543 /* first check if it's a known filename */
544 if (file[0] == '/' || strcmp(&(file[1]), ":\\") == 0)
546 g_Metamod.PathFormat(full_path, sizeof(full_path), "%s", file);
550 const char *ext = UTIL_GetExtension(file);
551 #if defined WIN32 || defined _WIN32
552 ext = ext ? "" : ".dll";
554 ext = ext ? "" : "_i486.so";
556 g_Metamod.PathFormat(full_path,
559 g_Metamod.GetBaseDir(),
564 List<CPluginManager::CPlugin *>::iterator iter, end;
565 CPluginManager::CPlugin *pl;
566 iter = g_PluginMngr._begin();
567 end = g_PluginMngr._end();
568 for (; iter!=end; iter++)
571 if (strcmp(pl->m_File.c_str(), full_path) == 0)
579 CONMSG("Plugin \"%s\" not found.\n", full_path);
584 if (!g_PluginMngr.Unload(id, false, error, sizeof(error)))
586 CONMSG("Unload failed: %s\n", error);
590 CONMSG("Plugin %d unloaded.\n", id);
594 CONMSG("Usage: meta unload <id>\n");
598 else if (strcmp(command, "force_unload") == 0)
602 int id = atoi(info->GetArg(2));
605 if (!g_PluginMngr.Unload(id, false, error, sizeof(error)))
607 CONMSG("Force unload failed: %s\n", error);
611 CONMSG("Plugin %d force unloaded.\n", id);
617 CONMSG("Usage: meta force_unload <id>\n");
622 else if (strcmp(command, "clear") == 0)
624 if (!g_PluginMngr.UnloadAll())
626 CONMSG("One or more plugins resisted removal (cleaned anyway).\n");
630 CONMSG("All plugins unloaded.\n");
634 else if (strcmp(command, "retry") == 0)
638 int id = atoi(info->GetArg(2));
641 if (!g_PluginMngr.Retry(id, error, sizeof(error)))
643 CONMSG("Error reloading plugin: %s\n", error);
647 CONMSG("Plugin %d successfully reloaded.\n", id);
653 CONMSG("Usage: meta retry <id>\n");
660 CONMSG("Metamod:Source Menu\n");
661 CONMSG("usage: meta <command> [arguments]\n");
662 CONMSG(" alias - List or set an alias\n");
663 CONMSG(" clear - Unload all plugins forcefully\n");
664 CONMSG(" cmds - Show plugin commands\n");
665 CONMSG(" cvars - Show plugin cvars\n");
666 CONMSG(" credits - About Metamod:Source\n");
667 CONMSG(" force_unload - Forcefully unload a plugin\n");
668 CONMSG(" game - Information about GameDLL\n");
669 CONMSG(" info - Information about a plugin\n");
670 CONMSG(" list - List plugins\n");
671 CONMSG(" load - Load a plugin\n");
672 CONMSG(" pause - Pause a running plugin\n");
673 CONMSG(" refresh - Reparse plugin files\n");
674 CONMSG(" retry - Attempt to reload a plugin\n");
675 CONMSG(" unload - Unload a loaded plugin\n");
676 CONMSG(" unpause - Unpause a paused plugin\n");
677 CONMSG(" version - Version information\n");
682 bool Command_ClientMeta(edict_t *client, IMetamodSourceCommandInfo *info)
684 const char *cmd = info->GetArg(0);
686 if (strcmp(cmd, "meta") == 0)
688 unsigned int args = info->GetArgCount();
691 const char *subcmd = info->GetArg(1);
693 if (strcmp(subcmd, "credits") == 0)
695 CLIENT_CONMSG(client, "Metamod:Source was developed by:\n");
696 CLIENT_CONMSG(client, " SourceHook: Pavol \"PM OnoTo\" Marko\n");
697 CLIENT_CONMSG(client, " GameDLL/Plugins: David \"BAILOPAN\" Anderson\n");
698 CLIENT_CONMSG(client, " GameDLL: Scott \"DS\" Ehlert\n");
699 CLIENT_CONMSG(client, "For more information, see the official website\n");
700 CLIENT_CONMSG(client, "http://www.metamodsource.net/\n");
704 else if(strcmp(subcmd, "version") == 0)
706 CLIENT_CONMSG(client, "Metamod:Source version %s\n", MMS_FULL_VERSION);
707 CLIENT_CONMSG(client, "Compiled on: %s\n", SOURCEMM_DATE);
708 CLIENT_CONMSG(client, "Plugin interface version: %d:%d\n", METAMOD_PLAPI_VERSION, PLAPI_MIN_VERSION);
709 CLIENT_CONMSG(client, "SourceHook version: %d:%d\n", g_SHPtr->GetIfaceVersion(), g_SHPtr->GetImplVersion());
710 CLIENT_CONMSG(client, "http://www.metamodsource.net/\n");
714 else if(strcmp(subcmd, "list") == 0)
716 CPluginManager::CPlugin *pl;
724 for (i = g_PluginMngr._begin(); i != g_PluginMngr._end(); i++, len=0)
727 if (pl && pl->m_Status == Pl_Running)
730 if (!plapi || !plapi->QueryRunning(NULL, 0))
736 len += UTIL_Format(buffer, sizeof(buffer), " [%02d]", plnum);
738 plname = IS_STR_FILLED(plapi->GetName()) ? plapi->GetName() : pl->m_File.c_str();
739 len += UTIL_Format(&buffer[len], sizeof(buffer)-len, " %s", plname);
741 if (IS_STR_FILLED(plapi->GetVersion()))
743 len += UTIL_Format(&buffer[len], sizeof(buffer)-len, " (%s)", plapi->GetVersion());
745 if (IS_STR_FILLED(plapi->GetAuthor()))
747 UTIL_Format(&buffer[len], sizeof(buffer)-len, " by %s", plapi->GetAuthor());
750 CLIENT_CONMSG(client, "%s\n", buffer);
756 CLIENT_CONMSG(client, "No active plugins loaded.\n");
763 CLIENT_CONMSG(client, "Metamod:Source Menu\n");
764 CLIENT_CONMSG(client, "usage: meta <command>\n");
765 CLIENT_CONMSG(client, " credits - About Metamod:Source\n");
766 CLIENT_CONMSG(client, " list - List plugins\n");
767 CLIENT_CONMSG(client, " version - Version information\n");