diff --git a/CMakeLists.txt b/CMakeLists.txt index 98be5c4..7e38fea 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -41,16 +41,17 @@ add_executable(${PROJECT_NAME} src/Network/Http.cpp include/Http.h src/Network/Login.cpp src/Network/Update.cpp src/Discord.cpp src/Config.cpp + src/Memory/Memory.cpp include/Memory.h ) if (WIN32) target_link_libraries(${PROJECT_NAME} PRIVATE - ZLIB::ZLIB discord-rpc OpenSSL::SSL OpenSSL::Crypto ws2_32 wx::net wx::core wx::base) + ZLIB::ZLIB discord-rpc OpenSSL::SSL OpenSSL::Crypto ws2_32 wx::net wx::core wx::base Dbghelp) else(WIN32) #MINGW add_definitions("-D_WIN32_WINNT=0x0600") set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Os -s --static") - target_link_libraries(${PROJECT_NAME} discord-rpc ssl crypto ws2_32 ssp crypt32 z) + target_link_libraries(${PROJECT_NAME} discord-rpc ssl crypto ws2_32 ssp crypt32 z Dbghelp) endif(WIN32) add_definitions(-DELPP_NO_DEFAULT_LOG_FILE) target_include_directories(${PROJECT_NAME} PRIVATE "include") diff --git a/include/Launcher.h b/include/Launcher.h index 902e36e..44216d5 100644 --- a/include/Launcher.h +++ b/include/Launcher.h @@ -26,6 +26,7 @@ public: //available functions std::string Login(const std::string& fields); void RunDiscordRPC(); void QueryRegistry(); + void WaitForGame(); void LoadConfig(); void LaunchGame(); void CheckKey(); @@ -41,6 +42,7 @@ private: //functions void UpdateCheck(); void Relaunch(); private: //variables + size_t GamePID{0}; bool EnableUI = true; bool Shutdown = false; bool LoginAuth = false; diff --git a/include/Memory.h b/include/Memory.h new file mode 100644 index 0000000..bee4d59 --- /dev/null +++ b/include/Memory.h @@ -0,0 +1,12 @@ +/// +/// Created by Anonymous275 on 6/17/21 +/// Copyright (c) 2021-present Anonymous275 read the LICENSE file for more info. +/// + +#include + +class Memory { +public: + static size_t GetProcessID(const char* PName); + static size_t GetModuleBase(const char* Name); +}; \ No newline at end of file diff --git a/src/Launcher.cpp b/src/Launcher.cpp index 2306ab0..5f92f03 100644 --- a/src/Launcher.cpp +++ b/src/Launcher.cpp @@ -6,6 +6,7 @@ #define WIN32_LEAN_AND_MEAN #include "Launcher.h" #include "Logger.h" +#include "Memory.h" #include #include @@ -41,6 +42,20 @@ void Launcher::LaunchGame() { //ShowWindow(GetConsoleWindow(), HIDE_WINDOW); } +void Launcher::WaitForGame() { + LOG(INFO) << "Waiting for game launch"; + int Timeout = 0; + do{ + Timeout++; + GamePID = Memory::GetProcessID("BeamNG.drive.x64.exe"); + std::this_thread::sleep_for(std::chrono::milliseconds(200)); + }while(GamePID == 0 && !Shutdown && Timeout < 600); + if(GamePID == 0) { + LOG(FATAL) << "Game process not found! aborting"; + throw ShutdownException("Fatal Error"); + }else LOG(INFO) << "Game found! PID " << GamePID; +} + void Launcher::WindowsInit() { system("cls"); SetConsoleTitleA(("BeamMP Launcher v" + FullVersion).c_str()); diff --git a/src/Memory/Memory.cpp b/src/Memory/Memory.cpp new file mode 100644 index 0000000..d61c1b6 --- /dev/null +++ b/src/Memory/Memory.cpp @@ -0,0 +1,35 @@ +/// +/// Created by Anonymous275 on 6/17/21 +/// Copyright (c) 2021-present Anonymous275 read the LICENSE file for more info. +/// + +#define WIN32_LEAN_AND_MEAN +#include +#include "Memory.h" +#undef UNICODE +#include +#include + +size_t Memory::GetProcessID(const char* PName) { + SetLastError(0); + PROCESSENTRY32 pe32; + pe32.dwSize = sizeof(PROCESSENTRY32); + HANDLE Snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0); + + if(Process32First(Snapshot, &pe32)) { + do{ + if(std::string(PName) == pe32.szExeFile)break; + }while(Process32Next(Snapshot, &pe32)); + } + + if(Snapshot != INVALID_HANDLE_VALUE) { + CloseHandle(Snapshot); + } + + if(GetLastError() != 0)return 0; + return pe32.th32ProcessID; +} + +size_t Memory::GetModuleBase(const char* Name) { + return (size_t)GetModuleHandleA(Name); +} diff --git a/src/main.cpp b/src/main.cpp index 16a20db..32ae3c7 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -13,13 +13,14 @@ int main(int argc, char* argv[]) { launcher.LoadConfig(); launcher.CheckKey(); launcher.QueryRegistry(); - launcher.LaunchGame(); - //launcher.WaitForGame(); //UI call + //download mod + launcher.LaunchGame(); + launcher.WaitForGame(); } catch (const ShutdownException& e) { - LOG(INFO) << "Launcher shutting down, reason: " << e.what(); + LOG(INFO) << "Launcher shutting down with reason: " << e.what(); } catch (const std::exception& e) { LOG(FATAL) << e.what(); }