2 * vim: set ts=4 sw=4 tw=99 noet :
3 * ======================================================
5 * Copyright (C) 2004-2010 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_ALIENSWARM
115 CONMSG(" Engine: Alien Swarm (2010)\n");
116 #elif SOURCE_ENGINE == SE_LEFT4DEAD2
117 CONMSG(" Engine: Left 4 Dead 2 (2009)\n");
118 #elif SOURCE_ENGINE == SE_LEFT4DEAD
119 CONMSG(" Engine: Left 4 Dead (2008)\n");
120 #elif SOURCE_ENGINE == SE_ORANGEBOX
121 CONMSG(" Engine: Episode 2 (Orange Box, 2007)\n");
122 #elif SOURCE_ENGINE == SE_ORANGEBOXVALVE
123 CONMSG(" Engine: Episode 2 (Valve Orange Box, 2009)\n");
124 #elif SOURCE_ENGINE == SE_DARKMESSIAH
125 CONMSG(" Engine: Dark Messiah (2006)\n");
127 #error "SOURCE_ENGINE not defined to a known value"
130 // Display user messages
133 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 else if (strcmp(command, "refresh") == 0)
157 char filepath[PATH_SIZE], vdfpath[PATH_SIZE];
158 g_Metamod.PathFormat(filepath,
161 g_Metamod.GetBaseDir(),
162 g_Metamod.GetPluginsFile());
163 g_Metamod.PathFormat(vdfpath,
166 g_Metamod.GetBaseDir(),
167 g_Metamod.GetVDFDir());
169 mm_LoadPlugins(filepath, vdfpath);
173 else if (strcmp(command, "list") == 0)
175 CPluginManager::CPlugin *pl;
181 int plnum = g_PluginMngr.GetPluginCount();
185 CONMSG("No plugins loaded.\n");
188 CONMSG("Listing %d plugin%s:\n", plnum, (plnum > 1) ? "s" : "");
191 for (i = g_PluginMngr._begin(); i != g_PluginMngr._end(); i++)
201 if (pl->m_Status != Pl_Running)
203 len += UTIL_Format(buffer, sizeof(buffer), " [%02d] <%s>", pl->m_Id, g_PluginMngr.GetStatusText(pl));
205 len += UTIL_Format(buffer, sizeof(buffer), " [%02d]", pl->m_Id);
208 if ((plapi = pl->m_API))
210 plname = IS_STR_FILLED(plapi->GetName()) ? plapi->GetName() : pl->m_File.c_str();
211 len += UTIL_Format(&buffer[len], sizeof(buffer)-len, " %s", plname);
213 if (IS_STR_FILLED(plapi->GetVersion()))
215 len += UTIL_Format(&buffer[len], sizeof(buffer)-len, " (%s)", plapi->GetVersion());
217 if (IS_STR_FILLED(plapi->GetAuthor()))
219 UTIL_Format(&buffer[len], sizeof(buffer)-len, " by %s", plapi->GetAuthor());
223 CONMSG("%s\n", buffer);
228 else if (strcmp(command, "cmds") == 0)
232 int id = atoi(info->GetArg(2));
233 CPluginManager::CPlugin *pl = g_PluginMngr.FindById(id);
237 CONMSG("Plugin %d not found.\n", id);
243 CONMSG("Plugin %d is not loaded.\n", id);
247 CONMSG("Console commands for %s:\n", pl->m_API->GetName());
248 List<ConCommandBase *>::iterator ci;
251 for (ci=pl->m_Cmds.begin(); ci!=pl->m_Cmds.end(); ci++)
254 CONMSG(" [%5d] %-s\n", count, (*ci)->GetName());
260 CONMSG("Usage: meta cmds <id>\n");
265 else if (strcmp(command, "cvars") == 0)
269 int id = atoi(info->GetArg(2));
270 CPluginManager::CPlugin *pl = g_PluginMngr.FindById(id);
274 CONMSG("Plugin %d not found.\n", id);
280 CONMSG("Plugin %d is not loaded.\n", id);
284 CONMSG("Registered cvars for %s:\n", pl->m_API->GetName());
285 List<ConCommandBase *>::iterator ci;
288 for (ci=pl->m_Cvars.begin(); ci!=pl->m_Cvars.end(); ci++)
291 CONMSG(" [%5d] %-s\n", count, (*ci)->GetName());
297 CONMSG("Usage: meta cvars <id>\n");
302 else if (strcmp(command, "info") == 0)
306 int id = atoi(info->GetArg(2));
307 CPluginManager::CPlugin *pl = g_PluginMngr.FindById(id);
310 CONMSG("Plugin %d not found.\n", id);
316 CONMSG("Plugin %d is not loaded.\n", id);
320 if (pl->m_Status == Pl_Paused)
322 CONMSG("Plugin %d is paused.\n", id);
324 else if (pl->m_Status == Pl_Running)
328 if (pl->m_API && pl->m_API->QueryRunning(run_msg, sizeof(run_msg)-1))
334 CONMSG("Plugin %d is running.\n", id);
338 CONMSG("Plugin %d is stopped: %s\n", id, run_msg);
341 CONMSG(" Name: \"%s\" by %s\n", pl->m_API->GetName(), pl->m_API->GetAuthor());
342 CONMSG(" Version: %s\n", pl->m_API->GetVersion());
343 CONMSG(" Description: %s\n", pl->m_API->GetDescription());
344 CONMSG(" License: %s\n", pl->m_API->GetLicense());
345 CONMSG(" URL: %s\n", pl->m_API->GetURL());
346 CONMSG(" Details: API %03d, Date: %s\n", pl->m_API->GetApiVersion(), pl->m_API->GetDate());
348 CONMSG("File: %s\n\n", pl->m_File.c_str());
354 CONMSG("Usage: meta info <id>\n");
359 else if (strcmp(command, "pause") == 0)
363 int id = atoi(info->GetArg(2));
367 if (!g_PluginMngr.Pause(id, error, sizeof(error)))
369 CONMSG("Pause failed: %s\n", error);
373 CONMSG("Plugin %d has been paused.\n", id);
379 CONMSG("Usage: meta pause <id>\n");
384 else if (strcmp(command, "unpause") == 0)
388 int id = atoi(info->GetArg(2));
391 if (!g_PluginMngr.Unpause(id, error, sizeof(error)))
393 CONMSG("Unpause failed: %s\n", error);
397 CONMSG("Plugin %d has been unpaused.\n", id);
403 CONMSG("Usage: meta unpause <id>\n");
408 else if (strcmp(command, "load") == 0)
412 const char *file = info->GetArg(2);
415 const char *alias = g_PluginMngr.LookupAlias(file);
421 g_Metamod.GetFullPluginPath(file, full_path, sizeof(full_path));
425 CPluginManager::CPlugin *pl;
427 PluginId id = g_PluginMngr.Load(full_path, Pl_Console, already, error, sizeof(error));
428 pl = g_PluginMngr.FindById(id);
429 if (!pl || id < Pl_MinId || (pl->m_Status < Pl_Paused))
431 CONMSG("Failed to load plugin %s (%s).\n", file, error);
437 CONMSG("Plugin \"%s\" loaded with id %d.\n", pl->m_API->GetName(), pl->m_Id);
439 CONMSG("Plugin \"%s\" is already loaded as %d.\n", pl->m_API->GetName(), pl->m_Id);
446 CONMSG("Usage: meta load <path>\n");
451 else if ( (strcmp(command, "alias") == 0) ||
452 (strcmp(command, "aliases") == 0) )
456 const char *alias = info->GetArg(2);
457 const char *value = info->GetArg(3);
459 g_PluginMngr.SetAlias(alias, value);
460 if (value == NULL || value[0] == '\0')
462 CONMSG("Deleted alias: %s.\n", alias);
464 CONMSG("Set alias \"%s\" to: %s\n", alias, value);
469 const char *alias = info->GetArg(2);
470 const char *value = g_PluginMngr.LookupAlias(alias);
473 CONMSG("Alias \"%s\" is set to: %s\n", alias, value);
475 CONMSG("Alias \"%s\" was not found.\n", alias);
480 List<CNameAlias *>::iterator iter, end;
483 iter = g_PluginMngr._alias_begin();
484 end = g_PluginMngr._alias_end();
488 CONMSG("%-10.9s %s\n", "Alias", "File");
490 for (; iter!=end; iter++)
493 CONMSG("%-10.9s %s\n", p->alias.c_str(), p->value.c_str());
497 CONMSG("%d aliases total.\n", total);
501 CONMSG("No aliases found.\n");
506 else if (strcmp(command, "unload") == 0)
510 const char *file = info->GetArg(2);
514 if (id == 0 && isalpha(file[0]))
517 const char *alias = g_PluginMngr.LookupAlias(file);
524 g_Metamod.GetFullPluginPath(file, full_path, sizeof(full_path));
526 List<CPluginManager::CPlugin *>::iterator iter, end;
527 CPluginManager::CPlugin *pl;
528 iter = g_PluginMngr._begin();
529 end = g_PluginMngr._end();
530 for (; iter!=end; iter++)
533 if (strcmp(pl->m_File.c_str(), full_path) == 0)
541 CONMSG("Plugin \"%s\" not found.\n", full_path);
546 if (!g_PluginMngr.Unload(id, false, error, sizeof(error)))
548 CONMSG("Unload failed: %s\n", error);
552 CONMSG("Plugin %d unloaded.\n", id);
556 CONMSG("Usage: meta unload <id>\n");
560 else if (strcmp(command, "force_unload") == 0)
564 int id = atoi(info->GetArg(2));
567 if (!g_PluginMngr.Unload(id, false, error, sizeof(error)))
569 CONMSG("Force unload failed: %s\n", error);
573 CONMSG("Plugin %d force unloaded.\n", id);
579 CONMSG("Usage: meta force_unload <id>\n");
584 else if (strcmp(command, "clear") == 0)
586 if (!g_PluginMngr.UnloadAll())
588 CONMSG("One or more plugins resisted removal (cleaned anyway).\n");
592 CONMSG("All plugins unloaded.\n");
596 else if (strcmp(command, "retry") == 0)
600 int id = atoi(info->GetArg(2));
603 if (!g_PluginMngr.Retry(id, error, sizeof(error)))
605 CONMSG("Error reloading plugin: %s\n", error);
609 CONMSG("Plugin %d successfully reloaded.\n", id);
615 CONMSG("Usage: meta retry <id>\n");
622 CONMSG("Metamod:Source Menu\n");
623 CONMSG("usage: meta <command> [arguments]\n");
624 CONMSG(" alias - List or set an alias\n");
625 CONMSG(" clear - Unload all plugins forcefully\n");
626 CONMSG(" cmds - Show plugin commands\n");
627 CONMSG(" cvars - Show plugin cvars\n");
628 CONMSG(" credits - About Metamod:Source\n");
629 CONMSG(" force_unload - Forcefully unload a plugin\n");
630 CONMSG(" game - Information about GameDLL\n");
631 CONMSG(" info - Information about a plugin\n");
632 CONMSG(" list - List plugins\n");
633 CONMSG(" load - Load a plugin\n");
634 CONMSG(" pause - Pause a running plugin\n");
635 CONMSG(" refresh - Reparse plugin files\n");
636 CONMSG(" retry - Attempt to reload a plugin\n");
637 CONMSG(" unload - Unload a loaded plugin\n");
638 CONMSG(" unpause - Unpause a paused plugin\n");
639 CONMSG(" version - Version information\n");
644 bool Command_ClientMeta(edict_t *client, IMetamodSourceCommandInfo *info)
646 const char *cmd = info->GetArg(0);
648 if (strcmp(cmd, "meta") == 0)
650 unsigned int args = info->GetArgCount();
653 const char *subcmd = info->GetArg(1);
655 if (strcmp(subcmd, "credits") == 0)
657 CLIENT_CONMSG(client, "Metamod:Source was developed by:\n");
658 CLIENT_CONMSG(client, " SourceHook: Pavol \"PM OnoTo\" Marko\n");
659 CLIENT_CONMSG(client, " GameDLL/Plugins: David \"BAILOPAN\" Anderson\n");
660 CLIENT_CONMSG(client, " GameDLL: Scott \"DS\" Ehlert\n");
661 CLIENT_CONMSG(client, "For more information, see the official website\n");
662 CLIENT_CONMSG(client, "http://www.metamodsource.net/\n");
666 else if(strcmp(subcmd, "version") == 0)
668 CLIENT_CONMSG(client, "Metamod:Source version %s\n", MMS_FULL_VERSION);
669 CLIENT_CONMSG(client, "Compiled on: %s\n", SOURCEMM_DATE);
670 CLIENT_CONMSG(client, "Plugin interface version: %d:%d\n", METAMOD_PLAPI_VERSION, PLAPI_MIN_VERSION);
671 CLIENT_CONMSG(client, "SourceHook version: %d:%d\n", g_SHPtr->GetIfaceVersion(), g_SHPtr->GetImplVersion());
672 CLIENT_CONMSG(client, "http://www.metamodsource.net/\n");
676 else if(strcmp(subcmd, "list") == 0)
678 CPluginManager::CPlugin *pl;
686 for (i = g_PluginMngr._begin(); i != g_PluginMngr._end(); i++, len=0)
689 if (pl && pl->m_Status == Pl_Running)
692 if (!plapi || !plapi->QueryRunning(NULL, 0))
698 len += UTIL_Format(buffer, sizeof(buffer), " [%02d]", plnum);
700 plname = IS_STR_FILLED(plapi->GetName()) ? plapi->GetName() : pl->m_File.c_str();
701 len += UTIL_Format(&buffer[len], sizeof(buffer)-len, " %s", plname);
703 if (IS_STR_FILLED(plapi->GetVersion()))
705 len += UTIL_Format(&buffer[len], sizeof(buffer)-len, " (%s)", plapi->GetVersion());
707 if (IS_STR_FILLED(plapi->GetAuthor()))
709 UTIL_Format(&buffer[len], sizeof(buffer)-len, " by %s", plapi->GetAuthor());
712 CLIENT_CONMSG(client, "%s\n", buffer);
718 CLIENT_CONMSG(client, "No active plugins loaded.\n");
725 CLIENT_CONMSG(client, "Metamod:Source Menu\n");
726 CLIENT_CONMSG(client, "usage: meta <command>\n");
727 CLIENT_CONMSG(client, " credits - About Metamod:Source\n");
728 CLIENT_CONMSG(client, " list - List plugins\n");
729 CLIENT_CONMSG(client, " version - Version information\n");