extensions/sdktools/hooks.cpp
author Ruben Gonzalez <drifter01620@gmail.com>
Thu May 22 09:49:27 2014 -0400 (2014-05-22)
changeset 4378 568f4497ef4a
parent 4268 3c383c5b1aaa
permissions -rw-r--r--
Update CS:GO team score gamedata.
     1 /**
     2 * vim: set ts=4 :
     3 * =============================================================================
     4 * SourceMod SDKTools Extension
     5 * Copyright (C) 2004-2008 AlliedModders LLC.  All rights reserved.
     6 * =============================================================================
     7 *
     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.
    11 *
    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
    15 * details.
    16 *
    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/>.
    19 *
    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>.
    28 *
    29 * Version: $Id$
    30 */
    31 
    32 #include "hooks.h"
    33 #include "extension.h"
    34 
    35 #include "basehandle.h"
    36 #include "vector.h"
    37 #include "utlvector.h"
    38 #include <shareddefs.h>
    39 #include "usercmd.h"
    40 
    41 #define FEATURECAP_PLAYERRUNCMD_11PARAMS	"SDKTools PlayerRunCmd 11Params"
    42 
    43 CHookManager g_Hooks;
    44 static bool PRCH_enabled = false;
    45 static bool PRCH_used = false;
    46 
    47 SH_DECL_MANUALHOOK2_void(PlayerRunCmdHook, 0, 0, 0, CUserCmd *, IMoveHelper *);
    48 
    49 CHookManager::CHookManager()
    50 {
    51 	m_usercmdsFwd = NULL;
    52 }
    53 
    54 void CHookManager::Initialize()
    55 {
    56 	int offset;
    57 	if (g_pGameConf->GetOffset("PlayerRunCmd", &offset))
    58 	{
    59 		SH_MANUALHOOK_RECONFIGURE(PlayerRunCmdHook, offset, 0, 0);
    60 		PRCH_enabled = true;
    61 	}
    62 	else
    63 	{
    64 		g_pSM->LogError(myself, "Failed to find PlayerRunCmd offset - OnPlayerRunCmd forward disabled.");
    65 		PRCH_enabled = false;
    66 	}
    67 	
    68 	plsys->AddPluginsListener(this);
    69 	sharesys->AddCapabilityProvider(myself, this, FEATURECAP_PLAYERRUNCMD_11PARAMS);
    70 
    71 	m_usercmdsFwd = forwards->CreateForward("OnPlayerRunCmd", ET_Event, 11, NULL,
    72 		Param_Cell,			// client
    73 		Param_CellByRef,	// buttons
    74 		Param_CellByRef,	// impulse
    75 		Param_Array,		// Float:vel[3]
    76 		Param_Array,		// Float:angles[3]
    77 		Param_CellByRef,	// weapon
    78 		Param_CellByRef,	// subtype
    79 		Param_CellByRef,	// cmdnum
    80 		Param_CellByRef,	// tickcount
    81 		Param_CellByRef,	// seed
    82 		Param_Array);		// mouse[2]
    83 }
    84 
    85 void CHookManager::Shutdown()
    86 {
    87 	forwards->ReleaseForward(m_usercmdsFwd);
    88 
    89 	plsys->RemovePluginsListener(this);
    90 	sharesys->DropCapabilityProvider(myself, this, FEATURECAP_PLAYERRUNCMD_11PARAMS);
    91 }
    92 
    93 void CHookManager::OnClientPutInServer(int client)
    94 {
    95 	if (!PRCH_enabled)
    96 		return;
    97 
    98 	if (!PRCH_used)
    99 		return;
   100 
   101 	edict_t *pEdict = PEntityOfEntIndex(client);
   102 	if (!pEdict)
   103 	{
   104 		return;
   105 	}
   106 
   107 	IServerUnknown *pUnknown = pEdict->GetUnknown();
   108 	if (!pUnknown)
   109 	{
   110 		return;
   111 	}
   112 
   113 	CBaseEntity *pEntity = pUnknown->GetBaseEntity();
   114 	if (!pEntity)
   115 	{
   116 		return;
   117 	}
   118 
   119 	CVTableHook hook(pEntity);
   120 	for (size_t i = 0; i < m_runUserCmdHooks.length(); ++i)
   121 	{
   122 		if (hook == m_runUserCmdHooks[i])
   123 		{
   124 			return;
   125 		}
   126 	}
   127 
   128 	int hookid = SH_ADD_MANUALVPHOOK(PlayerRunCmdHook, pEntity, SH_MEMBER(this, &CHookManager::PlayerRunCmd), false);
   129 	hook.SetHookID(hookid);
   130 	m_runUserCmdHooks.append(new CVTableHook(hook));
   131 }
   132 
   133 void CHookManager::PlayerRunCmd(CUserCmd *ucmd, IMoveHelper *moveHelper)
   134 {
   135 	if (m_usercmdsFwd->GetFunctionCount() == 0)
   136 	{
   137 		RETURN_META(MRES_IGNORED);
   138 	}
   139 
   140 	CBaseEntity *pEntity = META_IFACEPTR(CBaseEntity);
   141 
   142 	if (!pEntity)
   143 	{
   144 		RETURN_META(MRES_IGNORED);
   145 	}
   146 
   147 	edict_t *pEdict = gameents->BaseEntityToEdict(pEntity);
   148 
   149 	if (!pEdict)
   150 	{
   151 		RETURN_META(MRES_IGNORED);
   152 	}
   153 
   154 	int client = IndexOfEdict(pEdict);
   155 
   156 
   157 	cell_t result = 0;
   158 	/* Impulse is a byte so we copy it back manually */
   159 	cell_t impulse = ucmd->impulse;
   160 	cell_t vel[3] = {sp_ftoc(ucmd->forwardmove), sp_ftoc(ucmd->sidemove), sp_ftoc(ucmd->upmove)};
   161 	cell_t angles[3] = {sp_ftoc(ucmd->viewangles.x), sp_ftoc(ucmd->viewangles.y), sp_ftoc(ucmd->viewangles.z)};
   162 	cell_t mouse[2] = {ucmd->mousedx, ucmd->mousedy};
   163 
   164 	m_usercmdsFwd->PushCell(client);
   165 	m_usercmdsFwd->PushCellByRef(&ucmd->buttons);
   166 	m_usercmdsFwd->PushCellByRef(&impulse);
   167 	m_usercmdsFwd->PushArray(vel, 3, SM_PARAM_COPYBACK);
   168 	m_usercmdsFwd->PushArray(angles, 3, SM_PARAM_COPYBACK);
   169 	m_usercmdsFwd->PushCellByRef(&ucmd->weaponselect);
   170 	m_usercmdsFwd->PushCellByRef(&ucmd->weaponsubtype);
   171 	m_usercmdsFwd->PushCellByRef(&ucmd->command_number);
   172 	m_usercmdsFwd->PushCellByRef(&ucmd->tick_count);
   173 	m_usercmdsFwd->PushCellByRef(&ucmd->random_seed);
   174 	m_usercmdsFwd->PushArray(mouse, 2, SM_PARAM_COPYBACK);
   175 	m_usercmdsFwd->Execute(&result);
   176 
   177 	ucmd->impulse = impulse;
   178 	ucmd->forwardmove = sp_ctof(vel[0]);
   179 	ucmd->sidemove = sp_ctof(vel[1]);
   180 	ucmd->upmove = sp_ctof(vel[2]);
   181 	ucmd->viewangles.x = sp_ctof(angles[0]);
   182 	ucmd->viewangles.y = sp_ctof(angles[1]);
   183 	ucmd->viewangles.z = sp_ctof(angles[2]);
   184 	ucmd->mousedx = mouse[0];
   185 	ucmd->mousedy = mouse[1];
   186 
   187 
   188 	if (result == Pl_Handled)
   189 	{
   190 		RETURN_META(MRES_SUPERCEDE);
   191 	}
   192 
   193 	RETURN_META(MRES_IGNORED);
   194 }
   195 
   196 void CHookManager::OnPluginLoaded(IPlugin *plugin)
   197 {
   198 	if (!PRCH_enabled)
   199 		return;
   200 
   201 	if (PRCH_used)
   202 		return;
   203 
   204 	if (!m_usercmdsFwd->GetFunctionCount())
   205 		return;
   206 
   207 	PRCH_used = true;
   208 
   209 	int MaxClients = playerhelpers->GetMaxClients();
   210 	for (int i = 1; i <= MaxClients; i++)
   211 	{
   212 		if (playerhelpers->GetGamePlayer(i)->IsInGame())
   213 		{
   214 			OnClientPutInServer(i);
   215 		}
   216 	}
   217 }
   218 
   219 void CHookManager::OnPluginUnloaded(IPlugin *plugin)
   220 {
   221 	if (!PRCH_enabled)
   222 		return;
   223 
   224 	if (!PRCH_used)
   225 		return;
   226 
   227 	if (m_usercmdsFwd->GetFunctionCount())
   228 		return;
   229 
   230 	for (size_t i = 0; i < m_runUserCmdHooks.length(); ++i)
   231 	{
   232 		delete m_runUserCmdHooks[i];
   233 	}
   234 
   235 	m_runUserCmdHooks.clear();
   236 	PRCH_used = false;
   237 }
   238 
   239 FeatureStatus CHookManager::GetFeatureStatus(FeatureType type, const char *name)
   240 {
   241 	return FeatureStatus_Available;
   242 }