core/ConCmdManager.cpp
author David Anderson <dvander@alliedmods.net>
Mon Jun 18 07:04:22 2007 +0000 (2007-06-18)
changeset 978 54782a83a1ef
parent 895 a1068846fee8
child 1119 9e3486386c04
permissions -rw-r--r--
- removed OnServerCfg in favor of OnConfigsExecuted
- added AutoExecConfig() which automates safe config execution
ds@579
     1
/**
ds@653
     2
 * vim: set ts=4 :
ds@579
     3
 * ===============================================================
ds@579
     4
 * SourceMod (C)2004-2007 AlliedModders LLC.  All rights reserved.
ds@579
     5
 * ===============================================================
ds@579
     6
 *
ds@579
     7
 * This file is not open source and may not be copied without explicit
ds@579
     8
 * written permission of AlliedModders LLC.  This file may not be redistributed 
ds@579
     9
 * in whole or significant part.
ds@579
    10
 * For information, see LICENSE.txt or http://www.sourcemod.net/license.php
ds@579
    11
 *
ds@579
    12
 * Version: $Id$
ds@579
    13
 */
ds@579
    14
ds@579
    15
#include "ConCmdManager.h"
ds@579
    16
#include "sm_srvcmds.h"
ds@579
    17
#include "AdminCache.h"
ds@579
    18
#include "sm_stringutil.h"
dvander@591
    19
#include "PlayerManager.h"
dvander@591
    20
#include "Translator.h"
dvander@895
    21
#include "HalfLife2.h"
dvander@895
    22
#include "ChatTriggers.h"
ds@579
    23
ds@579
    24
ConCmdManager g_ConCmds;
ds@579
    25
ds@579
    26
SH_DECL_HOOK0_void(ConCommand, Dispatch, SH_NOATTRIB, false);
ds@579
    27
SH_DECL_HOOK1_void(IServerGameClients, SetCommandClient, SH_NOATTRIB, false, int);
ds@579
    28
ds@579
    29
struct PlCmdInfo
ds@579
    30
{
ds@579
    31
	ConCmdInfo *pInfo;
ds@579
    32
	CmdHook *pHook;
ds@579
    33
	CmdType type;
ds@579
    34
};
ds@579
    35
typedef List<PlCmdInfo> CmdList;
dvander@723
    36
ds@579
    37
void AddToPlCmdList(CmdList *pList, const PlCmdInfo &info);
ds@579
    38
ds@579
    39
ConCmdManager::ConCmdManager() : m_Strings(1024)
ds@579
    40
{
ds@579
    41
	m_pCmds = sm_trie_create();
ds@579
    42
	m_pCmdGrps = sm_trie_create();
dvander@769
    43
	m_CmdClient = 0;
ds@579
    44
}
ds@579
    45
ds@579
    46
ConCmdManager::~ConCmdManager()
ds@579
    47
{
ds@579
    48
	sm_trie_destroy(m_pCmds);
ds@579
    49
	sm_trie_destroy(m_pCmdGrps);
ds@579
    50
}
ds@579
    51
ds@579
    52
void ConCmdManager::OnSourceModAllInitialized()
ds@579
    53
{
ds@579
    54
	g_PluginSys.AddPluginsListener(this);
ds@579
    55
	g_RootMenu.AddRootConsoleCommand("cmds", "List console commands", this);
ds@579
    56
	SH_ADD_HOOK_MEMFUNC(IServerGameClients, SetCommandClient, serverClients, this, &ConCmdManager::SetCommandClient, false);
ds@579
    57
}
ds@579
    58
ds@579
    59
void ConCmdManager::OnSourceModShutdown()
ds@579
    60
{
ds@579
    61
	/* All commands should already be removed by the time we're done */
ds@579
    62
	SH_REMOVE_HOOK_MEMFUNC(IServerGameClients, SetCommandClient, serverClients, this, &ConCmdManager::SetCommandClient, false);
ds@579
    63
	g_RootMenu.RemoveRootConsoleCommand("cmds", this);
ds@579
    64
}
ds@579
    65
ds@579
    66
void ConCmdManager::RemoveConCmds(List<CmdHook *> &cmdlist, IPluginContext *pContext)
ds@579
    67
{
ds@579
    68
	List<CmdHook *>::iterator iter = cmdlist.begin();
ds@579
    69
	CmdHook *pHook;
ds@579
    70
ds@579
    71
	while (iter != cmdlist.end())
ds@579
    72
	{
ds@579
    73
		pHook = (*iter);
ds@579
    74
		if (pHook->pf->GetParentContext() == pContext)
ds@579
    75
		{
ds@579
    76
			delete pHook->pAdmin;
ds@579
    77
			delete pHook;
ds@579
    78
			iter = cmdlist.erase(iter);
ds@579
    79
		} else {
ds@579
    80
			iter++;
ds@579
    81
		}
ds@579
    82
	}
ds@579
    83
}
ds@579
    84
ds@579
    85
void ConCmdManager::OnPluginDestroyed(IPlugin *plugin)
ds@579
    86
{
ds@579
    87
	CmdList *pList;
ds@579
    88
	List<ConCmdInfo *> removed;
ds@579
    89
	if (plugin->GetProperty("CommandList", (void **)&pList, true))
ds@579
    90
	{
ds@579
    91
		IPluginContext *pContext = plugin->GetBaseContext();
ds@579
    92
		CmdList::iterator iter;
ds@579
    93
		/* First pass!
ds@579
    94
	 	 * Only bother if there's an actual command list on this plugin... 
ds@579
    95
	 	 */
ds@579
    96
		for (iter=pList->begin();
ds@579
    97
			iter!=pList->end();
ds@579
    98
			iter++)
ds@579
    99
		{
ds@579
   100
			PlCmdInfo &cmd = (*iter);
ds@579
   101
			ConCmdInfo *pInfo = cmd.pInfo;
ds@579
   102
ds@579
   103
			/* Has this chain already been fully cleaned/removed? */
ds@579
   104
			if (removed.find(pInfo) != removed.end())
ds@579
   105
			{
ds@579
   106
				continue;
ds@579
   107
			}
ds@579
   108
ds@579
   109
			/* Remove any hooks from us on this command */
ds@579
   110
			RemoveConCmds(pInfo->conhooks, pContext);
ds@579
   111
			RemoveConCmds(pInfo->srvhooks, pContext);
ds@579
   112
ds@579
   113
			/* See if there are still hooks */
ds@579
   114
			if (pInfo->srvhooks.size())
ds@579
   115
			{
ds@579
   116
				continue;
ds@579
   117
			}
ds@579
   118
			if (pInfo->conhooks.size())
ds@579
   119
			{
ds@579
   120
				continue;
ds@579
   121
			}
ds@579
   122
ds@579
   123
			/* Remove the command, it should be safe now */
ds@579
   124
			RemoveConCmd(pInfo);
ds@579
   125
			removed.push_back(pInfo);
ds@579
   126
		}
ds@579
   127
		delete pList;
ds@579
   128
	}
ds@579
   129
}
ds@579
   130
