extensions/sdkhooks/natives.cpp
author David Anderson <dvander@alliedmods.net>
Thu Aug 08 22:02:42 2013 -0700 (2013-08-08)
changeset 3980 dc7dfcd855dc
parent 3963 39fc1ce67130
child 4054 0964336bb978
permissions -rw-r--r--
More Windows build fix.
     1 /**
     2  * vim: set ts=4 :
     3  * =============================================================================
     4  * Source SDK Hooks Extension
     5  * Copyright (C) 2010-2012 Nicholas Hastings
     6  * Copyright (C) 2009-2010 Erik Minekus
     7  * =============================================================================
     8  *
     9  * This program is free software; you can redistribute it and/or modify it under
    10  * the terms of the GNU General Public License, version 3.0, as published by the
    11  * Free Software Foundation.
    12  * 
    13  * This program is distributed in the hope that it will be useful, but WITHOUT
    14  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
    15  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
    16  * details.
    17  *
    18  * You should have received a copy of the GNU General Public License along with
    19  * this program.  If not, see <http://www.gnu.org/licenses/>.
    20  *
    21  * As a special exception, AlliedModders LLC gives you permission to link the
    22  * code of this program (as well as its derivative works) to "Half-Life 2," the
    23  * "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
    24  * by the Valve Corporation.  You must obey the GNU General Public License in
    25  * all respects for all other code used.  Additionally, AlliedModders LLC grants
    26  * this exception to all derivative works.  AlliedModders LLC defines further
    27  * exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
    28  * or <http://www.sourcemod.net/license.php>.
    29  *
    30  * Version: $Id$
    31  */
    32 
    33 #include "extension.h"
    34 #include "natives.h"
    35 #include <compat_wrappers.h>
    36 
    37 #if defined SH_DECL_MANUALEXTERN1
    38 SH_DECL_MANUALEXTERN1(OnTakeDamage, int, CTakeDamageInfoHack &);
    39 SH_DECL_MANUALEXTERN3_void(Weapon_Drop, CBaseCombatWeapon *, const Vector *, const Vector *);
    40 #endif
    41 
    42 cell_t Native_Hook(IPluginContext *pContext, const cell_t *params)
    43 {
    44 	int entity = (int)params[1];
    45 	SDKHookType type = (SDKHookType)params[2];
    46 	IPluginFunction *callback = pContext->GetFunctionById(params[3]);
    47 	HookReturn ret = g_Interface.Hook(entity, type, callback);
    48 	switch (ret)
    49 	{
    50 		case HookRet_InvalidEntity:
    51 			pContext->ThrowNativeError("Entity %d is invalid", entity);
    52 			break;
    53 		case HookRet_InvalidHookType:
    54 			pContext->ThrowNativeError("Invalid hook type specified");
    55 			break;
    56 		case HookRet_NotSupported:
    57 			pContext->ThrowNativeError("Hook type not supported on this game");
    58 			break;
    59 		case HookRet_BadEntForHookType:
    60 		{
    61 			const char * pClassname = gamehelpers->GetEntityClassname(PEntityOfEntIndex(gamehelpers->ReferenceToIndex(params[1])));
    62 			if (!pClassname)
    63 			{
    64 				pContext->ThrowNativeError("Hook type not valid for this type of entity (%i).", entity);
    65 			}
    66 			else
    67 			{
    68 				pContext->ThrowNativeError("Hook type not valid for this type of entity (%s)", pClassname);
    69 			}
    70 			
    71 			break;
    72 		}
    73 	}
    74 
    75 	return 0;
    76 }
    77 
    78 cell_t Native_HookEx(IPluginContext *pContext, const cell_t *params)
    79 {
    80 	int entity = (int)params[1];
    81 	SDKHookType type = (SDKHookType)params[2];
    82 	IPluginFunction *callback = pContext->GetFunctionById(params[3]);
    83 	HookReturn ret = g_Interface.Hook(entity, type, callback);
    84 	if (ret == HookRet_Successful)
    85 		return true;
    86 
    87 	return false;
    88 }
    89 
    90 cell_t Native_Unhook(IPluginContext *pContext, const cell_t *params)
    91 {
    92 	int entity = (int)params[1];
    93 	SDKHookType type = (SDKHookType)params[2];
    94 	IPluginFunction *callback = pContext->GetFunctionById(params[3]);
    95 
    96 	for(int i = g_HookList.Count() - 1; i >= 0; i--)
    97 	{
    98 		if(g_HookList[i].entity == entity && g_HookList[i].type == type && g_HookList[i].callback == callback)
    99 			g_Interface.Unhook(i);
   100 	}
   101 
   102 	return 0;
   103 }
   104 
   105 cell_t Native_TakeDamage(IPluginContext *pContext, const cell_t *params)
   106 {
   107 // todo: fix code to not require this >.<
   108 #if !defined SH_DECL_MANUALEXTERN1
   109 	pContext->ThrowNativeError("SDKHooks_TakeDamage is not supported on this engine.");
   110 #else
   111 	CBaseEntity *pVictim = UTIL_GetCBaseEntity(params[1]);
   112 	if (!pVictim)
   113 		return pContext->ThrowNativeError("Invalid entity index %d for victim", params[1]);
   114 	
   115 	CBaseEntity *pInflictor = UTIL_GetCBaseEntity(params[2]);
   116 	if (!pInflictor)
   117 		return pContext->ThrowNativeError("Invalid entity index %d for inflictor", params[2]);
   118 
   119 	CBaseEntity *pAttacker = NULL;
   120 	if (params[3] != -1)
   121 	{
   122 		pAttacker = UTIL_GetCBaseEntity(params[3]);
   123 		if (!pAttacker)
   124 		{
   125 			return pContext->ThrowNativeError("Invalid entity index %d for attackerr", params[3]);
   126 		}
   127 	}
   128 
   129 	float flDamage = sp_ctof(params[4]);
   130 	int iDamageType = params[5];
   131 
   132 	CBaseEntity *pWeapon = NULL;
   133 	if (params[6] != -1)
   134 	{
   135 		pAttacker = UTIL_GetCBaseEntity(params[6]);
   136 		if (!pAttacker)
   137 		{
   138 			return pContext->ThrowNativeError("Invalid entity index %d for weapon", params[6]);
   139 		}
   140 	}
   141 
   142 	Vector vecDamageForce = Vector(0.0f, 0.0f, 0.0f);
   143 	cell_t *addr;
   144 	int err;
   145 	if ((err = pContext->LocalToPhysAddr(params[7], &addr)) != SP_ERROR_NONE)
   146 	{
   147 		return pContext->ThrowNativeError("Could not read damageForce vector");
   148 	}
   149 
   150 	if (addr != pContext->GetNullRef(SP_NULL_VECTOR))
   151 	{
   152 		vecDamageForce = Vector(
   153 			sp_ctof(addr[0]),
   154 			sp_ctof(addr[1]),
   155 			sp_ctof(addr[2]));
   156 	}
   157 
   158 	Vector vecDamagePosition = vec3_origin;
   159 	if ((err = pContext->LocalToPhysAddr(params[8], &addr)) != SP_ERROR_NONE)
   160 	{
   161 		return pContext->ThrowNativeError("Could not read damagePosition vector");
   162 	}
   163 
   164 	if (addr != pContext->GetNullRef(SP_NULL_VECTOR))
   165 	{
   166 		vecDamagePosition = Vector(
   167 			sp_ctof(addr[0]),
   168 			sp_ctof(addr[1]),
   169 			sp_ctof(addr[2]));
   170 	}
   171 
   172 	CTakeDamageInfoHack info(pInflictor, pAttacker, flDamage, iDamageType, pWeapon, vecDamageForce, vecDamagePosition);
   173 	SH_MCALL(pVictim, OnTakeDamage)((CTakeDamageInfoHack &)info);
   174 #endif
   175 
   176 	return 0;
   177 }
   178 
   179 cell_t Native_DropWeapon(IPluginContext *pContext, const cell_t *params)
   180 {
   181 // todo: fix code to not require this >.<
   182 #if !defined SH_DECL_MANUALEXTERN1
   183 	pContext->ThrowNativeError("SDKHooks_DropWeapon is not supported on this engine.");
   184 #else
   185 	CBaseEntity *pPlayer = UTIL_GetCBaseEntity(params[1], true);
   186 	if (!pPlayer)
   187 		return pContext->ThrowNativeError("Invalid client index %d", params[1]);
   188 	
   189 	CBaseEntity *pWeapon = UTIL_GetCBaseEntity(params[2]);
   190 	if (!pWeapon)
   191 		return pContext->ThrowNativeError("Invalid entity index %d for weapon", params[2]);
   192 
   193 	sm_sendprop_info_t spi;
   194 	IServerUnknown *pUnk = (IServerUnknown *)pWeapon;
   195 	IServerNetworkable *pNet = pUnk->GetNetworkable();
   196 
   197 	if (!UTIL_FindDataTable(pNet->GetServerClass()->m_pTable, "DT_BaseCombatWeapon", &spi, 0))
   198 		return pContext->ThrowNativeError("Entity index %d is not a weapon", params[2]);
   199 
   200 	if (!gamehelpers->FindSendPropInfo("CBaseCombatWeapon", "m_hOwnerEntity", &spi))
   201 		return pContext->ThrowNativeError("Invalid entity index %d for weapon", params[2]);
   202 
   203 	CBaseHandle &hndl = *(CBaseHandle *)((uint8_t *)pWeapon + spi.actual_offset);
   204 	if (params[1] != hndl.GetEntryIndex())
   205 		return pContext->ThrowNativeError("Weapon %d is not owned by client %d", params[2], params[1]);
   206 
   207 	Vector vecTarget;
   208 	cell_t *addr;
   209 	int err;
   210 	if ((err = pContext->LocalToPhysAddr(params[3], &addr)) != SP_ERROR_NONE)
   211 	{
   212 		return pContext->ThrowNativeError("Could not read vecTarget vector");
   213 	}
   214 
   215 	if (addr != pContext->GetNullRef(SP_NULL_VECTOR))
   216 	{
   217 		vecTarget = Vector(
   218 			sp_ctof(addr[0]),
   219 			sp_ctof(addr[1]),
   220 			sp_ctof(addr[2]));
   221 	}
   222 	else
   223 	{
   224 		SH_MCALL(pPlayer, Weapon_Drop)((CBaseCombatWeapon *)pWeapon, NULL, NULL);
   225 		return 0;
   226 	}
   227 
   228 	Vector vecVelocity;
   229 	if ((err = pContext->LocalToPhysAddr(params[8], &addr)) != SP_ERROR_NONE)
   230 	{
   231 		return pContext->ThrowNativeError("Could not read vecVelocity vector");
   232 	}
   233 
   234 	if (addr != pContext->GetNullRef(SP_NULL_VECTOR))
   235 	{
   236 		vecVelocity = Vector(
   237 			sp_ctof(addr[0]),
   238 			sp_ctof(addr[1]),
   239 			sp_ctof(addr[2]));
   240 	}
   241 	else
   242 	{
   243 		SH_MCALL(pPlayer, Weapon_Drop)((CBaseCombatWeapon *)pWeapon, &vecTarget, NULL);
   244 		return 0;
   245 	}
   246 
   247 	SH_MCALL(pPlayer, Weapon_Drop)((CBaseCombatWeapon *)pWeapon, &vecTarget, &vecVelocity);
   248 #endif
   249 
   250 	return 0;
   251 }