diff --git a/Common/Cpp/Filesystem/FilePath.h b/Common/Cpp/Filesystem/FilePath.h index 2f705c3248..b6e1e28bbe 100644 --- a/Common/Cpp/Filesystem/FilePath.h +++ b/Common/Cpp/Filesystem/FilePath.h @@ -52,6 +52,7 @@ class Path{ m_path.clear(); } + public: // Implicit conversion to the C++ std::filesystem::path so it can be passed to other library functions. operator const std::filesystem::path&() const{ @@ -62,6 +63,7 @@ class Path{ return m_path; } + public: bool empty() const{ return m_path.empty(); @@ -109,12 +111,18 @@ class Path{ return m_path.extension(); } + Path lexically_normal() const{ + return m_path.lexically_normal(); + } + + public: Path& replace_extension(const Path& replacement){ m_path.replace_extension(replacement); return *this; } + public: friend bool operator==(const Path& x, const Path& y){ return x.m_path == y.m_path; @@ -130,6 +138,7 @@ class Path{ friend std::ostream& operator<<(std::ostream& stream, const Path& x); + private: std::filesystem::path m_path; }; diff --git a/Common/Cpp/Filesystem/Filesystem.cpp b/Common/Cpp/Filesystem/Filesystem.cpp index a61f494c41..aa0c5ff454 100644 --- a/Common/Cpp/Filesystem/Filesystem.cpp +++ b/Common/Cpp/Filesystem/Filesystem.cpp @@ -30,6 +30,10 @@ namespace Filesystem{ +Path absolute(const Path& path){ + return std::filesystem::absolute(path.stdpath()); +} + bool exists(const Path& path){ return std::filesystem::exists(path.stdpath()); } diff --git a/Common/Cpp/Filesystem/Filesystem.h b/Common/Cpp/Filesystem/Filesystem.h index 30cadb0343..f68f73a59c 100644 --- a/Common/Cpp/Filesystem/Filesystem.h +++ b/Common/Cpp/Filesystem/Filesystem.h @@ -29,6 +29,8 @@ namespace PokemonAutomation{ namespace Filesystem{ +Path absolute(const Path& path); + // Whether a path exists. bool exists(const Path& path); diff --git a/Common/Cpp/Strings/StringTools.cpp b/Common/Cpp/Strings/StringTools.cpp index 470f7c47e3..f53ed5b082 100644 --- a/Common/Cpp/Strings/StringTools.cpp +++ b/Common/Cpp/Strings/StringTools.cpp @@ -13,7 +13,11 @@ namespace PokemonAutomation{ namespace StringTools{ -std::string replace(const std::string& str, const std::string& desired, const std::string& replace_with){ +std::string replace( + const std::string& str, + const std::string& desired, + const std::string& replace_with +){ std::string ret; size_t index = 0; while (index < str.size()){ diff --git a/Common/Cpp/Strings/StringTools.h b/Common/Cpp/Strings/StringTools.h index 603c2f346c..f7bccbd734 100644 --- a/Common/Cpp/Strings/StringTools.h +++ b/Common/Cpp/Strings/StringTools.h @@ -14,7 +14,11 @@ namespace PokemonAutomation{ namespace StringTools{ -std::string replace(const std::string& str, const std::string& desired, const std::string& replace_with); +std::string replace( + const std::string& str, + const std::string& desired, + const std::string& replace_with +); // Trim leading and trailing white spaces std::string strip(const std::string& str); diff --git a/SerialPrograms/Source/CommonFramework/GlobalAutoPaths.cpp b/SerialPrograms/Source/CommonFramework/GlobalAutoPaths.cpp index e32fcf63c2..3f46fef5b8 100644 --- a/SerialPrograms/Source/CommonFramework/GlobalAutoPaths.cpp +++ b/SerialPrograms/Source/CommonFramework/GlobalAutoPaths.cpp @@ -17,6 +17,7 @@ #ifdef __linux__ #include #include +#include "Common/Cpp/Strings/StringTools.h" #include "Common/Cpp/Filesystem/FileIO.h" #endif @@ -60,6 +61,7 @@ namespace{ + Filesystem::Path get_application_base_dir_path(){ #if defined(__linux__) // Check for AppImage environment variables to find the root directory, if running as an AppImage. @@ -93,10 +95,12 @@ Filesystem::Path get_application_base_dir_path(){ if (pre.size() < 5 || post.size() < 2){ continue; } - QString mountPoint = pre[4].replace(QStringLiteral("\\040"), QStringLiteral(" ")); - QString source = post[1].replace(QStringLiteral("\\040"), QStringLiteral(" ")); - if (mountPoint == QString::fromUtf8(app_dir) && source.endsWith(QStringLiteral(".AppImage"))){ - return QDir::cleanPath(QFileInfo(source).dir().absolutePath()).toStdString(); + std::string mount_point = StringTools::replace(pre[4].toStdString(), "\\040", " "); + std::string source = StringTools::replace(post[1].toStdString(), "\\040", " "); + if (mount_point == app_dir && source.ends_with(".AppImage")){ + Filesystem::Path dir_path = Filesystem::Path(source).parent_path(); + Filesystem::Path abs_path = Filesystem::absolute(dir_path); + return abs_path.lexically_normal(); } } }