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_BLOODYGOODTIME
115 CONMSG(" Engine: Bloody Good Time (2010)\n");
116 #elif SOURCE_ENGINE == SE_ALIENSWARM
117 CONMSG(" Engine: Alien Swarm (2010)\n");
118 #elif SOURCE_ENGINE == SE_LEFT4DEAD2
119 CONMSG(" Engine: Left 4 Dead 2 (2009)\n");
120 #elif SOURCE_ENGINE == SE_NUCLEARDAWN
121 CONMSG(" Engine: Nuclear Dawn (2011)\n");
122 #elif SOURCE_ENGINE == SE_LEFT4DEAD
123 CONMSG(" Engine: Left 4 Dead (2008)\n");
124 #elif SOURCE_ENGINE == SE_ORANGEBOX
125 CONMSG(" Engine: Episode 2 (Orange Box, 2007)\n");
126 #elif SOURCE_ENGINE == SE_CSS
127 CONMSG(" Engine: Counter-Strike: Source (Valve Orange Box)\n");
128 #elif SOURCE_ENGINE == SE_HL2DM
129 CONMSG(" Engine: Half-Life 2 Deathmatch (Valve Orange Box)\n");
130 #elif SOURCE_ENGINE == SE_DODS
131 CONMSG(" Engine: Day of Defeat: Source (Valve Orange Box)\n");
132 #elif SOURCE_ENGINE == SE_SDK2013
133 CONMSG(" Engine: Source SDK 2013 (2013)\n");
134 #elif SOURCE_ENGINE == SE_TF2
135 CONMSG(" Engine: Team Fortress 2 (Valve Orange Box)\n");
136 #elif SOURCE_ENGINE == SE_DARKMESSIAH
137 CONMSG(" Engine: Dark Messiah (2006)\n");
138 #elif SOURCE_ENGINE == SE_EYE
139 CONMSG(" Engine: E.Y.E. Divine Cybermancy (2011)\n");
140 #elif SOURCE_ENGINE == SE_PORTAL2
141 CONMSG(" Engine: Portal 2 (2011)\n");
142 #elif SOURCE_ENGINE == SE_BLADE
143 CONMSG(" Engine: Blade Symphony (2013)\n");
144 #elif SOURCE_ENGINE == SE_INSURGENCY
145 CONMSG(" Engine: Insurgency (2013)\n");
146 #elif SOURCE_ENGINE == SE_CSGO
147 CONMSG(" Engine: Counter-Strike: Global Offensive (2012)\n");
148 #elif SOURCE_ENGINE == SE_DOTA
149 CONMSG(" Engine: Dota 2 (2013)\n");
151 #error "SOURCE_ENGINE not defined to a known value"
154 // Display user messages
157 int messages = g_Metamod.GetUserMessageCount();
161 CONMSG(" User Messages: %-32.31s %-5s %-5s\n", "Name", "Index", "Size");
163 for (int i = 0; i < messages; i++)
165 msgname = g_Metamod.GetUserMessage(i, &msgsize);
167 CONMSG(" %-32.31s %-5d %-5d\n", msgname, i, msgsize);
170 CONMSG(" %d user message%s in total\n", messages, (messages > 1) ? "s" : "");
174 CONMSG(" User Messages: None\n");
179 else if (strcmp(command, "refresh") == 0)
181 char filepath[PATH_SIZE], vdfpath[PATH_SIZE];
182 g_Metamod.PathFormat(filepath,
185 g_Metamod.GetBaseDir(),
186 g_Metamod.GetPluginsFile());
187 g_Metamod.PathFormat(vdfpath,
190 g_Metamod.GetBaseDir(),
191 g_Metamod.GetVDFDir());
193 mm_LoadPlugins(filepath, vdfpath);
197 else if (strcmp(command, "list") == 0)
199 CPluginManager::CPlugin *pl;
205 int plnum = g_PluginMngr.GetPluginCount();
209 CONMSG("No plugins loaded.\n");
212 CONMSG("Listing %d plugin%s:\n", plnum, (plnum > 1) ? "s" : "");
215 for (i = g_PluginMngr._begin(); i != g_PluginMngr._end(); i++)
225 if (pl->m_Status != Pl_Running)
227 len += UTIL_Format(buffer, sizeof(buffer), " [%02d] <%s>", pl->m_Id, g_PluginMngr.GetStatusText(pl));
229 len += UTIL_Format(buffer, sizeof(buffer), " [%02d]", pl->m_Id);
232 if ((plapi = pl->m_API))
234 plname = IS_STR_FILLED(plapi->GetName()) ? plapi->GetName() : pl->m_File.c_str();
235 len += UTIL_Format(&buffer[len], sizeof(buffer)-len, " %s", plname);
237 if (IS_STR_FILLED(plapi->GetVersion()))
239 len += UTIL_Format(&buffer[len], sizeof(buffer)-len, " (%s)", plapi->GetVersion());
241 if (IS_STR_FILLED(plapi->GetAuthor()))
243 UTIL_Format(&buffer[len], sizeof(buffer)-len, " by %s", plapi->GetAuthor());
247 CONMSG("%s\n", buffer);
252 else if (strcmp(command, "cmds") == 0)
256 int id = atoi(info->GetArg(2));
257 CPluginManager::CPlugin *pl = g_PluginMngr.FindById(id);
261 CONMSG("Plugin %d not found.\n", id);
267 CONMSG("Plugin %d is not loaded.\n", id);
271 CONMSG("Console commands for %s:\n", pl->m_API->GetName());
272 List<ConCommandBase *>::iterator ci;
275 for (ci=pl->m_Cmds.begin(); ci!=pl->m_Cmds.end(); ci++)
278 CONMSG(" [%5d] %-s\n", count, (*ci)->GetName());
284 CONMSG("Usage: meta cmds <id>\n");
289 else if (strcmp(command, "cvars") == 0)
293 int id = atoi(info->GetArg(2));
294 CPluginManager::CPlugin *pl = g_PluginMngr.FindById(id);
298 CONMSG("Plugin %d not found.\n", id);
304 CONMSG("Plugin %d is not loaded.\n", id);
308 CONMSG("Registered cvars for %s:\n", pl->m_API->GetName());
309 List<ConCommandBase *>::iterator ci;
312 for (ci=pl->m_Cvars.begin(); ci!=pl->m_Cvars.end(); ci++)
315 CONMSG(" [%5d] %-s\n", count, (*ci)->GetName());
321 CONMSG("Usage: meta cvars <id>\n");
326 else if (strcmp(command, "info") == 0)
330 int id = atoi(info->GetArg(2));
331 CPluginManager::CPlugin *pl = g_PluginMngr.FindById(id);
334 CONMSG("Plugin %d not found.\n", id);
340 CONMSG("Plugin %d is not loaded.\n", id);
344 if (pl->m_Status == Pl_Paused)
346 CONMSG("Plugin %d is paused.\n", id);
348 else if (pl->m_Status == Pl_Running)
352 if (pl->m_API && pl->m_API->QueryRunning(run_msg, sizeof(run_msg)-1))
358 CONMSG("Plugin %d is running.\n", id);
362 CONMSG("Plugin %d is stopped: %s\n", id, run_msg);
365 CONMSG(" Name: \"%s\" by %s\n", pl->m_API->GetName(), pl->m_API->GetAuthor());
366 CONMSG(" Version: %s\n", pl->m_API->GetVersion());
367 CONMSG(" Description: %s\n", pl->m_API->GetDescription());
368 CONMSG(" License: %s\n", pl->m_API->GetLicense());
369 CONMSG(" URL: %s\n", pl->m_API->GetURL());
370 CONMSG(" Details: API %03d, Date: %s\n", pl->m_API->GetApiVersion(), pl->m_API->GetDate());
372 CONMSG("File: %s\n\n", pl->m_File.c_str());
378 CONMSG("Usage: meta info <id>\n");
383 else if (strcmp(command, "pause") == 0)
387 int id = atoi(info->GetArg(2));
391 if (!g_PluginMngr.Pause(id, error, sizeof(error)))
393 CONMSG("Pause failed: %s\n", error);
397 CONMSG("Plugin %d has been paused.\n", id);
403 CONMSG("Usage: meta pause <id>\n");
408 else if (strcmp(command, "unpause") == 0)
412 int id = atoi(info->GetArg(2));
415 if (!g_PluginMngr.Unpause(id, error, sizeof(error)))
417 CONMSG("Unpause failed: %s\n", error);
421 CONMSG("Plugin %d has been unpaused.\n", id);
427 CONMSG("Usage: meta unpause <id>\n");
432 else if (strcmp(command, "load") == 0)
436 const char *file = info->GetArg(2);
439 const char *alias = g_PluginMngr.LookupAlias(file);
445 g_Metamod.GetFullPluginPath(file, full_path, sizeof(full_path));
449 CPluginManager::CPlugin *pl;
451 // If we've recently tried to unload plugins, they might still
452 // be in the unload queue. Force them out now. This is not
453 // lowered to CPluginManager because it's not strictly safe
455 g_SourceHook.ResolvePendingUnloads(true);
457 PluginId id = g_PluginMngr.Load(full_path, Pl_Console, already, error, sizeof(error));
458 pl = g_PluginMngr.FindById(id);
459 if (!pl || id < Pl_MinId || (pl->m_Status < Pl_Paused))
461 CONMSG("Failed to load plugin %s (%s).\n", file, error);
467 CONMSG("Plugin \"%s\" loaded with id %d.\n", pl->m_API->GetName(), pl->m_Id);
469 CONMSG("Plugin \"%s\" is already loaded as %d.\n", pl->m_API->GetName(), pl->m_Id);
476 CONMSG("Usage: meta load <path>\n");
481 else if ( (strcmp(command, "alias") == 0) ||
482 (strcmp(command, "aliases") == 0) )
486 const char *alias = info->GetArg(2);
487 const char *value = info->GetArg(3);
489 g_PluginMngr.SetAlias(alias, value);
490 if (value == NULL || value[0] == '\0')
492 CONMSG("Deleted alias: %s.\n", alias);
494 CONMSG("Set alias \"%s\" to: %s\n", alias, value);
499 const char *alias = info->GetArg(2);
500 const char *value = g_PluginMngr.LookupAlias(alias);
503 CONMSG("Alias \"%s\" is set to: %s\n", alias, value);
505 CONMSG("Alias \"%s\" was not found.\n", alias);
510 List<CNameAlias *>::iterator iter, end;
513 iter = g_PluginMngr._alias_begin();
514 end = g_PluginMngr._alias_end();
518 CONMSG("%-10.9s %s\n", "Alias", "File");
520 for (; iter!=end; iter++)
523 CONMSG("%-10.9s %s\n", p->alias.c_str(), p->value.c_str());
527 CONMSG("%d aliases total.\n", total);
531 CONMSG("No aliases found.\n");
536 else if (strcmp(command, "unload") == 0)
540 const char *file = info->GetArg(2);
544 if (id == 0 && isalpha(file[0]))
547 const char *alias = g_PluginMngr.LookupAlias(file);
554 g_Metamod.GetFullPluginPath(file, full_path, sizeof(full_path));
556 List<CPluginManager::CPlugin *>::iterator iter, end;
557 CPluginManager::CPlugin *pl;
558 iter = g_PluginMngr._begin();
559 end = g_PluginMngr._end();
560 for (; iter!=end; iter++)
563 if (strcmp(pl->m_File.c_str(), full_path) == 0)
571 CONMSG("Plugin \"%s\" not found.\n", full_path);
576 if (!g_PluginMngr.Unload(id, false, error, sizeof(error)))
578 CONMSG("Unload failed: %s\n", error);
582 CONMSG("Plugin %d unloaded.\n", id);
586 CONMSG("Usage: meta unload <id>\n");
590 else if (strcmp(command, "force_unload") == 0)
594 int id = atoi(info->GetArg(2));
597 if (!g_PluginMngr.Unload(id, false, error, sizeof(error)))
599 CONMSG("Force unload failed: %s\n", error);
603 CONMSG("Plugin %d force unloaded.\n", id);
609 CONMSG("Usage: meta force_unload <id>\n");
614 else if (strcmp(command, "clear") == 0)
616 if (!g_PluginMngr.UnloadAll())
618 CONMSG("One or more plugins resisted removal (cleaned anyway).\n");
622 CONMSG("All plugins unloaded.\n");
626 else if (strcmp(command, "retry") == 0)
630 int id = atoi(info->GetArg(2));
633 if (!g_PluginMngr.Retry(id, error, sizeof(error)))
635 CONMSG("Error reloading plugin: %s\n", error);
639 CONMSG("Plugin %d successfully reloaded.\n", id);
645 CONMSG("Usage: meta retry <id>\n");
652 CONMSG("Metamod:Source Menu\n");
653 CONMSG("usage: meta <command> [arguments]\n");
654 CONMSG(" alias - List or set an alias\n");
655 CONMSG(" clear - Unload all plugins forcefully\n");
656 CONMSG(" cmds - Show plugin commands\n");
657 CONMSG(" cvars - Show plugin cvars\n");
658 CONMSG(" credits - About Metamod:Source\n");
659 CONMSG(" force_unload - Forcefully unload a plugin\n");
660 CONMSG(" game - Information about GameDLL\n");
661 CONMSG(" info - Information about a plugin\n");
662 CONMSG(" list - List plugins\n");
663 CONMSG(" load - Load a plugin\n");
664 CONMSG(" pause - Pause a running plugin\n");
665 CONMSG(" refresh - Reparse plugin files\n");
666 CONMSG(" retry - Attempt to reload a plugin\n");
667 CONMSG(" unload - Unload a loaded plugin\n");
668 CONMSG(" unpause - Unpause a paused plugin\n");
669 CONMSG(" version - Version information\n");
674 #if SOURCE_ENGINE == SE_DOTA
675 bool Command_ClientMeta(int client, IMetamodSourceCommandInfo *info)
677 bool Command_ClientMeta(edict_t *client, IMetamodSourceCommandInfo *info)
680 const char *cmd = info->GetArg(0);
682 if (strcmp(cmd, "meta") == 0)
684 unsigned int args = info->GetArgCount();
687 const char *subcmd = info->GetArg(1);
689 if (strcmp(subcmd, "credits") == 0)
691 CLIENT_CONMSG(client, "Metamod:Source was developed by:\n");
692 CLIENT_CONMSG(client, " SourceHook: Pavol \"PM OnoTo\" Marko\n");
693 CLIENT_CONMSG(client, " GameDLL/Plugins: David \"BAILOPAN\" Anderson\n");
694 CLIENT_CONMSG(client, " GameDLL: Scott \"DS\" Ehlert\n");
695 CLIENT_CONMSG(client, "For more information, see the official website\n");
696 CLIENT_CONMSG(client, "http://www.metamodsource.net/\n");
700 else if(strcmp(subcmd, "version") == 0)
702 CLIENT_CONMSG(client, "Metamod:Source version %s\n", MMS_FULL_VERSION);
703 CLIENT_CONMSG(client, "Compiled on: %s\n", SOURCEMM_DATE);
704 CLIENT_CONMSG(client, "Plugin interface version: %d:%d\n", METAMOD_PLAPI_VERSION, PLAPI_MIN_VERSION);
705 CLIENT_CONMSG(client, "SourceHook version: %d:%d\n", g_SHPtr->GetIfaceVersion(), g_SHPtr->GetImplVersion());
706 CLIENT_CONMSG(client, "http://www.metamodsource.net/\n");
710 else if(strcmp(subcmd, "list") == 0)
712 CPluginManager::CPlugin *pl;
720 for (i = g_PluginMngr._begin(); i != g_PluginMngr._end(); i++, len=0)
723 if (pl && pl->m_Status == Pl_Running)
726 if (!plapi || !plapi->QueryRunning(NULL, 0))
732 len += UTIL_Format(buffer, sizeof(buffer), " [%02d]", plnum);
734 plname = IS_STR_FILLED(plapi->GetName()) ? plapi->GetName() : pl->m_File.c_str();
735 len += UTIL_Format(&buffer[len], sizeof(buffer)-len, " %s", plname);
737 if (IS_STR_FILLED(plapi->GetVersion()))
739 len += UTIL_Format(&buffer[len], sizeof(buffer)-len, " (%s)", plapi->GetVersion());
741 if (IS_STR_FILLED(plapi->GetAuthor()))
743 UTIL_Format(&buffer[len], sizeof(buffer)-len, " by %s", plapi->GetAuthor());
746 CLIENT_CONMSG(client, "%s\n", buffer);
752 CLIENT_CONMSG(client, "No active plugins loaded.\n");
759 CLIENT_CONMSG(client, "Metamod:Source Menu\n");
760 CLIENT_CONMSG(client, "usage: meta <command>\n");
761 CLIENT_CONMSG(client, " credits - About Metamod:Source\n");
762 CLIENT_CONMSG(client, " list - List plugins\n");
763 CLIENT_CONMSG(client, " version - Version information\n");