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>.
33 #include "extension.h"
36 static bool PRCH_enabled = false;
37 static bool PRCH_used = false;
39 SH_DECL_MANUALHOOK2_void(PlayerRunCmdHook, 0, 0, 0, CUserCmd *, IMoveHelper *);
41 CHookManager::CHookManager()
46 void CHookManager::Initialize()
49 if (g_pGameConf->GetOffset("PlayerRunCmd", &offset))
51 SH_MANUALHOOK_RECONFIGURE(PlayerRunCmdHook, offset, 0, 0);
55 plsys->AddPluginsListener(this);
57 m_usercmdsFwd = forwards->CreateForward("OnPlayerRunCmd", ET_Event, 6, NULL, Param_Cell, Param_CellByRef, Param_CellByRef, Param_Array, Param_Array, Param_CellByRef);
60 void CHookManager::Shutdown()
62 forwards->ReleaseForward(m_usercmdsFwd);
64 plsys->RemovePluginsListener(this);
67 void CHookManager::OnClientPutInServer(int client)
75 edict_t *pEdict = PEntityOfEntIndex(client);
81 IServerUnknown *pUnknown = pEdict->GetUnknown();
87 CBaseEntity *pEntity = pUnknown->GetBaseEntity();
93 SH_ADD_MANUALHOOK_MEMFUNC(PlayerRunCmdHook, pEntity, this, &CHookManager::PlayerRunCmd, false);
96 void CHookManager::OnClientDisconnecting(int client)
104 edict_t *pEdict = PEntityOfEntIndex(client);
110 IServerUnknown *pUnknown = pEdict->GetUnknown();
116 CBaseEntity *pEntity = pUnknown->GetBaseEntity();
122 SH_REMOVE_MANUALHOOK_MEMFUNC(PlayerRunCmdHook, pEntity, this, &CHookManager::PlayerRunCmd, false);
125 void CHookManager::PlayerRunCmd(CUserCmd *ucmd, IMoveHelper *moveHelper)
127 if (m_usercmdsFwd->GetFunctionCount() == 0)
129 RETURN_META(MRES_IGNORED);
132 CBaseEntity *pEntity = META_IFACEPTR(CBaseEntity);
136 RETURN_META(MRES_IGNORED);
139 edict_t *pEdict = gameents->BaseEntityToEdict(pEntity);
143 RETURN_META(MRES_IGNORED);
146 int client = IndexOfEdict(pEdict);
150 /* Impulse is a byte so we copy it back manually */
151 cell_t impulse = ucmd->impulse;
152 cell_t vel[3] = {sp_ftoc(ucmd->forwardmove), sp_ftoc(ucmd->sidemove), sp_ftoc(ucmd->upmove)};
153 cell_t angles[3] = {sp_ftoc(ucmd->viewangles.x), sp_ftoc(ucmd->viewangles.y), sp_ftoc(ucmd->viewangles.z)};
155 m_usercmdsFwd->PushCell(client);
156 m_usercmdsFwd->PushCellByRef(&ucmd->buttons);
157 m_usercmdsFwd->PushCellByRef(&impulse);
158 m_usercmdsFwd->PushArray(vel, 3, SM_PARAM_COPYBACK);
159 m_usercmdsFwd->PushArray(angles, 3, SM_PARAM_COPYBACK);
160 m_usercmdsFwd->PushCellByRef(&ucmd->weaponselect);
161 m_usercmdsFwd->Execute(&result);
163 ucmd->impulse = impulse;
164 ucmd->forwardmove = sp_ctof(vel[0]);
165 ucmd->sidemove = sp_ctof(vel[1]);
166 ucmd->upmove = sp_ctof(vel[2]);
167 ucmd->viewangles.x = sp_ctof(angles[0]);
168 ucmd->viewangles.y = sp_ctof(angles[1]);
169 ucmd->viewangles.z = sp_ctof(angles[2]);
172 if (result == Pl_Handled)
174 RETURN_META(MRES_SUPERCEDE);
177 RETURN_META(MRES_IGNORED);
180 void CHookManager::OnPluginLoaded(IPlugin *plugin)
188 if (!m_usercmdsFwd->GetFunctionCount())
193 int MaxClients = playerhelpers->GetMaxClients();
194 for (int i = 1; i <= MaxClients; i++)
196 if (playerhelpers->GetGamePlayer(i)->IsInGame())
198 OnClientPutInServer(i);
203 void CHookManager::OnPluginUnloaded(IPlugin *plugin)
211 if (m_usercmdsFwd->GetFunctionCount())
214 int MaxClients = playerhelpers->GetMaxClients();
215 for (int i = 1; i <= MaxClients; i++)
217 if (playerhelpers->GetGamePlayer(i)->IsInGame())
219 OnClientDisconnecting(i);