mirror of
https://github.com/BeamMP/BeamMP-Launcher.git
synced 2026-04-07 16:26:02 +00:00
more cleanup
This commit is contained in:
@@ -1,74 +1,68 @@
|
||||
// Copyright (c) 2020 Anonymous275.
|
||||
// BeamMP Launcher code is not in the public domain and is not free software.
|
||||
// One must be granted explicit permission by the copyright holder in order to modify or distribute any part of the source or binaries.
|
||||
// Anything else is prohibited. Modified works may not be published and have be upstreamed to the official repository.
|
||||
///
|
||||
/// Created by Anonymous275 on 7/19/2020
|
||||
///
|
||||
#include "Security/Enc.h"
|
||||
|
||||
#include <Windows.h>
|
||||
#include "Startup.h"
|
||||
#include <ShlObj.h>
|
||||
#include "Logger.h"
|
||||
#include <iostream>
|
||||
#include <thread>
|
||||
|
||||
unsigned long GamePID = 0;
|
||||
std::string QueryKey(HKEY hKey,int ID);
|
||||
void DeleteKey(){
|
||||
std::string GetGamePath(){
|
||||
static std::string Path;
|
||||
if(!Path.empty())return Path;
|
||||
|
||||
HKEY hKey;
|
||||
LPCTSTR sk = Sec("Software\\BeamNG\\BeamNG.drive");
|
||||
RegOpenKeyEx(HKEY_CURRENT_USER, sk, 0, KEY_ALL_ACCESS, &hKey);
|
||||
RegDeleteValueA(hKey, Sec("userpath_override"));
|
||||
}
|
||||
std::string Write(const std::string&Path){
|
||||
HKEY hKey;
|
||||
LPCTSTR sk = Sec("Software\\BeamNG\\BeamNG.drive");
|
||||
LPCTSTR sk = "Software\\BeamNG\\BeamNG.drive";
|
||||
LONG openRes = RegOpenKeyEx(HKEY_CURRENT_USER, sk, 0, KEY_ALL_ACCESS, &hKey);
|
||||
if (openRes != ERROR_SUCCESS){
|
||||
fatal(Sec("Please launch the game at least once"));
|
||||
fatal("Please launch the game at least once");
|
||||
}
|
||||
std::string Query = QueryKey(hKey,4);
|
||||
LONG setRes = RegSetValueEx(hKey, Sec("userpath_override"), 0, REG_SZ, (LPBYTE)Path.c_str(), DWORD(Path.size()));
|
||||
if(setRes != ERROR_SUCCESS){
|
||||
fatal(Sec("Failed to launch the game")); //not fatal later
|
||||
Path = QueryKey(hKey,4);
|
||||
|
||||
if(Path.empty()){
|
||||
CoInitialize(nullptr);
|
||||
wchar_t * path = nullptr;
|
||||
SHGetKnownFolderPath(FOLDERID_Documents, KF_FLAG_SIMPLE_IDLIST, nullptr, (PWSTR *)(&path));
|
||||
CoTaskMemFree(path);
|
||||
std::wstring ws(path);
|
||||
std::string s(ws.begin(), ws.end());
|
||||
Path = s;
|
||||
Path += "\\BeamNG.drive\\";
|
||||
}
|
||||
RegCloseKey(hKey);
|
||||
return Query;
|
||||
|
||||
return Path;
|
||||
}
|
||||
void RollBack(const std::string&Val,int T){
|
||||
std::this_thread::sleep_for(std::chrono::seconds(T));
|
||||
if(!Val.empty()){
|
||||
if(Write(Val) == Val)DeleteKey();
|
||||
}else DeleteKey();
|
||||
}
|
||||
std::string Restore;
|
||||
void OnExit(){
|
||||
RollBack(Restore,0);
|
||||
}
|
||||
void StartGame(std::string Dir,std::string Current){
|
||||
Current = Current.substr(0,Current.find_last_of('\\')) + Sec("\\BeamNG\\");
|
||||
|
||||
void StartGame(std::string Dir){
|
||||
BOOL bSuccess = FALSE;
|
||||
PROCESS_INFORMATION pi;
|
||||
STARTUPINFO si = {0};
|
||||
si.cb = sizeof(si);
|
||||
std::string BaseDir = Dir + Sec("\\Bin64");
|
||||
Dir += Sec(R"(\Bin64\BeamNG.drive.x64.exe)");
|
||||
std::string BaseDir = Dir +"\\Bin64";
|
||||
Dir += R"(\Bin64\BeamNG.drive.x64.exe)";
|
||||
bSuccess = CreateProcessA(Dir.c_str(), nullptr, nullptr, nullptr, TRUE, 0, nullptr, BaseDir.c_str(), &si, &pi);
|
||||
if (bSuccess){
|
||||
info(Sec("Game Launched!"));
|
||||
info("Game Launched!");
|
||||
GamePID = pi.dwProcessId;
|
||||
Restore = Write(Current);
|
||||
atexit(OnExit);
|
||||
std::thread RB(RollBack,Restore,7);
|
||||
RB.detach();
|
||||
WaitForSingleObject(pi.hProcess, INFINITE);
|
||||
error(Sec("Game Closed! launcher closing soon"));
|
||||
RollBack(Restore,0);
|
||||
error("Game Closed! launcher closing soon");
|
||||
}else{
|
||||
error(Sec("Failed to Launch the game! launcher closing soon"));
|
||||
RollBack(Write(Current),0);
|
||||
error("Failed to Launch the game! launcher closing soon");
|
||||
}
|
||||
std::this_thread::sleep_for(std::chrono::seconds(5));
|
||||
exit(2);
|
||||
}
|
||||
void InitGame(const std::string& Dir,const std::string&Current){
|
||||
void InitGame(const std::string& Dir){
|
||||
if(!Dev){
|
||||
std::thread Game(StartGame, Dir, Current);
|
||||
std::thread Game(StartGame, Dir);
|
||||
Game.detach();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user