ds@579
   131
void CommandCallback()
ds@579
   132
{
ds@579
   133
	g_ConCmds.InternalDispatch();
ds@579
   134
}
ds@579
   135
ds@579
   136
void ConCmdManager::SetCommandClient(int client)
ds@579
   137
{
ds@579
   138
	m_CmdClient = client + 1;
ds@579
   139
}
ds@579
   140
ds@579
   141
ResultType ConCmdManager::DispatchClientCommand(int client, ResultType type)
ds@579
   142
{
ds@579
   143
	const char *cmd = engine->Cmd_Argv(0);
ds@579
   144
	int args = engine->Cmd_Argc() - 1;
ds@579
   145
ds@579
   146
	ConCmdInfo *pInfo;
ds@579
   147
	if (sm_trie_retrieve(m_pCmds, cmd, (void **)&pInfo))
ds@579
   148
	{
ds@579
   149
		cell_t result = type;
ds@579
   150
		cell_t tempres = result;
ds@579
   151
		List<CmdHook *>::iterator iter;
ds@579
   152
		CmdHook *pHook;
ds@579
   153
		for (iter=pInfo->conhooks.begin();
ds@579
   154
			 iter!=pInfo->conhooks.end();
ds@579
   155
			 iter++)
ds@579
   156
		{
ds@579
   157
			pHook = (*iter);
ds@579
   158
			if (!pHook->pf->IsRunnable())
ds@579
   159
			{
ds@579
   160
				continue;
ds@579
   161
			}
ds@579
   162
			if (pHook->pAdmin && !CheckAccess(client, cmd, pHook->pAdmin))
ds@579
   163
			{
ds@579
   164
				if (result < Pl_Handled)
ds@579
   165
				{
ds@579
   166
					result = Pl_Handled;
ds@579
   167
				}
ds@579
   168
				continue;
ds@579
   169
			}
ds@579
   170
			pHook->pf->PushCell(client);
ds@579
   171
			pHook->pf->PushCell(args);
ds@579
   172
			if (pHook->pf->Execute(&tempres) == SP_ERROR_NONE)
ds@579
   173
			{
ds@579
   174
				if (tempres > result)
ds@579
   175
				{
ds@579
   176
					result = tempres;
ds@579
   177
				}
ds@579
   178
				if (result == Pl_Stop)
ds@579
   179
				{
ds@579
   180
					break;
ds@579
   181
				}
ds@579
   182
			}
ds@579
   183
		}
ds@579
   184
		type = (ResultType)result;
ds@579
   185
	}
ds@579
   186
ds@579
   187
	return type;
ds@579
   188
}
ds@579
   189
ds@579
   190
void ConCmdManager::InternalDispatch()
ds@579
   191
{
ds@579
   192
	/**
ds@579
   193
	 * Note: Console commands will EITHER go through IServerGameDLL::ClientCommand,
ds@579
   194
	 * OR this dispatch.  They will NEVER go through both.
ds@579
   195
	 * -- 
ds@579
   196
	 * Whether or not it goes through the callback is determined by FCVAR_GAMEDLL
ds@579
   197
	 */
ds@579
   198
ds@579
   199
	const char *cmd = engine->Cmd_Argv(0);
ds@579
   200
	ConCmdInfo *pInfo;
ds@579
   201
	if (!sm_trie_retrieve(m_pCmds, cmd, (void **)&pInfo))
ds@579
   202
	{
ds@579
   203
		return;
ds@579
   204
	}
ds@579
   205
ds@579
   206
	cell_t result = Pl_Continue;
ds@579
   207
	int args = engine->Cmd_Argc() - 1;
ds@579
   208
ds@579
   209
	List<CmdHook *>::iterator iter;
ds@579
   210
	CmdHook *pHook;
ds@579
   211
ds@579
   212
	/* Execute server-only commands if viable */
ds@579
   213
	if (m_CmdClient == 0 && pInfo->srvhooks.size())
ds@579
   214
	{
ds@579
   215
		cell_t tempres = result;
ds@579
   216
		for (iter=pInfo->srvhooks.begin();
ds@579
   217
			 iter!=pInfo->srvhooks.end();
ds@579
   218
			 iter++)
ds@579
   219
		{
ds@579
   220
			pHook = (*iter);
ds@579
   221
			if (!pHook->pf->IsRunnable())
ds@579
   222
			{
ds@579
   223
				continue;
ds@579
   224
			}
ds@579
   225
			pHook->pf->PushCell(args);
ds@579
   226
			if (pHook->pf->Execute(&tempres) == SP_ERROR_NONE)
ds@579
   227
			{
ds@579
   228
				if (tempres > result)
ds@579
   229
				{
ds@579
   230
					result = tempres;
ds@579
   231
				}
ds@579
   232
				if (result == Pl_Stop)
ds@579
   233
				{
ds@579
   234
					break;
ds@579
   235
				}
ds@579
   236
			}
ds@579
   237
		}
ds@579
   238
	
ds@579
   239
		/* Check if there's an early stop */
ds@579
   240
		if (result >= Pl_Stop)
ds@579
   241
		{
ds@579
   242
			if (!pInfo->sourceMod)
ds@579
   243
			{
ds@579
   244
				RETURN_META(MRES_SUPERCEDE);
ds@579
   245
			}
ds@579
   246
			return;
ds@579
   247
		}
ds@579
   248
	}
ds@579
   249
ds@579
   250
	/* Execute console commands */
ds@579
   251
	if (pInfo->conhooks.size())
ds@579
   252
	{
ds@579
   253
		cell_t tempres = result;
ds@579
   254
		for (iter=pInfo->conhooks.begin();
ds@579
   255
			iter!=pInfo->conhooks.end();
ds@579
   256
			iter++)
ds@579
   257
		{
ds@579
   258
			pHook = (*iter);
ds@579
   259
			if (!pHook->pf->IsRunnable())
ds@579
   260
			{
ds@579
   261
				continue;
ds@579
   262
			}
ds@579
   263
			if (m_CmdClient 
ds@579
   264
				&& pHook->pAdmin
ds@579
   265
				&& !CheckAccess(m_CmdClient, cmd, pHook->pAdmin))
ds@579
   266
			{
ds@579
   267
				if (result < Pl_Handled)
ds@579
   268
				{
ds@579
   269
					result = Pl_Handled;
ds@579
   270
				}
ds@579
   271
				continue;
ds@579
   272
			}
ds@856
   273
ds@856
   274
			/* On a listen server, sometimes the server host's client index can be set as 0.
ds@856
   275
			 * So index 1 is passed to the command callback to correct this potential problem.
ds@856
   276
			 */
ds@856
   277
			if (m_CmdClient == 0 && !engine->IsDedicatedServer())
ds@856
   278
			{
ds@856
   279
				pHook->pf->PushCell(1);
ds@856
   280
			} else {
ds@856
   281
				pHook->pf->PushCell(m_CmdClient);
ds@856
   282
			}
ds@856
   283
ds@579
   284
			pHook->pf->PushCell(args);
ds@579
   285
			if (pHook->pf->Execute(&tempres) == SP_ERROR_NONE)
ds@579
   286
			{
ds@579
   287
				if (tempres > result)
ds@579
   288
				{
ds@579
   289
					result = tempres;
ds@579
   290
				}
ds@579
   291
				if (result == Pl_Stop)
ds@579
   292
				{
ds@579
   293
					break;
ds@579
   294
				}
ds@579
   295
			}
ds@579
   296
		}
ds@579
   297
	}
ds@579
   298
ds@579
   299
	if (result >= Pl_Handled)
ds@579
   300
	{
ds@579
   301
		if (!pInfo->sourceMod)
ds@579
   302
		{
ds@579
   303
			RETURN_META(MRES_SUPERCEDE);
ds@579
   304
		}
ds@579
   305
		return;
ds@579
   306
	}
ds@579
   307
}
ds@579
   308
