mirror of
https://github.com/BeamMP/BeamMP-Launcher.git
synced 2026-04-13 11:16:18 +00:00
Reformat to clang-format
This commit is contained in:
@@ -4,23 +4,25 @@
|
||||
///
|
||||
|
||||
#define WIN32_LEAN_AND_MEAN
|
||||
#include "Memory/Memory.h"
|
||||
#include "Launcher.h"
|
||||
#include "Logger.h"
|
||||
#include <csignal>
|
||||
#include "HttpAPI.h"
|
||||
#include <windows.h>
|
||||
#include <shellapi.h>
|
||||
#include "Logger.h"
|
||||
#include "Memory/Memory.h"
|
||||
#include <ShlObj.h>
|
||||
#include <comutil.h>
|
||||
#include <csignal>
|
||||
#include <mutex>
|
||||
#include <shellapi.h>
|
||||
#include <windows.h>
|
||||
|
||||
LONG WINAPI CrashHandler(EXCEPTION_POINTERS* p) {
|
||||
LOG(ERROR) << "CAUGHT EXCEPTION! Code 0x" << std::hex << std::uppercase << p->ExceptionRecord->ExceptionCode;
|
||||
return EXCEPTION_EXECUTE_HANDLER;
|
||||
}
|
||||
|
||||
Launcher::Launcher(int argc, char* argv[]) : CurrentPath(std::filesystem::path(argv[0])), DiscordMessage("Just launched") {
|
||||
Launcher::Launcher(int argc, char* argv[])
|
||||
: CurrentPath(std::filesystem::path(argv[0]))
|
||||
, DiscordMessage("Just launched") {
|
||||
Launcher::StaticAbort(this);
|
||||
DiscordTime = std::time(nullptr);
|
||||
Log::Init();
|
||||
@@ -33,16 +35,16 @@ Launcher::Launcher(int argc, char* argv[]) : CurrentPath(std::filesystem::path(a
|
||||
void Launcher::Abort() {
|
||||
Shutdown.store(true);
|
||||
ServerHandler.Close();
|
||||
if(DiscordRPC.joinable()) {
|
||||
if (DiscordRPC.joinable()) {
|
||||
DiscordRPC.join();
|
||||
}
|
||||
if(IPCSystem.joinable()) {
|
||||
if (IPCSystem.joinable()) {
|
||||
IPCSystem.join();
|
||||
}
|
||||
if(!MPUserPath.empty()) {
|
||||
if (!MPUserPath.empty()) {
|
||||
ResetMods();
|
||||
}
|
||||
if(GamePID != 0) {
|
||||
if (GamePID != 0) {
|
||||
auto Handle = OpenProcess(PROCESS_TERMINATE, false, DWORD(GamePID));
|
||||
TerminateProcess(Handle, 0);
|
||||
CloseHandle(Handle);
|
||||
@@ -50,25 +52,25 @@ void Launcher::Abort() {
|
||||
}
|
||||
|
||||
Launcher::~Launcher() {
|
||||
if(!Shutdown.load()) {
|
||||
if (!Shutdown.load()) {
|
||||
Abort();
|
||||
}
|
||||
}
|
||||
|
||||
void ShutdownHandler(int sig) {
|
||||
Launcher::StaticAbort();
|
||||
while(HTTP::isDownload) {
|
||||
while (HTTP::isDownload) {
|
||||
std::this_thread::sleep_for(std::chrono::seconds(1));
|
||||
}
|
||||
LOG(INFO) << "Got termination signal (" << sig << ")";
|
||||
while(!Launcher::getExit()) {
|
||||
while (!Launcher::getExit()) {
|
||||
std::this_thread::sleep_for(std::chrono::seconds(1));
|
||||
}
|
||||
}
|
||||
|
||||
void Launcher::StaticAbort(Launcher* Instance) {
|
||||
static Launcher* Address;
|
||||
if(Instance) {
|
||||
if (Instance) {
|
||||
Address = Instance;
|
||||
return;
|
||||
}
|
||||
@@ -86,21 +88,21 @@ void Launcher::WindowsInit() {
|
||||
|
||||
void Launcher::LaunchGame() {
|
||||
VersionParser GameVersion(BeamVersion);
|
||||
if(GameVersion.data[1] > SupportedVersion.data[1]) {
|
||||
if (GameVersion.data[1] > SupportedVersion.data[1]) {
|
||||
LOG(FATAL) << "BeamNG V" << BeamVersion << " not yet supported, please wait until we update BeamMP!";
|
||||
throw ShutdownException("Fatal Error");
|
||||
} else if(GameVersion.data[1] < SupportedVersion.data[1]) {
|
||||
} else if (GameVersion.data[1] < SupportedVersion.data[1]) {
|
||||
LOG(FATAL) << "BeamNG V" << BeamVersion << " not supported, please update and launch the new update!";
|
||||
throw ShutdownException("Fatal Error");
|
||||
} else if(GameVersion > SupportedVersion) {
|
||||
} else if (GameVersion > SupportedVersion) {
|
||||
LOG(WARNING) << "BeamNG V" << BeamVersion << " is slightly newer than recommended, this might cause issues!";
|
||||
} else if(GameVersion < SupportedVersion) {
|
||||
} else if (GameVersion < SupportedVersion) {
|
||||
LOG(WARNING) << "BeamNG V" << BeamVersion << " is slightly older than recommended, this might cause issues!";
|
||||
}
|
||||
if(Memory::GetBeamNGPID({}) == 0) {
|
||||
if (Memory::GetBeamNGPID({}) == 0) {
|
||||
LOG(INFO) << "Launching BeamNG from steam";
|
||||
ShellExecuteA(nullptr, nullptr, "steam://rungameid/284160", nullptr, nullptr, SW_SHOWNORMAL);
|
||||
//ShowWindow(GetConsoleWindow(), HIDE_WINDOW);
|
||||
// ShowWindow(GetConsoleWindow(), HIDE_WINDOW);
|
||||
}
|
||||
LOG(INFO) << "Waiting for a game process, please start BeamNG manually in case of steam issues";
|
||||
}
|
||||
@@ -109,16 +111,17 @@ void Launcher::WaitForGame() {
|
||||
std::set<uint32_t> BlackList;
|
||||
do {
|
||||
auto PID = Memory::GetBeamNGPID(BlackList);
|
||||
if(PID != 0 && IPC::mem_used(PID)) {
|
||||
if (PID != 0 && IPC::mem_used(PID)) {
|
||||
BlackList.emplace(PID);
|
||||
} else {
|
||||
GamePID = PID;
|
||||
}
|
||||
std::this_thread::sleep_for(std::chrono::seconds(1));
|
||||
} while(GamePID == 0 && !Shutdown.load());
|
||||
if(Shutdown.load())return;
|
||||
} while (GamePID == 0 && !Shutdown.load());
|
||||
if (Shutdown.load())
|
||||
return;
|
||||
|
||||
if(GamePID == 0) {
|
||||
if (GamePID == 0) {
|
||||
LOG(FATAL) << "Game process not found! aborting";
|
||||
throw ShutdownException("Fatal Error");
|
||||
}
|
||||
@@ -126,12 +129,12 @@ void Launcher::WaitForGame() {
|
||||
LOG(INFO) << "Game found! PID " << GamePID;
|
||||
|
||||
IPCToGame = std::make_unique<IPC>(GamePID, 0x1900000);
|
||||
IPCFromGame = std::make_unique<IPC>(GamePID+1, 0x1900000);
|
||||
IPCFromGame = std::make_unique<IPC>(GamePID + 1, 0x1900000);
|
||||
|
||||
IPCSystem = std::thread(&Launcher::ListenIPC, this);
|
||||
Memory::Inject(GamePID);
|
||||
setDiscordMessage("In menus");
|
||||
while(!Shutdown.load() && Memory::GetBeamNGPID(BlackList) != 0) {
|
||||
while (!Shutdown.load() && Memory::GetBeamNGPID(BlackList) != 0) {
|
||||
std::this_thread::sleep_for(std::chrono::seconds(2));
|
||||
}
|
||||
LOG(INFO) << "Game process was lost";
|
||||
@@ -139,11 +142,11 @@ void Launcher::WaitForGame() {
|
||||
}
|
||||
|
||||
void Launcher::ListenIPC() {
|
||||
while(!Shutdown.load()) {
|
||||
while (!Shutdown.load()) {
|
||||
IPCFromGame->receive();
|
||||
if(!IPCFromGame->receive_timed_out()) {
|
||||
if (!IPCFromGame->receive_timed_out()) {
|
||||
auto& MSG = IPCFromGame->msg();
|
||||
if(MSG[0] == 'C') {
|
||||
if (MSG[0] == 'C') {
|
||||
HandleIPC(IPCFromGame->msg().substr(1));
|
||||
} else {
|
||||
ServerHandler.ServerSend(IPCFromGame->msg().substr(1), false);
|
||||
@@ -153,12 +156,14 @@ void Launcher::ListenIPC() {
|
||||
}
|
||||
}
|
||||
|
||||
void Launcher::SendIPC(const std::string& Data, bool core) {
|
||||
void Launcher::SendIPC(const std::string& Data, bool core) {
|
||||
static std::mutex Lock;
|
||||
std::scoped_lock Guard(Lock);
|
||||
if(core)IPCToGame->send("C" + Data);
|
||||
else IPCToGame->send("G" + Data);
|
||||
if(IPCToGame->send_timed_out()) {
|
||||
if (core)
|
||||
IPCToGame->send("C" + Data);
|
||||
else
|
||||
IPCToGame->send("G" + Data);
|
||||
if (IPCToGame->send_timed_out()) {
|
||||
LOG(WARNING) << "Timed out while sending \"" << Data << "\"";
|
||||
}
|
||||
}
|
||||
@@ -166,8 +171,8 @@ void Launcher::SendIPC(const std::string& Data, bool core) {
|
||||
std::string QueryValue(HKEY& hKey, const char* Name) {
|
||||
DWORD keySize;
|
||||
BYTE buffer[16384];
|
||||
if(RegQueryValueExA(hKey, Name, nullptr, nullptr, buffer, &keySize) == ERROR_SUCCESS) {
|
||||
return {(char*)buffer, keySize-1};
|
||||
if (RegQueryValueExA(hKey, Name, nullptr, nullptr, buffer, &keySize) == ERROR_SUCCESS) {
|
||||
return { (char*)buffer, keySize - 1 };
|
||||
}
|
||||
return {};
|
||||
}
|
||||
@@ -185,7 +190,7 @@ std::string Launcher::GetLocalAppdata() {
|
||||
std::string Path((char*)bstrPath);
|
||||
CoTaskMemFree(folderPath);
|
||||
|
||||
if(!Path.empty()) {
|
||||
if (!Path.empty()) {
|
||||
Path += "\\BeamNG.drive\\";
|
||||
VersionParser GameVer(BeamVersion);
|
||||
Path += GameVer.split[0] + '.' + GameVer.split[1] + '\\';
|
||||
@@ -196,21 +201,22 @@ std::string Launcher::GetLocalAppdata() {
|
||||
void Launcher::QueryRegistry() {
|
||||
HKEY BeamNG;
|
||||
LONG RegRes = RegOpenKeyExA(HKEY_CURRENT_USER, R"(Software\BeamNG\BeamNG.drive)", 0, KEY_READ, &BeamNG);
|
||||
if(RegRes == ERROR_SUCCESS) {
|
||||
if (RegRes == ERROR_SUCCESS) {
|
||||
BeamRoot = QueryValue(BeamNG, "rootpath");
|
||||
BeamVersion = QueryValue(BeamNG, "version");
|
||||
BeamUserPath = QueryValue(BeamNG, "userpath_override");
|
||||
RegCloseKey(BeamNG);
|
||||
if(BeamUserPath.empty() && !BeamVersion.empty()) {
|
||||
if (BeamUserPath.empty() && !BeamVersion.empty()) {
|
||||
BeamUserPath = GetLocalAppdata();
|
||||
} else if(!BeamUserPath.empty() && !BeamVersion.empty()) {
|
||||
} else if (!BeamUserPath.empty() && !BeamVersion.empty()) {
|
||||
VersionParser GameVer(BeamVersion);
|
||||
BeamUserPath += GameVer.split[0] + '.' + GameVer.split[1] + '\\';
|
||||
}
|
||||
if(!BeamUserPath.empty()) {
|
||||
if (!BeamUserPath.empty()) {
|
||||
MPUserPath = BeamUserPath + "mods\\multiplayer";
|
||||
}
|
||||
if(!BeamRoot.empty() && !BeamVersion.empty() && !BeamUserPath.empty())return;
|
||||
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";
|
||||
throw ShutdownException("Fatal Error");
|
||||
@@ -219,13 +225,13 @@ void Launcher::QueryRegistry() {
|
||||
void Launcher::AdminRelaunch() {
|
||||
system("cls");
|
||||
ShellExecuteA(nullptr, "runas", CurrentPath.string().c_str(), nullptr, nullptr, SW_SHOWNORMAL);
|
||||
ShowWindow(GetConsoleWindow(),0);
|
||||
ShowWindow(GetConsoleWindow(), 0);
|
||||
throw ShutdownException("Relaunching");
|
||||
}
|
||||
|
||||
void Launcher::Relaunch() {
|
||||
ShellExecuteA(nullptr, "open", CurrentPath.string().c_str(), nullptr, nullptr, SW_SHOWNORMAL);
|
||||
ShowWindow(GetConsoleWindow(),0);
|
||||
ShowWindow(GetConsoleWindow(), 0);
|
||||
std::this_thread::sleep_for(std::chrono::seconds(1));
|
||||
throw ShutdownException("Relaunching");
|
||||
}
|
||||
@@ -258,6 +264,6 @@ const std::string& Launcher::getMPUserPath() {
|
||||
return MPUserPath;
|
||||
}
|
||||
|
||||
const std::string &Launcher::getPublicKey() {
|
||||
const std::string& Launcher::getPublicKey() {
|
||||
return PublicKey;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user