Implement 'ThreadInMainThread()' (originally tier0 export)

This commit is contained in:
Kawe Mazidjatari 2022-08-18 12:00:01 +02:00
parent edbf1af98c
commit efd16fbc72
5 changed files with 47 additions and 11 deletions

View File

@ -17,6 +17,7 @@ using std::ostringstream;
using std::unordered_map;
namespace fs = std::filesystem;
typedef DWORD ThreadId_t;
typedef const unsigned char* rsig_t;
typedef std::make_signed_t<std::size_t> ssize_t;

View File

@ -264,7 +264,7 @@ void MOD_ProcessPakQueue()
}
sub_140441220(v25, v24);
if (GetCurrentThreadId() == *dword_1641E443C)
if (ThreadInMainThread())
{
if (*g_bPakFifoLockAcquired)
{

View File

@ -18,7 +18,6 @@ inline void** unk_1418749B0;
inline void** unk_141874550;
inline int64_t* qword_167ED7BC0;
inline int64_t* qword_167ED7C68;
inline DWORD* dword_1641E443C;
inline auto sub_14045BAC0 = p_MOD_ProcessPakQueue.RCast<__int64(*)(__int64(__fastcall* a1)(__int64, _DWORD*, __int64, _QWORD*), JobFifoLock_s* pFifoLock, __int64 a3, __int64 a4)>();
inline auto sub_14045A1D0 = p_MOD_ProcessPakQueue.RCast<__int64(*)(unsigned __int8(__fastcall* a1)(_QWORD), JobFifoLock_s* pFifoLock, __int64 a3, __int64 a4, volatile signed __int64* a5, char a6)>();
@ -57,7 +56,6 @@ class VModel_BSP : public IDetour
spdlog::debug("| VAR: unk_141874550 : {:#18x} |\n", reinterpret_cast<uintptr_t>(unk_141874550));
spdlog::debug("| VAR: qword_167ED7BC0 : {:#18x} |\n", reinterpret_cast<uintptr_t>(qword_167ED7BC0));
spdlog::debug("| VAR: qword_167ED7C68 : {:#18x} |\n", reinterpret_cast<uintptr_t>(qword_167ED7C68));
spdlog::debug("| VAR: dword_1641E443C : {:#18x} |\n", reinterpret_cast<uintptr_t>(dword_1641E443C)); // TODO: ThreadInMainThread() (g_ThreadMainThreadID) threadtools.cpp
spdlog::debug("+----------------------------------------------------------------+\n");
}
virtual void GetFun(void) const
@ -93,7 +91,6 @@ class VModel_BSP : public IDetour
unk_141874550 = p_MOD_ProcessPakQueue.Offset(0x150).FindPatternSelf("48 8D 2D").ResolveRelativeAddressSelf(0x3, 0x7).RCast<void**>();
qword_167ED7BC0 = p_MOD_ProcessPakQueue.Offset(0x200).FindPatternSelf("48 83 3D").ResolveRelativeAddressSelf(0x3, 0x8).RCast<int64_t*>();
qword_167ED7C68 = p_MOD_ProcessPakQueue.Offset(0x200).FindPatternSelf("0F B7 05").ResolveRelativeAddressSelf(0x3, 0x7).RCast<int64_t*>();
dword_1641E443C = p_MOD_ProcessPakQueue.Offset(0x2A0).FindPatternSelf("3B 05").ResolveRelativeAddressSelf(0x2, 0x6).RCast<DWORD*>();
(*((char**)(&qword_167ED7C68))) -= 6;
}

View File