ds@579
   309
bool ConCmdManager::CheckAccess(int client, const char *cmd, AdminCmdInfo *pAdmin)
ds@579
   310
{
ds@579
   311
	FlagBits cmdflags = pAdmin->eflags;
ds@579
   312
	if (cmdflags == 0)
ds@579
   313
	{
ds@579
   314
		return true;
ds@579
   315
	}
ds@579
   316
ds@856
   317
	/* If running listen server, then client 1 is the server host and should have 'root' access */
ds@856
   318
	if (client == 1 && !engine->IsDedicatedServer())
ds@856
   319
	{
ds@856
   320
		return true;
ds@856
   321
	}
ds@856
   322
ds@579
   323
	CPlayer *player = g_Players.GetPlayerByIndex(client);
ds@579
   324
	if (!player)
ds@579
   325
	{
ds@579
   326
		return false;
ds@579
   327
	}
ds@579
   328
ds@579
   329
	AdminId adm = player->GetAdminId();
ds@579
   330
	if (adm != INVALID_ADMIN_ID)
ds@579
   331
	{
ds@579
   332
		FlagBits bits = g_Admins.GetAdminFlags(adm, Access_Effective);
ds@579
   333
ds@579
   334
		/* root knows all, WHOA */
ds@579
   335
		if ((bits & ADMFLAG_ROOT) == ADMFLAG_ROOT)
ds@579
   336
		{
ds@579
   337
			return true;
ds@579
   338
		}
ds@579
   339
ds@579
   340
		/* See if our other flags match */
ds@579
   341
		if ((bits & cmdflags) == cmdflags)
ds@579
   342
		{
ds@579
   343
			return true;
ds@579
   344
		}
ds@579
   345
ds@579
   346
		/* Check for overrides */
ds@579
   347
		unsigned int groups = g_Admins.GetAdminGroupCount(adm);
ds@579
   348
		GroupId gid;
ds@579
   349
		OverrideRule rule;
ds@579
   350
		bool override = false;
ds@579
   351
		for (unsigned int i=0; i<groups; i++)
ds@579
   352
		{
ds@579
   353
			gid = g_Admins.GetAdminGroup(adm, i, NULL);
ds@579
   354
			/* First get group-level override */
ds@579
   355
			override = g_Admins.GetGroupCommandOverride(gid, cmd, Override_CommandGroup, &rule);
ds@579
   356
			/* Now get the specific command override */
ds@579
   357
			if (g_Admins.GetGroupCommandOverride(gid, cmd, Override_Command, &rule))
ds@579
   358
			{
ds@579
   359
				override = true;
ds@579
   360
			}
ds@579
   361
			if (override)
ds@579
   362
			{
ds@579
   363
				if (rule == Command_Allow)
ds@579
   364
				{
ds@579
   365
					return true;
ds@579
   366
				} else if (rule == Command_Deny) {
ds@579
   367
					break;
ds@579
   368
				}
ds@579
   369
			}
ds@579
   370
		}
ds@579
   371
	}
ds@579
   372
ds@579
   373
	if (player->IsFakeClient()
ds@579
   374
		|| player->GetEdict() == NULL)
ds@579
   375
	{
ds@579
   376
		return false;
ds@579
   377
	}
ds@579
   378
	
ds@579
   379
	/* If we got here, the command failed... */
ds@579
   380
	char buffer[128];
ds@579
   381
	if (g_Translator.CoreTrans(client, buffer, sizeof(buffer), "No Access", NULL, NULL)
ds@579
   382
		!= Trans_Okay)
ds@579
   383
	{
ds@579
   384
		snprintf(buffer, sizeof(buffer), "You do not have access to this command");
ds@579
   385
	}
ds@579
   386
dvander@895
   387
	unsigned int replyto = g_ChatTriggers.GetReplyTo();
dvander@895
   388
	if (replyto == SM_REPLY_CONSOLE)
dvander@895
   389
	{
dvander@895
   390
		char fullbuffer[192];
dvander@895
   391
		snprintf(fullbuffer, sizeof(fullbuffer), "[SM] %s.\n", buffer);
dvander@895
   392
		engine->ClientPrintf(player->GetEdict(), fullbuffer);
dvander@895
   393
	} else if (replyto == SM_REPLY_CHAT) {
dvander@895
   394
		char fullbuffer[192];
dvander@895
   395
		snprintf(fullbuffer, sizeof(fullbuffer), "[SM] %s.", buffer);
dvander@895
   396
		g_HL2.TextMsg(client, HUD_PRINTTALK, fullbuffer);
dvander@895
   397
	}
ds@579
   398
	
ds@579
   399
	return false;
ds@579
   400
}
ds@579
   401
