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"
29 #include <versionlib.h>
31 #include "metamod_util.h"
32 #include "metamod_console.h"
33 #include "metamod_plugins.h"
35 using namespace SourceMM;
36 using namespace SourceHook;
39 * @brief Console Command Implementations
40 * @file concommands.cpp
43 #define CONMSG g_Metamod.ConPrintf
44 #define CLIENT_CONMSG g_Metamod.ClientConPrintf
46 bool Command_Meta(IMetamodSourceCommandInfo *info)
48 unsigned int args = info->GetArgCount();
50 if (mm_IsVspBridged() && !mm_IsVspLoadComplete())
52 CONMSG("You must change the map to activate Metamod:Source.\n");
58 const char *command = info->GetArg(1);
59 if (strcmp(command, "credits") == 0)
61 CONMSG("Metamod:Source was developed by:\n");
62 CONMSG(" SourceHook: Pavol \"PM OnoTo\" Marko\n");
63 CONMSG(" GameDLL/Plugins: David \"BAILOPAN\" Anderson\n");
64 CONMSG(" GameDLL: Scott \"DS\" Ehlert\n");
65 CONMSG("For more information, see the official website\n");
66 CONMSG("http://www.metamodsource.net/\n");
70 else if (strcmp(command, "version") == 0)
72 CONMSG("Metamod:Source version %s\n", METAMOD_VERSION);
73 CONMSG("Build ID: %s\n", METAMOD_BUILD_ID);
75 if (g_Metamod.IsLoadedAsGameDLL())
77 CONMSG("Loaded As: GameDLL (gameinfo.txt)\n");
81 CONMSG("Loaded As: Valve Server Plugin\n");
84 CONMSG("Compiled on: %s\n", SOURCEMM_DATE);
85 CONMSG("Plugin interface version: %d:%d\n", METAMOD_PLAPI_VERSION, PLAPI_MIN_VERSION);
86 CONMSG("SourceHook version: %d:%d\n", g_SHPtr->GetIfaceVersion(), g_SHPtr->GetImplVersion());
87 CONMSG("http://www.metamodsource.net/\n");
91 else if (strcmp(command, "game") == 0)
93 CONMSG("GameDLL Information\n");
94 CONMSG(" Description: %s\n", provider->GetGameDescription());
95 CONMSG(" Mod Path: %s\n", g_Metamod.GetBaseDir());
96 CONMSG(" DLL Path: %s\n", g_Metamod.GetGameBinaryPath());
97 CONMSG(" Interface: ServerGameDLL%03d\n", g_Metamod.GetGameDLLVersion());
100 int engine = g_Metamod.GetSourceEngineBuild();
101 if (engine == SOURCE_ENGINE_ORIGINAL)
103 CONMSG(" Engine: Original (pre-Episode 1)\n");
105 else if (engine == SOURCE_ENGINE_EPISODEONE)
107 CONMSG(" Engine: Episode 1 (2004)\n");
109 else if (engine == SOURCE_ENGINE_ORANGEBOX)
111 CONMSG(" Engine: Episode 2 (Orange Box, 2007)\n");
115 #if SOURCE_ENGINE == SE_BLOODYGOODTIME
116 CONMSG(" Engine: Bloody Good Time (2010)\n");
117 #elif SOURCE_ENGINE == SE_ALIENSWARM
118 CONMSG(" Engine: Alien Swarm (2010)\n");
119 #elif SOURCE_ENGINE == SE_LEFT4DEAD2
120 CONMSG(" Engine: Left 4 Dead 2 (2009)\n");
121 #elif SOURCE_ENGINE == SE_NUCLEARDAWN
122 CONMSG(" Engine: Nuclear Dawn (2011)\n");
123 #elif SOURCE_ENGINE == SE_LEFT4DEAD
124 CONMSG(" Engine: Left 4 Dead (2008)\n");
125 #elif SOURCE_ENGINE == SE_ORANGEBOX
126 CONMSG(" Engine: Episode 2 (Orange Box, 2007)\n");
127 #elif SOURCE_ENGINE == SE_CSS
128 CONMSG(" Engine: Counter-Strike: Source (Valve Orange Box)\n");
129 #elif SOURCE_ENGINE == SE_HL2DM
130 CONMSG(" Engine: Half-Life 2 Deathmatch (Valve Orange Box)\n");
131 #elif SOURCE_ENGINE == SE_DODS
132 CONMSG(" Engine: Day of Defeat: Source (Valve Orange Box)\n");
133 #elif SOURCE_ENGINE == SE_SDK2013
134 CONMSG(" Engine: Source SDK 2013 (2013)\n");
135 #elif SOURCE_ENGINE == SE_TF2
136 CONMSG(" Engine: Team Fortress 2 (Valve Orange Box)\n");
137 #elif SOURCE_ENGINE == SE_DARKMESSIAH
138 CONMSG(" Engine: Dark Messiah (2006)\n");
139 #elif SOURCE_ENGINE == SE_EYE
140 CONMSG(" Engine: E.Y.E. Divine Cybermancy (2011)\n");
141 #elif SOURCE_ENGINE == SE_PORTAL2
142 CONMSG(" Engine: Portal 2 (2011)\n");
143 #elif SOURCE_ENGINE == SE_BLADE
144 CONMSG(" Engine: Blade Symphony (2013)\n");
145 #elif SOURCE_ENGINE == SE_INSURGENCY
146 CONMSG(" Engine: Insurgency (2013)\n");
147 #elif SOURCE_ENGINE == SE_CSGO
148 CONMSG(" Engine: Counter-Strike: Global Offensive (2012)\n");
149 #elif SOURCE_ENGINE == SE_DOTA
150 CONMSG(" Engine: Dota 2 (2013)\n");
152 #error "SOURCE_ENGINE not defined to a known value"
155 // Display user messages
158 int messages = g_Metamod.GetUserMessageCount();
162 CONMSG(" User Messages: %-32.31s %-5s %-5s\n", "Name", "Index", "Size");
164 for (int i = 0; i < messages; i++)
166 msgname = g_Metamod.GetUserMessage(i, &msgsize);
168 CONMSG(" %-32.31s %-5d %-5d\n", msgname, i, msgsize);
171 CONMSG(" %d user message%s in total\n", messages, (messages > 1) ? "s" : "");
175 CONMSG(" User Messages: None\n");
180 else if (strcmp(command, "refresh") == 0)
182 char filepath[PATH_SIZE], vdfpath[PATH_SIZE];
183 g_Metamod.PathFormat(filepath,
186 g_Metamod.GetBaseDir(),
187 g_Metamod.GetPluginsFile());
188 g_Metamod.PathFormat(vdfpath,
191 g_Metamod.GetBaseDir(),
192 g_Metamod.GetVDFDir());
194 mm_LoadPlugins(filepath, vdfpath);
198 else if (strcmp(command, "list") == 0)
200 CPluginManager::CPlugin *pl;
206 int plnum = g_PluginMngr.GetPluginCount();
210 CONMSG("No plugins loaded.\n");
213 CONMSG("Listing %d plugin%s:\n", plnum, (plnum > 1) ? "s" : "");
216 for (i = g_PluginMngr._begin(); i != g_PluginMngr._end(); i++)
226 if (pl->m_Status != Pl_Running)
228 len += UTIL_Format(buffer, sizeof(buffer), " [%02d] <%s>", pl->m_Id, g_PluginMngr.GetStatusText(pl));
230 len += UTIL_Format(buffer, sizeof(buffer), " [%02d]", pl->m_Id);
233 if ((plapi = pl->m_API))
235 plname = IS_STR_FILLED(plapi->GetName()) ? plapi->GetName() : pl->m_File.c_str();
236 len += UTIL_Format(&buffer[len], sizeof(buffer)-len, " %s", plname);
238 if (IS_STR_FILLED(plapi->GetVersion()))
240 len += UTIL_Format(&buffer[len], sizeof(buffer)-len, " (%s)", plapi->GetVersion());
242 if (IS_STR_FILLED(plapi->GetAuthor()))
244 UTIL_Format(&buffer[len], sizeof(buffer)-len, " by %s", plapi->GetAuthor());
248 CONMSG("%s\n", buffer);
253 else if (strcmp(command, "cmds") == 0)
257 int id = atoi(info->GetArg(2));
258 CPluginManager::CPlugin *pl = g_PluginMngr.FindById(id);
262 CONMSG("Plugin %d not found.\n", id);
268 CONMSG("Plugin %d is not loaded.\n", id);
272 CONMSG("Console commands for %s:\n", pl->m_API->GetName());
273 List<ConCommandBase *>::iterator ci;
276 for (ci=pl->m_Cmds.begin(); ci!=pl->m_Cmds.end(); ci++)
279 CONMSG(" [%5d] %-s\n", count, (*ci)->GetName());
285 CONMSG("Usage: meta cmds <id>\n");
290 else if (strcmp(command, "cvars") == 0)
294 int id = atoi(info->GetArg(2));
295 CPluginManager::CPlugin *pl = g_PluginMngr.FindById(id);
299 CONMSG("Plugin %d not found.\n", id);
305 CONMSG("Plugin %d is not loaded.\n", id);
309 CONMSG("Registered cvars for %s:\n", pl->m_API->GetName());
310 List<ConCommandBase *>::iterator ci;
313 for (ci=pl->m_Cvars.begin(); ci!=pl->m_Cvars.end(); ci++)
316 CONMSG(" [%5d] %-s\n", count, (*ci)->GetName());
322 CONMSG("Usage: meta cvars <id>\n");
327 else if (strcmp(command, "info") == 0)
331 int id = atoi(info->GetArg(2));
332 CPluginManager::CPlugin *pl = g_PluginMngr.FindById(id);
335 CONMSG("Plugin %d not found.\n", id);
341 CONMSG("Plugin %d is not loaded.\n", id);
345 if (pl->m_Status == Pl_Paused)
347 CONMSG("Plugin %d is paused.\n", id);
349 else if (pl->m_Status == Pl_Running)
353 if (pl->m_API && pl->m_API->QueryRunning(run_msg, sizeof(run_msg)-1))
359 CONMSG("Plugin %d is running.\n", id);
363 CONMSG("Plugin %d is stopped: %s\n", id, run_msg);
366 CONMSG(" Name: \"%s\" by %s\n", pl->m_API->GetName(), pl->m_API->GetAuthor());
367 CONMSG(" Version: %s\n", pl->m_API->GetVersion());
368 CONMSG(" Description: %s\n", pl->m_API->GetDescription());
369 CONMSG(" License: %s\n", pl->m_API->GetLicense());
370 CONMSG(" URL: %s\n", pl->m_API->GetURL());
371 CONMSG(" Details: API %03d, Date: %s\n", pl->m_API->GetApiVersion(), pl->m_API->GetDate());
373 CONMSG("File: %s\n\n", pl->m_File.c_str());
379 CONMSG("Usage: meta info <id>\n");
384 else if (strcmp(command, "pause") == 0)
388 int id = atoi(info->GetArg(2));
392 if (!g_PluginMngr.Pause(id, error, sizeof(error)))
394 CONMSG("Pause failed: %s\n", error);
398 CONMSG("Plugin %d has been paused.\n", id);
404 CONMSG("Usage: meta pause <id>\n");
409 else if (strcmp(command, "unpause") == 0)
413 int id = atoi(info->GetArg(2));
416 if (!g_PluginMngr.Unpause(id, error, sizeof(error)))
418 CONMSG("Unpause failed: %s\n", error);
422 CONMSG("Plugin %d has been unpaused.\n", id);
428 CONMSG("Usage: meta unpause <id>\n");
433 else if (strcmp(command, "load") == 0)
437 const char *file = info->GetArg(2);
440 const char *alias = g_PluginMngr.LookupAlias(file);
446 g_Metamod.GetFullPluginPath(file, full_path, sizeof(full_path));
450 CPluginManager::CPlugin *pl;
452 // If we've recently tried to unload plugins, they might still
453 // be in the unload queue. Force them out now. This is not
454 // lowered to CPluginManager because it's not strictly safe
456 g_SourceHook.ResolvePendingUnloads(true);
458 PluginId id = g_PluginMngr.Load(full_path, Pl_Console, already, error, sizeof(error));
459 pl = g_PluginMngr.FindById(id);
460 if (!pl || id < Pl_MinId || (pl->m_Status < Pl_Paused))
462 CONMSG("Failed to load plugin %s (%s).\n", file, error);
468 CONMSG("Plugin \"%s\" loaded with id %d.\n", pl->m_API->GetName(), pl->m_Id);
470 CONMSG("Plugin \"%s\" is already loaded as %d.\n", pl->m_API->GetName(), pl->m_Id);
477 CONMSG("Usage: meta load <path>\n");
482 else if ( (strcmp(command, "alias") == 0) ||
483 (strcmp(command, "aliases") == 0) )
487 const char *alias = info->GetArg(2);
488 const char *value = info->GetArg(3);
490 g_PluginMngr.SetAlias(alias, value);
491 if (value == NULL || value[0] == '\0')
493 CONMSG("Deleted alias: %s.\n", alias);
495 CONMSG("Set alias \"%s\" to: %s\n", alias, value);
500 const char *alias = info->GetArg(2);
501 const char *value = g_PluginMngr.LookupAlias(alias);
504 CONMSG("Alias \"%s\" is set to: %s\n", alias, value);
506 CONMSG("Alias \"%s\" was not found.\n", alias);
511 List<CNameAlias *>::iterator iter, end;
514 iter = g_PluginMngr._alias_begin();
515 end = g_PluginMngr._alias_end();
519 CONMSG("%-10.9s %s\n", "Alias", "File");
521 for (; iter!=end; iter++)
524 CONMSG("%-10.9s %s\n", p->alias.c_str(), p->value.c_str());
528 CONMSG("%d aliases total.\n", total);
532 CONMSG("No aliases found.\n");
537 else if (strcmp(command, "unload") == 0)
541 const char *file = info->GetArg(2);
545 if (id == 0 && isalpha(file[0]))
548 const char *alias = g_PluginMngr.LookupAlias(file);
555 g_Metamod.GetFullPluginPath(file, full_path, sizeof(full_path));
557 List<CPluginManager::CPlugin *>::iterator iter, end;
558 CPluginManager::CPlugin *pl;
559 iter = g_PluginMngr._begin();
560 end = g_PluginMngr._end();
561 for (; iter!=end; iter++)
564 if (strcmp(pl->m_File.c_str(), full_path) == 0)
572 CONMSG("Plugin \"%s\" not found.\n", full_path);
577 if (!g_PluginMngr.Unload(id, false, error, sizeof(error)))
579 CONMSG("Unload failed: %s\n", error);
583 CONMSG("Plugin %d unloaded.\n", id);
587 CONMSG("Usage: meta unload <id>\n");
591 else if (strcmp(command, "force_unload") == 0)
595 int id = atoi(info->GetArg(2));
598 if (!g_PluginMngr.Unload(id, false, error, sizeof(error)))
600 CONMSG("Force unload failed: %s\n", error);
604 CONMSG("Plugin %d force unloaded.\n", id);
610 CONMSG("Usage: meta force_unload <id>\n");
615 else if (strcmp(command, "clear") == 0)
617 if (!g_PluginMngr.UnloadAll())
619 CONMSG("One or more plugins resisted removal (cleaned anyway).\n");
623 CONMSG("All plugins unloaded.\n");
627 else if (strcmp(command, "retry") == 0)
631 int id = atoi(info->GetArg(2));
634 if (!g_PluginMngr.Retry(id, error, sizeof(error)))
636 CONMSG("Error reloading plugin: %s\n", error);
640 CONMSG("Plugin %d successfully reloaded.\n", id);
646 CONMSG("Usage: meta retry <id>\n");
653 CONMSG("Metamod:Source Menu\n");
654 CONMSG("usage: meta <command> [arguments]\n");
655 CONMSG(" alias - List or set an alias\n");
656 CONMSG(" clear - Unload all plugins forcefully\n");
657 CONMSG(" cmds - Show plugin commands\n");
658 CONMSG(" cvars - Show plugin cvars\n");
659 CONMSG(" credits - About Metamod:Source\n");
660 CONMSG(" force_unload - Forcefully unload a plugin\n");
661 CONMSG(" game - Information about GameDLL\n");
662 CONMSG(" info - Information about a plugin\n");
663 CONMSG(" list - List plugins\n");
664 CONMSG(" load - Load a plugin\n");
665 CONMSG(" pause - Pause a running plugin\n");
666 CONMSG(" refresh - Reparse plugin files\n");
667 CONMSG(" retry - Attempt to reload a plugin\n");
668 CONMSG(" unload - Unload a loaded plugin\n");
669 CONMSG(" unpause - Unpause a paused plugin\n");
670 CONMSG(" version - Version information\n");
675 #if SOURCE_ENGINE == SE_DOTA
676 bool Command_ClientMeta(int client, IMetamodSourceCommandInfo *info)
678 bool Command_ClientMeta(edict_t *client, IMetamodSourceCommandInfo *info)
681 const char *cmd = info->GetArg(0);
683 if (strcmp(cmd, "meta") == 0)
685 unsigned int args = info->GetArgCount();
688 const char *subcmd = info->GetArg(1);
690 if (strcmp(subcmd, "credits") == 0)
692 CLIENT_CONMSG(client, "Metamod:Source was developed by:\n");
693 CLIENT_CONMSG(client, " SourceHook: Pavol \"PM OnoTo\" Marko\n");
694 CLIENT_CONMSG(client, " GameDLL/Plugins: David \"BAILOPAN\" Anderson\n");
695 CLIENT_CONMSG(client, " GameDLL: Scott \"DS\" Ehlert\n");
696 CLIENT_CONMSG(client, "For more information, see the official website\n");
697 CLIENT_CONMSG(client, "http://www.metamodsource.net/\n");
701 else if(strcmp(subcmd, "version") == 0)
703 CLIENT_CONMSG(client, "Metamod:Source version %s\n", METAMOD_VERSION);
704 CLIENT_CONMSG(client, "Compiled on: %s\n", SOURCEMM_DATE);
705 CLIENT_CONMSG(client, "Plugin interface version: %d:%d\n", METAMOD_PLAPI_VERSION, PLAPI_MIN_VERSION);
706 CLIENT_CONMSG(client, "SourceHook version: %d:%d\n", g_SHPtr->GetIfaceVersion(), g_SHPtr->GetImplVersion());
707 CLIENT_CONMSG(client, "http://www.metamodsource.net/\n");
711 else if(strcmp(subcmd, "list") == 0)
713 CPluginManager::CPlugin *pl;
721 for (i = g_PluginMngr._begin(); i != g_PluginMngr._end(); i++, len=0)
724 if (pl && pl->m_Status == Pl_Running)
727 if (!plapi || !plapi->QueryRunning(NULL, 0))
733 len += UTIL_Format(buffer, sizeof(buffer), " [%02d]", plnum);
735 plname = IS_STR_FILLED(plapi->GetName()) ? plapi->GetName() : pl->m_File.c_str();
736 len += UTIL_Format(&buffer[len], sizeof(buffer)-len, " %s", plname);
738 if (IS_STR_FILLED(plapi->GetVersion()))
740 len += UTIL_Format(&buffer[len], sizeof(buffer)-len, " (%s)", plapi->GetVersion());
742 if (IS_STR_FILLED(plapi->GetAuthor()))
744 UTIL_Format(&buffer[len], sizeof(buffer)-len, " by %s", plapi->GetAuthor());
747 CLIENT_CONMSG(client, "%s\n", buffer);
753 CLIENT_CONMSG(client, "No active plugins loaded.\n");
760 CLIENT_CONMSG(client, "Metamod:Source Menu\n");
761 CLIENT_CONMSG(client, "usage: meta <command>\n");
762 CLIENT_CONMSG(client, " credits - About Metamod:Source\n");
763 CLIENT_CONMSG(client, " list - List plugins\n");
764 CLIENT_CONMSG(client, " version - Version information\n");