diff --git a/extensions/tf2/AMBuilder b/extensions/tf2/AMBuilder index 04c15166..2a053bf3 100644 --- a/extensions/tf2/AMBuilder +++ b/extensions/tf2/AMBuilder @@ -18,6 +18,7 @@ binary.AddSourceFiles('extensions/tf2', [ 'criticals.cpp', 'holiday.cpp', 'gameplayrules.cpp', + 'conditions.cpp', 'CDetour/detours.cpp', 'sdk/smsdk_ext.cpp', 'asm/asm.c' diff --git a/extensions/tf2/Makefile b/extensions/tf2/Makefile index 25e3fd19..80d80cf2 100644 --- a/extensions/tf2/Makefile +++ b/extensions/tf2/Makefile @@ -19,7 +19,7 @@ PROJECT = game.tf2 USEMETA = true OBJECTS = sdk/smsdk_ext.cpp extension.cpp natives.cpp RegNatives.cpp criticals.cpp \ - holiday.cpp gameplayrules.cpp util.cpp CDetour/detours.cpp asm/asm.c + holiday.cpp gameplayrules.cpp conditions.cpp util.cpp CDetour/detours.cpp asm/asm.c ############################################## ### CONFIGURE ANY OTHER FLAGS/OPTIONS HERE ### diff --git a/extensions/tf2/conditions.cpp b/extensions/tf2/conditions.cpp new file mode 100644 index 00000000..32b69ed0 --- /dev/null +++ b/extensions/tf2/conditions.cpp @@ -0,0 +1,210 @@ +/** + * vim: set ts=4 : + * ============================================================================= + * SourceMod Team Fortress 2 Extension + * Copyright (C) 2004-2011 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 . + * + * 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 . + * + * Version: $Id$ + */ + +#include "extension.h" +#include "conditions.h" +#include "util.h" + +typedef int condflags_t; +#define CONDITION_TAUNTING 7 + +IForward *g_addCondForward = NULL; +IForward *g_removeCondForward = NULL; + +CDetour *detConditionAdd = NULL; +CDetour *detConditionRemove = NULL; +CDetour *detConditionRemoveAll = NULL; + +int playerCondOffset = -1; +int conditionBitsOffset = -1; + +bool g_bIgnoreRemove; + +inline condflags_t GetPlayerConds(CBaseEntity *pPlayer) +{ + condflags_t cond1 = *(condflags_t *)((intptr_t)pPlayer + playerCondOffset); + condflags_t cond2 = *(condflags_t *)((intptr_t)pPlayer + conditionBitsOffset); + return cond1|cond2; +} + +DETOUR_DECL_MEMBER2(AddCond, void, int, condition, float, duration) +{ + CBaseEntity *pPlayer = (CBaseEntity *)((intptr_t)this - playerSharedOffset->actual_offset); + condflags_t oldconds = GetPlayerConds(pPlayer); + + DETOUR_MEMBER_CALL(AddCond)(condition, duration); + + int bit = 1 << condition; + + // The player already had this condition so bug out + if ((oldconds & bit) == bit) + return; + + // The condition wasn't actually added (this can happen!) + if ((GetPlayerConds(pPlayer) & bit) != bit) + return; + + int client = gamehelpers->EntityToBCompatRef(pPlayer); + + g_addCondForward->PushCell(client); + g_addCondForward->PushCell(condition); + g_addCondForward->PushFloat(duration); + g_addCondForward->Execute(NULL, NULL); +} + +DETOUR_DECL_MEMBER2(RemoveCond, void, int, condition, bool, bForceRemove) +{ + if (g_bIgnoreRemove) + { + DETOUR_MEMBER_CALL(RemoveCond)(condition, bForceRemove); + return; + } + + CBaseEntity *pPlayer = (CBaseEntity *)((intptr_t)this - playerSharedOffset->actual_offset); + + condflags_t oldconds = GetPlayerConds(pPlayer); + int bit = 1 << condition; + + // If we didn't have it, it won't be removed + if ((oldconds & bit) != bit) + { + DETOUR_MEMBER_CALL(RemoveCond)(condition, bForceRemove); + return; + } + + DETOUR_MEMBER_CALL(RemoveCond)(condition, bForceRemove); + + // Calling RemoveCond doesn't necessarily remove the cond... + if ((GetPlayerConds(pPlayer) & bit) == bit) + { + return; + } + + int client = gamehelpers->EntityToBCompatRef(pPlayer); + + DoRemoveCond(client, condition); +} + +DETOUR_DECL_MEMBER1(RemoveAllCond, void, CBaseEntity *, unknown) +{ + // We're going to ignore Remove here since it can be called multiple times for the same cond + g_bIgnoreRemove = true; + + CBaseEntity *pPlayer = (CBaseEntity *)((intptr_t)this - playerSharedOffset->actual_offset); + + int client = gamehelpers->EntityToBCompatRef(pPlayer); + condflags_t conds = GetPlayerConds(pPlayer); + + // I don't know what the unknown is, but it's always NULL + DETOUR_MEMBER_CALL(RemoveAllCond)(unknown); + + int max = sizeof(condflags_t)*8; + for (int i = 0; i < max; i++) + { + // Taunt flag removal doesn't go through RemoveCond nor OnCondRemoved (or was mangled by compiler?) + // Better to "documentedly" give no removes rather than only in some cases + if (i == CONDITION_TAUNTING) + continue; + + int bit = (1<PushCell(client); + g_removeCondForward->PushCell(condition); + g_removeCondForward->Execute(NULL, NULL); +} + +bool InitialiseConditionDetours() +{ + sm_sendprop_info_t prop; + if (!gamehelpers->FindSendPropInfo("CTFPlayer", "m_nPlayerCond", &prop)) + { + g_pSM->LogError(myself, "Failed to find m_nPlayerCond prop offset"); + return false; + } + + playerCondOffset = prop.actual_offset; + + if (!gamehelpers->FindSendPropInfo("CTFPlayer", "_condition_bits", &prop)) + { + g_pSM->LogError(myself, "Failed to find _condition_bits prop offset"); + return false; + } + + conditionBitsOffset = prop.actual_offset; + + if (playerCondOffset == -1 || conditionBitsOffset == -1) + return false; + + detConditionAdd = DETOUR_CREATE_MEMBER(AddCond, "AddCondition"); + if (detConditionAdd == NULL) + { + g_pSM->LogError(myself, "CTFPlayerShared::AddCond detour failed"); + return false; + } + + detConditionRemove = DETOUR_CREATE_MEMBER(RemoveCond, "RemoveCondition"); + if (detConditionRemove == NULL) + { + g_pSM->LogError(myself, "CTFPlayerShared::RemoveCond detour failed"); + return false; + } + + detConditionRemoveAll = DETOUR_CREATE_MEMBER(RemoveAllCond, "RemoveAllConditions"); + if (detConditionRemoveAll == NULL) + { + g_pSM->LogError(myself, "CTFPlayerShared::RemoveAllCond detour failed"); + return false; + } + + detConditionAdd->EnableDetour(); + detConditionRemove->EnableDetour(); + detConditionRemoveAll->EnableDetour(); + + return true; +} + +void RemoveConditionDetours() +{ + detConditionAdd->Destroy(); + detConditionRemove->Destroy(); + detConditionRemoveAll->Destroy(); +} + diff --git a/extensions/tf2/conditions.h b/extensions/tf2/conditions.h new file mode 100644 index 00000000..9fe42b89 --- /dev/null +++ b/extensions/tf2/conditions.h @@ -0,0 +1,47 @@ +/** + * vim: set ts=4 : + * ============================================================================= + * SourceMod Team Fortress 2 Extension + * Copyright (C) 2004-2011 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 . + * + * 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 . + * + * Version: $Id$ + */ + +#ifndef _INCLUDE_SOURCEMOD_CONDITIONS_H_ +#define _INCLUDE_SOURCEMOD_CONDITIONS_H_ + +#include "extension.h" +#include +#include +#include "CDetour/detours.h" + +bool InitialiseConditionDetours(); +void RemoveConditionDetours(); +void DoRemoveCond(int client, int condition); + +extern IForward *g_addCondForward; +extern IForward *g_removeCondForward; + +#endif //_INCLUDE_SOURCEMOD_CONDITIONS_H_ diff --git a/extensions/tf2/extension.cpp b/extensions/tf2/extension.cpp index df98a2e8..d8472ae1 100644 --- a/extensions/tf2/extension.cpp +++ b/extensions/tf2/extension.cpp @@ -2,7 +2,7 @@ * vim: set ts=4 : * ============================================================================= * SourceMod Team Fortress 2 Extension - * Copyright (C) 2004-2008 AlliedModders LLC. All rights reserved. + * Copyright (C) 2004-2011 AlliedModders LLC. All rights reserved. * ============================================================================= * * This program is free software; you can redistribute it and/or modify it under @@ -37,6 +37,7 @@ #include "sm_trie_tpl.h" #include "criticals.h" #include "holiday.h" +#include "conditions.h" #include "gameplayrules.h" #include "CDetour/detours.h" @@ -111,6 +112,8 @@ bool TF2Tools::SDK_OnLoad(char *error, size_t maxlength, bool late) g_critForward = forwards->CreateForward("TF2_CalcIsAttackCritical", ET_Hook, 4, NULL, Param_Cell, Param_Cell, Param_String, Param_CellByRef); g_getHolidayForward = forwards->CreateForward("TF2_OnGetHoliday", ET_Event, 1, NULL, Param_CellByRef); + g_addCondForward = forwards->CreateForward("TF2_OnConditionAdded", ET_Ignore, 3, NULL, Param_Cell, Param_Cell, Param_Float); + g_removeCondForward = forwards->CreateForward("TF2_OnConditionRemoved", ET_Ignore, 2, NULL, Param_Cell, Param_Cell); g_waitingPlayersStartForward = forwards->CreateForward("TF2_OnWaitingForPlayersStart", ET_Ignore, 0, NULL); g_waitingPlayersEndForward = forwards->CreateForward("TF2_OnWaitingForPlayersEnd", ET_Ignore, 0, NULL); @@ -118,6 +121,7 @@ bool TF2Tools::SDK_OnLoad(char *error, size_t maxlength, bool late) m_CritDetoursEnabled = false; m_GetHolidayDetourEnabled = false; + m_CondChecksEnabled = false; m_RulesDetoursEnabled = false; return true; @@ -163,6 +167,8 @@ void TF2Tools::SDK_OnUnload() forwards->ReleaseForward(g_critForward); forwards->ReleaseForward(g_getHolidayForward); + forwards->ReleaseForward(g_addCondForward); + forwards->ReleaseForward(g_removeCondForward); forwards->ReleaseForward(g_waitingPlayersStartForward); forwards->ReleaseForward(g_waitingPlayersEndForward); } @@ -317,6 +323,13 @@ void TF2Tools::OnPluginLoaded(IPlugin *plugin) m_GetHolidayDetourEnabled = InitialiseGetHolidayDetour(); } + if (!m_CondChecksEnabled + && ( g_addCondForward->GetFunctionCount() || g_removeCondForward->GetFunctionCount() ) + ) + { + m_CondChecksEnabled = InitialiseConditionDetours(); + } + if (!m_RulesDetoursEnabled && ( g_waitingPlayersStartForward->GetFunctionCount() || g_waitingPlayersEndForward->GetFunctionCount() ) ) @@ -337,6 +350,14 @@ void TF2Tools::OnPluginUnloaded(IPlugin *plugin) RemoveGetHolidayDetour(); m_GetHolidayDetourEnabled = false; } + if (m_CondChecksEnabled) + { + if (!g_addCondForward->GetFunctionCount() && !g_removeCondForward->GetFunctionCount()) + { + RemoveConditionDetours(); + m_CondChecksEnabled = false; + } + } if (m_RulesDetoursEnabled) { if (!g_waitingPlayersStartForward->GetFunctionCount() && !g_waitingPlayersEndForward->GetFunctionCount()) diff --git a/extensions/tf2/extension.h b/extensions/tf2/extension.h index 8a99ee79..7f9d3fc3 100644 --- a/extensions/tf2/extension.h +++ b/extensions/tf2/extension.h @@ -2,7 +2,7 @@ * vim: set ts=4 : * ============================================================================= * SourceMod Team Fortress 2 Extension - * Copyright (C) 2004-2008 AlliedModders LLC. All rights reserved. + * Copyright (C) 2004-2011 AlliedModders LLC. All rights reserved. * ============================================================================= * * This program is free software; you can redistribute it and/or modify it under @@ -114,6 +114,7 @@ public: private: bool m_CritDetoursEnabled; bool m_GetHolidayDetourEnabled; + bool m_CondChecksEnabled; bool m_RulesDetoursEnabled; }; diff --git a/extensions/tf2/msvc9/tf2.vcproj b/extensions/tf2/msvc9/tf2.vcproj index 28e35db6..6c723002 100644 --- a/extensions/tf2/msvc9/tf2.vcproj +++ b/extensions/tf2/msvc9/tf2.vcproj @@ -187,6 +187,10 @@ Filter="cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx" UniqueIdentifier="{4FC737F1-C7A5-4376-A066-2A32D752A2FF}" > + + @@ -221,6 +225,10 @@ Filter="h;hpp;hxx;hm;inl;inc;xsd" UniqueIdentifier="{93995380-89BD-4b04-88EB-625FBE52EBFB}" > + + diff --git a/gamedata/sm-tf2.games.txt b/gamedata/sm-tf2.games.txt index e3fdee40..82a2fb23 100644 --- a/gamedata/sm-tf2.games.txt +++ b/gamedata/sm-tf2.games.txt @@ -67,14 +67,14 @@ "AddCondition" { "library" "server" - "windows" "\xD9\x44\x2A\x2A\x56\x57\x8B\x7C\x2A\x2A\x8B\xF1\x8B\x86\x2A\x2A\x2A\x00\x50\x51\xD9\x2A\x2A\x57" + "windows" "\x2A\x2A\x2A\x2A\x2A\x2A\x8B\x7C\x2A\x2A\x8B\xF1\x8B\x86\x2A\x2A\x2A\x00\x50\x51\xD9\x2A\x2A\x57" "linux" "@_ZN15CTFPlayerShared7AddCondEif" "mac" "@_ZN15CTFPlayerShared7AddCondEif" } "RemoveCondition" { "library" "server" - "windows" "\x8B\x44\x2A\x2A\x56\x57\x8B\x7C\x2A\x2A\x8B\xF1\x50\x57\x8D\x8E\x2A\x2A\x2A\x2A\xE8" + "windows" "\x2A\x2A\x2A\x2A\x2A\x2A\x8B\x7C\x2A\x2A\x8B\xF1\x50\x57\x8D\x8E\x2A\x2A\x2A\x2A\xE8" "linux" "@_ZN15CTFPlayerShared10RemoveCondEib" "mac" "@_ZN15CTFPlayerShared10RemoveCondEib" } @@ -113,6 +113,13 @@ "linux" "@_Z21DuelMiniGame_IsInDuelP9CTFPlayer" "mac" "@_Z21DuelMiniGame_IsInDuelP9CTFPlayer" } + "RemoveAllConditions" + { + "library" "server" + "windows" "\x83\xEC\x2A\x53\x55\x56\x8B\xE9\x57\x8D\x2A\x2A\x2A\x2A\x2A\xE8\x2A\x2A\x2A\x2A\x8D" + "linux" "@_ZN15CTFPlayerShared13RemoveAllCondEP9CTFPlayer" + "mac" "@_ZN15CTFPlayerShared13RemoveAllCondEP9CTFPlayer" + } } "Offsets" { diff --git a/plugins/include/tf2.inc b/plugins/include/tf2.inc index 9eb464b4..3b705227 100644 --- a/plugins/include/tf2.inc +++ b/plugins/include/tf2.inc @@ -283,6 +283,29 @@ forward Action:TF2_OnGetHoliday(&TFHoliday:holiday); */ native bool:TF2_IsPlayerInDuel(client); +/** + * Called after a condition is added to a player + * + * @note Actual duration can differ based on various events ingame. + * + * @param client Index of the client to which the conditon is being added. + * @param condition Condition that is being added. + * @param duration Initial duration of the condition or -1 for infinite. + * @noreturn + */ +forward TF2_OnConditionAdded(client, TFCond:condition, Float:duration); + +/** + * Called after a condition is removed from a player + * + * @note This will never be called for the removal of TFCond_Taunting. + * + * @param client Index of the client to which the condition is being removed. + * @param condition Condition that is being removed. + * @noreturn + */ +forward TF2_OnConditionRemoved(client, TFCond:condition); + /** * Called when the server enters the Waiting for Players round state *