ds@579
   402
bool ConCmdManager::AddConsoleCommand(IPluginFunction *pFunction, 
ds@579
   403
									   const char *name, 
ds@579
   404
									   const char *description, 
ds@579
   405
									   int flags)
ds@579
   406
{
ds@579
   407
	ConCmdInfo *pInfo = AddOrFindCommand(name, description, flags);
ds@579
   408
ds@579
   409
	if (!pInfo)
ds@579
   410
	{
ds@579
   411
		return false;
ds@579
   412
	}
ds@579
   413
ds@579
   414
	CmdHook *pHook = new CmdHook();
ds@579
   415
ds@579
   416
	pHook->pf = pFunction;
ds@579
   417
	if (description && description[0])
ds@579
   418
	{
ds@579
   419
		pHook->helptext.assign(description);
ds@579
   420
	}
ds@579
   421
	pInfo->conhooks.push_back(pHook);
ds@579
   422
ds@579
   423
	/* Add to the plugin */
ds@579
   424
	CmdList *pList;
ds@579
   425
	IPlugin *pPlugin = g_PluginSys.GetPluginByCtx(pFunction->GetParentContext()->GetContext());
ds@579
   426
	if (!pPlugin->GetProperty("CommandList", (void **)&pList))
ds@579
   427
	{
ds@579
   428
		pList = new CmdList();
ds@579
   429
		pPlugin->SetProperty("CommandList", pList);
ds@579
   430
	}
ds@579
   431
	PlCmdInfo info;
ds@579
   432
	info.pInfo = pInfo;
ds@579
   433
	info.type = Cmd_Console;
ds@579
   434
	info.pHook = pHook;
ds@579
   435
	AddToPlCmdList(pList, info);
ds@579
   436
ds@579
   437
	return true;
ds@579
   438
}
ds@579
   439
ds@579
   440
bool ConCmdManager::AddAdminCommand(IPluginFunction *pFunction, 
ds@579
   441
									 const char *name, 
ds@579
   442
									 const char *group,
ds@579
   443
									 int adminflags,
ds@579
   444
									 const char *description, 
ds@579
   445
									 int flags)
ds@579
   446
{
ds@579
   447
	ConCmdInfo *pInfo = AddOrFindCommand(name, description, flags);
ds@579
   448
ds@579
   449
	if (!pInfo)
ds@579
   450
	{
ds@579
   451
		return false;
ds@579
   452
	}
ds@579
   453
ds@579
   454
	CmdHook *pHook = new CmdHook();
ds@579
   455
	AdminCmdInfo *pAdmin = new AdminCmdInfo();
ds@579
   456
ds@579
   457
	pHook->pf = pFunction;
ds@579
   458
	if (description && description[0])
ds@579
   459
	{
ds@579
   460
		pHook->helptext.assign(description);
ds@579
   461
	}
ds@579
   462
	pHook->pAdmin = pAdmin;
ds@579
   463
ds@579
   464
	void *object;
ds@579
   465
	int grpid;
ds@579
   466
	if (!sm_trie_retrieve(m_pCmdGrps, group, (void **)&object))
ds@579
   467
	{
ds@579
   468
		grpid = m_Strings.AddString(group);
ds@579
   469
		sm_trie_insert(m_pCmdGrps, group, (void *)grpid);
ds@579
   470
	} else {
ds@579
   471
		grpid = (int)object;
ds@579
   472
	}
ds@579
   473
ds@579
   474
	pAdmin->cmdGrpId = grpid;
ds@579
   475
	pAdmin->flags = adminflags;
ds@579
   476
ds@579
   477
	/* First get the command group override, if any */
ds@579
   478
	bool override = g_Admins.GetCommandOverride(group, 
ds@579
   479
		Override_CommandGroup,
ds@579
   480
		&(pAdmin->eflags));
ds@579
   481
ds@579
   482
	/* Next get the command override, if any */
ds@579
   483
	if (g_Admins.GetCommandOverride(name,
ds@579
   484
		Override_Command,
ds@579
   485
		&(pAdmin->eflags)))
ds@579
   486
	{
ds@579
   487
		override = true;
ds@579
   488
	}
ds@579
   489
ds@579
   490
	/* Assign normal flags if there were no overrides */
ds@579
   491
	if (!override)
ds@579
   492
	{
ds@579
   493
		pAdmin->eflags = pAdmin->flags;
ds@579
   494
	}
ds@579
   495
ds@579
   496
	/* Finally, add the hook */
ds@579
   497
	pInfo->conhooks.push_back(pHook);
ds@579
   498
ds@579
   499
	/* Now add to the plugin */
ds@579
   500
	CmdList *pList;
ds@579
   501
	IPlugin *pPlugin = g_PluginSys.GetPluginByCtx(pFunction->GetParentContext()->GetContext());
ds@579
   502
	if (!pPlugin->GetProperty("CommandList", (void **)&pList))
ds@579
   503
	{
ds@579
   504
		pList = new CmdList();
ds@579
   505
		pPlugin->SetProperty("CommandList", pList);
ds@579
   506
	}
ds@579
   507
	PlCmdInfo info;
ds@579
   508
	info.pInfo = pInfo;
ds@579
   509
	info.type = Cmd_Admin;
ds@579
   510
	info.pHook = pHook;
ds@579
   511
	AddToPlCmdList(pList, info);
ds@579
   512
ds@579
   513
	return true;
ds@579
   514
}
ds@579
   515
ds@579
   516
bool ConCmdManager::AddServerCommand(IPluginFunction *pFunction, 
ds@579
   517
									  const char *name, 
ds@579
   518
									  const char *description, 
ds@579
   519
									  int flags)
ds@579
   520
