3 * =============================================================================
5 * Copyright (C) 2004-2007 AlliedModders LLC. All rights reserved.
6 * =============================================================================
8 * This program is free software; you can redistribute it and/or modify it under
9 * the terms of the GNU General Public License, version 3.0, as published by the
10 * Free Software Foundation.
12 * This program is distributed in the hope that it will be useful, but WITHOUT
13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
17 * You should have received a copy of the GNU General Public License along with
18 * this program. If not, see <http://www.gnu.org/licenses/>.
20 * As a special exception, AlliedModders LLC gives you permission to link the
21 * code of this program (as well as its derivative works) to "Half-Life 2," the
22 * "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
23 * by the Valve Corporation. You must obey the GNU General Public License in
24 * all respects for all other code used. Additionally, AlliedModders LLC grants
25 * this exception to all derivative works. AlliedModders LLC defines further
26 * exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
27 * or <http://www.sourcemod.net/license.php>.
32 #include "sm_srvcmds.h"
33 #include "sm_version.h"
34 #include "sm_stringutil.h"
35 #include "HandleSys.h"
36 #include "CoreConfig.h"
38 RootConsoleMenu g_RootMenu;
40 ConVar sourcemod_version("sourcemod_version", SVN_FULL_VERSION, FCVAR_SPONLY|FCVAR_REPLICATED|FCVAR_NOTIFY, "SourceMod Version");
42 RootConsoleMenu::RootConsoleMenu()
44 m_pCommands = sm_trie_create();
45 m_CfgExecDone = false;
48 RootConsoleMenu::~RootConsoleMenu()
50 sm_trie_destroy(m_pCommands);
52 List<ConsoleEntry *>::iterator iter;
53 for (iter=m_Menu.begin(); iter!=m_Menu.end(); iter++)
60 #if defined ORANGEBOX_BUILD
61 extern void _IntExt_OnHostnameChanged(IConVar *var, const char *pOldValue, float flOldValue);
63 extern void _IntExt_OnHostnameChanged(ConVar *pConVar, char const *oldValue);
66 void RootConsoleMenu::OnSourceModStartup(bool late)
68 #if defined ORANGEBOX_BUILD
71 CONVAR_REGISTER(this);
72 AddRootConsoleCommand("version", "Display version information", this);
73 AddRootConsoleCommand("credits", "Display credits listing", this);
75 ConVar *pHost = icvar->FindVar("hostname");
76 pHost->InstallChangeCallback(_IntExt_OnHostnameChanged);
79 void RootConsoleMenu::OnSourceModShutdown()
81 RemoveRootConsoleCommand("credits", this);
82 RemoveRootConsoleCommand("version", this);
85 bool RootConsoleMenu::RegisterConCommandBase(ConCommandBase *pCommand)
87 META_REGCVAR(pCommand);
89 /* Override values of convars created by SourceMod convar manager if specified on command line */
90 const char *cmdLineValue = icvar->GetCommandLineValue(pCommand->GetName());
91 if (cmdLineValue && !pCommand->IsCommand())
93 static_cast<ConVar *>(pCommand)->SetValue(cmdLineValue);
99 void RootConsoleMenu::ConsolePrint(const char *fmt, ...)
105 size_t len = vsnprintf(buffer, sizeof(buffer), fmt, ap);
108 if (len >= sizeof(buffer) - 1)
113 buffer[len++] = '\n';
117 META_CONPRINT(buffer);
120 bool RootConsoleMenu::AddRootConsoleCommand(const char *cmd, const char *text, IRootConsoleCommand *pHandler)
122 if (sm_trie_retrieve(m_pCommands, cmd, NULL))
127 sm_trie_insert(m_pCommands, cmd, pHandler);
129 /* Sort this into the menu */
130 List<ConsoleEntry *>::iterator iter = m_Menu.begin();
131 ConsoleEntry *pEntry;
132 bool inserted = false;
133 while (iter != m_Menu.end())
136 if (strcmp(cmd, pEntry->command.c_str()) < 0)
138 ConsoleEntry *pNew = new ConsoleEntry;
139 pNew->command.assign(cmd);
140 pNew->description.assign(text);
141 m_Menu.insert(iter, pNew);
150 ConsoleEntry *pNew = new ConsoleEntry;
151 pNew->command.assign(cmd);
152 pNew->description.assign(text);
153 m_Menu.push_back(pNew);
159 bool RootConsoleMenu::RemoveRootConsoleCommand(const char *cmd, IRootConsoleCommand *pHandler)
162 IRootConsoleCommand *object;
163 if (sm_trie_retrieve(m_pCommands, cmd, (void **)&object))
165 if (object != pHandler)
173 sm_trie_delete(m_pCommands, cmd);
175 List<ConsoleEntry *>::iterator iter;
176 ConsoleEntry *pEntry;
177 for (iter=m_Menu.begin(); iter!=m_Menu.end(); iter++)
180 if (pEntry->command.compare(cmd) == 0)
191 void RootConsoleMenu::DrawGenericOption(const char *cmd, const char *text)
194 size_t len, cmdlen = strlen(cmd);
196 len = UTIL_Format(buffer, sizeof(buffer), " %s", cmd);
199 size_t num = 16 - cmdlen;
200 for (size_t i = 0; i < num; i++)
204 len += snprintf(&buffer[len], sizeof(buffer) - len, " - %s", text);
205 ConsolePrint("%s", buffer);
209 extern void _IntExt_EnableYams();
211 void RootConsoleMenu::GotRootCmd(const CCommand &cmd)
213 unsigned int argnum = cmd.ArgC();
217 const char *cmdname = cmd.Arg(1);
218 if (strcmp(cmdname, "text") == 0)
220 _IntExt_EnableYams();
223 else if (strcmp(cmdname, "internal") == 0)
227 const char *arg = cmd.Arg(2);
228 if (strcmp(arg, "1") == 0)
230 SM_ConfigsExecuted_Global();
232 else if (strcmp(arg, "2") == 0)
236 SM_ConfigsExecuted_Plugin(atoi(cmd.Arg(3)));
243 IRootConsoleCommand *pHandler;
244 if (sm_trie_retrieve(m_pCommands, cmdname, (void **)&pHandler))
246 pHandler->OnRootConsoleCommand(cmdname, cmd);
251 ConsolePrint("SourceMod Menu:");
252 ConsolePrint("Usage: sm <command> [arguments]");
254 List<ConsoleEntry *>::iterator iter;
255 ConsoleEntry *pEntry;
256 for (iter=m_Menu.begin(); iter!=m_Menu.end(); iter++)
259 DrawGenericOption(pEntry->command.c_str(), pEntry->description.c_str());
263 void RootConsoleMenu::OnRootConsoleCommand(const char *cmdname, const CCommand &command)
265 if (strcmp(cmdname, "credits") == 0)
267 ConsolePrint(" SourceMod was developed by AlliedModders, LLC.");
268 ConsolePrint(" Development would not have been possible without the following people:");
269 ConsolePrint(" David \"BAILOPAN\" Anderson, lead developer");
270 ConsolePrint(" Borja \"faluco\" Ferrer, Core developer");
271 ConsolePrint(" Scott \"Damaged Soul\" Ehlert, Core developer");
272 ConsolePrint(" Pavol \"PM OnoTo\" Marko, SourceHook developer");
273 ConsolePrint(" Special thanks to Viper of GameConnect");
274 ConsolePrint(" Special thanks to Mani of Mani-Admin-Plugin");
275 ConsolePrint(" http://www.sourcemod.net/");
276 } else if (strcmp(cmdname, "version") == 0) {
277 ConsolePrint(" SourceMod Version Information:");
278 ConsolePrint(" SourceMod Version: %s", SVN_FULL_VERSION);
279 ConsolePrint(" JIT Version: %s, %s", g_pVM->GetVMName(), g_pVM->GetVersionString());
280 ConsolePrint(" JIT Settings: %s", g_pVM->GetCPUOptimizations());
281 ConsolePrint(" http://www.sourcemod.net/");
285 CON_COMMAND(sm, "SourceMod Menu")
287 #if !defined ORANGEBOX_BUILD
290 g_RootMenu.GotRootCmd(args);
293 FILE *g_pHndlLog = NULL;
295 void write_handles_to_log(const char *fmt, ...)
300 vfprintf(g_pHndlLog, fmt, ap);
301 fprintf(g_pHndlLog, "\n");
305 void write_handles_to_game(const char *fmt, ...)
312 len = UTIL_FormatArgs(buffer, sizeof(buffer)-2, fmt, ap);
316 buffer[len+1] = '\0';
318 engine->LogPrint(buffer);
321 CON_COMMAND(sm_dump_handles, "Dumps Handle usage to a file for finding Handle leaks")
323 #if !defined ORANGEBOX_BUILD
328 g_RootMenu.ConsolePrint("Usage: sm_dump_handles <file> or <log> for game logs");
332 if (strcmp(args.Arg(1), "log") != 0)
334 const char *arg = args.Arg(1);
335 FILE *fp = fopen(arg, "wt");
338 g_RootMenu.ConsolePrint("Could not find file \"%s\"", arg);
343 g_HandleSys.Dump(write_handles_to_log);
350 g_HandleSys.Dump(write_handles_to_game);