From 461d3ec6949b9a4a1ef5082ea10112ff1e1852d5 Mon Sep 17 00:00:00 2001 From: Patrick Wuttke Date: Sun, 19 Jan 2025 21:24:38 +0100 Subject: [PATCH] Added getExecutablePath(). Still need to check if the Windows version works, but who cares? --- source/mijin/util/os.cpp | 32 +++++++++++++++++++++++++++++++- source/mijin/util/os.hpp | 1 + 2 files changed, 32 insertions(+), 1 deletion(-) diff --git a/source/mijin/util/os.cpp b/source/mijin/util/os.cpp index 31d0feb..9d2d98a 100644 --- a/source/mijin/util/os.cpp +++ b/source/mijin/util/os.cpp @@ -8,7 +8,9 @@ #include #include #elif MIJIN_TARGET_OS == MIJIN_OS_WINDOWS -// TODO + #include + #include + #include "../util/winundef.hpp" #endif namespace fs = std::filesystem; @@ -97,6 +99,34 @@ void setCurrentThreadName(const char* threadName) MIJIN_NOEXCEPT #endif } +std::string getExecutablePath() MIJIN_NOEXCEPT +{ +#if MIJIN_TARGET_OS == MIJIN_OS_LINUX + return fs::canonical("/proc/self/exe").string(); +#elif MIJIN_TARGET_OS == MIJIN_OS_WINDOWS + // TODO: this is completely untested + std::array buffer; + if (FAILED(GetModuleFileNameA(nullptr, buffer.data(), buffer.size()))) + { + std::vector buffer2; + buffer2.resize(1024); + do + { + if (buffer2.size() >= 10240) + { + MIJIN_ERROR("Something is wrong."); + return ""; + } + buffer2.resize(buffer2.size() + 1024); + } + while (FAILED(GetModuleFileNameA(nullptr, buffer.data(), buffer.size()))); + return buffer2.data(); + } + return buffer.data(); +#else +#endif +} + [[nodiscard]] std::string makeLibraryFilename(std::string_view libraryName) MIJIN_NOEXCEPT { #if MIJIN_TARGET_OS == MIJIN_OS_LINUX diff --git a/source/mijin/util/os.hpp b/source/mijin/util/os.hpp index 7744681..b071d66 100644 --- a/source/mijin/util/os.hpp +++ b/source/mijin/util/os.hpp @@ -77,6 +77,7 @@ public: bool closeSharedLibrary(const LibraryHandle library) MIJIN_NOEXCEPT; [[nodiscard]] void* loadSymbolFromLibrary(const LibraryHandle library, const char* symbolName) MIJIN_NOEXCEPT; void setCurrentThreadName(const char* threadName) MIJIN_NOEXCEPT; +[[nodiscard]] std::string getExecutablePath() MIJIN_NOEXCEPT; [[nodiscard]] std::string makeLibraryFilename(std::string_view libraryName) MIJIN_NOEXCEPT;