Renamed core-1.4 to core-legacy.
1 /* ======== SourceMM ========
2 * Copyright (C) 2004-2008 Metamod:Source Development Team
3 * No warranties of any kind
7 * Author(s): David "BAILOPAN" Anderson
8 * ============================
12 #include "convar_smm.h"
14 #include "concommands.h"
16 #include "sh_string.h"
18 #include "vsp_listener.h"
21 * @brief Console Command Implementations
22 * @file concommands.cpp
25 SMConVarAccessor g_SMConVarAccessor;
27 SMConVarAccessor::SMConVarAccessor()
29 m_TopConCommandBase = NULL;
32 bool SMConVarAccessor::RegisterConCommandBase(ConCommandBase *pCommand)
34 /* Add the FCVAR_GAMEDLL flag
35 * => No crash on exit!
36 * UPDATE: Do _not_ add the FCVAR_GAMEDLL flag here, as it
37 * causes the command to be unusable on listen servers until you load a map
38 * We will set the FCVAR_GAMEDLL flag on all commands we have registered once we are being unloaded
40 // pCommand->AddFlags(FCVAR_GAMEDLL);
41 m_RegisteredCommands.push_back(pCommand);
43 pCommand->SetNext(NULL);
44 g_Engine.icvar->RegisterConCommandBase(pCommand);
49 bool SMConVarAccessor::Register(ConCommandBase *pCommand)
51 /* Simple, don't mark as part of sourcemm! */
52 pCommand->SetNext(NULL);
53 g_Engine.icvar->RegisterConCommandBase(pCommand);
58 void SMConVarAccessor::MarkCommandsAsGameDLL()
60 for (SourceHook::List<ConCommandBase*>::iterator iter = m_RegisteredCommands.begin();
61 iter != m_RegisteredCommands.end(); ++iter)
63 (*iter)->AddFlags(FCVAR_GAMEDLL);
67 void SMConVarAccessor::Unregister(ConCommandBase *pCommand)
69 ConCommandBase *pCur = NULL;
70 ConCommandBase *pPrev = NULL;
72 if (!pCommand || !pCommand->IsRegistered())
77 pCur = g_Engine.icvar->GetCommands();
78 pCommand->SetRegistered(false);
80 if (!m_TopConCommandBase || !pCur)
87 *m_TopConCommandBase = const_cast<ConCommandBase *>(pCommand->GetNext());
88 pCommand->SetNext(NULL);
93 pCur = const_cast<ConCommandBase *>(pCur->GetNext());
99 pPrev->SetNext(const_cast<ConCommandBase *>(pCommand->GetNext()));
100 pCommand->SetNext(NULL);
104 pCur = const_cast<ConCommandBase *>(pCur->GetNext());
108 ConVar metamod_version("metamod_version", SOURCEMM_VERSION, FCVAR_SPONLY | FCVAR_NOTIFY, "Metamod:Source Version");
110 ConVar mm_pluginsfile("mm_pluginsfile", "addons\\metamod\\metaplugins.ini", FCVAR_SPONLY, "Metamod:Source Plugins File");
111 ConVar mm_basedir("mm_basedir", "addons\\metamod", FCVAR_SPONLY, "Metamod:Source base folder");
113 ConVar mm_pluginsfile("mm_pluginsfile", "addons/metamod/metaplugins.ini", FCVAR_SPONLY, "Metamod:Source Plugins File");
114 ConVar mm_basedir("mm_basedir", "addons/metamod", FCVAR_SPONLY, "Metamod:Source base folder");
117 CON_COMMAND(meta, "Metamod:Source Menu")
119 IVEngineServer *e = g_Engine.engine;
121 int args = e->Cmd_Argc();
123 if (g_VspListener.IsRootLoadMethod() && !g_bLevelChanged)
125 CONMSG("WARNING: You must change the map to activate Metamod:Source.\n");
131 const char *command = e->Cmd_Argv(1);
132 if (strcmp(command, "credits") == 0)
134 CONMSG("Metamod:Source was developed by:\n");
135 CONMSG(" SourceHook: Pavol \"PM OnoTo\" Marko\n");
136 CONMSG(" GameDLL/Plugins: David \"BAILOPAN\" Anderson\n");
137 CONMSG(" GameDLL: Scott \"Damaged Soul\" Ehlert\n");
138 CONMSG("For more information, see the official website\n");
139 CONMSG("http://www.metamodsource.net/\n");
142 } else if (strcmp(command, "version") == 0) {
143 CONMSG("Metamod:Source version %s\n", SOURCEMM_VERSION);
144 if (g_VspListener.IsRootLoadMethod())
146 CONMSG("Loaded As: Valve Server Plugin\n");
150 CONMSG("Loaded As: GameDLL (gameinfo.txt)\n");
152 CONMSG("Compiled on: %s\n", SOURCEMM_DATE);
153 CONMSG("Plugin interface version: %d:%d\n", PLAPI_VERSION, PLAPI_MIN_VERSION);
154 CONMSG("SourceHook version: %d:%d\n", g_SourceHook.GetIfaceVersion(), g_SourceHook.GetImplVersion());
155 CONMSG("http://www.metamodsource.net/\n");
158 } else if (strcmp(command, "game") == 0) {
159 CONMSG("GameDLL Information\n");
160 CONMSG(" Description: %s\n", g_GameDll.pGameDLL->GetGameDescription());
161 CONMSG(" Mod Path: %s\n", g_ModPath.c_str());
162 CONMSG(" DLL Path: %s\n", g_BinPath.c_str());
163 CONMSG(" Interface: ServerGameDLL%03d\n", g_GameDllVersion);
165 // Display user messages
166 if (g_SmmAPI.MsgCacheSuccessful())
170 int msgcount = g_SmmAPI.GetUserMessageCount();
174 CONMSG(" User Messages: %-32.31s %-5s %-5s\n", "Name", "Index", "Size");
176 for (int i = 0; i < msgcount; i++)
178 msgname = g_SmmAPI.GetUserMessage(i, &msgsize);
180 CONMSG(" %-32.31s %-5d %-5d\n", msgname, i, msgsize);
183 CONMSG(" %d user message%s in total\n", msgcount, (msgcount > 1) ? "s" : "");
185 CONMSG(" User Messages: None\n");
188 CONMSG(" User Messages: Failed to get list of user messages\n");
192 } else if (strcmp(command, "refresh") == 0) {
194 g_SmmAPI.PathFormat(full_path, sizeof(full_path), "%s/%s", g_ModPath.c_str(), GetPluginsFile());
196 LoadPluginsFromFile(full_path);
199 } else if (strcmp(command, "list") == 0) {
205 SourceMM::CPluginManager::CPlugin *pl;
206 unsigned int plnum = g_PluginMngr.GetPluginCount();
208 #define IS_STR_FILLED(var) (var != NULL && var[0] != '\0')
212 CONMSG("No plugins loaded.\n");
217 CONMSG("Listing %d plugin%s:\n", plnum, (plnum > 1) ? "s" : "");
220 for (i=g_PluginMngr._begin(); i!=g_PluginMngr._end(); i++)
230 if (pl->m_Status != Pl_Running)
232 len += UTIL_Format(buffer, sizeof(buffer), " [%02d] <%s>", pl->m_Id, g_PluginMngr.GetStatusText(pl));
236 len += UTIL_Format(buffer, sizeof(buffer), " [%02d]", pl->m_Id);
239 if ((plapi = pl->m_API))
241 plname = IS_STR_FILLED(plapi->GetName()) ? plapi->GetName() : pl->m_File.c_str();
242 len += UTIL_Format(&buffer[len], sizeof(buffer)-len, " %s", plname);
244 if (IS_STR_FILLED(plapi->GetVersion()))
246 len += UTIL_Format(&buffer[len], sizeof(buffer)-len, " (%s)", plapi->GetVersion());
248 if (IS_STR_FILLED(plapi->GetAuthor()))
250 UTIL_Format(&buffer[len], sizeof(buffer)-len, " by %s", plapi->GetAuthor());
254 CONMSG("%s\n", buffer);
258 } else if (strcmp(command, "cmds") == 0) {
261 int id = atoi(e->Cmd_Argv(2));
262 SourceMM::CPluginManager::CPlugin *pl = g_PluginMngr.FindById(id);
266 CONMSG("Plugin %d not found.\n", id);
272 CONMSG("Plugin %d is not loaded.\n", id);
274 CONMSG("Console commands for %s:\n", pl->m_API->GetName());
275 SourceHook::List<ConCommandBase *>::iterator ci;
278 for (ci=pl->m_Cmds.begin(); ci!=pl->m_Cmds.end(); ci++)
281 CONMSG(" [%5d] %-s\n", count, (*ci)->GetName());
285 CONMSG("Usage: meta cmds <id>\n");
289 } else if (strcmp(command, "cvars") == 0) {
292 int id = atoi(e->Cmd_Argv(2));
293 SourceMM::CPluginManager::CPlugin *pl = g_PluginMngr.FindById(id);
297 CONMSG("Plugin %d not found.\n", id);
303 CONMSG("Plugin %d is not loaded.\n", id);
305 CONMSG("Registered cvars for %s:\n", pl->m_API->GetName());
306 SourceHook::List<ConCommandBase *>::iterator ci;
309 for (ci=pl->m_Cvars.begin(); ci!=pl->m_Cvars.end(); ci++)
312 CONMSG(" [%5d] %-s\n", count, (*ci)->GetName());
316 CONMSG("Usage: meta cvars <id>\n");
320 } else if (strcmp(command, "info") == 0) {
323 int id = atoi(e->Cmd_Argv(2));
324 SourceMM::CPluginManager::CPlugin *pl = g_PluginMngr.FindById(id);
327 CONMSG("Plugin %d not found.\n", id);
333 CONMSG("Plugin %d is not loaded.\n", id);
335 if (pl->m_Status == Pl_Paused)
337 CONMSG("Plugin %d is paused.\n", id);
338 } else if (pl->m_Status == Pl_Running) {
341 if (pl->m_API && pl->m_API->QueryRunning(run_msg, sizeof(run_msg)-1))
345 CONMSG("Plugin %d is running.\n", id);
347 CONMSG("Plugin %d is stopped: %s\n", id, run_msg);
350 CONMSG(" Name: \"%s\" by %s\n", pl->m_API->GetName(), pl->m_API->GetAuthor());
351 CONMSG(" Version: %s\n", pl->m_API->GetVersion());
352 CONMSG(" Description: %s\n", pl->m_API->GetDescription());
353 CONMSG(" License: %s\n", pl->m_API->GetLicense());
354 CONMSG(" URL: %s\n", pl->m_API->GetURL());
355 CONMSG(" Details: API %03d, Date: %s\n", pl->m_API->GetApiVersion(), pl->m_API->GetDate());
357 CONMSG("File: %s\n\n", pl->m_File.c_str());
361 CONMSG("Usage: meta info <id>\n");
365 } else if (strcmp(command, "pause") == 0) {
368 int id = atoi(e->Cmd_Argv(2));
372 if (!g_PluginMngr.Pause(id, error, sizeof(error)))
374 CONMSG("Pause failed: %s\n", error);
378 CONMSG("Plugin %d has been paused.\n", id);
382 CONMSG("Usage: meta pause <id>\n");
386 } else if (strcmp(command, "unpause") == 0) {
389 int id = atoi(e->Cmd_Argv(2));
392 if (!g_PluginMngr.Unpause(id, error, sizeof(error)))
394 CONMSG("Unpause failed: %s\n", error);
398 CONMSG("Plugin %d has been unpaused.\n", id);
402 CONMSG("Usage: meta unpause <id>\n");
406 } else if (strcmp(command, "load") == 0) {
409 const char *file = e->Cmd_Argv(2);
412 const char *alias = g_PluginMngr.LookupAlias(file);
418 if (file[0] == '/' || strcmp(&(file[1]), ":\\") == 0)
420 g_SmmAPI.PathFormat(full_path, sizeof(full_path), "%s", file);
422 const char *ext = UTIL_GetExtension(file);
423 #if defined WIN32 || defined _WIN32
424 ext = ext ? "" : ".dll";
426 ext = ext ? "" : "_i486.so";
428 g_SmmAPI.PathFormat(full_path, sizeof(full_path), "%s/%s%s", g_ModPath.c_str(), file, ext);
433 SourceMM::CPluginManager::CPlugin *pl;
435 PluginId id = g_PluginMngr.Load(full_path, Pl_Console, already, error, sizeof(error));
436 pl = g_PluginMngr.FindById(id);
437 if (!pl || id < Pl_MinId || (pl->m_Status < Pl_Paused))
439 CONMSG("Failed to load plugin %s (%s).\n", file, error);
445 CONMSG("Plugin \"%s\" loaded with id %d.\n", pl->m_API->GetName(), pl->m_Id);
447 CONMSG("Plugin \"%s\" is already loaded as %d.\n", pl->m_API->GetName(), pl->m_Id);
452 CONMSG("Usage: meta load <path>\n");
456 } else if ( (strcmp(command, "alias") == 0) ||
457 (strcmp(command, "aliases") == 0) ) {
460 const char *alias = e->Cmd_Argv(2);
461 const char *value = e->Cmd_Argv(3);
463 g_PluginMngr.SetAlias(alias, value);
464 if (value[0] == '\0')
466 CONMSG("Deleted alias: %s.\n", alias);
468 CONMSG("Set alias \"%s\" to: %s\n", alias, value);
470 } else if (args >= 3) {
471 const char *alias = e->Cmd_Argv(2);
472 const char *value = g_PluginMngr.LookupAlias(alias);
475 CONMSG("Alias \"%s\" is set to: %s\n", alias, value);
477 CONMSG("Alias \"%s\" was not found.\n", alias);
480 SourceHook::List<SourceMM::CNameAlias *>::iterator iter, end;
481 SourceMM::CNameAlias *p;
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);
499 CONMSG("No aliases found.\n");
503 } else if (strcmp(command, "unload") == 0) {
506 const char *file = e->Cmd_Argv(2);
510 if (id == 0 && isalpha(file[0]))
513 const char *alias = g_PluginMngr.LookupAlias(file);
520 /* first check if it's a known filename */
521 if (file[0] == '/' || strcmp(&(file[1]), ":\\") == 0)
523 g_SmmAPI.PathFormat(full_path, sizeof(full_path), "%s", file);
525 const char *ext = UTIL_GetExtension(file);
526 #if defined WIN32 || defined _WIN32
527 ext = ext ? "" : ".dll";
529 ext = ext ? "" : "_i486.so";
531 g_SmmAPI.PathFormat(full_path, sizeof(full_path), "%s/%s%s", g_ModPath.c_str(), file, ext);
534 SourceHook::List<SourceMM::CPluginManager::CPlugin *>::iterator iter, end;
535 SourceMM::CPluginManager::CPlugin *pl;
536 iter = g_PluginMngr._begin();
537 end = g_PluginMngr._end();
538 for (; iter!=end; iter++)
541 if (strcmp(pl->m_File.c_str(), full_path) == 0)
549 CONMSG("Plugin \"%s\" not found.\n", full_path);
554 if (!g_PluginMngr.Unload(id, false, error, sizeof(error)))
556 CONMSG("Unload failed: %s\n", error);
560 CONMSG("Plugin %d unloaded.\n", id);
562 CONMSG("Usage: meta unload <id>\n");
565 } else if (strcmp(command, "force_unload") == 0) {
568 int id = atoi(e->Cmd_Argv(2));
571 if (!g_PluginMngr.Unload(id, false, error, sizeof(error)))
573 CONMSG("Force unload failed: %s\n", error);
577 CONMSG("Plugin %d force unloaded.\n", id);
581 CONMSG("Usage: meta force_unload <id>\n");
585 } 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");
595 } else if (strcmp(command, "retry") == 0) {
598 int id = atoi(e->Cmd_Argv(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);
611 CONMSG("Usage: meta retry <id>\n");
618 CONMSG("Metamod:Source Menu\n");
619 CONMSG("usage: meta <command> [arguments]\n");
620 CONMSG(" alias - List or set an alias\n");
621 CONMSG(" clear - Unload all plugins forcefully\n");
622 CONMSG(" cmds - Show plugin commands\n");
623 CONMSG(" cvars - Show plugin cvars\n");
624 CONMSG(" credits - About Metamod:Source\n");
625 CONMSG(" force_unload - Forcefully unload a plugin\n");
626 CONMSG(" game - Information about GameDLL\n");
627 CONMSG(" info - Information about a plugin\n");
628 CONMSG(" list - List plugins\n");
629 CONMSG(" load - Load a plugin\n");
630 CONMSG(" pause - Pause a running plugin\n");
631 CONMSG(" refresh - Reparse plugins file\n");
632 CONMSG(" retry - Attempt to reload a plugin\n");
633 CONMSG(" unload - Unload a loaded plugin\n");
634 CONMSG(" unpause - Unpause a paused plugin\n");
635 CONMSG(" version - Version information\n");
638 void ClientCommand_handler(edict_t *client)
640 IVEngineServer *e = g_Engine.engine;
641 const char *cmd = e->Cmd_Argv(0);
643 if (strcmp(cmd, "meta") == 0)
645 int args = e->Cmd_Argc();
648 const char *subcmd = e->Cmd_Argv(1);
650 if (strcmp(subcmd, "credits") == 0)
652 CLIENT_CONMSG(client, "Metamod:Source was developed by:\n");
653 CLIENT_CONMSG(client, " SourceHook: Pavol \"PM OnoTo\" Marko\n");
654 CLIENT_CONMSG(client, " Core: David \"BAILOPAN\" Anderson\n");
655 CLIENT_CONMSG(client, " Core: Scott \"Damaged Soul\" Ehlert\n");
656 CLIENT_CONMSG(client, "For more information, see the official website\n");
657 CLIENT_CONMSG(client, "http://www.metamodsource.net/\n");
659 RETURN_META(MRES_SUPERCEDE);
660 } else if(strcmp(subcmd, "version") == 0) {
661 CLIENT_CONMSG(client, "Metamod:Source version %s\n", SOURCEMM_VERSION);
662 CLIENT_CONMSG(client, "Compiled on: %s\n", SOURCEMM_DATE);
663 CLIENT_CONMSG(client, "Plugin interface version: %d:%d\n", PLAPI_VERSION, PLAPI_MIN_VERSION);
664 CLIENT_CONMSG(client, "SourceHook version: %d:%d\n", g_SourceHook.GetIfaceVersion(), g_SourceHook.GetImplVersion());
665 CLIENT_CONMSG(client, "http://www.metamodsource.net/\n");
667 RETURN_META(MRES_SUPERCEDE);
668 } else if(strcmp(subcmd, "list") == 0) {
669 SourceMM::CPluginManager::CPlugin *pl;
677 for (i = g_PluginMngr._begin(); i != g_PluginMngr._end(); i++, len=0)
680 if (pl && pl->m_Status == Pl_Running)
683 if (!plapi || !plapi->QueryRunning(NULL, 0))
689 len += UTIL_Format(buffer, sizeof(buffer), " [%02d]", plnum);
691 plname = IS_STR_FILLED(plapi->GetName()) ? plapi->GetName() : pl->m_File.c_str();
692 len += UTIL_Format(&buffer[len], sizeof(buffer)-len, " %s", plname);
694 if (IS_STR_FILLED(plapi->GetVersion()))
696 len += UTIL_Format(&buffer[len], sizeof(buffer)-len, " (%s)", plapi->GetVersion());
698 if (IS_STR_FILLED(plapi->GetAuthor()))
700 UTIL_Format(&buffer[len], sizeof(buffer)-len, " by %s", plapi->GetAuthor());
703 CLIENT_CONMSG(client, "%s\n", buffer);
709 CLIENT_CONMSG(client, "No active plugins loaded.\n");
712 RETURN_META(MRES_SUPERCEDE);
716 CLIENT_CONMSG(client, "Metamod:Source Menu\n");
717 CLIENT_CONMSG(client, "usage: meta <command>\n");
718 CLIENT_CONMSG(client, " credits - About Metamod:Source\n");
719 CLIENT_CONMSG(client, " list - List plugins\n");
720 CLIENT_CONMSG(client, " version - Version information\n");
722 RETURN_META(MRES_SUPERCEDE);
725 RETURN_META(MRES_IGNORED);
728 void SMConVarAccessor::UnloadMetamodCommands()
730 Unregister(&metamod_version);
731 Unregister(&mm_pluginsfile);
732 Unregister(&mm_basedir);
733 Unregister(&meta_command);
736 const char *GetPluginsFile()
738 return mm_pluginsfile.GetString();
741 const char *GetMetamodBaseDir()
743 return mm_basedir.GetString();
746 /* Signature for ICvar::GetCommands() in vstdlib for Win32 and Linux.
748 * 20226EE0 A1 50 5C 5A 20 mov eax,dword ptr ds:[205A5C50h] <-- What we want
751 #define CMDLIST_SIG "\xA1\x2A\x2A\x2A\x2A\xC3"
752 #define CMDLIST_SIGLEN 6
754 /* Linux symbol name of ConCommandBase list in vstdlib */
755 #define CMDLIST_SYMBOL "_ZN14ConCommandBase18s_pConCommandBasesE"
757 /* This function retrieves the address of the var that holds the top of the ConCommandBase list.
758 * Having this allows us to change the beginning of this list with ease.
760 * This craziness eliminates the need for the eternal command/cvar used previously which
761 * could have caused a crash as a result of registering commands/cvars more than once.
763 bool SMConVarAccessor::InitConCommandBaseList()
765 char *vfunc = UTIL_GetOrigFunction(&ICvar::GetCommands, g_Engine.icvar, g_CvarPatch);
773 if (UTIL_VerifySignature(vfunc, CMDLIST_SIG, CMDLIST_SIGLEN))
775 /* Skip past 0xA1 and get addr of ConCommandBase list var */
776 m_TopConCommandBase = *reinterpret_cast<ConCommandBase ***>(vfunc + 1);
779 #elif defined OS_LINUX
780 /* Try dlsym first */
781 char path[PATH_SIZE];
782 if (GetFileOfAddress((void *)g_Engine.icvar, path, sizeof(path)))
784 void *handle = dlopen(path, RTLD_NOW);
787 m_TopConCommandBase = reinterpret_cast<ConCommandBase **>(dlsym(handle, CMDLIST_SYMBOL));
793 /* If dlsym failed, then verify signature of function */
794 if (!m_TopConCommandBase && UTIL_VerifySignature(vfunc, CMDLIST_SIG, CMDLIST_SIGLEN))
796 /* Skip past 0xA1 and get addr of ConCommandBase list var */
797 m_TopConCommandBase = *reinterpret_cast<ConCommandBase ***>(vfunc + 1);