ds@579
   521
{
ds@579
   522
	ConCmdInfo *pInfo = AddOrFindCommand(name, description, flags);
ds@579
   523
ds@579
   524
	if (!pInfo)
ds@579
   525
	{
ds@579
   526
		return false;
ds@579
   527
	}
ds@579
   528
ds@579
   529
	CmdHook *pHook = new CmdHook();
ds@579
   530
ds@579
   531
	pHook->pf = pFunction;
ds@579
   532
	if (description && description[0])
ds@579
   533
	{
ds@579
   534
		pHook->helptext.assign(description);
ds@579
   535
	}
ds@579
   536
ds@579
   537
	pInfo->srvhooks.push_back(pHook);
ds@579
   538
ds@579
   539
	/* Add to the plugin */
ds@579
   540
	CmdList *pList;
ds@579
   541
	IPlugin *pPlugin = g_PluginSys.GetPluginByCtx(pFunction->GetParentContext()->GetContext());
ds@579
   542
	if (!pPlugin->GetProperty("CommandList", (void **)&pList))
ds@579
   543
	{
ds@579
   544
		pList = new CmdList();
ds@579
   545
		pPlugin->SetProperty("CommandList", pList);
ds@579
   546
	}
ds@579
   547
	PlCmdInfo info;
ds@579
   548
	info.pInfo = pInfo;
ds@579
   549
	info.type = Cmd_Server;
ds@579
   550
	info.pHook = pHook;
ds@579
   551
	AddToPlCmdList(pList, info);
ds@579
   552
ds@579
   553
	return true;
ds@579
   554
}
ds@579
   555
ds@579
   556
void AddToPlCmdList(CmdList *pList, const PlCmdInfo &info)
ds@579
   557
{
ds@579
   558
	CmdList::iterator iter = pList->begin();
ds@579
   559
	bool inserted = false;
ds@579
   560
	const char *orig = NULL;
ds@579
   561
ds@579
   562
	orig = info.pInfo->pCmd->GetName();
ds@579
   563
ds@579
   564
	/* Insert this into the help list, SORTED alphabetically. */
ds@579
   565
	while (iter != pList->end())
ds@579
   566
	{
ds@579
   567
		PlCmdInfo &obj = (*iter);
ds@579
   568
		const char *cmd = obj.pInfo->pCmd->GetName();
ds@579
   569
		if (strcmp(orig, cmd) < 0)
ds@579
   570
		{
ds@579
   571
			pList->insert(iter, info);
ds@579
   572
			inserted = true;
ds@579
   573
			break;
ds@579
   574
		}
ds@579
   575
		iter++;
ds@579
   576
	}
ds@579
   577
ds@579
   578
	if (!inserted)
ds@579
   579
	{
ds@579
   580
		pList->push_back(info);
ds@579
   581
	}
ds@579
   582
}
ds@579
   583
ds@579
   584
void ConCmdManager::AddToCmdList(ConCmdInfo *info)
ds@579
   585
{
ds@579
   586
	List<ConCmdInfo *>::iterator iter = m_CmdList.begin();
ds@579
   587
	ConCmdInfo *pInfo;
ds@579
   588
	bool inserted = false;
ds@579
   589
	const char *orig = NULL;
ds@579
   590
ds@579
   591
	orig = info->pCmd->GetName();
ds@579
   592
ds@579
   593
	/* Insert this into the help list, SORTED alphabetically. */
ds@579
   594
	while (iter != m_CmdList.end())
ds@579
   595
	{
ds@579
   596
		const char *cmd = NULL;
ds@579
   597
		pInfo = (*iter);
ds@579
   598
		cmd = pInfo->pCmd->GetName();
ds@579
   599
		if (strcmp(orig, cmd) < 0)
ds@579
   600
		{
ds@579
   601
			m_CmdList.insert(iter, info);
ds@579
   602
			inserted = true;
ds@579
   603
			break;
ds@579
   604
		}
ds@579
   605
		iter++;
ds@579
   606
	}
ds@579
   607
ds@579
   608
	if (!inserted)
ds@579
   609
	{
ds@579
   610
		m_CmdList.push_back(info);
ds@579
   611
	}
ds@579
   612
}
ds@579
   613
ds@579
   614
void ConCmdManager::UpdateAdminCmdFlags(const char *cmd, OverrideType type, FlagBits bits)
ds@579
   615
{
ds@579
   616
	ConCmdInfo *pInfo;
ds@579
   617
ds@579
   618
	if (type == Override_Command)
ds@579
   619
	{
ds@579
   620
		if (!sm_trie_retrieve(m_pCmds, cmd, (void **)&pInfo))
ds@579
   621
		{
ds@579
   622
			return;
ds@579
   623
		}
ds@579
   624
ds@579
   625
		List<CmdHook *>::iterator iter;
ds@579
   626
		CmdHook *pHook;
ds@579
   627
ds@579
   628
		for (iter=pInfo->conhooks.begin(); iter!=pInfo->conhooks.end(); iter++)
ds@579
   629
		{
ds@579
   630
			pHook = (*iter);
ds@579
   631
			if (pHook->pAdmin)
ds@579
   632
			{
ds@579
   633
				if (bits)
ds@579
   634
				{
ds@579
   635
					pHook->pAdmin->eflags = bits;
ds@579
   636
				} else {
ds@579
   637
					pHook->pAdmin->eflags = pHook->pAdmin->flags;
ds@579
   638
				}
ds@579
   639
			}
ds@579
   640
		}
ds@579
   641
	} else if (type == Override_CommandGroup) {
ds@579
   642
		void *object;
ds@579
   643
		if (!sm_trie_retrieve(m_pCmdGrps, cmd, &object))
ds@579
   644
		{
ds@579
   645
			return;
ds@579
   646
		}
ds@579
   647
		int grpid = (int)object;
ds@579
   648
ds@579
   649
		/* This is bad :( loop through all commands */
ds@579
   650
		List<ConCmdInfo *>::iterator iter;
ds@579
   651
		CmdHook *pHook;
ds@579
   652
		for (iter=m_CmdList.begin(); iter!=m_CmdList.end(); iter++)
ds@579
   653
		{
ds@579
   654
			pInfo = (*iter);
ds@579
   655
			for (List<CmdHook *>::iterator citer=pInfo->conhooks.begin();
ds@579
   656
				 citer!=pInfo->conhooks.end();
ds@579
   657
				 citer++)
ds@579
   658
			{
ds@579
   659
				pHook = (*citer);
ds@579
   660
				if (pHook->pAdmin && pHook->pAdmin->cmdGrpId == grpid)
ds@579
   661
				{
ds@579
   662
					if (bits)
ds@579
   663
					{
ds@579
   664
						pHook->pAdmin->eflags = bits;
ds@579
   665
					} else {
ds@579
   666
						pHook->pAdmin->eflags = pHook->pAdmin->flags;
ds@579
   667
					}
ds@579
   668
				}
ds@579
   669
			}
ds@579
   670
		}
ds@579
   671
	}
ds@579
   672
}
ds@579
   673
