1 /* ======== SourceMM ========
2 * Copyright (C) 2004-2007 Metamod:Source Development Team
3 * No warranties of any kind
7 * Author(s): David "BAILOPAN" Anderson
8 * ============================
17 #include "metamod_util.h"
18 #include "metamod_console.h"
19 #include "metamod_plugins.h"
25 using namespace SourceMM;
26 using namespace SourceHook;
29 * @brief Console Command Implementations
30 * @file concommands.cpp
33 #define CONMSG g_Metamod.ConPrintf
34 #define CLIENT_CONMSG g_Metamod.ClientConPrintf
36 bool Command_Meta(edict_t *pEdict, IMetamodSourceCommandInfo *info)
38 unsigned int args = info->GetArgCount();
42 const char *command = info->GetArg(1);
43 if (strcmp(command, "credits") == 0)
45 CONMSG("Metamod:Source was developed by:\n");
46 CONMSG(" SourceHook: Pavol \"PM OnoTo\" Marko\n");
47 CONMSG(" GameDLL/Plugins: David \"BAILOPAN\" Anderson\n");
48 CONMSG(" GameDLL: Scott \"Damaged Soul\" Ehlert\n");
49 CONMSG("For more information, see the official website\n");
50 CONMSG("http://www.sourcemm.net/\n");
54 else if (strcmp(command, "version") == 0)
56 CONMSG("Metamod:Source version %s\n", SOURCEMM_VERSION);
57 CONMSG("Compiled on: %s\n", SOURCEMM_DATE);
58 CONMSG("Plugin interface version: %d:%d\n", PLAPI_VERSION, PLAPI_MIN_VERSION);
59 CONMSG("SourceHook version: %d:%d\n", g_SourceHook.GetIfaceVersion(), g_SourceHook.GetImplVersion());
60 CONMSG("http://www.sourcemm.net/\n");
64 else if (strcmp(command, "game") == 0)
66 CONMSG("GameDLL Information\n");
67 CONMSG(" Description: %s\n", provider->GetGameDescription());
68 CONMSG(" Mod Path: %s\n", g_Metamod.GetBaseDir());
69 CONMSG(" DLL Path: %s\n", g_Metamod.GetGameBinaryPath());
70 CONMSG(" Interface: ServerGameDLL%03d\n", g_Metamod.GetGameDLLVersion());
72 // Display user messages
73 int messages = g_Metamod.GetUserMessageCount();
81 CONMSG(" User Messages: %-32.31s %-5s %-5s\n", "Name", "Index", "Size");
83 for (int i = 0; i < messages; i++)
85 msgname = g_Metamod.GetUserMessage(i, &msgsize);
87 CONMSG(" %-32.31s %-5d %-5d\n", msgname, i, msgsize);
90 CONMSG(" %d user message%s in total\n", messages, (messages > 1) ? "s" : "");
94 CONMSG(" User Messages: None\n");
99 CONMSG(" User Messages: Failed to get list of user messages\n");
104 else if (strcmp(command, "refresh") == 0)
107 g_Metamod.PathFormat(full_path,
110 g_Metamod.GetBaseDir(),
111 g_Metamod.GetPluginsFile());
113 LoadPluginsFromFile(full_path);
117 else if (strcmp(command, "list") == 0)
119 CPluginManager::CPlugin *pl;
125 int plnum = g_PluginMngr.GetPluginCount();
129 CONMSG("No plugins loaded.\n");
132 CONMSG("Listing %d plugin%s:\n", plnum, (plnum > 1) ? "s" : "");
135 for (i = g_PluginMngr._begin(); i != g_PluginMngr._end(); i++)
145 if (pl->m_Status != Pl_Running)
147 len += UTIL_Format(buffer, sizeof(buffer), " [%02d] <%s>", pl->m_Id, g_PluginMngr.GetStatusText(pl));
149 len += UTIL_Format(buffer, sizeof(buffer), " [%02d]", pl->m_Id);
152 if ((plapi = pl->m_API))
154 plname = IS_STR_FILLED(plapi->GetName()) ? plapi->GetName() : pl->m_File.c_str();
155 len += UTIL_Format(&buffer[len], sizeof(buffer)-len, " %s", plname);
157 if (IS_STR_FILLED(plapi->GetVersion()))
159 len += UTIL_Format(&buffer[len], sizeof(buffer)-len, " (%s)", plapi->GetVersion());
161 if (IS_STR_FILLED(plapi->GetAuthor()))
163 UTIL_Format(&buffer[len], sizeof(buffer)-len, " by %s", plapi->GetAuthor());
167 CONMSG("%s\n", buffer);
172 else if (strcmp(command, "cmds") == 0)
176 int id = atoi(info->GetArg(2));
177 CPluginManager::CPlugin *pl = g_PluginMngr.FindById(id);
181 CONMSG("Plugin %d not found.\n", id);
187 CONMSG("Plugin %d is not loaded.\n", id);
191 CONMSG("Console commands for %s:\n", pl->m_API->GetName());
192 List<ConCommandBase *>::iterator ci;
195 for (ci=pl->m_Cmds.begin(); ci!=pl->m_Cmds.end(); ci++)
198 CONMSG(" [%5d] %-s\n", count, (*ci)->GetName());
204 CONMSG("Usage: meta cmds <id>\n");
209 else if (strcmp(command, "cvars") == 0)
213 int id = atoi(info->GetArg(2));
214 CPluginManager::CPlugin *pl = g_PluginMngr.FindById(id);
218 CONMSG("Plugin %d not found.\n", id);
224 CONMSG("Plugin %d is not loaded.\n", id);
228 CONMSG("Registered cvars for %s:\n", pl->m_API->GetName());
229 List<ConCommandBase *>::iterator ci;
232 for (ci=pl->m_Cvars.begin(); ci!=pl->m_Cvars.end(); ci++)
235 CONMSG(" [%5d] %-s\n", count, (*ci)->GetName());
241 CONMSG("Usage: meta cvars <id>\n");
246 else if (strcmp(command, "info") == 0)
250 int id = atoi(info->GetArg(2));
251 CPluginManager::CPlugin *pl = g_PluginMngr.FindById(id);
254 CONMSG("Plugin %d not found.\n", id);
260 CONMSG("Plugin %d is not loaded.\n", id);
264 if (pl->m_Status == Pl_Paused)
266 CONMSG("Plugin %d is paused.\n", id);
268 else if (pl->m_Status == Pl_Running)
272 if (pl->m_API && pl->m_API->QueryRunning(run_msg, sizeof(run_msg)-1))
278 CONMSG("Plugin %d is running.\n", id);
282 CONMSG("Plugin %d is stopped: %s\n", id, run_msg);
285 CONMSG(" Name: \"%s\" by %s\n", pl->m_API->GetName(), pl->m_API->GetAuthor());
286 CONMSG(" Version: %s\n", pl->m_API->GetVersion());
287 CONMSG(" Description: %s\n", pl->m_API->GetDescription());
288 CONMSG(" License: %s\n", pl->m_API->GetLicense());
289 CONMSG(" URL: %s\n", pl->m_API->GetURL());
290 CONMSG(" Details: API %03d, Date: %s\n", pl->m_API->GetApiVersion(), pl->m_API->GetDate());
292 CONMSG("File: %s\n\n", pl->m_File.c_str());
298 CONMSG("Usage: meta info <id>\n");
303 else if (strcmp(command, "pause") == 0)
307 int id = atoi(info->GetArg(2));
311 if (!g_PluginMngr.Pause(id, error, sizeof(error)))
313 CONMSG("Pause failed: %s\n", error);
317 CONMSG("Plugin %d has been paused.\n", id);
323 CONMSG("Usage: meta pause <id>\n");
328 else if (strcmp(command, "unpause") == 0)
332 int id = atoi(info->GetArg(2));
335 if (!g_PluginMngr.Unpause(id, error, sizeof(error)))
337 CONMSG("Unpause failed: %s\n", error);
341 CONMSG("Plugin %d has been unpaused.\n", id);
347 CONMSG("Usage: meta unpause <id>\n");
352 else if (strcmp(command, "load") == 0)
356 const char *file = info->GetArg(2);
359 const char *alias = g_PluginMngr.LookupAlias(file);
365 if (file[0] == '/' || strcmp(&(file[1]), ":\\") == 0)
367 g_Metamod.PathFormat(full_path, sizeof(full_path), "%s", file);
371 const char *ext = UTIL_GetExtension(file);
372 #if defined WIN32 || defined _WIN32
373 ext = ext ? "" : ".dll";
375 ext = ext ? "" : "_i486.so";
377 g_Metamod.PathFormat(full_path,
380 g_Metamod.GetBaseDir(),
387 CPluginManager::CPlugin *pl;
389 PluginId id = g_PluginMngr.Load(full_path, Pl_Console, already, error, sizeof(error));
390 pl = g_PluginMngr.FindById(id);
391 if (!pl || id < Pl_MinId || (pl->m_Status < Pl_Paused))
393 CONMSG("Failed to load plugin %s (%s).\n", file, error);
399 CONMSG("Plugin \"%s\" loaded with id %d.\n", pl->m_API->GetName(), pl->m_Id);
401 CONMSG("Plugin \"%s\" is already loaded as %d.\n", pl->m_API->GetName(), pl->m_Id);
408 CONMSG("Usage: meta load <path>\n");
413 else if ( (strcmp(command, "alias") == 0) ||
414 (strcmp(command, "aliases") == 0) )
418 const char *alias = info->GetArg(2);
419 const char *value = info->GetArg(3);
421 g_PluginMngr.SetAlias(alias, value);
422 if (value == NULL || value[0] == '\0')
424 CONMSG("Deleted alias: %s.\n", alias);
426 CONMSG("Set alias \"%s\" to: %s\n", alias, value);
431 const char *alias = info->GetArg(2);
432 const char *value = g_PluginMngr.LookupAlias(alias);
435 CONMSG("Alias \"%s\" is set to: %s\n", alias, value);
437 CONMSG("Alias \"%s\" was not found.\n", alias);
442 List<CNameAlias *>::iterator iter, end;
445 iter = g_PluginMngr._alias_begin();
446 end = g_PluginMngr._alias_end();
450 CONMSG("%-10.9s %s\n", "Alias", "File");
452 for (; iter!=end; iter++)
455 CONMSG("%-10.9s %s\n", p->alias.c_str(), p->value.c_str());
459 CONMSG("%d aliases total.\n", total);
463 CONMSG("No aliases found.\n");
468 else if (strcmp(command, "unload") == 0)
472 const char *file = info->GetArg(2);
476 if (id == 0 && isalpha(file[0]))
479 const char *alias = g_PluginMngr.LookupAlias(file);
486 /* first check if it's a known filename */
487 if (file[0] == '/' || strcmp(&(file[1]), ":\\") == 0)
489 g_Metamod.PathFormat(full_path, sizeof(full_path), "%s", file);
493 const char *ext = UTIL_GetExtension(file);
494 #if defined WIN32 || defined _WIN32
495 ext = ext ? "" : ".dll";
497 ext = ext ? "" : "_i486.so";
499 g_Metamod.PathFormat(full_path,
502 g_Metamod.GetBaseDir(),
507 List<CPluginManager::CPlugin *>::iterator iter, end;
508 CPluginManager::CPlugin *pl;
509 iter = g_PluginMngr._begin();
510 end = g_PluginMngr._end();
511 for (; iter!=end; iter++)
514 if (strcmp(pl->m_File.c_str(), full_path) == 0)
522 CONMSG("Plugin \"%s\" not found.\n", full_path);
527 if (!g_PluginMngr.Unload(id, false, error, sizeof(error)))
529 CONMSG("Unload failed: %s\n", error);
533 CONMSG("Plugin %d unloaded.\n", id);
537 CONMSG("Usage: meta unload <id>\n");
541 else if (strcmp(command, "force_unload") == 0)
545 int id = atoi(info->GetArg(2));
548 if (!g_PluginMngr.Unload(id, false, error, sizeof(error)))
550 CONMSG("Force unload failed: %s\n", error);
554 CONMSG("Plugin %d force unloaded.\n", id);
560 CONMSG("Usage: meta force_unload <id>\n");
565 else if (strcmp(command, "clear") == 0)
567 if (!g_PluginMngr.UnloadAll())
569 CONMSG("One or more plugins resisted removal (cleaned anyway).\n");
573 CONMSG("All plugins unloaded.\n");
577 else if (strcmp(command, "retry") == 0)
581 int id = atoi(info->GetArg(2));
584 if (!g_PluginMngr.Retry(id, error, sizeof(error)))
586 CONMSG("Error reloading plugin: %s\n", error);
590 CONMSG("Plugin %d successfully reloaded.\n", id);
596 CONMSG("Usage: meta retry <id>\n");
603 CONMSG("Metamod:Source Menu\n");
604 CONMSG("usage: meta <command> [arguments]\n");
605 CONMSG(" alias - List or set an alias\n");
606 CONMSG(" clear - Unload all plugins forcefully\n");
607 CONMSG(" cmds - Show plugin commands\n");
608 CONMSG(" cvars - Show plugin cvars\n");
609 CONMSG(" credits - About Metamod:Source\n");
610 CONMSG(" force_unload - Forcefully unload a plugin\n");
611 CONMSG(" game - Information about GameDLL\n");
612 CONMSG(" info - Information about a plugin\n");
613 CONMSG(" list - List plugins\n");
614 CONMSG(" load - Load a plugin\n");
615 CONMSG(" pause - Pause a running plugin\n");
616 CONMSG(" refresh - Reparse plugins file\n");
617 CONMSG(" retry - Attempt to reload a plugin\n");
618 CONMSG(" unload - Unload a loaded plugin\n");
619 CONMSG(" unpause - Unpause a paused plugin\n");
620 CONMSG(" version - Version information\n");
625 bool Command_ClientMeta(edict_t *client, IMetamodSourceCommandInfo *info)
627 const char *cmd = info->GetArg(0);
629 if (strcmp(cmd, "meta") == 0)
631 unsigned int args = info->GetArgCount();
634 const char *subcmd = info->GetArg(1);
636 if (strcmp(subcmd, "credits") == 0)
638 CLIENT_CONMSG(client, "Metamod:Source was developed by:\n");
639 CLIENT_CONMSG(client, " SourceHook: Pavol \"PM OnoTo\" Marko\n");
640 CLIENT_CONMSG(client, " GameDLL/Plugins: David \"BAILOPAN\" Anderson\n");
641 CLIENT_CONMSG(client, " GameDLL: Scott \"Damaged Soul\" Ehlert\n");
642 CLIENT_CONMSG(client, "For more information, see the official website\n");
643 CLIENT_CONMSG(client, "http://www.sourcemm.net/\n");
647 else if(strcmp(subcmd, "version") == 0)
649 CLIENT_CONMSG(client, "Metamod:Source version %s\n", SOURCEMM_VERSION);
650 CLIENT_CONMSG(client, "Compiled on: %s\n", SOURCEMM_DATE);
651 CLIENT_CONMSG(client, "Plugin interface version: %d:%d\n", PLAPI_VERSION, PLAPI_MIN_VERSION);
652 CLIENT_CONMSG(client, "SourceHook version: %d:%d\n", g_SourceHook.GetIfaceVersion(), g_SourceHook.GetImplVersion());
653 CLIENT_CONMSG(client, "http://www.sourcemm.net/\n");
657 else if(strcmp(subcmd, "list") == 0)
659 CPluginManager::CPlugin *pl;
667 for (i = g_PluginMngr._begin(); i != g_PluginMngr._end(); i++, len=0)
670 if (pl && pl->m_Status == Pl_Running)
673 if (!plapi || !plapi->QueryRunning(NULL, 0))
679 len += UTIL_Format(buffer, sizeof(buffer), " [%02d]", plnum);
681 plname = IS_STR_FILLED(plapi->GetName()) ? plapi->GetName() : pl->m_File.c_str();
682 len += UTIL_Format(&buffer[len], sizeof(buffer)-len, " %s", plname);
684 if (IS_STR_FILLED(plapi->GetVersion()))
686 len += UTIL_Format(&buffer[len], sizeof(buffer)-len, " (%s)", plapi->GetVersion());
688 if (IS_STR_FILLED(plapi->GetAuthor()))
690 UTIL_Format(&buffer[len], sizeof(buffer)-len, " by %s", plapi->GetAuthor());
693 CLIENT_CONMSG(client, "%s\n", buffer);
699 CLIENT_CONMSG(client, "No active plugins loaded.\n");
706 CLIENT_CONMSG(client, "Metamod:Source Menu\n");
707 CLIENT_CONMSG(client, "usage: meta <command>\n");
708 CLIENT_CONMSG(client, " credits - About Metamod:Source\n");
709 CLIENT_CONMSG(client, " list - List plugins\n");
710 CLIENT_CONMSG(client, " version - Version information\n");