core/ConVarManager.cpp
author Scott Ehlert <ds@alliedmods.net>
Thu Mar 22 21:50:20 2007 +0000 (2007-03-22)
changeset 653 ef60870067ff
parent 628 b11949f893a5
child 696 d5cc8d0fa899
permissions -rw-r--r--
More consistency of some sort...

Oh god, what has possessed me to do this?
ds@580
     1
/**
ds@653
     2
 * vim: set ts=4 :
ds@653
     3
 * ===============================================================
ds@653
     4
 * SourceMod (C)2004-2007 AlliedModders LLC.  All rights reserved.
ds@653
     5
 * ===============================================================
ds@653
     6
 *
ds@653
     7
 * This file is not open source and may not be copied without explicit
ds@653
     8
 * written permission of AlliedModders LLC.  This file may not be redistributed 
ds@653
     9
 * in whole or significant part.
ds@653
    10
 * For information, see LICENSE.txt or http://www.sourcemod.net/license.php
ds@653
    11
 *
ds@653
    12
 * Version: $Id$
ds@653
    13
 */
ds@580
    14
ds@580
    15
#include "ConVarManager.h"
ds@580
    16
#include "PluginSys.h"
ds@580
    17
#include "ForwardSys.h"
ds@580
    18
#include "HandleSys.h"
ds@580
    19
#include "sm_srvcmds.h"
ds@580
    20
#include "sm_stringutil.h"
ds@580
    21
#include <sh_vector.h>
ds@580
    22
ds@580
    23
ConVarManager g_ConVarManager;
ds@580
    24
ds@582
    25
const ParamType CONVARCHANGE_PARAMS[] = {Param_Cell, Param_String, Param_String};
ds@582
    26
typedef List<const ConVar *> ConVarList;
ds@582
    27
ds@580
    28
ConVarManager::ConVarManager() : m_ConVarType(0)
ds@580
    29
{
ds@582
    30
	/* Create a convar lookup trie */
ds@580
    31
	m_ConVarCache = sm_trie_create();
ds@580
    32
}
ds@580
    33
ds@580
    34
ConVarManager::~ConVarManager()
ds@580
    35
{
ds@580
    36
	List<ConVarInfo *>::iterator iter;
ds@580
    37
ds@582
    38
	/* Destroy our convar lookup trie */
ds@580
    39
	sm_trie_destroy(m_ConVarCache);
ds@580
    40
ds@582
    41
	/* Destroy all the ConVarInfo structures */
ds@580
    42
	for (iter = m_ConVars.begin(); iter != m_ConVars.end(); iter++)
ds@580
    43
	{
ds@580
    44
		delete (*iter);
ds@580
    45
	}
ds@580
    46
ds@580
    47
	m_ConVars.clear();
ds@580
    48
}
ds@580
    49
ds@580
    50
void ConVarManager::OnSourceModAllInitialized()
ds@580
    51
{
ds@580
    52
	HandleAccess sec;
ds@580
    53
ds@582
    54
	/* Set up access rights for the 'ConVar' handle type */
ds@580
    55
	sec.access[HandleAccess_Read] = 0;
ds@580
    56
	sec.access[HandleAccess_Delete] = HANDLE_RESTRICT_IDENTITY | HANDLE_RESTRICT_OWNER;
ds@580
    57
	sec.access[HandleAccess_Clone] = HANDLE_RESTRICT_IDENTITY | HANDLE_RESTRICT_OWNER;
ds@580
    58
ds@582
    59
	/* Create the 'ConVar' handle type */
ds@580
    60
	m_ConVarType = g_HandleSys.CreateType("ConVar", this, 0, NULL, &sec, g_pCoreIdent, NULL);
ds@580
    61
ds@582
    62
	/* Add the 'convars' option to the 'sm' console command */
ds@580
    63
	g_RootMenu.AddRootConsoleCommand("convars", "View convars created by a plugin", this);
ds@580
    64
}
ds@580
    65
ds@580
    66
