Mm, oopsy. ICvar::GetCommandLineValue could return NULL for the plugins file. That would be bad. :(
(Now fixed with a call to GetPluginsFile in case it is NULL)
1 /* ======== SourceMM ========
2 * Copyright (C) 2004-2006 Metamod:Source Development Team
3 * No warranties of any kind
7 * Author(s): David "BAILOPAN" Anderson
8 * ============================
13 #include "concommands.h"
15 #include "sh_string.h"
19 * @brief Console Command Implementations
20 * @file concommands.cpp
23 CAlwaysRegisterableCommand g_EternalCommand;
24 SMConVarAccessor g_SMConVarAccessor;
26 bool SMConVarAccessor::RegisterConCommandBase(ConCommandBase *pCommand)
28 // Add the FCVAR_GAMEDLL flag
29 // => No crash on exit!
30 // UPDATE: Do _not_ add the FCVAR_GAMEDLL flag here, as it
31 // causes the command to be unusable on listenservers until you load a map
32 // We will set the FCVAR_GAMEDLL flag on all commands we have registered once we are being unloaded
33 //pCommand->AddFlags(FCVAR_GAMEDLL);
34 m_RegisteredCommands.push_back(pCommand);
36 pCommand->SetNext( NULL );
37 g_Engine.icvar->RegisterConCommandBase(pCommand);
42 bool SMConVarAccessor::Register(ConCommandBase *pCommand)
44 //simple, don't mark as part of sourcemm!
45 pCommand->SetNext( NULL );
46 g_Engine.icvar->RegisterConCommandBase(pCommand);
51 void SMConVarAccessor::MarkCommandsAsGameDLL()
53 for (SourceHook::List<ConCommandBase*>::iterator iter = m_RegisteredCommands.begin();
54 iter != m_RegisteredCommands.end(); ++iter)
56 (*iter)->AddFlags(FCVAR_GAMEDLL);
60 void SMConVarAccessor::Unregister(ConCommandBase *pCommand)
62 ICvar *cv = g_Engine.icvar;
63 ConCommandBase *ptr = cv->GetCommands();
68 g_EternalCommand.BringToFront();
69 g_EternalCommand.SetNext(const_cast<ConCommandBase *>(pCommand->GetNext()));
71 //find us and unregister us
72 ConCommandBase *pPrev = NULL;
78 ptr = const_cast<ConCommandBase *>(ptr->GetNext());
80 if (pPrev && ptr == pCommand)
82 pPrev->SetNext(const_cast<ConCommandBase *>(pCommand->GetNext()));
87 void SMConVarAccessor::UnregisterGameDLLCommands()
89 ConCommandBase *begin = g_Engine.icvar->GetCommands();
90 ConCommandBase *iter = begin;
91 ConCommandBase *prev = NULL;
94 // watch out for the ETERNAL COMMAND!
95 if (iter != &g_EternalCommand && iter->IsBitSet(FCVAR_GAMEDLL))
100 g_EternalCommand.BringToFront();
101 iter = const_cast<ConCommandBase*>(iter->GetNext());
102 g_EternalCommand.SetNext(iter);
103 prev = &g_EternalCommand;
108 iter = const_cast<ConCommandBase*>(iter->GetNext());
114 iter = const_cast<ConCommandBase*>(iter->GetNext());
118 ConVar metamod_version("metamod_version", SOURCEMM_VERSION, FCVAR_REPLICATED | FCVAR_SPONLY | FCVAR_NOTIFY, "Metamod:Source Version");
119 #if defined WIN32 || defined _WIN32
120 ConVar mm_pluginsfile("mm_pluginsfile", "addons\\metamod\\metaplugins.ini", FCVAR_SPONLY, "Metamod:Source Plugins File");
122 ConVar mm_pluginsfile("mm_pluginsfile", "addons/metamod/metaplugins.ini", FCVAR_SPONLY, "Metamod:Source Plugins File");
125 CON_COMMAND(meta, "Metamod:Source Menu")
127 IVEngineServer *e = g_Engine.engine;
129 int args = e->Cmd_Argc();
133 const char *command = e->Cmd_Argv(1);
134 if (strcmp(command, "credits") == 0)
136 CONMSG("Metamod:Source was developed by:\n");
137 CONMSG(" SourceHook: Pavol \"PM OnoTo\" Marko\n");
138 CONMSG(" GameDLL/Plugins: David \"BAILOPAN\" Anderson\n");
139 CONMSG(" GameDLL: Scott \"Damaged Soul\" Ehlert\n");
140 CONMSG("For more information, see the official website\n");
141 CONMSG("http://www.sourcemm.net/\n");
144 } else if (strcmp(command, "version") == 0) {
145 CONMSG("Metamod:Source version %s\n", SOURCEMM_VERSION);
146 CONMSG("Compiled on: %s\n", SOURCEMM_DATE);
147 CONMSG("Plugin interface version: %d:%d\n", PLAPI_VERSION, PLAPI_MIN_VERSION);
148 CONMSG("SourceHook version: %d:%d\n", g_SourceHook.GetIfaceVersion(), g_SourceHook.GetImplVersion());
149 CONMSG("http://www.sourcemm.net/\n");
152 } else if (strcmp(command, "game") == 0) {
153 CONMSG("GameDLL Information\n");
154 CONMSG(" Mod path: %s\n", g_ModPath.c_str());
155 CONMSG(" Dll path: %s\n", g_BinPath.c_str());
158 } else if (strcmp(command, "refresh") == 0) {
160 g_SmmAPI.PathFormat(full_path, sizeof(full_path) - 1, "%s/%s", g_ModPath.c_str(), GetPluginsFile());
162 LoadPluginsFromFile(full_path);
165 } else if (strcmp(command, "list") == 0) {
166 SourceMM::CPluginManager::CPlugin *pl;
168 const char *status="";
169 const char *version=NULL;
170 const char *name=NULL;
171 const char *author=NULL;
173 CONMSG("-Id- %-20.19s %-10.9s %-12.11s %-8.7s\n", "Name", "Version", "Author", "Status");
174 for (i=g_PluginMngr._begin(); i!=g_PluginMngr._end(); i++)
179 if (pl->m_Status == Pl_Paused)
182 } else if (pl->m_Status == Pl_Running) {
183 if (pl->m_API && pl->m_API->QueryRunning(NULL, 0))
187 } else if (pl->m_Status == Pl_Refused) {
189 } else if (pl->m_Status == Pl_Error) {
191 } else if (pl->m_Status == Pl_NotFound) {
197 version = pl->m_API->GetVersion();
198 author = pl->m_API->GetAuthor();
199 name = pl->m_API->GetName();
211 name = pl->m_File.c_str();
214 CONMSG("[%02d] %-20.19s %-10.9s %-12.11s %-8.7s\n", pl->m_Id, name, version, author, status);
220 } else if (strcmp(command, "cmds") == 0) {
223 int id = atoi(e->Cmd_Argv(2));
224 SourceMM::CPluginManager::CPlugin *pl = g_PluginMngr.FindById(id);
228 CONMSG("Plugin %d not found.\n", id);
234 CONMSG("Plugin %d is not loaded.\n", id);
236 CONMSG("Console commands for %s:\n", pl->m_API->GetName());
237 SourceHook::List<ConCommandBase *>::iterator ci;
240 for (ci=pl->m_Cmds.begin(); ci!=pl->m_Cmds.end(); ci++)
243 CONMSG(" [%5d] %-s\n", count, (*ci)->GetName());
247 CONMSG("Usage: meta cmds <id>\n");
251 } else if (strcmp(command, "cvars") == 0) {
254 int id = atoi(e->Cmd_Argv(2));
255 SourceMM::CPluginManager::CPlugin *pl = g_PluginMngr.FindById(id);
259 CONMSG("Plugin %d not found.\n", id);
265 CONMSG("Plugin %d is not loaded.\n", id);
267 CONMSG("Registered cvars for %s:\n", pl->m_API->GetName());
268 SourceHook::List<ConCommandBase *>::iterator ci;
271 for (ci=pl->m_Cvars.begin(); ci!=pl->m_Cvars.end(); ci++)
274 CONMSG(" [%5d] %-s\n", count, (*ci)->GetName());
278 CONMSG("Usage: meta cvars <id>\n");
282 } else if (strcmp(command, "info") == 0) {
285 int id = atoi(e->Cmd_Argv(2));
286 SourceMM::CPluginManager::CPlugin *pl = g_PluginMngr.FindById(id);
289 CONMSG("Plugin %d not found.\n", id);
295 CONMSG("Plugin %d is not loaded.\n", id);
297 if (pl->m_Status == Pl_Paused)
299 CONMSG("Plugin %d is paused.\n", id);
300 } else if (pl->m_Status == Pl_Running) {
303 if (pl->m_API && pl->m_API->QueryRunning(run_msg, sizeof(run_msg)-1))
307 CONMSG("Plugin %d is running.\n", id);
309 CONMSG("Plugin %d is stopped: %s\n", id, run_msg);
312 CONMSG(" Name: \"%s\" by %s\n", pl->m_API->GetName(), pl->m_API->GetAuthor());
313 CONMSG(" Version: %s\n", pl->m_API->GetVersion());
314 CONMSG(" Description: %s\n", pl->m_API->GetDescription());
315 CONMSG(" License: %s\n", pl->m_API->GetLicense());
316 CONMSG(" URL: %s\n", pl->m_API->GetURL());
317 CONMSG(" Details: API %03d, Date: %s\n", pl->m_API->GetApiVersion(), pl->m_API->GetDate());
319 CONMSG("File: %s\n\n", pl->m_File.c_str());
323 CONMSG("Usage: meta info <id>\n");
327 } else if (strcmp(command, "pause") == 0) {
330 int id = atoi(e->Cmd_Argv(2));
334 if (!g_PluginMngr.Pause(id, error, sizeof(error)-1))
336 CONMSG("Pause failed: %s\n", error);
340 CONMSG("Plugin %d has been paused.\n", id);
344 CONMSG("Usage: meta pause <id>\n");
348 } else if (strcmp(command, "unpause") == 0) {
351 int id = atoi(e->Cmd_Argv(2));
354 if (!g_PluginMngr.Unpause(id, error, sizeof(error)-1))
356 CONMSG("Unpause failed: %s\n", error);
360 CONMSG("Plugin %d has been unpaused.\n", id);
364 CONMSG("Usage: meta unpause <id>\n");
368 } else if (strcmp(command, "load") == 0) {
371 const char *file = e->Cmd_Argv(2);
374 if (file[0] == '/' || strcmp(&(file[1]), ":\\") == 0)
376 g_SmmAPI.PathFormat(full_path, sizeof(full_path)-1, "%s", file);
377 snprintf(full_path, sizeof(full_path)-1, "%s", file);
379 const char *ext = UTIL_GetExtension(file);
380 #if defined WIN32 || defined _WIN32
381 ext = ext ? "" : ".dll";
383 ext = ext ? "" : "_i486.so";
385 g_SmmAPI.PathFormat(full_path, sizeof(full_path)-1, "%s/%s%s", g_ModPath.c_str(), file, ext);
390 SourceMM::CPluginManager::CPlugin *pl;
392 PluginId id = g_PluginMngr.Load(full_path, Pl_Console, already, error, sizeof(error)-1);
393 pl = g_PluginMngr.FindById(id);
394 if (!pl || id < Pl_MinId || (pl->m_Status < Pl_Paused))
396 CONMSG("Failed to load plugin %s (%s).\n", file, error);
402 CONMSG("Plugin \"%s\" loaded with id %d.\n", pl->m_API->GetName(), pl->m_Id);
404 CONMSG("Plugin \"%s\" is already loaded as %d.\n", pl->m_API->GetName(), pl->m_Id);
409 CONMSG("Usage: meta load <path>\n");
413 } else if (strcmp(command, "unload") == 0) {
416 int id = atoi(e->Cmd_Argv(2));
419 if (!g_PluginMngr.Unload(id, false, error, sizeof(error)-1))
421 CONMSG("Unload failed: %s\n", error);
425 CONMSG("Plugin %d unloaded.\n", id);
429 CONMSG("Usage: meta unload <id>\n");
433 } else if (strcmp(command, "force_unload") == 0) {
436 int id = atoi(e->Cmd_Argv(2));
439 if (!g_PluginMngr.Unload(id, false, error, sizeof(error)-1))
441 CONMSG("Force unload failed: %s\n", error);
445 CONMSG("Plugin %d force unloaded.\n", id);
449 CONMSG("Usage: meta force_unload <id>\n");
453 } else if (strcmp(command, "clear") == 0) {
454 if (!g_PluginMngr.UnloadAll())
456 CONMSG("One or more plugins resisted removal (cleaned anyway).\n");
460 CONMSG("All plugins unloaded.\n");
463 } else if (strcmp(command, "retry") == 0) {
466 int id = atoi(e->Cmd_Argv(2));
469 if (!g_PluginMngr.Retry(id, error, sizeof(error)-1))
471 CONMSG("Error reloading plugin: %s\n", error);
475 CONMSG("Plugin %d successfully reloaded.\n", id);
479 CONMSG("Usage: meta retry <id>\n");
486 CONMSG("Metamod:Source Menu\n");
487 CONMSG("usage: meta <command> [arguments]\n");
488 CONMSG(" clear - Unload all plugins forcefully\n");
489 CONMSG(" cmds - Show plugin commands\n");
490 CONMSG(" cvars - Show plugin cvars\n");
491 CONMSG(" credits - About Metamod:Source\n");
492 CONMSG(" force_unload - Forcefully unload a plugin\n");
493 CONMSG(" game - Information about GameDLL\n");
494 CONMSG(" info - Information about a plugin\n");
495 CONMSG(" list - List plugins\n");
496 CONMSG(" load - Load a plugin\n");
497 CONMSG(" pause - Pause a running plugin\n");
498 CONMSG(" refresh - Reparse plugins file\n");
499 CONMSG(" retry - Attempt to reload a plugin\n");
500 CONMSG(" unload - Unload a loaded plugin\n");
501 CONMSG(" unpause - Unpause a paused plugin\n");
502 CONMSG(" version - Version information\n");
505 CAlwaysRegisterableCommand::CAlwaysRegisterableCommand()
507 Create("", NULL, FCVAR_UNREGISTERED|FCVAR_GAMEDLL);
511 bool CAlwaysRegisterableCommand::IsRegistered( void ) const
516 void CAlwaysRegisterableCommand::BringToFront()
519 m_pICvar = g_Engine.icvar;
521 // First, let's try to find us!
522 ConCommandBase *pPtr = m_pICvar->GetCommands();
526 // We are already at the beginning; Nothing to do
532 if (pPtr == this && pPtr->IsCommand() && stricmp(GetName(), pPtr->GetName()) == 0)
534 ConCommandBase *pPrev = NULL;
540 pPtr = const_cast<ConCommandBase*>(pPtr->GetNext());
542 if (pPrev && pPtr == this)
544 pPrev->SetNext(m_pNext); // Remove us from the list
548 m_pICvar->RegisterConCommandBase(this);
552 const char *GetPluginsFile()
554 return mm_pluginsfile.GetString();