Skip to content

Commit ffbb318

Browse files
authored
sirens: Improve Hook Compatibility (#227)
* sirens: Improve `hkRegisterCorona` Hook * sirens: Improve `hkAddPointLights` Hook * sirens: Improve `hkUsesSiren` Hook
1 parent 9be3342 commit ffbb318

File tree

2 files changed

+43
-39
lines changed

2 files changed

+43
-39
lines changed

src/features/sirens.cpp

Lines changed: 36 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ bool IsValidSirenVehicle(RwFrame *pFrame)
4242

4343
std::map<CVehicle *, bool> sirenExtraUsedFlag;
4444

45-
char __fastcall Sirens::hkUsesSiren(CVehicle *ptr)
45+
bool Sirens::hkUsesSiren(std::function<hkUsesSirenFunc> originalCall, CVehicle* ptr)
4646
{
4747
if (Util::IsEngineOff(ptr))
4848
{
@@ -55,20 +55,17 @@ char __fastcall Sirens::hkUsesSiren(CVehicle *ptr)
5555
sirenExtraUsedFlag[ptr] = IsValidSirenVehicle((RwFrame *)ptr->m_pRwClump->object.parent);
5656
}
5757

58-
if (Sirens::modelData.contains(ptr->m_nModelIndex) && sirenExtraUsedFlag[ptr])
58+
if (modelData.contains(ptr->m_nModelIndex) && sirenExtraUsedFlag[ptr])
5959
{
6060
ptr->m_vehicleAudio.m_bModelWithSiren = true;
6161
return true;
6262
}
63-
return ptr->IsLawEnforcementVehicle();
63+
64+
return originalCall(ptr);
6465
}
6566

67+
static ThiscallEvent<AddressList<0x6AAB71, H_CALL>, PRIORITY_BEFORE, ArgPickN<CVehicle*, 0>, void(CVehicle*)> Automobile__PreRenderEvent;
6668
static CVehicle *pCurrentVeh = nullptr;
67-
void __fastcall hkVehiclePreRender(CVehicle *ptr)
68-
{
69-
pCurrentVeh = ptr;
70-
CallMethod<0x6D6480>(ptr);
71-
}
7269

7370
void Sirens::Reload(CVehicle *pVeh)
7471
{
@@ -81,20 +78,22 @@ void Sirens::Reload(CVehicle *pVeh)
8178
}
8279

8380
void Sirens::hkAddPointLights(
84-
uint8_t type,
85-
float px, float py, float pz,
86-
float dx, float dy, float dz,
87-
float range,
88-
float red, float green, float blue,
89-
uint8_t fogEffect,
90-
bool bCastsShadowFromPlayerCarAndPed,
91-
CEntity* castingEntity
81+
std::function<hkAddPointLightsFunc> originalCall,
82+
uint8_t& type,
83+
CVector& position,
84+
CVector& direction,
85+
float& range,
86+
float& red, float& green, float& blue,
87+
uint8_t& fogEffect,
88+
bool& bCastsShadowFromPlayerCarAndPed,
89+
CEntity*& castingEntity
9290
)
9391
{
94-
if (pCurrentVeh == nullptr) return;
95-
if (Sirens::modelData.contains(pCurrentVeh->m_nModelIndex)) return;
92+
if (pCurrentVeh && modelData.contains(pCurrentVeh->m_nModelIndex)) {
93+
return;
94+
}
9695

97-
CPointLights::AddLight(type, {px, py, pz}, {dx, dy, dz}, range, red, green, blue, fogEffect, bCastsShadowFromPlayerCarAndPed, castingEntity);
96+
originalCall(type, position, direction, range, red, green, blue, fogEffect, bCastsShadowFromPlayerCarAndPed, castingEntity);
9897
}
9998

10099
VehicleSirenMaterial::VehicleSirenMaterial(std::string state, int material, nlohmann::json json)
@@ -926,36 +925,37 @@ void Sirens::Init()
926925
ModelInfoMgr::EnableSirenMaterial(vehicle, mat.first);
927926
} });
928927

929-
patch::ReplaceFunctionCall(0x6D8492, (void *)hkUsesSiren);
930-
patch::ReplaceFunctionCall(0x6AB80F, (void *)hkAddPointLights);
931-
patch::ReplaceFunctionCall(0x6AAB71, (void *)hkVehiclePreRender);
928+
using hkUsesSirenHook = injector::function_hooker_thiscall<injector::scoped_call, 0x6D8492, hkUsesSirenFunc>;
929+
injector::make_static_hook<hkUsesSirenHook>(hkUsesSiren);
930+
931+
Automobile__PreRenderEvent += [](CVehicle* pVeh) {
932+
pCurrentVeh = pVeh; // Captured for hkAddPointLights()
933+
};
934+
935+
using hkAddPointLightsHook = injector::function_hooker<injector::scoped_call, 0x6AB80F, hkAddPointLightsFunc>;
936+
injector::make_static_hook<hkAddPointLightsHook>(hkAddPointLights);
932937

933938
Events::initGameEvent += []
934939
{
935-
injector::MakeCALL((void *)0x6ABA60, hkRegisterCorona, true);
936-
injector::MakeCALL((void *)0x6ABB35, hkRegisterCorona, true);
937-
injector::MakeCALL((void *)0x6ABC69, hkRegisterCorona, true);
938-
injector::MakeCALL((void *)0x6BD4DD, hkRegisterCorona, true);
939-
injector::MakeCALL((void *)0x6BD531, hkRegisterCorona, true);
940+
using hkRegisterCoronaHook = injector::function_hooker<injector::scoped_call, 0x0, hkRegisterCoronaFunc>;
941+
injector::make_static_hook_dyn<hkRegisterCoronaHook>(hkRegisterCorona, 0x6ABA60);
942+
injector::make_static_hook_dyn<hkRegisterCoronaHook>(hkRegisterCorona, 0x6ABB35);
943+
injector::make_static_hook_dyn<hkRegisterCoronaHook>(hkRegisterCorona, 0x6ABC69);
944+
injector::make_static_hook_dyn<hkRegisterCoronaHook>(hkRegisterCorona, 0x6BD4DD);
945+
injector::make_static_hook_dyn<hkRegisterCoronaHook>(hkRegisterCorona, 0x6BD531);
940946
};
941947
};
942948