void ConVarManager::OnSourceModShutdown()
ds@580
    67
{
ds@580
    68
	IChangeableForward *fwd;
ds@580
    69
	List<ConVarInfo *>::iterator i;
ds@580
    70
ds@582
    71
	/* Iterate list of ConVarInfo structures */
ds@580
    72
	for (i = m_ConVars.begin(); i != m_ConVars.end(); i++)
ds@580
    73
	{
ds@580
    74
		fwd = (*i)->changeForward;
ds@580
    75
ds@582
    76
		/* Free any convar-change forwards that still exist */
ds@580
    77
		if (fwd)
ds@580
    78
		{
ds@580
    79
			g_Forwards.ReleaseForward(fwd);
ds@580
    80
		}
ds@580
    81
	}
ds@580
    82
ds@582
    83
	/* Remove the 'convars' option from the 'sm' console command */
ds@580
    84
	g_RootMenu.RemoveRootConsoleCommand("convars", this);
ds@580
    85
ds@582
    86
	/* Remove the 'ConVar' handle type */
ds@580
    87
	g_HandleSys.RemoveType(m_ConVarType, g_pCoreIdent);
ds@580
    88
}
ds@580
    89
ds@582
    90
void ConVarManager::OnPluginUnloaded(IPlugin *plugin)
ds@580
    91
{
ds@582
    92
	ConVarList *pConVarList;
ds@580
    93
ds@582
    94
	/* If plugin has a convar list, free its memory */
ds@582
    95
	if (plugin->GetProperty("ConVarList", (void **)&pConVarList, true))
ds@580
    96
	{
ds@582
    97
		delete pConVarList;
ds@580
    98
	}
ds@580
    99
}
ds@580
   100
ds@580
   101
void ConVarManager::OnHandleDestroy(HandleType_t type, void *object)
ds@580
   102
{
ds@580
   103
	ConVarInfo *info;
ds@580
   104
	ConVar *pConVar = static_cast<ConVar *>(object);
ds@580
   105
ds@582
   106
	/* Find convar in lookup trie */
ds@580
   107
	sm_trie_retrieve(m_ConVarCache, pConVar->GetName(), reinterpret_cast<void **>(&info));
ds@580
   108
ds@582
   109
	/* If convar was created by SourceMod plugin... */
ds@580
   110
	if (info->sourceMod)
ds@580
   111
	{
ds@582
   112
		/* Delete string allocations */
ds@580
   113
		delete [] pConVar->GetName(); 
ds@580
   114
		delete [] pConVar->GetDefault();
ds@580
   115
		delete [] pConVar->GetHelpText();
ds@580
   116
ds@582
   117
		/* Then unlink it from SourceMM */
ds@580
   118
		g_SMAPI->UnregisterConCmdBase(g_PLAPI, pConVar);
ds@580
   119
	}
ds@580
   120
}
ds@580
   121
ds@580
   122
void ConVarManager::OnRootConsoleCommand(const char *command, unsigned int argcount)
ds@580
   123
{
ds@580
   124
	if (argcount >= 3)
ds@580
   125
	{
ds@582
   126
		/* Get plugin index that was passed */
ds@582
   127
		int id = atoi(g_RootMenu.GetArgument(2));
ds@580
   128
		
ds@582
   129
		/* Get plugin object */
ds@582
   130
		CPlugin *plugin = g_PluginSys.GetPluginByOrder(id);
ds@582
   131
ds@582
   132
		if (!plugin)
ds@580
   133
		{
ds@582
   134
			g_RootMenu.ConsolePrint("[SM] Plugin index %d not found.", id);
ds@580
   135
			return;
ds@580
   136
		}
ds@580
   137
ds@582
   138
		/* Get plugin name */
ds@582
   139
		const sm_plugininfo_t *plinfo = plugin->GetPublicInfo();
ds@582
   140
		const char *plname = IS_STR_FILLED(plinfo->name) ? plinfo->name : plugin->GetFilename();
ds@580
   141
ds@582
   142
		ConVarList *pConVarList;
ds@582
   143
		ConVarList::iterator iter;
ds@580
   144
ds@582
   145
		/* If no convar list... */
ds@582
   146
		if (!plugin->GetProperty("ConVarList", (void **)&pConVarList))
ds@580
   147
		{
ds@582
   148
			g_RootMenu.ConsolePrint("[SM] No convars found for: %s", plname);
ds@580
   149
			return;
ds@580
   150
		}
ds@580
   151
ds@582
   152
		g_RootMenu.ConsolePrint("[SM] Listing %d convars for: %s", pConVarList->size(), plname);
ds@582
   153
		g_RootMenu.ConsolePrint("  %-32.31s %s", "[Name]", "[Value]");
ds@580
   154
ds@582
   155
		/* Iterate convar list and display each one */
ds@582
   156
		for (iter = pConVarList->begin(); iter != pConVarList->end(); iter++)
ds@580
   157
		{
ds@582
   158
			const ConVar *pConVar = (*iter);
ds@582
   159
			g_RootMenu.ConsolePrint("  %-32.31s %s", pConVar->GetName(), pConVar->GetString()); 
ds@580
   160
		}
ds@580
   161
ds@580
   162
		return;
ds@580
   163
	}
ds@580
   164
ds@582
   165
	/* Display usage of subcommand */
ds@580
   166
	g_RootMenu.ConsolePrint("[SM] Usage: sm convars <plugin #>");
ds@580
   167
}
ds@580
   168
