Removed -dev tag for release.
3 * =============================================================================
5 * Copyright (C) 2004-2008 AlliedModders LLC. All rights reserved.
6 * =============================================================================
8 * This program is free software; you can redistribute it and/or modify it under
9 * the terms of the GNU General Public License, version 3.0, as published by the
10 * Free Software Foundation.
12 * This program is distributed in the hope that it will be useful, but WITHOUT
13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
17 * You should have received a copy of the GNU General Public License along with
18 * this program. If not, see <http://www.gnu.org/licenses/>.
20 * As a special exception, AlliedModders LLC gives you permission to link the
21 * code of this program (as well as its derivative works) to "Half-Life 2," the
22 * "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
23 * by the Valve Corporation. You must obey the GNU General Public License in
24 * all respects for all other code used. Additionally, AlliedModders LLC grants
25 * this exception to all derivative works. AlliedModders LLC defines further
26 * exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
27 * or <http://www.sourcemod.net/license.php>.
32 #include "sm_globals.h"
33 #include "HalfLife2.h"
34 #include "sourcemm_api.h"
35 #include "HandleSys.h"
36 #include "ConVarManager.h"
37 #include "ConCmdManager.h"
38 #include "PluginSys.h"
39 #include "sm_stringutil.h"
40 #include "PlayerManager.h"
41 #include "ChatTriggers.h"
42 #include "AdminCache.h"
43 #include <inetchannel.h>
45 #include <sm_trie_tpl.h>
46 #include "TagsSystem.h"
48 #if SOURCE_ENGINE == SE_LEFT4DEAD
49 #define NET_SETCONVAR 6
51 #define NET_SETCONVAR 5
56 ConVarBound_Upper = 0,
60 HandleType_t hCmdIterType = 0;
61 HandleType_t htConCmdIter = 0;
66 List<ConCmdInfo *>::iterator iter;
71 #if SOURCE_ENGINE == SE_LEFT4DEAD
72 ICvarIteratorInternal *pLast;
74 const ConCommandBase *pLast;
78 class ConsoleHelpers :
80 public IHandleTypeDispatch
83 virtual void OnSourceModAllInitialized()
87 g_HandleSys.InitAccessDefaults(NULL, &access);
89 htConCmdIter = g_HandleSys.CreateType("ConCmdIter", this, 0, NULL, &access, g_pCoreIdent, NULL);
91 access.access[HandleAccess_Clone] = HANDLE_RESTRICT_OWNER | HANDLE_RESTRICT_IDENTITY;
93 hCmdIterType = g_HandleSys.CreateType("CmdIter", this, 0, NULL, &access, g_pCoreIdent, NULL);
95 virtual void OnHandleDestroy(HandleType_t type, void *object)
97 if (type == hCmdIterType)
99 GlobCmdIter *iter = (GlobCmdIter *)object;
102 else if (type == htConCmdIter)
104 ConCmdIter *iter = (ConCmdIter * )object;
105 #if SOURCE_ENGINE == SE_LEFT4DEAD
106 // ICvarIteratorInternal has no virtual destructor
107 g_pMemAlloc->Free(iter->pLast);
112 virtual bool GetHandleApproxSize(HandleType_t type, void *object, unsigned int *pSize)
114 if (type == htConCmdIter)
116 *pSize = sizeof(ConCmdIter);
119 else if (type == hCmdIterType)
121 *pSize = sizeof(GlobCmdIter);
128 class CommandFlagsHelper : public IConCommandTracker
131 void OnUnlinkConCommandBase(ConCommandBase *pBase, const char *name, bool is_read_safe)
133 m_CmdFlags.remove(name);
135 bool GetFlags(const char *name, int *flags)
137 ConCommandBase **ppCmd;
138 ConCommandBase *pCmd;
139 if ((ppCmd=m_CmdFlags.retrieve(name)))
141 TrackConCommandBase((*ppCmd), this);
142 *flags = (*ppCmd)->GetFlags();
145 else if ((pCmd=FindCommandBase(name)))
147 m_CmdFlags.insert(name, pCmd);
148 TrackConCommandBase(pCmd, this);
149 *flags = pCmd->GetFlags();
157 bool SetFlags(const char *name, int flags)
159 ConCommandBase **ppCmd;
160 ConCommandBase *pCmd;
161 if ((ppCmd=m_CmdFlags.retrieve(name)))
163 (*ppCmd)->SetFlags(flags);
164 TrackConCommandBase((*ppCmd), this);
167 else if ((pCmd=FindCommandBase(name)))
169 m_CmdFlags.insert(name, pCmd);
170 pCmd->SetFlags(flags);
171 TrackConCommandBase(pCmd, this);
180 KTrie<ConCommandBase *> m_CmdFlags;
181 } s_CommandFlagsHelper;
183 #if SOURCE_ENGINE >= SE_ORANGEBOX
184 static void ReplicateConVar(ConVar *pConVar)
186 int maxClients = g_Players.GetMaxClients();
189 bf_write buffer(data, sizeof(data));
191 buffer.WriteUBitLong(NET_SETCONVAR, 5);
193 buffer.WriteString(pConVar->GetName());
194 buffer.WriteString(pConVar->GetString());
196 for (int i = 1; i <= maxClients; i++)
198 CPlayer *pPlayer = g_Players.GetPlayerByIndex(i);
200 if (pPlayer && pPlayer->IsInGame() && !pPlayer->IsFakeClient())
202 INetChannel *netchan = static_cast<INetChannel *>(engine->GetPlayerNetInfo(i));
203 netchan->SendData(buffer);
208 static void NotifyConVar(ConVar *pConVar)
210 IGameEvent *pEvent = gameevents->CreateEvent("server_cvar");
211 pEvent->SetString("cvarname", pConVar->GetName());
212 if (IsFlagSet(pConVar, FCVAR_PROTECTED))
214 pEvent->SetString("cvarvalue", "***PROTECTED***");
218 pEvent->SetString("cvarvalue", pConVar->GetString());
221 gameevents->FireEvent(pEvent);
225 static cell_t sm_CreateConVar(IPluginContext *pContext, const cell_t *params)
227 char *name, *defaultVal, *helpText;
229 pContext->LocalToString(params[1], &name);
231 // While the engine seems to accept a blank convar name, it causes a crash upon server quit
232 if (name == NULL || strcmp(name, "") == 0)
234 return pContext->ThrowNativeError("Convar with blank name is not permitted");
237 pContext->LocalToString(params[2], &defaultVal);
238 pContext->LocalToString(params[3], &helpText);
240 bool hasMin = params[5] ? true : false;
241 bool hasMax = params[7] ? true : false;
242 float min = sp_ctof(params[6]);
243 float max = sp_ctof(params[8]);
245 Handle_t hndl = g_ConVarManager.CreateConVar(pContext, name, defaultVal, helpText, params[4], hasMin, min, hasMax, max);
247 if (hndl == BAD_HANDLE)
249 return pContext->ThrowNativeError("Convar \"%s\" was not created. A console command with the same might already exist.", name);
255 static cell_t sm_FindConVar(IPluginContext *pContext, const cell_t *params)
259 pContext->LocalToString(params[1], &name);
261 return g_ConVarManager.FindConVar(name);
264 static cell_t sm_HookConVarChange(IPluginContext *pContext, const cell_t *params)
266 Handle_t hndl = static_cast<Handle_t>(params[1]);
270 if ((err=g_ConVarManager.ReadConVarHandle(hndl, &pConVar))
273 return pContext->ThrowNativeError("Invalid convar handle %x (error %d)", hndl, err);
276 IPluginFunction *pFunction = pContext->GetFunctionById(params[2]);
280 return pContext->ThrowNativeError("Invalid function id (%X)", params[2]);
283 g_ConVarManager.HookConVarChange(pConVar, pFunction);
288 static cell_t sm_UnhookConVarChange(IPluginContext *pContext, const cell_t *params)
290 Handle_t hndl = static_cast<Handle_t>(params[1]);
294 if ((err=g_ConVarManager.ReadConVarHandle(hndl, &pConVar))
297 return pContext->ThrowNativeError("Invalid convar handle %x (error %d)", hndl, err);
300 IPluginFunction *pFunction = pContext->GetFunctionById(params[2]);
304 return pContext->ThrowNativeError("Invalid function id (%X)", params[2]);
307 g_ConVarManager.UnhookConVarChange(pConVar, pFunction);
312 static cell_t sm_GetConVarBool(IPluginContext *pContext, const cell_t *params)
314 Handle_t hndl = static_cast<Handle_t>(params[1]);
318 if ((err=g_ConVarManager.ReadConVarHandle(hndl, &pConVar))
321 return pContext->ThrowNativeError("Invalid convar handle %x (error %d)", hndl, err);
324 return pConVar->GetBool();
327 static cell_t sm_GetConVarInt(IPluginContext *pContext, const cell_t *params)
329 Handle_t hndl = static_cast<Handle_t>(params[1]);
333 if ((err=g_ConVarManager.ReadConVarHandle(hndl, &pConVar))
336 return pContext->ThrowNativeError("Invalid convar handle %x (error %d)", hndl, err);
339 return pConVar->GetInt();
342 /* This handles both SetConVarBool() and SetConVarInt() */
343 static cell_t sm_SetConVarNum(IPluginContext *pContext, const cell_t *params)
345 Handle_t hndl = static_cast<Handle_t>(params[1]);
349 if ((err=g_ConVarManager.ReadConVarHandle(hndl, &pConVar))
352 return pContext->ThrowNativeError("Invalid convar handle %x (error %d)", hndl, err);
355 pConVar->SetValue(params[2]);
357 #if SOURCE_ENGINE >= SE_ORANGEBOX
358 /* Should we replicate it? */
359 if (params[3] && IsFlagSet(pConVar, FCVAR_REPLICATED))
361 ReplicateConVar(pConVar);
364 /* Should we notify clients? */
365 if (params[4] && IsFlagSet(pConVar, FCVAR_NOTIFY))
367 NotifyConVar(pConVar);
374 static cell_t sm_GetConVarFloat(IPluginContext *pContext, const cell_t *params)
376 Handle_t hndl = static_cast<Handle_t>(params[1]);
380 if ((err=g_ConVarManager.ReadConVarHandle(hndl, &pConVar))
383 return pContext->ThrowNativeError("Invalid convar handle %x (error %d)", hndl, err);
386 float value = pConVar->GetFloat();
388 return sp_ftoc(value);
391 static cell_t sm_SetConVarFloat(IPluginContext *pContext, const cell_t *params)
393 Handle_t hndl = static_cast<Handle_t>(params[1]);
397 if ((err=g_ConVarManager.ReadConVarHandle(hndl, &pConVar))
400 return pContext->ThrowNativeError("Invalid convar handle %x (error %d)", hndl, err);
403 float value = sp_ctof(params[2]);
404 pConVar->SetValue(value);
406 #if SOURCE_ENGINE >= SE_ORANGEBOX
407 /* Should we replicate it? */
408 if (params[3] && IsFlagSet(pConVar, FCVAR_REPLICATED))
410 ReplicateConVar(pConVar);
413 /* Should we notify clients? */
414 if (params[4] && IsFlagSet(pConVar, FCVAR_NOTIFY))
416 NotifyConVar(pConVar);
423 static cell_t sm_GetConVarString(IPluginContext *pContext, const cell_t *params)
425 Handle_t hndl = static_cast<Handle_t>(params[1]);
429 if ((err=g_ConVarManager.ReadConVarHandle(hndl, &pConVar))
432 return pContext->ThrowNativeError("Invalid convar handle %x (error %d)", hndl, err);
435 pContext->StringToLocalUTF8(params[2], params[3], pConVar->GetString(), NULL);
440 static cell_t sm_SetConVarString(IPluginContext *pContext, const cell_t *params)
442 Handle_t hndl = static_cast<Handle_t>(params[1]);
446 if ((err=g_ConVarManager.ReadConVarHandle(hndl, &pConVar))
449 return pContext->ThrowNativeError("Invalid convar handle %x (error %d)", hndl, err);
453 pContext->LocalToString(params[2], &value);
455 pConVar->SetValue(value);
457 #if SOURCE_ENGINE >= SE_ORANGEBOX
458 /* Should we replicate it? */
459 if (params[3] && IsFlagSet(pConVar, FCVAR_REPLICATED))
461 ReplicateConVar(pConVar);
464 /* Should we notify clients? */
465 if (params[4] && IsFlagSet(pConVar, FCVAR_NOTIFY))
467 NotifyConVar(pConVar);
474 static cell_t sm_ResetConVar(IPluginContext *pContext, const cell_t *params)
476 Handle_t hndl = static_cast<Handle_t>(params[1]);
480 if ((err=g_ConVarManager.ReadConVarHandle(hndl, &pConVar))
483 return pContext->ThrowNativeError("Invalid convar handle %x (error %d)", hndl, err);
488 #if SOURCE_ENGINE >= SE_ORANGEBOX
489 /* Should we replicate it? */
490 if (params[3] && IsFlagSet(pConVar, FCVAR_REPLICATED))
492 ReplicateConVar(pConVar);
495 /* Should we notify clients? */
496 if (params[4] && IsFlagSet(pConVar, FCVAR_NOTIFY))
498 NotifyConVar(pConVar);
505 static cell_t sm_GetConVarFlags(IPluginContext *pContext, const cell_t *params)
507 Handle_t hndl = static_cast<Handle_t>(params[1]);
511 if ((err=g_ConVarManager.ReadConVarHandle(hndl, &pConVar))
514 return pContext->ThrowNativeError("Invalid convar handle %x (error %d)", hndl, err);
517 return pConVar->GetFlags();
520 static cell_t sm_SetConVarFlags(IPluginContext *pContext, const cell_t *params)
522 Handle_t hndl = static_cast<Handle_t>(params[1]);
526 if ((err=g_ConVarManager.ReadConVarHandle(hndl, &pConVar))
529 return pContext->ThrowNativeError("Invalid convar handle %x (error %d)", hndl, err);
532 pConVar->SetFlags(params[2]);
537 static cell_t sm_GetConVarBounds(IPluginContext *pContext, const cell_t *params)
539 Handle_t hndl = static_cast<Handle_t>(params[1]);
543 if ((err=g_ConVarManager.ReadConVarHandle(hndl, &pConVar))
546 return pContext->ThrowNativeError("Invalid convar handle %x (error %d)", hndl, err);
555 case ConVarBound_Upper:
556 hasBound = pConVar->GetMax(bound);
558 case ConVarBound_Lower:
559 hasBound = pConVar->GetMin(bound);
562 return pContext->ThrowNativeError("Invalid ConVarBounds value %d");
565 pContext->LocalToPhysAddr(params[3], &addr);
566 *addr = sp_ftoc(bound);
571 static cell_t sm_SetConVarBounds(IPluginContext *pContext, const cell_t *params)
573 Handle_t hndl = static_cast<Handle_t>(params[1]);
577 if ((err=g_ConVarManager.ReadConVarHandle(hndl, &pConVar))
580 return pContext->ThrowNativeError("Invalid convar handle %x (error %d)", hndl, err);
585 case ConVarBound_Upper:
586 pConVar->SetMax(params[3] ? true : false, sp_ctof(params[4]));
588 case ConVarBound_Lower:
589 pConVar->SetMin(params[3] ? true : false, sp_ctof(params[4]));
592 return pContext->ThrowNativeError("Invalid ConVarBounds value %d");
598 static cell_t sm_GetConVarName(IPluginContext *pContext, const cell_t *params)
600 Handle_t hndl = static_cast<Handle_t>(params[1]);
604 if ((err=g_ConVarManager.ReadConVarHandle(hndl, &pConVar))
607 return pContext->ThrowNativeError("Invalid convar handle %x (error %d)", hndl, err);
610 pContext->StringToLocalUTF8(params[2], params[3], pConVar->GetName(), NULL);
615 static bool s_QueryAlreadyWarned = false;
617 static cell_t sm_QueryClientConVar(IPluginContext *pContext, const cell_t *params)
621 IPluginFunction *pCallback;
623 if (!g_ConVarManager.IsQueryingSupported())
625 if (!s_QueryAlreadyWarned)
627 s_QueryAlreadyWarned = true;
628 return pContext->ThrowNativeError("Game does not support client convar querying (one time warning)");
634 pPlayer = g_Players.GetPlayerByIndex(params[1]);
638 return pContext->ThrowNativeError("Client index %d is invalid", params[1]);
641 if (!pPlayer->IsConnected())
643 return pContext->ThrowNativeError("Client %d is not connected", params[1]);
646 /* Trying a query on a bot results in callback not be fired, so don't bother */
647 if (pPlayer->IsFakeClient())
652 pContext->LocalToString(params[2], &name);
653 pCallback = pContext->GetFunctionById(params[3]);
657 return pContext->ThrowNativeError("Invalid function id (%X)", params[3]);
660 return g_ConVarManager.QueryClientConVar(pPlayer->GetEdict(), name, pCallback, params[4]);
663 static cell_t sm_RegServerCmd(IPluginContext *pContext, const cell_t *params)
666 IPluginFunction *pFunction;
668 pContext->LocalToString(params[1], &name);
669 pContext->LocalToString(params[3], &help);
670 pFunction = pContext->GetFunctionById(params[2]);
674 return pContext->ThrowNativeError("Invalid function id (%X)", params[2]);
677 if (!g_ConCmds.AddServerCommand(pFunction, name, help, params[4]))
679 return pContext->ThrowNativeError("Command \"%s\" could not be created. A convar with the same name already exists.", name);
685 static cell_t sm_RegConsoleCmd(IPluginContext *pContext, const cell_t *params)
688 IPluginFunction *pFunction;
690 pContext->LocalToString(params[1], &name);
691 pContext->LocalToString(params[3], &help);
692 pFunction = pContext->GetFunctionById(params[2]);
696 return pContext->ThrowNativeError("Invalid function id (%X)", params[2]);
699 if (!g_ConCmds.AddConsoleCommand(pFunction, name, help, params[4]))
701 return pContext->ThrowNativeError("Command \"%s\" could not be created. A convar with the same name already exists.", name);
707 static cell_t sm_RegAdminCmd(IPluginContext *pContext, const cell_t *params)
711 IPluginFunction *pFunction;
712 FlagBits flags = params[3];
713 int cmdflags = params[6];
715 pContext->LocalToString(params[1], &name);
716 pContext->LocalToString(params[4], &help);
717 pContext->LocalToString(params[5], (char **)&group);
718 pFunction = pContext->GetFunctionById(params[2]);
720 if (group[0] == '\0')
722 CPlugin *pPlugin = g_PluginSys.GetPluginByCtx(pContext->GetContext());
723 group = pPlugin->GetFilename();
728 return pContext->ThrowNativeError("Invalid function id (%X)", params[2]);
731 if (!g_ConCmds.AddAdminCommand(pFunction, name, group, flags, help, cmdflags))
733 return pContext->ThrowNativeError("Command \"%s\" could not be created. A convar with the same name already exists.", name);
739 static cell_t sm_GetCmdArgs(IPluginContext *pContext, const cell_t *params)
741 const CCommand *pCmd = g_HL2.PeekCommandStack();
745 return pContext->ThrowNativeError("No command callback available");
748 return pCmd->ArgC() - 1;
751 static cell_t sm_GetCmdArg(IPluginContext *pContext, const cell_t *params)
753 const CCommand *pCmd = g_HL2.PeekCommandStack();
757 return pContext->ThrowNativeError("No command callback available");
762 const char *arg = pCmd->Arg(params[1]);
764 pContext->StringToLocalUTF8(params[2], params[3], arg ? arg : "", &length);
766 return (cell_t)length;
769 static cell_t sm_GetCmdArgString(IPluginContext *pContext, const cell_t *params)
771 const CCommand *pCmd = g_HL2.PeekCommandStack();
775 return pContext->ThrowNativeError("No command callback available");
778 const char *args = pCmd->ArgS();
786 pContext->StringToLocalUTF8(params[1], params[2], args, &length);
788 return (cell_t)length;
791 static cell_t sm_PrintToServer(IPluginContext *pCtx, const cell_t *params)
797 pCtx->LocalToString(params[1], &fmt);
798 size_t res = atcprintf(buffer, sizeof(buffer)-2, fmt, pCtx, params, &arg);
800 buffer[res++] = '\n';
803 META_CONPRINT(buffer);
808 static cell_t sm_PrintToConsole(IPluginContext *pCtx, const cell_t *params)
810 int index = params[1];
811 if ((index < 0) || (index > g_Players.GetMaxClients()))
813 return pCtx->ThrowNativeError("Client index %d is invalid", index);
816 CPlayer *pPlayer = NULL;
819 pPlayer = g_Players.GetPlayerByIndex(index);
820 if (!pPlayer->IsInGame())
822 return pCtx->ThrowNativeError("Client %d is not in game", index);
825 /* Silent fail on bots, engine will crash */
826 if (pPlayer->IsFakeClient())
836 pCtx->LocalToString(params[2], &fmt);
837 size_t res = atcprintf(buffer, sizeof(buffer)-2, fmt, pCtx, params, &arg);
839 buffer[res++] = '\n';
844 engine->ClientPrintf(pPlayer->GetEdict(), buffer);
846 META_CONPRINT(buffer);
852 static cell_t sm_ServerCommand(IPluginContext *pContext, const cell_t *params)
854 g_SourceMod.SetGlobalTarget(SOURCEMOD_SERVER_LANGUAGE);
857 size_t len = g_SourceMod.FormatString(buffer, sizeof(buffer)-2, pContext, params, 1);
859 if (pContext->GetLastNativeError() != SP_ERROR_NONE)
864 /* One byte for null terminator, one for newline */
865 buffer[len++] = '\n';
868 engine->ServerCommand(buffer);
873 static cell_t sm_InsertServerCommand(IPluginContext *pContext, const cell_t *params)
875 g_SourceMod.SetGlobalTarget(SOURCEMOD_SERVER_LANGUAGE);
878 size_t len = g_SourceMod.FormatString(buffer, sizeof(buffer)-2, pContext, params, 1);
880 if (pContext->GetLastNativeError() != SP_ERROR_NONE)
885 /* One byte for null terminator, one for newline */
886 buffer[len++] = '\n';
889 InsertServerCommand(buffer);
894 static cell_t sm_ServerExecute(IPluginContext *pContext, const cell_t *params)
896 engine->ServerExecute();
901 static cell_t sm_ClientCommand(IPluginContext *pContext, const cell_t *params)
903 CPlayer *pPlayer = g_Players.GetPlayerByIndex(params[1]);
907 return pContext->ThrowNativeError("Client index %d is invalid", params[1]);
910 if (!pPlayer->IsConnected())
912 return pContext->ThrowNativeError("Client %d is not connected", params[1]);
915 g_SourceMod.SetGlobalTarget(params[1]);
918 g_SourceMod.FormatString(buffer, sizeof(buffer), pContext, params, 2);
920 if (pContext->GetLastNativeError() != SP_ERROR_NONE)
925 engine->ClientCommand(pPlayer->GetEdict(), "%s", buffer);
930 static cell_t FakeClientCommand(IPluginContext *pContext, const cell_t *params)
932 CPlayer *pPlayer = g_Players.GetPlayerByIndex(params[1]);
936 return pContext->ThrowNativeError("Client index %d is invalid", params[1]);
939 if (!pPlayer->IsConnected())
941 return pContext->ThrowNativeError("Client %d is not connected", params[1]);
945 g_SourceMod.FormatString(buffer, sizeof(buffer), pContext, params, 2);
947 if (pContext->GetLastNativeError() != SP_ERROR_NONE)
952 serverpluginhelpers->ClientCommand(pPlayer->GetEdict(), buffer);
957 static cell_t FakeClientCommandEx(IPluginContext *pContext, const cell_t *params)
959 CPlayer *pPlayer = g_Players.GetPlayerByIndex(params[1]);
963 return pContext->ThrowNativeError("Client index %d is invalid", params[1]);
966 if (!pPlayer->IsConnected())
968 return pContext->ThrowNativeError("Client %d is not connected", params[1]);
972 g_SourceMod.FormatString(buffer, sizeof(buffer), pContext, params, 2);
974 if (pContext->GetLastNativeError() != SP_ERROR_NONE)
979 g_HL2.AddToFakeCliCmdQueue(params[1], engine->GetPlayerUserId(pPlayer->GetEdict()), buffer);
984 static cell_t ReplyToCommand(IPluginContext *pContext, const cell_t *params)
986 g_SourceMod.SetGlobalTarget(params[1]);
988 /* Build the format string */
990 size_t len = g_SourceMod.FormatString(buffer, sizeof(buffer)-2, pContext, params, 2);
992 if (pContext->GetLastNativeError() != SP_ERROR_NONE)
997 /* If we're printing to the server, shortcut out */
1001 buffer[len++] = '\n';
1003 META_CONPRINT(buffer);
1007 CPlayer *pPlayer = g_Players.GetPlayerByIndex(params[1]);
1011 return pContext->ThrowNativeError("Client index %d is invalid", params[1]);
1014 if (!pPlayer->IsConnected())
1016 return pContext->ThrowNativeError("Client %d is not connected", params[1]);
1019 g_SourceMod.SetGlobalTarget(params[1]);
1021 unsigned int replyto = g_ChatTriggers.GetReplyTo();
1022 if (replyto == SM_REPLY_CONSOLE)
1024 buffer[len++] = '\n';
1026 engine->ClientPrintf(pPlayer->GetEdict(), buffer);
1027 } else if (replyto == SM_REPLY_CHAT) {
1033 g_HL2.TextMsg(params[1], HUD_PRINTTALK, buffer);
1039 static cell_t GetCmdReplyTarget(IPluginContext *pContext, const cell_t *params)
1041 return g_ChatTriggers.GetReplyTo();
1044 static cell_t SetCmdReplyTarget(IPluginContext *pContext, const cell_t *params)
1046 return g_ChatTriggers.SetReplyTo(params[1]);
1049 static cell_t GetCommandIterator(IPluginContext *pContext, const cell_t *params)
1051 GlobCmdIter *iter = new GlobCmdIter;
1052 iter->started = false;
1054 Handle_t hndl = g_HandleSys.CreateHandle(hCmdIterType, iter, pContext->GetIdentity(), g_pCoreIdent, NULL);
1055 if (hndl == BAD_HANDLE)
1063 static cell_t ReadCommandIterator(IPluginContext *pContext, const cell_t *params)
1067 HandleSecurity sec(pContext->GetIdentity(), g_pCoreIdent);
1069 if ((err = g_HandleSys.ReadHandle(params[1], hCmdIterType, &sec, (void **)&iter))
1070 != HandleError_None)
1072 return pContext->ThrowNativeError("Invalid GlobCmdIter Handle %x", params[1]);
1075 const List<ConCmdInfo *> &cmds = g_ConCmds.GetCommandList();
1079 iter->iter = cmds.begin();
1080 iter->started = true;
1083 while (iter->iter != cmds.end()
1084 && !(*(iter->iter))->sourceMod)
1089 if (iter->iter == cmds.end())
1094 ConCmdInfo *pInfo = (*(iter->iter));
1096 pContext->StringToLocalUTF8(params[2], params[3], pInfo->pCmd->GetName(), NULL);
1097 pContext->StringToLocalUTF8(params[5], params[6], pInfo->pCmd->GetHelpText(), NULL);
1100 pContext->LocalToPhysAddr(params[4], &addr);
1101 *addr = pInfo->admin.eflags;
1108 static cell_t CheckCommandAccess(IPluginContext *pContext, const cell_t *params)
1116 pContext->LocalToString(params[2], &cmd);
1118 /* Auto-detect a command if we can */
1119 FlagBits bits = params[3];
1120 bool found_command = false;
1121 if (params[0] < 4 || !params[4])
1123 found_command = g_ConCmds.LookForCommandAdminFlags(cmd, &bits);
1128 g_Admins.GetCommandOverride(cmd, Override_Command, &bits);
1131 return g_ConCmds.CheckCommandAccess(params[1], cmd, bits) ? 1 : 0;
1134 static cell_t IsChatTrigger(IPluginContext *pContext, const cell_t *params)
1136 return g_ChatTriggers.IsChatTrigger() ? 1 : 0;
1139 static cell_t SetCommandFlags(IPluginContext *pContext, const cell_t *params)
1142 pContext->LocalToString(params[1], &name);
1144 return (s_CommandFlagsHelper.SetFlags(name, params[2])) ? 1 : 0;
1147 static cell_t GetCommandFlags(IPluginContext *pContext, const cell_t *params)
1151 pContext->LocalToString(params[1], &name);
1153 return (s_CommandFlagsHelper.GetFlags(name, &flags)) ? flags : -1;
1156 static cell_t FindFirstConCommand(IPluginContext *pContext, const cell_t *params)
1160 cell_t *pIsCmd, *pFlags;
1161 const ConCommandBase *pConCmd;
1164 pContext->LocalToPhysAddr(params[3], &pIsCmd);
1165 pContext->LocalToPhysAddr(params[4], &pFlags);
1167 #if SOURCE_ENGINE == SE_LEFT4DEAD
1168 ICvarIteratorInternal *cvarIter = icvar->FactoryInternalIterator();
1169 cvarIter->SetFirst();
1170 if (!cvarIter->IsValid())
1174 pConCmd = cvarIter->Get();
1176 pConCmd = icvar->GetCommands();
1177 if (pConCmd == NULL)
1183 pContext->StringToLocalUTF8(params[1], params[2], pConCmd->GetName(), NULL);
1184 *pIsCmd = pConCmd->IsCommand() ? 1 : 0;
1185 *pFlags = pConCmd->GetFlags();
1189 desc = pConCmd->GetHelpText();
1190 pContext->StringToLocalUTF8(params[5], params[6], (desc && desc[0]) ? desc : "", NULL);
1193 pIter = new ConCmdIter;
1194 #if SOURCE_ENGINE == SE_LEFT4DEAD
1195 pIter->pLast = cvarIter;
1197 pIter->pLast = pConCmd;
1200 if ((hndl = g_HandleSys.CreateHandle(htConCmdIter, pIter, pContext->GetIdentity(), g_pCoreIdent, NULL))
1210 static cell_t FindNextConCommand(IPluginContext *pContext, const cell_t *params)
1214 cell_t *pIsCmd, *pFlags;
1216 const ConCommandBase *pConCmd;
1217 HandleSecurity sec(pContext->GetIdentity(), g_pCoreIdent);
1219 if ((err = g_HandleSys.ReadHandle(params[1], htConCmdIter, &sec, (void **)&pIter)) != HandleError_None)
1221 return pContext->ThrowNativeError("Invalid Handle %x (error %d)", params[1], err);
1224 if (pIter->pLast == NULL)
1229 #if SOURCE_ENGINE == SE_LEFT4DEAD
1230 ICvarIteratorInternal *cvarIter = pIter->pLast;
1232 if (!cvarIter->IsValid())
1236 pConCmd = cvarIter->Get();
1238 pConCmd = pIter->pLast->GetNext();
1239 if (pConCmd == NULL)
1243 pIter->pLast = pConCmd;
1246 pContext->LocalToPhysAddr(params[4], &pIsCmd);
1247 pContext->LocalToPhysAddr(params[5], &pFlags);
1249 pContext->StringToLocalUTF8(params[2], params[3], pConCmd->GetName(), NULL);
1250 *pIsCmd = pConCmd->IsCommand() ? 1 : 0;
1251 *pFlags = pConCmd->GetFlags();
1255 desc = pConCmd->GetHelpText();
1256 pContext->StringToLocalUTF8(params[6], params[7], (desc && desc[0]) ? desc : "", NULL);
1262 static cell_t SendConVarValue(IPluginContext *pContext, const cell_t *params)
1264 Handle_t hndl = static_cast<Handle_t>(params[2]);
1269 pContext->LocalToString(params[3], &value);
1271 if ((err=g_ConVarManager.ReadConVarHandle(hndl, &pConVar))
1272 != HandleError_None)
1274 return pContext->ThrowNativeError("Invalid convar handle %x (error %d)", hndl, err);
1278 bf_write buffer(data, sizeof(data));
1280 buffer.WriteUBitLong(NET_SETCONVAR, 5);
1281 buffer.WriteByte(1);
1282 buffer.WriteString(pConVar->GetName());
1283 buffer.WriteString(value);
1285 CPlayer *pPlayer = g_Players.GetPlayerByIndex(params[1]);
1289 return pContext->ThrowNativeError("Client index %d is invalid", params[1]);
1292 if (!pPlayer->IsConnected())
1294 return pContext->ThrowNativeError("Client %d is not connected", params[1]);
1297 if (pPlayer->IsFakeClient())
1299 return pContext->ThrowNativeError("Client %d is fake and cannot be targeted", params[1]);
1302 INetChannel *netchan = static_cast<INetChannel *>(engine->GetPlayerNetInfo(params[1]));
1303 netchan->SendData(buffer);
1308 static cell_t AddServerTag(IPluginContext *pContext, const cell_t *params)
1311 pContext->LocalToString(params[1], &value);
1313 if (!g_Tags.AddTag(value))
1318 IPlugin *pPlugin = g_PluginSys.FindPluginByContext(pContext->GetContext());
1319 SourceHook::List<char *> *pList = NULL;
1321 if (!pPlugin->GetProperty("ServerTags", (void **)&pList, false) || !pList)
1323 pList = new SourceHook::List<char *>;
1324 pPlugin->SetProperty("ServerTags", pList);
1327 pList->push_back(strdup(value));
1332 static cell_t RemoveServerTag(IPluginContext *pContext, const cell_t *params)
1335 pContext->LocalToString(params[1], &value);
1337 IPlugin *pPlugin = g_PluginSys.FindPluginByContext(pContext->GetContext());
1338 SourceHook::List<char *> *pList = NULL;
1340 if (!pPlugin->GetProperty("ServerTags", (void **)&pList, false) || !pList)
1342 pList = new SourceHook::List<char *>;
1343 pPlugin->SetProperty("ServerTags", pList);
1346 SourceHook::List<char *>::iterator iter = pList->begin();
1348 while (iter != pList->end())
1350 if (strcmp(*iter, value) == 0)
1352 g_pMemAlloc->Free(*iter);
1355 return g_Tags.RemoveTag(value);
1362 REGISTER_NATIVES(consoleNatives)
1364 {"CreateConVar", sm_CreateConVar},
1365 {"FindConVar", sm_FindConVar},
1366 {"HookConVarChange", sm_HookConVarChange},
1367 {"UnhookConVarChange", sm_UnhookConVarChange},
1368 {"GetConVarBool", sm_GetConVarBool},
1369 {"SetConVarBool", sm_SetConVarNum},
1370 {"GetConVarInt", sm_GetConVarInt},
1371 {"SetConVarInt", sm_SetConVarNum},
1372 {"GetConVarFloat", sm_GetConVarFloat},
1373 {"SetConVarFloat", sm_SetConVarFloat},
1374 {"GetConVarString", sm_GetConVarString},
1375 {"SetConVarString", sm_SetConVarString},
1376 {"GetConVarFlags", sm_GetConVarFlags},
1377 {"SetConVarFlags", sm_SetConVarFlags},
1378 {"ResetConVar", sm_ResetConVar},
1379 {"GetConVarName", sm_GetConVarName},
1380 {"GetConVarBounds", sm_GetConVarBounds},
1381 {"SetConVarBounds", sm_SetConVarBounds},
1382 {"QueryClientConVar", sm_QueryClientConVar},
1383 {"RegServerCmd", sm_RegServerCmd},
1384 {"RegConsoleCmd", sm_RegConsoleCmd},
1385 {"GetCmdArgString", sm_GetCmdArgString},
1386 {"GetCmdArgs", sm_GetCmdArgs},
1387 {"GetCmdArg", sm_GetCmdArg},
1388 {"PrintToServer", sm_PrintToServer},
1389 {"PrintToConsole", sm_PrintToConsole},
1390 {"RegAdminCmd", sm_RegAdminCmd},
1391 {"ServerCommand", sm_ServerCommand},
1392 {"InsertServerCommand", sm_InsertServerCommand},
1393 {"ServerExecute", sm_ServerExecute},
1394 {"ClientCommand", sm_ClientCommand},
1395 {"FakeClientCommand", FakeClientCommand},
1396 {"ReplyToCommand", ReplyToCommand},
1397 {"GetCmdReplySource", GetCmdReplyTarget},
1398 {"SetCmdReplySource", SetCmdReplyTarget},
1399 {"GetCommandIterator", GetCommandIterator},
1400 {"ReadCommandIterator", ReadCommandIterator},
1401 {"CheckCommandAccess", CheckCommandAccess},
1402 {"FakeClientCommandEx", FakeClientCommandEx},
1403 {"IsChatTrigger", IsChatTrigger},
1404 {"SetCommandFlags", SetCommandFlags},
1405 {"GetCommandFlags", GetCommandFlags},
1406 {"FindFirstConCommand", FindFirstConCommand},
1407 {"FindNextConCommand", FindNextConCommand},
1408 {"SendConVarValue", SendConVarValue},
1409 {"AddServerTag", AddServerTag},
1410 {"RemoveServerTag", RemoveServerTag},