mirror of
https://github.com/BeamMP/BeamMP-Launcher.git
synced 2025-08-17 08:46:24 +00:00
Compare commits
No commits in common. "master" and "v2.5.1" have entirely different histories.
@ -11,7 +11,7 @@
|
||||
#include <string>
|
||||
class HTTP {
|
||||
public:
|
||||
static bool Download(const std::string& IP, const beammp_fs_string& Path, const std::string& Hash);
|
||||
static bool Download(const std::string& IP, const beammp_fs_string& Path);
|
||||
static std::string Post(const std::string& IP, const std::string& Fields);
|
||||
static std::string Get(const std::string& IP);
|
||||
static bool ProgressBar(size_t c, size_t t);
|
||||
|
@ -13,7 +13,6 @@
|
||||
|
||||
void InitLauncher();
|
||||
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::string GetVer();
|
||||
std::string GetPatch();
|
||||
|
@ -29,6 +29,7 @@
|
||||
#endif
|
||||
|
||||
namespace Utils {
|
||||
|
||||
inline std::vector<std::string> Split(const std::string& String, const std::string& delimiter) {
|
||||
std::vector<std::string> Val;
|
||||
size_t pos;
|
||||
@ -165,7 +166,7 @@ namespace Utils {
|
||||
}
|
||||
|
||||
#ifdef _WIN32
|
||||
inline std::wstring ToWString(const std::string& s) {
|
||||
inline std::wstring ToWString(const std::string& s) {
|
||||
if (s.empty()) return std::wstring();
|
||||
|
||||
int size_needed = MultiByteToWideChar(CP_UTF8, 0, s.c_str(), (int)s.size(), nullptr, 0);
|
||||
@ -184,7 +185,7 @@ namespace Utils {
|
||||
return s;
|
||||
}
|
||||
#endif
|
||||
inline std::string GetSha256HashReallyFastFile(const beammp_fs_string& filename) {
|
||||
inline std::string GetSha256HashReallyFast(const beammp_fs_string& filename) {
|
||||
try {
|
||||
EVP_MD_CTX* mdctx;
|
||||
const EVP_MD* md;
|
||||
@ -236,51 +237,7 @@ namespace Utils {
|
||||
for (size_t i = 0; i < sha256_len; i++) {
|
||||
char buf[3];
|
||||
sprintf(buf, "%02x", sha256_value[i]);
|
||||
buf[2] = '\0';
|
||||
result += buf;
|
||||
}
|
||||
return result;
|
||||
} catch (const std::exception& e) {
|
||||
error(beammp_wide("Sha256 hashing of '") + filename + beammp_wide("' failed: ") + ToWString(e.what()));
|
||||
return "";
|
||||
}
|
||||
}
|
||||
inline std::string GetSha256HashReallyFast(const std::string& text, const beammp_fs_string& filename) {
|
||||
try {
|
||||
EVP_MD_CTX* mdctx;
|
||||
const EVP_MD* md;
|
||||
uint8_t sha256_value[EVP_MAX_MD_SIZE];
|
||||
md = EVP_sha256();
|
||||
if (md == nullptr) {
|
||||
throw std::runtime_error("EVP_sha256() failed");
|
||||
}
|
||||
|
||||
mdctx = EVP_MD_CTX_new();
|
||||
if (mdctx == nullptr) {
|
||||
throw std::runtime_error("EVP_MD_CTX_new() failed");
|
||||
}
|
||||
if (!EVP_DigestInit_ex2(mdctx, md, NULL)) {
|
||||
EVP_MD_CTX_free(mdctx);
|
||||
throw std::runtime_error("EVP_DigestInit_ex2() failed");
|
||||
}
|
||||
|
||||
if (!EVP_DigestUpdate(mdctx, text.data(), text.size())) {
|
||||
EVP_MD_CTX_free(mdctx);
|
||||
throw std::runtime_error("EVP_DigestUpdate() failed");
|
||||
}
|
||||
|
||||
unsigned int sha256_len = 0;
|
||||
if (!EVP_DigestFinal_ex(mdctx, sha256_value, &sha256_len)) {
|
||||
EVP_MD_CTX_free(mdctx);
|
||||
throw std::runtime_error("EVP_DigestFinal_ex() failed");
|
||||
}
|
||||
EVP_MD_CTX_free(mdctx);
|
||||
|
||||
std::string result;
|
||||
for (size_t i = 0; i < sha256_len; i++) {
|
||||
char buf[3];
|
||||
sprintf(buf, "%02x", sha256_value[i]);
|
||||
buf[2] = '\0';
|
||||
buf[2] = 0;
|
||||
result += buf;
|
||||
}
|
||||
return result;
|
||||
|
@ -127,7 +127,7 @@ std::string HTTP::Post(const std::string& IP, const std::string& Fields) {
|
||||
return Ret;
|
||||
}
|
||||
|
||||
bool HTTP::Download(const std::string& IP, const beammp_fs_string& Path, const std::string& Hash) {
|
||||
bool HTTP::Download(const std::string& IP, const beammp_fs_string& Path) {
|
||||
static std::mutex Lock;
|
||||
std::scoped_lock Guard(Lock);
|
||||
|
||||
@ -139,16 +139,6 @@ bool HTTP::Download(const std::string& IP, const beammp_fs_string& Path, const s
|
||||
return false;
|
||||
}
|
||||
|
||||
std::string RetHash = Utils::GetSha256HashReallyFast(Ret, Path);
|
||||
|
||||
debug("Return hash: " + RetHash);
|
||||
debug("Expected hash: " + Hash);
|
||||
|
||||
if (RetHash != Hash) {
|
||||
error("Downloaded file hash does not match expected hash");
|
||||
return false;
|
||||
}
|
||||
|
||||
std::ofstream File(Path, std::ios::binary);
|
||||
if (File.is_open()) {
|
||||
File << Ret;
|
||||
|
@ -445,7 +445,7 @@ void NewSyncResources(SOCKET Sock, const std::string& Mods, const std::vector<Mo
|
||||
}
|
||||
auto FileName = std::filesystem::path(ModInfoIter->FileName).stem().string() + "-" + ModInfoIter->Hash.substr(0, 8) + std::filesystem::path(ModInfoIter->FileName).extension().string();
|
||||
auto PathToSaveTo = (CachingDirectory / FileName);
|
||||
if (fs::exists(PathToSaveTo) && Utils::GetSha256HashReallyFastFile(PathToSaveTo) == ModInfoIter->Hash) {
|
||||
if (fs::exists(PathToSaveTo) && Utils::GetSha256HashReallyFast(PathToSaveTo) == ModInfoIter->Hash) {
|
||||
debug("Mod '" + FileName + "' found in cache");
|
||||
UpdateUl(false, std::to_string(ModNo) + "/" + std::to_string(TotalMods) + ": " + ModInfoIter->FileName);
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(50));
|
||||
@ -476,7 +476,7 @@ void NewSyncResources(SOCKET Sock, const std::string& Mods, const std::vector<Mo
|
||||
WaitForConfirm();
|
||||
continue;
|
||||
} else if (auto OldCachedPath = CachingDirectory / std::filesystem::path(ModInfoIter->FileName).filename();
|
||||
fs::exists(OldCachedPath) && Utils::GetSha256HashReallyFastFile(OldCachedPath) == ModInfoIter->Hash) {
|
||||
fs::exists(OldCachedPath) && Utils::GetSha256HashReallyFast(OldCachedPath) == ModInfoIter->Hash) {
|
||||
debug("Mod '" + FileName + "' found in old cache, copying it to the new cache");
|
||||
UpdateUl(false, std::to_string(ModNo) + "/" + std::to_string(TotalMods) + ": " + ModInfoIter->FileName);
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(50));
|
||||
@ -562,7 +562,7 @@ void NewSyncResources(SOCKET Sock, const std::string& Mods, const std::vector<Mo
|
||||
Terminate = true;
|
||||
}
|
||||
|
||||
if (Utils::GetSha256HashReallyFastFile(PathToSaveTo) != ModInfoIter->Hash) {
|
||||
if (Utils::GetSha256HashReallyFast(PathToSaveTo) != ModInfoIter->Hash) {
|
||||
error(beammp_wide("Failed to write or download the entire file '") + beammp_fs_string(PathToSaveTo) + beammp_wide("' correctly (hash mismatch)"));
|
||||
Terminate = true;
|
||||
}
|
||||
|
@ -187,7 +187,6 @@ void LegitimacyCheck() {
|
||||
// Right now only steam is supported
|
||||
std::vector<std::filesystem::path> steamappsCommonPaths = {
|
||||
".steam/root/steamapps", // default
|
||||
".steam/steam/steamapps", // Legacy Steam installations
|
||||
".var/app/com.valvesoftware.Steam/.steam/root/steamapps", // flatpak
|
||||
"snap/steam/common/.local/share/Steam/steamapps" // snap
|
||||
};
|
||||
|
@ -14,10 +14,7 @@
|
||||
#if defined(_WIN32)
|
||||
#elif defined(__linux__)
|
||||
#include <unistd.h>
|
||||
#elif defined (__APPLE__)
|
||||
#include <unistd.h>
|
||||
#include <libproc.h>
|
||||
#endif // __APPLE__
|
||||
#endif
|
||||
#include "Http.h"
|
||||
#include "Logger.h"
|
||||
#include "Network/network.hpp"
|
||||
@ -97,34 +94,6 @@ beammp_fs_string GetEP(const beammp_fs_char* P) {
|
||||
}();
|
||||
return Ret;
|
||||
}
|
||||
|
||||
fs::path 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)
|
||||
void ReLaunch() {
|
||||
std::wstring Arg;
|
||||
@ -134,7 +103,7 @@ void ReLaunch() {
|
||||
}
|
||||
info("Relaunch!");
|
||||
system("cls");
|
||||
ShellExecuteW(nullptr, L"runas", (GetBP() / GetEN()).c_str(), Arg.c_str(), nullptr, SW_SHOWNORMAL);
|
||||
ShellExecuteW(nullptr, L"runas", (GetEP() + GetEN()).c_str(), Arg.c_str(), nullptr, SW_SHOWNORMAL);
|
||||
ShowWindow(GetConsoleWindow(), 0);
|
||||
std::this_thread::sleep_for(std::chrono::seconds(1));
|
||||
exit(1);
|
||||
@ -145,7 +114,7 @@ void URelaunch() {
|
||||
Arg += Utils::ToWString(options.argv[c - 1]);
|
||||
Arg += L" ";
|
||||
}
|
||||
ShellExecuteW(nullptr, L"open", (GetBP() / GetEN()).c_str(), Arg.c_str(), nullptr, SW_SHOWNORMAL);
|
||||
ShellExecuteW(nullptr, L"open", (GetEP() + GetEN()).c_str(), Arg.c_str(), nullptr, SW_SHOWNORMAL);
|
||||
ShowWindow(GetConsoleWindow(), 0);
|
||||
std::this_thread::sleep_for(std::chrono::seconds(1));
|
||||
exit(1);
|
||||
@ -159,7 +128,7 @@ void ReLaunch() {
|
||||
}
|
||||
info("Relaunch!");
|
||||
system("clear");
|
||||
int ret = execv((GetBP() / GetEN()).c_str(), const_cast<char**>(options.argv));
|
||||
int ret = execv(options.executable_name.c_str(), const_cast<char**>(options.argv));
|
||||
if (ret < 0) {
|
||||
error(std::string("execv() failed with: ") + strerror(errno) + ". Failed to relaunch");
|
||||
exit(1);
|
||||
@ -168,7 +137,7 @@ void ReLaunch() {
|
||||
exit(1);
|
||||
}
|
||||
void URelaunch() {
|
||||
int ret = execv((GetBP() / GetEN()).c_str(), const_cast<char**>(options.argv));
|
||||
int ret = execv(options.executable_name.c_str(), const_cast<char**>(options.argv));
|
||||
if (ret < 0) {
|
||||
error(std::string("execv() failed with: ") + strerror(errno) + ". Failed to relaunch");
|
||||
exit(1);
|
||||
@ -200,9 +169,9 @@ void CheckForUpdates(const std::string& CV) {
|
||||
"https://backend.beammp.com/version/launcher?branch=" + Branch + "&pk=" + PublicKey);
|
||||
|
||||
transform(LatestHash.begin(), LatestHash.end(), LatestHash.begin(), ::tolower);
|
||||
beammp_fs_string BP(GetBP() / GetEN()), Back(GetBP() / beammp_wide("BeamMP-Launcher.back"));
|
||||
beammp_fs_string EP(GetEP() + GetEN()), Back(GetEP() + beammp_wide("BeamMP-Launcher.back"));
|
||||
|
||||
std::string FileHash = Utils::GetSha256HashReallyFastFile(BP);
|
||||
std::string FileHash = Utils::GetSha256HashReallyFast(EP);
|
||||
|
||||
if (FileHash != LatestHash && IsOutdated(Version(VersionStrToInts(GetVer() + GetPatch())), Version(VersionStrToInts(LatestVersion)))) {
|
||||
if (!options.no_update) {
|
||||
@ -210,21 +179,14 @@ void CheckForUpdates(const std::string& CV) {
|
||||
#if defined(__linux__)
|
||||
error("Auto update is NOT implemented for the Linux version. Please update manually ASAP as updates contain security patches.");
|
||||
#else
|
||||
fs::remove(Back);
|
||||
fs::rename(EP, Back);
|
||||
info("Downloading Launcher update " + LatestHash);
|
||||
HTTP::Download(
|
||||
"https://backend.beammp.com/builds/launcher?download=true"
|
||||
"&pk="
|
||||
+ PublicKey + "&branch=" + Branch,
|
||||
beammp_wide("new_") + BP, LatestHash);
|
||||
std::error_code ec;
|
||||
fs::remove(Back, ec);
|
||||
if (ec == std::errc::permission_denied) {
|
||||
error("Failed to remove old backup file: " + ec.message() + ". Using alternative name.");
|
||||
fs::rename(BP, Back + beammp_wide(".") + Utils::ToWString(FileHash.substr(0, 8)));
|
||||
} else {
|
||||
fs::rename(BP, Back);
|
||||
}
|
||||
fs::rename(beammp_wide("new_") + BP, BP);
|
||||
EP);
|
||||
URelaunch();
|
||||
#endif
|
||||
} else {
|
||||
@ -285,8 +247,8 @@ void InitLauncher() {
|
||||
}
|
||||
#endif
|
||||
|
||||
size_t DirCount(const fs::path& path) {
|
||||
return (size_t)std::distance(fs::directory_iterator { path }, fs::directory_iterator {});
|
||||
size_t DirCount(const std::filesystem::path& path) {
|
||||
return (size_t)std::distance(std::filesystem::directory_iterator { path }, std::filesystem::directory_iterator {});
|
||||
}
|
||||
|
||||
void CheckMP(const beammp_fs_string& Path) {
|
||||
@ -367,14 +329,14 @@ void PreGame(const beammp_fs_string& GamePath) {
|
||||
std::string ZipPath(GetGamePath() / R"(mods/multiplayer/beammp.zip)");
|
||||
#endif
|
||||
|
||||
std::string FileHash = fs::exists(ZipPath) ? Utils::GetSha256HashReallyFastFile(ZipPath) : "";
|
||||
std::string FileHash = fs::exists(ZipPath) ? Utils::GetSha256HashReallyFast(ZipPath) : "";
|
||||
|
||||
if (FileHash != LatestHash) {
|
||||
info("Downloading BeamMP Update " + LatestHash);
|
||||
HTTP::Download("https://backend.beammp.com/builds/client?download=true"
|
||||
"&pk="
|
||||
+ PublicKey + "&branch=" + Branch,
|
||||
ZipPath, LatestHash);
|
||||
ZipPath);
|
||||
}
|
||||
|
||||
beammp_fs_string Target(GetGamePath() / beammp_wide("mods/unpacked/beammp"));
|
||||
|
Loading…
x
Reference in New Issue
Block a user