core/sm_srvcmds.cpp
author Borja Ferrer <faluco@alliedmods.net>
Sat Jan 20 02:12:53 2007 +0000 (2007-01-20)
changeset 333 b668cbd49c35
parent 324 e23aca731e37
child 370 d3a484f98f6d
permissions -rw-r--r--
plugins get updated on map change
maponly plugins get unloaded on map change
     1 #include "sm_srvcmds.h"
     2 #include "sm_version.h"
     3 #include "sm_stringutil.h"
     4 
     5 RootConsoleMenu g_RootMenu;
     6 
     7 RootConsoleMenu::RootConsoleMenu()
     8 {
     9 	m_pCommands = sm_trie_create();
    10 }
    11 
    12 RootConsoleMenu::~RootConsoleMenu()
    13 {
    14 	sm_trie_destroy(m_pCommands);
    15 
    16 	List<ConsoleEntry *>::iterator iter;
    17 	for (iter=m_Menu.begin(); iter!=m_Menu.end(); iter++)
    18 	{
    19 		delete (*iter);
    20 	}
    21 	m_Menu.clear();
    22 }
    23 
    24 void RootConsoleMenu::OnSourceModStartup(bool late)
    25 {
    26 	ConCommandBaseMgr::OneTimeInit(this);
    27 	AddRootConsoleCommand("version", "Display version information", this);
    28 	AddRootConsoleCommand("credits", "Display credits listing", this);
    29 }
    30 
    31 void RootConsoleMenu::OnSourceModShutdown()
    32 {
    33 	RemoveRootConsoleCommand("credits", this);
    34 	RemoveRootConsoleCommand("version", this);
    35 }
    36 
    37 bool RootConsoleMenu::RegisterConCommandBase(ConCommandBase *pCommand)
    38 {
    39 	META_REGCVAR(pCommand);
    40 
    41 	return true;
    42 }
    43 
    44 void RootConsoleMenu::ConsolePrint(const char *fmt, ...)
    45 {
    46 	char buffer[512];
    47 
    48 	va_list ap;
    49 	va_start(ap, fmt);
    50 	size_t len = vsnprintf(buffer, sizeof(buffer), fmt, ap);
    51 	va_end(ap);
    52 
    53 	if (len >= sizeof(buffer) - 1)
    54 	{
    55 		buffer[510] = '\n';
    56 		buffer[511] = '\0';
    57 	} else {
    58 		buffer[len++] = '\n';
    59 		buffer[len] = '\0';
    60 	}
    61 	
    62 	META_CONPRINT(buffer);
    63 }
    64 
    65 bool RootConsoleMenu::AddRootConsoleCommand(const char *cmd, const char *text, IRootConsoleCommand *pHandler)
    66 {
    67 	if (sm_trie_retrieve(m_pCommands, cmd, NULL))
    68 	{
    69 		return false;
    70 	}
    71 
    72 	sm_trie_insert(m_pCommands, cmd, pHandler);
    73 
    74 	/* Sort this into the menu */
    75 	List<ConsoleEntry *>::iterator iter = m_Menu.begin();
    76 	ConsoleEntry *pEntry;
    77 	bool inserted = false;
    78 	while (iter != m_Menu.end())
    79 	{
    80 		pEntry = (*iter);
    81 		if (strcmp(cmd, pEntry->command.c_str()) < 0)
    82 		{
    83 			ConsoleEntry *pNew = new ConsoleEntry;
    84 			pNew->command.assign(cmd);
    85 			pNew->description.assign(text);
    86 			m_Menu.insert(iter, pNew);
    87 			inserted = true;
    88 			break;
    89 		}
    90 		iter++;
    91 	}
    92 
    93 	if (!inserted)
    94 	{
    95 		ConsoleEntry *pNew = new ConsoleEntry;
    96 		pNew->command.assign(cmd);
    97 		pNew->description.assign(text);
    98 		m_Menu.push_back(pNew);
    99 	}
   100 
   101 	return true;
   102 }
   103 
   104 bool RootConsoleMenu::RemoveRootConsoleCommand(const char *cmd, IRootConsoleCommand *pHandler)
   105 {
   106 	/* Sanity tests */
   107 	IRootConsoleCommand *object;
   108 	if (sm_trie_retrieve(m_pCommands, cmd, (void **)&object))
   109 	{
   110 		if (object != pHandler)
   111 		{
   112 			return false;
   113 		}
   114 	} else {
   115 		return false;
   116 	}
   117 
   118 	sm_trie_delete(m_pCommands, cmd);
   119 
   120 	List<ConsoleEntry *>::iterator iter;
   121 	ConsoleEntry *pEntry;
   122 	for (iter=m_Menu.begin(); iter!=m_Menu.end(); iter++)
   123 	{
   124 		pEntry = (*iter);
   125 		if (pEntry->command.compare(cmd) == 0)
   126 		{
   127 			delete pEntry;
   128 			m_Menu.erase(iter);
   129 			break;
   130 		}
   131 	}
   132 
   133 	return true;
   134 }
   135 
   136 void RootConsoleMenu::DrawGenericOption(const char *cmd, const char *text)
   137 {
   138 	char buffer[255];
   139 	size_t len, cmdlen = strlen(cmd);
   140 
   141 	len = snprintf(buffer, sizeof(buffer), "    %s", cmd);
   142 	if (cmdlen < 16)
   143 	{
   144 		size_t num = 16 - cmdlen;
   145 		for (size_t i = 0; i < num; i++)
   146 		{
   147 			buffer[len++] = ' ';
   148 		}
   149 		len += snprintf(&buffer[len], sizeof(buffer) - len, " - %s", text);
   150 		ConsolePrint("%s", buffer);
   151 	}
   152 }
   153 
   154 void RootConsoleMenu::GotRootCmd()
   155 {
   156 	unsigned int argnum = GetArgumentCount();
   157 
   158 	if (argnum >= 2)
   159 	{
   160 		const char *cmd = GetArgument(1);
   161 		IRootConsoleCommand *pHandler;
   162 		if (sm_trie_retrieve(m_pCommands, cmd, (void **)&pHandler))
   163 		{
   164 			pHandler->OnRootConsoleCommand(cmd, argnum);
   165 			return;
   166 		}
   167 	}
   168 
   169 	ConsolePrint("SourceMod Menu:");
   170 	ConsolePrint("Usage: sm <command> [arguments]");
   171 
   172 	List<ConsoleEntry *>::iterator iter;
   173 	ConsoleEntry *pEntry;
   174 	for (iter=m_Menu.begin(); iter!=m_Menu.end(); iter++)
   175 	{
   176 		pEntry = (*iter);
   177 		DrawGenericOption(pEntry->command.c_str(), pEntry->description.c_str());
   178 	}
   179 }
   180 
   181 const char *RootConsoleMenu::GetArgument(unsigned int argno)
   182 {
   183 	return engine->Cmd_Argv(argno);
   184 }
   185 
   186 const char *RootConsoleMenu::GetArguments()
   187 {
   188 	return engine->Cmd_Args();
   189 }
   190 
   191 unsigned int RootConsoleMenu::GetArgumentCount()
   192 {
   193 	return engine->Cmd_Argc();
   194 }
   195 
   196 void RootConsoleMenu::OnRootConsoleCommand(const char *cmd, unsigned int argcount)
   197 {
   198 	if (strcmp(cmd, "credits") == 0)
   199 	{
   200 		ConsolePrint(" SourceMod was developed by AlliedModders, LLC.");
   201 		ConsolePrint(" Development would not have been possible without the following people:");
   202 		ConsolePrint("  David \"BAILOPAN\" Anderson, lead developer");
   203 		ConsolePrint("  Borja \"faluco\" Ferrer, Core developer");
   204 		ConsolePrint("  Scott \"Damaged Soul\" Ehlert, SourceMM developer");
   205 		ConsolePrint("  Pavol \"PM OnoTo\" Marko, SourceHook developer");
   206 		ConsolePrint(" Special thanks to Viper of GameConnect");
   207 		ConsolePrint(" Special thanks to Mani of Mani-Admin-Plugin");
   208 		ConsolePrint(" http://www.sourcemod.net/");
   209 	} else if (strcmp(cmd, "version") == 0) {
   210 		ConsolePrint(" SourceMod Version Information:");
   211 		ConsolePrint("    SourceMod Version: %s", SOURCEMOD_VERSION);
   212 		ConsolePrint("    JIT Version: %s, %s", g_pVM->GetVMName(), g_pVM->GetVersionString());
   213 		ConsolePrint("    JIT Settings: %s", g_pVM->GetCPUOptimizations());
   214 		ConsolePrint("    http://www.sourcemod.net/");
   215 	}
   216 }
   217 
   218 CON_COMMAND(sm, "SourceMod Menu")
   219 {
   220 	g_RootMenu.GotRootCmd();
   221 }