Updated some CS:GO gamedata.
2 * vim: set ts=4 sw=4 tw=99 noet :
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 "common_logic.h"
33 #include <IPluginSys.h>
34 #include <IPlayerHelpers.h>
35 #include <sh_string.h>
37 #include "CellArray.h"
38 #include "AutoHandleRooter.h"
40 using namespace SourceHook;
41 using namespace SourceMod;
43 class PlayerLogicHelpers :
45 public IPluginsListener,
46 public ICommandTargetProcessor
48 struct SimpleMultiTargetFilter
51 SourceHook::String pattern;
53 SourceHook::String phrase;
56 SimpleMultiTargetFilter(IPlugin *plugin, const char *pattern, IPluginFunction *fun,
57 const char *phrase, bool phraseIsML)
58 : plugin(plugin), pattern(pattern), fun(fun), phrase(phrase), phraseIsML(phraseIsML)
63 List<SimpleMultiTargetFilter *> simpleMultis;
67 void AddMultiTargetFilter(IPlugin *plugin, const char *pattern, IPluginFunction *fun,
68 const char *phrase, bool phraseIsML)
70 SimpleMultiTargetFilter *smtf = new SimpleMultiTargetFilter(plugin, pattern, fun, phrase,
73 simpleMultis.push_back(smtf);
76 playerhelpers->RegisterCommandTargetProcessor(this);
81 void RemoveMultiTargetFilter(const char *pattern, IPluginFunction *fun)
83 List<SimpleMultiTargetFilter *>::iterator iter = simpleMultis.begin();
85 while (iter != simpleMultis.end()) {
86 if ((*iter)->fun == fun && strcmp((*iter)->pattern.c_str(), pattern) == 0) {
88 iter = simpleMultis.erase(iter);
96 : filterEnabled(false)
100 public: //ICommandTargetProcessor
101 bool ProcessCommandTarget(cmd_target_info_t *info)
103 List<SimpleMultiTargetFilter *>::iterator iter;
105 for (iter = simpleMultis.begin(); iter != simpleMultis.end(); iter++) {
106 SimpleMultiTargetFilter *smtf = (*iter);
107 if (strcmp(smtf->pattern.c_str(), info->pattern) == 0) {
108 CellArray *array = new CellArray(1);
109 HandleSecurity sec(g_pCoreIdent, g_pCoreIdent);
110 Handle_t hndl = handlesys->CreateHandleEx(htCellArray, array, &sec, NULL, NULL);
111 AutoHandleCloner ahc(hndl, sec);
112 if (ahc.getClone() == BAD_HANDLE) {
113 smcore.LogError("[SM] Could not allocate a handle (%s, %d)", __FILE__, __LINE__);
118 smtf->fun->PushString(info->pattern);
119 smtf->fun->PushCell(ahc.getClone());
121 if (smtf->fun->Execute(&result) != SP_ERROR_NONE || !result)
124 IGamePlayer *pAdmin = info->admin
125 ? playerhelpers->GetGamePlayer(info->admin)
128 info->num_targets = 0;
129 for (size_t i = 0; i < array->size(); i++) {
130 cell_t client = *array->at(i);
131 IGamePlayer *pClient = playerhelpers->GetGamePlayer(client);
132 if (pClient == NULL || !pClient->IsConnected())
134 if (playerhelpers->FilterCommandTarget(pAdmin, pClient, info->flags) ==
135 COMMAND_TARGET_VALID)
137 info->targets[info->num_targets++] = client;
138 if (info->num_targets >= unsigned(info->max_targets))
143 info->reason = info->num_targets > 0
144 ? COMMAND_TARGET_VALID
145 : COMMAND_TARGET_EMPTY_FILTER;
146 if (info->num_targets) {
147 smcore.strncopy(info->target_name, smtf->phrase.c_str(), info->target_name_maxlength);
148 info->target_name_style = smtf->phraseIsML
149 ? COMMAND_TARGETNAME_ML
150 : COMMAND_TARGETNAME_RAW;
160 public: //SMGlobalClass
161 void OnSourceModAllInitialized()
163 pluginsys->AddPluginsListener(this);
166 void OnSourceModShutdown()
168 pluginsys->RemovePluginsListener(this);
170 playerhelpers->UnregisterCommandTargetProcessor(this);
171 filterEnabled = false;
175 public: //IPluginsListener
177 void OnPluginDestroyed(IPlugin *plugin)
179 List<SimpleMultiTargetFilter *>::iterator iter = simpleMultis.begin();
181 while (iter != simpleMultis.end()) {
182 if ((*iter)->plugin != plugin) {
186 iter = simpleMultis.erase(iter);
190 } s_PlayerLogicHelpers;
193 AddMultiTargetFilter(IPluginContext *ctx, const cell_t *params)
195 IPluginFunction *fun = ctx->GetFunctionById(funcid_t(params[2]));
197 return ctx->ThrowNativeError("Invalid function id (%X)", params[2]);
202 ctx->LocalToString(params[1], &pattern);
203 ctx->LocalToString(params[3], &phrase);
205 bool phraseIsML = !!params[4];
206 IPlugin *plugin = pluginsys->FindPluginByContext(ctx->GetContext());
208 s_PlayerLogicHelpers.AddMultiTargetFilter(plugin, pattern, fun, phrase, phraseIsML);
214 RemoveMultiTargetFilter(IPluginContext *ctx, const cell_t *params)
216 IPluginFunction *fun = ctx->GetFunctionById(funcid_t(params[2]));
218 return ctx->ThrowNativeError("Invalid function id (%X)", params[2]);
222 ctx->LocalToString(params[1], &pattern);
224 s_PlayerLogicHelpers.RemoveMultiTargetFilter(pattern, fun);
229 REGISTER_NATIVES(playernatives)
231 {"AddMultiTargetFilter", AddMultiTargetFilter},
232 {"RemoveMultiTargetFilter", RemoveMultiTargetFilter},