From 24ccd9af3b4b4d96988bdf0739badd28912f199a Mon Sep 17 00:00:00 2001 From: Edo Date: Sat, 17 Dec 2022 18:54:41 +0100 Subject: [PATCH] [String]: Use ARRAY_COUNT (#656) --- src/Components/Modules/Download.cpp | 2 +- src/Components/Modules/Scheduler.cpp | 2 +- src/Components/Modules/Threading.cpp | 3 +++ src/Game/Structs.hpp | 26 ++++++++++++++++++++++++++ src/Utils/String.hpp | 5 ++++- src/Utils/Thread.cpp | 28 ++++++++++++++-------------- src/Utils/Thread.hpp | 20 ++++++++++---------- 7 files changed, 59 insertions(+), 27 deletions(-) diff --git a/src/Components/Modules/Download.cpp b/src/Components/Modules/Download.cpp index 16c324d4..285503a3 100644 --- a/src/Components/Modules/Download.cpp +++ b/src/Components/Modules/Download.cpp @@ -688,7 +688,7 @@ namespace Components ServerRunning = true; Terminate = false; - ServerThread = std::thread([] + ServerThread = Utils::Thread::CreateNamedThread("Mongoose", [] { while (!Terminate) { diff --git a/src/Components/Modules/Scheduler.cpp b/src/Components/Modules/Scheduler.cpp index bc29402e..48246130 100644 --- a/src/Components/Modules/Scheduler.cpp +++ b/src/Components/Modules/Scheduler.cpp @@ -156,7 +156,7 @@ namespace Components Scheduler::Scheduler() { - Thread = Utils::Thread::createNamedThread("Async Scheduler", [] + Thread = Utils::Thread::CreateNamedThread("Async Scheduler", [] { while (!Kill) { diff --git a/src/Components/Modules/Threading.cpp b/src/Components/Modules/Threading.cpp index f22315a7..9382a1f9 100644 --- a/src/Components/Modules/Threading.cpp +++ b/src/Components/Modules/Threading.cpp @@ -154,6 +154,9 @@ namespace Components // dvar setting function, unknown stuff related to server thread sync Utils::Hook::Set(0x647781, 0xEB); + // make VA thread safe + Utils::Hook(0x4785B0, Utils::String::VA, HOOK_JUMP).install()->quick(); + Utils::Hook(0x627695, 0x627040, HOOK_CALL).install()->quick(); Utils::Hook(0x43D1C7, PacketEventStub, HOOK_JUMP).install()->quick(); Utils::Hook(0x6272E3, FrameEpilogueStub, HOOK_JUMP).install()->quick(); diff --git a/src/Game/Structs.hpp b/src/Game/Structs.hpp index 09ffb489..a8be593b 100644 --- a/src/Game/Structs.hpp +++ b/src/Game/Structs.hpp @@ -10307,6 +10307,32 @@ namespace Game GfxLight sceneLights[253]; }; + enum + { + THREAD_VALUE_PROF_STACK = 0x0, + THREAD_VALUE_VA = 0x1, + THREAD_VALUE_COM_ERROR = 0x2, + THREAD_VALUE_TRACE = 0x3, + THREAD_VALUE_COUNT = 0x4, + }; + + struct va_info_t + { + char va_string[2][1024]; + int index; + }; + + struct TraceCheckCount + { + int global; + int* partitions; + }; + + struct TraceThreadInfo + { + TraceCheckCount checkcount; + }; + #pragma endregion #ifndef IDA diff --git a/src/Utils/String.hpp b/src/Utils/String.hpp index 242ea0d9..88856d75 100644 --- a/src/Utils/String.hpp +++ b/src/Utils/String.hpp @@ -1,5 +1,8 @@ #pragma once +template +constexpr auto ARRAY_COUNT(Type(&)[n]) { return n; } + namespace Utils::String { template @@ -13,7 +16,7 @@ namespace Utils::String [[nodiscard]] const char* get(const char* format, va_list ap) { - ++this->currentBuffer %= ARRAYSIZE(this->stringPool); + ++this->currentBuffer %= ARRAY_COUNT(this->stringPool); auto entry = &this->stringPool[this->currentBuffer]; if (!entry->size || !entry->buffer) diff --git a/src/Utils/Thread.cpp b/src/Utils/Thread.cpp index e328ff30..c6e5423c 100644 --- a/src/Utils/Thread.cpp +++ b/src/Utils/Thread.cpp @@ -2,7 +2,7 @@ namespace Utils::Thread { - bool setName(const HANDLE t, const std::string& name) + bool SetName(const HANDLE t, const std::string& name) { const Library kernel32("kernel32.dll"); if (!kernel32) @@ -19,7 +19,7 @@ namespace Utils::Thread return SUCCEEDED(setDescription(t, String::Convert(name).data())); } - bool setName(const DWORD id, const std::string& name) + bool SetName(const DWORD id, const std::string& name) { auto* const t = OpenThread(THREAD_SET_LIMITED_INFORMATION, FALSE, id); if (!t) return false; @@ -29,20 +29,20 @@ namespace Utils::Thread CloseHandle(t); }); - return setName(t, name); + return SetName(t, name); } - bool setName(std::thread& t, const std::string& name) + bool SetName(std::thread& t, const std::string& name) { - return setName(t.native_handle(), name); + return SetName(t.native_handle(), name); } - bool setName(const std::string& name) + bool SetName(const std::string& name) { - return setName(GetCurrentThread(), name); + return SetName(GetCurrentThread(), name); } - std::vector getThreadIds() + std::vector GetThreadIds() { auto* const h = CreateToolhelp32Snapshot(TH32CS_SNAPTHREAD, GetCurrentProcessId()); if (h == INVALID_HANDLE_VALUE) @@ -79,9 +79,9 @@ namespace Utils::Thread return ids; } - void forEachThread(const std::function& callback) + void ForEachThread(const std::function& callback) { - const auto ids = getThreadIds(); + const auto ids = GetThreadIds(); for (const auto& id : ids) { @@ -98,9 +98,9 @@ namespace Utils::Thread } } - void suspendOtherThreads() + void SuspendOtherThreads() { - forEachThread([](const HANDLE thread) + ForEachThread([](const HANDLE thread) { if (GetThreadId(thread) != GetCurrentThreadId()) { @@ -109,9 +109,9 @@ namespace Utils::Thread }); } - void resumeOtherThreads() + void ResumeOtherThreads() { - forEachThread([](const HANDLE thread) + ForEachThread([](const HANDLE thread) { if (GetThreadId(thread) != GetCurrentThreadId()) { diff --git a/src/Utils/Thread.hpp b/src/Utils/Thread.hpp index 807633f4..084eb2ce 100644 --- a/src/Utils/Thread.hpp +++ b/src/Utils/Thread.hpp @@ -2,22 +2,22 @@ namespace Utils::Thread { - bool setName(HANDLE t, const std::string& name); - bool setName(DWORD id, const std::string& name); - bool setName(std::thread& t, const std::string& name); - bool setName(const std::string& name); + bool SetName(HANDLE t, const std::string& name); + bool SetName(DWORD id, const std::string& name); + bool SetName(std::thread& t, const std::string& name); + bool SetName(const std::string& name); template - std::thread createNamedThread(const std::string& name, Args&&... args) + std::thread CreateNamedThread(const std::string& name, Args&&... args) { auto t = std::thread(std::forward(args)...); - setName(t, name); + SetName(t, name); return t; } - std::vector getThreadIds(); - void forEachThread(const std::function& callback); + std::vector GetThreadIds(); + void ForEachThread(const std::function& callback); - void suspendOtherThreads(); - void resumeOtherThreads(); + void SuspendOtherThreads(); + void ResumeOtherThreads(); }