ds@579
   674
void ConCmdManager::RemoveConCmd(ConCmdInfo *info)
ds@579
   675
{
ds@579
   676
	/* Remove console-specific information
ds@579
   677
	 * This should always be true as of right now
ds@579
   678
	 */
ds@579
   679
	if (info->pCmd)
ds@579
   680
	{
ds@579
   681
		/* Remove from the trie */
ds@579
   682
		sm_trie_delete(m_pCmds, info->pCmd->GetName());
ds@579
   683
ds@579
   684
		if (info->sourceMod)
ds@579
   685
		{
ds@579
   686
			/* Unlink from SourceMM */
ds@579
   687
			g_SMAPI->UnregisterConCmdBase(g_PLAPI, info->pCmd);
ds@579
   688
			/* Delete the command's memory */
ds@579
   689
			char *new_help = const_cast<char *>(info->pCmd->GetHelpText());
ds@579
   690
			char *new_name = const_cast<char *>(info->pCmd->GetName());
ds@579
   691
			delete [] new_help;
ds@579
   692
			delete [] new_name;
ds@579
   693
			delete info->pCmd;
ds@579
   694
		} else {
ds@579
   695
			/* Remove the external hook */
ds@579
   696
			SH_REMOVE_HOOK_STATICFUNC(ConCommand, Dispatch, info->pCmd, CommandCallback, false);
ds@579
   697
		}
ds@579
   698
	}
ds@579
   699
	
ds@579
   700
	/* Remove from list */
ds@579
   701
	m_CmdList.remove(info);
ds@579
   702
ds@579
   703
	delete info;
ds@579
   704
}
ds@579
   705
dvander@893
   706
bool ConCmdManager::LookForSourceModCommand(const char *cmd)
dvander@893
   707
{
dvander@893
   708
	ConCmdInfo *pInfo;
dvander@893
   709
dvander@893
   710
	if (!sm_trie_retrieve(m_pCmds, cmd, (void **)&pInfo))
dvander@893
   711
	{
dvander@893
   712
		return false;
dvander@893
   713
	}
dvander@893
   714
dvander@893
   715
	return pInfo->sourceMod && (pInfo->conhooks.size() > 0);
dvander@893
   716
}
dvander@893
   717
ds@579
   718
ConCmdInfo *ConCmdManager::AddOrFindCommand(const char *name, const char *description, int flags)
ds@579
   719
{
ds@579
   720
	ConCmdInfo *pInfo;
ds@579
   721
	if (!sm_trie_retrieve(m_pCmds, name, (void **)&pInfo))
ds@579
   722
	{
ds@579
   723
		pInfo = new ConCmdInfo();
ds@579
   724
		/* Find the commandopan */
ds@579
   725
		ConCommandBase *pBase = icvar->GetCommands();
ds@579
   726
		ConCommand *pCmd = NULL;
ds@579
   727
		while (pBase)
ds@579
   728
		{
ds@579
   729
			if (strcmp(pBase->GetName(), name) == 0)
ds@579
   730
			{
ds@579
   731
				/* Don't want to return convar with same name */
ds@579
   732
				if (!pBase->IsCommand())
ds@579
   733
				{
ds@579
   734
					return NULL;
ds@579
   735
				}
ds@579
   736
ds@579
   737
				pCmd = (ConCommand *)pBase;
ds@579
   738
				break;
ds@579
   739
			}
ds@579
   740
			pBase = const_cast<ConCommandBase *>(pBase->GetNext());
ds@579
   741
		}
ds@579
   742
ds@579
   743
		if (!pCmd)
ds@579
   744
		{
ds@579
   745
			/* Note that we have to duplicate because the source might not be 
ds@579
   746
			 * a static string, and these expect static memory.
ds@579
   747
			 */
ds@579
   748
			if (!description)
ds@579
   749
			{
ds@579
   750
				description = "";
ds@579
   751
			}
ds@579
   752
			char *new_name = sm_strdup(name);
ds@579
   753
			char *new_help = sm_strdup(description);
ds@579
   754
			pCmd = new ConCommand(new_name, CommandCallback, new_help, flags);
ds@579
   755
			pInfo->sourceMod = true;
ds@579
   756
		} else {
ds@579
   757
			SH_ADD_HOOK_STATICFUNC(ConCommand, Dispatch, pCmd, CommandCallback, false);
ds@579
   758
		}
ds@579
   759
ds@579
   760
		pInfo->pCmd = pCmd;
ds@579
   761
ds@579
   762
		sm_trie_insert(m_pCmds, name, pInfo);
ds@579
   763
		AddToCmdList(pInfo);
ds@579
   764
	}
ds@579
   765
ds@579
   766
	return pInfo;
ds@579
   767
}
ds@579
   768
ds@579
   769
