Fix SDKHooks load issue and reliance on gEntList ptr on CS:GO and Dota 2 on Windows.
3 * =============================================================================
4 * SourceMod SDKTools Extension
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 <extension.h>
35 #define SPEAK_NORMAL 0
38 #define SPEAK_LISTENALL 4
40 #define SPEAK_LISTENTEAM 16
41 #define LISTEN_DEFAULT 0
45 /* this enum needs to be in the order as the LISTEN_ defines above to keep
46 the old broken SetClientListening functionality untouched */
49 Listen_Default, /**< Leave it up to the game */
50 Listen_No, /**< Can't hear */
51 Listen_Yes, /**< Can hear */
54 size_t g_VoiceFlags[65];
55 size_t g_VoiceHookCount = 0;
56 ListenOverride g_VoiceMap[65][65];
57 bool g_ClientMutes[65][65];
59 SH_DECL_HOOK3(IVoiceServer, SetClientListening, SH_NOATTRIB, 0, bool, int, int, bool);
61 #if SOURCE_ENGINE == SE_DOTA
62 SH_DECL_HOOK2_void(IServerGameClients, ClientCommand, SH_NOATTRIB, 0, CEntityIndex, const CCommand &);
63 #elif SOURCE_ENGINE >= SE_ORANGEBOX
64 SH_DECL_HOOK2_void(IServerGameClients, ClientCommand, SH_NOATTRIB, 0, edict_t *, const CCommand &);
66 SH_DECL_HOOK1_void(IServerGameClients, ClientCommand, SH_NOATTRIB, 0, edict_t *);
71 if (--g_VoiceHookCount == 0)
73 SH_REMOVE_HOOK(IVoiceServer, SetClientListening, voiceserver, SH_MEMBER(&g_SdkTools, &SDKTools::OnSetClientListening), false);
82 if (!g_VoiceHookCount++)
84 SH_ADD_HOOK(IVoiceServer, SetClientListening, voiceserver, SH_MEMBER(&g_SdkTools, &SDKTools::OnSetClientListening), false);
88 void SDKTools::VoiceInit()
90 memset(g_VoiceMap, 0, sizeof(g_VoiceMap));
91 memset(g_ClientMutes, 0, sizeof(g_ClientMutes));
93 SH_ADD_HOOK(IServerGameClients, ClientCommand, serverClients, SH_MEMBER(this, &SDKTools::OnClientCommand), true);
96 #if SOURCE_ENGINE == SE_DOTA
97 void SDKTools::OnClientCommand(CEntityIndex index, const CCommand &args)
99 int client = index.Get();
100 #elif SOURCE_ENGINE >= SE_ORANGEBOX
101 void SDKTools::OnClientCommand(edict_t *pEntity, const CCommand &args)
103 int client = IndexOfEdict(pEntity);
105 void SDKTools::OnClientCommand(edict_t *pEntity)
108 int client = IndexOfEdict(pEntity);
111 if ((args.ArgC() > 1) && (stricmp(args.Arg(0), "vban") == 0))
113 for (int i = 1; (i < args.ArgC()) && (i < 3); i++)
115 unsigned long mask = 0;
116 sscanf(args.Arg(i), "%p", (void**)&mask);
118 for (int j = 0; j < 32; j++)
120 g_ClientMutes[client][1 + j + 32 * (i - 1)] = !!(mask & 1 << j);
125 RETURN_META(MRES_IGNORED);
128 bool SDKTools::OnSetClientListening(int iReceiver, int iSender, bool bListen)
130 if (g_ClientMutes[iReceiver][iSender])
132 RETURN_META_VALUE_NEWPARAMS(MRES_IGNORED, bListen, &IVoiceServer::SetClientListening, (iReceiver, iSender, false));
135 if (g_VoiceFlags[iSender] & SPEAK_MUTED)
137 RETURN_META_VALUE_NEWPARAMS(MRES_IGNORED, bListen, &IVoiceServer::SetClientListening, (iReceiver, iSender, false));
140 if (g_VoiceMap[iReceiver][iSender] == Listen_No)
142 RETURN_META_VALUE_NEWPARAMS(MRES_IGNORED, bListen, &IVoiceServer::SetClientListening, (iReceiver, iSender, false));
144 else if (g_VoiceMap[iReceiver][iSender] == Listen_Yes)
146 RETURN_META_VALUE_NEWPARAMS(MRES_IGNORED, bListen, &IVoiceServer::SetClientListening, (iReceiver, iSender, true));
149 if ((g_VoiceFlags[iSender] & SPEAK_ALL) || (g_VoiceFlags[iReceiver] & SPEAK_LISTENALL))
151 RETURN_META_VALUE_NEWPARAMS(MRES_IGNORED, bListen, &IVoiceServer::SetClientListening, (iReceiver, iSender, true));
154 if ((g_VoiceFlags[iSender] & SPEAK_TEAM) || (g_VoiceFlags[iReceiver] & SPEAK_LISTENTEAM))
156 IGamePlayer *pReceiver = playerhelpers->GetGamePlayer(iReceiver);
157 IGamePlayer *pSender = playerhelpers->GetGamePlayer(iSender);
159 if (pReceiver && pSender && pReceiver->IsInGame() && pSender->IsInGame())
161 IPlayerInfo *pRInfo = pReceiver->GetPlayerInfo();
162 IPlayerInfo *pSInfo = pSender->GetPlayerInfo();
164 if (pRInfo && pSInfo && pRInfo->GetTeamIndex() == pSInfo->GetTeamIndex())
166 RETURN_META_VALUE_NEWPARAMS(MRES_IGNORED, bListen, &IVoiceServer::SetClientListening, (iReceiver, iSender, true));
171 RETURN_META_VALUE(MRES_IGNORED, bListen);
174 void SDKTools::OnClientDisconnecting(int client)
176 g_Hooks.OnClientDisconnecting(client);
178 int max_clients = playerhelpers->GetMaxClients();
180 if (g_VoiceHookCount == 0)
185 /* This can probably be optimized more, but I doubt it's that much
186 * of an actual bottleneck.
189 /* Reset other clients who receive from or have muted this client */
190 for (int i = 1; i <= max_clients; i++)
197 g_ClientMutes[i][client] = false;
198 g_ClientMutes[client][i] = false;
200 if (g_VoiceMap[i][client] != Listen_Default)
202 g_VoiceMap[i][client] = Listen_Default;
208 if (g_VoiceMap[client][i] != Listen_Default)
210 g_VoiceMap[client][i] = Listen_Default;
218 if (g_VoiceFlags[client])
220 g_VoiceFlags[client] = SPEAK_NORMAL;
225 static cell_t SetClientListeningFlags(IPluginContext *pContext, const cell_t *params)
227 IGamePlayer *player = playerhelpers->GetGamePlayer(params[1]);
230 return pContext->ThrowNativeError("Client index %d is invalid", params[1]);
232 else if (!player->IsConnected())
234 return pContext->ThrowNativeError("Client %d is not connected", params[1]);
237 if (!params[2] && g_VoiceFlags[params[1]])
241 else if (!g_VoiceFlags[params[1]] && params[2])
246 g_VoiceFlags[params[1]] = params[2];
251 static cell_t GetClientListeningFlags(IPluginContext *pContext, const cell_t *params)
253 IGamePlayer *player = playerhelpers->GetGamePlayer(params[1]);
256 return pContext->ThrowNativeError("Client index %d is invalid", params[1]);
258 else if (!player->IsConnected())
260 return pContext->ThrowNativeError("Client %d is not connected", params[1]);
263 return g_VoiceFlags[params[1]];
266 static cell_t SetClientListening(IPluginContext *pContext, const cell_t *params)
271 player = playerhelpers->GetGamePlayer(params[1]);
274 return pContext->ThrowNativeError("Receiver client index %d is invalid", params[1]);
276 else if (!player->IsConnected())
278 return pContext->ThrowNativeError("Receiver client %d is not connected", params[1]);
281 player = playerhelpers->GetGamePlayer(params[2]);
284 return pContext->ThrowNativeError("Sender client index %d is invalid", params[2]);
286 else if (!player->IsConnected())
288 return pContext->ThrowNativeError("Sender client %d is not connected", params[2]);
294 if (g_VoiceMap[r][s] == Listen_Default && params[3] != Listen_Default)
296 g_VoiceMap[r][s] = (ListenOverride) params[3];
299 else if (g_VoiceMap[r][s] != Listen_Default && params[3] == Listen_Default)
301 g_VoiceMap[r][s] = (ListenOverride) params[3];
306 g_VoiceMap[r][s] = (ListenOverride) params[3];
312 static cell_t GetClientListening(IPluginContext *pContext, const cell_t *params)
316 player = playerhelpers->GetGamePlayer(params[1]);
319 return pContext->ThrowNativeError("Receiver client index %d is invalid", params[1]);
321 else if (!player->IsConnected())
323 return pContext->ThrowNativeError("Receiver client %d is not connected", params[1]);
326 player = playerhelpers->GetGamePlayer(params[2]);
329 return pContext->ThrowNativeError("Sender client index %d is invalid", params[2]);
331 else if (!player->IsConnected())
333 return pContext->ThrowNativeError("Sender client %d is not connected", params[2]);
336 return g_VoiceMap[params[1]][params[2]];
339 static cell_t IsClientMuted(IPluginContext *pContext, const cell_t *params)
343 player = playerhelpers->GetGamePlayer(params[1]);
346 return pContext->ThrowNativeError("Muter client index %d is invalid", params[1]);
348 else if (!player->IsConnected())
350 return pContext->ThrowNativeError("Muter client %d is not connected", params[1]);
353 player = playerhelpers->GetGamePlayer(params[2]);
356 return pContext->ThrowNativeError("Mutee client index %d is invalid", params[2]);
358 else if (!player->IsConnected())
360 return pContext->ThrowNativeError("Mutee client %d is not connected", params[2]);
363 return g_ClientMutes[params[1]][params[2]];
366 sp_nativeinfo_t g_VoiceNatives[] =
368 {"SetClientListeningFlags", SetClientListeningFlags},
369 {"GetClientListeningFlags", GetClientListeningFlags},
370 {"SetClientListening", SetClientListening},
371 {"GetClientListening", GetClientListening},
372 {"SetListenOverride", SetClientListening},
373 {"GetListenOverride", GetClientListening},
374 {"IsClientMuted", IsClientMuted},