Skip to content

Commit

Permalink
Fix cdcMultiCore breaking with high number of CPU cores
Browse files Browse the repository at this point in the history
  • Loading branch information
TheIndra55 committed Sep 22, 2024
1 parent 9526daf commit 213df3a
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 0 deletions.
15 changes: 15 additions & 0 deletions src/modules/Patches.cpp
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
#include <Hooking.Patterns.h>
#include <MinHook.h>
#include <thread>

#include "Patches.h"
#include "util/Hooking.h"
#include "game/Game.h"
#include "MainMenu.h"
#include "patches/Reloc.h"
#include "patches/ErrorHandler.h"
#include "patches/Multicore.h"
#include "game/Camera.h"

// Instance of patches so we can get it in our hooks without calling GetModule<T> each call
Expand Down Expand Up @@ -91,6 +93,13 @@ static void __stdcall DeathState_Process(int player, int data)
}
}

static unsigned char CPUCount(unsigned int* TotAvailLogical, unsigned int* TotAvailCore, unsigned int* PhysicalNum)
{
*TotAvailLogical = std::thread::hardware_concurrency();

return 0;
}

Patches::Patches()
{
s_patches = this;
Expand Down Expand Up @@ -134,6 +143,12 @@ Patches::Patches()
PatchHeapSize();
}

#ifdef TR8
// Fix cdcMultiCore breaking on high number of CPU cores
MH_CreateHook((void*)0x4A21D0, CPUCount, nullptr);
MH_CreateHook((void*)0x4A2680, cdc::JobChainImplWithThreads::StartSystem, (void**)&cdc::JobChainImplWithThreads::s_StartSystem);
#endif

#ifdef TR7
if (m_shadowMapSize.GetValue() > 0)
{
Expand Down
18 changes: 18 additions & 0 deletions src/modules/patches/Multicore.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#include "Multicore.h"

#include "Hook.h"
#include "modules/Log.h"

bool(*cdc::JobChainImplWithThreads::s_StartSystem)(int);

bool cdc::JobChainImplWithThreads::StartSystem(int numThreads)
{
if (numThreads > 32)
{
numThreads = 32;
}

Hook::GetInstance().GetModule<Log>()->WriteLine("cdc::JobChainImplWithThreads::StartSystem(%d)", numThreads);

return s_StartSystem(numThreads);
}
12 changes: 12 additions & 0 deletions src/modules/patches/Multicore.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#pragma once

namespace cdc
{
class JobChainImplWithThreads
{
public:
static bool(*s_StartSystem)(int);

static bool StartSystem(int numThreads);
};
}

0 comments on commit 213df3a

Please sign in to comment.