Skip to content

Commit

Permalink
simplify DETOUR_MEMBER_CALL / DETOUR_STATIC_CALL / VHOOK_CALL
Browse files Browse the repository at this point in the history
  • Loading branch information
rafradek committed Jun 8, 2024
1 parent 6bc79dd commit 1e4f3f1
Show file tree
Hide file tree
Showing 250 changed files with 2,044 additions and 2,008 deletions.
2 changes: 1 addition & 1 deletion .vscode/c_cpp_properties.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
],
"compilerPath": "/usr/bin/gcc",
"cStandard": "c17",
"cppStandard": "c++20",
"cppStandard": "gnu++20",
"intelliSenseMode": "linux-gcc-x86",
"compilerArgs": [
"-m64"
Expand Down
2 changes: 1 addition & 1 deletion AMBuildScript
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,7 @@ class ExtensionConfig(object):
############################################################################
# C++ COMPILER #############################################################
############################################################################
cxx.cxxflags += ['-std=c++2a']
cxx.cxxflags += ['-std=gnu++2a']
# cxx.cxxflags += ['-fno-exceptions']
cxx.cxxflags += ['-fexceptions'] # !!!
# cxx.cxxflags += ['-fno-threadsafe-statics']
Expand Down
28 changes: 15 additions & 13 deletions src/mem/detour.h
Original file line number Diff line number Diff line change
Expand Up @@ -299,15 +299,17 @@ class CDetouredFunc

static inline std::map<void *, CDetouredFunc> s_FuncMap;
};


#define DETOUR_MEMBER_CALL(name) (this->*Actual)
#define DETOUR_STATIC_CALL(name) (Actual_##name)
#define DETOUR_MEMBER_CALL(...) (Actual)(this, ## __VA_ARGS__)
#define DETOUR_STATIC_CALL(...) (Actual)(__VA_ARGS__)

#define __DETOUR_DECL_STATIC(prefix, name, ...) \
CDetour *detour_##name = nullptr; \
prefix (*Actual_##name)(__VA_ARGS__) = nullptr; \
prefix Detour_##name(__VA_ARGS__)
namespace detour_ns_##name {\
prefix Detour(__VA_ARGS__); \
prefix (*Actual)(__VA_ARGS__) = nullptr; \
}\
CDetour *detour_##name= nullptr; \
prefix detour_ns_##name::Detour(__VA_ARGS__)

