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.
34 #include "metamod_util.h"
35 #include "metamod_console.h"
36 #include "metamod_plugins.h"
42 using namespace SourceMM;
43 using namespace SourceHook;
46 * @brief Console Command Implementations
47 * @file concommands.cpp
50 #define CONMSG g_Metamod.ConPrintf
51 #define CLIENT_CONMSG g_Metamod.ClientConPrintf
53 bool Command_Meta(IMetamodSourceCommandInfo *info)
55 unsigned int args = info->GetArgCount();
59 const char *command = info->GetArg(1);
60 if (strcmp(command, "credits") == 0)
62 CONMSG("Metamod:Source was developed by:\n");
63 CONMSG(" SourceHook: Pavol \"PM OnoTo\" Marko\n");
64 CONMSG(" GameDLL/Plugins: David \"BAILOPAN\" Anderson\n");
65 CONMSG(" GameDLL: Scott \"Damaged Soul\" Ehlert\n");
66 CONMSG("For more information, see the official website\n");
67 CONMSG("http://www.sourcemm.net/\n");
71 else if (strcmp(command, "version") == 0)
73 CONMSG("Metamod:Source version %s\n", SOURCEMM_VERSION);
74 CONMSG("Compiled on: %s\n", SOURCEMM_DATE);
75 CONMSG("Plugin interface version: %d:%d\n", PLAPI_VERSION, PLAPI_MIN_VERSION);
76 CONMSG("SourceHook version: %d:%d\n", g_SourceHook.GetIfaceVersion(), g_SourceHook.GetImplVersion());
77 CONMSG("http://www.sourcemm.net/\n");
81 else if (strcmp(command, "game") == 0)
83 CONMSG("GameDLL Information\n");
84 CONMSG(" Description: %s\n", provider->GetGameDescription());
85 CONMSG(" Mod Path: %s\n", g_Metamod.GetBaseDir());
86 CONMSG(" DLL Path: %s\n", g_Metamod.GetGameBinaryPath());
87 CONMSG(" Interface: ServerGameDLL%03d\n", g_Metamod.GetGameDLLVersion());
89 // Display user messages
90 int messages = g_Metamod.GetUserMessageCount();
98 CONMSG(" User Messages: %-32.31s %-5s %-5s\n", "Name", "Index", "Size");
100 for (int i = 0; i < messages; i++)
102 msgname = g_Metamod.GetUserMessage(i, &msgsize);
104 CONMSG(" %-32.31s %-5d %-5d\n", msgname, i, msgsize);
107 CONMSG(" %d user message%s in total\n", messages, (messages > 1) ? "s" : "");
111 CONMSG(" User Messages: None\n");
116 CONMSG(" User Messages: Failed to get list of user messages\n");
121 else if (strcmp(command, "refresh") == 0)
124 g_Metamod.PathFormat(full_path,
127 g_Metamod.GetBaseDir(),
128 g_Metamod.GetPluginsFile());
130 LoadPluginsFromFile(full_path);
134 else if (strcmp(command, "list") == 0)
136 CPluginManager::CPlugin *pl;
142 int plnum = g_PluginMngr.GetPluginCount();
146 CONMSG("No plugins loaded.\n");
149 CONMSG("Listing %d plugin%s:\n", plnum, (plnum > 1) ? "s" : "");
152 for (i = g_PluginMngr._begin(); i != g_PluginMngr._end(); i++)
162 if (pl->m_Status != Pl_Running)
164 len += UTIL_Format(buffer, sizeof(buffer), " [%02d] <%s>", pl->m_Id, g_PluginMngr.GetStatusText(pl));
166 len += UTIL_Format(buffer, sizeof(buffer), " [%02d]", pl->m_Id);
169 if ((plapi = pl->m_API))
171 plname = IS_STR_FILLED(plapi->GetName()) ? plapi->GetName() : pl->m_File.c_str();
172 len += UTIL_Format(&buffer[len], sizeof(buffer)-len, " %s", plname);
174 if (IS_STR_FILLED(plapi->GetVersion()))
176 len += UTIL_Format(&buffer[len], sizeof(buffer)-len, " (%s)", plapi->GetVersion());
178 if (IS_STR_FILLED(plapi->GetAuthor()))
180 UTIL_Format(&buffer[len], sizeof(buffer)-len, " by %s", plapi->GetAuthor());
184 CONMSG("%s\n", buffer);
189 else if (strcmp(command, "cmds") == 0)
193 int id = atoi(info->GetArg(2));
194 CPluginManager::CPlugin *pl = g_PluginMngr.FindById(id);
198 CONMSG("Plugin %d not found.\n", id);
204 CONMSG("Plugin %d is not loaded.\n", id);
208 CONMSG("Console commands for %s:\n", pl->m_API->GetName());
209 List<ConCommandBase *>::iterator ci;
212 for (ci=pl->m_Cmds.begin(); ci!=pl->m_Cmds.end(); ci++)
215 CONMSG(" [%5d] %-s\n", count, (*ci)->GetName());
221 CONMSG("Usage: meta cmds <id>\n");
226 else if (strcmp(command, "cvars") == 0)
230 int id = atoi(info->GetArg(2));
231 CPluginManager::CPlugin *pl = g_PluginMngr.FindById(id);
235 CONMSG("Plugin %d not found.\n", id);
241 CONMSG("Plugin %d is not loaded.\n", id);
245 CONMSG("Registered cvars for %s:\n", pl->m_API->GetName());
246 List<ConCommandBase *>::iterator ci;
249 for (ci=pl->m_Cvars.begin(); ci!=pl->m_Cvars.end(); ci++)
252 CONMSG(" [%5d] %-s\n", count, (*ci)->GetName());
258 CONMSG("Usage: meta cvars <id>\n");
263 else if (strcmp(command, "info") == 0)
267 int id = atoi(info->GetArg(2));
268 CPluginManager::CPlugin *pl = g_PluginMngr.FindById(id);
271 CONMSG("Plugin %d not found.\n", id);
277 CONMSG("Plugin %d is not loaded.\n", id);
281 if (pl->m_Status == Pl_Paused)
283 CONMSG("Plugin %d is paused.\n", id);
285 else if (pl->m_Status == Pl_Running)
289 if (pl->m_API && pl->m_API->QueryRunning(run_msg, sizeof(run_msg)-1))
295 CONMSG("Plugin %d is running.\n", id);
299 CONMSG("Plugin %d is stopped: %s\n", id, run_msg);
302 CONMSG(" Name: \"%s\" by %s\n", pl->m_API->GetName(), pl->m_API->GetAuthor());
303 CONMSG(" Version: %s\n", pl->m_API->GetVersion());
304 CONMSG(" Description: %s\n", pl->m_API->GetDescription());
305 CONMSG(" License: %s\n", pl->m_API->GetLicense());
306 CONMSG(" URL: %s\n", pl->m_API->GetURL());
307 CONMSG(" Details: API %03d, Date: %s\n", pl->m_API->GetApiVersion(), pl->m_API->GetDate());
309 CONMSG("File: %s\n\n", pl->m_File.c_str());
315 CONMSG("Usage: meta info <id>\n");
320 else if (strcmp(command, "pause") == 0)
324 int id = atoi(info->GetArg(2));
328 if (!g_PluginMngr.Pause(id, error, sizeof(error)))
330 CONMSG("Pause failed: %s\n", error);
334 CONMSG("Plugin %d has been paused.\n", id);
340 CONMSG("Usage: meta pause <id>\n");
345 else if (strcmp(command, "unpause") == 0)
349 int id = atoi(info->GetArg(2));
352 if (!g_PluginMngr.Unpause(id, error, sizeof(error)))
354 CONMSG("Unpause failed: %s\n", error);
358 CONMSG("Plugin %d has been unpaused.\n", id);
364 CONMSG("Usage: meta unpause <id>\n");
369 else if (strcmp(command, "load") == 0)
373 const char *file = info->GetArg(2);
376 const char *alias = g_PluginMngr.LookupAlias(file);
382 if (file[0] == '/' || strcmp(&(file[1]), ":\\") == 0)
384 g_Metamod.PathFormat(full_path, sizeof(full_path), "%s", file);
388 const char *ext = UTIL_GetExtension(file);
389 #if defined WIN32 || defined _WIN32
390 ext = ext ? "" : ".dll";
392 ext = ext ? "" : "_i486.so";
394 g_Metamod.PathFormat(full_path,
397 g_Metamod.GetBaseDir(),
404 CPluginManager::CPlugin *pl;
406 PluginId id = g_PluginMngr.Load(full_path, Pl_Console, already, error, sizeof(error));
407 pl = g_PluginMngr.FindById(id);
408 if (!pl || id < Pl_MinId || (pl->m_Status < Pl_Paused))
410 CONMSG("Failed to load plugin %s (%s).\n", file, error);
416 CONMSG("Plugin \"%s\" loaded with id %d.\n", pl->m_API->GetName(), pl->m_Id);
418 CONMSG("Plugin \"%s\" is already loaded as %d.\n", pl->m_API->GetName(), pl->m_Id);
425 CONMSG("Usage: meta load <path>\n");
430 else if ( (strcmp(command, "alias") == 0) ||
431 (strcmp(command, "aliases") == 0) )
435 const char *alias = info->GetArg(2);
436 const char *value = info->GetArg(3);
438 g_PluginMngr.SetAlias(alias, value);
439 if (value == NULL || value[0] == '\0')
441 CONMSG("Deleted alias: %s.\n", alias);
443 CONMSG("Set alias \"%s\" to: %s\n", alias, value);
448 const char *alias = info->GetArg(2);
449 const char *value = g_PluginMngr.LookupAlias(alias);
452 CONMSG("Alias \"%s\" is set to: %s\n", alias, value);
454 CONMSG("Alias \"%s\" was not found.\n", alias);
459 List<CNameAlias *>::iterator iter, end;
462 iter = g_PluginMngr._alias_begin();
463 end = g_PluginMngr._alias_end();
467 CONMSG("%-10.9s %s\n", "Alias", "File");
469 for (; iter!=end; iter++)
472 CONMSG("%-10.9s %s\n", p->alias.c_str(), p->value.c_str());
476 CONMSG("%d aliases total.\n", total);
480 CONMSG("No aliases found.\n");
485 else if (strcmp(command, "unload") == 0)
489 const char *file = info->GetArg(2);
493 if (id == 0 && isalpha(file[0]))
496 const char *alias = g_PluginMngr.LookupAlias(file);
503 /* first check if it's a known filename */
504 if (file[0] == '/' || strcmp(&(file[1]), ":\\") == 0)
506 g_Metamod.PathFormat(full_path, sizeof(full_path), "%s", file);
510 const char *ext = UTIL_GetExtension(file);
511 #if defined WIN32 || defined _WIN32
512 ext = ext ? "" : ".dll";
514 ext = ext ? "" : "_i486.so";
516 g_Metamod.PathFormat(full_path,
519 g_Metamod.GetBaseDir(),
524 List<CPluginManager::CPlugin *>::iterator iter, end;
525 CPluginManager::CPlugin *pl;
526 iter = g_PluginMngr._begin();
527 end = g_PluginMngr._end();
528 for (; iter!=end; iter++)
531 if (strcmp(pl->m_File.c_str(), full_path) == 0)
539 CONMSG("Plugin \"%s\" not found.\n", full_path);
544 if (!g_PluginMngr.Unload(id, false, error, sizeof(error)))
546 CONMSG("Unload failed: %s\n", error);
550 CONMSG("Plugin %d unloaded.\n", id);
554 CONMSG("Usage: meta unload <id>\n");
558 else if (strcmp(command, "force_unload") == 0)
562 int id = atoi(info->GetArg(2));
565 if (!g_PluginMngr.Unload(id, false, error, sizeof(error)))
567 CONMSG("Force unload failed: %s\n", error);
571 CONMSG("Plugin %d force unloaded.\n", id);
577 CONMSG("Usage: meta force_unload <id>\n");
582 else if (strcmp(command, "clear") == 0)
584 if (!g_PluginMngr.UnloadAll())
586 CONMSG("One or more plugins resisted removal (cleaned anyway).\n");
590 CONMSG("All plugins unloaded.\n");
594 else if (strcmp(command, "retry") == 0)
598 int id = atoi(info->GetArg(2));
601 if (!g_PluginMngr.Retry(id, error, sizeof(error)))
603 CONMSG("Error reloading plugin: %s\n", error);
607 CONMSG("Plugin %d successfully reloaded.\n", id);
613 CONMSG("Usage: meta retry <id>\n");
620 CONMSG("Metamod:Source Menu\n");
621 CONMSG("usage: meta <command> [arguments]\n");
622 CONMSG(" alias - List or set an alias\n");
623 CONMSG(" clear - Unload all plugins forcefully\n");
624 CONMSG(" cmds - Show plugin commands\n");
625 CONMSG(" cvars - Show plugin cvars\n");
626 CONMSG(" credits - About Metamod:Source\n");
627 CONMSG(" force_unload - Forcefully unload a plugin\n");
628 CONMSG(" game - Information about GameDLL\n");
629 CONMSG(" info - Information about a plugin\n");
630 CONMSG(" list - List plugins\n");
631 CONMSG(" load - Load a plugin\n");
632 CONMSG(" pause - Pause a running plugin\n");
633 CONMSG(" refresh - Reparse plugins file\n");
634 CONMSG(" retry - Attempt to reload a plugin\n");
635 CONMSG(" unload - Unload a loaded plugin\n");
636 CONMSG(" unpause - Unpause a paused plugin\n");
637 CONMSG(" version - Version information\n");
642 bool Command_ClientMeta(edict_t *client, IMetamodSourceCommandInfo *info)
644 const char *cmd = info->GetArg(0);
646 if (strcmp(cmd, "meta") == 0)
648 unsigned int args = info->GetArgCount();
651 const char *subcmd = info->GetArg(1);
653 if (strcmp(subcmd, "credits") == 0)
655 CLIENT_CONMSG(client, "Metamod:Source was developed by:\n");
656 CLIENT_CONMSG(client, " SourceHook: Pavol \"PM OnoTo\" Marko\n");
657 CLIENT_CONMSG(client, " GameDLL/Plugins: David \"BAILOPAN\" Anderson\n");
658 CLIENT_CONMSG(client, " GameDLL: Scott \"Damaged Soul\" Ehlert\n");
659 CLIENT_CONMSG(client, "For more information, see the official website\n");
660 CLIENT_CONMSG(client, "http://www.sourcemm.net/\n");
664 else if(strcmp(subcmd, "version") == 0)
666 CLIENT_CONMSG(client, "Metamod:Source version %s\n", SOURCEMM_VERSION);
667 CLIENT_CONMSG(client, "Compiled on: %s\n", SOURCEMM_DATE);
668 CLIENT_CONMSG(client, "Plugin interface version: %d:%d\n", PLAPI_VERSION, PLAPI_MIN_VERSION);
669 CLIENT_CONMSG(client, "SourceHook version: %d:%d\n", g_SourceHook.GetIfaceVersion(), g_SourceHook.GetImplVersion());
670 CLIENT_CONMSG(client, "http://www.sourcemm.net/\n");
674 else if(strcmp(subcmd, "list") == 0)
676 CPluginManager::CPlugin *pl;
684 for (i = g_PluginMngr._begin(); i != g_PluginMngr._end(); i++, len=0)
687 if (pl && pl->m_Status == Pl_Running)
690 if (!plapi || !plapi->QueryRunning(NULL, 0))
696 len += UTIL_Format(buffer, sizeof(buffer), " [%02d]", plnum);
698 plname = IS_STR_FILLED(plapi->GetName()) ? plapi->GetName() : pl->m_File.c_str();
699 len += UTIL_Format(&buffer[len], sizeof(buffer)-len, " %s", plname);
701 if (IS_STR_FILLED(plapi->GetVersion()))
703 len += UTIL_Format(&buffer[len], sizeof(buffer)-len, " (%s)", plapi->GetVersion());
705 if (IS_STR_FILLED(plapi->GetAuthor()))
707 UTIL_Format(&buffer[len], sizeof(buffer)-len, " by %s", plapi->GetAuthor());
710 CLIENT_CONMSG(client, "%s\n", buffer);
716 CLIENT_CONMSG(client, "No active plugins loaded.\n");
723 CLIENT_CONMSG(client, "Metamod:Source Menu\n");
724 CLIENT_CONMSG(client, "usage: meta <command>\n");
725 CLIENT_CONMSG(client, " credits - About Metamod:Source\n");
726 CLIENT_CONMSG(client, " list - List plugins\n");
727 CLIENT_CONMSG(client, " version - Version information\n");