943-
void Sirens::hkRegisterCorona(unsigned int id, CEntity *attachTo, unsigned char red, unsigned char green, unsigned char blue, unsigned char alpha, CVector const &posn, float radius, float farClip, eCoronaType coronaType, eCoronaFlareType flaretype, bool enableReflection, bool checkObstacles, int _param_not_used, float angle, bool longDistance, float nearClip, unsigned char fadeState, float fadeSpeed, bool onlyFromBelow, bool reflectionDelay)
949+
void Sirens::hkRegisterCorona(std::function<hkRegisterCoronaFunc> originalCall, unsigned int& id, CEntity*& attachTo, unsigned char& red, unsigned char& green, unsigned char& blue, unsigned char& alpha, const CVector& posn, float& radius, float& farClip, eCoronaType& coronaType, eCoronaFlareType& flaretype, bool& enableReflection, bool& checkObstacles, int& _param_not_used, float& angle, bool& longDistance, float& nearClip, unsigned char& fadeState, float& fadeSpeed, bool& onlyFromBelow, bool& reflectionDelay)
944950
{
945-
CVehicle *vehicle = NULL;
946-
947-
_asm {
948-
pushad
949-
mov vehicle, esi
950-
popad
951-
}
951+
CVehicle *vehicle = (CVehicle*)attachTo;
952952

953953
if (vehicle && modelData.contains(vehicle->m_nModelIndex))
954954
{
955955
return;
956956
}
957957

958-
CCoronas::RegisterCorona(id, attachTo, red, green, blue, alpha, posn, radius, farClip, coronaType, flaretype, enableReflection, checkObstacles, _param_not_used, angle, longDistance, nearClip, fadeState, fadeSpeed, onlyFromBelow, reflectionDelay);
958+
originalCall(id, attachTo, red, green, blue, alpha, posn, radius, farClip, coronaType, flaretype, enableReflection, checkObstacles, _param_not_used, angle, longDistance, nearClip, fadeState, fadeSpeed, onlyFromBelow, reflectionDelay);
959959
}
960960

961961
void Sirens::EnableDummy(int id, VehicleDummy *dummy, CVehicle *vehicle, VehicleSirenMaterial *material, eCoronaFlareType type, uint64_t time)

src/features/sirens.h

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -207,9 +207,13 @@ class Sirens : public CBaseFeature
207207
static inline std::map<int, VehicleSirenData *> modelData;
208208
static inline std::map<int, std::vector<VehicleDummy *>> modelRotators;
209209

210-
static char __fastcall hkUsesSiren(CVehicle *ptr);
211-
static void hkRegisterCorona(unsigned int id, CEntity *attachTo, unsigned char red, unsigned char green, unsigned char blue, unsigned char alpha, CVector const &posn, float radius, float farClip, eCoronaType coronaType, eCoronaFlareType flaretype, bool enableReflection, bool checkObstacles, int _param_not_used, float angle, bool longDistance, float nearClip, unsigned char fadeState, float fadeSpeed, bool onlyFromBelow, bool reflectionDelay);
212-
static void __cdecl hkAddPointLights(uint8_t type, float px, float py, float pz, float dx, float dy, float dz, float range, float red, float green, float blue, uint8_t fogEffect, bool bCastsShadowFromPlayerCarAndPed, CEntity* castingEntity);
210+
using hkUsesSirenFunc = bool(CVehicle*);
211+
using hkRegisterCoronaFunc = void(unsigned int&, CEntity *&, unsigned char&, unsigned char&, unsigned char&, unsigned char&, const CVector&, float&, float&, eCoronaType&, eCoronaFlareType&, bool&, bool&, int&, float&, bool&, float&, unsigned char&, float&, bool&, bool&);
212+
using hkAddPointLightsFunc = void(unsigned char&, CVector&, CVector&, float&, float&, float&, float&, unsigned char&, bool&, CEntity*&);
213+
214+
static bool hkUsesSiren(std::function<hkUsesSirenFunc> originalCall, CVehicle* ptr);
215+
static void hkRegisterCorona(std::function<hkRegisterCoronaFunc> originalCall, unsigned int& id, CEntity*& attachTo, unsigned char& red, unsigned char& green, unsigned char& blue, unsigned char& alpha, const CVector& posn, float& radius, float& farClip, eCoronaType& coronaType, eCoronaFlareType& flaretype, bool& enableReflection, bool& checkObstacles, int& _param_not_used, float& angle, bool& longDistance, float& nearClip, unsigned char& fadeState, float& fadeSpeed, bool& onlyFromBelow, bool& reflectionDelay);
216+
static void hkAddPointLights(std::function<hkAddPointLightsFunc> originalCall, uint8_t& type, CVector& position, CVector& direction, float& range, float& red, float& green, float& blue, uint8_t& fogEffect, bool& bCastsShadowFromPlayerCarAndPed, CEntity*& castingEntity);
213217

214218
static void RegisterMaterial(CVehicle *vehicle, RpMaterial *material);
215219
static void EnableDummy(int id, VehicleDummy *dummy, CVehicle *vehicle, VehicleSirenMaterial *material, eCoronaFlareType type, uint64_t time);

0 commit comments

Comments
 (0)