From 7c419e0afe9695b4ace2be26c4b43e1ad08e3711 Mon Sep 17 00:00:00 2001 From: Mr_Goldberg Date: Sun, 7 Aug 2022 23:11:21 -0400 Subject: [PATCH] Fix overlay crash in games that load then unload steam api dll. --- dll/steam_client.cpp | 5 +++++ overlay_experimental/Renderer_Detector.cpp | 23 ++++++++++++++++++++-- overlay_experimental/Renderer_Detector.h | 1 + overlay_experimental/steam_overlay.cpp | 11 +++++++++++ overlay_experimental/steam_overlay.h | 2 ++ 5 files changed, 40 insertions(+), 2 deletions(-) diff --git a/dll/steam_client.cpp b/dll/steam_client.cpp index b1ee2fef..35ea62ba 100644 --- a/dll/steam_client.cpp +++ b/dll/steam_client.cpp @@ -830,6 +830,11 @@ bool Steam_Client::BShutdownIfAllPipesClosed() } steam_controller->Shutdown(); +#ifdef EMU_OVERLAY + if(!settings_client->disable_overlay) + steam_overlay->UnSetupOverlay(); +#endif + if (joinable) { background_keepalive.join(); } diff --git a/overlay_experimental/Renderer_Detector.cpp b/overlay_experimental/Renderer_Detector.cpp index b914691e..251610cc 100644 --- a/overlay_experimental/Renderer_Detector.cpp +++ b/overlay_experimental/Renderer_Detector.cpp @@ -77,6 +77,10 @@ public: delete vulkan_hook; } + bool force_stop_detection; + std::condition_variable detect_renderer_thread_cv; + std::mutex destroy_render_thread_mutex; + private: Renderer_Detector(): dxgi_hooked(false), @@ -94,7 +98,8 @@ private: dx12_hook(nullptr), opengl_hook(nullptr), vulkan_hook(nullptr), - detection_done(false) + detection_done(false), + force_stop_detection(false) { std::wstring tmp(4096, L'\0'); tmp.resize(GetSystemDirectoryW(&tmp[0], tmp.size())); @@ -1064,6 +1069,9 @@ public: auto start_time = std::chrono::steady_clock::now(); do { + std::unique_lock lck(destroy_render_thread_mutex); + if (force_stop_detection) break; + for (auto const& library : libraries) { void* lib_handle = System::Library::GetLibraryHandle(library.first.c_str()); @@ -1074,7 +1082,10 @@ public: (this->*library.second)(name); } } - std::this_thread::sleep_for(std::chrono::milliseconds{ 100 }); + + detect_renderer_thread_cv.wait_for(lck, std::chrono::milliseconds(100)); + if (force_stop_detection) break; + } while (!detection_done && (timeout.count() == -1 || (std::chrono::steady_clock::now() - start_time) <= timeout)); { @@ -1415,3 +1426,11 @@ std::future detect_renderer(std::chrono::milliseconds timeout) { return std::async(std::launch::async, &Renderer_Detector::detect_renderer, Renderer_Detector::Inst(), timeout); } + +void stop_renderer_detector() +{ + Renderer_Detector::Inst()->destroy_render_thread_mutex.lock(); + Renderer_Detector::Inst()->force_stop_detection = true; + Renderer_Detector::Inst()->destroy_render_thread_mutex.unlock(); + Renderer_Detector::Inst()->detect_renderer_thread_cv.notify_all(); +} diff --git a/overlay_experimental/Renderer_Detector.h b/overlay_experimental/Renderer_Detector.h index f6972e1a..190db07c 100644 --- a/overlay_experimental/Renderer_Detector.h +++ b/overlay_experimental/Renderer_Detector.h @@ -28,3 +28,4 @@ #include "Renderer_Hook.h" std::future detect_renderer(std::chrono::milliseconds timeout = std::chrono::milliseconds{-1}); +void stop_renderer_detector(); diff --git a/overlay_experimental/steam_overlay.cpp b/overlay_experimental/steam_overlay.cpp index bac1e516..d94de610 100644 --- a/overlay_experimental/steam_overlay.cpp +++ b/overlay_experimental/steam_overlay.cpp @@ -200,6 +200,17 @@ void Steam_Overlay::SetupOverlay() } } + +void Steam_Overlay::UnSetupOverlay() +{ + stop_renderer_detector(); + if (!Ready() && future_renderer.valid()) { + if (future_renderer.wait_for(std::chrono::milliseconds{500}) == std::future_status::ready) { + future_renderer.get(); + } + } +} + void Steam_Overlay::HookReady(bool ready) { { diff --git a/overlay_experimental/steam_overlay.h b/overlay_experimental/steam_overlay.h index 8345b576..6840a0ef 100644 --- a/overlay_experimental/steam_overlay.h +++ b/overlay_experimental/steam_overlay.h @@ -161,6 +161,7 @@ public: void SetNotificationInset(int nHorizontalInset, int nVerticalInset); void SetupOverlay(); + void UnSetupOverlay(); void HookReady(bool ready); @@ -202,6 +203,7 @@ public: void SetNotificationInset(int nHorizontalInset, int nVerticalInset) {} void SetupOverlay() {} + void UnSetupOverlay() {} void HookReady(bool ready) {}