#define DETOUR_DECL_STATIC(ret, name, ...) \
__DETOUR_DECL_STATIC(static ret, name, ##__VA_ARGS__)
#define DETOUR_DECL_STATIC_CALL_CONVENTION(cc, ret, name, ...) \
Expand All @@ -318,10 +320,10 @@ class CDetouredFunc
{ \
public: \
ret callback(__VA_ARGS__); \
static ret (Detour_##name::* Actual)(__VA_ARGS__); \
static ret (*Actual)(Detour_##name *, ## __VA_ARGS__); \
}; \
static CDetour *detour_##name = nullptr; \
ret (Detour_##name::* Detour_##name::Actual)(__VA_ARGS__) = nullptr; \
ret (* Detour_##name::Actual)(Detour_##name *, ## __VA_ARGS__) = nullptr; \
ret Detour_##name::callback(__VA_ARGS__)


Expand All @@ -330,17 +332,17 @@ class CDetouredFunc
{ \
public: \
cc ret callback(__VA_ARGS__); \
cc static ret (Detour_##name::* Actual)(__VA_ARGS__); \
cc static ret (*Actual)(Detour_##name *, ## __VA_ARGS__); \
}; \
static CDetour *detour_##name = nullptr; \
cc ret (Detour_##name::* Detour_##name::Actual)(__VA_ARGS__) = nullptr; \
cc ret (* Detour_##name::Actual)(Detour_##name *, ## __VA_ARGS__) = nullptr; \
cc ret Detour_##name::callback(__VA_ARGS__)

#define GET_MEMBER_CALLBACK(name) GetAddrOfMemberFunc(&Detour_##name::callback)
#define GET_MEMBER_INNERPTR(name) reinterpret_cast<void **>(&Detour_##name::Actual)

#define GET_STATIC_CALLBACK(name) reinterpret_cast<void *>(&Detour_##name)
#define GET_STATIC_INNERPTR(name) reinterpret_cast<void **>(&Actual_##name)
#define GET_STATIC_CALLBACK(name) reinterpret_cast<void *>(&detour_ns_##name::Detour)
#define GET_STATIC_INNERPTR(name) reinterpret_cast<void **>(&detour_ns_##name::Actual)


// TODO: have an "exclusive" version of CDetour, which is identical, but when it's enabled/disabled,
Expand Down
6 changes: 3 additions & 3 deletions src/mem/virtual_hook.h
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ class CVirtualHookFunc
std::string m_szName;
};

#define VHOOK_CALL(name) (this->*Actual)
#define VHOOK_CALL(...) (Actual)(this, ## __VA_ARGS__)

// For per object virtual hooks
#define VHOOK_CALL_OBJSPEC(name, ...) (this->*MakePtrToMemberFunc<Vhook_##name, __VA_ARGS__>((*((void ***)this)-5)[vhook_##name->GetOffset()]))
Expand All @@ -189,10 +189,10 @@ class CVirtualHookFunc
{ \
public: \
ret callback(__VA_ARGS__); \
static ret (Vhook_##name::* Actual)(__VA_ARGS__); \
static ret (*Actual)(Vhook_##name *, ##__VA_ARGS__); \
}; \
static CVirtualHook *vhook_##name = nullptr; \
ret (Vhook_##name::* Vhook_##name::Actual)(__VA_ARGS__) = nullptr; \
ret (* Vhook_##name::Actual)(Vhook_##name *, ##__VA_ARGS__) = nullptr; \
ret Vhook_##name::callback(__VA_ARGS__)

#define GET_VHOOK_CALLBACK(name) GetAddrOfMemberFunc(&Vhook_##name::callback)
Expand Down
18 changes: 9 additions & 9 deletions src/mod/ai/engiebot_dispensers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ namespace Mod::AI::EngieBot_Dispensers
TRACE();

hint_sg = hint;
DETOUR_MEMBER_CALL(CTFBotMvMEngineerBuildSentryGun_ctor)(hint);
DETOUR_MEMBER_CALL(hint);
}

CTFBotHintTeleporterExit *hint_te = nullptr;
Expand All @@ -252,7 +252,7 @@ namespace Mod::AI::EngieBot_Dispensers
TRACE();

hint_te = hint;
DETOUR_MEMBER_CALL(CTFBotMvMEngineerBuildTeleportExit_ctor)(hint);
DETOUR_MEMBER_CALL(hint);
}

std::unordered_map<Action<CTFBot> *, IHotplugAction<CTFBot> *> build_actions;
Expand All @@ -264,7 +264,7 @@ namespace Mod::AI::EngieBot_Dispensers
delete build_actions[action];
build_actions.erase(action);
}
DETOUR_MEMBER_CALL(CTFBotMvMEngineerIdle_dtor0)();
DETOUR_MEMBER_CALL();
}

DETOUR_DECL_MEMBER(void, CTFBotMvMEngineerIdle_dtor1)
Expand All @@ -274,7 +274,7 @@ namespace Mod::AI::EngieBot_Dispensers
delete build_actions[action];
build_actions.erase(action);
}
DETOUR_MEMBER_CALL(CTFBotMvMEngineerIdle_dtor1)();
DETOUR_MEMBER_CALL();
}

DETOUR_DECL_MEMBER(void, CTFBotMvMEngineerIdle_dtor2)
Expand All @@ -284,7 +284,7 @@ namespace Mod::AI::EngieBot_Dispensers
delete build_actions[action];
build_actions.erase(action);
}
DETOUR_MEMBER_CALL(CTFBotMvMEngineerIdle_dtor2)();
DETOUR_MEMBER_CALL();
}

RefCount rc_CTFBotMvMEngineerIdle_Update;
Expand Down Expand Up @@ -325,7 +325,7 @@ namespace Mod::AI::EngieBot_Dispensers
}
}

auto result = DETOUR_MEMBER_CALL(CTFBotMvMEngineerIdle_Update)(actor, dt);
auto result = DETOUR_MEMBER_CALL(actor, dt);

if (result.transition == ActionTransition::SUSPEND_FOR && result.action != nullptr) {
if (strcmp(result.action->GetName(), "MvMEngineerBuildSentryGun") == 0 && hint_sg != nullptr) {
Expand Down Expand Up @@ -374,7 +374,7 @@ namespace Mod::AI::EngieBot_Dispensers
return obj->EyeAngles();
}

return VHOOK_CALL(CObjectDispenser_GetAvailableMetal)();
return VHOOK_CALL();
}


Expand All @@ -384,13 +384,13 @@ namespace Mod::AI::EngieBot_Dispensers
return false;
}

return DETOUR_MEMBER_CALL(CTFPlayer_SpeakConceptIfAllowed)(iConcept, modifiers, pszOutResponseChosen, bufsize, filter);
return DETOUR_MEMBER_CALL(iConcept, modifiers, pszOutResponseChosen, bufsize, filter);
}


