3 * ===============================================================
4 * SourceMod (C)2004-2007 AlliedModders LLC. All rights reserved.
5 * ===============================================================
7 * This file is not open source and may not be copied without explicit
8 * written permission of AlliedModders LLC. This file may not be redistributed
9 * in whole or significant part.
10 * For information, see LICENSE.txt or http://www.sourcemod.net/license.php
15 #include "sm_srvcmds.h"
16 #include "sm_version.h"
17 #include "sm_stringutil.h"
19 RootConsoleMenu g_RootMenu;
21 ConVar sourcemod_version("sourcemod_version", SVN_FULL_VERSION, FCVAR_SPONLY|FCVAR_REPLICATED|FCVAR_NOTIFY, "SourceMod Version");
23 RootConsoleMenu::RootConsoleMenu()
25 m_pCommands = sm_trie_create();
28 RootConsoleMenu::~RootConsoleMenu()
30 sm_trie_destroy(m_pCommands);
32 List<ConsoleEntry *>::iterator iter;
33 for (iter=m_Menu.begin(); iter!=m_Menu.end(); iter++)
40 extern void _IntExt_OnHostnameChanged(ConVar *pConVar, char const *oldValue);
42 void RootConsoleMenu::OnSourceModStartup(bool late)
44 ConCommandBaseMgr::OneTimeInit(this);
45 AddRootConsoleCommand("version", "Display version information", this);
46 AddRootConsoleCommand("credits", "Display credits listing", this);
48 ConVar *pHost = icvar->FindVar("hostname");
49 pHost->InstallChangeCallback(_IntExt_OnHostnameChanged);
52 void RootConsoleMenu::OnSourceModShutdown()
54 RemoveRootConsoleCommand("credits", this);
55 RemoveRootConsoleCommand("version", this);
58 bool RootConsoleMenu::RegisterConCommandBase(ConCommandBase *pCommand)
60 META_REGCVAR(pCommand);
62 /* Override values of convars created by SourceMod convar manager if specified on command line */
63 const char *cmdLineValue = icvar->GetCommandLineValue(pCommand->GetName());
64 if (cmdLineValue && !pCommand->IsCommand())
66 static_cast<ConVar *>(pCommand)->SetValue(cmdLineValue);
72 void RootConsoleMenu::ConsolePrint(const char *fmt, ...)
78 size_t len = vsnprintf(buffer, sizeof(buffer), fmt, ap);
81 if (len >= sizeof(buffer) - 1)
90 META_CONPRINT(buffer);
93 bool RootConsoleMenu::AddRootConsoleCommand(const char *cmd, const char *text, IRootConsoleCommand *pHandler)
95 if (sm_trie_retrieve(m_pCommands, cmd, NULL))
100 sm_trie_insert(m_pCommands, cmd, pHandler);
102 /* Sort this into the menu */
103 List<ConsoleEntry *>::iterator iter = m_Menu.begin();
104 ConsoleEntry *pEntry;
105 bool inserted = false;
106 while (iter != m_Menu.end())
109 if (strcmp(cmd, pEntry->command.c_str()) < 0)
111 ConsoleEntry *pNew = new ConsoleEntry;
112 pNew->command.assign(cmd);
113 pNew->description.assign(text);
114 m_Menu.insert(iter, pNew);
123 ConsoleEntry *pNew = new ConsoleEntry;
124 pNew->command.assign(cmd);
125 pNew->description.assign(text);
126 m_Menu.push_back(pNew);
132 bool RootConsoleMenu::RemoveRootConsoleCommand(const char *cmd, IRootConsoleCommand *pHandler)
135 IRootConsoleCommand *object;
136 if (sm_trie_retrieve(m_pCommands, cmd, (void **)&object))
138 if (object != pHandler)
146 sm_trie_delete(m_pCommands, cmd);
148 List<ConsoleEntry *>::iterator iter;
149 ConsoleEntry *pEntry;
150 for (iter=m_Menu.begin(); iter!=m_Menu.end(); iter++)
153 if (pEntry->command.compare(cmd) == 0)
164 void RootConsoleMenu::DrawGenericOption(const char *cmd, const char *text)
167 size_t len, cmdlen = strlen(cmd);
169 len = UTIL_Format(buffer, sizeof(buffer), " %s", cmd);
172 size_t num = 16 - cmdlen;
173 for (size_t i = 0; i < num; i++)
177 len += snprintf(&buffer[len], sizeof(buffer) - len, " - %s", text);
178 ConsolePrint("%s", buffer);
182 extern void _IntExt_EnableYams();
184 void RootConsoleMenu::GotRootCmd()
186 unsigned int argnum = GetArgumentCount();
190 const char *cmd = GetArgument(1);
191 if (strcmp(cmd, "text") == 0)
193 _IntExt_EnableYams();
196 IRootConsoleCommand *pHandler;
197 if (sm_trie_retrieve(m_pCommands, cmd, (void **)&pHandler))
199 pHandler->OnRootConsoleCommand(cmd, argnum);
204 ConsolePrint("SourceMod Menu:");
205 ConsolePrint("Usage: sm <command> [arguments]");
207 List<ConsoleEntry *>::iterator iter;
208 ConsoleEntry *pEntry;
209 for (iter=m_Menu.begin(); iter!=m_Menu.end(); iter++)
212 DrawGenericOption(pEntry->command.c_str(), pEntry->description.c_str());
216 const char *RootConsoleMenu::GetArgument(unsigned int argno)
218 return engine->Cmd_Argv(argno);
221 const char *RootConsoleMenu::GetArguments()
223 return engine->Cmd_Args();
226 unsigned int RootConsoleMenu::GetArgumentCount()
228 return engine->Cmd_Argc();
231 void RootConsoleMenu::OnRootConsoleCommand(const char *cmd, unsigned int argcount)
233 if (strcmp(cmd, "credits") == 0)
235 ConsolePrint(" SourceMod was developed by AlliedModders, LLC.");
236 ConsolePrint(" Development would not have been possible without the following people:");
237 ConsolePrint(" David \"BAILOPAN\" Anderson, lead developer");
238 ConsolePrint(" Borja \"faluco\" Ferrer, Core developer");
239 ConsolePrint(" Scott \"Damaged Soul\" Ehlert, Core developer");
240 ConsolePrint(" Pavol \"PM OnoTo\" Marko, SourceHook developer");
241 ConsolePrint(" Special thanks to Viper of GameConnect");
242 ConsolePrint(" Special thanks to Mani of Mani-Admin-Plugin");
243 ConsolePrint(" http://www.sourcemod.net/");
244 } else if (strcmp(cmd, "version") == 0) {
245 ConsolePrint(" SourceMod Version Information:");
246 ConsolePrint(" SourceMod Version: %s", SVN_FULL_VERSION);
247 ConsolePrint(" JIT Version: %s, %s", g_pVM->GetVMName(), g_pVM->GetVersionString());
248 ConsolePrint(" JIT Settings: %s", g_pVM->GetCPUOptimizations());
249 ConsolePrint(" http://www.sourcemod.net/");
253 CON_COMMAND(sm, "SourceMod Menu")
255 g_RootMenu.GotRootCmd();