void ConCmdManager::OnRootConsoleCommand(const char *command, unsigned int argcount)
ds@579
   770
{
ds@579
   771
	if (argcount >= 3)
ds@579
   772
	{
ds@579
   773
		const char *text = engine->Cmd_Argv(2);
dvander@723
   774
ds@579
   775
		int id = atoi(text);
ds@579
   776
		CPlugin *pPlugin = g_PluginSys.GetPluginByOrder(id);
ds@579
   777
ds@579
   778
		if (!pPlugin)
ds@579
   779
		{
ds@579
   780
			g_RootMenu.ConsolePrint("[SM] Plugin index %d not found.", id);
ds@579
   781
			return;
ds@579
   782
		}
ds@579
   783
ds@582
   784
		const sm_plugininfo_t *plinfo = pPlugin->GetPublicInfo();
ds@582
   785
		const char *plname = IS_STR_FILLED(plinfo->name) ? plinfo->name : pPlugin->GetFilename();
ds@582
   786
ds@579
   787
		CmdList *pList;
ds@579
   788
		if (!pPlugin->GetProperty("CommandList", (void **)&pList))
ds@579
   789
		{
ds@582
   790
			g_RootMenu.ConsolePrint("[SM] No commands found for: %s", plname);
ds@579
   791
			return;
ds@579
   792
		}
ds@579
   793
		if (!pList->size())
ds@579
   794
		{
ds@582
   795
			g_RootMenu.ConsolePrint("[SM] No commands found for: %s", plname);
ds@579
   796
			return;
ds@579
   797
		}
ds@579
   798
ds@579
   799
		CmdList::iterator iter;
ds@579
   800
		const char *type = NULL;
ds@579
   801
		const char *name;
ds@579
   802
		const char *help;
ds@582
   803
		g_RootMenu.ConsolePrint("[SM] Listing %d commands for: %s", pList->size(), plname);
ds@582
   804
		g_RootMenu.ConsolePrint("  %-17.16s %-8.7s %s", "[Name]", "[Type]", "[Help]");
ds@579
   805
		for (iter=pList->begin();
ds@579
   806
			 iter!=pList->end();
ds@582
   807
			 iter++, id++)
ds@579
   808
		{
ds@579
   809
			PlCmdInfo &cmd = (*iter);
ds@579
   810
			if (cmd.type == Cmd_Server)
ds@579
   811
			{
ds@579
   812
				type = "server";
ds@579
   813
			} else if (cmd.type == Cmd_Console) {
ds@579
   814
				type = "console";
ds@579
   815
			} else if (cmd.type == Cmd_Admin) {
ds@579
   816
				type = "admin";
ds@579
   817
			}
ds@579
   818
			name = cmd.pInfo->pCmd->GetName();
ds@579
   819
			if (cmd.pHook->helptext.size())
ds@579
   820
			{
ds@579
   821
				help = cmd.pHook->helptext.c_str();
ds@579
   822
			} else {
ds@579
   823
				help = cmd.pInfo->pCmd->GetHelpText();
ds@579
   824
			}
ds@582
   825
			g_RootMenu.ConsolePrint("  %-17.16s %-12.11s %s", name, type, help);		
ds@579
   826
		}
ds@579
   827
ds@579
   828
		return;
ds@579
   829
	}
ds@579
   830
ds@579
   831
	g_RootMenu.ConsolePrint("[SM] Usage: sm cmds <plugin #>");
ds@579
   832
}
dvander@614
   833
dvander@614
   834
static int g_yam_state = 0;
dvander@614
   835
void _YamState(int state)
dvander@614
   836
{
dvander@614
   837
	if (state == 0)
dvander@614
   838
	{
dvander@614
   839
		g_RootMenu.ConsolePrint("Welcome to the SourceMod Text Adventure.");
dvander@614
   840
		g_RootMenu.ConsolePrint("Type sm_text to see the last message.");
dvander@614
   841
		g_RootMenu.ConsolePrint("Type sm_text <word> to follow a capital word.");
dvander@614
   842
		g_RootMenu.ConsolePrint("-------------------------------");
dvander@614
   843
		g_RootMenu.ConsolePrint("You are at VALVE HEADQUARTERS.");
dvander@614
   844
		g_RootMenu.ConsolePrint("To your LEFT is BAILOPAN, rearranging the letters to spell");
dvander@614
   845
		g_RootMenu.ConsolePrint("\"A HARD VEAL QUEST\".  FORWARD is the entrance to the building.");
dvander@614
   846
		g_RootMenu.ConsolePrint("To your RIGHT is your last chance to flee in terror.");
dvander@614
   847
	} else if (state == 1) {
dvander@614
   848
		g_RootMenu.ConsolePrint("BAILOPAN tells you that his name his pronounced");
dvander@614
   849
		g_RootMenu.ConsolePrint("\"bye low pahn,\" not \"bay low pan.\"  Do you ");
dvander@614
   850
		g_RootMenu.ConsolePrint("MOCK him, or NOD quietly?");
dvander@614
   851
	} else if (state == 2) {
dvander@614
   852
		g_RootMenu.ConsolePrint("You enter the Valve building.  You hear screams coming from within.");
dvander@614
   853
		g_RootMenu.ConsolePrint("A grotesque figure lumbers up to greet you; it is Gabe Newell.");
dvander@614
   854
		g_RootMenu.ConsolePrint("\"Welcome,\" he belches, \"to my lair.\"");
dvander@614
   855
		g_RootMenu.ConsolePrint("Do you SHAKE Gaben's hand, WALK past him, or OFFER a donut?");
dvander@614
   856
	} else if (state == 3) {
dvander@614
   857
		g_RootMenu.ConsolePrint("You walk into the break room.  Alfred \"Adolf\" Reynolds and");
dvander@614
   858
		g_RootMenu.ConsolePrint("Yahn \"Yeti\" Bernier are discussing something (you overhear ");
dvander@614
   859
		g_RootMenu.ConsolePrint("the phrase \"and next Steam update, here's what we should break\").");
dvander@614
   860
		g_RootMenu.ConsolePrint("Should you DIE in a fire, REPORT a bug, REQUEST a feature, or ");
dvander@614
   861
		g_RootMenu.ConsolePrint("SPRAY them with butter?");
dvander@614
   862
	}
dvander@614
   863
	g_yam_state = state;
dvander@614
   864
}
dvander@614
   865
dvander@614
   866
