Added forwards to TF2 ext for cond addition and removal (bug 4851, r=fyren).

This commit is contained in:
Nicholas Hastings 2011-04-09 00:33:55 -04:00
parent 7e06cfb471
commit 33aebeb727
9 changed files with 323 additions and 5 deletions

View File

@ -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'

View File

@ -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 ###

View File

@ -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 <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 "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<<i);
if ((conds & bit) == bit)
{
DoRemoveCond(client, i);
}
}
g_bIgnoreRemove = false;
}
void DoRemoveCond(int client, int condition)
{
g_removeCondForward->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();
}

View File

@ -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 <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$
*/
#ifndef _INCLUDE_SOURCEMOD_CONDITIONS_H_
#define _INCLUDE_SOURCEMOD_CONDITIONS_H_
#include "extension.h"
#include <jit/jit_helpers.h>
#include <jit/x86/x86_macros.h>
#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_

View File

@ -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())

View File

@ -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;
};

View File

@ -187,6 +187,10 @@
Filter="cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx"
UniqueIdentifier="{4FC737F1-C7A5-4376-A066-2A32D752A2FF}"
>
<File
RelativePath="..\conditions.cpp"
>
</File>
<File
RelativePath="..\criticals.cpp"
>
@ -221,6 +225,10 @@
Filter="h;hpp;hxx;hm;inl;inc;xsd"
UniqueIdentifier="{93995380-89BD-4b04-88EB-625FBE52EBFB}"
>
<File
RelativePath="..\conditions.h"
>
</File>
<File
RelativePath="..\criticals.h"
>

View File

@ -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"
{

View File

@ -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
*