mirror of
https://github.com/BeamMP/BeamMP-Launcher.git
synced 2026-04-06 07:46:18 +00:00
Basic foundation done
This commit is contained in:
@@ -6,25 +6,67 @@
|
||||
#define WIN32_LEAN_AND_MEAN
|
||||
#include "Launcher.h"
|
||||
#include "Logger.h"
|
||||
#include "Memory.h"
|
||||
#include "BeamNG.h"
|
||||
#include <windows.h>
|
||||
#include <shellapi.h>
|
||||
|
||||
Launcher::Launcher(int argc, char* argv[]) : CurrentPath(std::filesystem::path(argv[0])), DiscordMessage("Just launched") {
|
||||
Launcher::StaticAbort(this);
|
||||
Log::Init();
|
||||
WindowsInit();
|
||||
LOG(INFO) << "Starting Launcher V" << FullVersion;
|
||||
UpdateCheck();
|
||||
}
|
||||
|
||||
Launcher::~Launcher() {
|
||||
Shutdown = true;
|
||||
void Launcher::Abort() {
|
||||
Shutdown.store(true);
|
||||
if(DiscordRPC.joinable()) {
|
||||
DiscordRPC.join();
|
||||
}
|
||||
if(!MPUserPath.empty()) {
|
||||
ResetMods();
|
||||
}
|
||||
if(GamePID != 0) {
|
||||
auto Handle = OpenProcess(PROCESS_TERMINATE, false, DWORD(GamePID));
|
||||
TerminateProcess(Handle, 0);
|
||||
CloseHandle(Handle);
|
||||
}
|
||||
}
|
||||
|
||||
Launcher::~Launcher() {
|
||||
Abort();
|
||||
}
|
||||
|
||||
BOOL WINAPI CtrlHandler(DWORD Flag) {
|
||||
if((Flag >= 0 && Flag < 3) || (Flag > 4 && Flag < 7)) {
|
||||
Launcher::StaticAbort();
|
||||
return 1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
void Launcher::StaticAbort(Launcher* Instance) {
|
||||
static Launcher* Address;
|
||||
if(Instance) {
|
||||
Address = Instance;
|
||||
return;
|
||||
}
|
||||
Address->Abort();
|
||||
}
|
||||
|
||||
void Launcher::WindowsInit() {
|
||||
system("cls");
|
||||
SetConsoleTitleA(("BeamMP Launcher v" + FullVersion).c_str());
|
||||
if(!SetConsoleCtrlHandler(CtrlHandler, TRUE)) {
|
||||
LOG(WARNING) << "Failed to set CtrlHandler";
|
||||
}
|
||||
}
|
||||
|
||||
void Launcher::LaunchGame() {
|
||||
if(BeamNG::GetProcessID() != 0) {
|
||||
LOG(FATAL) << "Game is already running, please close it and try again!";
|
||||
throw ShutdownException("Fatal Error");
|
||||
}
|
||||
VersionParser GameVersion(BeamVersion);
|
||||
if(GameVersion.data[0] > SupportedVersion.data[0]) {
|
||||
LOG(FATAL) << "BeamNG V" << BeamVersion << " not yet supported, please wait until we update BeamMP!";
|
||||
@@ -43,20 +85,22 @@ void Launcher::LaunchGame() {
|
||||
}
|
||||
|
||||
void Launcher::WaitForGame() {
|
||||
LOG(INFO) << "Waiting for game launch";
|
||||
LOG(INFO) << "Waiting for the game, please start BeamNG manually in case of steam issues";
|
||||
do{
|
||||
GamePID = Memory::GetProcessID("BeamNG.drive.x64.exe");
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(500));
|
||||
}while(GamePID == 0 && !Shutdown);
|
||||
GamePID = BeamNG::GetProcessID();
|
||||
std::this_thread::sleep_for(std::chrono::seconds(2));
|
||||
}while(GamePID == 0 && !Shutdown.load());
|
||||
if(Shutdown.load())return;
|
||||
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());
|
||||
}
|
||||
LOG(INFO) << "Game found! PID " << GamePID;
|
||||
//TODO: Inject then start IPC
|
||||
while(!Shutdown.load() && BeamNG::GetProcessID() != 0) {
|
||||
std::this_thread::sleep_for(std::chrono::seconds(2));
|
||||
}
|
||||
LOG(INFO) << "Game process was lost";
|
||||
}
|
||||
|
||||
std::string QueryValue(HKEY& hKey, const char* Name) {
|
||||
@@ -95,6 +139,9 @@ void Launcher::QueryRegistry() {
|
||||
VersionParser GameVer(BeamVersion);
|
||||
BeamUserPath += GameVer.split[0] + '.' + GameVer.split[1] + '\\';
|
||||
}
|
||||
if(!BeamUserPath.empty()) {
|
||||
MPUserPath = BeamUserPath + "mods\\multiplayer";
|
||||
}
|
||||
if(!BeamRoot.empty() && !BeamVersion.empty() && !BeamUserPath.empty())return;
|
||||
}
|
||||
LOG(FATAL) << "Please launch the game at least once, failed to read registry key Software\\BeamNG\\BeamNG.drive";
|
||||
|
||||
Reference in New Issue
Block a user