[String]: Use ARRAY_COUNT (#656)

This commit is contained in:
Edo 2022-12-17 18:54:41 +01:00 committed by GitHub
parent 54aabee08d
commit 24ccd9af3b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 59 additions and 27 deletions

View File

@ -688,7 +688,7 @@ namespace Components
ServerRunning = true; ServerRunning = true;
Terminate = false; Terminate = false;
ServerThread = std::thread([] ServerThread = Utils::Thread::CreateNamedThread("Mongoose", []
{ {
while (!Terminate) while (!Terminate)
{ {

View File

@ -156,7 +156,7 @@ namespace Components
Scheduler::Scheduler() Scheduler::Scheduler()
{ {
Thread = Utils::Thread::createNamedThread("Async Scheduler", [] Thread = Utils::Thread::CreateNamedThread("Async Scheduler", []
{ {
while (!Kill) while (!Kill)
{ {

View File

@ -154,6 +154,9 @@ namespace Components
// dvar setting function, unknown stuff related to server thread sync // dvar setting function, unknown stuff related to server thread sync
Utils::Hook::Set<std::uint8_t>(0x647781, 0xEB); Utils::Hook::Set<std::uint8_t>(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(0x627695, 0x627040, HOOK_CALL).install()->quick();
Utils::Hook(0x43D1C7, PacketEventStub, HOOK_JUMP).install()->quick(); Utils::Hook(0x43D1C7, PacketEventStub, HOOK_JUMP).install()->quick();
Utils::Hook(0x6272E3, FrameEpilogueStub, HOOK_JUMP).install()->quick(); Utils::Hook(0x6272E3, FrameEpilogueStub, HOOK_JUMP).install()->quick();

View File

@ -10307,6 +10307,32 @@ namespace Game
GfxLight sceneLights[253]; 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 #pragma endregion
#ifndef IDA #ifndef IDA

View File

@ -1,5 +1,8 @@
#pragma once #pragma once
template <class Type, std::size_t n>
constexpr auto ARRAY_COUNT(Type(&)[n]) { return n; }
namespace Utils::String namespace Utils::String
{ {
template <std::size_t Buffers, std::size_t MinBufferSize> template <std::size_t Buffers, std::size_t MinBufferSize>
@ -13,7 +16,7 @@ namespace Utils::String
[[nodiscard]] const char* get(const char* format, va_list ap) [[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]; auto entry = &this->stringPool[this->currentBuffer];
if (!entry->size || !entry->buffer) if (!entry->size || !entry->buffer)

View File

@ -2,7 +2,7 @@
namespace Utils::Thread 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"); const Library kernel32("kernel32.dll");
if (!kernel32) if (!kernel32)
@ -19,7 +19,7 @@ namespace Utils::Thread
return SUCCEEDED(setDescription(t, String::Convert(name).data())); 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); auto* const t = OpenThread(THREAD_SET_LIMITED_INFORMATION, FALSE, id);
if (!t) return false; if (!t) return false;
@ -29,20 +29,20 @@ namespace Utils::Thread
CloseHandle(t); 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<DWORD> getThreadIds() std::vector<DWORD> GetThreadIds()
{ {
auto* const h = CreateToolhelp32Snapshot(TH32CS_SNAPTHREAD, GetCurrentProcessId()); auto* const h = CreateToolhelp32Snapshot(TH32CS_SNAPTHREAD, GetCurrentProcessId());
if (h == INVALID_HANDLE_VALUE) if (h == INVALID_HANDLE_VALUE)
@ -79,9 +79,9 @@ namespace Utils::Thread
return ids; return ids;
} }
void forEachThread(const std::function<void(HANDLE)>& callback) void ForEachThread(const std::function<void(HANDLE)>& callback)
{ {
const auto ids = getThreadIds(); const auto ids = GetThreadIds();
for (const auto& id : ids) 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()) 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()) if (GetThreadId(thread) != GetCurrentThreadId())
{ {

View File

@ -2,22 +2,22 @@
namespace Utils::Thread namespace Utils::Thread
{ {
bool setName(HANDLE t, const std::string& name); bool SetName(HANDLE t, const std::string& name);
bool setName(DWORD id, const std::string& name); bool SetName(DWORD id, const std::string& name);
bool setName(std::thread& t, const std::string& name); bool SetName(std::thread& t, const std::string& name);
bool setName(const std::string& name); bool SetName(const std::string& name);
template <typename ...Args> template <typename ...Args>
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>(args)...); auto t = std::thread(std::forward<Args>(args)...);
setName(t, name); SetName(t, name);
return t; return t;
} }
std::vector<DWORD> getThreadIds(); std::vector<DWORD> GetThreadIds();
void forEachThread(const std::function<void(HANDLE)>& callback); void ForEachThread(const std::function<void(HANDLE)>& callback);
void suspendOtherThreads(); void SuspendOtherThreads();
void resumeOtherThreads(); void ResumeOtherThreads();
} }