2121
2222#include < cstdio>
2323#include < filesystem>
24+ #include < Hook.hpp>
25+ #include < InstanceIdentifier.hpp>
2426
2527REDECL (EngineDemoRecorder::SetSignonState);
2628REDECL (EngineDemoRecorder::StartRecording);
@@ -346,6 +348,30 @@ DETOUR_COMMAND(EngineDemoRecorder::record) {
346348 }
347349}
348350
351+ Memory::Patch *g_StartupDemoFile_headerNamePatch;
352+ Memory::Patch *g_StartupDemoHeader_headerNamePatch;
353+ std::string g_uniqueDemoHeaderName;
354+
355+ void patchDemoHeaderTempFileToBeUnique () {
356+ int instanceID = InstanceIdentifier::GetID ();
357+ if (instanceID == 0 ) {
358+ // no need to patch if we're the only instance
359+ return ;
360+ }
361+
362+ g_uniqueDemoHeaderName = Utils::ssprintf (" demoheader_%d.tmp" , instanceID+1 );
363+
364+ auto uniqueDemoHeaderName = g_uniqueDemoHeaderName.c_str ();
365+ auto uniqueDemoHeaderNamePtr = (unsigned char *)&uniqueDemoHeaderName;
366+
367+ g_StartupDemoFile_headerNamePatch = new Memory::Patch ();
368+ auto StartupDemoFile = Memory::Scan (engine->demorecorder ->Name (), Offsets::StartupDemoFile);
369+ g_StartupDemoFile_headerNamePatch->Execute (StartupDemoFile + Offsets::StartupDemoFile_HeaderName, uniqueDemoHeaderNamePtr, 4 );
370+ g_StartupDemoHeader_headerNamePatch = new Memory::Patch ();
371+ auto StartupDemoHeader = Memory::Scan (engine->demorecorder ->Name (), Offsets::StartupDemoHeader);
372+ g_StartupDemoHeader_headerNamePatch->Execute (StartupDemoHeader + Offsets::StartupDemoHeader_HeaderName, uniqueDemoHeaderNamePtr, 4 );
373+ }
374+
349375bool EngineDemoRecorder::Init () {
350376 auto disconnect = engine->cl ->Original (Offsets::Disconnect);
351377 auto demorecorder = Memory::DerefDeref<void *>(disconnect + Offsets::demorecorder);
@@ -363,6 +389,8 @@ bool EngineDemoRecorder::Init() {
363389 this ->m_bRecording = reinterpret_cast <bool *>((uintptr_t )demorecorder + Offsets::m_bRecording);
364390
365391 engine->net_time = Memory::Deref<double *>((uintptr_t )this ->GetRecordingTick + Offsets::net_time);
392+
393+ patchDemoHeaderTempFileToBeUnique ();
366394 }
367395
368396 Command::Hook (" stop" , EngineDemoRecorder::stop_callback_hook, EngineDemoRecorder::stop_callback);
@@ -371,6 +399,11 @@ bool EngineDemoRecorder::Init() {
371399 return this ->hasLoaded = this ->s_ClientDemoRecorder ;
372400}
373401void EngineDemoRecorder::Shutdown () {
402+ g_StartupDemoFile_headerNamePatch->Restore ();
403+ SAFE_DELETE (g_StartupDemoFile_headerNamePatch);
404+ g_StartupDemoHeader_headerNamePatch->Restore ();
405+ SAFE_DELETE (g_StartupDemoHeader_headerNamePatch);
406+
374407 Interface::Delete (this ->s_ClientDemoRecorder );
375408 Command::Unhook (" stop" , EngineDemoRecorder::stop_callback);
376409 Command::Unhook (" record" , EngineDemoRecorder::record_callback);
0 commit comments