added blocking WaitForGame using PID

This commit is contained in:
Anonymous275
2022-01-19 01:56:48 +02:00
parent 48e6b21eda
commit 046695d37d
6 changed files with 71 additions and 5 deletions

View File

@@ -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")

View File

@@ -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;

12
include/Memory.h Normal file
View File

@@ -0,0 +1,12 @@
///
/// Created by Anonymous275 on 6/17/21
/// Copyright (c) 2021-present Anonymous275 read the LICENSE file for more info.
///
#include <cstdint>
class Memory {
public:
static size_t GetProcessID(const char* PName);
static size_t GetModuleBase(const char* Name);
};

View File

@@ -6,6 +6,7 @@
#define WIN32_LEAN_AND_MEAN
#include "Launcher.h"
#include "Logger.h"
#include "Memory.h"
#include <windows.h>
#include <shellapi.h>
@@ -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());

35
src/Memory/Memory.cpp Normal file
View File

@@ -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 <string>
#include "Memory.h"
#undef UNICODE
#include <windows.h>
#include <tlhelp32.h>
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);
}

View File

@@ -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();
}