3 * ======================================================
5 * Copyright (C) 2004-2007 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.
28 #include "metamod_oslink.h"
35 #include "metamod_util.h"
36 #include "metamod_console.h"
37 #include "metamod_plugins.h"
43 using namespace SourceMM;
44 using namespace SourceHook;
47 * @brief Console Command Implementations
48 * @file concommands.cpp
51 #define CONMSG g_Metamod.ConPrintf
52 #define CLIENT_CONMSG g_Metamod.ClientConPrintf
54 bool Command_Meta(IMetamodSourceCommandInfo *info)
56 unsigned int args = info->GetArgCount();
58 if (provider->IsAlternatelyLoaded() && !g_Metamod.IsAlternateLoadComplete())
60 CONMSG("You must change the map to activate Metamod:Source.\n");
66 const char *command = info->GetArg(1);
67 if (strcmp(command, "credits") == 0)
69 CONMSG("Metamod:Source was developed by:\n");
70 CONMSG(" SourceHook: Pavol \"PM OnoTo\" Marko\n");
71 CONMSG(" GameDLL/Plugins: David \"BAILOPAN\" Anderson\n");
72 CONMSG(" GameDLL: Scott \"Damaged Soul\" Ehlert\n");
73 CONMSG("For more information, see the official website\n");
74 CONMSG("http://www.sourcemm.net/\n");
78 else if (strcmp(command, "version") == 0)
80 CONMSG("Metamod:Source version %s\n", SOURCEMM_VERSION);
82 if (g_Metamod.IsLoadedAsGameDLL())
84 CONMSG("Loaded As: GameDLL (gameinfo.txt)\n");
88 CONMSG("Loaded As: Valve Server Plugin\n");
91 CONMSG("Compiled on: %s\n", SOURCEMM_DATE);
92 CONMSG("Plugin interface version: %d:%d\n", METAMOD_PLAPI_VERSION, PLAPI_MIN_VERSION);
93 CONMSG("SourceHook version: %d:%d\n", g_SHPtr->GetIfaceVersion(), g_SHPtr->GetImplVersion());
94 CONMSG("http://www.sourcemm.net/\n");
98 else if (strcmp(command, "game") == 0)
100 CONMSG("GameDLL Information\n");
101 CONMSG(" Description: %s\n", provider->GetGameDescription());
102 CONMSG(" Mod Path: %s\n", g_Metamod.GetBaseDir());
103 CONMSG(" DLL Path: %s\n", g_Metamod.GetGameBinaryPath());
104 CONMSG(" Interface: ServerGameDLL%03d\n", g_Metamod.GetGameDLLVersion());
106 int engine = g_Metamod.GetSourceEngineBuild();
107 if (engine == SOURCE_ENGINE_ORIGINAL)
109 CONMSG(" Engine: Original (pre-Episode 1)\n");
111 else if (engine == SOURCE_ENGINE_EPISODEONE)
113 CONMSG(" Engine: Episode 1 (2004)\n");
115 else if (engine == SOURCE_ENGINE_ORANGEBOX)
117 CONMSG(" Engine: Episode 2 (Orange Box, 2007)\n");
120 // Display user messages
121 int messages = g_Metamod.GetUserMessageCount();
129 CONMSG(" User Messages: %-32.31s %-5s %-5s\n", "Name", "Index", "Size");
131 for (int i = 0; i < messages; i++)
133 msgname = g_Metamod.GetUserMessage(i, &msgsize);
135 CONMSG(" %-32.31s %-5d %-5d\n", msgname, i, msgsize);
138 CONMSG(" %d user message%s in total\n", messages, (messages > 1) ? "s" : "");
142 CONMSG(" User Messages: None\n");
147 CONMSG(" User Messages: Failed to get list of user messages\n");
152 else if (strcmp(command, "refresh") == 0)
155 g_Metamod.PathFormat(full_path,
158 g_Metamod.GetBaseDir(),
159 g_Metamod.GetPluginsFile());
161 LoadPluginsFromFile(full_path);
165 else if (strcmp(command, "list") == 0)
167 CPluginManager::CPlugin *pl;
173 int plnum = g_PluginMngr.GetPluginCount();
177 CONMSG("No plugins loaded.\n");
180 CONMSG("Listing %d plugin%s:\n", plnum, (plnum > 1) ? "s" : "");
183 for (i = g_PluginMngr._begin(); i != g_PluginMngr._end(); i++)
193 if (pl->m_Status != Pl_Running)
195 len += UTIL_Format(buffer, sizeof(buffer), " [%02d] <%s>", pl->m_Id, g_PluginMngr.GetStatusText(pl));
197 len += UTIL_Format(buffer, sizeof(buffer), " [%02d]", pl->m_Id);
200 if ((plapi = pl->m_API))
202 plname = IS_STR_FILLED(plapi->GetName()) ? plapi->GetName() : pl->m_File.c_str();
203 len += UTIL_Format(&buffer[len], sizeof(buffer)-len, " %s", plname);
205 if (IS_STR_FILLED(plapi->GetVersion()))
207 len += UTIL_Format(&buffer[len], sizeof(buffer)-len, " (%s)", plapi->GetVersion());
209 if (IS_STR_FILLED(plapi->GetAuthor()))
211 UTIL_Format(&buffer[len], sizeof(buffer)-len, " by %s", plapi->GetAuthor());
215 CONMSG("%s\n", buffer);
220 else if (strcmp(command, "cmds") == 0)
224 int id = atoi(info->GetArg(2));
225 CPluginManager::CPlugin *pl = g_PluginMngr.FindById(id);
229 CONMSG("Plugin %d not found.\n", id);
235 CONMSG("Plugin %d is not loaded.\n", id);
239 CONMSG("Console commands for %s:\n", pl->m_API->GetName());
240 List<ConCommandBase *>::iterator ci;
243 for (ci=pl->m_Cmds.begin(); ci!=pl->m_Cmds.end(); ci++)
246 CONMSG(" [%5d] %-s\n", count, (*ci)->GetName());
252 CONMSG("Usage: meta cmds <id>\n");
257 else if (strcmp(command, "cvars") == 0)
261 int id = atoi(info->GetArg(2));
262 CPluginManager::CPlugin *pl = g_PluginMngr.FindById(id);
266 CONMSG("Plugin %d not found.\n", id);
272 CONMSG("Plugin %d is not loaded.\n", id);
276 CONMSG("Registered cvars for %s:\n", pl->m_API->GetName());
277 List<ConCommandBase *>::iterator ci;
280 for (ci=pl->m_Cvars.begin(); ci!=pl->m_Cvars.end(); ci++)
283 CONMSG(" [%5d] %-s\n", count, (*ci)->GetName());
289 CONMSG("Usage: meta cvars <id>\n");
294 else if (strcmp(command, "info") == 0)
298 int id = atoi(info->GetArg(2));
299 CPluginManager::CPlugin *pl = g_PluginMngr.FindById(id);
302 CONMSG("Plugin %d not found.\n", id);
308 CONMSG("Plugin %d is not loaded.\n", id);
312 if (pl->m_Status == Pl_Paused)
314 CONMSG("Plugin %d is paused.\n", id);
316 else if (pl->m_Status == Pl_Running)
320 if (pl->m_API && pl->m_API->QueryRunning(run_msg, sizeof(run_msg)-1))
326 CONMSG("Plugin %d is running.\n", id);
330 CONMSG("Plugin %d is stopped: %s\n", id, run_msg);
333 CONMSG(" Name: \"%s\" by %s\n", pl->m_API->GetName(), pl->m_API->GetAuthor());
334 CONMSG(" Version: %s\n", pl->m_API->GetVersion());
335 CONMSG(" Description: %s\n", pl->m_API->GetDescription());
336 CONMSG(" License: %s\n", pl->m_API->GetLicense());
337 CONMSG(" URL: %s\n", pl->m_API->GetURL());
338 CONMSG(" Details: API %03d, Date: %s\n", pl->m_API->GetApiVersion(), pl->m_API->GetDate());
340 CONMSG("File: %s\n\n", pl->m_File.c_str());
346 CONMSG("Usage: meta info <id>\n");
351 else if (strcmp(command, "pause") == 0)
355 int id = atoi(info->GetArg(2));
359 if (!g_PluginMngr.Pause(id, error, sizeof(error)))
361 CONMSG("Pause failed: %s\n", error);
365 CONMSG("Plugin %d has been paused.\n", id);
371 CONMSG("Usage: meta pause <id>\n");
376 else if (strcmp(command, "unpause") == 0)
380 int id = atoi(info->GetArg(2));
383 if (!g_PluginMngr.Unpause(id, error, sizeof(error)))
385 CONMSG("Unpause failed: %s\n", error);
389 CONMSG("Plugin %d has been unpaused.\n", id);
395 CONMSG("Usage: meta unpause <id>\n");
400 else if (strcmp(command, "load") == 0)
404 const char *file = info->GetArg(2);
407 const char *alias = g_PluginMngr.LookupAlias(file);
413 if (file[0] == '/' || strcmp(&(file[1]), ":\\") == 0)
415 g_Metamod.PathFormat(full_path, sizeof(full_path), "%s", file);
419 const char *ext = UTIL_GetExtension(file);
420 #if defined WIN32 || defined _WIN32
421 ext = ext ? "" : ".dll";
423 ext = ext ? "" : "_i486.so";
425 g_Metamod.PathFormat(full_path,
428 g_Metamod.GetBaseDir(),
435 CPluginManager::CPlugin *pl;
437 PluginId id = g_PluginMngr.Load(full_path, Pl_Console, already, error, sizeof(error));
438 pl = g_PluginMngr.FindById(id);
439 if (!pl || id < Pl_MinId || (pl->m_Status < Pl_Paused))
441 CONMSG("Failed to load plugin %s (%s).\n", file, error);
447 CONMSG("Plugin \"%s\" loaded with id %d.\n", pl->m_API->GetName(), pl->m_Id);
449 CONMSG("Plugin \"%s\" is already loaded as %d.\n", pl->m_API->GetName(), pl->m_Id);
456 CONMSG("Usage: meta load <path>\n");
461 else if ( (strcmp(command, "alias") == 0) ||
462 (strcmp(command, "aliases") == 0) )
466 const char *alias = info->GetArg(2);
467 const char *value = info->GetArg(3);
469 g_PluginMngr.SetAlias(alias, value);
470 if (value == NULL || value[0] == '\0')
472 CONMSG("Deleted alias: %s.\n", alias);
474 CONMSG("Set alias \"%s\" to: %s\n", alias, value);
479 const char *alias = info->GetArg(2);
480 const char *value = g_PluginMngr.LookupAlias(alias);
483 CONMSG("Alias \"%s\" is set to: %s\n", alias, value);
485 CONMSG("Alias \"%s\" was not found.\n", alias);
490 List<CNameAlias *>::iterator iter, end;
493 iter = g_PluginMngr._alias_begin();
494 end = g_PluginMngr._alias_end();
498 CONMSG("%-10.9s %s\n", "Alias", "File");
500 for (; iter!=end; iter++)
503 CONMSG("%-10.9s %s\n", p->alias.c_str(), p->value.c_str());
507 CONMSG("%d aliases total.\n", total);
511 CONMSG("No aliases found.\n");
516 else if (strcmp(command, "unload") == 0)
520 const char *file = info->GetArg(2);
524 if (id == 0 && isalpha(file[0]))
527 const char *alias = g_PluginMngr.LookupAlias(file);
534 /* first check if it's a known filename */
535 if (file[0] == '/' || strcmp(&(file[1]), ":\\") == 0)
537 g_Metamod.PathFormat(full_path, sizeof(full_path), "%s", file);
541 const char *ext = UTIL_GetExtension(file);
542 #if defined WIN32 || defined _WIN32
543 ext = ext ? "" : ".dll";
545 ext = ext ? "" : "_i486.so";
547 g_Metamod.PathFormat(full_path,
550 g_Metamod.GetBaseDir(),
555 List<CPluginManager::CPlugin *>::iterator iter, end;
556 CPluginManager::CPlugin *pl;
557 iter = g_PluginMngr._begin();
558 end = g_PluginMngr._end();
559 for (; iter!=end; iter++)
562 if (strcmp(pl->m_File.c_str(), full_path) == 0)
570 CONMSG("Plugin \"%s\" not found.\n", full_path);
575 if (!g_PluginMngr.Unload(id, false, error, sizeof(error)))
577 CONMSG("Unload failed: %s\n", error);
581 CONMSG("Plugin %d unloaded.\n", id);
585 CONMSG("Usage: meta unload <id>\n");
589 else if (strcmp(command, "force_unload") == 0)
593 int id = atoi(info->GetArg(2));
596 if (!g_PluginMngr.Unload(id, false, error, sizeof(error)))
598 CONMSG("Force unload failed: %s\n", error);
602 CONMSG("Plugin %d force unloaded.\n", id);
608 CONMSG("Usage: meta force_unload <id>\n");
613 else if (strcmp(command, "clear") == 0)
615 if (!g_PluginMngr.UnloadAll())
617 CONMSG("One or more plugins resisted removal (cleaned anyway).\n");
621 CONMSG("All plugins unloaded.\n");
625 else if (strcmp(command, "retry") == 0)
629 int id = atoi(info->GetArg(2));
632 if (!g_PluginMngr.Retry(id, error, sizeof(error)))
634 CONMSG("Error reloading plugin: %s\n", error);
638 CONMSG("Plugin %d successfully reloaded.\n", id);
644 CONMSG("Usage: meta retry <id>\n");
651 CONMSG("Metamod:Source Menu\n");
652 CONMSG("usage: meta <command> [arguments]\n");
653 CONMSG(" alias - List or set an alias\n");
654 CONMSG(" clear - Unload all plugins forcefully\n");
655 CONMSG(" cmds - Show plugin commands\n");
656 CONMSG(" cvars - Show plugin cvars\n");
657 CONMSG(" credits - About Metamod:Source\n");
658 CONMSG(" force_unload - Forcefully unload a plugin\n");
659 CONMSG(" game - Information about GameDLL\n");
660 CONMSG(" info - Information about a plugin\n");
661 CONMSG(" list - List plugins\n");
662 CONMSG(" load - Load a plugin\n");
663 CONMSG(" pause - Pause a running plugin\n");
664 CONMSG(" refresh - Reparse plugins file\n");
665 CONMSG(" retry - Attempt to reload a plugin\n");
666 CONMSG(" unload - Unload a loaded plugin\n");
667 CONMSG(" unpause - Unpause a paused plugin\n");
668 CONMSG(" version - Version information\n");
673 bool Command_ClientMeta(edict_t *client, IMetamodSourceCommandInfo *info)
675 const char *cmd = info->GetArg(0);
677 if (strcmp(cmd, "meta") == 0)
679 unsigned int args = info->GetArgCount();
682 const char *subcmd = info->GetArg(1);
684 if (strcmp(subcmd, "credits") == 0)
686 CLIENT_CONMSG(client, "Metamod:Source was developed by:\n");
687 CLIENT_CONMSG(client, " SourceHook: Pavol \"PM OnoTo\" Marko\n");
688 CLIENT_CONMSG(client, " GameDLL/Plugins: David \"BAILOPAN\" Anderson\n");
689 CLIENT_CONMSG(client, " GameDLL: Scott \"Damaged Soul\" Ehlert\n");
690 CLIENT_CONMSG(client, "For more information, see the official website\n");
691 CLIENT_CONMSG(client, "http://www.sourcemm.net/\n");
695 else if(strcmp(subcmd, "version") == 0)
697 CLIENT_CONMSG(client, "Metamod:Source version %s\n", SOURCEMM_VERSION);
698 CLIENT_CONMSG(client, "Compiled on: %s\n", SOURCEMM_DATE);
699 CLIENT_CONMSG(client, "Plugin interface version: %d:%d\n", METAMOD_PLAPI_VERSION, PLAPI_MIN_VERSION);
700 CLIENT_CONMSG(client, "SourceHook version: %d:%d\n", g_SHPtr->GetIfaceVersion(), g_SHPtr->GetImplVersion());
701 CLIENT_CONMSG(client, "http://www.sourcemm.net/\n");
705 else if(strcmp(subcmd, "list") == 0)
707 CPluginManager::CPlugin *pl;
715 for (i = g_PluginMngr._begin(); i != g_PluginMngr._end(); i++, len=0)
718 if (pl && pl->m_Status == Pl_Running)
721 if (!plapi || !plapi->QueryRunning(NULL, 0))
727 len += UTIL_Format(buffer, sizeof(buffer), " [%02d]", plnum);
729 plname = IS_STR_FILLED(plapi->GetName()) ? plapi->GetName() : pl->m_File.c_str();
730 len += UTIL_Format(&buffer[len], sizeof(buffer)-len, " %s", plname);
732 if (IS_STR_FILLED(plapi->GetVersion()))
734 len += UTIL_Format(&buffer[len], sizeof(buffer)-len, " (%s)", plapi->GetVersion());
736 if (IS_STR_FILLED(plapi->GetAuthor()))
738 UTIL_Format(&buffer[len], sizeof(buffer)-len, " by %s", plapi->GetAuthor());
741 CLIENT_CONMSG(client, "%s\n", buffer);
747 CLIENT_CONMSG(client, "No active plugins loaded.\n");
754 CLIENT_CONMSG(client, "Metamod:Source Menu\n");
755 CLIENT_CONMSG(client, "usage: meta <command>\n");
756 CLIENT_CONMSG(client, " credits - About Metamod:Source\n");
757 CLIENT_CONMSG(client, " list - List plugins\n");
758 CLIENT_CONMSG(client, " version - Version information\n");