2 * vim: set ts=4 sw=4 tw=99 noet :
3 * ======================================================
5 * Copyright (C) 2004-2009 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.
27 #include "metamod_oslink.h"
30 #include "metamod_util.h"
31 #include "metamod_console.h"
32 #include "metamod_plugins.h"
34 using namespace SourceMM;
35 using namespace SourceHook;
38 * @brief Console Command Implementations
39 * @file concommands.cpp
42 #define CONMSG g_Metamod.ConPrintf
43 #define CLIENT_CONMSG g_Metamod.ClientConPrintf
45 bool Command_Meta(IMetamodSourceCommandInfo *info)
47 unsigned int args = info->GetArgCount();
49 if (mm_IsVspBridged() && !mm_IsVspLoadComplete())
51 CONMSG("You must change the map to activate Metamod:Source.\n");
57 const char *command = info->GetArg(1);
58 if (strcmp(command, "credits") == 0)
60 CONMSG("Metamod:Source was developed by:\n");
61 CONMSG(" SourceHook: Pavol \"PM OnoTo\" Marko\n");
62 CONMSG(" GameDLL/Plugins: David \"BAILOPAN\" Anderson\n");
63 CONMSG(" GameDLL: Scott \"DS\" Ehlert\n");
64 CONMSG("For more information, see the official website\n");
65 CONMSG("http://www.metamodsource.net/\n");
69 else if (strcmp(command, "version") == 0)
71 CONMSG("Metamod:Source version %s\n", MMS_FULL_VERSION);
72 CONMSG("Build ID: %s\n", MMS_BUILD_UNIQUEID);
74 if (g_Metamod.IsLoadedAsGameDLL())
76 CONMSG("Loaded As: GameDLL (gameinfo.txt)\n");
80 CONMSG("Loaded As: Valve Server Plugin\n");
83 CONMSG("Compiled on: %s\n", SOURCEMM_DATE);
84 CONMSG("Plugin interface version: %d:%d\n", METAMOD_PLAPI_VERSION, PLAPI_MIN_VERSION);
85 CONMSG("SourceHook version: %d:%d\n", g_SHPtr->GetIfaceVersion(), g_SHPtr->GetImplVersion());
86 CONMSG("http://www.metamodsource.net/\n");
90 else if (strcmp(command, "game") == 0)
92 CONMSG("GameDLL Information\n");
93 CONMSG(" Description: %s\n", provider->GetGameDescription());
94 CONMSG(" Mod Path: %s\n", g_Metamod.GetBaseDir());
95 CONMSG(" DLL Path: %s\n", g_Metamod.GetGameBinaryPath());
96 CONMSG(" Interface: ServerGameDLL%03d\n", g_Metamod.GetGameDLLVersion());
99 int engine = g_Metamod.GetSourceEngineBuild();
100 if (engine == SOURCE_ENGINE_ORIGINAL)
102 CONMSG(" Engine: Original (pre-Episode 1)\n");
104 else if (engine == SOURCE_ENGINE_EPISODEONE)
106 CONMSG(" Engine: Episode 1 (2004)\n");
108 else if (engine == SOURCE_ENGINE_ORANGEBOX)
110 CONMSG(" Engine: Episode 2 (Orange Box, 2007)\n");
114 #if SOURCE_ENGINE == SE_LEFT4DEAD2
115 CONMSG(" Engine: Left 4 Dead 2 (2009)\n");
116 #elif 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");
120 #elif SOURCE_ENGINE == SE_ORANGEBOXVALVE
121 CONMSG(" Engine: Episode 2 (Valve Orange Box, 2009)\n");
122 #elif SOURCE_ENGINE == SE_DARKMESSIAH
123 CONMSG(" Engine: Dark Messiah (2006)\n");
125 #error "SOURCE_ENGINE not defined to a known value"
128 // Display user messages
129 int messages = g_Metamod.GetUserMessageCount();
137 CONMSG(" User Messages: %-32.31s %-5s %-5s\n", "Name", "Index", "Size");
139 for (int i = 0; i < messages; i++)
141 msgname = g_Metamod.GetUserMessage(i, &msgsize);
143 CONMSG(" %-32.31s %-5d %-5d\n", msgname, i, msgsize);
146 CONMSG(" %d user message%s in total\n", messages, (messages > 1) ? "s" : "");
150 CONMSG(" User Messages: None\n");
155 CONMSG(" User Messages: Failed to get list of user messages\n");
160 else if (strcmp(command, "refresh") == 0)
162 char filepath[PATH_SIZE], vdfpath[PATH_SIZE];
163 g_Metamod.PathFormat(filepath,
166 g_Metamod.GetBaseDir(),
167 g_Metamod.GetPluginsFile());
168 g_Metamod.PathFormat(vdfpath,
171 g_Metamod.GetBaseDir(),
172 g_Metamod.GetVDFDir());
174 mm_LoadPlugins(filepath, vdfpath);
178 else if (strcmp(command, "list") == 0)
180 CPluginManager::CPlugin *pl;
186 int plnum = g_PluginMngr.GetPluginCount();
190 CONMSG("No plugins loaded.\n");
193 CONMSG("Listing %d plugin%s:\n", plnum, (plnum > 1) ? "s" : "");
196 for (i = g_PluginMngr._begin(); i != g_PluginMngr._end(); i++)
206 if (pl->m_Status != Pl_Running)
208 len += UTIL_Format(buffer, sizeof(buffer), " [%02d] <%s>", pl->m_Id, g_PluginMngr.GetStatusText(pl));
210 len += UTIL_Format(buffer, sizeof(buffer), " [%02d]", pl->m_Id);
213 if ((plapi = pl->m_API))
215 plname = IS_STR_FILLED(plapi->GetName()) ? plapi->GetName() : pl->m_File.c_str();
216 len += UTIL_Format(&buffer[len], sizeof(buffer)-len, " %s", plname);
218 if (IS_STR_FILLED(plapi->GetVersion()))
220 len += UTIL_Format(&buffer[len], sizeof(buffer)-len, " (%s)", plapi->GetVersion());
222 if (IS_STR_FILLED(plapi->GetAuthor()))
224 UTIL_Format(&buffer[len], sizeof(buffer)-len, " by %s", plapi->GetAuthor());
228 CONMSG("%s\n", buffer);
233 else if (strcmp(command, "cmds") == 0)
237 int id = atoi(info->GetArg(2));
238 CPluginManager::CPlugin *pl = g_PluginMngr.FindById(id);
242 CONMSG("Plugin %d not found.\n", id);
248 CONMSG("Plugin %d is not loaded.\n", id);
252 CONMSG("Console commands for %s:\n", pl->m_API->GetName());
253 List<ConCommandBase *>::iterator ci;
256 for (ci=pl->m_Cmds.begin(); ci!=pl->m_Cmds.end(); ci++)
259 CONMSG(" [%5d] %-s\n", count, (*ci)->GetName());
265 CONMSG("Usage: meta cmds <id>\n");
270 else if (strcmp(command, "cvars") == 0)
274 int id = atoi(info->GetArg(2));
275 CPluginManager::CPlugin *pl = g_PluginMngr.FindById(id);
279 CONMSG("Plugin %d not found.\n", id);
285 CONMSG("Plugin %d is not loaded.\n", id);
289 CONMSG("Registered cvars for %s:\n", pl->m_API->GetName());
290 List<ConCommandBase *>::iterator ci;
293 for (ci=pl->m_Cvars.begin(); ci!=pl->m_Cvars.end(); ci++)
296 CONMSG(" [%5d] %-s\n", count, (*ci)->GetName());
302 CONMSG("Usage: meta cvars <id>\n");
307 else if (strcmp(command, "info") == 0)
311 int id = atoi(info->GetArg(2));
312 CPluginManager::CPlugin *pl = g_PluginMngr.FindById(id);
315 CONMSG("Plugin %d not found.\n", id);
321 CONMSG("Plugin %d is not loaded.\n", id);
325 if (pl->m_Status == Pl_Paused)
327 CONMSG("Plugin %d is paused.\n", id);
329 else if (pl->m_Status == Pl_Running)
333 if (pl->m_API && pl->m_API->QueryRunning(run_msg, sizeof(run_msg)-1))
339 CONMSG("Plugin %d is running.\n", id);
343 CONMSG("Plugin %d is stopped: %s\n", id, run_msg);
346 CONMSG(" Name: \"%s\" by %s\n", pl->m_API->GetName(), pl->m_API->GetAuthor());
347 CONMSG(" Version: %s\n", pl->m_API->GetVersion());
348 CONMSG(" Description: %s\n", pl->m_API->GetDescription());
349 CONMSG(" License: %s\n", pl->m_API->GetLicense());
350 CONMSG(" URL: %s\n", pl->m_API->GetURL());
351 CONMSG(" Details: API %03d, Date: %s\n", pl->m_API->GetApiVersion(), pl->m_API->GetDate());
353 CONMSG("File: %s\n\n", pl->m_File.c_str());
359 CONMSG("Usage: meta info <id>\n");
364 else if (strcmp(command, "pause") == 0)
368 int id = atoi(info->GetArg(2));
372 if (!g_PluginMngr.Pause(id, error, sizeof(error)))
374 CONMSG("Pause failed: %s\n", error);
378 CONMSG("Plugin %d has been paused.\n", id);
384 CONMSG("Usage: meta pause <id>\n");
389 else if (strcmp(command, "unpause") == 0)
393 int id = atoi(info->GetArg(2));
396 if (!g_PluginMngr.Unpause(id, error, sizeof(error)))
398 CONMSG("Unpause failed: %s\n", error);
402 CONMSG("Plugin %d has been unpaused.\n", id);
408 CONMSG("Usage: meta unpause <id>\n");
413 else if (strcmp(command, "load") == 0)
417 const char *file = info->GetArg(2);
420 const char *alias = g_PluginMngr.LookupAlias(file);
426 if (file[0] == '/' || strcmp(&(file[1]), ":\\") == 0)
428 g_Metamod.PathFormat(full_path, sizeof(full_path), "%s", file);
432 const char *ext = UTIL_GetExtension(file);
433 #if defined WIN32 || defined _WIN32
434 ext = ext ? "" : ".dll";
436 ext = ext ? "" : "_i486.so";
438 g_Metamod.PathFormat(full_path,
441 g_Metamod.GetBaseDir(),
448 CPluginManager::CPlugin *pl;
450 PluginId id = g_PluginMngr.Load(full_path, Pl_Console, already, error, sizeof(error));
451 pl = g_PluginMngr.FindById(id);
452 if (!pl || id < Pl_MinId || (pl->m_Status < Pl_Paused))
454 CONMSG("Failed to load plugin %s (%s).\n", file, error);
460 CONMSG("Plugin \"%s\" loaded with id %d.\n", pl->m_API->GetName(), pl->m_Id);
462 CONMSG("Plugin \"%s\" is already loaded as %d.\n", pl->m_API->GetName(), pl->m_Id);
469 CONMSG("Usage: meta load <path>\n");
474 else if ( (strcmp(command, "alias") == 0) ||
475 (strcmp(command, "aliases") == 0) )
479 const char *alias = info->GetArg(2);
480 const char *value = info->GetArg(3);
482 g_PluginMngr.SetAlias(alias, value);
483 if (value == NULL || value[0] == '\0')
485 CONMSG("Deleted alias: %s.\n", alias);
487 CONMSG("Set alias \"%s\" to: %s\n", alias, value);
492 const char *alias = info->GetArg(2);
493 const char *value = g_PluginMngr.LookupAlias(alias);
496 CONMSG("Alias \"%s\" is set to: %s\n", alias, value);
498 CONMSG("Alias \"%s\" was not found.\n", alias);
503 List<CNameAlias *>::iterator iter, end;
506 iter = g_PluginMngr._alias_begin();
507 end = g_PluginMngr._alias_end();
511 CONMSG("%-10.9s %s\n", "Alias", "File");
513 for (; iter!=end; iter++)
516 CONMSG("%-10.9s %s\n", p->alias.c_str(), p->value.c_str());
520 CONMSG("%d aliases total.\n", total);
524 CONMSG("No aliases found.\n");
529 else if (strcmp(command, "unload") == 0)
533 const char *file = info->GetArg(2);
537 if (id == 0 && isalpha(file[0]))
540 const char *alias = g_PluginMngr.LookupAlias(file);
547 /* first check if it's a known filename */
548 if (file[0] == '/' || strcmp(&(file[1]), ":\\") == 0)
550 g_Metamod.PathFormat(full_path, sizeof(full_path), "%s", file);
554 const char *ext = UTIL_GetExtension(file);
555 #if defined WIN32 || defined _WIN32
556 ext = ext ? "" : ".dll";
558 ext = ext ? "" : "_i486.so";
560 g_Metamod.PathFormat(full_path,
563 g_Metamod.GetBaseDir(),
568 List<CPluginManager::CPlugin *>::iterator iter, end;
569 CPluginManager::CPlugin *pl;
570 iter = g_PluginMngr._begin();
571 end = g_PluginMngr._end();
572 for (; iter!=end; iter++)
575 if (strcmp(pl->m_File.c_str(), full_path) == 0)
583 CONMSG("Plugin \"%s\" not found.\n", full_path);
588 if (!g_PluginMngr.Unload(id, false, error, sizeof(error)))
590 CONMSG("Unload failed: %s\n", error);
594 CONMSG("Plugin %d unloaded.\n", id);
598 CONMSG("Usage: meta unload <id>\n");
602 else if (strcmp(command, "force_unload") == 0)
606 int id = atoi(info->GetArg(2));
609 if (!g_PluginMngr.Unload(id, false, error, sizeof(error)))
611 CONMSG("Force unload failed: %s\n", error);
615 CONMSG("Plugin %d force unloaded.\n", id);
621 CONMSG("Usage: meta force_unload <id>\n");
626 else if (strcmp(command, "clear") == 0)
628 if (!g_PluginMngr.UnloadAll())
630 CONMSG("One or more plugins resisted removal (cleaned anyway).\n");
634 CONMSG("All plugins unloaded.\n");
638 else if (strcmp(command, "retry") == 0)
642 int id = atoi(info->GetArg(2));
645 if (!g_PluginMngr.Retry(id, error, sizeof(error)))
647 CONMSG("Error reloading plugin: %s\n", error);
651 CONMSG("Plugin %d successfully reloaded.\n", id);
657 CONMSG("Usage: meta retry <id>\n");
664 CONMSG("Metamod:Source Menu\n");
665 CONMSG("usage: meta <command> [arguments]\n");
666 CONMSG(" alias - List or set an alias\n");
667 CONMSG(" clear - Unload all plugins forcefully\n");
668 CONMSG(" cmds - Show plugin commands\n");
669 CONMSG(" cvars - Show plugin cvars\n");
670 CONMSG(" credits - About Metamod:Source\n");
671 CONMSG(" force_unload - Forcefully unload a plugin\n");
672 CONMSG(" game - Information about GameDLL\n");
673 CONMSG(" info - Information about a plugin\n");
674 CONMSG(" list - List plugins\n");
675 CONMSG(" load - Load a plugin\n");
676 CONMSG(" pause - Pause a running plugin\n");
677 CONMSG(" refresh - Reparse plugin files\n");
678 CONMSG(" retry - Attempt to reload a plugin\n");
679 CONMSG(" unload - Unload a loaded plugin\n");
680 CONMSG(" unpause - Unpause a paused plugin\n");
681 CONMSG(" version - Version information\n");
686 bool Command_ClientMeta(edict_t *client, IMetamodSourceCommandInfo *info)
688 const char *cmd = info->GetArg(0);
690 if (strcmp(cmd, "meta") == 0)
692 unsigned int args = info->GetArgCount();
695 const char *subcmd = info->GetArg(1);
697 if (strcmp(subcmd, "credits") == 0)
699 CLIENT_CONMSG(client, "Metamod:Source was developed by:\n");
700 CLIENT_CONMSG(client, " SourceHook: Pavol \"PM OnoTo\" Marko\n");
701 CLIENT_CONMSG(client, " GameDLL/Plugins: David \"BAILOPAN\" Anderson\n");
702 CLIENT_CONMSG(client, " GameDLL: Scott \"DS\" Ehlert\n");
703 CLIENT_CONMSG(client, "For more information, see the official website\n");
704 CLIENT_CONMSG(client, "http://www.metamodsource.net/\n");
708 else if(strcmp(subcmd, "version") == 0)
710 CLIENT_CONMSG(client, "Metamod:Source version %s\n", MMS_FULL_VERSION);
711 CLIENT_CONMSG(client, "Compiled on: %s\n", SOURCEMM_DATE);
712 CLIENT_CONMSG(client, "Plugin interface version: %d:%d\n", METAMOD_PLAPI_VERSION, PLAPI_MIN_VERSION);
713 CLIENT_CONMSG(client, "SourceHook version: %d:%d\n", g_SHPtr->GetIfaceVersion(), g_SHPtr->GetImplVersion());
714 CLIENT_CONMSG(client, "http://www.metamodsource.net/\n");
718 else if(strcmp(subcmd, "list") == 0)
720 CPluginManager::CPlugin *pl;
728 for (i = g_PluginMngr._begin(); i != g_PluginMngr._end(); i++, len=0)
731 if (pl && pl->m_Status == Pl_Running)
734 if (!plapi || !plapi->QueryRunning(NULL, 0))
740 len += UTIL_Format(buffer, sizeof(buffer), " [%02d]", plnum);
742 plname = IS_STR_FILLED(plapi->GetName()) ? plapi->GetName() : pl->m_File.c_str();
743 len += UTIL_Format(&buffer[len], sizeof(buffer)-len, " %s", plname);
745 if (IS_STR_FILLED(plapi->GetVersion()))
747 len += UTIL_Format(&buffer[len], sizeof(buffer)-len, " (%s)", plapi->GetVersion());
749 if (IS_STR_FILLED(plapi->GetAuthor()))
751 UTIL_Format(&buffer[len], sizeof(buffer)-len, " by %s", plapi->GetAuthor());
754 CLIENT_CONMSG(client, "%s\n", buffer);
760 CLIENT_CONMSG(client, "No active plugins loaded.\n");
767 CLIENT_CONMSG(client, "Metamod:Source Menu\n");
768 CLIENT_CONMSG(client, "usage: meta <command>\n");
769 CLIENT_CONMSG(client, " credits - About Metamod:Source\n");
770 CLIENT_CONMSG(client, " list - List plugins\n");
771 CLIENT_CONMSG(client, " version - Version information\n");