From 6c740e2562f3ad7b9ae93c0851f51f33f3753717 Mon Sep 17 00:00:00 2001 From: Tixx <83774803+WiserTixx@users.noreply.github.com> Date: Tue, 3 Jun 2025 11:33:32 +0200 Subject: [PATCH] Fix merge --- include/Utils.h | 52 +++++++++++++++++++++++++++++++++++---- src/GameStart.cpp | 17 ++++++------- src/Network/Core.cpp | 1 + src/Network/Resources.cpp | 6 ++--- src/Security/BeamNG.cpp | 1 - src/Startup.cpp | 1 - 6 files changed, 59 insertions(+), 19 deletions(-) diff --git a/include/Utils.h b/include/Utils.h index 28c601b..f8d7d8c 100644 --- a/include/Utils.h +++ b/include/Utils.h @@ -19,6 +19,8 @@ #define beammp_fs_string std::wstring #define beammp_fs_char wchar_t #define beammp_wide(str) L##str +#define WIN32_LEAN_AND_MEAN +#include #else #define beammp_fs_string std::string #define beammp_fs_char char @@ -71,6 +73,38 @@ namespace Utils { return result; } +#ifdef _WIN32 + inline std::wstring ExpandEnvVars(const std::wstring& input) { + std::wstring result; + std::wregex envPattern(LR"(%([^%]+)%|\$([A-Za-z_][A-Za-z0-9_]*)|\$\{([^}]+)\})"); + + std::wsregex_iterator begin(input.begin(), input.end(), envPattern); + std::wsregex_iterator end; + + size_t lastPos = 0; + + for (auto it = begin; it != end; ++it) { + const auto& match = *it; + + result.append(input, lastPos, match.position() - lastPos); + + std::wstring varName; + if (match[1].matched) varName = match[1].str(); // %VAR% + else if (match[2].matched) varName = match[2].str(); // $VAR + else if (match[3].matched) varName = match[3].str(); // ${VAR} + + if (const wchar_t* envValue = _wgetenv(varName.c_str())) { + result.append(envValue); + } + + lastPos = match.position() + match.length(); + } + + result.append(input, lastPos, input.length() - lastPos); + + return result; + } +#endif inline std::map> ParseINI(const std::string& contents) { std::map> ini; @@ -127,12 +161,20 @@ namespace Utils { return ini; } - inline std::string ToString(const std::wstring& w) { - return std::wstring_convert>().to_bytes(w); - } #ifdef _WIN32 - inline std::wstring ToWString(const std::string& s) { - return std::wstring_convert>().from_bytes(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); + if (size_needed <= 0) { + return L""; + } + + std::wstring result(size_needed, 0); + + MultiByteToWideChar(CP_UTF8, 0, s.c_str(), (int)s.size(), &result[0], size_needed); + + return result; } #else inline std::string ToWString(const std::string& s) { diff --git a/src/GameStart.cpp b/src/GameStart.cpp index 70ed9a2..1f39548 100644 --- a/src/GameStart.cpp +++ b/src/GameStart.cpp @@ -6,7 +6,6 @@ #if defined(_WIN32) #include -#include #elif defined(__linux__) #include "vdf_parser.hpp" #include @@ -32,14 +31,14 @@ std::wstring QueryKey(HKEY hKey, int ID); std::wstring GetGamePath() { static std::filesystem::path Path; if (!Path.empty()) - return Path.string(); + return Path.wstring(); if (options.user_path) { if (std::filesystem::exists(options.user_path)) { Path = options.user_path; - debug("Using custom user folder path: " + Path.string()); + debug(L"Using custom user folder path: " + Path.wstring()); } else - warn("Invalid or non-existent path (" + std::string(options.user_path) + ") specified using --user-path, skipping"); + warn(L"Invalid or non-existent path (" + Utils::ToWString(options.user_path) + L") specified using --user-path, skipping"); } if (const auto startupIniPath = std::filesystem::path(GetGameDir()) / "startup.ini"; exists(startupIniPath)) { @@ -58,16 +57,16 @@ std::wstring GetGamePath() { } else debug("Successfully parsed startup.ini"); - std::string userPath; + std::wstring userPath; if (ini.contains("filesystem") && ini["filesystem"].contains("UserPath")) - userPath = ini["filesystem"]["UserPath"]; + userPath = Utils::ToWString(ini["filesystem"]["UserPath"]); if (!userPath.empty()) if (userPath = Utils::ExpandEnvVars(userPath); std::filesystem::exists(userPath)) { Path = userPath; - debug("Using custom user folder path from startup.ini: " + Path.string()); + debug(L"Using custom user folder path from startup.ini: " + Path.wstring()); } else - warn("Found custom user folder path (" + userPath + ") in startup.ini but it doesn't exist, skipping"); + warn(L"Found custom user folder path (" + userPath + L") in startup.ini but it doesn't exist, skipping"); } if (Path.empty()) { @@ -129,7 +128,7 @@ void StartGame(std::wstring Dir) { gameArgs += Utils::ToWString(options.game_arguments[i]); } - debug("BeamNG executable path: " + Dir); + debug(L"BeamNG executable path: " + Dir); bSuccess = CreateProcessW(nullptr, (wchar_t*)(Dir + gameArgs).c_str(), nullptr, nullptr, TRUE, 0, nullptr, BaseDir.c_str(), &si, &pi); if (bSuccess) { diff --git a/src/Network/Core.cpp b/src/Network/Core.cpp index 9121f2a..902e1c7 100644 --- a/src/Network/Core.cpp +++ b/src/Network/Core.cpp @@ -12,6 +12,7 @@ #if defined(_WIN32) #include #include +#include #elif defined(__linux__) #include #include diff --git a/src/Network/Resources.cpp b/src/Network/Resources.cpp index 68e917d..8f580a8 100644 --- a/src/Network/Resources.cpp +++ b/src/Network/Resources.cpp @@ -525,7 +525,7 @@ void NewSyncResources(SOCKET Sock, const std::string& Mods, const std::vectorFileName; do { - debug("Loading file '" + FName + "' to '" + PathToSaveTo.string() + "'"); + debug(L"Loading file '" + Utils::ToWString(FName) + L"' to '" + PathToSaveTo.wstring() + L"'"); TCPSend("f" + ModInfoIter->FileName, Sock); std::string Data = TCPRcv(Sock); @@ -562,8 +562,8 @@ void NewSyncResources(SOCKET Sock, const std::string& Mods, const std::vectorHash) { - error("Failed to write or download the entire file '" + PathToSaveTo + "' correctly (hash mismatch)"); + if (Utils::GetSha256HashReallyFast(PathToSaveTo) != ModInfoIter->Hash) { + error(L"Failed to write or download the entire file '" + PathToSaveTo.wstring() + L"' correctly (hash mismatch)"); Terminate = true; } } while (fs::file_size(PathToSaveTo) != ModInfoIter->FileSize && !Terminate); diff --git a/src/Security/BeamNG.cpp b/src/Security/BeamNG.cpp index 36ff5d6..9b6cbe3 100644 --- a/src/Security/BeamNG.cpp +++ b/src/Security/BeamNG.cpp @@ -7,7 +7,6 @@ #include #if defined(_WIN32) -#include #elif defined(__linux__) #include "vdf_parser.hpp" #include diff --git a/src/Startup.cpp b/src/Startup.cpp index cd87590..a6b0590 100644 --- a/src/Startup.cpp +++ b/src/Startup.cpp @@ -12,7 +12,6 @@ #include #include #if defined(_WIN32) -#include #elif defined(__linux__) #include #endif