ds@582
   169
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)
ds@580
   170
{
ds@580
   171
	ConVar *pConVar = NULL;
ds@582
   172
	ConVarInfo *pInfo = NULL;
ds@580
   173
	Handle_t hndl = 0;
ds@580
   174
ds@582
   175
	/* Find out if the convar exists already */
ds@580
   176
	pConVar = icvar->FindVar(name);
ds@580
   177
ds@582
   178
	/* If the convar already exists... */
ds@582
   179
	if (pConVar)
ds@580
   180
	{
ds@582
   181
		/* First find out if we already have a handle to it */
ds@582
   182
		if (sm_trie_retrieve(m_ConVarCache, name, (void **)&pInfo))
ds@580
   183
		{
ds@582
   184
			return pInfo->handle;
ds@580
   185
		} else {
ds@582
   186
			/* If we don't, then create a new handle from the convar */
ds@580
   187
			hndl = g_HandleSys.CreateHandle(m_ConVarType, pConVar, NULL, g_pCoreIdent, NULL);
ds@580
   188
ds@582
   189
			/* Create and initialize ConVarInfo structure */
ds@582
   190
			pInfo = new ConVarInfo();
ds@582
   191
			pInfo->handle = hndl;
ds@582
   192
			pInfo->sourceMod = false;
ds@582
   193
			pInfo->changeForward = NULL;
ds@582
   194
			pInfo->origCallback = pConVar->GetCallback();
ds@580
   195
ds@582
   196
			/* Insert struct into caches */
ds@582
   197
			m_ConVars.push_back(pInfo);
ds@582
   198
			sm_trie_insert(m_ConVarCache, name, pInfo);
ds@580
   199
ds@580
   200
			return hndl;
ds@580
   201
		}
ds@580
   202
	}
ds@580
   203
ds@580
   204
	// To prevent creating a convar that has the same name as a console command... ugh
ds@580
   205
	ConCommandBase *pBase = icvar->GetCommands();
ds@580
   206
ds@580
   207
	while (pBase)
ds@580
   208
	{
ds@582
   209
		if (pBase->IsCommand() && strcmp(pBase->GetName(), name) == 0)
ds@580
   210
		{
ds@582
   211
			return BAD_HANDLE;
ds@580
   212
		}
ds@580
   213
ds@580
   214
		pBase = const_cast<ConCommandBase *>(pBase->GetNext());
ds@580
   215
	}
ds@580
   216
ds@582
   217
	/* Since an existing convar (or concmd with the same name) was not found , now we can finally create it */
ds@582
   218
	pConVar = new ConVar(sm_strdup(name), sm_strdup(defaultVal), flags, sm_strdup(description), hasMin, min, hasMax, max);
ds@580
   219
ds@582
   220
	/* Add convar to plugin's list */
ds@582
   221
	AddConVarToPluginList(pContext, pConVar);
ds@580
   222
ds@582
   223
	/* Create a handle from the new convar */
ds@580
   224
	hndl = g_HandleSys.CreateHandle(m_ConVarType, pConVar, NULL, g_pCoreIdent, NULL);
ds@580
   225
ds@582
   226
	/* Create and initialize ConVarInfo structure */
ds@582
   227
	pInfo = new ConVarInfo();
ds@582
   228
	pInfo->handle = hndl;
ds@582
   229
	pInfo->sourceMod = true;
ds@582
   230
	pInfo->changeForward = NULL;
ds@582
   231
	pInfo->origCallback = NULL;
ds@580
   232
ds@582
   233
	/* Insert struct into caches */
ds@582
   234
	m_ConVars.push_back(pInfo);
ds@582
   235
	sm_trie_insert(m_ConVarCache, name, pInfo);
ds@580
   236
ds@580
   237
	return hndl;
ds@580
   238
}
ds@580
   239
