Skip to content

Commit

Permalink
Add automatic offset finding
Browse files Browse the repository at this point in the history
  • Loading branch information
pongo1231 committed Sep 5, 2019
1 parent 7b2d7af commit 0ba2218
Show file tree
Hide file tree
Showing 5 changed files with 127 additions and 54 deletions.
16 changes: 8 additions & 8 deletions Pongbot/TF2/DebugCommands.cpp → Pongbot/DebugCommands.cpp
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
#include <stdafx.h>
#include "../IConVarBase.h"
#include "../Util.h"
#include "Entity/EntityProvider.h"
#include "Entity/CTFFlag.h"
#include "Entity/Player.h"
#include "Entity/EntityDataProvider.h"
#include "IConVarBase.h"
#include "Util.h"
#include "TF2/Entity/EntityProvider.h"
#include "TF2/Entity/CTFFlag.h"
#include "TF2/Entity/Player.h"
#include "TF2/Entity/EntityDataProvider.h"
#include <metamod/ISmmAPI.h>
#include <hlsdk/public/edict.h>
#include <hlsdk/public/server_class.h>
#include <fstream>

extern IVEngineServer *Engine;
extern IServerGameDLL *Server;
extern IVEngineServer* Engine;
extern IServerGameDLL* Server;

CON_COMMAND(pongbot_debug_dumpoffsets, "Dumps all offsets to addons/pongbot/offsets_dump.txt")
{
Expand Down
38 changes: 19 additions & 19 deletions Pongbot/TF2/Class/TFClassInfoProvider.h
Original file line number Diff line number Diff line change
Expand Up @@ -104,52 +104,52 @@ class TFClassInfoProvider
private:
const std::map<TFClass, TFClassInfo> _ClassInfos =
{
{CLASS_UNK, TFClassInfo()},
{CLASS_UNK, {}},

{SCOUT, TFClassInfo(133.f,
{SCOUT, {133.f,
TFClassInfoWeapon("TF_WEAPON_SCATTERGUN", WEAPONFLAG_PRIORITIZE_MIDDLEDIST | WEAPONFLAG_PRIORITIZE_SHORTDIST),
TFClassInfoWeapon("TF_WEAPON_PISTOL_SCOUT", WEAPONFLAG_PRIORITIZE_LONGDIST),
TFClassInfoWeapon("TF_WEAPON_BAT"))},
TFClassInfoWeapon("TF_WEAPON_BAT")}},

{SOLDIER, TFClassInfo(80.f,
{SOLDIER, {80.f,
TFClassInfoWeapon("TF_WEAPON_ROCKETLAUNCHER", WEAPONFLAG_PRIORITIZE_LONGDIST | WEAPONFLAG_PRIORITIZE_MIDDLEDIST),
TFClassInfoWeapon("TF_WEAPON_SHOTGUN_SOLDIER", WEAPONFLAG_PRIORITIZE_SHORTDIST),
TFClassInfoWeapon("TF_WEAPON_SHOVEL"))},
TFClassInfoWeapon("TF_WEAPON_SHOVEL")}},

{PYRO, TFClassInfo(100.f,
{PYRO, {100.f,
TFClassInfoWeapon("TF_WEAPON_FLAMETHROWER", WEAPONFLAG_PRIORITIZE_MIDDLEDIST | WEAPONFLAG_PRIORITIZE_SHORTDIST),
TFClassInfoWeapon("TF_WEAPON_SHOTGUN_PYRO", WEAPONFLAG_PRIORITIZE_LONGDIST),
TFClassInfoWeapon("TF_WEAPON_FIREAXE"))},
TFClassInfoWeapon("TF_WEAPON_FIREAXE")}},

{DEMO, TFClassInfo(93.f,
{DEMO, {93.f,
TFClassInfoWeapon("TF_WEAPON_GRENADELAUNCHER", WEAPONFLAG_PRIORITIZE_LONGDIST | WEAPONFLAG_PRIORITIZE_MIDDLEDIST),
TFClassInfoWeapon("TF_WEAPON_PIPEBOMBLAUNCHER", WEAPONFLAG_NOATTACK),
TFClassInfoWeapon("TF_WEAPON_BOTTLE", WEAPONFLAG_PRIORITIZE_SHORTDIST))},
TFClassInfoWeapon("TF_WEAPON_BOTTLE", WEAPONFLAG_PRIORITIZE_SHORTDIST)}},

{HEAVY, TFClassInfo(77.f,
{HEAVY, {77.f,
TFClassInfoWeapon("TF_WEAPON_MINIGUN", WEAPONFLAG_PRIORITIZE_LONGDIST | WEAPONFLAG_PRIORITIZE_MIDDLEDIST | WEAPONFLAG_PRIORITIZE_SHORTDIST),
TFClassInfoWeapon("TF_WEAPON_SHOTGUN_HWG"),
TFClassInfoWeapon("TF_WEAPON_FISTS"))},
TFClassInfoWeapon("TF_WEAPON_FISTS")}},

{ENGI, TFClassInfo(100.f,
{ENGI, {100.f,
TFClassInfoWeapon("TF_WEAPON_SHOTGUN_PRIMARY", WEAPONFLAG_PRIORITIZE_MIDDLEDIST | WEAPONFLAG_PRIORITIZE_SHORTDIST),
TFClassInfoWeapon("TF_WEAPON_PISTOL", WEAPONFLAG_PRIORITIZE_LONGDIST),
TFClassInfoWeapon("TF_WEAPON_WRENCH"))},
TFClassInfoWeapon("TF_WEAPON_WRENCH")}},

{MED, TFClassInfo(109.f,
{MED, {109.f,
TFClassInfoWeapon("TF_WEAPON_SYRINGEGUN_MEDIC", WEAPONFLAG_PRIORITIZE_LONGDIST | WEAPONFLAG_PRIORITIZE_MIDDLEDIST),
TFClassInfoWeapon("TF_WEAPON_MEDIGUN", WEAPONFLAG_NOATTACK),
TFClassInfoWeapon("TF_WEAPON_BONESAW", WEAPONFLAG_PRIORITIZE_SHORTDIST))},
TFClassInfoWeapon("TF_WEAPON_BONESAW", WEAPONFLAG_PRIORITIZE_SHORTDIST)}},

{SNIPER, TFClassInfo(100.f,
{SNIPER, {100.f,
TFClassInfoWeapon("TF_WEAPON_SNIPERRIFLE", WEAPONFLAG_PRIORITIZE_LONGDIST),
TFClassInfoWeapon("TF_WEAPON_SMG", WEAPONFLAG_PRIORITIZE_MIDDLEDIST),
TFClassInfoWeapon("TF_WEAPON_CLUB", WEAPONFLAG_PRIORITIZE_SHORTDIST))},
TFClassInfoWeapon("TF_WEAPON_CLUB", WEAPONFLAG_PRIORITIZE_SHORTDIST)}},

{SPY, TFClassInfo(109.f,
{SPY, {109.f,
TFClassInfoWeapon("TF_WEAPON_REVOLVER", WEAPONFLAG_PRIORITIZE_LONGDIST | WEAPONFLAG_PRIORITIZE_MIDDLEDIST),
TFClassInfoWeapon("TF_WEAPON_SAPPER", WEAPONFLAG_NOATTACK), /* Sapper is using build 3 0 for some stupid reason*/
TFClassInfoWeapon("TF_WEAPON_KNIFE", WEAPONFLAG_PRIORITIZE_SHORTDIST))}
TFClassInfoWeapon("TF_WEAPON_KNIFE", WEAPONFLAG_PRIORITIZE_SHORTDIST)}}
};
};

