From 59449bbe4754de7bf9f13212dbad54fdc4726fc6 Mon Sep 17 00:00:00 2001 From: WerWolv Date: Mon, 1 Aug 2022 23:25:13 +0200 Subject: [PATCH] fix: Portable version detection not working correctly --- lib/libimhex/include/hex/helpers/utils.hpp | 12 +++++++----- lib/libimhex/source/helpers/fs.cpp | 13 ++++++++++--- main/source/init/tasks.cpp | 2 +- 3 files changed, 18 insertions(+), 9 deletions(-) diff --git a/lib/libimhex/include/hex/helpers/utils.hpp b/lib/libimhex/include/hex/helpers/utils.hpp index 5f4e29afc..2deef52d8 100644 --- a/lib/libimhex/include/hex/helpers/utils.hpp +++ b/lib/libimhex/include/hex/helpers/utils.hpp @@ -274,20 +274,22 @@ namespace hex { return result; } - inline void trimLeft(std::string &s) { + template + inline void trimLeft(std::basic_string &s) { s.erase(s.begin(), std::find_if(s.begin(), s.end(), [](unsigned char ch) { return !std::isspace(ch) && ch >= 0x20; })); } - inline void trimRight(std::string &s) { + template + inline void trimRight(std::basic_string &s) { s.erase(std::find_if(s.rbegin(), s.rend(), [](unsigned char ch) { return !std::isspace(ch) && ch >= 0x20; - }).base(), - s.end()); + }).base(), s.end()); } - inline void trim(std::string &s) { + template + inline void trim(std::basic_string &s) { trimLeft(s); trimRight(s); } diff --git a/lib/libimhex/source/helpers/fs.cpp b/lib/libimhex/source/helpers/fs.cpp index 19906d737..2bb3bd155 100644 --- a/lib/libimhex/source/helpers/fs.cpp +++ b/lib/libimhex/source/helpers/fs.cpp @@ -5,6 +5,7 @@ #include #include #include +#include #include @@ -27,23 +28,29 @@ namespace hex::fs { if (GetModuleFileNameW(nullptr, exePath.data(), exePath.length()) == 0) return std::nullopt; + hex::trim(exePath); + return exePath; #elif defined(OS_LINUX) std::string exePath(PATH_MAX, '\0'); if (readlink("/proc/self/exe", exePath.data(), PATH_MAX) < 0) return std::nullopt; + hex::trim(exePath); + return exePath; #elif defined(OS_MACOS) - std::string result; + std::string exePath; { auto string = getMacExecutableDirectoryPath(); - result = string; + exePath = string; macFree(string); } - return result; + hex::trim(exePath); + + return exePath; #else return std::nullopt; #endif diff --git a/main/source/init/tasks.cpp b/main/source/init/tasks.cpp index e20d1f13f..cae18f373 100644 --- a/main/source/init/tasks.cpp +++ b/main/source/init/tasks.cpp @@ -75,7 +75,7 @@ namespace hex::init { // Check if ImHex is installed in portable mode { if (const auto executablePath = fs::getExecutablePath(); executablePath.has_value()) { - const auto flagFile = executablePath.value() / "PORTABLE"; + const auto flagFile = executablePath->parent_path() / "PORTABLE"; if (fs::exists(flagFile) && fs::isRegularFile(flagFile)) ImHexApi::System::impl::setPortableVersion(true);