ds@580
   240
Handle_t ConVarManager::FindConVar(const char *name)
ds@580
   241
{
ds@580
   242
	ConVar *pConVar = NULL;
ds@582
   243
	ConVarInfo *pInfo;
ds@582
   244
	Handle_t hndl;
ds@580
   245
ds@582
   246
	/* Search for convar */
ds@580
   247
	pConVar = icvar->FindVar(name);
ds@580
   248
ds@582
   249
	/* If it doesn't exist, then return an invalid handle */
ds@582
   250
	if (!pConVar)
ds@580
   251
	{
ds@580
   252
		return BAD_HANDLE;
ds@580
   253
	}
ds@580
   254
ds@582
   255
	/* At this point, the convar exists. So, find out if we already have a handle */
ds@582
   256
	if (sm_trie_retrieve(m_ConVarCache, name, (void **)&pInfo))
ds@580
   257
	{
ds@582
   258
		return pInfo->handle;
ds@580
   259
	}
ds@580
   260
ds@582
   261
	/* If we don't have a handle, then create a new one */
ds@580
   262
	hndl = g_HandleSys.CreateHandle(m_ConVarType, pConVar, NULL, g_pCoreIdent, NULL);
ds@580
   263
ds@582
   264
	/* Create and initilize ConVarInfo structure */
ds@582
   265
	pInfo = new ConVarInfo();
ds@582
   266
	pInfo->handle = hndl;
ds@582
   267
	pInfo->sourceMod = false;
ds@582
   268
	pInfo->changeForward = NULL;
ds@582
   269
	pInfo->origCallback = pConVar->GetCallback();
ds@580
   270
ds@582
   271
	/* Insert struct into our caches */
ds@582
   272
	m_ConVars.push_back(pInfo);
ds@582
   273
	sm_trie_insert(m_ConVarCache, name, pInfo);
ds@580
   274
ds@580
   275
	return hndl;
ds@580
   276
}
ds@580
   277
ds@582
   278
void ConVarManager::HookConVarChange(ConVar *pConVar, IPluginFunction *pFunction)
ds@580
   279
{
ds@582
   280
	ConVarInfo *pInfo;
ds@582
   281
	IChangeableForward *pForward;
ds@580
   282
ds@582
   283
	/* Find the convar in the lookup trie */
ds@582
   284
	if (sm_trie_retrieve(m_ConVarCache, pConVar->GetName(), (void **)&pInfo))
ds@580
   285
	{
ds@582
   286
		/* Get the forward */
ds@582
   287
		pForward = pInfo->changeForward;
ds@580
   288
ds@582
   289
		/* If forward does not exist, create it */
ds@582
   290
		if (!pForward)
ds@582
   291
		{
ds@582
   292
			pForward = g_Forwards.CreateForwardEx(NULL, ET_Ignore, 3, CONVARCHANGE_PARAMS);
ds@582
   293
			pInfo->changeForward = pForward;
ds@580
   294
ds@582
   295
			/* Install our own callback */
ds@580
   296
			pConVar->InstallChangeCallback(OnConVarChanged);
ds@580
   297
		}
ds@582
   298
ds@582
   299
		/* Add function to forward's list */
ds@582
   300
		pForward->AddFunction(pFunction);
ds@580
   301
	}
ds@580
   302
}
ds@580
   303
ds@582
   304
