Merge pull request #266 from alliedmodders/tfconds-reducks

Replace frameloop for player condition checks in TF2 ext with sendproxies (r=All of the KyleS).
This commit is contained in:
Nicholas Hastings 2015-02-27 04:43:15 -08:00
commit fdc3c0791e
4 changed files with 163 additions and 149 deletions

View File

@ -2,7 +2,7 @@
* vim: set ts=4 : * vim: set ts=4 :
* ============================================================================= * =============================================================================
* SourceMod Team Fortress 2 Extension * SourceMod Team Fortress 2 Extension
* Copyright (C) 2004-2011 AlliedModders LLC. All rights reserved. * Copyright (C) 2004-2015 AlliedModders LLC. All rights reserved.
* ============================================================================= * =============================================================================
* *
* This program is free software; you can redistribute it and/or modify it under * This program is free software; you can redistribute it and/or modify it under
@ -31,152 +31,133 @@
#include "extension.h" #include "extension.h"
#include "conditions.h" #include "conditions.h"
#include "util.h"
#include <iplayerinfo.h>
#include <bitvec.h>
const int TF_MAX_CONDITIONS = 128;
typedef CBitVec<TF_MAX_CONDITIONS> condbitvec_t;
condbitvec_t g_PlayerActiveConds[SM_MAXPLAYERS + 1];
IForward *g_addCondForward = NULL; IForward *g_addCondForward = NULL;
IForward *g_removeCondForward = NULL; IForward *g_removeCondForward = NULL;
int playerCondOffset = -1; struct CondChangeData_t
int playerCondExOffset = -1;
int playerCondEx2Offset = -1;
int playerCondEx3Offset = -1;
int conditionBitsOffset = -1;
bool g_bIgnoreRemove;
#define MAX_CONDS (sizeof(uint64_t) * 8)
inline void GetPlayerConds(CBaseEntity *pPlayer, condbitvec_t *pOut)
{ {
uint32_t tmp = *(uint32_t *)((intptr_t)pPlayer + playerCondOffset); CBaseEntity *pPlayer;
tmp |= *(uint32_t *)((intptr_t)pPlayer + conditionBitsOffset); PlayerConditionsMgr::CondVar var;
pOut->SetDWord(0, tmp); int newConds;
tmp = *(uint32_t *)((intptr_t)pPlayer + playerCondExOffset); };
pOut->SetDWord(1, tmp);
tmp = *(uint32_t *)((intptr_t)pPlayer + playerCondEx2Offset); static void HandleCondChange(void *pData)
pOut->SetDWord(2, tmp); {
tmp = *(uint32_t *)((intptr_t) pPlayer + playerCondEx3Offset); auto *pCondData = reinterpret_cast<CondChangeData_t *>(pData);
pOut->SetDWord(3, tmp); g_CondMgr.ProcessCondChange(pCondData);
delete pCondData;
} }
inline void CondBitVecAndNot(const condbitvec_t &src, const condbitvec_t &addStr, condbitvec_t *out) void PlayerConditionsMgr::ProcessCondChange(CondChangeData_t *pCondData)
{ {
static_assert(TF_MAX_CONDITIONS == 128, "CondBitVecAndNot hack is hardcoded for 128-bit bitvec."); int client = gamehelpers->EntityToBCompatRef(pCondData->pPlayer);
if (!playerhelpers->GetGamePlayer(client)->IsInGame())
// CBitVec has And and Not, but not a simple, combined AndNot.
// We'll also treat the halves as two 64-bit ints instead of four 32-bit ints
// as a minor optimization (maybe?) that the compiler is not making itself.
uint64 *pDest = (uint64 *)out->Base();
const uint64 *pOperand1 = (const uint64 *) src.Base();
const uint64 *pOperand2 = (const uint64 *) addStr.Base();
pDest[0] = pOperand1[0] & ~pOperand2[0];
pDest[1] = pOperand1[1] & ~pOperand2[1];
}
void Conditions_OnGameFrame(bool simulating)
{
if (!simulating)
return; return;
static condbitvec_t newconds; int newConds = 0;
int prevConds = 0;
static condbitvec_t addedconds; CondVar var = pCondData->var;
static condbitvec_t removedconds;
int maxClients = gpGlobals->maxClients; if (var == m_nPlayerCond)
for (int i = 1; i <= maxClients; i++)
{ {
IGamePlayer *pPlayer = playerhelpers->GetGamePlayer(i); prevConds = m_OldConds[client][_condition_bits] | m_OldConds[client][var];
if (!pPlayer->IsInGame() || pPlayer->IsSourceTV() || pPlayer->IsReplay()) newConds = m_OldConds[client][_condition_bits] | pCondData->newConds;
continue; }
else if (var == _condition_bits)
{
prevConds = m_OldConds[client][m_nPlayerCond] | m_OldConds[client][var];
newConds = m_OldConds[client][m_nPlayerCond] | pCondData->newConds;
}
else
{
prevConds = m_OldConds[client][var];
newConds = pCondData->newConds;
}
CBaseEntity *pEntity = gamehelpers->ReferenceToEntity(i); if (prevConds != newConds)
condbitvec_t &oldconds = g_PlayerActiveConds[i]; {
GetPlayerConds(pEntity, &newconds); int changedConds = newConds ^ prevConds;
int addedConds = changedConds & newConds;
int removedConds = changedConds & prevConds;
m_OldConds[client][var] = newConds;
if (oldconds == newconds) for (int i = 0; i < 32; i++)
continue;
CondBitVecAndNot(newconds, oldconds, &addedconds);
CondBitVecAndNot(oldconds, newconds, &removedconds);
int bit;
bit = -1;
while ((bit = addedconds.FindNextSetBit(bit + 1)) != -1)
{ {
g_addCondForward->PushCell(i); if (addedConds & (1 << i))
g_addCondForward->PushCell(bit); {
g_addCondForward->Execute(NULL, NULL); g_addCondForward->PushCell(client);
g_addCondForward->PushCell(i + m_CondOffset[var]);
g_addCondForward->Execute(NULL);
}
else if (removedConds & (1 << i))
{
g_removeCondForward->PushCell(client);
g_removeCondForward->PushCell(i + m_CondOffset[var]);
g_removeCondForward->Execute(NULL);
}
} }
bit = -1;
while ((bit = removedconds.FindNextSetBit(bit + 1)) != -1)
{
g_removeCondForward->PushCell(i);
g_removeCondForward->PushCell(bit);
g_removeCondForward->Execute(NULL, NULL);
}
g_PlayerActiveConds[i] = newconds;
} }
} }
bool InitialiseConditionChecks() template<PlayerConditionsMgr::CondVar CondVar>
static void OnPlayerCondChange(const SendProp *pProp, const void *pStructBase, const void *pData, DVariant *pOut, int iElement, int objectID)
{ {
sm_sendprop_info_t prop; g_CondMgr.OnConVarChange(CondVar, pProp, pStructBase, pData, pOut, iElement, objectID);
if (!gamehelpers->FindSendPropInfo("CTFPlayer", "m_nPlayerCond", &prop)) }
void PlayerConditionsMgr::OnConVarChange(CondVar var, const SendProp *pProp, const void *pStructBase, const void *pData, DVariant *pOut, int iElement, int objectID)
{
auto pCondData = new CondChangeData_t;
pCondData->pPlayer = (CBaseEntity *)((intp)pData - GetPropOffs(var));
pCondData->var = var;
pCondData->newConds = *(int *)pData;
g_pSM->AddFrameAction(&HandleCondChange, pCondData);
if (m_BackupProxyFns[var] != nullptr)
m_BackupProxyFns[var](pProp, pStructBase, pData, pOut, iElement, objectID);
}
template <PlayerConditionsMgr::CondVar var>
bool PlayerConditionsMgr::SetupProp(const char *varname)
{
if (!gamehelpers->FindSendPropInfo("CTFPlayer", varname, &m_CondVarProps[var]))
{ {
g_pSM->LogError(myself, "Failed to find m_nPlayerCond prop offset"); g_pSM->LogError(myself, "Failed to find %s prop offset", varname);
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; return false;
} }
conditionBitsOffset = prop.actual_offset; m_BackupProxyFns[var] = GetProp(var)->GetProxyFn();
GetProp(var)->SetProxyFn(OnPlayerCondChange<var>);
if (!gamehelpers->FindSendPropInfo("CTFPlayer", "m_nPlayerCondEx", &prop)) return true;
{ }
g_pSM->LogError(myself, "Failed to find m_nPlayerCondEx prop offset");
PlayerConditionsMgr::PlayerConditionsMgr()
{
m_CondOffset[m_nPlayerCond] = 0;
m_CondOffset[_condition_bits] = 0;
m_CondOffset[m_nPlayerCondEx] = 32;
m_CondOffset[m_nPlayerCondEx2] = 64;
m_CondOffset[m_nPlayerCondEx3] = 96;
}
bool PlayerConditionsMgr::Init()
{
memset(m_BackupProxyFns, 0, sizeof(m_BackupProxyFns));
bool bFoundProps = SetupProp<m_nPlayerCond>("m_nPlayerCond")
&& SetupProp<_condition_bits>("_condition_bits")
&& SetupProp<m_nPlayerCondEx>("m_nPlayerCondEx")
&& SetupProp<m_nPlayerCondEx2>("m_nPlayerCondEx2")
&& SetupProp<m_nPlayerCondEx3>("m_nPlayerCondEx3");
if (!bFoundProps)
return false; return false;
}
playerCondExOffset = prop.actual_offset;
if (!gamehelpers->FindSendPropInfo("CTFPlayer", "m_nPlayerCondEx2", &prop)) playerhelpers->AddClientListener(this);
{
g_pSM->LogError(myself, "Failed to find m_nPlayerCondEx2 prop offset");
return false;
}
playerCondEx2Offset = prop.actual_offset;
if (!gamehelpers->FindSendPropInfo("CTFPlayer", "m_nPlayerCondEx3", &prop))
{
g_pSM->LogError(myself, "Failed to find m_nPlayerCondEx3 prop offset");
return false;
}
playerCondEx3Offset = prop.actual_offset;
if (playerCondOffset == -1 || playerCondExOffset == -1 || conditionBitsOffset == -1 || playerCondEx2Offset == -1 || playerCondEx3Offset == -1)
return false;
int maxClients = gpGlobals->maxClients; int maxClients = gpGlobals->maxClients;
for (int i = 1; i <= maxClients; i++) for (int i = 1; i <= maxClients; i++)
{ {
@ -184,21 +165,29 @@ bool InitialiseConditionChecks()
if (!pPlayer || !pPlayer->IsInGame()) if (!pPlayer || !pPlayer->IsInGame())
continue; continue;
GetPlayerConds(gamehelpers->ReferenceToEntity(i), &g_PlayerActiveConds[i]); CBaseEntity *pEntity = gamehelpers->ReferenceToEntity(i);
for (size_t j = 0; j < CondVar_Count; ++j)
{
m_OldConds[i][j] = *(int *)((intp) pEntity + GetPropOffs((CondVar)j));
}
} }
g_pSM->AddGameFrameHook(Conditions_OnGameFrame);
return true; return true;
} }
void Conditions_OnClientPutInServer(int client) void PlayerConditionsMgr::Shutdown()
{ {
g_PlayerActiveConds[client].ClearAll(); for (size_t i = 0; i < CondVar_Count; ++i)
{
GetProp((CondVar)i)->SetProxyFn(m_BackupProxyFns[i]);
}
playerhelpers->RemoveClientListener(this);
} }
void RemoveConditionChecks() void PlayerConditionsMgr::OnClientPutInServer(int client)
{ {
g_pSM->RemoveGameFrameHook(Conditions_OnGameFrame); memset(&m_OldConds[client], 0, sizeof(m_OldConds[0]));
} }
PlayerConditionsMgr g_CondMgr;

