From 3810e0eb0bd01ab3f1100a221ed0d708c453b98c Mon Sep 17 00:00:00 2001 From: Sam39 Date: Tue, 9 Aug 2022 14:08:52 +0300 Subject: [PATCH] fix game hanging on launcher error --- include/Memory/BeamNG.h | 1 + src/Memory/BeamNG.cpp | 29 +++++++++++++++++++++++------ src/Memory/IPC.cpp | 1 + src/gui/Gui.cpp | 8 +++++--- 4 files changed, 30 insertions(+), 9 deletions(-) diff --git a/include/Memory/BeamNG.h b/include/Memory/BeamNG.h index 7a3835e..6e7bba3 100644 --- a/include/Memory/BeamNG.h +++ b/include/Memory/BeamNG.h @@ -27,4 +27,5 @@ class BeamNG { // static int GetTickCount_D(void* GEState, void* Param2, void* Param3, void* // Param4); static void IPCListener(); + static uint32_t IPCSender(void* LP); }; diff --git a/src/Memory/BeamNG.cpp b/src/Memory/BeamNG.cpp index 59931de..9b15de9 100644 --- a/src/Memory/BeamNG.cpp +++ b/src/Memory/BeamNG.cpp @@ -7,7 +7,7 @@ #include "Memory/Memory.h" #include "atomic_queue.h" -std::unique_ptr> Queue; +std::unique_ptr> RCVQueue, SendQueue; int BeamNG::lua_open_jit_D(lua_State* State) { Memory::Print("Got lua State"); @@ -17,7 +17,8 @@ int BeamNG::lua_open_jit_D(lua_State* State) { } void BeamNG::EntryPoint() { - Queue = std::make_unique>(); + RCVQueue = std::make_unique>(); + SendQueue = std::make_unique>(); uint32_t PID = Memory::GetPID(); auto status = MH_Initialize(); if (status != MH_OK) @@ -31,6 +32,8 @@ void BeamNG::EntryPoint() { OpenJITDetour->Enable(); IPCFromLauncher = std::make_unique(PID, 0x1900000); IPCToLauncher = std::make_unique(PID + 1, 0x1900000); + CreateThread(nullptr, 0, LPTHREAD_START_ROUTINE(IPCSender), nullptr, 0, + nullptr); IPCListener(); } @@ -60,7 +63,7 @@ int Game(lua_State* L) { int LuaPop(lua_State* L) { std::string MSG; - if (Queue->try_pop(MSG)) { + if (RCVQueue->try_pop(MSG)) { GELua::lua_push_fstring(L, "%s", MSG.c_str()); return 1; } @@ -78,7 +81,7 @@ void BeamNG::RegisterGEFunctions() { } void BeamNG::SendIPC(const std::string& Data) { - IPCToLauncher->send(Data); + if (SendQueue->size() < 800 || !RCVQueue->empty()) { SendQueue->push(Data); } } void BeamNG::IPCListener() { @@ -87,9 +90,23 @@ void BeamNG::IPCListener() { IPCFromLauncher->receive(); if (!IPCFromLauncher->receive_timed_out()) { TimeOuts = 0; - Queue->push(IPCFromLauncher->msg()); + RCVQueue->push(IPCFromLauncher->msg()); IPCFromLauncher->confirm_receive(); } else TimeOuts++; } - Memory::Print("IPC System shutting down"); + Memory::Print("IPC Listener System shutting down"); +} + +uint32_t BeamNG::IPCSender(void* LP) { + std::string result; + int TimeOuts = 0; + while (TimeOuts < 20) { + if (SendQueue->try_pop(result)) { + IPCToLauncher->send(result); + if (!IPCToLauncher->send_timed_out()) TimeOuts = 0; + else TimeOuts++; + } + } + Memory::Print("IPC Sender System shutting down"); + return 0; } diff --git a/src/Memory/IPC.cpp b/src/Memory/IPC.cpp index 85d6252..d1582b0 100644 --- a/src/Memory/IPC.cpp +++ b/src/Memory/IPC.cpp @@ -5,6 +5,7 @@ #define WIN32_LEAN_AND_MEAN #include "Memory/IPC.h" +#include "Memory/Memory.h" #include IPC::IPC(uint32_t ID, size_t Size) noexcept : Size_(Size) { diff --git a/src/gui/Gui.cpp b/src/gui/Gui.cpp index d4018e0..b171dbd 100644 --- a/src/gui/Gui.cpp +++ b/src/gui/Gui.cpp @@ -17,6 +17,7 @@ #include #include "Launcher.h" #include "Logger.h" +#include #endif /////////// Inherit App class /////////// @@ -293,7 +294,7 @@ MyAccountFrame::MyAccountFrame() : wxFrame(nullptr, wxID_ANY, "Account Manager", wxStaticBitmap *image; image = new wxStaticBitmap( this, wxID_ANY, wxBitmapBundle(wxImage("icons/BeamMP_black.png", wxBITMAP_TYPE_PNG).Scale(120,120, wxIMAGE_QUALITY_HIGH)), wxPoint(180,20), wxSize(120, 120)); - if (!isSignedIn()) { + if (isSignedIn()) { image->SetBitmap(wxBitmapBundle(wxImage("icons/default.png", wxBITMAP_TYPE_PNG).Scale(120,120, wxIMAGE_QUALITY_HIGH))); auto* txtName = new wxStaticText(this, wxID_ANY, wxT("Username: BeamMP"), wxPoint(180, 200)); @@ -430,16 +431,17 @@ void MyAccountFrame::OnClickLogout(wxCommandEvent& event WXUNUSED(event)) { /////////// OnClick Launch Event /////////// void MyMainFrame::OnClickLaunch(wxCommandEvent& event WXUNUSED(event)) { static bool FirstTime = true; -/* if (Launcher::EntryThread.joinable()) Launcher::EntryThread.join(); + if (Launcher::EntryThread.joinable()) Launcher::EntryThread.join(); Launcher::EntryThread = std::thread([&]() { entry(); + std::this_thread::sleep_for(std::chrono::seconds(2)); txtStatusResult->SetLabelText(wxT("Online")); txtStatusResult->SetForegroundColour("green"); btnLaunch->Enable(); }); txtStatusResult->SetLabelText(wxT("In-Game")); txtStatusResult->SetForegroundColour("purple"); - btnLaunch->Disable();*/ + btnLaunch->Disable(); if(FirstTime) { wxMessageBox("Please launch BeamNG.drive manually in case of Steam issues.", "Alert");