3 * =============================================================================
5 * Copyright (C) 2004-2009 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 <sourcemod_version.h>
34 #include "sm_stringutil.h"
35 #include "HandleSys.h"
36 #include "CoreConfig.h"
37 #include "ConVarManager.h"
40 RootConsoleMenu g_RootMenu;
42 ConVar sourcemod_version("sourcemod_version", SM_VERSION_STRING, FCVAR_SPONLY|FCVAR_REPLICATED|FCVAR_NOTIFY, "SourceMod Version");
44 RootConsoleMenu::RootConsoleMenu()
46 m_pCommands = sm_trie_create();
47 m_CfgExecDone = false;
50 RootConsoleMenu::~RootConsoleMenu()
52 sm_trie_destroy(m_pCommands);
54 List<ConsoleEntry *>::iterator iter;
55 for (iter=m_Menu.begin(); iter!=m_Menu.end(); iter++)
62 void RootConsoleMenu::OnSourceModStartup(bool late)
64 #if SOURCE_ENGINE >= SE_ORANGEBOX
67 CONVAR_REGISTER(this);
68 AddRootConsoleCommand("version", "Display version information", this);
69 AddRootConsoleCommand("credits", "Display credits listing", this);
72 void RootConsoleMenu::OnSourceModAllInitialized()
74 g_ShareSys.AddInterface(NULL, this);
77 void RootConsoleMenu::OnSourceModShutdown()
79 RemoveRootConsoleCommand("credits", this);
80 RemoveRootConsoleCommand("version", this);
83 bool RootConsoleMenu::RegisterConCommandBase(ConCommandBase *pCommand)
85 META_REGCVAR(pCommand);
87 /* Override values of convars created by SourceMod convar manager if specified on command line */
88 const char *cmdLineValue = icvar->GetCommandLineValue(pCommand->GetName());
89 if (cmdLineValue && !pCommand->IsCommand())
91 static_cast<ConVar *>(pCommand)->SetValue(cmdLineValue);
97 void RootConsoleMenu::ConsolePrint(const char *fmt, ...)
103 size_t len = vsnprintf(buffer, sizeof(buffer), fmt, ap);
106 if (len >= sizeof(buffer) - 1)
111 buffer[len++] = '\n';
115 META_CONPRINT(buffer);
118 bool RootConsoleMenu::AddRootConsoleCommand(const char *cmd, const char *text, IRootConsoleCommand *pHandler)
120 return _AddRootConsoleCommand(cmd, text, pHandler, false);
123 bool RootConsoleMenu::AddRootConsoleCommand2(const char *cmd, const char *text, IRootConsoleCommand *pHandler)
125 return _AddRootConsoleCommand(cmd, text, pHandler, true);
128 bool RootConsoleMenu::_AddRootConsoleCommand(const char *cmd,
130 IRootConsoleCommand *pHandler,
133 if (sm_trie_retrieve(m_pCommands, cmd, NULL))
138 /* Sort this into the menu */
139 List<ConsoleEntry *>::iterator iter = m_Menu.begin();
140 ConsoleEntry *pEntry;
141 bool inserted = false;
142 while (iter != m_Menu.end())
145 if (strcmp(cmd, pEntry->command.c_str()) < 0)
147 ConsoleEntry *pNew = new ConsoleEntry;
148 pNew->command.assign(cmd);
149 pNew->description.assign(text);
150 pNew->version2 = version2;
151 pNew->cmd = pHandler;
152 sm_trie_insert(m_pCommands, cmd, pNew);
153 m_Menu.insert(iter, pNew);
162 ConsoleEntry *pNew = new ConsoleEntry;
163 pNew->command.assign(cmd);
164 pNew->description.assign(text);
165 pNew->version2 = version2;
166 pNew->cmd = pHandler;
167 sm_trie_insert(m_pCommands, cmd, pNew);
168 m_Menu.push_back(pNew);
174 bool RootConsoleMenu::RemoveRootConsoleCommand(const char *cmd, IRootConsoleCommand *pHandler)
176 sm_trie_delete(m_pCommands, cmd);
178 List<ConsoleEntry *>::iterator iter;
179 ConsoleEntry *pEntry;
180 for (iter=m_Menu.begin(); iter!=m_Menu.end(); iter++)
183 if (pEntry->command.compare(cmd) == 0)
194 void RootConsoleMenu::DrawGenericOption(const char *cmd, const char *text)
197 size_t len, cmdlen = strlen(cmd);
199 len = UTIL_Format(buffer, sizeof(buffer), " %s", cmd);
202 size_t num = 16 - cmdlen;
203 for (size_t i = 0; i < num; i++)
207 len += snprintf(&buffer[len], sizeof(buffer) - len, " - %s", text);
208 ConsolePrint("%s", buffer);
212 const char *RootConsoleMenu::GetInterfaceName()
214 return SMINTERFACE_ROOTCONSOLE_NAME;
217 unsigned int RootConsoleMenu::GetInterfaceVersion()
219 return SMINTERFACE_ROOTCONSOLE_VERSION;
222 #if SOURCE_ENGINE==SE_EPISODEONE || SOURCE_ENGINE==SE_DARKMESSIAH
223 class CCommandArgs : public ICommandArgs
226 CCommandArgs(const CCommand& _cmd)
229 const char *Arg(int n) const
231 return engine->Cmd_Argv(n);
235 return engine->Cmd_Argc();
237 const char *ArgS() const
239 return engine->Cmd_Args();
243 class CCommandArgs : public ICommandArgs
247 CCommandArgs(const CCommand& _cmd) : cmd(&_cmd)
250 const char *Arg(int n) const
258 const char *ArgS() const
265 void RootConsoleMenu::GotRootCmd(const CCommand &cmd)
267 unsigned int argnum = cmd.ArgC();
271 const char *cmdname = cmd.Arg(1);
272 if (strcmp(cmdname, "internal") == 0)
276 const char *arg = cmd.Arg(2);
277 if (strcmp(arg, "1") == 0)
279 SM_ConfigsExecuted_Global();
281 else if (strcmp(arg, "2") == 0)
285 SM_ConfigsExecuted_Plugin(atoi(cmd.Arg(3)));
292 CCommandArgs ocmd(cmd);
295 if (sm_trie_retrieve(m_pCommands, cmdname, (void **)&entry))
299 entry->cmd->OnRootConsoleCommand2(cmdname, &ocmd);
303 entry->cmd->OnRootConsoleCommand(cmdname, cmd);
309 ConsolePrint("SourceMod Menu:");
310 ConsolePrint("Usage: sm <command> [arguments]");
312 List<ConsoleEntry *>::iterator iter;
313 ConsoleEntry *pEntry;
314 for (iter=m_Menu.begin(); iter!=m_Menu.end(); iter++)
317 DrawGenericOption(pEntry->command.c_str(), pEntry->description.c_str());
321 void RootConsoleMenu::OnRootConsoleCommand(const char *cmdname, const CCommand &command)
323 if (strcmp(cmdname, "credits") == 0)
325 ConsolePrint(" SourceMod was developed by AlliedModders, LLC.");
326 ConsolePrint(" Development would not have been possible without the following people:");
327 ConsolePrint(" David \"BAILOPAN\" Anderson");
328 ConsolePrint(" Matt \"pRED\" Woodrow");
329 ConsolePrint(" Scott \"DS\" Ehlert");
330 ConsolePrint(" Fyren");
331 ConsolePrint(" Nicholas \"psychonic\" Hastings");
332 ConsolePrint(" Asher \"asherkin\" Baker");
333 ConsolePrint(" Borja \"faluco\" Ferrer");
334 ConsolePrint(" Pavol \"PM OnoTo\" Marko");
335 ConsolePrint(" Special thanks to Liam, ferret, and Mani");
336 ConsolePrint(" Special thanks to Viper and SteamFriends");
337 ConsolePrint(" http://www.sourcemod.net/");
339 else if (strcmp(cmdname, "version") == 0)
341 ConsolePrint(" SourceMod Version Information:");
342 ConsolePrint(" SourceMod Version: %s", SM_VERSION_STRING);
343 ConsolePrint(" SourcePawn Engine: %s (build %s)", g_pSourcePawn2->GetEngineName(), g_pSourcePawn2->GetVersionString());
344 ConsolePrint(" SourcePawn API: v1 = %d, v2 = %d", g_pSourcePawn->GetEngineAPIVersion(), g_pSourcePawn2->GetAPIVersion());
345 ConsolePrint(" Compiled on: %s %s", __DATE__, __TIME__);
346 ConsolePrint(" Build ID: %s", SM_BUILD_UNIQUEID);
347 ConsolePrint(" http://www.sourcemod.net/");
351 CON_COMMAND(sm, "SourceMod Menu")
353 #if SOURCE_ENGINE <= SE_DARKMESSIAH
356 g_RootMenu.GotRootCmd(args);
359 FILE *g_pHndlLog = NULL;
361 void write_handles_to_log(const char *fmt, ...)
366 vfprintf(g_pHndlLog, fmt, ap);
367 fprintf(g_pHndlLog, "\n");
371 void write_handles_to_game(const char *fmt, ...)
378 len = UTIL_FormatArgs(buffer, sizeof(buffer)-2, fmt, ap);
382 buffer[len+1] = '\0';
384 engine->LogPrint(buffer);
387 CON_COMMAND(sm_dump_handles, "Dumps Handle usage to a file for finding Handle leaks")
389 #if SOURCE_ENGINE <= SE_DARKMESSIAH
394 g_RootMenu.ConsolePrint("Usage: sm_dump_handles <file> or <log> for game logs");
398 if (strcmp(args.Arg(1), "log") != 0)
400 const char *arg = args.Arg(1);
401 FILE *fp = fopen(arg, "wt");
404 g_RootMenu.ConsolePrint("Could not find file \"%s\"", arg);
409 g_HandleSys.Dump(write_handles_to_log);
416 g_HandleSys.Dump(write_handles_to_game);