2008-04-10 06:49:17 +02:00
|
|
|
/**
|
|
|
|
* vim: set ts=4 :
|
|
|
|
* =============================================================================
|
2008-04-11 19:16:36 +02:00
|
|
|
* SourceMod Team Fortress 2 Extension
|
2008-04-10 06:49:17 +02:00
|
|
|
* Copyright (C) 2004-2008 AlliedModders LLC. All rights reserved.
|
|
|
|
* =============================================================================
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify it under
|
|
|
|
* the terms of the GNU General Public License, version 3.0, as published by the
|
|
|
|
* Free Software Foundation.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful, but WITHOUT
|
|
|
|
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
|
|
|
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
|
|
|
|
* details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along with
|
|
|
|
* this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
* As a special exception, AlliedModders LLC gives you permission to link the
|
|
|
|
* code of this program (as well as its derivative works) to "Half-Life 2," the
|
|
|
|
* "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
|
|
|
|
* by the Valve Corporation. You must obey the GNU General Public License in
|
|
|
|
* all respects for all other code used. Additionally, AlliedModders LLC grants
|
|
|
|
* this exception to all derivative works. AlliedModders LLC defines further
|
|
|
|
* exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
|
|
|
|
* or <http://www.sourcemod.net/license.php>.
|
|
|
|
*
|
|
|
|
* Version: $Id$
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "criticals.h"
|
2013-08-12 02:18:01 +02:00
|
|
|
#include "util.h"
|
2008-04-10 06:49:17 +02:00
|
|
|
|
2013-08-29 15:27:35 +02:00
|
|
|
CritManager g_CritManager;
|
2008-04-10 06:49:17 +02:00
|
|
|
|
2013-08-29 15:47:20 +02:00
|
|
|
IForward *g_critForward = NULL;
|
2013-08-12 02:18:01 +02:00
|
|
|
|
2013-08-29 15:27:35 +02:00
|
|
|
SH_DECL_MANUALHOOK0(CalcIsAttackCriticalHelper, 0, 0, 0, bool);
|
|
|
|
SH_DECL_MANUALHOOK0(CalcIsAttackCriticalHelperNoCrits, 0, 0, 0, bool);
|
2008-04-10 06:49:17 +02:00
|
|
|
|
2013-08-29 15:27:35 +02:00
|
|
|
const char TF_WEAPON_DATATABLE[] = "DT_TFWeaponBase";
|
2008-04-10 06:49:17 +02:00
|
|
|
|
2013-08-29 15:27:35 +02:00
|
|
|
CritManager::CritManager() :
|
|
|
|
m_enabled(false),
|
|
|
|
m_hooksSetup(false)
|
2009-02-26 22:19:46 +01:00
|
|
|
{
|
2013-08-29 15:27:35 +02:00
|
|
|
m_entsHooked.Init();
|
|
|
|
}
|
2009-02-26 22:19:46 +01:00
|
|
|
|
2013-08-29 15:27:35 +02:00
|
|
|
bool CritManager::TryEnable()
|
2008-04-10 06:49:17 +02:00
|
|
|
{
|
2013-08-29 15:27:35 +02:00
|
|
|
if (!m_hooksSetup)
|
2008-04-10 06:49:17 +02:00
|
|
|
{
|
2013-08-29 15:27:35 +02:00
|
|
|
int offset;
|
2008-04-10 06:49:17 +02:00
|
|
|
|
2013-08-29 15:27:35 +02:00
|
|
|
if (!g_pGameConf->GetOffset("CalcIsAttackCriticalHelper", &offset))
|
|
|
|
{
|
|
|
|
g_pSM->LogError(myself, "Failed to find CalcIsAttackCriticalHelper offset");
|
|
|
|
return false;
|
|
|
|
}
|
2008-04-10 06:49:17 +02:00
|
|
|
|
2013-08-29 15:27:35 +02:00
|
|
|
SH_MANUALHOOK_RECONFIGURE(CalcIsAttackCriticalHelper, offset, 0, 0);
|
2008-04-10 06:49:17 +02:00
|
|
|
|
2013-08-29 15:27:35 +02:00
|
|
|
if (!g_pGameConf->GetOffset("CalcIsAttackCriticalHelperNoCrits", &offset))
|
|
|
|
{
|
|
|
|
g_pSM->LogError(myself, "Failed to find CalcIsAttackCriticalHelperNoCrits offset");
|
|
|
|
return false;
|
|
|
|
}
|
2008-04-10 06:49:17 +02:00
|
|
|
|
2013-08-29 15:27:35 +02:00
|
|
|
SH_MANUALHOOK_RECONFIGURE(CalcIsAttackCriticalHelperNoCrits, offset, 0, 0);
|
2009-02-26 22:19:46 +01:00
|
|
|
|
2013-08-29 15:27:35 +02:00
|
|
|
m_hooksSetup = true;
|
2008-04-10 06:49:17 +02:00
|
|
|
}
|
|
|
|
|
2013-08-29 15:27:35 +02:00
|
|
|
for (size_t i = playerhelpers->GetMaxClients() + 1; i < MAX_EDICTS; ++i)
|
2008-04-10 06:49:17 +02:00
|
|
|
{
|
2013-08-29 15:27:35 +02:00
|
|
|
CBaseEntity *pEntity = gamehelpers->ReferenceToEntity(i);
|
2013-08-29 15:47:20 +02:00
|
|
|
if (pEntity == NULL)
|
2013-08-29 15:27:35 +02:00
|
|
|
continue;
|
2008-04-10 06:49:17 +02:00
|
|
|
|
2013-08-29 15:27:35 +02:00
|
|
|
IServerUnknown *pUnknown = (IServerUnknown *)pEntity;
|
|
|
|
IServerNetworkable *pNetworkable = pUnknown->GetNetworkable();
|
|
|
|
if (!pNetworkable)
|
|
|
|
continue;
|
2008-07-01 11:04:00 +02:00
|
|
|
|
2013-08-29 15:27:35 +02:00
|
|
|
if (!UTIL_ContainsDataTable(pNetworkable->GetServerClass()->m_pTable, TF_WEAPON_DATATABLE))
|
|
|
|
continue;
|
2009-02-26 22:19:46 +01:00
|
|
|
|
2013-11-10 01:33:06 +01:00
|
|
|
SH_ADD_MANUALHOOK(CalcIsAttackCriticalHelper, pEntity, SH_MEMBER(&g_CritManager, &CritManager::Hook_CalcIsAttackCriticalHelper), false);
|
|
|
|
SH_ADD_MANUALHOOK(CalcIsAttackCriticalHelperNoCrits, pEntity, SH_MEMBER(&g_CritManager, &CritManager::Hook_CalcIsAttackCriticalHelperNoCrits), false);
|
2008-04-10 06:49:17 +02:00
|
|
|
|
2013-08-29 15:27:35 +02:00
|
|
|
m_entsHooked.Set(i);
|
2013-08-12 02:18:01 +02:00
|
|
|
}
|
|
|
|
|
2013-08-29 15:27:35 +02:00
|
|
|
m_enabled = true;
|
2008-04-10 06:49:17 +02:00
|
|
|
|
2013-08-29 15:27:35 +02:00
|
|
|
return true;
|
2009-02-26 22:19:46 +01:00
|
|
|
}
|
|
|
|
|
2013-08-29 15:27:35 +02:00
|
|
|
void CritManager::Disable()
|
2009-02-26 22:19:46 +01:00
|
|
|
{
|
2013-08-29 15:27:35 +02:00
|
|
|
int i = m_entsHooked.FindNextSetBit(playerhelpers->GetMaxClients() + 1);
|
|
|
|
for (i; i != -1; i = m_entsHooked.FindNextSetBit(i))
|
2009-02-26 22:19:46 +01:00
|
|
|
{
|
2013-08-29 15:27:35 +02:00
|
|
|
CBaseEntity *pEntity = gamehelpers->ReferenceToEntity(i);
|
2013-11-10 01:33:06 +01:00
|
|
|
SH_REMOVE_MANUALHOOK(CalcIsAttackCriticalHelper, pEntity, SH_MEMBER(&g_CritManager, &CritManager::Hook_CalcIsAttackCriticalHelper), false);
|
|
|
|
SH_REMOVE_MANUALHOOK(CalcIsAttackCriticalHelperNoCrits, pEntity, SH_MEMBER(&g_CritManager, &CritManager::Hook_CalcIsAttackCriticalHelperNoCrits), false);
|
2009-02-26 22:19:46 +01:00
|
|
|
|
2013-08-29 15:27:35 +02:00
|
|
|
m_entsHooked.Set(i, false);
|
2009-02-26 22:19:46 +01:00
|
|
|
}
|
2009-09-17 18:42:58 +02:00
|
|
|
|
2013-08-29 15:27:35 +02:00
|
|
|
m_enabled = false;
|
2009-09-17 18:42:58 +02:00
|
|
|
}
|
|
|
|
|
2013-08-29 15:27:35 +02:00
|
|
|
void CritManager::OnEntityCreated(CBaseEntity *pEntity, const char *classname)
|
2013-08-12 02:18:01 +02:00
|
|
|
{
|
2013-08-29 15:27:35 +02:00
|
|
|
if (!m_enabled)
|
|
|
|
return;
|
2013-08-12 02:18:01 +02:00
|
|
|
|
2013-08-29 15:27:35 +02:00
|
|
|
IServerUnknown *pUnknown = (IServerUnknown *)pEntity;
|
|
|
|
IServerNetworkable *pNetworkable = pUnknown->GetNetworkable();
|
|
|
|
if (!pNetworkable)
|
|
|
|
return;
|
2013-08-12 02:18:01 +02:00
|
|
|
|
2013-08-29 15:27:35 +02:00
|
|
|
if (!UTIL_ContainsDataTable(pNetworkable->GetServerClass()->m_pTable, TF_WEAPON_DATATABLE))
|
|
|
|
return;
|
2013-08-12 02:18:01 +02:00
|
|
|
|
2013-11-10 01:33:06 +01:00
|
|
|
SH_ADD_MANUALHOOK(CalcIsAttackCriticalHelper, pEntity, SH_MEMBER(&g_CritManager, &CritManager::Hook_CalcIsAttackCriticalHelper), false);
|
|
|
|
SH_ADD_MANUALHOOK(CalcIsAttackCriticalHelperNoCrits, pEntity, SH_MEMBER(&g_CritManager, &CritManager::Hook_CalcIsAttackCriticalHelperNoCrits), false);
|
2013-08-12 02:18:01 +02:00
|
|
|
|
2013-08-29 15:27:35 +02:00
|
|
|
m_entsHooked.Set(gamehelpers->EntityToBCompatRef(pEntity));
|
2013-08-12 02:18:01 +02:00
|
|
|
}
|
|
|
|
|
2013-08-29 15:27:35 +02:00
|
|
|
void CritManager::OnEntityDestroyed(CBaseEntity *pEntity)
|
2009-02-26 00:42:13 +01:00
|
|
|
{
|
2013-08-29 15:27:35 +02:00
|
|
|
if (!m_enabled)
|
|
|
|
return;
|
2013-08-12 02:18:01 +02:00
|
|
|
|
2013-08-29 15:27:35 +02:00
|
|
|
int index = gamehelpers->EntityToBCompatRef(pEntity);
|
|
|
|
if (index < 0 || index >= MAX_EDICTS)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (!m_entsHooked.IsBitSet(index))
|
|
|
|
return;
|
2009-02-26 00:42:13 +01:00
|
|
|
|
2013-11-10 01:33:06 +01:00
|
|
|
SH_REMOVE_MANUALHOOK(CalcIsAttackCriticalHelper, pEntity, SH_MEMBER(&g_CritManager, &CritManager::Hook_CalcIsAttackCriticalHelper), false);
|
|
|
|
SH_REMOVE_MANUALHOOK(CalcIsAttackCriticalHelperNoCrits, pEntity, SH_MEMBER(&g_CritManager, &CritManager::Hook_CalcIsAttackCriticalHelperNoCrits), false);
|
2009-02-26 00:42:13 +01:00
|
|
|
|
2013-08-29 15:27:35 +02:00
|
|
|
m_entsHooked.Set(index, false);
|
|
|
|
}
|
2009-02-26 00:42:13 +01:00
|
|
|
|
2013-11-10 01:33:06 +01:00
|
|
|
bool CritManager::Hook_CalcIsAttackCriticalHelper()
|
|
|
|
{
|
|
|
|
return Hook_CalcIsAttackCriticalHelpers(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CritManager::Hook_CalcIsAttackCriticalHelperNoCrits()
|
|
|
|
{
|
|
|
|
return Hook_CalcIsAttackCriticalHelpers(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CritManager::Hook_CalcIsAttackCriticalHelpers(bool noCrits)
|
2013-08-29 15:27:35 +02:00
|
|
|
{
|
|
|
|
CBaseEntity *pWeapon = META_IFACEPTR(CBaseEntity);
|
|
|
|
|
|
|
|
// If there's an invalid ent or invalid networkable here, we've got issues elsewhere.
|
2009-02-26 00:42:13 +01:00
|
|
|
|
2013-08-29 15:27:35 +02:00
|
|
|
IServerNetworkable *pNetWeapon = ((IServerUnknown *)pWeapon)->GetNetworkable();
|
|
|
|
ServerClass *pServerClass = pNetWeapon->GetServerClass();
|
|
|
|
if (!pServerClass)
|
2009-09-17 18:42:58 +02:00
|
|
|
{
|
2013-08-29 15:27:35 +02:00
|
|
|
g_pSM->LogError(myself, "Invalid server class on weapon.");
|
|
|
|
RETURN_META_VALUE(MRES_IGNORED, false);
|
2009-09-17 18:42:58 +02:00
|
|
|
}
|
|
|
|
|
2013-08-29 15:27:35 +02:00
|
|
|
sm_sendprop_info_t info;
|
|
|
|
if (!gamehelpers->FindSendPropInfo(pServerClass->GetName(), "m_hOwnerEntity", &info))
|
2013-08-12 02:18:01 +02:00
|
|
|
{
|
2013-08-29 15:27:35 +02:00
|
|
|
g_pSM->LogError(myself, "Could not find m_hOwnerEntity on %s", pServerClass->GetName());
|
|
|
|
RETURN_META_VALUE(MRES_IGNORED, false);
|
2013-08-12 02:18:01 +02:00
|
|
|
}
|
|
|
|
|
2013-11-10 01:33:06 +01:00
|
|
|
int returnValue;
|
|
|
|
if (noCrits)
|
|
|
|
{
|
|
|
|
returnValue = SH_MCALL(pWeapon, CalcIsAttackCriticalHelperNoCrits)() ? 1 : 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
returnValue = SH_MCALL(pWeapon, CalcIsAttackCriticalHelper)() ? 1 : 0;
|
|
|
|
}
|
2013-08-12 02:18:01 +02:00
|
|
|
|
2013-08-29 15:27:35 +02:00
|
|
|
int ownerIndex = -1;
|
|
|
|
CBaseHandle &hndl = *(CBaseHandle *) ((intptr_t)pWeapon + info.actual_offset);
|
|
|
|
CBaseEntity *pHandleEntity = gamehelpers->ReferenceToEntity(hndl.GetEntryIndex());
|
2013-08-12 02:18:01 +02:00
|
|
|
|
2013-08-29 15:47:20 +02:00
|
|
|
if (pHandleEntity != NULL && hndl == reinterpret_cast<IHandleEntity *>(pHandleEntity)->GetRefEHandle())
|
2009-02-26 00:42:13 +01:00
|
|
|
{
|
2013-08-29 15:27:35 +02:00
|
|
|
ownerIndex = hndl.GetEntryIndex();
|
2009-02-26 00:42:13 +01:00
|
|
|
}
|
|
|
|
|
2013-08-29 15:27:35 +02:00
|
|
|
g_critForward->PushCell(ownerIndex); //Client index
|
|
|
|
g_critForward->PushCell(gamehelpers->EntityToBCompatRef(pWeapon)); // Weapon index
|
|
|
|
g_critForward->PushString(gamehelpers->GetEntityClassname(pWeapon)); //Weapon classname
|
|
|
|
g_critForward->PushCellByRef(&returnValue); //return value
|
2009-02-26 00:42:13 +01:00
|
|
|
|
2013-08-29 15:27:35 +02:00
|
|
|
cell_t result = 0;
|
2010-11-29 04:41:48 +01:00
|
|
|
|
2013-08-29 15:27:35 +02:00
|
|
|
g_critForward->Execute(&result);
|
2010-11-29 04:41:48 +01:00
|
|
|
|
2013-11-10 01:33:06 +01:00
|
|
|
if (result > Pl_Continue)
|
2010-11-29 04:41:48 +01:00
|
|
|
{
|
2013-11-10 01:33:06 +01:00
|
|
|
RETURN_META_VALUE(MRES_SUPERCEDE, returnValue);
|
2010-11-29 04:41:48 +01:00
|
|
|
}
|
2013-08-12 02:18:01 +02:00
|
|
|
|
2013-08-29 15:27:35 +02:00
|
|
|
RETURN_META_VALUE(MRES_IGNORED, false);
|
2008-04-10 06:49:17 +02:00
|
|
|
}
|