@@ -468,6 +468,9 @@ DETOUR(Engine::PurgeUnusedModels) {
468468 return result;
469469}
470470
471+ Memory::Patch *g_ReadCustomDataPatch = nullptr ;
472+ Memory::Patch *g_ReadConsoleCommandPatch = nullptr ;
473+
471474DETOUR (Engine::ReadCustomData, int *callbackIndex, char **data) {
472475 auto size = Engine::ReadCustomData (thisptr, callbackIndex, data);
473476 if (callbackIndex && data && *callbackIndex == 0 && size > 8 ) {
@@ -1127,20 +1130,20 @@ bool Engine::Init() {
11271130 Cmd_ExecuteCommand_Hook.SetFunc (g_Cmd_ExecuteCommand);
11281131 InsertCommand_Hook.SetFunc (g_InsertCommand);
11291132
1130- this ->readCustomDataInjectAddr = Memory::Scan (this ->Name (), Offsets::readCustomDataInjectSig, Offsets::readCustomDataInjectOff);
1131- this ->readConsoleCommandInjectAddr = Memory::Scan (this ->Name (), Offsets::readConsoleCommandInjectSig, Offsets::readConsoleCommandInjectOff);
1132- if (this ->readCustomDataInjectAddr && this ->readConsoleCommandInjectAddr ) {
1133- // Pesky memory protection doesn't want us overwriting code - we
1134- // get around it with a call to mprotect or VirtualProtect
1135- Memory::UnProtect ((void *)this ->readCustomDataInjectAddr , 4 );
1136- Memory::UnProtect ((void *)this ->readConsoleCommandInjectAddr , 4 );
1137-
1138- // It's a relative call, so we have to do some weird fuckery lol
1139- Engine::ReadCustomData = reinterpret_cast <_ReadCustomData>(*(uint32_t *)this ->readCustomDataInjectAddr + (this ->readCustomDataInjectAddr + 4 ));
1140- *(uint32_t *)this ->readCustomDataInjectAddr = (uint32_t )&ReadCustomData_Hook - (this ->readCustomDataInjectAddr + 4 ); // Add 4 to get address of next instruction
1133+ g_ReadCustomDataPatch = new Memory::Patch ();
1134+ auto readCustomDataInjectAddr = Memory::Scan (this ->Name (), Offsets::readCustomDataInjectSig, Offsets::readCustomDataInjectOff);
1135+ if (readCustomDataInjectAddr) {
1136+ Engine::ReadCustomData = (_ReadCustomData)Memory::Read (readCustomDataInjectAddr);
1137+ auto ReadCustomDataInject = (uint32_t )&ReadCustomData_Hook - (readCustomDataInjectAddr + 4 );
1138+ g_ReadCustomDataPatch->Execute (readCustomDataInjectAddr, (unsigned char *)&ReadCustomDataInject, 4 );
1139+ }
11411140
1142- Engine::ReadConsoleCommand = (_ReadConsoleCommand)Memory::Read (this ->readConsoleCommandInjectAddr );
1143- *(uint32_t *)this ->readConsoleCommandInjectAddr = (uint32_t )&ReadConsoleCommand_Hook - (this ->readConsoleCommandInjectAddr + 4 );
1141+ g_ReadConsoleCommandPatch = new Memory::Patch ();
1142+ auto readConsoleCommandInjectAddr = Memory::Scan (this ->Name (), Offsets::readConsoleCommandInjectSig, Offsets::readConsoleCommandInjectOff);
1143+ if (readConsoleCommandInjectAddr) {
1144+ Engine::ReadConsoleCommand = (_ReadConsoleCommand)Memory::Read (readConsoleCommandInjectAddr);
1145+ auto ReadConsoleCommandInject = (uint32_t )&ReadConsoleCommand_Hook - (readConsoleCommandInjectAddr + 4 );
1146+ g_ReadConsoleCommandPatch->Execute (readConsoleCommandInjectAddr, (unsigned char *)&ReadConsoleCommandInject, 4 );
11441147 }
11451148
11461149 if (auto debugoverlay = Interface::Create (this ->Name (), " VDebugOverlay004" , false )) {
@@ -1229,15 +1232,10 @@ void Engine::Shutdown() {
12291232 Interface::Delete (this ->g_physCollision );
12301233
12311234 // Reset to the offsets that were originally in the code
1232- if (this ->readCustomDataInjectAddr && this ->readConsoleCommandInjectAddr ) {
1233- #ifdef _WIN32
1234- *(uint32_t *)this ->readCustomDataInjectAddr = 0x50E8458D ;
1235- *(uint32_t *)this ->readConsoleCommandInjectAddr = 0x000491E3 ;
1236- #else
1237- *(uint32_t *)this ->readCustomDataInjectAddr = 0x08244489 ;
1238- *(uint32_t *)this ->readConsoleCommandInjectAddr = 0x0008155A ;
1239- #endif
1240- }
1235+ g_ReadCustomDataPatch->Restore ();
1236+ g_ReadConsoleCommandPatch->Restore ();
1237+ SAFE_DELETE (g_ReadCustomDataPatch)
1238+ SAFE_DELETE (g_ReadConsoleCommandPatch)
12411239
12421240#ifdef _WIN32
12431241 MH_UNHOOK (Engine::ParseSmoothingInfo_Mid);
0 commit comments