core/ConVarManager.cpp
author Scott Ehlert <ds@alliedmods.net>
Mon Jul 23 19:34:26 2007 +0000 (2007-07-23)
changeset 1148 c523115e1124
parent 1146 ee44bbf5b70f
child 1149 70d171906c7e
permissions -rw-r--r--
Fixed another issue with crashing on exit in relation to convars
     1 /**
     2  * vim: set ts=4 :
     3  * ===============================================================
     4  * SourceMod (C)2004-2007 AlliedModders LLC.  All rights reserved.
     5  * ===============================================================
     6  *
     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
    11  *
    12  * Version: $Id$
    13  */
    14 
    15 #include "ConVarManager.h"
    16 #include "HalfLife2.h"
    17 #include "PluginSys.h"
    18 #include "ForwardSys.h"
    19 #include "HandleSys.h"
    20 #include "sm_srvcmds.h"
    21 #include "sm_stringutil.h"
    22 #include <sh_vector.h>
    23 
    24 ConVarManager g_ConVarManager;
    25 
    26 SH_DECL_HOOK5_void(IServerGameDLL, OnQueryCvarValueFinished, SH_NOATTRIB, 0, QueryCvarCookie_t, edict_t *, EQueryCvarValueStatus, const char *, const char *);
    27 SH_DECL_HOOK5_void(IServerPluginCallbacks, OnQueryCvarValueFinished, SH_NOATTRIB, 0, QueryCvarCookie_t, edict_t *, EQueryCvarValueStatus, const char *, const char *);
    28 
    29 const ParamType CONVARCHANGE_PARAMS[] = {Param_Cell, Param_String, Param_String};
    30 typedef List<const ConVar *> ConVarList;
    31 
    32 ConVarManager::ConVarManager() : m_ConVarType(0), m_VSPIface(NULL), m_CanQueryConVars(false)
    33 {
    34 #if PLAPI_VERSION < 12
    35 	m_IgnoreHandle = false;
    36 #endif
    37 
    38 	/* Create a convar lookup trie */
    39 	m_ConVarCache = sm_trie_create();
    40 }
    41 
    42 ConVarManager::~ConVarManager()
    43 {
    44 	List<ConVarInfo *>::iterator iter;
    45 
    46 	/* Destroy our convar lookup trie */
    47 	sm_trie_destroy(m_ConVarCache);
    48 
    49 	/* Destroy all the ConVarInfo structures */
    50 	for (iter = m_ConVars.begin(); iter != m_ConVars.end(); iter++)
    51 	{
    52 		delete (*iter);
    53 	}
    54 
    55 	m_ConVars.clear();
    56 }
    57 
    58 void ConVarManager::OnSourceModAllInitialized()
    59 {
    60 	/* Only valid with ServerGameDLL006 or greater */
    61 	if (g_SMAPI->GetGameDLLVersion() >= 6)
    62 	{
    63 		SH_ADD_HOOK_MEMFUNC(IServerGameDLL, OnQueryCvarValueFinished, gamedll, this, &ConVarManager::OnQueryCvarValueFinished, false);
    64 		m_CanQueryConVars = true;
    65 	}
    66 
    67 	HandleAccess sec;
    68 
    69 	/* Set up access rights for the 'ConVar' handle type */
    70 	sec.access[HandleAccess_Read] = 0;
    71 	sec.access[HandleAccess_Delete] = HANDLE_RESTRICT_IDENTITY | HANDLE_RESTRICT_OWNER;
    72 	sec.access[HandleAccess_Clone] = HANDLE_RESTRICT_IDENTITY | HANDLE_RESTRICT_OWNER;
    73 
    74 	/* Create the 'ConVar' handle type */
    75 	m_ConVarType = g_HandleSys.CreateType("ConVar", this, 0, NULL, &sec, g_pCoreIdent, NULL);
    76 
    77 	/* Add the 'convars' option to the 'sm' console command */
    78 	g_RootMenu.AddRootConsoleCommand("cvars", "View convars created by a plugin", this);
    79 }
    80 
    81 void ConVarManager::OnSourceModShutdown()
    82 {
    83 	if (m_CanQueryConVars)
    84 	{
    85 		SH_REMOVE_HOOK_MEMFUNC(IServerGameDLL, OnQueryCvarValueFinished, gamedll, this, &ConVarManager::OnQueryCvarValueFinished, false);
    86 		SH_REMOVE_HOOK_MEMFUNC(IServerPluginCallbacks, OnQueryCvarValueFinished, m_VSPIface, this, &ConVarManager::OnQueryCvarValueFinished, false);
    87 	}
    88 
    89 	IChangeableForward *fwd;
    90 	List<ConVarInfo *>::iterator i;
    91 
    92 	/* Iterate list of ConVarInfo structures */
    93 	for (i = m_ConVars.begin(); i != m_ConVars.end(); i++)
    94 	{
    95 		fwd = (*i)->pChangeForward;
    96 
    97 		/* Free any convar-change forwards that still exist */
    98 		if (fwd)
    99 		{
   100 			g_Forwards.ReleaseForward(fwd);
   101 		}
   102 	}
   103 
   104 	/* Remove the 'convars' option from the 'sm' console command */
   105 	g_RootMenu.RemoveRootConsoleCommand("cvars", this);
   106 
   107 	/* Remove the 'ConVar' handle type */
   108 	g_HandleSys.RemoveType(m_ConVarType, g_pCoreIdent);
   109 }
   110 
   111 void ConVarManager::OnSourceModVSPReceived(IServerPluginCallbacks *iface)
   112 {
   113 	/* This will be called after OnSourceModAllInitialized(), so...
   114 	 *
   115 	 * If we haven't been able to hook the IServerGameDLL version at this point,
   116 	 * then use hook IServerPluginCallbacks version from the engine.
   117 	 */
   118 
   119 	/* The Ship currently only supports ServerPluginCallbacks001, but we need 002 */
   120 	if (g_IsOriginalEngine)
   121 	{
   122 		return;
   123 	}
   124 
   125 	if (!m_CanQueryConVars)
   126 	{
   127 		m_VSPIface = iface;
   128 
   129 		SH_ADD_HOOK_MEMFUNC(IServerPluginCallbacks, OnQueryCvarValueFinished, iface, this, &ConVarManager::OnQueryCvarValueFinished, false);
   130 		m_CanQueryConVars = true;
   131 	}
   132 }
   133 
   134 #if PLAPI_VERSION >= 12
   135 void ConVarManager::OnUnlinkConCommandBase(PluginId id, ConCommandBase *pCommand)
   136 {
   137 	/* Only check convars that have not been created by SourceMod's core */
   138 	if (id != g_PLID && !pCommand->IsCommand())
   139 	{
   140 		ConVarInfo *pInfo;
   141 		const char *cvarName = pCommand->GetName();
   142 		HandleSecurity sec(NULL, g_pCoreIdent);
   143 		bool handleExists = sm_trie_retrieve(m_ConVarCache, cvarName, reinterpret_cast<void **>(&pInfo));
   144 
   145 		if (handleExists)
   146 		{
   147 			g_HandleSys.FreeHandle(pInfo->handle, &sec);
   148 			sm_trie_delete(m_ConVarCache, cvarName);
   149 			m_ConVars.remove(pInfo);
   150 		}
   151 	}
   152 }
   153 
   154 #else
   155 
   156 /* I truly detest this code */
   157 void ConVarManager::OnMetamodPluginUnloaded(PluginId id)
   158 {
   159 	ConVarInfo *pInfo;
   160 	const char *cvarName;
   161 	HandleSecurity sec(NULL, g_pCoreIdent);
   162 
   163 	List<ConVarInfo *>::iterator i;
   164 	for (i = m_ConVars.begin(); i != m_ConVars.end(); i++)
   165 	{
   166 		pInfo = (*i);
   167 		cvarName = pInfo->name;
   168 
   169 		if (cvarName && !icvar->FindVar(cvarName))
   170 		{
   171 			m_IgnoreHandle = true;
   172 			g_HandleSys.FreeHandle(pInfo->handle, &sec);
   173 
   174 			sm_trie_delete(m_ConVarCache, cvarName);
   175 			m_ConVars.erase(i);
   176 
   177 			delete [] cvarName;
   178 		}
   179 	}
   180 }
   181 #endif
   182 
   183 void ConVarManager::OnPluginUnloaded(IPlugin *plugin)
   184 {
   185 	ConVarList *pConVarList;
   186 
   187 	/* If plugin has a convar list, free its memory */
   188 	if (plugin->GetProperty("ConVarList", (void **)&pConVarList, true))
   189 	{
   190 		delete pConVarList;
   191 	}
   192 }
   193 
   194 void ConVarManager::OnHandleDestroy(HandleType_t type, void *object)
   195 {
   196 #if PLAPI_VERSION < 12
   197 	/* Lovely workaround for our workaround! */
   198 	if (m_IgnoreHandle)
   199 	{
   200 		m_IgnoreHandle = false;
   201 		return;
   202 	}
   203 #endif
   204 
   205 	ConVarInfo *info;
   206 	ConVar *pConVar = static_cast<ConVar *>(object);
   207 
   208 	/* Find convar in lookup trie */
   209 	sm_trie_retrieve(m_ConVarCache, pConVar->GetName(), reinterpret_cast<void **>(&info));
   210 
   211 	/* If convar was created by SourceMod plugin... */
   212 	if (info->sourceMod)
   213 	{
   214 		/* Delete string allocations */
   215 		delete [] pConVar->GetName(); 
   216 		delete [] pConVar->GetDefault();
   217 		delete [] pConVar->GetHelpText();
   218 
   219 		/* Then unlink it from SourceMM */
   220 		g_SMAPI->UnregisterConCmdBase(g_PLAPI, pConVar);
   221 	}
   222 }
   223 
   224 void ConVarManager::OnRootConsoleCommand(const char *command, unsigned int argcount)
   225 {
   226 	if (argcount >= 3)
   227 	{
   228 		/* Get plugin index that was passed */
   229 		int id = atoi(g_RootMenu.GetArgument(2));
   230 		
   231 		/* Get plugin object */
   232 		CPlugin *plugin = g_PluginSys.GetPluginByOrder(id);
   233 
   234 		if (!plugin)
   235 		{
   236 			g_RootMenu.ConsolePrint("[SM] Plugin index %d not found.", id);
   237 			return;
   238 		}
   239 
   240 		/* Get plugin name */
   241 		const sm_plugininfo_t *plinfo = plugin->GetPublicInfo();
   242 		const char *plname = IS_STR_FILLED(plinfo->name) ? plinfo->name : plugin->GetFilename();
   243 
   244 		ConVarList *pConVarList;
   245 		ConVarList::iterator iter;
   246 
   247 		/* If no convar list... */
   248 		if (!plugin->GetProperty("ConVarList", (void **)&pConVarList))
   249 		{
   250 			g_RootMenu.ConsolePrint("[SM] No convars found for: %s", plname);
   251 			return;
   252 		}
   253 
   254 		g_RootMenu.ConsolePrint("[SM] Listing %d convars for: %s", pConVarList->size(), plname);
   255 		g_RootMenu.ConsolePrint("  %-32.31s %s", "[Name]", "[Value]");
   256 
   257 		/* Iterate convar list and display each one */
   258 		for (iter = pConVarList->begin(); iter != pConVarList->end(); iter++)
   259 		{
   260 			const ConVar *pConVar = (*iter);
   261 			g_RootMenu.ConsolePrint("  %-32.31s %s", pConVar->GetName(), pConVar->GetString()); 
   262 		}
   263 
   264 		return;
   265 	}
   266 
   267 	/* Display usage of subcommand */
   268 	g_RootMenu.ConsolePrint("[SM] Usage: sm convars <plugin #>");
   269 }
   270 
   271 Handle_t ConVarManager::CreateConVar(IPluginContext *pContext, const char *name, const char *defaultVal, const char *description, int flags, bool hasMin, float min, bool hasMax, float max)
   272 {
   273 	ConVar *pConVar = NULL;
   274 	ConVarInfo *pInfo = NULL;
   275 	Handle_t hndl = 0;
   276 
   277 	/* Find out if the convar exists already */
   278 	pConVar = icvar->FindVar(name);
   279 
   280 	/* If the convar already exists... */
   281 	if (pConVar)
   282 	{
   283 		/* Add convar to plugin's list */
   284 		AddConVarToPluginList(pContext, pConVar);
   285 
   286 		/* First find out if we already have a handle to it */
   287 		if (sm_trie_retrieve(m_ConVarCache, name, (void **)&pInfo))
   288 		{
   289 			return pInfo->handle;
   290 		} else {
   291 			/* If we don't, then create a new handle from the convar */
   292 			hndl = g_HandleSys.CreateHandle(m_ConVarType, pConVar, NULL, g_pCoreIdent, NULL);
   293 
   294 			/* Create and initialize ConVarInfo structure */
   295 			pInfo = new ConVarInfo();
   296 			pInfo->handle = hndl;
   297 			pInfo->sourceMod = false;
   298 			pInfo->pChangeForward = NULL;
   299 			pInfo->origCallback = pConVar->GetCallback();
   300 		#if PLAPI_VERSION < 12
   301 			pInfo->name = sm_strdup(pConVar->GetName());
   302 		#endif
   303 
   304 			/* Insert struct into caches */
   305 			m_ConVars.push_back(pInfo);
   306 			sm_trie_insert(m_ConVarCache, name, pInfo);
   307 
   308 			return hndl;
   309 		}
   310 	}
   311 
   312 	/* To prevent creating a convar that has the same name as a console command... ugh */
   313 	ConCommandBase *pBase = icvar->GetCommands();
   314 
   315 	while (pBase)
   316 	{
   317 		if (pBase->IsCommand() && strcmp(pBase->GetName(), name) == 0)
   318 		{
   319 			return BAD_HANDLE;
   320 		}
   321 
   322 		pBase = const_cast<ConCommandBase *>(pBase->GetNext());
   323 	}
   324 
   325 	/* Since an existing convar (or concmd with the same name) was not found , now we can finally create it */
   326 	pConVar = new ConVar(sm_strdup(name), sm_strdup(defaultVal), flags, sm_strdup(description), hasMin, min, hasMax, max);
   327 
   328 	/* Add convar to plugin's list */
   329 	AddConVarToPluginList(pContext, pConVar);
   330 
   331 	/* Create a handle from the new convar */
   332 	hndl = g_HandleSys.CreateHandle(m_ConVarType, pConVar, NULL, g_pCoreIdent, NULL);
   333 
   334 	/* Create and initialize ConVarInfo structure */
   335 	pInfo = new ConVarInfo();
   336 	pInfo->handle = hndl;
   337 	pInfo->sourceMod = true;
   338 	pInfo->pChangeForward = NULL;
   339 	pInfo->origCallback = NULL;
   340 
   341 	/* Insert struct into caches */
   342 	m_ConVars.push_back(pInfo);
   343 	sm_trie_insert(m_ConVarCache, name, pInfo);
   344 
   345 	return hndl;
   346 }
   347 
   348 Handle_t ConVarManager::FindConVar(const char *name)
   349 {
   350 	ConVar *pConVar = NULL;
   351 	ConVarInfo *pInfo;
   352 	Handle_t hndl;
   353 
   354 	/* Search for convar */
   355 	pConVar = icvar->FindVar(name);
   356 
   357 	/* If it doesn't exist, then return an invalid handle */
   358 	if (!pConVar)
   359 	{
   360 		return BAD_HANDLE;
   361 	}
   362 
   363 	/* At this point, the convar exists. So, find out if we already have a handle */
   364 	if (sm_trie_retrieve(m_ConVarCache, name, (void **)&pInfo))
   365 	{
   366 		return pInfo->handle;
   367 	}
   368 
   369 	/* If we don't have a handle, then create a new one */
   370 	hndl = g_HandleSys.CreateHandle(m_ConVarType, pConVar, NULL, g_pCoreIdent, NULL);
   371 
   372 	/* Create and initialize ConVarInfo structure */
   373 	pInfo = new ConVarInfo();
   374 	pInfo->handle = hndl;
   375 	pInfo->sourceMod = false;
   376 	pInfo->pChangeForward = NULL;
   377 	pInfo->origCallback = pConVar->GetCallback();
   378 #if PLAPI_VERSION < 12
   379 	pInfo->name = sm_strdup(pConVar->GetName());
   380 #endif
   381 
   382 	/* Insert struct into our caches */
   383 	m_ConVars.push_back(pInfo);
   384 	sm_trie_insert(m_ConVarCache, name, pInfo);
   385 
   386 	return hndl;
   387 }
   388 
   389 void ConVarManager::HookConVarChange(ConVar *pConVar, IPluginFunction *pFunction)
   390 {
   391 	ConVarInfo *pInfo;
   392 	IChangeableForward *pForward;
   393 
   394 	/* Find the convar in the lookup trie */
   395 	if (sm_trie_retrieve(m_ConVarCache, pConVar->GetName(), (void **)&pInfo))
   396 	{
   397 		/* Get the forward */
   398 		pForward = pInfo->pChangeForward;
   399 
   400 		/* If forward does not exist, create it */
   401 		if (!pForward)
   402 		{
   403 			pForward = g_Forwards.CreateForwardEx(NULL, ET_Ignore, 3, CONVARCHANGE_PARAMS);
   404 			pInfo->pChangeForward = pForward;
   405 
   406 			/* Install our own callback */
   407 			pConVar->InstallChangeCallback(OnConVarChanged);
   408 		}
   409 
   410 		/* Add function to forward's list */
   411 		pForward->AddFunction(pFunction);
   412 	}
   413 }
   414 
   415 void ConVarManager::UnhookConVarChange(ConVar *pConVar, IPluginFunction *pFunction)
   416 {
   417 	ConVarInfo *pInfo;
   418 	IChangeableForward *pForward;
   419 	IPluginContext *pContext = pFunction->GetParentContext();
   420 
   421 	/* Find the convar in the lookup trie */
   422 	if (sm_trie_retrieve(m_ConVarCache, pConVar->GetName(), (void **)&pInfo))
   423 	{
   424 		/* Get the forward */
   425 		pForward = pInfo->pChangeForward;
   426 
   427 		/* If the forward doesn't exist, we can't unhook anything */
   428 		if (!pForward)
   429 		{
   430 			pContext->ThrowNativeError("Convar \"%s\" has no active hook", pConVar->GetName());
   431 			return;
   432 		}
   433 
   434 		/* Remove the function from the forward's list */
   435 		if (!pForward->RemoveFunction(pFunction))
   436 		{
   437 			pContext->ThrowNativeError("Invalid hook callback specified for convar \"%s\"", pConVar->GetName());
   438 			return;
   439 		}
   440 
   441 		/* If the forward now has 0 functions in it... */
   442 		if (pForward->GetFunctionCount() == 0)
   443 		{
   444 			/* Free this forward */
   445 			g_Forwards.ReleaseForward(pForward);
   446 			pInfo->pChangeForward = NULL;
   447 
   448 			/* Put back the original convar callback */
   449 			pConVar->InstallChangeCallback(pInfo->origCallback);
   450 		}
   451 	}
   452 }
   453 
   454 QueryCvarCookie_t ConVarManager::QueryClientConVar(edict_t *pPlayer, const char *name, IPluginFunction *pCallback, Handle_t hndl)
   455 {
   456 	QueryCvarCookie_t cookie;
   457 
   458 	/* Call StartQueryCvarValue() in either the IVEngineServer or IServerPluginHelpers depending on situation */
   459 	if (!m_VSPIface)
   460 	{
   461 		cookie = engine->StartQueryCvarValue(pPlayer, name);	
   462 	} else {
   463 		cookie = serverpluginhelpers->StartQueryCvarValue(pPlayer, name);
   464 	}
   465 
   466 	ConVarQuery query = {cookie, pCallback, hndl};
   467 	m_ConVarQueries.push_back(query);
   468 
   469 	return cookie;
   470 }
   471 
   472 void ConVarManager::AddConVarToPluginList(IPluginContext *pContext, const ConVar *pConVar)
   473 {
   474 	ConVarList *pConVarList;
   475 	ConVarList::iterator iter;
   476 	bool inserted = false;
   477 	const char *orig = pConVar->GetName();
   478 
   479 	IPlugin *plugin = g_PluginSys.FindPluginByContext(pContext->GetContext());
   480 
   481 	/* Check plugin for an existing convar list */
   482 	if (!plugin->GetProperty("ConVarList", (void **)&pConVarList))
   483 	{
   484 		pConVarList = new ConVarList();
   485 		plugin->SetProperty("ConVarList", pConVarList);
   486 	} else if (pConVarList->find(pConVar) != pConVarList->end()) {
   487 		/* If convar is already in list, then don't add it */
   488 		return;
   489 	}
   490 
   491 	/* Insert convar into list which is sorted alphabetically */
   492 	for (iter = pConVarList->begin(); iter != pConVarList->end(); iter++)
   493 	{
   494 		if (strcmp(orig, (*iter)->GetName()) < 0)
   495 		{
   496 			pConVarList->insert(iter, pConVar);
   497 			inserted = true;
   498 			break;
   499 		}
   500 	}
   501 
   502 	if (!inserted)
   503 	{
   504 		pConVarList->push_back(pConVar);
   505 	}
   506 }
   507 
   508 void ConVarManager::OnConVarChanged(ConVar *pConVar, const char *oldValue)
   509 {
   510 	/* If the values are the same, exit early in order to not trigger callbacks */
   511 	if (strcmp(pConVar->GetString(), oldValue) == 0)
   512 	{
   513 		return;
   514 	}
   515 
   516 	Trie *pCache = g_ConVarManager.GetConVarCache();
   517 	ConVarInfo *pInfo;
   518 
   519 	/* Find the convar in the lookup trie */
   520 	sm_trie_retrieve(pCache, pConVar->GetName(), (void **)&pInfo);
   521 
   522 	FnChangeCallback origCallback = pInfo->origCallback;
   523 	IChangeableForward *pForward = pInfo->pChangeForward;
   524 
   525 	/* If there was a change callback installed previously, call it */
   526 	if (origCallback)
   527 	{
   528 		origCallback(pConVar, oldValue);
   529 	}
   530 
   531 	/* Now call forwards in plugins that have hooked this */
   532 	pForward->PushCell(pInfo->handle);
   533 	pForward->PushString(oldValue);
   534 	pForward->PushString(pConVar->GetString());
   535 	pForward->Execute(NULL);
   536 }
   537 
   538 void ConVarManager::OnQueryCvarValueFinished(QueryCvarCookie_t cookie, edict_t *pPlayer, EQueryCvarValueStatus result, const char *cvarName, const char *cvarValue)
   539 {
   540 	IPluginFunction *pCallback = NULL;
   541 	cell_t value = 0;
   542 	List<ConVarQuery>::iterator iter;
   543 
   544 	for (iter = m_ConVarQueries.begin(); iter != m_ConVarQueries.end(); iter++)
   545 	{
   546 		ConVarQuery &query = (*iter);
   547 		
   548 		if (query.cookie == cookie)
   549 		{
   550 			pCallback = query.pCallback;
   551 			value = query.value;
   552 
   553 			break;
   554 		}
   555 	}
   556 
   557 	if (pCallback)
   558 	{
   559 		cell_t ret;
   560 
   561 		pCallback->PushCell(cookie);
   562 		pCallback->PushCell(engine->IndexOfEdict(pPlayer));
   563 		pCallback->PushCell(result);
   564 		pCallback->PushString(cvarName);
   565 
   566 		if (result == eQueryCvarValueStatus_ValueIntact)
   567 		{
   568 			pCallback->PushString(cvarValue);
   569 		} else {
   570 			pCallback->PushString("\0");
   571 		}
   572 
   573 		pCallback->PushCell(value);
   574 		pCallback->Execute(&ret);
   575 
   576 		m_ConVarQueries.erase(iter);
   577 	}
   578 }
   579 
   580 
   581 static int s_YamagramState = 0;
   582 
   583 void _YamagramPrinterTwoPointOhOh(int yamagram)
   584 {
   585 	switch (yamagram)
   586 	{
   587 	case 0:
   588 		g_RootMenu.ConsolePrint("Answer the following questions correctly and Gaben may not eat you after all.");
   589 		g_RootMenu.ConsolePrint("You will be given one hint in the form of my patented yamagrams.");
   590 		g_RootMenu.ConsolePrint("Type sm_nana to see the last question.");
   591 		g_RootMenu.ConsolePrint("Type sm_nana <answer> to attempt an answer of the question.");
   592 		g_RootMenu.ConsolePrint("-------------------------------");
   593 		_YamagramPrinterTwoPointOhOh(1);
   594 		return;
   595 	case 1:
   596 		g_RootMenu.ConsolePrint("Question Ichi (1)");
   597 		g_RootMenu.ConsolePrint("One can turn into a cow by doing what action?");
   598 		g_RootMenu.ConsolePrint("Hint: AGE SANS GRIT");
   599 		break;
   600 	case 2:
   601 		g_RootMenu.ConsolePrint("Question Ni (2)");
   602 		g_RootMenu.ConsolePrint("What kind of hat should you wear when using the Internet?");
   603 		g_RootMenu.ConsolePrint("Hint: BRR MOOSE");
   604 		break;
   605 	case 3:
   606 		g_RootMenu.ConsolePrint("Question San (3)");
   607 		g_RootMenu.ConsolePrint("Who is the lead developer of SourceMod?");
   608 		g_RootMenu.ConsolePrint("Hint: VEAL BANDANA DID RIP SOON");
   609 		break;
   610 	case 4:
   611 		g_RootMenu.ConsolePrint("Question Yon (4)");
   612 		g_RootMenu.ConsolePrint("A terrible translation of 'SVN Revision' to Japanese romaji might be ...");
   613 		g_RootMenu.ConsolePrint("Hint: I TAKE IN AN AIR OK");
   614 		break;
   615 	case 5:
   616 		g_RootMenu.ConsolePrint("Question Go (5)");
   617 		g_RootMenu.ConsolePrint("What is a fundamental concept in the game of Go?");
   618 		g_RootMenu.ConsolePrint("Hint: AD LADEN THIEF");
   619 		break;
   620 	case 6:
   621 		g_RootMenu.ConsolePrint("Question Roku (6)");
   622 		g_RootMenu.ConsolePrint("Why am I asking all these strange questions?");
   623 		g_RootMenu.ConsolePrint("Hint: CHUBBY TITAN EATS EWE WAGE DATA");
   624 		break;
   625 	case 7:
   626 		g_RootMenu.ConsolePrint("Question Nana (7)");
   627 		g_RootMenu.ConsolePrint("What is my name?");
   628 		g_RootMenu.ConsolePrint("Hint: AD MODE LAG US");
   629 		break;
   630 	default:
   631 		break;
   632 	}
   633 
   634 	s_YamagramState = yamagram;
   635 }
   636 
   637 void _IntExt_CallYamagrams()
   638 {
   639 	bool correct = false;
   640 	const char *arg = engine->Cmd_Args();
   641 
   642 	if (!arg || arg[0] == '\0')
   643 	{
   644 		_YamagramPrinterTwoPointOhOh(s_YamagramState);
   645 		return;
   646 	}
   647 
   648 	switch (s_YamagramState)
   649 	{
   650 	case 1:
   651 		correct = !strcasecmp(arg, "eating grass");
   652 		break;
   653 	case 2:
   654 		correct = !strcasecmp(arg, "sombrero");
   655 		break;
   656 	case 3:
   657 		correct = !strcasecmp(arg, "david bailopan anderson");
   658 		break;
   659 	case 4:
   660 		correct = !strcasecmp(arg, "kaitei no kairan");
   661 		break;
   662 	case 5:
   663 		correct = !strcasecmp(arg, "life and death");
   664 		break;
   665 	case 6:
   666 		correct = !strcasecmp(arg, "because gabe wanted it that way");
   667 		if (correct)
   668 		{
   669 			g_RootMenu.ConsolePrint("Congratulations, you have answered 6 of my questions.");
   670 			g_RootMenu.ConsolePrint("However, I have one final question for you. It wouldn't be nana without it.");
   671 			g_RootMenu.ConsolePrint("-------------------------------");
   672 			_YamagramPrinterTwoPointOhOh(7);
   673 			return;
   674 		}
   675 		break;
   676 	case 7:
   677 		correct = !strcasecmp(arg, "damaged soul");
   678 		if (correct)
   679 		{
   680 			g_RootMenu.ConsolePrint("You don't know how lucky you are to still be alive!");
   681 			g_RootMenu.ConsolePrint("Congratulations. You have answered all 7 questions correctly.");
   682 			g_RootMenu.ConsolePrint("The SourceMod Dev Team will be at your door with anti-Gaben grenades");
   683 			g_RootMenu.ConsolePrint("within seconds. You will also be provided with a rocket launcher,");
   684 			g_RootMenu.ConsolePrint("just in case Alfred decides to strike you with a blitzkrieg in retaliation.");
   685 
   686 			s_YamagramState = 0;
   687 			return;
   688 		}
   689 		break;
   690 	default:
   691 		break;
   692 	}
   693 
   694 	if (s_YamagramState > 0)
   695 	{
   696 		if (correct)
   697 		{
   698 			g_RootMenu.ConsolePrint("Correct! You are one step closer to avoiding the deadly jaws of Gaben.");
   699 			g_RootMenu.ConsolePrint("-------------------------------");
   700 			s_YamagramState++;
   701 		} else {
   702 			g_RootMenu.ConsolePrint("Wrong! You better be more careful. Gaben may be at your door at any minute.");
   703 			return;
   704 		}
   705 	}
   706 
   707 	_YamagramPrinterTwoPointOhOh(s_YamagramState);
   708 }
   709 
   710 void _IntExt_EnableYamagrams()
   711 {
   712 	static ConCommand *pCmd = NULL;
   713 	if (!pCmd)
   714 	{
   715 		pCmd = new ConCommand("sm_nana", _IntExt_CallYamagrams, "Try these yamagrams!", FCVAR_GAMEDLL);
   716 		g_RootMenu.ConsolePrint("[SM] Warning: Gaben has been alerted of your actions. You may be eaten.");
   717 	} else {
   718 		g_RootMenu.ConsolePrint("[SM] Gaben has already been alerted of your actions...");
   719 	}
   720 }
   721 
   722 void _IntExt_OnHostnameChanged(ConVar *pConVar, char const *oldValue)
   723 {
   724 	if (strcmp(oldValue, "Good morning, DS-san.") == 0
   725 		&& strcmp(pConVar->GetString(), "Good night, talking desk lamp.") == 0)
   726 	{
   727 		_IntExt_EnableYamagrams();
   728 	}
   729 }