View File

@ -2,7 +2,7 @@
* vim: set ts=4 : * vim: set ts=4 :
* ============================================================================= * =============================================================================
* SourceMod Team Fortress 2 Extension * SourceMod Team Fortress 2 Extension
* Copyright (C) 2004-2011 AlliedModders LLC. All rights reserved. * Copyright (C) 2004-2015 AlliedModders LLC. All rights reserved.
* ============================================================================= * =============================================================================
* *
* This program is free software; you can redistribute it and/or modify it under * This program is free software; you can redistribute it and/or modify it under
@ -33,15 +33,50 @@
#define _INCLUDE_SOURCEMOD_CONDITIONS_H_ #define _INCLUDE_SOURCEMOD_CONDITIONS_H_
#include "extension.h" #include "extension.h"
#include <jit/jit_helpers.h>
#include <jit/x86/x86_macros.h>
#include "CDetour/detours.h"
bool InitialiseConditionChecks(); struct CondChangeData_t;
void RemoveConditionChecks();
void DoRemoveCond(int client, int condition);
void Conditions_OnClientPutInServer(int client); class PlayerConditionsMgr : public IClientListener
{
public:
PlayerConditionsMgr();
bool Init();
void Shutdown();
public: // IClientListener
void OnClientPutInServer(int client);
public:
enum CondVar : size_t
{
m_nPlayerCond,
_condition_bits,
m_nPlayerCondEx,
m_nPlayerCondEx2,
m_nPlayerCondEx3,
CondVar_Count
};
void OnConVarChange(CondVar var, const SendProp *pProp, const void *pStructBase, const void *pData, DVariant *pOut, int iElement, int objectID);
void ProcessCondChange(CondChangeData_t *pCondData);
private:
inline unsigned int GetPropOffs(CondVar var)
{
return m_CondVarProps[var].actual_offset;
}
inline SendProp *GetProp(CondVar var)
{
return m_CondVarProps[var].prop;
}
template<CondVar var>
bool SetupProp(const char *varname);
private:
int m_OldConds[SM_MAXPLAYERS + 1][CondVar_Count];
sm_sendprop_info_t m_CondVarProps[CondVar_Count];
int m_CondOffset[CondVar_Count];
SendVarProxyFn m_BackupProxyFns[CondVar_Count];
};
extern PlayerConditionsMgr g_CondMgr;
extern IForward *g_addCondForward; extern IForward *g_addCondForward;
extern IForward *g_removeCondForward; extern IForward *g_removeCondForward;

