diff --git a/CMakeLists.txt b/CMakeLists.txt index 02b9d8c..6ba8116 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -82,7 +82,7 @@ if(USING_VCPKG) target_link_libraries(${PROJECT_NAME} PUBLIC unofficial::maddy::maddy) endif() if(WIN32) - target_link_libraries(${PROJECT_NAME} PUBLIC Advapi32 Dwmapi Gdiplus Shell32) + target_link_libraries(${PROJECT_NAME} PUBLIC Advapi32 Dwmapi Gdiplus Kernel32 Shell32) elseif(LINUX) set(THREADS_PREFER_PTHREAD_FLAG ON) find_package(Threads REQUIRED) diff --git a/docs/aura.md b/docs/aura.md index 09caecf..ed6004d 100644 --- a/docs/aura.md +++ b/docs/aura.md @@ -168,6 +168,10 @@ Path: `Nickvision::Aura::Aura` ``` - Returns: The reference to the singleton `Aura` object. - Throws: `std::logic_error` if `Aura::init()` was not yet called. +- ```cpp + std::filesystem::path getExecutableDirectory() + ``` + - Returns: The path of the executable's directory. - ```cpp std::string getEnvVar(const std::string& key) ``` diff --git a/include/aura/aura.h b/include/aura/aura.h index e940de0..11bf5a0 100644 --- a/include/aura/aura.h +++ b/include/aura/aura.h @@ -86,6 +86,11 @@ namespace Nickvision::Aura * @return The active aura instance */ static Aura& getActive(); + /** + * @brief Gets the path of the executable's directory. + * @return The executable's directory path + */ + static std::filesystem::path getExecutableDirectory() noexcept; /** * @brief Gets a system environment variable. * @param key The environment variable to get diff --git a/src/aura/aura.cpp b/src/aura/aura.cpp index 22a0ffa..ac3cd2f 100644 --- a/src/aura/aura.cpp +++ b/src/aura/aura.cpp @@ -6,7 +6,9 @@ #include "filesystem/systemdirectories.h" #include "localization/gettext.h" #include "helpers/stringhelpers.h" -#ifdef __linux__ +#ifdef _WIN32 +#include +#elif defined(__linux__) #include #endif @@ -63,6 +65,21 @@ namespace Nickvision::Aura return *m_instance; } + std::filesystem::path Aura::getExecutableDirectory() noexcept + { +#ifdef _WIN32 + char pth[MAX_PATH]; + DWORD len{ GetModuleFileNameA(nullptr, pth, sizeof(pth)) }; + if(len > 0) + { + return std::filesystem::path(std::string(pth, len)).parent_path(); + } +#elif defined(__linux__) + return std::filesystem::canonical("/proc/self/exe").parent_path(); +#endif + return {}; + } + std::string Aura::getEnvVar(const std::string& key) noexcept { char* var{ std::getenv(key.c_str()) }; @@ -102,7 +119,7 @@ namespace Nickvision::Aura } } locations[dependency] = std::filesystem::path(); - std::filesystem::path path{ std::filesystem::current_path() / dependency }; + std::filesystem::path path{ getExecutableDirectory() / dependency }; if (std::filesystem::exists(path)) { locations[dependency] = path; diff --git a/src/localization/gettext.cpp b/src/localization/gettext.cpp index ef196ff..de5223c 100644 --- a/src/localization/gettext.cpp +++ b/src/localization/gettext.cpp @@ -1,6 +1,7 @@ #include "localization/gettext.h" #include #include +#include "aura/aura.h" namespace Nickvision::Localization { @@ -15,9 +16,9 @@ namespace Nickvision::Localization setlocale(LC_ALL, ""); m_domainName = domainName; #ifdef _WIN32 - res = res && (wbindtextdomain(m_domainName.c_str(), std::filesystem::current_path().c_str()) != nullptr); + res = res && (wbindtextdomain(m_domainName.c_str(), Aura::Aura::getExecutableDirectory().c_str()) != nullptr); #elif defined(__linux__) - res = res && (bindtextdomain(m_domainName.c_str(), std::filesystem::current_path().c_str()) != nullptr); + res = res && (bindtextdomain(m_domainName.c_str(), Aura::Aura::getExecutableDirectory().c_str()) != nullptr); res = res && (bind_textdomain_codeset(m_domainName.c_str(), "UTF-8") != nullptr); #endif res = res && (textdomain(m_domainName.c_str()) != nullptr); diff --git a/tests/auratests.cpp b/tests/auratests.cpp index e21d538..544be4f 100644 --- a/tests/auratests.cpp +++ b/tests/auratests.cpp @@ -45,6 +45,7 @@ class AuraTest : public testing::Test TEST_F(AuraTest, EnsureAura) { ASSERT_NO_THROW(Aura::getActive()); + std::cout << Aura::getExecutableDirectory() << std::endl; } TEST_F(AuraTest, SetAppInfo) diff --git a/tests/stringtests.cpp b/tests/stringtests.cpp index 01a2f8d..2cdc068 100644 --- a/tests/stringtests.cpp +++ b/tests/stringtests.cpp @@ -54,6 +54,7 @@ TEST(StringTests, Guid1) ASSERT_NO_THROW(s = StringHelpers::newGuid()); ASSERT_FALSE(s.empty()); ASSERT_TRUE(s.size() == 36); + std::cout << s << std::endl; } TEST(StringTests, UrlValidity1)