DETOUR_DECL_MEMBER(void, CTFBotHintEngineerNest_HintTeleporterThink)
{
DETOUR_MEMBER_CALL(CTFBotHintEngineerNest_HintTeleporterThink)();
DETOUR_MEMBER_CALL();

auto nest = reinterpret_cast<CTFBotHintEngineerNest *>(this);
nest->m_bHasActiveTeleporter = true;
Expand Down
10 changes: 5 additions & 5 deletions src/mod/ai/engiebot_nopush.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,23 +12,23 @@ namespace Mod::AI::EngieBot_NoPush
{
SCOPED_INCREMENT(rc_TeleSpawn_Update);
pusher_engineer = actor;
return DETOUR_MEMBER_CALL(CTFBotMvMEngineerTeleportSpawn_Update)(actor, dt);
return DETOUR_MEMBER_CALL(actor, dt);
}

RefCount rc_BuildSentry_Update;
DETOUR_DECL_MEMBER(ActionResult<CTFBot>,CTFBotMvMEngineerBuildSentryGun_Update, CTFBot *actor, float dt)
{
SCOPED_INCREMENT(rc_BuildSentry_Update);
pusher_engineer = actor;
return DETOUR_MEMBER_CALL(CTFBotMvMEngineerBuildSentryGun_Update)(actor, dt);
return DETOUR_MEMBER_CALL(actor, dt);
}

RefCount rc_BuildTele_Update;
DETOUR_DECL_MEMBER(ActionResult<CTFBot>, CTFBotMvMEngineerBuildTeleportExit_Update, CTFBot *actor, float dt)
{
SCOPED_INCREMENT(rc_BuildTele_Update);
pusher_engineer = actor;
return DETOUR_MEMBER_CALL(CTFBotMvMEngineerBuildTeleportExit_Update)(actor, dt);
return DETOUR_MEMBER_CALL(actor, dt);
}

ConVar cvar_reducerange("sig_ai_engiebot_pushrange", "0", FCVAR_NOTIFY | FCVAR_GAMEDLL,
Expand All @@ -41,7 +41,7 @@ namespace Mod::AI::EngieBot_NoPush
return;
}

DETOUR_MEMBER_CALL(CTFPlayer_ApplyAbsVelocityImpulse)(v1);
DETOUR_MEMBER_CALL(v1);
}

DETOUR_DECL_MEMBER(void, CTFGameRules_PushAllPlayersAway, const Vector& vFromThisPoint, float flRange, float flForce, int nTeam, CUtlVector< CTFPlayer* > *pPushedPlayers)
Expand All @@ -55,7 +55,7 @@ namespace Mod::AI::EngieBot_NoPush

}

DETOUR_MEMBER_CALL(CTFGameRules_PushAllPlayersAway)(vFromThisPoint, flRange, flForce, nTeam, pPushedPlayers);
DETOUR_MEMBER_CALL(vFromThisPoint, flRange, flForce, nTeam, pPushedPlayers);
}

class CMod : public IMod
Expand Down
8 changes: 4 additions & 4 deletions src/mod/ai/engiebot_wrangler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -170,15 +170,15 @@ namespace Mod::AI::EngieBot_Wrangler
DETOUR_DECL_MEMBER(void, CTFLaserPointer_CreateLaserDot)
{
laser_dot = nullptr;
DETOUR_MEMBER_CALL(CTFLaserPointer_CreateLaserDot)();
DETOUR_MEMBER_CALL();
if (laser_dot != nullptr && laser_dot->GetTeamNumber() == TF_TEAM_BLUE && TFGameRules()->IsMannVsMachineMode()) {
laser_dot->SetTeamNumber(TF_TEAM_RED);
}
}