void _IntExt_CallYams()
dvander@614
   867
{
dvander@614
   868
	const char *arg = engine->Cmd_Argv(1);
dvander@614
   869
dvander@614
   870
	/* should be impossible */
dvander@614
   871
	if (!arg || arg[0] == '\0')
dvander@614
   872
	{
dvander@614
   873
		_YamState(g_yam_state);
dvander@614
   874
	}
dvander@614
   875
dvander@614
   876
	if (g_yam_state == 1)
dvander@614
   877
	{
dvander@614
   878
		if (strcasecmp(arg, "mock") == 0)
dvander@614
   879
		{
ds@729
   880
			g_RootMenu.ConsolePrint("You mock BAILOPAN's pronunciation.  In a fit of rage, ");
dvander@614
   881
			g_RootMenu.ConsolePrint("he sticks an INT 3 call into your chest, rendering you broken.");
dvander@614
   882
			g_RootMenu.ConsolePrint("YOU HAVE DIED.  GAME OVER.");
dvander@614
   883
			g_yam_state = 0;
dvander@614
   884
		} else if (strcasecmp(arg, "nod") == 0) {
dvander@614
   885
			g_RootMenu.ConsolePrint("You nod quietly, and then slowly back away into the Valve headquarters.");
dvander@614
   886
			_YamState(2);
dvander@614
   887
		} else {
dvander@614
   888
			g_RootMenu.ConsolePrint("Commands are MOCK and NOD.");
dvander@614
   889
		}
dvander@614
   890
	} else if (g_yam_state == 3) {
dvander@614
   891
		if (strcasecmp(arg, "report") == 0)
dvander@614
   892
		{
dvander@614
   893
			g_RootMenu.ConsolePrint("You report a bug to Alfred and Yeti.  Immediately, both fall asleep.");
dvander@614
   894
			g_RootMenu.ConsolePrint("You decay in the break room for two years while they sleep.");
dvander@614
   895
			g_RootMenu.ConsolePrint("YOU HAVE DIED.  GAME OVER.");
dvander@614
   896
			g_yam_state = 0;
dvander@614
   897
		} else if (strcasecmp(arg, "request") == 0) {
dvander@614
   898
			g_RootMenu.ConsolePrint("You request a feature to Alfred and Yeti.  They both mutter something");
dvander@614
   899
			g_RootMenu.ConsolePrint("about it being implemented \"soon.\"  Then, by accident, someone sends");
dvander@614
   900
			g_RootMenu.ConsolePrint("a message over \"Friends.\"  The entire building catches fire.");
dvander@614
   901
			g_RootMenu.ConsolePrint("YOU HAVE DIED.  GAME OVER.");
dvander@614
   902
			g_yam_state = 0;
dvander@614
   903
		} else if (strcasecmp(arg, "die") == 0) {
dvander@614
   904
			g_RootMenu.ConsolePrint("For no reason, you suddenly catch fire.  Alfred and Yeti find this");
dvander@614
   905
			g_RootMenu.ConsolePrint("deeply disturbing, and cover your flaming corpse with Episode 2");
dvander@617
   906
			g_RootMenu.ConsolePrint("advertisements.  Coming soon, with Team Fortress 2, and Portal!");
dvander@614
   907
			g_RootMenu.ConsolePrint("YOU HAVE DIED.  GAME OVER.");
dvander@614
   908
			g_yam_state = 0;
dvander@614
   909
		} else if (strcasecmp(arg, "spray") == 0) {
dvander@614
   910
			g_RootMenu.ConsolePrint("You spray Alfred and Yeti with butter.  Like Jack Thompson to an");
dvander@614
   911
			g_RootMenu.ConsolePrint("ambulance, Gabe Newell instantly appears and devours them both.");
dvander@614
   912
			g_RootMenu.ConsolePrint("You run away, just in time, as Gabe Newell explodes, registering ");
dvander@614
   913
			g_RootMenu.ConsolePrint("a 5.6 earthquake.  Outside, world peace is achieved.");
dvander@614
   914
			g_RootMenu.ConsolePrint("YOU HAVE WON.");
dvander@614
   915
			g_yam_state = 0;
dvander@614
   916
		}
dvander@614
   917
	} else if (g_yam_state == 2) {
dvander@614
   918
		if (strcasecmp(arg, "shake") == 0)
dvander@614
   919
		{
dvander@614
   920
			g_RootMenu.ConsolePrint("You shake Gaben's hand.  It is a terrifying and disgusting experience.");
dvander@614
   921
			g_RootMenu.ConsolePrint("However, you survive, and continue on.");
dvander@614
   922
			_YamState(3);
dvander@614
   923
		} else if (strcasecmp(arg, "offer") == 0) {
dvander@614
   924
			g_RootMenu.ConsolePrint("You offer Gabe Newell one (1) donut.  With a gleam in his eyes, ");
dvander@614
   925
			g_RootMenu.ConsolePrint("he picks you up and devours you whole.");
dvander@614
   926
			g_RootMenu.ConsolePrint("YOU HAVE DIED.  GAME OVER.");
dvander@614
   927
			g_yam_state = 0;
dvander@614
   928
		} else if (strcasecmp(arg, "walk") == 0) {
dvander@614
   929
			g_RootMenu.ConsolePrint("You walk past Gabe Newell.  He can't keep up with your pace!");
dvander@614
   930
			_YamState(3);
dvander@614
   931
		} else {
dvander@614
   932
			g_RootMenu.ConsolePrint("Commands are SHAKE, OFFER, and WALK.");
dvander@614
   933
		}
dvander@614
   934
	} else if (g_yam_state == 0) {
dvander@614
   935
		if (strcasecmp(arg, "left") == 0)
dvander@614
   936
		{
dvander@614
   937
			_YamState(1);
dvander@614
   938
		} else if (strcasecmp(arg, "right") == 0) {
dvander@614
   939
			g_RootMenu.ConsolePrint("You run away from the Valve headquarters in sheer terror.");
dvander@614
   940
			g_RootMenu.ConsolePrint("While running, you smash into an unknown person, who turns out to be your soul mate.");
dvander@614
   941
			g_RootMenu.ConsolePrint("You marry and raise a family of 3 kids.");
dvander@614
   942
			g_RootMenu.ConsolePrint("Many years later, you look back, and realize this was your best choice.");
dvander@614
   943
			g_RootMenu.ConsolePrint("YOU HAVE WON.");
dvander@614
   944
			g_yam_state = 0;
dvander@614
   945
		} else if (strcasecmp(arg, "forward") == 0) {
dvander@614
   946
			_YamState(2);
dvander@614
   947
		} else if (arg[0] != '\0') {
dvander@614
   948
			g_RootMenu.ConsolePrint("Commands are FORWARD, LEFT, and RIGHT.");
dvander@614
   949
		}
dvander@614
   950
	}
dvander@614
   951
}
dvander@614
   952
dvander@614
   953
void _IntExt_EnableYams()
dvander@614
   954
{
dvander@614
   955
	static ConCommand *pCmd = NULL;
dvander@614
   956
	if (!pCmd)
dvander@614
   957
	{
dvander@614
   958
		pCmd = new ConCommand("sm_text", _IntExt_CallYams, "Fountain of Yams Adventure Game", FCVAR_GAMEDLL);
dvander@614
   959
		g_RootMenu.ConsolePrint("Something is now enabled...");
dvander@614
   960
	} else {
dvander@614
   961
		g_RootMenu.ConsolePrint("Something is already enabled...");
dvander@614
   962
	}
dvander@614
   963
}