1212#include " window.hpp"
1313#include < SDL3/SDL_timer.h>
1414
15- std::atomic<bool > kRUNNING = true ;
15+ std::atomic_bool kRUNNING = true ;
16+ std::atomic_bool kGAME_RUNNING = false ;
1617
1718namespace {
1819 auto dll_path = std::make_unique<char []>(MAX_PATH);
@@ -36,19 +37,27 @@ void AtExit() {
3637 base::common::logging::Manager::Shutdown ();
3738}
3839
39- void DoFrame (const base::injector::Window& window) {
40- const HWND game_wnd = base::win32::GetHwnd (kSETTINGS .target_window_class , kSETTINGS .target_window_name ).value_or (nullptr );
40+ void GameRunningChecker () {
41+ while (kRUNNING ) {
42+ const HWND game_wnd = base::win32::GetHwnd (kSETTINGS .target_window_class , kSETTINGS .target_window_name ).value_or (nullptr );
43+ kGAME_RUNNING = !base::win32::GetPIDFromHWND (game_wnd).has_error ();
44+
45+ std::this_thread::sleep_for (std::chrono::seconds (1 ));
46+ }
47+ }
4148
49+ void DoFrame (const base::injector::Window& window) {
4250 window.PreFrame ();
4351
4452 {
4553 if (ImGui::Begin (" Injector window" , nullptr , ImGuiWindowFlags_NoCollapse | ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoResize)) {
4654 if (ImGui::BeginTabBar (" tabs" )) {
4755 if (ImGui::BeginTabItem (" Injector" )) {
48- ImGui::Text (!game_wnd ? " Game not running" : fmt::format ( " Game running: {} " , base::win32::GetPIDFromHWND (game_wnd). value_or ( 0 )). c_str () );
49- if (game_wnd ) {
56+ ImGui::Text (!kGAME_RUNNING ? " Game not running" : " Game running" );
57+ if (kGAME_RUNNING ) {
5058 if (ImGui::Button (" Inject" )) {
51- auto _ = std::async (std::launch::async, [game_wnd]() {
59+ auto _ = std::async (std::launch::async, []() {
60+ const HWND game_wnd = base::win32::GetHwnd (kSETTINGS .target_window_class , kSETTINGS .target_window_name ).value_or (nullptr );
5261 if (const auto res = base::injector::Inject (base::win32::GetPIDFromHWND (game_wnd).value_or (0 ), kSETTINGS .dll_path ); res.error ()) {
5362 LOG_ERROR (" Failed to inject: {}" , res.error ());
5463 MessageBoxA (nullptr , res.error ().GetResultMessage ().c_str (), " Error" , MB_OK | MB_ICONERROR);
@@ -120,6 +129,8 @@ std::int32_t APIENTRY WinMain(HINSTANCE, HINSTANCE, LPSTR, int) {
120129
121130 const Window window (kWINDOW_WIDTH , kWINDOW_HEIGHT );
122131
132+ std::thread game_running_check_thread (GameRunningChecker);
133+
123134 // init to initial values
124135 strncpy_s (dll_path.get (), MAX_PATH, kSETTINGS .dll_path .c_str (), _TRUNCATE);
125136 strncpy_s (target_window_name.get (), 255 , kSETTINGS .target_window_name .c_str (), _TRUNCATE);
@@ -143,6 +154,12 @@ std::int32_t APIENTRY WinMain(HINSTANCE, HINSTANCE, LPSTR, int) {
143154 prev_tick = cur_tick;
144155 DoFrame (window);
145156 }
157+
158+ std::this_thread::yield ();
159+ }
160+
161+ if (game_running_check_thread.joinable ()) {
162+ game_running_check_thread.join ();
146163 }
147164
148165 return 0 ;
0 commit comments