1
0
Fork 0
mirror of https://github.com/Detanup01/gbe_fork.git synced 2025-08-04 06:35:32 +02:00

fix debug condition + add debug messages

This commit is contained in:
a 2024-12-10 21:10:51 +02:00
parent e02cc9b366
commit 5b6f5b749c

View file

@ -8,6 +8,9 @@
#include <chrono> #include <chrono>
#include <thread> #include <thread>
#if defined(DEBUG) || defined(_DEBUG)
#define STUB_EXTRA_DEBUG
#endif
static std::mutex dll_unload_mtx{}; static std::mutex dll_unload_mtx{};
static std::condition_variable dll_unload_cv{}; static std::condition_variable dll_unload_cv{};
@ -29,7 +32,7 @@ static void send_unload_signal()
DWORD WINAPI self_unload(LPVOID lpParameter) DWORD WINAPI self_unload(LPVOID lpParameter)
{ {
constexpr const auto UNLOAD_TIMEOUT = constexpr const auto UNLOAD_TIMEOUT =
#ifdef _DEBUG #ifdef STUB_EXTRA_DEBUG
std::chrono::minutes(5) std::chrono::minutes(5)
#else #else
std::chrono::seconds(5) std::chrono::seconds(5)
@ -37,8 +40,21 @@ DWORD WINAPI self_unload(LPVOID lpParameter)
; ;
{ {
#ifdef STUB_EXTRA_DEBUG
auto t1 = std::chrono::high_resolution_clock::now();
#endif
std::unique_lock lock(dll_unload_mtx); std::unique_lock lock(dll_unload_mtx);
dll_unload_cv.wait_for(lock, UNLOAD_TIMEOUT, [](){ return unload_dll; }); dll_unload_cv.wait_for(lock, UNLOAD_TIMEOUT, []{ return unload_dll; });
#ifdef STUB_EXTRA_DEBUG
if (!unload_dll) { // flag was not raised, means we timed out
auto t2 = std::chrono::high_resolution_clock::now();
auto dd = std::chrono::duration_cast<std::chrono::seconds>(t2 - t1);
std::string msg = "Unloading after " + std::to_string(dd.count()) + " seconds, due to timeout";
MessageBoxA(nullptr, msg.c_str(), "Self-unload thread", MB_OK | MB_ICONERROR);
}
#endif
} }
unload_thread_handle = INVALID_HANDLE_VALUE; unload_thread_handle = INVALID_HANDLE_VALUE;
FreeLibraryAndExitThread(my_hModule, 0); FreeLibraryAndExitThread(my_hModule, 0);
@ -52,19 +68,27 @@ BOOL APIENTRY DllMain(
switch (reason) switch (reason)
{ {
case DLL_PROCESS_ATTACH: case DLL_PROCESS_ATTACH:
stubdrm::set_cleanup_cb(send_unload_signal);
my_hModule = hModule;
if (!stubdrm::patch()) { if (!stubdrm::patch()) {
#ifdef STUB_EXTRA_DEBUG
MessageBoxA(nullptr, "Failed to detect .bind", "Main", MB_OK | MB_ICONERROR);
#endif
// https://learn.microsoft.com/en-us/windows/win32/dlls/dllmain // https://learn.microsoft.com/en-us/windows/win32/dlls/dllmain
// "The system immediately calls your entry-point function with DLL_PROCESS_DETACH and unloads the DLL" // "The system immediately calls your entry-point function with DLL_PROCESS_DETACH and unloads the DLL"
unload_dll = true; unload_dll = true;
return FALSE; return FALSE;
} }
my_hModule = hModule;
stubdrm::set_cleanup_cb(send_unload_signal);
unload_thread_handle = CreateThread(nullptr, 0, self_unload, nullptr, 0, nullptr); unload_thread_handle = CreateThread(nullptr, 0, self_unload, nullptr, 0, nullptr);
break; break;
case DLL_PROCESS_DETACH: case DLL_PROCESS_DETACH:
if (!unload_dll) { // not unloaded yet, just an early exit, or thread timed out if (!unload_dll) { // not unloaded yet, just an early exit, or thread timed out
#ifdef STUB_EXTRA_DEBUG
MessageBoxA(nullptr, "Unclean exit", "Main", MB_OK | MB_ICONERROR);
#endif
stubdrm::restore(); stubdrm::restore();
if (unload_thread_handle != INVALID_HANDLE_VALUE && unload_thread_handle != NULL) { if (unload_thread_handle != INVALID_HANDLE_VALUE && unload_thread_handle != NULL) {
TerminateThread(unload_thread_handle, 0); TerminateThread(unload_thread_handle, 0);