Fix wstring for windows and add crossplatform code for fs strings

This commit is contained in:
Tixx
2025-01-26 23:08:31 +01:00
parent 5e448dc34f
commit 06c741edc5
10 changed files with 81 additions and 126 deletions
+1 -1
View File
@@ -187,7 +187,7 @@ void StartGame(std::string Dir) {
}
#endif
void InitGame(const std::wstring& Dir) {
void InitGame(const beammp_fs_string& Dir) {
if (!options.no_launch) {
std::thread Game(StartGame, Dir);
Game.detach();
+6 -4
View File
@@ -37,7 +37,7 @@ std::string getDate() {
}
void InitLog() {
std::ofstream LFS;
LFS.open(GetEP() + L"Launcher.log");
LFS.open(GetEP() + beammp_wide("Launcher.log"));
if (!LFS.is_open()) {
error("logger file init failed!");
} else
@@ -45,13 +45,13 @@ void InitLog() {
}
void addToLog(const std::string& Line) {
std::ofstream LFS;
LFS.open(GetEP() + L"Launcher.log", std::ios_base::app);
LFS.open(GetEP() + beammp_wide("Launcher.log"), std::ios_base::app);
LFS << Line.c_str();
LFS.close();
}
void addToLog(const std::wstring& Line) {
std::wofstream LFS;
LFS.open(GetEP() + L"Launcher.log", std::ios_base::app);
LFS.open(GetEP() + beammp_wide("Launcher.log"), std::ios_base::app);
LFS << Line.c_str();
LFS.close();
}
@@ -91,6 +91,7 @@ void except(const std::string& toPrint) {
}
#ifdef _WIN32
void info(const std::wstring& toPrint) {
std::wstring Print = Utils::ToWString(getDate()) + L"[INFO] " + toPrint + L"\n";
std::wcout << Print;
@@ -124,4 +125,5 @@ void except(const std::wstring& toPrint) {
std::wstring Print = Utils::ToWString(getDate()) + L"[EXCEP] " + toPrint + L"\n";
std::wcout << Print;
addToLog(Print);
}
}
#endif
+2 -2
View File
@@ -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 std::wstring& Path) {
bool HTTP::Download(const std::string& IP, const beammp_fs_string& Path) {
static std::mutex Lock;
std::scoped_lock Guard(Lock);
@@ -145,7 +145,7 @@ bool HTTP::Download(const std::string& IP, const std::wstring& Path) {
File.close();
info("Download Complete!");
} else {
error(L"Failed to open file directory: " + Path);
error(beammp_wide("Failed to open file directory: ") + Path);
return false;
}
+17 -17
View File
@@ -444,14 +444,14 @@ void NewSyncResources(SOCKET Sock, const std::string& Mods, const std::vector<Mo
return;
}
auto FileName = std::filesystem::path(ModInfoIter->FileName).stem().string() + "-" + ModInfoIter->Hash.substr(0, 8) + std::filesystem::path(ModInfoIter->FileName).extension().string();
auto PathToSaveTo = (fs::path(CachingDirectory) / FileName).string();
auto PathToSaveTo = (fs::path(CachingDirectory) / FileName);
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));
try {
if (!fs::exists(GetGamePath() + L"mods/multiplayer")) {
fs::create_directories(GetGamePath() + L"mods/multiplayer");
if (!fs::exists(GetGamePath() + beammp_wide("mods/multiplayer"))) {
fs::create_directories(GetGamePath() + beammp_wide("mods/multiplayer"));
}
auto modname = ModInfoIter->FileName;
#if defined(__linux__)
@@ -476,15 +476,15 @@ void NewSyncResources(SOCKET Sock, const std::string& Mods, const std::vector<Mo
WaitForConfirm();
continue;
} else if (auto OldCachedPath = fs::path(CachingDirectory) / std::filesystem::path(ModInfoIter->FileName).filename();
fs::exists(OldCachedPath) && GetSha256HashReallyFast(OldCachedPath.string()) == 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));
try {
fs::copy_file(OldCachedPath, PathToSaveTo, fs::copy_options::overwrite_existing);
if (!fs::exists(GetGamePath() + "mods/multiplayer")) {
fs::create_directories(GetGamePath() + "mods/multiplayer");
if (!fs::exists(GetGamePath() + beammp_wide("mods/multiplayer"))) {
fs::create_directories(GetGamePath() + beammp_wide("mods/multiplayer"));
}
auto modname = ModInfoIter->FileName;
@@ -525,7 +525,7 @@ void NewSyncResources(SOCKET Sock, const std::string& Mods, const std::vector<Mo
CheckForDir();
std::string FName = ModInfoIter->FileName;
do {
debug("Loading file '" + FName + "' to '" + PathToSaveTo + "'");
debug("Loading file '" + FName + "' to '" + PathToSaveTo.string() + "'");
TCPSend("f" + ModInfoIter->FileName, Sock);
std::string Data = TCPRcv(Sock);
@@ -558,7 +558,7 @@ void NewSyncResources(SOCKET Sock, const std::string& Mods, const std::vector<Mo
}
// 2. verify size and hash
if (std::filesystem::file_size(PathToSaveTo) != DownloadedFile.size()) {
error("Failed to write the entire file '" + PathToSaveTo + "' correctly (file size mismatch)");
error("Failed to write the entire file '" + PathToSaveTo.string() + "' correctly (file size mismatch)");
Terminate = true;
}
@@ -568,8 +568,8 @@ void NewSyncResources(SOCKET Sock, const std::string& Mods, const std::vector<Mo
}
} while (fs::file_size(PathToSaveTo) != ModInfoIter->FileSize && !Terminate);
if (!Terminate) {
if (!fs::exists(GetGamePath() + L"mods/multiplayer")) {
fs::create_directories(GetGamePath() + L"mods/multiplayer");
if (!fs::exists(GetGamePath() + beammp_wide("mods/multiplayer"))) {
fs::create_directories(GetGamePath() + beammp_wide("mods/multiplayer"));
}
// Linux version of the game doesnt support uppercase letters in mod names
@@ -667,8 +667,8 @@ void SyncResources(SOCKET Sock) {
UpdateUl(false, std::to_string(Pos) + "/" + std::to_string(Amount) + ": " + PathToSaveTo.substr(PathToSaveTo.find_last_of('/')));
std::this_thread::sleep_for(std::chrono::milliseconds(50));
try {
if (!fs::exists(GetGamePath() + L"mods/multiplayer")) {
fs::create_directories(GetGamePath() + L"mods/multiplayer");
if (!fs::exists(GetGamePath() + beammp_wide("mods/multiplayer"))) {
fs::create_directories(GetGamePath() + beammp_wide("mods/multiplayer"));
}
auto modname = PathToSaveTo.substr(PathToSaveTo.find_last_of('/'));
#if defined(__linux__)
@@ -677,8 +677,8 @@ void SyncResources(SOCKET Sock) {
c = ::tolower(c);
}
#endif
auto name = GetGamePath() + L"mods/multiplayer" + Utils::ToWString(modname);
auto tmp_name = name + L".tmp";
auto name = GetGamePath() + beammp_wide("mods/multiplayer") + Utils::ToWString(modname);
auto tmp_name = name + beammp_wide(".tmp");
fs::copy_file(PathToSaveTo, tmp_name, fs::copy_options::overwrite_existing);
fs::rename(tmp_name, name);
UpdateModUsage(modname);
@@ -725,8 +725,8 @@ void SyncResources(SOCKET Sock) {
}
} while (fs::file_size(PathToSaveTo) != std::stoull(*FS) && !Terminate);
if (!Terminate) {
if (!fs::exists(GetGamePath() + L"mods/multiplayer")) {
fs::create_directories(GetGamePath() + L"mods/multiplayer");
if (!fs::exists(GetGamePath() + beammp_wide("mods/multiplayer"))) {
fs::create_directories(GetGamePath() + beammp_wide("mods/multiplayer"));
}
// Linux version of the game doesnt support uppercase letters in mod names
@@ -736,7 +736,7 @@ void SyncResources(SOCKET Sock) {
}
#endif
fs::copy_file(PathToSaveTo, GetGamePath() + L"mods/multiplayer" + Utils::ToWString(FName), fs::copy_options::overwrite_existing);
fs::copy_file(PathToSaveTo, GetGamePath() + beammp_wide("mods/multiplayer") + Utils::ToWString(FName), fs::copy_options::overwrite_existing);
UpdateModUsage(FN->substr(pos));
}
WaitForConfirm();
+6 -8
View File
@@ -15,6 +15,8 @@
#include <vector>
#endif
#include "Logger.h"
#include "Utils.h"
#include <fstream>
#include <string>
#include <thread>
@@ -23,7 +25,7 @@
#define MAX_VALUE_NAME 16383
int TraceBack = 0;
std::wstring GameDir;
beammp_fs_string GameDir;
void lowExit(int code) {
TraceBack = 0;
@@ -33,7 +35,7 @@ void lowExit(int code) {
exit(2);
}
std::wstring GetGameDir() {
beammp_fs_string GetGameDir() {
#if defined(_WIN32)
return GameDir.substr(0, GameDir.find_last_of('\\'));
#elif defined(__linux__)
@@ -231,13 +233,9 @@ void LegitimacyCheck() {
}
#endif
}
std::string CheckVer(const std::wstring& dir) {
std::string CheckVer(const beammp_fs_string& dir) {
std::string temp;
#if defined(_WIN32)
std::wstring Path = dir + L"\\integrity.json";
#elif defined(__linux__)
std::wstring Path = dir + L"/integrity.json";
#endif
beammp_fs_string Path = dir + beammp_wide("\\integrity.json");
std::ifstream f(Path.c_str(), std::ios::binary);
int Size = int(std::filesystem::file_size(Path));
std::string vec(Size, 0);
+20 -24
View File
@@ -73,12 +73,8 @@ Version::Version(const std::array<uint8_t, 3>& v)
: Version(v[0], v[1], v[2]) {
}
std::wstring GetEN() {
#if defined(_WIN32)
return L"BeamMP-Launcher.exe";
#elif defined(__linux__)
return L"BeamMP-Launcher";
#endif
beammp_fs_string GetEN() {
return beammp_wide("BeamMP-Launcher.exe");
}
std::string GetVer() {
@@ -88,10 +84,10 @@ std::string GetPatch() {
return ".1";
}
std::wstring GetEP(const wchar_t* P) {
static std::wstring Ret = [&]() {
std::wstring path(P);
return path.substr(0, path.find_last_of(L"\\/") + 1);
beammp_fs_string GetEP(const beammp_fs_char* P) {
static beammp_fs_string Ret = [&]() {
beammp_fs_string path(P);
return path.substr(0, path.find_last_of(beammp_wide("\\/")) + 1);
}();
return Ret;
}
@@ -152,14 +148,14 @@ void CheckName() {
#if defined(_WIN32)
std::wstring DN = GetEN(), CDir = Utils::ToWString(options.executable_name), FN = CDir.substr(CDir.find_last_of('\\') + 1);
#elif defined(__linux__)
std::wstring DN = GetEN(), CDir = Utils::ToWString(options.executable_name), FN = CDir.substr(CDir.find_last_of('/') + 1);
std::string DN = GetEN(), CDir = options.executable_name, FN = CDir.substr(CDir.find_last_of('/') + 1);
#endif
if (FN != DN) {
if (fs::exists(DN))
_wremove(DN.c_str());
fs::remove(DN.c_str());
if (fs::exists(DN))
ReLaunch();
_wrename(FN.c_str(), DN.c_str());
fs::rename(FN.c_str(), DN.c_str());
URelaunch();
}
}
@@ -170,7 +166,7 @@ void CheckForUpdates(const std::string& CV) {
"https://backend.beammp.com/version/launcher?branch=" + Branch + "&pk=" + PublicKey);
transform(LatestHash.begin(), LatestHash.end(), LatestHash.begin(), ::tolower);
std::wstring EP(GetEP() + GetEN()), Back(GetEP() + L"BeamMP-Launcher.back");
beammp_fs_string EP(GetEP() + GetEN()), Back(GetEP() + beammp_wide("BeamMP-Launcher.back"));
std::string FileHash = Utils::GetSha256HashReallyFast(EP);
@@ -252,7 +248,7 @@ 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 std::wstring& Path) {
void CheckMP(const beammp_fs_string& Path) {
if (!fs::exists(Path))
return;
size_t c = DirCount(fs::path(Path));
@@ -272,7 +268,7 @@ void CheckMP(const std::wstring& Path) {
}
void EnableMP() {
std::wstring File(GetGamePath() + L"mods/db.json");
beammp_fs_string File(GetGamePath() + beammp_wide("mods/db.json"));
if (!fs::exists(File))
return;
auto Size = fs::file_size(File);
@@ -295,18 +291,18 @@ void EnableMP() {
ofs << d.dump();
ofs.close();
} else {
error(L"Failed to write " + File);
error(beammp_wide("Failed to write ") + File);
}
}
}
}
void PreGame(const std::wstring& GamePath) {
void PreGame(const beammp_fs_string& GamePath) {
std::string GameVer = CheckVer(GamePath);
info("Game Version : " + GameVer);
CheckMP(GetGamePath() + L"mods/multiplayer");
info(L"Game user path: " + GetGamePath());
CheckMP(GetGamePath() + beammp_wide("mods/multiplayer"));
info(beammp_wide("Game user path: ") + GetGamePath());
if (!options.no_download) {
std::string LatestHash = HTTP::Get("https://backend.beammp.com/sha/mod?branch=" + Branch + "&pk=" + PublicKey);
@@ -316,8 +312,8 @@ void PreGame(const std::wstring& GamePath) {
LatestHash.end());
try {
if (!fs::exists(GetGamePath() + L"mods/multiplayer")) {
fs::create_directories(GetGamePath() + L"mods/multiplayer");
if (!fs::exists(GetGamePath() + beammp_wide("mods/multiplayer"))) {
fs::create_directories(GetGamePath() + beammp_wide("mods/multiplayer"));
}
EnableMP();
} catch (std::exception& e) {
@@ -327,7 +323,7 @@ void PreGame(const std::wstring& GamePath) {
std::wstring ZipPath(GetGamePath() + LR"(mods\multiplayer\BeamMP.zip)");
#elif defined(__linux__)
// Linux version of the game cant handle mods with uppercase names
std::wstring ZipPath(GetGamePath() + LR"(mods/multiplayer/beammp.zip)");
std::string ZipPath(GetGamePath() + R"(mods/multiplayer/beammp.zip)");
#endif
std::string FileHash = Utils::GetSha256HashReallyFast(ZipPath);
@@ -340,7 +336,7 @@ void PreGame(const std::wstring& GamePath) {
ZipPath);
}
std::wstring Target(GetGamePath() + L"mods/unpacked/beammp");
beammp_fs_string Target(GetGamePath() + beammp_wide("mods/unpacked/beammp"));
if (fs::is_directory(Target) && !fs::is_directory(Target + "/.git")) {
fs::remove_all(Target);