Expand Down
51 changes: 51 additions & 0 deletions Pongbot/TF2/Entity/EntityDataProvider.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
#include "EntityDataProvider.h"
#include "../../Util.h"

extern IServerGameDLL* Server;

EntityDataProvider* _EntityDataProvider = nullptr;

void EntityDataProvider::Init()
Expand All @@ -11,6 +13,7 @@ void EntityDataProvider::Init()
Util::DebugLog("INIT EntityDataProvider");

_EntityDataProvider = new EntityDataProvider();
_EntityDataProvider->UpdateOffsets();
}
}

Expand All @@ -23,4 +26,52 @@ void EntityDataProvider::Destroy()
delete _EntityDataProvider;
_EntityDataProvider = nullptr;
}
}

void EntityDataProvider::UpdateOffsets()
{
ServerClass* serverClass = Server->GetAllServerClasses();
while (serverClass)
{
const char* serverClassName = serverClass->GetName();
SendTable* sendTable = serverClass->m_pTable;
for (int i = 0; i < sendTable->GetNumProps(); i++)
{
SendProp* sendProp = sendTable->GetProp(i);
const char* sendPropName = sendProp->GetName();
for (int j = 0; j < _DATA_AMOUNT; j++)
{
EntityData& entityData = _EntityOffsets[j];
const char* entityClass = entityData.GetEntityClass();
const char* entityAttr = entityData.GetEntityAttr();
if (entityData.Offset == -1 && strcmp(entityClass, serverClassName) == 0
&& strcmp(entityAttr, sendPropName) == 0)
{
int offset = sendProp->GetOffset();
entityData.Offset = offset;
Util::DebugLog("EntityDataProvider: Offset of %s::%s is %d", entityClass, entityAttr, offset);
break;
}
}
}

serverClass = serverClass->m_pNext;
}

bool offsetsNotFound = false;
for (int i = 0; i < _DATA_AMOUNT; i++)
{
EntityData entityData = _EntityOffsets[i];
if (entityData.Offset == -1)
{
Util::Log("!! Offset of %s::%s not found !!", entityData.GetEntityClass(),
entityData.GetEntityAttr());
offsetsNotFound = true;
}
}

if (offsetsNotFound)
{
Util::Log("ERROR: One or more offsets were not found, this WILL cause problems");
}
}
75 changes: 48 additions & 27 deletions Pongbot/TF2/Entity/EntityDataProvider.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ enum EntityDataType
DATA_PLAYER_FOV, // CBasePlayer::m_iFOV
DATA_PLAYER_CURRENTWEAPON, // CBaseCombatCharacter::m_hActiveWeapon
DATA_PLAYER_ANGLE, // CBaseEntity::m_angRotation
DATA_ROUND_TIMER_STATE // CTeamRoundTimer::m_nState
DATA_ROUND_TIMER_STATE, // CTeamRoundTimer::m_nState
_DATA_AMOUNT
};