@ -28,4 +28,26 @@ int64 ThreadInterlockedCompareExchange64(int64 volatile* pDest, int64 value, int
bool ThreadInterlockedAssignIf64(int64 volatile* pDest, int64 value, int64 comperand)
{
return _InterlockedCompareExchange64(pDest, comperand, value);
}
bool ThreadInMainThread()
{
return (ThreadGetCurrentId() == (*g_ThreadMainThreadID));
}
ThreadId_t ThreadGetCurrentId()
{
#ifdef _WIN32
return GetCurrentThreadId();
#elif defined( _PS3 )
sys_ppu_thread_t th = 0;
sys_ppu_thread_get_id(&th);
return th;
#elif defined(POSIX)
return (ThreadId_t)pthread_self();
#else
Assert(0);
DebuggerBreak();
return 0;
#endif
}

View File

@ -52,10 +52,9 @@ inline void ThreadPause()
#error "implement me"
#endif
}
LONG ThreadInterlockedCompareExchange64(LONG volatile* pDest, int64 value, int64 comperand);
bool ThreadInterlockedAssignIf(LONG volatile* p, int32 value, int32 comperand);
int64 ThreadInterlockedCompareExchange64(int64 volatile* pDest, int64 value, int64 comperand);
bool ThreadInterlockedAssignIf64(int64 volatile* pDest, int64 value, int64 comperand);
bool ThreadInMainThread();
ThreadId_t ThreadGetCurrentId();
//-----------------------------------------------------------------------------
//
@ -64,6 +63,11 @@ bool ThreadInterlockedAssignIf64(int64 volatile* pDest, int64 value, int64 compe
//
//-----------------------------------------------------------------------------
LONG ThreadInterlockedCompareExchange64(LONG volatile* pDest, int64 value, int64 comperand);
bool ThreadInterlockedAssignIf(LONG volatile* p, int32 value, int32 comperand);
int64 ThreadInterlockedCompareExchange64(int64 volatile* pDest, int64 value, int64 comperand);
bool ThreadInterlockedAssignIf64(int64 volatile* pDest, int64 value, int64 comperand);
#ifdef _WIN32
#define NOINLINE
#elif defined( _PS3 )
@ -205,6 +209,11 @@ inline auto v_MutexInternal_WaitForLock = p_MutexInternal_WaitForLock.RCast<int
inline CMemory p_MutexInternal_ReleaseWaiter;
inline auto v_MutexInternal_ReleaseWaiter = p_MutexInternal_ReleaseWaiter.RCast<int (*)(CThreadFastMutex* mutex)>();
inline CMemory p_DeclareCurrentThreadIsMainThread;
inline auto v_DeclareCurrentThreadIsMainThread = p_DeclareCurrentThreadIsMainThread.RCast<ThreadId_t (*)(void)>();
inline ThreadId_t* g_ThreadMainThreadID;
///////////////////////////////////////////////////////////////////////////////
class CThreadFastMutex
{
@ -235,17 +244,24 @@ class VThreadTools : public IDetour
{
spdlog::debug("| FUN: CThreadFastMutex::WaitForLock : {:#18x} |\n", p_MutexInternal_WaitForLock.GetPtr());
spdlog::debug("| FUN: CThreadFastMutex::ReleaseWaiter : {:#18x} |\n", p_MutexInternal_ReleaseWaiter.GetPtr());
spdlog::debug("| FUN: DeclareCurrentThreadIsMainThread : {:#18x} |\n", p_DeclareCurrentThreadIsMainThread.GetPtr());
spdlog::debug("| VAR: g_ThreadMainThreadID : {:#18x} |\n", reinterpret_cast<uintptr_t>(g_ThreadMainThreadID));
spdlog::debug("+----------------------------------------------------------------+\n");
}
virtual void GetFun(void) const
{
p_MutexInternal_WaitForLock = g_GameDll.FindPatternSIMD(reinterpret_cast<rsig_t>("\x48\x89\x5C\x24\x00\x48\x89\x74\x24\x00\x57\x48\x83\xEC\x20\x48\x8B\xD9\xFF\x15\x00\x00\x00\x00"), "xxxx?xxxx?xxxxxxxxxx????");
p_MutexInternal_ReleaseWaiter = g_GameDll.FindPatternSIMD(reinterpret_cast<rsig_t>("\x40\x53\x48\x83\xEC\x20\x8B\x41\x04\x48\x8B\xD9\x83\xE8\x01"), "xxxxxxxxxxxxxxx");
p_DeclareCurrentThreadIsMainThread = g_GameDll.FindPatternSIMD(reinterpret_cast<rsig_t>("\x48\x83\xEC\x28\xFF\x15\x00\x00\x00\x00\x89\x05\x00\x00\x00\x00\x48\x83\xC4\x28"), "xxxxxx????xx????xxxx");
v_MutexInternal_WaitForLock = p_MutexInternal_WaitForLock.RCast<int (*)(CThreadFastMutex*)>(); /*48 89 5C 24 ?? 48 89 74 24 ?? 57 48 83 EC 20 48 8B D9 FF 15 ?? ?? ?? ??*/
v_MutexInternal_ReleaseWaiter = p_MutexInternal_ReleaseWaiter.RCast<int (*)(CThreadFastMutex*)>(); /*40 53 48 83 EC 20 8B 41 04 48 8B D9 83 E8 01*/
v_MutexInternal_WaitForLock = p_MutexInternal_WaitForLock.RCast<int (*)(CThreadFastMutex*)>(); /*48 89 5C 24 ?? 48 89 74 24 ?? 57 48 83 EC 20 48 8B D9 FF 15 ?? ?? ?? ??*/
v_MutexInternal_ReleaseWaiter = p_MutexInternal_ReleaseWaiter.RCast<int (*)(CThreadFastMutex*)>(); /*40 53 48 83 EC 20 8B 41 04 48 8B D9 83 E8 01*/
v_DeclareCurrentThreadIsMainThread = p_DeclareCurrentThreadIsMainThread.RCast<ThreadId_t(*)(void)>(); /*48 83 EC 28 FF 15 ?? ?? ?? ?? 89 05 ?? ?? ?? ?? 48 83 C4 28 */
}
virtual void GetVar(void) const
{
g_ThreadMainThreadID = p_DeclareCurrentThreadIsMainThread.FindPattern("89 05").ResolveRelativeAddressSelf(0x2, 0x6).RCast<DWORD*>();
}
virtual void GetVar(void) const { }
virtual void GetCon(void) const { }
virtual void Attach(void) const { }
virtual void Detach(void) const { }