diff --git a/src/Components/Modules/ClientCommand.cpp b/src/Components/Modules/ClientCommand.cpp index 07fdbba1..a005cdca 100644 --- a/src/Components/Modules/ClientCommand.cpp +++ b/src/Components/Modules/ClientCommand.cpp @@ -246,7 +246,7 @@ namespace Components if (ent->client->sess.sessionState != Game::SESS_STATE_PLAYING || !CheatsOk(ent)) return; - auto** bgs = Game::Sys::GetTls(Game::Sys::ThreadOffset::LEVEL_BGS); + auto** bgs = Game::Sys::GetTls(Game::Sys::TLS_OFFSET::LEVEL_BGS); assert(*bgs == nullptr); diff --git a/src/Game/Common.hpp b/src/Game/Common.hpp index 85f296bd..012e5f07 100644 --- a/src/Game/Common.hpp +++ b/src/Game/Common.hpp @@ -77,16 +77,16 @@ namespace Game extern const char* Com_LoadInfoString_FastFile(const char* fileName, const char* fileDesc, const char* ident, char* loadBuffer); } -#define Com_InitThreadData() \ -{ \ - static Game::ProfileStack profile_stack{}; \ - static Game::va_info_t va_info{}; \ - static jmp_buf g_com_error{}; \ - static Game::TraceThreadInfo g_trace_thread_info{}; \ - static void* g_thread_values[Game::THREAD_VALUE_COUNT]{}; \ - *(Game::Sys::GetTls(Game::Sys::THREAD_VALUES)) = g_thread_values; \ - Game::Sys_SetValue(Game::THREAD_VALUE_PROF_STACK, &profile_stack); \ - Game::Sys_SetValue(Game::THREAD_VALUE_VA, &va_info); \ - Game::Sys_SetValue(Game::THREAD_VALUE_COM_ERROR, &g_com_error); \ - Game::Sys_SetValue(Game::THREAD_VALUE_TRACE, &g_trace_thread_info); \ +#define Com_InitThreadData() \ +{ \ + static Game::ProfileStack profile_stack{}; \ + static Game::va_info_t va_info{}; \ + static jmp_buf g_com_error{}; \ + static Game::TraceThreadInfo g_trace_thread_info{}; \ + static void* g_thread_values[Game::THREAD_VALUE_COUNT]{}; \ + *(Game::Sys::GetTls(Game::Sys::TLS_OFFSET::THREAD_VALUES)) = g_thread_values; \ + Game::Sys_SetValue(Game::THREAD_VALUE_PROF_STACK, &profile_stack); \ + Game::Sys_SetValue(Game::THREAD_VALUE_VA, &va_info); \ + Game::Sys_SetValue(Game::THREAD_VALUE_COM_ERROR, &g_com_error); \ + Game::Sys_SetValue(Game::THREAD_VALUE_TRACE, &g_trace_thread_info); \ } diff --git a/src/Game/System.cpp b/src/Game/System.cpp index b4943c4c..21148722 100644 --- a/src/Game/System.cpp +++ b/src/Game/System.cpp @@ -12,6 +12,7 @@ namespace Game Sys_ListFiles_t Sys_ListFiles = Sys_ListFiles_t(0x45A660); Sys_Milliseconds_t Sys_Milliseconds = Sys_Milliseconds_t(0x42A660); Sys_Error_t Sys_Error = Sys_Error_t(0x43D570); + Sys_Sleep_t Sys_Sleep = Sys_Sleep_t(0x4169C0); Sys_LockWrite_t Sys_LockWrite = Sys_LockWrite_t(0x435880); Sys_TempPriorityAtLeastNormalBegin_t Sys_TempPriorityAtLeastNormalBegin = Sys_TempPriorityAtLeastNormalBegin_t(0x478680); Sys_TempPriorityEnd_t Sys_TempPriorityEnd = Sys_TempPriorityEnd_t(0x4DCF00); @@ -30,7 +31,7 @@ namespace Game void Sys_LockRead(FastCriticalSection* critSect) { InterlockedIncrement(&critSect->readCount); - while (critSect->writeCount) std::this_thread::sleep_for(1ms); + while (critSect->writeCount) Sys_Sleep(1); } void Sys_UnlockRead(FastCriticalSection* critSect) @@ -39,10 +40,16 @@ namespace Game InterlockedDecrement(&critSect->readCount); } + void Sys_UnlockWrite(FastCriticalSection* critSect) + { + assert(critSect->writeCount > 0); + InterlockedDecrement(&critSect->writeCount); + Sys_TempPriorityEnd(&critSect->tempPriority); + } + bool Sys_TryEnterCriticalSection(CriticalSection critSect) { - assert(static_cast(critSect) < - static_cast(CRITSECT_COUNT)); + AssertIn(critSect, CRITSECT_COUNT); return TryEnterCriticalSection(&s_criticalSection[critSect]) != FALSE; } diff --git a/src/Game/System.hpp b/src/Game/System.hpp index db6ce89f..5e3260f1 100644 --- a/src/Game/System.hpp +++ b/src/Game/System.hpp @@ -32,6 +32,9 @@ namespace Game typedef int(*Sys_Milliseconds_t)(); extern Sys_Milliseconds_t Sys_Milliseconds; + typedef void(*Sys_Sleep_t)(int msec); + extern Sys_Sleep_t Sys_Sleep; + typedef void(*Sys_LockWrite_t)(FastCriticalSection* critSect); extern Sys_LockWrite_t Sys_LockWrite; @@ -68,13 +71,14 @@ namespace Game extern void Sys_LockRead(FastCriticalSection* critSect); extern void Sys_UnlockRead(FastCriticalSection* critSect); + extern void Sys_UnlockWrite(FastCriticalSection* critSect); extern bool Sys_TryEnterCriticalSection(CriticalSection critSect); class Sys { public: - enum ThreadOffset : unsigned int + enum class TLS_OFFSET : unsigned int { LEVEL_BGS = 0xC, THREAD_VALUES = 0x14, @@ -85,10 +89,10 @@ namespace Game }; template - static T* GetTls(ThreadOffset offset) + static T* GetTls(TLS_OFFSET offset) { const auto* tls = reinterpret_cast(__readfsdword(0x2C)); - return reinterpret_cast(tls[*g_dwTlsIndex] + offset); + return reinterpret_cast(tls[*g_dwTlsIndex] + static_cast>(offset)); } }; }