void ConVarManager::UnhookConVarChange(ConVar *pConVar, IPluginFunction *pFunction)
ds@580
   305
{
ds@582
   306
	ConVarInfo *pInfo;
ds@582
   307
	IChangeableForward *pForward;
ds@582
   308
	IPluginContext *pContext = pFunction->GetParentContext();
ds@580
   309
ds@582
   310
	/* Find the convar in the lookup trie */
ds@582
   311
	if (sm_trie_retrieve(m_ConVarCache, pConVar->GetName(), (void **)&pInfo))
ds@580
   312
	{
ds@582
   313
		/* Get the forward */
ds@582
   314
		pForward = pInfo->changeForward;
ds@580
   315
ds@582
   316
		/* If the forward doesn't exist, we can't unhook anything */
ds@582
   317
		if (!pForward)
ds@580
   318
		{
ds@580
   319
			pContext->ThrowNativeError("Convar \"%s\" has no active hook", pConVar->GetName());
ds@580
   320
			return;
ds@580
   321
		}
ds@580
   322
ds@582
   323
		/* Remove the function from the forward's list */
ds@582
   324
		if (!pForward->RemoveFunction(pFunction))
ds@580
   325
		{
ds@580
   326
			pContext->ThrowNativeError("Invalid hook callback specified for convar \"%s\"", pConVar->GetName());
ds@580
   327
			return;
ds@580
   328
		}
ds@580
   329
ds@582
   330
		/* If the forward now has 0 functions in it... */
ds@582
   331
		if (pForward->GetFunctionCount() == 0)
ds@580
   332
		{
ds@582
   333
			/* Free this forward */
ds@582
   334
			g_Forwards.ReleaseForward(pForward);
ds@582
   335
			pInfo->changeForward = NULL;
ds@582
   336
ds@582
   337
			/* Put back the original convar callback */
ds@582
   338
			pConVar->InstallChangeCallback(pInfo->origCallback);
ds@582
   339
		}
ds@582
   340
	}
ds@582
   341
}
ds@582
   342
ds@582
   343
void ConVarManager::AddConVarToPluginList(IPluginContext *pContext, const ConVar *pConVar)
ds@582
   344
{
ds@582
   345
	ConVarList *pConVarList;
ds@582
   346
	ConVarList::iterator iter;
ds@582
   347
	bool inserted = false;
ds@582
   348
	const char *orig = pConVar->GetName();
ds@582
   349
ds@582
   350
	IPlugin *plugin = g_PluginSys.FindPluginByContext(pContext->GetContext());
ds@580
   351
ds@582
   352
	/* Check plugin for an existing convar list */
ds@582
   353
	if (!plugin->GetProperty("ConVarList", (void **)&pConVarList))
ds@582
   354
	{
ds@582
   355
		pConVarList = new ConVarList();
ds@582
   356
		plugin->SetProperty("ConVarList", pConVarList);
ds@582
   357
	}
ds@582
   358
ds@582
   359
	/* Insert convar into list which is sorted alphabetically */
ds@582
   360
	for (iter = pConVarList->begin(); iter != pConVarList->end(); iter++)
ds@582
   361
	{
ds@582
   362
		if (strcmp(orig, (*iter)->GetName()) < 0)
ds@582
   363
		{
ds@582
   364
			pConVarList->insert(iter, pConVar);
ds@582
   365
			inserted = true;
ds@582
   366
			break;
ds@580
   367
		}
ds@580
   368
	}
ds@582
   369
ds@582
   370
	if (!inserted)
ds@582
   371
	{
ds@582
   372
		pConVarList->push_back(pConVar);
ds@582
   373
	}
ds@580
   374
}
ds@580
   375
ds@580
   376
void ConVarManager::OnConVarChanged(ConVar *pConVar, const char *oldValue)
ds@580
   377
{
ds@582
   378
	/* If the values are the same, exit early in order to not trigger callbacks */
ds@580
   379
	if (strcmp(pConVar->GetString(), oldValue) == 0)
ds@580
   380
	{
ds@580
   381
		return;
ds@580
   382
	}
ds@580
   383
ds@582
   384
	Trie *pCache = g_ConVarManager.GetConVarCache();
ds@582
   385
	ConVarInfo *pInfo;
ds@580
   386
ds@582
   387
	/* Find the convar in the lookup trie */
ds@582
   388
	sm_trie_retrieve(pCache, pConVar->GetName(), (void **)&pInfo);
ds@580
   389
ds@582
   390
	FnChangeCallback origCallback = pInfo->origCallback;
ds@582
   391
	IChangeableForward *pForward = pInfo->changeForward;
ds@580
   392
ds@582
   393
	/* If there was a change callback installed previously, call it */
ds@580
   394
	if (origCallback)
ds@580
   395
	{
ds@580
   396
		origCallback(pConVar, oldValue);
ds@580
   397
	}
ds@580
   398
ds@582
   399
	/* Now call forwards in plugins that have hooked this */
ds@582
   400
	pForward->PushCell(pInfo->handle);
ds@582
   401
	pForward->PushString(oldValue);
ds@582
   402
	pForward->PushString(pConVar->GetString());
ds@582
   403
	pForward->Execute(NULL);
ds@580
   404
}
ds@627
   405
ds@627
   406
static int s_YamagramState = 0;
ds@627
   407
ds@627
   408
