mirror of
https://github.com/BeamMP/BeamMP-Launcher.git
synced 2025-08-17 00:35:55 +00:00
Compare commits
10 Commits
Author | SHA1 | Date | |
---|---|---|---|
|
5b2eb0a1a0 | ||
|
f27d3c6120 | ||
|
7a4b24d616 | ||
|
b6b0e4ba3e | ||
|
a87aa7230a | ||
|
b9cc025083 | ||
|
c0ed056440 | ||
|
e6e5bf8327 | ||
|
e7cfb6e406 | ||
|
185818d174 |
@ -11,7 +11,7 @@
|
|||||||
#include <string>
|
#include <string>
|
||||||
class HTTP {
|
class HTTP {
|
||||||
public:
|
public:
|
||||||
static bool Download(const std::string& IP, const beammp_fs_string& Path);
|
static bool Download(const std::string& IP, const beammp_fs_string& Path, const std::string& Hash);
|
||||||
static std::string Post(const std::string& IP, const std::string& Fields);
|
static std::string Post(const std::string& IP, const std::string& Fields);
|
||||||
static std::string Get(const std::string& IP);
|
static std::string Get(const std::string& IP);
|
||||||
static bool ProgressBar(size_t c, size_t t);
|
static bool ProgressBar(size_t c, size_t t);
|
||||||
|
@ -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();
|
||||||
|
@ -29,7 +29,6 @@
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
namespace Utils {
|
namespace Utils {
|
||||||
|
|
||||||
inline std::vector<std::string> Split(const std::string& String, const std::string& delimiter) {
|
inline std::vector<std::string> Split(const std::string& String, const std::string& delimiter) {
|
||||||
std::vector<std::string> Val;
|
std::vector<std::string> Val;
|
||||||
size_t pos;
|
size_t pos;
|
||||||
@ -166,7 +165,7 @@ namespace Utils {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
inline std::wstring ToWString(const std::string& s) {
|
inline std::wstring ToWString(const std::string& s) {
|
||||||
if (s.empty()) return std::wstring();
|
if (s.empty()) return std::wstring();
|
||||||
|
|
||||||
int size_needed = MultiByteToWideChar(CP_UTF8, 0, s.c_str(), (int)s.size(), nullptr, 0);
|
int size_needed = MultiByteToWideChar(CP_UTF8, 0, s.c_str(), (int)s.size(), nullptr, 0);
|
||||||
@ -185,7 +184,7 @@ inline std::wstring ToWString(const std::string& s) {
|
|||||||
return s;
|
return s;
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
inline std::string GetSha256HashReallyFast(const beammp_fs_string& filename) {
|
inline std::string GetSha256HashReallyFastFile(const beammp_fs_string& filename) {
|
||||||
try {
|
try {
|
||||||
EVP_MD_CTX* mdctx;
|
EVP_MD_CTX* mdctx;
|
||||||
const EVP_MD* md;
|
const EVP_MD* md;
|
||||||
@ -237,7 +236,51 @@ inline std::wstring ToWString(const std::string& s) {
|
|||||||
for (size_t i = 0; i < sha256_len; i++) {
|
for (size_t i = 0; i < sha256_len; i++) {
|
||||||
char buf[3];
|
char buf[3];
|
||||||
sprintf(buf, "%02x", sha256_value[i]);
|
sprintf(buf, "%02x", sha256_value[i]);
|
||||||
buf[2] = 0;
|
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';
|
||||||
result += buf;
|
result += buf;
|
||||||
}
|
}
|
||||||
return result;
|
return result;
|
||||||
|
@ -127,7 +127,7 @@ std::string HTTP::Post(const std::string& IP, const std::string& Fields) {
|
|||||||
return Ret;
|
return Ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool HTTP::Download(const std::string& IP, const beammp_fs_string& Path) {
|
bool HTTP::Download(const std::string& IP, const beammp_fs_string& Path, const std::string& Hash) {
|
||||||
static std::mutex Lock;
|
static std::mutex Lock;
|
||||||
std::scoped_lock Guard(Lock);
|
std::scoped_lock Guard(Lock);
|
||||||
|
|
||||||
@ -139,6 +139,16 @@ bool HTTP::Download(const std::string& IP, const beammp_fs_string& Path) {
|
|||||||
return false;
|
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);
|
std::ofstream File(Path, std::ios::binary);
|
||||||
if (File.is_open()) {
|
if (File.is_open()) {
|
||||||
File << Ret;
|
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 FileName = std::filesystem::path(ModInfoIter->FileName).stem().string() + "-" + ModInfoIter->Hash.substr(0, 8) + std::filesystem::path(ModInfoIter->FileName).extension().string();
|
||||||
auto PathToSaveTo = (CachingDirectory / FileName);
|
auto PathToSaveTo = (CachingDirectory / FileName);
|
||||||
if (fs::exists(PathToSaveTo) && Utils::GetSha256HashReallyFast(PathToSaveTo) == ModInfoIter->Hash) {
|
if (fs::exists(PathToSaveTo) && Utils::GetSha256HashReallyFastFile(PathToSaveTo) == ModInfoIter->Hash) {
|
||||||
debug("Mod '" + FileName + "' found in cache");
|
debug("Mod '" + FileName + "' found in cache");
|
||||||
UpdateUl(false, std::to_string(ModNo) + "/" + std::to_string(TotalMods) + ": " + ModInfoIter->FileName);
|
UpdateUl(false, std::to_string(ModNo) + "/" + std::to_string(TotalMods) + ": " + ModInfoIter->FileName);
|
||||||
std::this_thread::sleep_for(std::chrono::milliseconds(50));
|
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();
|
WaitForConfirm();
|
||||||
continue;
|
continue;
|
||||||
} else if (auto OldCachedPath = CachingDirectory / std::filesystem::path(ModInfoIter->FileName).filename();
|
} else if (auto OldCachedPath = CachingDirectory / std::filesystem::path(ModInfoIter->FileName).filename();
|
||||||
fs::exists(OldCachedPath) && Utils::GetSha256HashReallyFast(OldCachedPath) == ModInfoIter->Hash) {
|
fs::exists(OldCachedPath) && Utils::GetSha256HashReallyFastFile(OldCachedPath) == ModInfoIter->Hash) {
|
||||||
debug("Mod '" + FileName + "' found in old cache, copying it to the new cache");
|
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);
|
UpdateUl(false, std::to_string(ModNo) + "/" + std::to_string(TotalMods) + ": " + ModInfoIter->FileName);
|
||||||
std::this_thread::sleep_for(std::chrono::milliseconds(50));
|
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;
|
Terminate = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (Utils::GetSha256HashReallyFast(PathToSaveTo) != ModInfoIter->Hash) {
|
if (Utils::GetSha256HashReallyFastFile(PathToSaveTo) != ModInfoIter->Hash) {
|
||||||
error(beammp_wide("Failed to write or download the entire file '") + beammp_fs_string(PathToSaveTo) + beammp_wide("' correctly (hash mismatch)"));
|
error(beammp_wide("Failed to write or download the entire file '") + beammp_fs_string(PathToSaveTo) + beammp_wide("' correctly (hash mismatch)"));
|
||||||
Terminate = true;
|
Terminate = true;
|
||||||
}
|
}
|
||||||
|
@ -187,6 +187,7 @@ void LegitimacyCheck() {
|
|||||||
// Right now only steam is supported
|
// Right now only steam is supported
|
||||||
std::vector<std::filesystem::path> steamappsCommonPaths = {
|
std::vector<std::filesystem::path> steamappsCommonPaths = {
|
||||||
".steam/root/steamapps", // default
|
".steam/root/steamapps", // default
|
||||||
|
".steam/steam/steamapps", // Legacy Steam installations
|
||||||
".var/app/com.valvesoftware.Steam/.steam/root/steamapps", // flatpak
|
".var/app/com.valvesoftware.Steam/.steam/root/steamapps", // flatpak
|
||||||
"snap/steam/common/.local/share/Steam/steamapps" // snap
|
"snap/steam/common/.local/share/Steam/steamapps" // snap
|
||||||
};
|
};
|
||||||
|
@ -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;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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)
|
#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,9 +200,9 @@ 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::GetSha256HashReallyFast(EP);
|
std::string FileHash = Utils::GetSha256HashReallyFastFile(BP);
|
||||||
|
|
||||||
if (FileHash != LatestHash && IsOutdated(Version(VersionStrToInts(GetVer() + GetPatch())), Version(VersionStrToInts(LatestVersion)))) {
|
if (FileHash != LatestHash && IsOutdated(Version(VersionStrToInts(GetVer() + GetPatch())), Version(VersionStrToInts(LatestVersion)))) {
|
||||||
if (!options.no_update) {
|
if (!options.no_update) {
|
||||||
@ -179,14 +210,21 @@ void CheckForUpdates(const std::string& CV) {
|
|||||||
#if defined(__linux__)
|
#if defined(__linux__)
|
||||||
error("Auto update is NOT implemented for the Linux version. Please update manually ASAP as updates contain security patches.");
|
error("Auto update is NOT implemented for the Linux version. Please update manually ASAP as updates contain security patches.");
|
||||||
#else
|
#else
|
||||||
fs::remove(Back);
|
|
||||||
fs::rename(EP, Back);
|
|
||||||
info("Downloading Launcher update " + LatestHash);
|
info("Downloading Launcher update " + LatestHash);
|
||||||
HTTP::Download(
|
HTTP::Download(
|
||||||
"https://backend.beammp.com/builds/launcher?download=true"
|
"https://backend.beammp.com/builds/launcher?download=true"
|
||||||
"&pk="
|
"&pk="
|
||||||
+ PublicKey + "&branch=" + Branch,
|
+ PublicKey + "&branch=" + Branch,
|
||||||
EP);
|
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);
|
||||||
URelaunch();
|
URelaunch();
|
||||||
#endif
|
#endif
|
||||||
} else {
|
} else {
|
||||||
@ -247,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) {
|
||||||
@ -329,14 +367,14 @@ void PreGame(const beammp_fs_string& GamePath) {
|
|||||||
std::string ZipPath(GetGamePath() / R"(mods/multiplayer/beammp.zip)");
|
std::string ZipPath(GetGamePath() / R"(mods/multiplayer/beammp.zip)");
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
std::string FileHash = fs::exists(ZipPath) ? Utils::GetSha256HashReallyFast(ZipPath) : "";
|
std::string FileHash = fs::exists(ZipPath) ? Utils::GetSha256HashReallyFastFile(ZipPath) : "";
|
||||||
|
|
||||||
if (FileHash != LatestHash) {
|
if (FileHash != LatestHash) {
|
||||||
info("Downloading BeamMP Update " + LatestHash);
|
info("Downloading BeamMP Update " + LatestHash);
|
||||||
HTTP::Download("https://backend.beammp.com/builds/client?download=true"
|
HTTP::Download("https://backend.beammp.com/builds/client?download=true"
|
||||||
"&pk="
|
"&pk="
|
||||||
+ PublicKey + "&branch=" + Branch,
|
+ PublicKey + "&branch=" + Branch,
|
||||||
ZipPath);
|
ZipPath, LatestHash);
|
||||||
}
|
}
|
||||||
|
|
||||||
beammp_fs_string Target(GetGamePath() / beammp_wide("mods/unpacked/beammp"));
|
beammp_fs_string Target(GetGamePath() / beammp_wide("mods/unpacked/beammp"));
|
||||||
|
Loading…
x
Reference in New Issue
Block a user