extensions/tf2/criticals.cpp
author Nicholas Hastings <skamonkey@gmail.com>
Mon Jul 29 10:03:18 2013 -0400 (2013-07-29)
changeset 3960 d50b36dc6821
parent 3159 608c56ebced9
child 4001 608bc5be1ef0
permissions -rw-r--r--
Updated for latest hl2sdk-dota changes.
     1 /**
     2  * vim: set ts=4 :
     3  * =============================================================================
     4  * SourceMod Team Fortress 2 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 "criticals.h"
    33 
    34 IServerGameEnts *gameents = NULL;
    35 
    36 CDetour *calcIsAttackCriticalDetour = NULL;
    37 CDetour *calcIsAttackCriticalMeleeDetour = NULL;
    38 CDetour *calcIsAttackCriticalBowDetour = NULL;
    39 
    40 IForward *g_critForward = NULL;
    41 
    42 enum DetourResult
    43 {
    44 	Result_Ignore,
    45 	Result_NoCrit,
    46 	Result_Crit,
    47 };
    48 
    49 int CheckBaseHandle(CBaseHandle &hndl)
    50 {
    51 	if (!hndl.IsValid())
    52 	{
    53 		return -1;
    54 	}
    55 
    56 	int index = hndl.GetEntryIndex();
    57 
    58 	edict_t *pStoredEdict;
    59 
    60 	pStoredEdict = engine->PEntityOfEntIndex(index);
    61 
    62 	if (pStoredEdict == NULL)
    63 	{
    64 		return -1;
    65 	}
    66 
    67 	IServerEntity *pSE = pStoredEdict->GetIServerEntity();
    68 
    69 	if (pSE == NULL)
    70 	{
    71 		return -1;
    72 	}
    73 
    74 	if (pSE->GetRefEHandle() != hndl)
    75 	{
    76 		return -1;
    77 	}
    78 
    79 	return index;
    80 }
    81 
    82 DetourResult DetourCallback(CBaseEntity *pEnt)
    83 {
    84 	edict_t *pEdict = gameents->BaseEntityToEdict((CBaseEntity *)pEnt);
    85 
    86 	if (!pEdict)
    87 	{
    88 		g_pSM->LogMessage(myself, "Entity Error");
    89 		return Result_Ignore;
    90 	}
    91 
    92 	sm_sendprop_info_t info;
    93 
    94 	if (!gamehelpers->FindSendPropInfo(pEdict->GetNetworkable()->GetServerClass()->GetName(), "m_hOwnerEntity", &info))
    95 	{
    96 		g_pSM->LogMessage(myself, "Offset Error");
    97 		return Result_Ignore;
    98 	}
    99 
   100 	if (!g_critForward)
   101 	{
   102 		g_pSM->LogMessage(myself, "Invalid Forward");
   103 		return Result_Ignore;
   104 	}
   105 
   106 	int returnValue=0;
   107 
   108 	CBaseHandle &hndl = *(CBaseHandle *)((uint8_t *)pEnt + info.actual_offset);
   109 	int index = CheckBaseHandle(hndl);
   110 
   111 	g_critForward->PushCell(index); //Client index
   112 	g_critForward->PushCell(engine->IndexOfEdict(pEdict)); // Weapon index
   113 	g_critForward->PushString(pEdict->GetClassName()); //Weapon classname
   114 	g_critForward->PushCellByRef(&returnValue); //return value
   115 
   116 	cell_t result = 0;
   117 
   118 	g_critForward->Execute(&result);
   119 
   120 	if (result)
   121 	{
   122 		if (returnValue)
   123 		{
   124 			return Result_Crit;
   125 		}
   126 		else
   127 		{
   128 			return Result_NoCrit;
   129 		}
   130 	}
   131 	else
   132 	{
   133 		return Result_Ignore;
   134 	}
   135 }
   136 
   137 DETOUR_DECL_MEMBER0(CalcIsAttackCriticalHelperMelee, bool)
   138 {
   139 	DetourResult result = DetourCallback((CBaseEntity *)this);
   140 
   141 	if (result == Result_Ignore)
   142 	{
   143 		return DETOUR_MEMBER_CALL(CalcIsAttackCriticalHelperMelee)();
   144 	}
   145 	else if (result == Result_NoCrit)
   146 	{
   147 		return 0;
   148 	}
   149 	else
   150 	{
   151 		return 1;
   152 	}
   153 }
   154 
   155 DETOUR_DECL_MEMBER0(CalcIsAttackCriticalHelper, bool)
   156 {
   157 	DetourResult result = DetourCallback((CBaseEntity *)this);
   158 
   159 	if (result == Result_Ignore)
   160 	{
   161 		return DETOUR_MEMBER_CALL(CalcIsAttackCriticalHelper)();
   162 	}
   163 	else if (result == Result_NoCrit)
   164 	{
   165 		return 0;
   166 	}
   167 	else
   168 	{
   169 		return 1;
   170 	}
   171 }
   172 
   173 DETOUR_DECL_MEMBER0(CalcIsAttackCriticalHelperBow, bool)
   174 {
   175 	DetourResult result = DetourCallback((CBaseEntity *)this);
   176 
   177 	if (result == Result_Ignore)
   178 	{
   179 		return DETOUR_MEMBER_CALL(CalcIsAttackCriticalHelperBow)();
   180 	}
   181 	else if (result == Result_NoCrit)
   182 	{
   183 		return 0;
   184 	}
   185 	else
   186 	{
   187 		return 1;
   188 	}
   189 }
   190 
   191 bool InitialiseCritDetours()
   192 {
   193 	calcIsAttackCriticalDetour = DETOUR_CREATE_MEMBER(CalcIsAttackCriticalHelper, "CalcCritical");
   194 	calcIsAttackCriticalMeleeDetour = DETOUR_CREATE_MEMBER(CalcIsAttackCriticalHelperMelee, "CalcCriticalMelee");
   195 	calcIsAttackCriticalBowDetour = DETOUR_CREATE_MEMBER(CalcIsAttackCriticalHelperBow, "CalcCriticalBow");
   196 
   197 	bool HookCreated = false;
   198 
   199 	if (calcIsAttackCriticalDetour != NULL)
   200 	{
   201 		calcIsAttackCriticalDetour->EnableDetour();
   202 		HookCreated = true;
   203 	}
   204 
   205 	if (calcIsAttackCriticalMeleeDetour != NULL)
   206 	{
   207 		calcIsAttackCriticalMeleeDetour->EnableDetour();
   208 		HookCreated = true;
   209 	}
   210 
   211 	if (calcIsAttackCriticalBowDetour != NULL)
   212 	{
   213 		calcIsAttackCriticalBowDetour->EnableDetour();
   214 		HookCreated = true;
   215 	}
   216 
   217 	if (HookCreated)
   218 	{
   219 		return true;
   220 	}
   221 
   222 	g_pSM->LogError(myself, "No critical hit forwards could be initialized - Disabled critical hit hooks");
   223 
   224 	return false;
   225 }
   226 
   227 void RemoveCritDetours()
   228 {
   229 	if (calcIsAttackCriticalDetour != NULL)
   230 	{
   231 		calcIsAttackCriticalDetour->Destroy();
   232 		calcIsAttackCriticalDetour = NULL;
   233 	}
   234 
   235 	if (calcIsAttackCriticalMeleeDetour != NULL)
   236 	{
   237 		calcIsAttackCriticalMeleeDetour->Destroy();
   238 		calcIsAttackCriticalMeleeDetour = NULL;
   239 	}
   240 
   241 	if (calcIsAttackCriticalBowDetour != NULL)
   242 	{
   243 		calcIsAttackCriticalBowDetour->Destroy();
   244 		calcIsAttackCriticalBowDetour = NULL;
   245 	}
   246 }