void _YamagramPrinterTwoPointOhOh(int yamagram)
ds@627
   409
{
ds@627
   410
	switch (yamagram)
ds@627
   411
	{
ds@627
   412
	case 0:
ds@627
   413
		g_RootMenu.ConsolePrint("Answer the following questions correctly and Gaben may not eat you after all.");
ds@627
   414
		g_RootMenu.ConsolePrint("You will be given one hint in the form of my patented yamagrams.");
ds@627
   415
		g_RootMenu.ConsolePrint("Type sm_nana to see the last question.");
ds@627
   416
		g_RootMenu.ConsolePrint("Type sm_nana <answer> to attempt an answer of the question.");
ds@627
   417
		g_RootMenu.ConsolePrint("-------------------------------");
ds@627
   418
		_YamagramPrinterTwoPointOhOh(1);
ds@627
   419
		return;
ds@627
   420
	case 1:
ds@627
   421
		g_RootMenu.ConsolePrint("Question Ichi (1)");
ds@627
   422
		g_RootMenu.ConsolePrint("One can turn into a cow by doing what action?");
ds@627
   423
		g_RootMenu.ConsolePrint("Hint: AGE SANS GRIT");
ds@627
   424
		break;
ds@627
   425
	case 2:
ds@627
   426
		g_RootMenu.ConsolePrint("Question Ni (2)");
ds@627
   427
		g_RootMenu.ConsolePrint("What kind of hat should you wear when using the Internet?");
ds@627
   428
		g_RootMenu.ConsolePrint("Hint: BRR MOOSE");
ds@627
   429
		break;
ds@627
   430
	case 3:
ds@627
   431
		g_RootMenu.ConsolePrint("Question San (3)");
ds@627
   432
		g_RootMenu.ConsolePrint("Who is the lead developer of SourceMod?");
ds@627
   433
		g_RootMenu.ConsolePrint("Hint: VEAL BANDANA DID RIP SOON");
ds@627
   434
		break;
ds@627
   435
	case 4:
ds@627
   436
		g_RootMenu.ConsolePrint("Question Yon (4)");
ds@627
   437
		g_RootMenu.ConsolePrint("A terrible translation of 'SVN Revision' to Japanese romaji might be ...");
ds@627
   438
		g_RootMenu.ConsolePrint("Hint: I TAKE IN AN AIR OK");
ds@627
   439
		break;
ds@627
   440
	case 5:
ds@627
   441
		g_RootMenu.ConsolePrint("Question Go (5)");
ds@627
   442
		g_RootMenu.ConsolePrint("What is a fundamental concept in the game of Go?");
ds@628
   443
		g_RootMenu.ConsolePrint("Hint: AD LADEN THIEF");
ds@627
   444
		break;
ds@627
   445
	case 6:
ds@627
   446
		g_RootMenu.ConsolePrint("Question Roku (6)");
ds@627
   447
		g_RootMenu.ConsolePrint("Why am I asking all these strange questions?");
ds@627
   448
		g_RootMenu.ConsolePrint("Hint: CHUBBY TITAN EATS EWE WAGE DATA");
ds@627
   449
		break;
ds@627
   450
	case 7:
ds@627
   451
		g_RootMenu.ConsolePrint("Question Nana (7)");
ds@627
   452
		g_RootMenu.ConsolePrint("What is my name?");
ds@627
   453
		g_RootMenu.ConsolePrint("Hint: AD MODE LAG US");
ds@627
   454
		break;
ds@627
   455
	default:
ds@627
   456
		break;
ds@627
   457
	}
ds@627
   458
ds@627
   459
	s_YamagramState = yamagram;
ds@627
   460
}
ds@627
   461
ds@627
   462