DETOUR_DECL_STATIC(CBaseEntity *, CLaserDot_Create, Vector &origin, CBaseEntity *owner, bool visibleDot)
{
return laser_dot = DETOUR_STATIC_CALL(CLaserDot_Create)(origin, owner, visibleDot);
return laser_dot = DETOUR_STATIC_CALL(origin, owner, visibleDot);
}

DETOUR_DECL_MEMBER(EventDesiredResult<CTFBot>, Action_CTFBot_OnCommandString, CTFBot *actor, const char *cmd)
Expand All @@ -194,7 +194,7 @@ namespace Mod::AI::EngieBot_Wrangler
}
}

return DETOUR_MEMBER_CALL(Action_CTFBot_OnCommandString)(actor, cmd);
return DETOUR_MEMBER_CALL(actor, cmd);
}
DETOUR_DECL_MEMBER(ActionResult< CTFBot >, CTFBotMvMEngineerIdle_Update, CTFBot *actor, float interval)
{
Expand All @@ -203,7 +203,7 @@ namespace Mod::AI::EngieBot_Wrangler
if (CTFBotMvMEngineerDisableAutopilot::IsPossible(actor) && actor->GetVisionInterface()->GetPrimaryKnownThreat(false) != nullptr) {
return ActionResult< CTFBot >::SuspendFor(new CTFBotMvMEngineerDisableAutopilot(), "This thing ain't on autopilot, son!");
}
return DETOUR_MEMBER_CALL(CTFBotMvMEngineerIdle_Update)(actor, interval);
return DETOUR_MEMBER_CALL(actor, interval);
}

class CMod : public IMod
Expand Down
2 changes: 1 addition & 1 deletion src/mod/ai/focus_one_player.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ namespace Mod::AI::Focus_One_Player
CTFPlayer *player = ToTFPlayer(ent);
if (player != nullptr && IsTheTarget(player)) {
return false;
// return DETOUR_MEMBER_CALL(CTFBotVision_IsIgnored)(ent);
// return DETOUR_MEMBER_CALL(ent);
}

return true;
Expand Down
14 changes: 7 additions & 7 deletions src/mod/ai/improved_targeting.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ namespace Mod::AI::Improved_Targeting
else if (threat2nonsentrybuilding && !threat1nonsentrybuilding) {
return threat1;
}
auto ret = DETOUR_MEMBER_CALL(CTFBotMainAction_SelectMoreDangerousThreat)(nextbot, them, threat1, threat2);
auto ret = DETOUR_MEMBER_CALL(nextbot, them, threat1, threat2);

thread_bot_caller = nullptr;

Expand All @@ -103,7 +103,7 @@ namespace Mod::AI::Improved_Targeting
}
}

return DETOUR_MEMBER_CALL(CTFBotMainAction_GetHealerOfThreat)(threat);
return DETOUR_MEMBER_CALL(threat);
}

bool callingfromhere = false;
Expand Down Expand Up @@ -173,23 +173,23 @@ namespace Mod::AI::Improved_Targeting
}
Vector move = trace->endpos - actor->GetAbsOrigin();
}
return DETOUR_MEMBER_CALL(CTFBotMainAction_OnContact)(actor, ent, trace);
return DETOUR_MEMBER_CALL(actor, ent, trace);
}

RefCount rc_CTFBot_Touch;
DETOUR_DECL_MEMBER(void, CTFBot_Touch, CBaseEntity *ent)
{
auto bot = reinterpret_cast<CTFBot *>(this);
SCOPED_INCREMENT_IF(rc_CTFBot_Touch, cvar_anti_spy_touch.GetBool());
DETOUR_MEMBER_CALL(CTFBot_Touch)(ent);
DETOUR_MEMBER_CALL(ent);
}

DETOUR_DECL_MEMBER(void, CTFBot_SuspectSpy, CTFPlayer *spy)
{
if (rc_CTFBot_Touch && !callingfromhere)
return;

DETOUR_MEMBER_CALL(CTFBot_SuspectSpy)(spy);
DETOUR_MEMBER_CALL(spy);
}

