Change to canonical paths for executable paths

This commit is contained in:
Vali0004 2025-07-26 10:34:58 -04:00
parent b6b0e4ba3e
commit 7a4b24d616
3 changed files with 41 additions and 8 deletions

View File

@ -13,6 +13,7 @@
void InitLauncher(); void InitLauncher();
beammp_fs_string GetEP(const beammp_fs_char* P = nullptr); beammp_fs_string GetEP(const beammp_fs_char* P = nullptr);
std::filesystem::path GetBP(const beammp_fs_char* P = nullptr);
std::filesystem::path GetGamePath(); std::filesystem::path GetGamePath();
std::string GetVer(); std::string GetVer();
std::string GetPatch(); std::string GetPatch();

View File

@ -14,7 +14,10 @@
#if defined(_WIN32) #if defined(_WIN32)
#elif defined(__linux__) #elif defined(__linux__)
#include <unistd.h> #include <unistd.h>
#endif #elif defined (__APPLE__)
#include <unistd.h>
#include <libproc.h>
#endif // __APPLE__
#include "Http.h" #include "Http.h"
#include "Logger.h" #include "Logger.h"
#include "Network/network.hpp" #include "Network/network.hpp"
@ -94,6 +97,34 @@ beammp_fs_string GetEP(const beammp_fs_char* P) {
}(); }();
return Ret; return Ret;
} }
beammp_fs_string GetBP(const beammp_fs_char* P) {
fs::path fspath = {};
#if defined(_WIN32)
beammp_fs_char path[256];
GetModuleFileNameW(nullptr, path, sizeof(path));
fspath = path;
#elif defined(__linux__)
fspath = fs::canonical("/proc/self/exe");
#elif defined(__APPLE__)
pid_t pid = getpid();
char path[PROC_PIDPATHINFO_MAXSIZE];
// While this is fine for a raw executable,
// an application bundle is read-only and these files
// should instead be placed in Application Support.
proc_pidpath(pid, path, sizeof(path));
fspath = std::string(path);
#else
fspath = beammp_fs_string(P);
#endif
fspath = fs::weakly_canonical(fspath.string() + "/..");
#if defined(_WIN32)
return fspath.wstring();
#else
return fspath.string();
#endif
}
#if defined(_WIN32) #if defined(_WIN32)
void ReLaunch() { void ReLaunch() {
std::wstring Arg; std::wstring Arg;
@ -103,7 +134,7 @@ void ReLaunch() {
} }
info("Relaunch!"); info("Relaunch!");
system("cls"); system("cls");
ShellExecuteW(nullptr, L"runas", (GetEP() + GetEN()).c_str(), Arg.c_str(), nullptr, SW_SHOWNORMAL); ShellExecuteW(nullptr, L"runas", (GetBP() / GetEN()).c_str(), Arg.c_str(), nullptr, SW_SHOWNORMAL);
ShowWindow(GetConsoleWindow(), 0); ShowWindow(GetConsoleWindow(), 0);
std::this_thread::sleep_for(std::chrono::seconds(1)); std::this_thread::sleep_for(std::chrono::seconds(1));
exit(1); exit(1);
@ -114,7 +145,7 @@ void URelaunch() {
Arg += Utils::ToWString(options.argv[c - 1]); Arg += Utils::ToWString(options.argv[c - 1]);
Arg += L" "; Arg += L" ";
} }
ShellExecuteW(nullptr, L"open", (GetEP() + GetEN()).c_str(), Arg.c_str(), nullptr, SW_SHOWNORMAL); ShellExecuteW(nullptr, L"open", (GetBP() / GetEN()).c_str(), Arg.c_str(), nullptr, SW_SHOWNORMAL);
ShowWindow(GetConsoleWindow(), 0); ShowWindow(GetConsoleWindow(), 0);
std::this_thread::sleep_for(std::chrono::seconds(1)); std::this_thread::sleep_for(std::chrono::seconds(1));
exit(1); exit(1);
@ -128,7 +159,7 @@ void ReLaunch() {
} }
info("Relaunch!"); info("Relaunch!");
system("clear"); system("clear");
int ret = execv(options.executable_name.c_str(), const_cast<char**>(options.argv)); int ret = execv((GetBP() / GetEN()).c_str(), const_cast<char**>(options.argv));
if (ret < 0) { if (ret < 0) {
error(std::string("execv() failed with: ") + strerror(errno) + ". Failed to relaunch"); error(std::string("execv() failed with: ") + strerror(errno) + ". Failed to relaunch");
exit(1); exit(1);
@ -137,7 +168,7 @@ void ReLaunch() {
exit(1); exit(1);
} }
void URelaunch() { void URelaunch() {
int ret = execv(options.executable_name.c_str(), const_cast<char**>(options.argv)); int ret = execv((GetBP() / GetEN()).c_str(), const_cast<char**>(options.argv));
if (ret < 0) { if (ret < 0) {
error(std::string("execv() failed with: ") + strerror(errno) + ". Failed to relaunch"); error(std::string("execv() failed with: ") + strerror(errno) + ". Failed to relaunch");
exit(1); exit(1);
@ -169,7 +200,7 @@ void CheckForUpdates(const std::string& CV) {
"https://backend.beammp.com/version/launcher?branch=" + Branch + "&pk=" + PublicKey); "https://backend.beammp.com/version/launcher?branch=" + Branch + "&pk=" + PublicKey);
transform(LatestHash.begin(), LatestHash.end(), LatestHash.begin(), ::tolower); transform(LatestHash.begin(), LatestHash.end(), LatestHash.begin(), ::tolower);
beammp_fs_string EP(GetEP() + GetEN()), Back(GetEP() + beammp_wide("BeamMP-Launcher.back")); beammp_fs_string BP(GetBP() / GetEN()), Back(GetBP() / beammp_wide("BeamMP-Launcher.back"));
std::string FileHash = Utils::GetSha256HashReallyFastFile(EP); std::string FileHash = Utils::GetSha256HashReallyFastFile(EP);
@ -254,8 +285,8 @@ void InitLauncher() {
} }
#endif #endif
size_t DirCount(const std::filesystem::path& path) { size_t DirCount(const fs::path& path) {
return (size_t)std::distance(std::filesystem::directory_iterator { path }, std::filesystem::directory_iterator {}); return (size_t)std::distance(fs::directory_iterator { path }, fs::directory_iterator {});
} }
void CheckMP(const beammp_fs_string& Path) { void CheckMP(const beammp_fs_string& Path) {

View File

@ -39,6 +39,7 @@ int main(int argc, const char** argv) try {
curl_global_init(CURL_GLOBAL_ALL); curl_global_init(CURL_GLOBAL_ALL);
GetEP(Utils::ToWString(std::string(argv[0])).c_str()); GetEP(Utils::ToWString(std::string(argv[0])).c_str());
info("BP Path: " + GetBP(Utils::ToWString(std::string(argv[0])).c_str()));
InitLog(); InitLog();
ConfigInit(); ConfigInit();