core/smn_console.cpp
author Phong Dang <teame06@gmail.com>
Wed Aug 01 07:29:40 2007 +0000 (2007-08-01)
changeset 1241 2e8046793bc9
parent 1231 17badaf66ce4
child 1273 9afc766f8e9c
permissions -rw-r--r--
Added check to make sure player are alive to perform sm_slap, sm_burn, sm_slay.
     1 /**
     2  * vim: set ts=4 :
     3  * ================================================================
     4  * SourceMod
     5  * Copyright (C) 2004-2007 AlliedModders LLC.  All rights reserved.
     6  * ================================================================
     7  *
     8  * This program is free software; you can redistribute it and/or 
     9  * modify it under the terms of the GNU General Public License, 
    10  * version 3.0, as published by the Free Software Foundation.
    11  * 
    12  * This program is distributed in the hope that it will be useful,
    13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
    14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    15  * GNU General Public License for more details.
    16  *
    17  * You should have received a copy of the GNU General Public License
    18  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
    19  *
    20  * As a special exception, AlliedModders LLC gives you permission to 
    21  * link the code of this program (as well as its derivative works) to 
    22  * "Half-Life 2," the "Source Engine," the "SourcePawn JIT," and any 
    23  * Game MODs that run on software by the Valve Corporation.  You must 
    24  * obey the GNU General Public License in all respects for all other 
    25  * code used. Additionally, AlliedModders LLC grants this exception 
    26  * to all derivative works. AlliedModders LLC defines further 
    27  * exceptions, found in LICENSE.txt (as of this writing, version 
    28  * JULY-31-2007), or <http://www.sourcemod.net/license.php>.
    29  *
    30  * Version: $Id$
    31  */
    32 
    33 #include "sm_globals.h"
    34 #include "HalfLife2.h"
    35 #include "sourcemm_api.h"
    36 #include "HandleSys.h"
    37 #include "ConVarManager.h"
    38 #include "ConCmdManager.h"
    39 #include "PluginSys.h"
    40 #include "sm_stringutil.h"
    41 #include "PlayerManager.h"
    42 #include "ChatTriggers.h"
    43 #include <inetchannel.h>
    44 #include <bitbuf.h>
    45 
    46 #define NET_SETCONVAR	5
    47 
    48 enum ConVarBounds
    49 {
    50 	ConVarBound_Upper = 0,
    51 	ConVarBound_Lower
    52 };
    53 
    54 HandleType_t hCmdIterType = 0;
    55 
    56 struct GlobCmdIter
    57 {
    58 	bool started;
    59 	List<ConCmdInfo *>::iterator iter;
    60 };
    61 
    62 class ConsoleHelpers : 
    63 	public SMGlobalClass,
    64 	public IHandleTypeDispatch
    65 {
    66 public:
    67 	virtual void OnSourceModAllInitialized()
    68 	{
    69 		HandleAccess access;
    70 		g_HandleSys.InitAccessDefaults(NULL, &access);
    71 		access.access[HandleAccess_Clone] = HANDLE_RESTRICT_OWNER | HANDLE_RESTRICT_IDENTITY;
    72 
    73 		hCmdIterType = g_HandleSys.CreateType(NULL, this, 0, NULL, &access, g_pCoreIdent, NULL);
    74 	}
    75 	virtual void OnHandleDestroy(HandleType_t type, void *object)
    76 	{
    77 		GlobCmdIter *iter = (GlobCmdIter *)object;
    78 		delete iter;
    79 	}
    80 } s_ConsoleHelpers;
    81 
    82 static void ReplicateConVar(ConVar *pConVar)
    83 {
    84 	int maxClients = g_Players.GetMaxClients();
    85 
    86 	char data[256];
    87 	bf_write buffer(data, sizeof(data));
    88 
    89 	buffer.WriteUBitLong(NET_SETCONVAR, 5);
    90 	buffer.WriteByte(1);
    91 	buffer.WriteString(pConVar->GetName());
    92 	buffer.WriteString(pConVar->GetString());
    93 
    94 	for (int i = 1; i <= maxClients; i++)
    95 	{
    96 		CPlayer *pPlayer = g_Players.GetPlayerByIndex(i);
    97 
    98 		if (pPlayer && pPlayer->IsInGame() && !pPlayer->IsFakeClient())
    99 		{
   100 			INetChannel *netchan = static_cast<INetChannel *>(engine->GetPlayerNetInfo(i));
   101 			netchan->SendData(buffer);
   102 		}
   103 	}
   104 }
   105 
   106 static void NotifyConVar(ConVar *pConVar)
   107 {
   108 	IGameEvent *pEvent = gameevents->CreateEvent("server_cvar");
   109 	pEvent->SetString("cvarname", pConVar->GetName());
   110 	if (pConVar->IsBitSet(FCVAR_PROTECTED))
   111 	{
   112 		pEvent->SetString("cvarvalue", "***PROTECTED***");
   113 	} else {
   114 		pEvent->SetString("cvarvalue", pConVar->GetString());
   115 	}
   116 
   117 	gameevents->FireEvent(pEvent);
   118 }
   119 
   120 static cell_t sm_CreateConVar(IPluginContext *pContext, const cell_t *params)
   121 {
   122 	char *name, *defaultVal, *helpText;
   123 
   124 	pContext->LocalToString(params[1], &name);
   125 
   126 	// While the engine seems to accept a blank convar name, it causes a crash upon server quit
   127 	if (name == NULL || strcmp(name, "") == 0)
   128 	{
   129 		return pContext->ThrowNativeError("Convar with blank name is not permitted");
   130 	}
   131 
   132 	pContext->LocalToString(params[2], &defaultVal);
   133 	pContext->LocalToString(params[3], &helpText);
   134 
   135 	bool hasMin = params[5] ? true : false;
   136 	bool hasMax = params[7] ? true : false;
   137 	float min = sp_ctof(params[6]);
   138 	float max = sp_ctof(params[8]);
   139 
   140 	Handle_t hndl = g_ConVarManager.CreateConVar(pContext, name, defaultVal, helpText, params[4], hasMin, min, hasMax, max);
   141 
   142 	if (hndl == BAD_HANDLE)
   143 	{
   144 		return pContext->ThrowNativeError("Convar \"%s\" was not created. A console command with the same name already exists.", name);
   145 	}
   146 
   147 	return hndl;
   148 }
   149 
   150 static cell_t sm_FindConVar(IPluginContext *pContext, const cell_t *params)
   151 {
   152 	char *name;
   153 
   154 	pContext->LocalToString(params[1], &name);
   155 
   156 	return g_ConVarManager.FindConVar(name);
   157 }
   158 
   159 static cell_t sm_HookConVarChange(IPluginContext *pContext, const cell_t *params)
   160 {
   161 	Handle_t hndl = static_cast<Handle_t>(params[1]);
   162 	HandleError err;
   163 	ConVar *pConVar;
   164 
   165 	if ((err=g_HandleSys.ReadHandle(hndl, g_ConVarManager.GetHandleType(), NULL, (void **)&pConVar))
   166 		!= HandleError_None)
   167 	{
   168 		return pContext->ThrowNativeError("Invalid convar handle %x (error %d)", hndl, err);
   169 	}
   170 
   171 	IPluginFunction *pFunction = pContext->GetFunctionById(params[2]);
   172 
   173 	if (!pFunction)
   174 	{
   175 		return pContext->ThrowNativeError("Invalid function id (%X)", params[2]);
   176 	}
   177 
   178 	g_ConVarManager.HookConVarChange(pConVar, pFunction);
   179 
   180 	return 1;
   181 }
   182 
   183 static cell_t sm_UnhookConVarChange(IPluginContext *pContext, const cell_t *params)
   184 {
   185 	Handle_t hndl = static_cast<Handle_t>(params[1]);
   186 	HandleError err;
   187 	ConVar *pConVar;
   188 
   189 	if ((err=g_HandleSys.ReadHandle(hndl, g_ConVarManager.GetHandleType(), NULL, (void **)&pConVar))
   190 		!= HandleError_None)
   191 	{
   192 		return pContext->ThrowNativeError("Invalid convar handle %x (error %d)", hndl, err);
   193 	}
   194 
   195 	IPluginFunction *pFunction = pContext->GetFunctionById(params[2]);
   196 
   197 	if (!pFunction)
   198 	{
   199 		return pContext->ThrowNativeError("Invalid function id (%X)", params[2]);
   200 	}
   201 
   202 	g_ConVarManager.UnhookConVarChange(pConVar, pFunction);
   203 
   204 	return 1;
   205 }
   206 
   207 static cell_t sm_GetConVarBool(IPluginContext *pContext, const cell_t *params)
   208 {
   209 	Handle_t hndl = static_cast<Handle_t>(params[1]);
   210 	HandleError err;
   211 	ConVar *pConVar;
   212 
   213 	if ((err=g_HandleSys.ReadHandle(hndl, g_ConVarManager.GetHandleType(), NULL, (void **)&pConVar))
   214 		!= HandleError_None)
   215 	{
   216 		return pContext->ThrowNativeError("Invalid convar handle %x (error %d)", hndl, err);
   217 	}
   218 
   219 	return pConVar->GetBool();
   220 }
   221 
   222 static cell_t sm_GetConVarInt(IPluginContext *pContext, const cell_t *params)
   223 {
   224 	Handle_t hndl = static_cast<Handle_t>(params[1]);
   225 	HandleError err;
   226 	ConVar *pConVar;
   227 
   228 	if ((err=g_HandleSys.ReadHandle(hndl, g_ConVarManager.GetHandleType(), NULL, (void **)&pConVar))
   229 		!= HandleError_None)
   230 	{
   231 		return pContext->ThrowNativeError("Invalid convar handle %x (error %d)", hndl, err);
   232 	}
   233 
   234 	return pConVar->GetInt();
   235 }
   236 
   237 /* This handles both SetConVarBool() and SetConVarInt() */
   238 static cell_t sm_SetConVarNum(IPluginContext *pContext, const cell_t *params)
   239 {
   240 	Handle_t hndl = static_cast<Handle_t>(params[1]);
   241 	HandleError err;
   242 	ConVar *pConVar;
   243 
   244 	if ((err=g_HandleSys.ReadHandle(hndl, g_ConVarManager.GetHandleType(), NULL, (void **)&pConVar))
   245 		!= HandleError_None)
   246 	{
   247 		return pContext->ThrowNativeError("Invalid convar handle %x (error %d)", hndl, err);
   248 	}
   249 
   250 	pConVar->SetValue(params[2]);
   251 
   252 	/* Should we replicate it? */
   253 	if (params[3] && pConVar->IsBitSet(FCVAR_REPLICATED))
   254 	{
   255 		ReplicateConVar(pConVar);
   256 	}
   257 
   258 	/* Should we notify clients? */
   259 	if (params[4] && pConVar->IsBitSet(FCVAR_NOTIFY))
   260 	{
   261 		NotifyConVar(pConVar);
   262 	}
   263 
   264 	return 1;
   265 }
   266 
   267 static cell_t sm_GetConVarFloat(IPluginContext *pContext, const cell_t *params)
   268 {
   269 	Handle_t hndl = static_cast<Handle_t>(params[1]);
   270 	HandleError err;
   271 	ConVar *pConVar;
   272 
   273 	if ((err=g_HandleSys.ReadHandle(hndl, g_ConVarManager.GetHandleType(), NULL, (void **)&pConVar))
   274 		!= HandleError_None)
   275 	{
   276 		return pContext->ThrowNativeError("Invalid convar handle %x (error %d)", hndl, err);
   277 	}
   278 	
   279 	float value = pConVar->GetFloat();
   280 
   281 	return sp_ftoc(value);
   282 }
   283 
   284 static cell_t sm_SetConVarFloat(IPluginContext *pContext, const cell_t *params)
   285 {
   286 	Handle_t hndl = static_cast<Handle_t>(params[1]);
   287 	HandleError err;
   288 	ConVar *pConVar;
   289 
   290 	if ((err=g_HandleSys.ReadHandle(hndl, g_ConVarManager.GetHandleType(), NULL, (void **)&pConVar))
   291 		!= HandleError_None)
   292 	{
   293 		return pContext->ThrowNativeError("Invalid convar handle %x (error %d)", hndl, err);
   294 	}
   295 
   296 	float value = sp_ctof(params[2]);
   297 	pConVar->SetValue(value);
   298 
   299 	/* Should we replicate it? */
   300 	if (params[3] && pConVar->IsBitSet(FCVAR_REPLICATED))
   301 	{
   302 		ReplicateConVar(pConVar);
   303 	}
   304 
   305 	/* Should we notify clients? */
   306 	if (params[4] && pConVar->IsBitSet(FCVAR_NOTIFY))
   307 	{
   308 		NotifyConVar(pConVar);
   309 	}
   310 
   311 	return 1;
   312 }
   313 
   314 static cell_t sm_GetConVarString(IPluginContext *pContext, const cell_t *params)
   315 {
   316 	Handle_t hndl = static_cast<Handle_t>(params[1]);
   317 	HandleError err;
   318 	ConVar *pConVar;
   319 
   320 	if ((err=g_HandleSys.ReadHandle(hndl, g_ConVarManager.GetHandleType(), NULL, (void **)&pConVar))
   321 		!= HandleError_None)
   322 	{
   323 		return pContext->ThrowNativeError("Invalid convar handle %x (error %d)", hndl, err);
   324 	}
   325 
   326 	pContext->StringToLocalUTF8(params[2], params[3], pConVar->GetString(), NULL);
   327 
   328 	return 1;
   329 }
   330 
   331 static cell_t sm_SetConVarString(IPluginContext *pContext, const cell_t *params)
   332 {
   333 	Handle_t hndl = static_cast<Handle_t>(params[1]);
   334 	HandleError err;
   335 	ConVar *pConVar;
   336 
   337 	if ((err=g_HandleSys.ReadHandle(hndl, g_ConVarManager.GetHandleType(), NULL, (void **)&pConVar))
   338 		!= HandleError_None)
   339 	{
   340 		return pContext->ThrowNativeError("Invalid convar handle %x (error %d)", hndl, err);
   341 	}
   342 
   343 	char *value;
   344 	pContext->LocalToString(params[2], &value);
   345 
   346 	pConVar->SetValue(value);
   347 
   348 	/* Should we replicate it? */
   349 	if (params[3] && pConVar->IsBitSet(FCVAR_REPLICATED))
   350 	{
   351 		ReplicateConVar(pConVar);
   352 	}
   353 
   354 	/* Should we notify clients? */
   355 	if (params[4] && pConVar->IsBitSet(FCVAR_NOTIFY))
   356 	{
   357 		NotifyConVar(pConVar);
   358 	}
   359 
   360 	return 1;
   361 }
   362 
   363 static cell_t sm_GetConVarFlags(IPluginContext *pContext, const cell_t *params)
   364 {
   365 	Handle_t hndl = static_cast<Handle_t>(params[1]);
   366 	HandleError err;
   367 	ConVar *pConVar;
   368 
   369 	if ((err=g_HandleSys.ReadHandle(hndl, g_ConVarManager.GetHandleType(), NULL, (void **)&pConVar))
   370 		!= HandleError_None)
   371 	{
   372 		return pContext->ThrowNativeError("Invalid convar handle %x (error %d)", hndl, err);
   373 	}
   374 
   375 	return pConVar->GetFlags();
   376 }
   377 
   378 static cell_t sm_SetConVarFlags(IPluginContext *pContext, const cell_t *params)
   379 {
   380 	Handle_t hndl = static_cast<Handle_t>(params[1]);
   381 	HandleError err;
   382 	ConVar *pConVar;
   383 
   384 	if ((err=g_HandleSys.ReadHandle(hndl, g_ConVarManager.GetHandleType(), NULL, (void **)&pConVar))
   385 		!= HandleError_None)
   386 	{
   387 		return pContext->ThrowNativeError("Invalid convar handle %x (error %d)", hndl, err);
   388 	}
   389 
   390 	pConVar->SetFlags(params[2]);
   391 
   392 	return 1;
   393 }
   394 
   395 static cell_t sm_GetConVarBounds(IPluginContext *pContext, const cell_t *params)
   396 {
   397 	Handle_t hndl = static_cast<Handle_t>(params[1]);
   398 	HandleError err;
   399 	ConVar *pConVar;
   400 
   401 	if ((err=g_HandleSys.ReadHandle(hndl, g_ConVarManager.GetHandleType(), NULL, (void **)&pConVar))
   402 		!= HandleError_None)
   403 	{
   404 		return pContext->ThrowNativeError("Invalid convar handle %x (error %d)", hndl, err);
   405 	}
   406 
   407 	cell_t *addr;
   408 	bool hasBound;
   409 	float bound;
   410 
   411 	switch (params[2])
   412 	{
   413 	case ConVarBound_Upper:
   414 		hasBound = pConVar->GetMax(bound);
   415 		break;
   416 	case ConVarBound_Lower:
   417 		hasBound = pConVar->GetMin(bound);
   418 		break;
   419 	default:
   420 		return pContext->ThrowNativeError("Invalid ConVarBounds value %d");
   421 	}
   422 	
   423 	pContext->LocalToPhysAddr(params[3], &addr);
   424 	*addr = sp_ftoc(bound);
   425 
   426 	return hasBound;
   427 }
   428 
   429 static cell_t sm_SetConVarBounds(IPluginContext *pContext, const cell_t *params)
   430 {
   431 	Handle_t hndl = static_cast<Handle_t>(params[1]);
   432 	HandleError err;
   433 	ConVar *pConVar;
   434 
   435 	if ((err=g_HandleSys.ReadHandle(hndl, g_ConVarManager.GetHandleType(), NULL, (void **)&pConVar))
   436 		!= HandleError_None)
   437 	{
   438 		return pContext->ThrowNativeError("Invalid convar handle %x (error %d)", hndl, err);
   439 	}
   440 
   441 	switch (params[2])
   442 	{
   443 	case ConVarBound_Upper:
   444 		pConVar->SetMax(params[3] ? true : false, sp_ctof(params[4]));
   445 		break;
   446 	case ConVarBound_Lower:
   447 		pConVar->SetMin(params[3] ? true : false, sp_ctof(params[4]));
   448 		break;
   449 	default:
   450 		return pContext->ThrowNativeError("Invalid ConVarBounds value %d");
   451 	}
   452 
   453 	return 1;
   454 }
   455 
   456 static cell_t sm_GetConVarName(IPluginContext *pContext, const cell_t *params)
   457 {
   458 	Handle_t hndl = static_cast<Handle_t>(params[1]);
   459 	HandleError err;
   460 	ConVar *pConVar;
   461 
   462 	if ((err=g_HandleSys.ReadHandle(hndl, g_ConVarManager.GetHandleType(), NULL, (void **)&pConVar))
   463 		!= HandleError_None)
   464 	{
   465 		return pContext->ThrowNativeError("Invalid convar handle %x (error %d)", hndl, err);
   466 	}
   467 
   468 	pContext->StringToLocalUTF8(params[2], params[3], pConVar->GetName(), NULL);
   469 
   470 	return 1;
   471 }
   472 
   473 static cell_t sm_ResetConVar(IPluginContext *pContext, const cell_t *params)
   474 {
   475 	Handle_t hndl = static_cast<Handle_t>(params[1]);
   476 	HandleError err;
   477 	ConVar *pConVar;
   478 
   479 	if ((err=g_HandleSys.ReadHandle(hndl, g_ConVarManager.GetHandleType(), NULL, (void **)&pConVar))
   480 		!= HandleError_None)
   481 	{
   482 		return pContext->ThrowNativeError("Invalid convar handle %x (error %d)", hndl, err);
   483 	}
   484 
   485 	pConVar->Revert();
   486 
   487 	return 1;
   488 }
   489 
   490 static bool s_QueryAlreadyWarned = false;
   491 
   492 static cell_t sm_QueryClientConVar(IPluginContext *pContext, const cell_t *params)
   493 {
   494 	CPlayer *pPlayer;
   495 	char *name;
   496 	IPluginFunction *pCallback;
   497 
   498 	if (g_IsOriginalEngine)
   499 	{
   500 		if (!s_QueryAlreadyWarned)
   501 		{
   502 			s_QueryAlreadyWarned = true;
   503 			return pContext->ThrowNativeError("Game does not support client convar querying (one time warning)");
   504 		}
   505 
   506 		return 0;
   507 	}
   508 
   509 	pPlayer = g_Players.GetPlayerByIndex(params[1]);
   510 
   511 	if (!pPlayer)
   512 	{
   513 		return pContext->ThrowNativeError("Client index %d is invalid", params[1]);
   514 	}
   515 
   516 	if (!pPlayer->IsConnected())
   517 	{
   518 		return pContext->ThrowNativeError("Client %d is not connected", params[1]);
   519 	}
   520 
   521 	/* Trying a query on a bot results in callback not be fired, so don't bother */
   522 	if (pPlayer->IsFakeClient())
   523 	{
   524 		return 0;
   525 	}
   526 
   527 	pContext->LocalToString(params[2], &name);
   528 	pCallback = pContext->GetFunctionById(params[3]);
   529 
   530 	if (!pCallback)
   531 	{
   532 		return pContext->ThrowNativeError("Invalid function id (%X)", params[3]);
   533 	}
   534 
   535 	return g_ConVarManager.QueryClientConVar(pPlayer->GetEdict(), name, pCallback, params[4]);
   536 }
   537 
   538 static cell_t sm_RegServerCmd(IPluginContext *pContext, const cell_t *params)
   539 {
   540 	char *name,*help;
   541 	IPluginFunction *pFunction;
   542 
   543 	pContext->LocalToString(params[1], &name);
   544 	pContext->LocalToString(params[3], &help);
   545 	pFunction = pContext->GetFunctionById(params[2]);
   546 
   547 	if (!pFunction)
   548 	{
   549 		return pContext->ThrowNativeError("Invalid function id (%X)", params[2]);
   550 	}
   551 
   552 	if (!g_ConCmds.AddServerCommand(pFunction, name, help, params[4]))
   553 	{
   554 		return pContext->ThrowNativeError("Command \"%s\" could not be created. A convar with the same name already exists.", name);
   555 	}
   556 
   557 	return 1;
   558 }
   559 
   560 static cell_t sm_RegConsoleCmd(IPluginContext *pContext, const cell_t *params)
   561 {
   562 	char *name,*help;
   563 	IPluginFunction *pFunction;
   564 
   565 	pContext->LocalToString(params[1], &name);
   566 	pContext->LocalToString(params[3], &help);
   567 	pFunction = pContext->GetFunctionById(params[2]);
   568 
   569 	if (!pFunction)
   570 	{
   571 		return pContext->ThrowNativeError("Invalid function id (%X)", params[2]);
   572 	}
   573 
   574 	if (!g_ConCmds.AddConsoleCommand(pFunction, name, help, params[4]))
   575 	{
   576 		return pContext->ThrowNativeError("Command \"%s\" could not be created. A convar with the same name already exists.", name);
   577 	}
   578 
   579 	return 1;
   580 }
   581 
   582 static cell_t sm_RegAdminCmd(IPluginContext *pContext, const cell_t *params)
   583 {
   584 	char *name,*help;
   585 	const char *group;
   586 	IPluginFunction *pFunction;
   587 	FlagBits flags = params[3];
   588 	int cmdflags = params[6];
   589 
   590 	pContext->LocalToString(params[1], &name);
   591 	pContext->LocalToString(params[4], &help);
   592 	pContext->LocalToString(params[5], (char **)&group);
   593 	pFunction = pContext->GetFunctionById(params[2]);
   594 
   595 	if (group[0] == '\0')
   596 	{
   597 		CPlugin *pPlugin = g_PluginSys.GetPluginByCtx(pContext->GetContext());
   598 		group = pPlugin->GetFilename();
   599 	}
   600 
   601 	if (!pFunction)
   602 	{
   603 		return pContext->ThrowNativeError("Invalid function id (%X)", params[2]);
   604 	}
   605 
   606 	if (!g_ConCmds.AddAdminCommand(pFunction, name, group, flags, help, cmdflags))
   607 	{
   608 		return pContext->ThrowNativeError("Command \"%s\" could not be created. A convar with the same name already exists.", name);
   609 	}
   610 
   611 	return 1;
   612 }
   613 
   614 static cell_t sm_GetCmdArgs(IPluginContext *pContext, const cell_t *params)
   615 {
   616 	return engine->Cmd_Argc() - 1;
   617 }
   618 
   619 static cell_t sm_GetCmdArg(IPluginContext *pContext, const cell_t *params)
   620 {
   621 	const char *arg = engine->Cmd_Argv(params[1]);
   622 	
   623 	pContext->StringToLocalUTF8(params[2], params[3], arg, NULL);
   624 
   625 	return 1;
   626 }
   627 
   628 static cell_t sm_GetCmdArgString(IPluginContext *pContext, const cell_t *params)
   629 {
   630 	const char *args = engine->Cmd_Args();
   631 
   632 	if (!args)
   633 	{
   634 		args = "";
   635 	}
   636 
   637 	pContext->StringToLocalUTF8(params[1], params[2], args, NULL);
   638 
   639 	return 1;
   640 }
   641 
   642 static cell_t sm_PrintToServer(IPluginContext *pCtx, const cell_t *params)
   643 {
   644 	char buffer[1024];
   645 	char *fmt;
   646 	int arg = 2;
   647 
   648 	pCtx->LocalToString(params[1], &fmt);
   649 	size_t res = atcprintf(buffer, sizeof(buffer)-2, fmt, pCtx, params, &arg);
   650 
   651 	buffer[res++] = '\n';
   652 	buffer[res] = '\0';
   653 
   654 	META_CONPRINT(buffer);
   655 
   656 	return 1;
   657 }
   658 
   659 static cell_t sm_PrintToConsole(IPluginContext *pCtx, const cell_t *params)
   660 {
   661 	int index = params[1];
   662 	if ((index < 0) || (index > g_Players.GetMaxClients()))
   663 	{
   664 		return pCtx->ThrowNativeError("Client index %d is invalid", index);
   665 	}
   666 
   667 	CPlayer *pPlayer = NULL;
   668 	if (index != 0)
   669 	{
   670 		pPlayer = g_Players.GetPlayerByIndex(index);
   671 		if (!pPlayer->IsInGame())
   672 		{
   673 			return pCtx->ThrowNativeError("Client %d is not in game", index);
   674 		}
   675 		
   676 		/* Silent fail on bots, engine will crash */
   677 		if (pPlayer->IsFakeClient())
   678 		{
   679 			return 0;
   680 		}
   681 	}
   682 
   683 	char buffer[1024];
   684 	char *fmt;
   685 	int arg = 3;
   686 
   687 	pCtx->LocalToString(params[2], &fmt);
   688 	size_t res = atcprintf(buffer, sizeof(buffer)-2, fmt, pCtx, params, &arg);
   689 
   690 	buffer[res++] = '\n';
   691 	buffer[res] = '\0';
   692 
   693 	if (index != 0)
   694 	{
   695 		engine->ClientPrintf(pPlayer->GetEdict(), buffer);
   696 	} else {
   697 		META_CONPRINT(buffer);
   698 	}
   699 
   700 	return 1;
   701 }
   702 
   703 static cell_t sm_ServerCommand(IPluginContext *pContext, const cell_t *params)
   704 {
   705 	char buffer[1024];
   706 	size_t len = g_SourceMod.FormatString(buffer, sizeof(buffer)-2, pContext, params, 1);
   707 
   708 	g_SourceMod.SetGlobalTarget(LANG_SERVER);
   709 
   710 	/* One byte for null terminator, one for newline */
   711 	buffer[len++] = '\n';
   712 	buffer[len] = '\0';
   713 
   714 	engine->ServerCommand(buffer);
   715 
   716 	return 1;
   717 }
   718 
   719 static cell_t sm_InsertServerCommand(IPluginContext *pContext, const cell_t *params)
   720 {
   721 	char buffer[1024];
   722 	size_t len = g_SourceMod.FormatString(buffer, sizeof(buffer)-2, pContext, params, 1);
   723 
   724 	g_SourceMod.SetGlobalTarget(LANG_SERVER);
   725 
   726 	/* One byte for null terminator, one for newline */
   727 	buffer[len++] = '\n';
   728 	buffer[len] = '\0';
   729 
   730 	engine->InsertServerCommand(buffer);
   731 
   732 	return 1;
   733 }
   734 
   735 static cell_t sm_ServerExecute(IPluginContext *pContext, const cell_t *params)
   736 {
   737 	engine->ServerExecute();
   738 
   739 	return 1;
   740 }
   741 
   742 static cell_t sm_ClientCommand(IPluginContext *pContext, const cell_t *params)
   743 {
   744 	CPlayer *pPlayer = g_Players.GetPlayerByIndex(params[1]);
   745 
   746 	if (!pPlayer)
   747 	{
   748 		return pContext->ThrowNativeError("Client index %d is invalid", params[1]);
   749 	}
   750 
   751 	if (!pPlayer->IsConnected())
   752 	{
   753 		return pContext->ThrowNativeError("Client %d is not connected", params[1]);
   754 	}
   755 
   756 	g_SourceMod.SetGlobalTarget(params[1]);
   757 
   758 	char buffer[256];
   759 	g_SourceMod.FormatString(buffer, sizeof(buffer), pContext, params, 2);
   760 
   761 	engine->ClientCommand(pPlayer->GetEdict(), "%s", buffer);
   762 
   763 	return 1;
   764 }
   765 
   766 
   767 static cell_t FakeClientCommand(IPluginContext *pContext, const cell_t *params)
   768 {
   769 	CPlayer *pPlayer = g_Players.GetPlayerByIndex(params[1]);
   770 
   771 	if (!pPlayer)
   772 	{
   773 		return pContext->ThrowNativeError("Client index %d is invalid", params[1]);
   774 	}
   775 
   776 	if (!pPlayer->IsConnected())
   777 	{
   778 		return pContext->ThrowNativeError("Client %d is not connected", params[1]);
   779 	}
   780 
   781 	char buffer[256];
   782 	g_SourceMod.FormatString(buffer, sizeof(buffer), pContext, params, 2);
   783 
   784 	unsigned int old = g_ChatTriggers.SetReplyTo(SM_REPLY_CONSOLE);
   785 	serverpluginhelpers->ClientCommand(pPlayer->GetEdict(), buffer);
   786 	g_ChatTriggers.SetReplyTo(old);
   787 
   788 	return 1;
   789 }
   790 
   791 static cell_t ReplyToCommand(IPluginContext *pContext, const cell_t *params)
   792 {
   793 	g_SourceMod.SetGlobalTarget(params[1]);
   794 
   795 	/* Build the format string */
   796 	char buffer[1024];
   797 	size_t len = g_SourceMod.FormatString(buffer, sizeof(buffer)-2, pContext, params, 2);
   798 
   799 	/* If we're printing to the server, shortcut out */
   800 	if (params[1] == 0)
   801 	{
   802 		/* Print */
   803 		buffer[len++] = '\n';
   804 		buffer[len] = '\0';
   805 		META_CONPRINT(buffer);
   806 		return 1;
   807 	}
   808 
   809 	CPlayer *pPlayer = g_Players.GetPlayerByIndex(params[1]);
   810 
   811 	if (!pPlayer)
   812 	{
   813 		return pContext->ThrowNativeError("Client index %d is invalid", params[1]);
   814 	}
   815 
   816 	if (!pPlayer->IsConnected())
   817 	{
   818 		return pContext->ThrowNativeError("Client %d is not connected", params[1]);
   819 	}
   820 
   821 	g_SourceMod.SetGlobalTarget(params[1]);
   822 
   823 	unsigned int replyto = g_ChatTriggers.GetReplyTo();
   824 	if (replyto == SM_REPLY_CONSOLE)
   825 	{
   826 		buffer[len++] = '\n';
   827 		buffer[len] = '\0';
   828 		engine->ClientPrintf(pPlayer->GetEdict(), buffer);
   829 	} else if (replyto == SM_REPLY_CHAT) {
   830 		if (len >= 191)
   831 		{
   832 			len = 191;
   833 		}
   834 		buffer[len] = '\0';
   835 		g_HL2.TextMsg(params[1], HUD_PRINTTALK, buffer);
   836 	}
   837 
   838 	return 1;
   839 }
   840 
   841 static cell_t GetCmdReplyTarget(IPluginContext *pContext, const cell_t *params)
   842 {
   843 	return g_ChatTriggers.GetReplyTo();
   844 }
   845 
   846 static cell_t SetCmdReplyTarget(IPluginContext *pContext, const cell_t *params)
   847 {
   848 	return g_ChatTriggers.SetReplyTo(params[1]);
   849 }
   850 
   851 static cell_t GetCommandIterator(IPluginContext *pContext, const cell_t *params)
   852 {
   853 	GlobCmdIter *iter = new GlobCmdIter;
   854 	iter->started = false;
   855 
   856 	Handle_t hndl = g_HandleSys.CreateHandle(hCmdIterType, iter, pContext->GetIdentity(), g_pCoreIdent, NULL);
   857 	if (hndl == BAD_HANDLE)
   858 	{
   859 		delete iter;
   860 	}
   861 
   862 	return hndl;
   863 }
   864 
   865 static cell_t ReadCommandIterator(IPluginContext *pContext, const cell_t *params)
   866 {
   867 	GlobCmdIter *iter;
   868 	HandleError err;
   869 	HandleSecurity sec(pContext->GetIdentity(), g_pCoreIdent);
   870 
   871 	if ((err = g_HandleSys.ReadHandle(params[1], hCmdIterType, &sec, (void **)&iter))
   872 		!= HandleError_None)
   873 	{
   874 		return pContext->ThrowNativeError("Invalid GlobCmdIter Handle %x", params[1]);
   875 	}
   876 
   877 	const List<ConCmdInfo *> &cmds = g_ConCmds.GetCommandList();
   878 
   879 	if (!iter->started)
   880 	{
   881 		iter->iter = cmds.begin();
   882 		iter->started = true;
   883 	}
   884 
   885 	while (iter->iter != cmds.end()
   886 			&& !(*(iter->iter))->sourceMod)
   887 	{
   888 		iter->iter++;
   889 	}
   890 
   891 	if (iter->iter == cmds.end())
   892 	{
   893 		return 0;
   894 	}
   895 
   896 	ConCmdInfo *pInfo = (*(iter->iter));
   897 
   898 	pContext->StringToLocalUTF8(params[2], params[3], pInfo->pCmd->GetName(), NULL);
   899 	pContext->StringToLocalUTF8(params[5], params[6], pInfo->pCmd->GetHelpText(), NULL);
   900 	
   901 	cell_t *addr;
   902 	pContext->LocalToPhysAddr(params[4], &addr);
   903 	*addr = pInfo->admin.eflags;
   904 
   905 	iter->iter++;
   906 
   907 	return 1;
   908 }
   909 
   910 static cell_t CheckCommandAccess(IPluginContext *pContext, const cell_t *params)
   911 {
   912 	if (params[1] == 0)
   913 	{
   914 		return 1;
   915 	}
   916 
   917 	char *cmd;
   918 	pContext->LocalToString(params[2], &cmd);
   919 
   920 	return g_ConCmds.CheckCommandAccess(params[1], cmd, params[3]) ? 1 : 0;
   921 }
   922 
   923 REGISTER_NATIVES(consoleNatives)
   924 {
   925 	{"CreateConVar",		sm_CreateConVar},
   926 	{"FindConVar",			sm_FindConVar},
   927 	{"HookConVarChange",	sm_HookConVarChange},
   928 	{"UnhookConVarChange",	sm_UnhookConVarChange},
   929 	{"GetConVarBool",		sm_GetConVarBool},
   930 	{"SetConVarBool",		sm_SetConVarNum},
   931 	{"GetConVarInt",		sm_GetConVarInt},
   932 	{"SetConVarInt",		sm_SetConVarNum},
   933 	{"GetConVarFloat",		sm_GetConVarFloat},
   934 	{"SetConVarFloat",		sm_SetConVarFloat},
   935 	{"GetConVarString",		sm_GetConVarString},
   936 	{"SetConVarString",		sm_SetConVarString},
   937 	{"GetConVarFlags",		sm_GetConVarFlags},
   938 	{"SetConVarFlags",		sm_SetConVarFlags},
   939 	{"GetConVarName",		sm_GetConVarName},
   940 	{"GetConVarBounds",		sm_GetConVarBounds},
   941 	{"SetConVarBounds",		sm_SetConVarBounds},
   942 	{"ResetConVar",			sm_ResetConVar},
   943 	{"QueryClientConVar",	sm_QueryClientConVar},
   944 	{"RegServerCmd",		sm_RegServerCmd},
   945 	{"RegConsoleCmd",		sm_RegConsoleCmd},
   946 	{"GetCmdArgString",		sm_GetCmdArgString},
   947 	{"GetCmdArgs",			sm_GetCmdArgs},
   948 	{"GetCmdArg",			sm_GetCmdArg},
   949 	{"PrintToServer",		sm_PrintToServer},
   950 	{"PrintToConsole",		sm_PrintToConsole},
   951 	{"RegAdminCmd",			sm_RegAdminCmd},
   952 	{"ServerCommand",		sm_ServerCommand},
   953 	{"InsertServerCommand",	sm_InsertServerCommand},
   954 	{"ServerExecute",		sm_ServerExecute},
   955 	{"ClientCommand",		sm_ClientCommand},
   956 	{"FakeClientCommand",	FakeClientCommand},
   957 	{"ReplyToCommand",		ReplyToCommand},
   958 	{"GetCmdReplySource",	GetCmdReplyTarget},
   959 	{"SetCmdReplySource",	SetCmdReplyTarget},
   960 	{"GetCommandIterator",	GetCommandIterator},
   961 	{"ReadCommandIterator",	ReadCommandIterator},
   962 	{"CheckCommandAccess",	CheckCommandAccess},
   963 	{NULL,					NULL}
   964 };