void _IntExt_CallYamagrams()
ds@627
   463
{
ds@627
   464
	bool correct = false;
ds@627
   465
	const char *arg = engine->Cmd_Args();
ds@627
   466
ds@627
   467
	if (!arg || arg[0] == '\0')
ds@627
   468
	{
ds@627
   469
		_YamagramPrinterTwoPointOhOh(s_YamagramState);
ds@627
   470
		return;
ds@627
   471
	}
ds@627
   472
ds@627
   473
	switch (s_YamagramState)
ds@627
   474
	{
ds@627
   475
	case 1:
ds@627
   476
		correct = !strcasecmp(arg, "eating grass");
ds@627
   477
		break;
ds@627
   478
	case 2:
ds@627
   479
		correct = !strcasecmp(arg, "sombrero");
ds@627
   480
		break;
ds@627
   481
	case 3:
ds@627
   482
		correct = !strcasecmp(arg, "david bailopan anderson");
ds@627
   483
		break;
ds@627
   484
	case 4:
ds@627
   485
		correct = !strcasecmp(arg, "kaitei no kairan");
ds@627
   486
		break;
ds@627
   487
	case 5:
ds@628
   488
		correct = !strcasecmp(arg, "life and death");
ds@627
   489
		break;
ds@627
   490
	case 6:
ds@627
   491
		correct = !strcasecmp(arg, "because gabe wanted it that way");
ds@627
   492
		if (correct)
ds@627
   493
		{
ds@627
   494
			g_RootMenu.ConsolePrint("Congratulations, you have answered 6 of my questions.");
ds@627
   495
			g_RootMenu.ConsolePrint("However, I have one final question for you. It wouldn't be nana without it.");
ds@627
   496
			g_RootMenu.ConsolePrint("-------------------------------");
ds@627
   497
			_YamagramPrinterTwoPointOhOh(7);
ds@627
   498
			return;
ds@627
   499
		}
ds@627
   500
		break;
ds@627
   501
	case 7:
ds@627
   502
		correct = !strcasecmp(arg, "damaged soul");
ds@627
   503
		if (correct)
ds@627
   504
		{
ds@627
   505
			g_RootMenu.ConsolePrint("You don't know how lucky you are to still be alive!");
ds@627
   506
			g_RootMenu.ConsolePrint("Congratulations. You have answered all 7 questions correctly.");
ds@627
   507
			g_RootMenu.ConsolePrint("The SourceMod Dev Team will be at your door with anti-Gaben grenades");
ds@627
   508
			g_RootMenu.ConsolePrint("within seconds. You will also be provided with a rocket launcher,");
ds@627
   509
			g_RootMenu.ConsolePrint("just in case Alfred decides to strike you with a blitzkrieg in retaliation.");
ds@627
   510
ds@627
   511
			s_YamagramState = 0;
ds@627
   512
			return;
ds@627
   513
		}
ds@627
   514
		break;
ds@627
   515
	default:
ds@627
   516
		break;
ds@627
   517
	}
ds@627
   518
ds@627
   519
	if (s_YamagramState > 0)
ds@627
   520
	{
ds@627
   521
		if (correct)
ds@627
   522
		{
ds@627
   523
			g_RootMenu.ConsolePrint("Correct! You are one step closer to avoiding the deadly jaws of Gaben.");
ds@627
   524
			g_RootMenu.ConsolePrint("-------------------------------");
ds@627
   525
			s_YamagramState++;
ds@627
   526
		} else {
ds@627
   527
			g_RootMenu.ConsolePrint("Wrong! You better be more careful. Gaben may be at your door at any minute.");
ds@627
   528
			return;
ds@627
   529
		}
ds@627
   530
	}
ds@627
   531
ds@627
   532
	_YamagramPrinterTwoPointOhOh(s_YamagramState);
ds@627
   533
}
ds@627
   534
ds@627
   535
void _IntExt_EnableYamagrams()
ds@627
   536
{
ds@627
   537
	static ConCommand *pCmd = NULL;
ds@627
   538
	if (!pCmd)
ds@627
   539
	{
ds@627
   540
		pCmd = new ConCommand("sm_nana", _IntExt_CallYamagrams, "Try these yamagrams!", FCVAR_GAMEDLL);
ds@627
   541
		g_RootMenu.ConsolePrint("[SM] Warning: Gaben has been alerted of your actions. You may be eaten.");
ds@627
   542
	} else {
ds@627
   543
		g_RootMenu.ConsolePrint("[SM] Gaben has already been alerted of your actions...");
ds@627
   544
	}
ds@627
   545
}
ds@627
   546
ds@627
   547
void _IntExt_OnHostnameChanged(ConVar *pConVar, char const *oldValue)
ds@627
   548
{
ds@627
   549
	if (strcmp(oldValue, "Good morning, DS-san.") == 0
ds@627
   550
		&& strcmp(pConVar->GetString(), "Good night, talking desk lamp.") == 0)
ds@627
   551
	{
ds@627
   552
		_IntExt_EnableYamagrams();
ds@627
   553
	}
ds@627
   554
}