class EntityDataProvider
Expand All @@ -27,50 +28,70 @@ class EntityDataProvider
static void Destroy();

template<typename T>
T GetDataFromEntity(Entity entity, EntityDataType dataType)
inline T GetDataFromEntity(Entity entity, EntityDataType dataType) const
{
if (!entity.Exists() || !entity.GetEdict()->GetUnknown())
{
return (T) 0;
}

return (T) *((char*) entity.GetEdict()->GetUnknown()->GetBaseEntity() + _EntityOffsets.at(dataType));
return (T) *((char*) entity.GetEdict()->GetUnknown()->GetBaseEntity() + _EntityOffsets[dataType].Offset);
}

template<typename T>
void SetDataOfEntity(Entity entity, EntityDataType dataType, T data)
inline void SetDataOfEntity(Entity entity, EntityDataType dataType, T data)
{
if (entity.Exists() && entity.GetEdict()->GetUnknown())
{
*((char*) entity.GetEdict()->GetUnknown()->GetBaseEntity() + _EntityOffsets.at(dataType)) = data;
*((char*) entity.GetEdict()->GetUnknown()->GetBaseEntity() + _EntityOffsets[dataType].Offset) = data;
}
}

private:
const std::map<EntityDataType, unsigned int> _EntityOffsets =
struct EntityData
{
#ifdef _WIN32
{DATA_TEAM, 516},
{DATA_FLAG_OWNER, 1648},
{DATA_FLAG_STATUS, 1632},
{DATA_FLAG_DISABLED, 1624},
{DATA_HEALTH, 244},
{DATA_PLAYER_FOV, 2876},
{DATA_PLAYER_CURRENTWEAPON, 2052},
{DATA_PLAYER_ANGLE, 804},
{DATA_ROUND_TIMER_STATE, 888}
#elif _LINUX
{DATA_TEAM, 536},
{DATA_FLAG_OWNER, 1668},
{DATA_FLAG_STATUS, 1652},
{DATA_FLAG_DISABLED, 1644},
{DATA_HEALTH, 264},
{DATA_PLAYER_FOV, 2896},
{DATA_PLAYER_CURRENTWEAPON, 2072},
{DATA_PLAYER_ANGLE, 824},
{DATA_ROUND_TIMER_STATE, 908}
#endif
public:
EntityData(EntityDataType dataType, const char* entityClass, const char* entityAttr)
: DataType(dataType), EntityClass(entityClass), EntityAttr(entityAttr) {}

public:
int Offset = -1;

inline EntityDataType GetDataType() const
{
return DataType;
}

inline const char* GetEntityClass() const
{
return EntityClass;
}

inline const char* GetEntityAttr() const
{
return EntityAttr;
}

private:
const EntityDataType DataType;
const char* EntityClass;
const char* EntityAttr;
};

std::vector<EntityData> _EntityOffsets =
{
{DATA_TEAM, "CBaseEntity", "m_iTeamNum"},
{DATA_FLAG_OWNER, "CCaptureFlag", "m_hPrevOwner"},
{DATA_FLAG_STATUS, "CCaptureFlag", "m_nFlagStatus"},
{DATA_FLAG_DISABLED, "CCaptureFlag", "m_bDisabled"},
{DATA_HEALTH, "CBaseObject", "m_iHealth"},
{DATA_PLAYER_FOV, "CBasePlayer", "m_iFOV"},
{DATA_PLAYER_CURRENTWEAPON, "CBaseCombatCharacter", "m_hActiveWeapon"},
{DATA_PLAYER_ANGLE, "CBaseEntity", "m_angRotation"},
{DATA_ROUND_TIMER_STATE, "CTeamRoundTimer", "m_nState"}
};

void UpdateOffsets();
};

extern EntityDataProvider* _EntityDataProvider;
1 change: 1 addition & 0 deletions Pongbot/stdafx.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include <hlsdk/public/mathlib/mathlib.h>
#include <hlsdk/public/edict.h>
#include <hlsdk/public/tier1/convar.h>
#include <hlsdk/public/server_class.h>
#include <cstdlib>
#include <algorithm>
#include <cmath>
Expand Down

0 comments on commit 0ba2218

Please sign in to comment.