View File

@ -2,7 +2,7 @@
* vim: set ts=4 : * vim: set ts=4 :
* ============================================================================= * =============================================================================
* SourceMod Team Fortress 2 Extension * SourceMod Team Fortress 2 Extension
* Copyright (C) 2004-2011 AlliedModders LLC. All rights reserved. * Copyright (C) 2004-2015 AlliedModders LLC. All rights reserved.
* ============================================================================= * =============================================================================
* *
* This program is free software; you can redistribute it and/or modify it under * This program is free software; you can redistribute it and/or modify it under
@ -116,7 +116,6 @@ bool TF2Tools::SDK_OnLoad(char *error, size_t maxlength, bool late)
plsys->AddPluginsListener(this); plsys->AddPluginsListener(this);
playerhelpers->RegisterCommandTargetProcessor(this); playerhelpers->RegisterCommandTargetProcessor(this);
playerhelpers->AddClientListener(this);
g_critForward = forwards->CreateForward("TF2_CalcIsAttackCritical", ET_Hook, 4, NULL, Param_Cell, Param_Cell, Param_String, Param_CellByRef); g_critForward = forwards->CreateForward("TF2_CalcIsAttackCritical", ET_Hook, 4, NULL, Param_Cell, Param_Cell, Param_String, Param_CellByRef);
g_addCondForward = forwards->CreateForward("TF2_OnConditionAdded", ET_Ignore, 2, NULL, Param_Cell, Param_Cell); g_addCondForward = forwards->CreateForward("TF2_OnConditionAdded", ET_Ignore, 2, NULL, Param_Cell, Param_Cell);
@ -172,7 +171,6 @@ void TF2Tools::SDK_OnUnload()
g_RegNatives.UnregisterAll(); g_RegNatives.UnregisterAll();
gameconfs->CloseGameConfigFile(g_pGameConf); gameconfs->CloseGameConfigFile(g_pGameConf);
playerhelpers->UnregisterCommandTargetProcessor(this); playerhelpers->UnregisterCommandTargetProcessor(this);
playerhelpers->RemoveClientListener(this);
plsys->RemovePluginsListener(this); plsys->RemovePluginsListener(this);
@ -357,7 +355,7 @@ void TF2Tools::OnPluginLoaded(IPlugin *plugin)
&& ( g_addCondForward->GetFunctionCount() || g_removeCondForward->GetFunctionCount() ) && ( g_addCondForward->GetFunctionCount() || g_removeCondForward->GetFunctionCount() )
) )
{ {
m_CondChecksEnabled = InitialiseConditionChecks(); m_CondChecksEnabled = g_CondMgr.Init();
} }
if (!m_RulesDetoursEnabled if (!m_RulesDetoursEnabled
@ -384,7 +382,7 @@ void TF2Tools::OnPluginUnloaded(IPlugin *plugin)
{ {
if (!g_addCondForward->GetFunctionCount() && !g_removeCondForward->GetFunctionCount()) if (!g_addCondForward->GetFunctionCount() && !g_removeCondForward->GetFunctionCount())
{ {
RemoveConditionChecks(); g_CondMgr.Shutdown();
m_CondChecksEnabled = false; m_CondChecksEnabled = false;
} }
} }
@ -403,11 +401,6 @@ void TF2Tools::OnPluginUnloaded(IPlugin *plugin)
} }
} }
void TF2Tools::OnClientPutInServer(int client)
{
Conditions_OnClientPutInServer(client);
}
int FindResourceEntity() int FindResourceEntity()
{ {
return FindEntityByNetClass(-1, "CTFPlayerResource"); return FindEntityByNetClass(-1, "CTFPlayerResource");

View File

@ -2,7 +2,7 @@
* vim: set ts=4 : * vim: set ts=4 :
* ============================================================================= * =============================================================================
* SourceMod Team Fortress 2 Extension * SourceMod Team Fortress 2 Extension
* Copyright (C) 2004-2011 AlliedModders LLC. All rights reserved. * Copyright (C) 2004-2015 AlliedModders LLC. All rights reserved.
* ============================================================================= * =============================================================================
* *
* This program is free software; you can redistribute it and/or modify it under * This program is free software; you can redistribute it and/or modify it under
@ -55,8 +55,7 @@ class TF2Tools :
public ICommandTargetProcessor, public ICommandTargetProcessor,
public IConCommandBaseAccessor, public IConCommandBaseAccessor,
public IGameEventListener2, public IGameEventListener2,
public IPluginsListener, public IPluginsListener
public IClientListener
{ {
public: //SDKExtension public: //SDKExtension
/** /**
@ -104,8 +103,6 @@ public: //IGameEventManager
public: //IPluginsListener public: //IPluginsListener
void OnPluginLoaded(IPlugin *plugin); void OnPluginLoaded(IPlugin *plugin);
void OnPluginUnloaded(IPlugin *plugin); void OnPluginUnloaded(IPlugin *plugin);
public: //IClientListener
void OnClientPutInServer(int client);
public: public:
#if defined SMEXT_CONF_METAMOD #if defined SMEXT_CONF_METAMOD
/** /**