DETOUR_DECL_MEMBER(bool, CTFBot_IsExplosiveProjectileWeapon, CTFWeaponBase *weapon)
Expand All @@ -202,12 +202,12 @@ namespace Mod::AI::Improved_Targeting
}
}

return DETOUR_MEMBER_CALL(CTFBot_IsExplosiveProjectileWeapon)(weapon);
return DETOUR_MEMBER_CALL(weapon);
}

DETOUR_DECL_MEMBER(ActionResult<CTFBot>, CTFBotMedicHeal_Update, CTFBot *me, float interval)
{
auto result = DETOUR_MEMBER_CALL(CTFBotMedicHeal_Update)(me, interval);
auto result = DETOUR_MEMBER_CALL(me, interval);
auto medigun = rtti_cast<CWeaponMedigun *>(me->GetActiveTFWeapon());
auto medigunHasTarget = medigun != nullptr && medigun->GetHealTarget() == reinterpret_cast<CTFBotMedicHeal *>(this)->GetPatient();
if (medigun && medigunHasTarget && me->ExtAttr()[CTFBot::ExtendedAttr::MEDIC_LOOK_AT_THREATS]) {
Expand Down
6 changes: 3 additions & 3 deletions src/mod/ai/improved_useitem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ namespace Mod::AI::Improved_UseItem

auto bot = reinterpret_cast<CTFBot *>(this);

CTFBotUseItem *result = reinterpret_cast<CTFBotUseItem *>(DETOUR_MEMBER_CALL(CTFBot_OpportunisticallyUseWeaponAbilities)());
CTFBotUseItem *result = reinterpret_cast<CTFBotUseItem *>(DETOUR_MEMBER_CALL());
if (result != nullptr) {
CTFWeaponBase *item = result->m_hItem;

Expand Down Expand Up @@ -318,7 +318,7 @@ namespace Mod::AI::Improved_UseItem
if (item_noisemaker != nullptr) {
return item_noisemaker;
}
return DETOUR_MEMBER_CALL(CPlayerInventory_GetInventoryItemByItemID)(param1, itemid);
return DETOUR_MEMBER_CALL(param1, itemid);
}
std::vector<bool> stack_m_bPlayingMannVsMachine;
void Quirk_MvM_Pre()
Expand All @@ -344,7 +344,7 @@ namespace Mod::AI::Improved_UseItem
bool mannvsmachine = TFGameRules()->IsMannVsMachineMode();
TFGameRules()->Set_m_bPlayingMannVsMachine(false);

auto result = DETOUR_MEMBER_CALL(CTFBot_EquipLongRangeWeapon)();
auto result = DETOUR_MEMBER_CALL();

TFGameRules()->Set_m_bPlayingMannVsMachine(mannvsmachine);
//Quirk_MvM_Post();
Expand Down
4 changes: 2 additions & 2 deletions src/mod/ai/medigun_shield_pathfinding.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ namespace Mod::Perf::Medigun_Shield_Pathfinding_ShouldHitEntity
{
DETOUR_DECL_MEMBER(bool, NextBotTraversableTraceFilter_ShouldHitEntity, IHandleEntity *pEntity, int contentsMask)
{
auto result = DETOUR_MEMBER_CALL(NextBotTraversableTraceFilter_ShouldHitEntity)(pEntity, contentsMask);
auto result = DETOUR_MEMBER_CALL(pEntity, contentsMask);

if (result) {
CBaseEntity *ent = EntityFromEntityHandle(pEntity);
Expand Down Expand Up @@ -47,7 +47,7 @@ namespace Mod::Perf::Medigun_Shield_Pathfinding_IsEntityTraversable
{
DETOUR_DECL_MEMBER(bool, CTFBotLocomotion_IsEntityTraversable, CBaseEntity *ent, ILocomotion::TraverseWhenType ttype)
{
auto result = DETOUR_MEMBER_CALL(CTFBotLocomotion_IsEntityTraversable)(ent, ttype);
auto result = DETOUR_MEMBER_CALL(ent, ttype);
if (!result && !ent->IsPlayer()) {
return ent->IsCombatItem();
}
Expand Down
Loading

0 comments on commit 1e4f3f1

Please sign in to comment.