From bbe4efd42323ef1f3cc6eca9fb85be9f910c9163 Mon Sep 17 00:00:00 2001 From: TymianekPL Date: Mon, 17 Feb 2025 20:24:22 +0100 Subject: [PATCH 1/6] Add Tkge::Utilities::GetCurrentExecutablePath() --- App/Src/Main.cpp | 3 ++- Lib/Include/Tkge/Utilities.hpp | 24 ++++++++++++++++++++++++ Lib/Src/AssetLoader.cpp | 18 ++---------------- Lib/Src/Utilities.cpp | 32 ++++++++++++++++++++++++++++++++ 4 files changed, 60 insertions(+), 17 deletions(-) create mode 100644 Lib/Include/Tkge/Utilities.hpp create mode 100644 Lib/Src/Utilities.cpp diff --git a/App/Src/Main.cpp b/App/Src/Main.cpp index deeb249..e2ee597 100644 --- a/App/Src/Main.cpp +++ b/App/Src/Main.cpp @@ -2,6 +2,7 @@ #include #include #include +#include #include #include #include @@ -112,7 +113,7 @@ int main([[maybe_unused]] int argc, char** argv) try { KLIB_ASSERT(argc > 0); - const auto assets_path = Upfind(*argv, "Assets"); + const auto assets_path = Upfind(Tkge::Utilities::GetCurrentExecutablePath(), "Assets"); Run(assets_path); } catch (const std::exception& e) diff --git a/Lib/Include/Tkge/Utilities.hpp b/Lib/Include/Tkge/Utilities.hpp new file mode 100644 index 0000000..6f6af88 --- /dev/null +++ b/Lib/Include/Tkge/Utilities.hpp @@ -0,0 +1,24 @@ +#pragma once + +#include +#include + +namespace Tkge +{ + class Utilities + { + public: + ~Utilities() = delete; + Utilities() = delete; + Utilities(const Utilities&) = delete; + Utilities(Utilities&) = delete; + Utilities& operator=(const Utilities&) = delete; + Utilities& operator=(Utilities&) = delete; + + /// + /// Gets the current executable directory. + /// + /// The parent directory of the main module + static [[nodiscard]] std::filesystem::path GetCurrentExecutablePath(); + }; +} // namespace Tkge diff --git a/Lib/Src/AssetLoader.cpp b/Lib/Src/AssetLoader.cpp index 26f14ec..2e581ff 100644 --- a/Lib/Src/AssetLoader.cpp +++ b/Lib/Src/AssetLoader.cpp @@ -1,29 +1,15 @@ #include +#include #include -#ifdef _WIN32 -#ifndef WIN32_MEAN_AND_LEAN -#define WIN32_MEAN_AND_LEAN -#define _AMD64_ -#endif -#include -#include -#endif - std::vector Tkge::AssetLoader::GetSearchPaths() const { std::vector paths{}; paths.emplace_back("."); -#ifdef _WIN32 - char buffer[MAX_PATH]{}; - GetModuleFileNameA(nullptr, buffer, MAX_PATH); - - paths.push_back(std::filesystem::path{buffer}.parent_path()); + paths.push_back(Tkge::Utilities::GetCurrentExecutablePath()); paths.push_back(paths.back() / "Assets"); -#else -#endif return paths; } diff --git a/Lib/Src/Utilities.cpp b/Lib/Src/Utilities.cpp new file mode 100644 index 0000000..8d0bcef --- /dev/null +++ b/Lib/Src/Utilities.cpp @@ -0,0 +1,32 @@ +#include + +#ifdef _WIN32 +#ifndef WIN32_MEAN_AND_LEAN +#define WIN32_MEAN_AND_LEAN +#define _AMD64_ +#endif +#include +#include +#endif + +std::filesystem::path Tkge::Utilities::GetCurrentExecutablePath() +{ + static std::filesystem::path CachePath{}; // can never change throughout the process existance + if (!CachePath.empty()) return CachePath; + +#ifdef _WIN32 + char buffer[MAX_PATH]{}; + DWORD length = GetModuleFileNameA(nullptr, buffer, MAX_PATH); + if (length == 0) return {}; // Error case + CachePath = std::filesystem::path{std::string(buffer, length)}.parent_path(); +#elif defined(__linux__) + char buffer[PATH_MAX]{}; + ssize_t length = readlink("/proc/self/exe", buffer, PATH_MAX); + if (length == -1) return {}; // Error case + CachePath = std::filesystem::path{std::string(buffer, length)}.parent_path(); +#else + static_assert(false, "Unsupported platform"); +#endif + + return CachePath; +} From d782c5b94dee070ba0fdc79b4660be7e4bdd4afc Mon Sep 17 00:00:00 2001 From: TymianekPL Date: Mon, 17 Feb 2025 20:26:41 +0100 Subject: [PATCH 2/6] [[nodiscard]] --- Lib/Include/Tkge/Utilities.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib/Include/Tkge/Utilities.hpp b/Lib/Include/Tkge/Utilities.hpp index 6f6af88..9d951f6 100644 --- a/Lib/Include/Tkge/Utilities.hpp +++ b/Lib/Include/Tkge/Utilities.hpp @@ -19,6 +19,6 @@ namespace Tkge /// Gets the current executable directory. /// /// The parent directory of the main module - static [[nodiscard]] std::filesystem::path GetCurrentExecutablePath(); + [[nodiscard]] static std::filesystem::path GetCurrentExecutablePath(); }; } // namespace Tkge From 45c0ed65620a53665a2116c3ea07855bf002ed81 Mon Sep 17 00:00:00 2001 From: TymianekPL Date: Mon, 17 Feb 2025 20:29:48 +0100 Subject: [PATCH 3/6] linux support --- Lib/Src/Utilities.cpp | 3 +++ 1 file changed, 3 insertions(+) diff --git a/Lib/Src/Utilities.cpp b/Lib/Src/Utilities.cpp index 8d0bcef..6a2048b 100644 --- a/Lib/Src/Utilities.cpp +++ b/Lib/Src/Utilities.cpp @@ -7,6 +7,9 @@ #endif #include #include +#else // defined(_WIN32) +#include +#include #endif std::filesystem::path Tkge::Utilities::GetCurrentExecutablePath() From 6be397efe371e56b49ad8f95b23cd5ba22e64390 Mon Sep 17 00:00:00 2001 From: TymianekPL Date: Mon, 17 Feb 2025 20:30:27 +0100 Subject: [PATCH 4/6] [[maybe_unused]] --- App/Src/Main.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/App/Src/Main.cpp b/App/Src/Main.cpp index e2ee597..74d4e34 100644 --- a/App/Src/Main.cpp +++ b/App/Src/Main.cpp @@ -108,7 +108,7 @@ namespace } } // namespace -int main([[maybe_unused]] int argc, char** argv) +int main([[maybe_unused]] int argc, [[maybe_unused]] char** argv) { try { From 1266c1762b498e44be5f3731ce24f9505dbb7080 Mon Sep 17 00:00:00 2001 From: TymianekPL Date: Mon, 17 Feb 2025 20:31:11 +0100 Subject: [PATCH 5/6] stupid space --- App/Src/Main.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/App/Src/Main.cpp b/App/Src/Main.cpp index 74d4e34..becaf7b 100644 --- a/App/Src/Main.cpp +++ b/App/Src/Main.cpp @@ -108,7 +108,7 @@ namespace } } // namespace -int main([[maybe_unused]] int argc, [[maybe_unused]] char** argv) +int main([[maybe_unused]] int argc, [[maybe_unused]] char** argv) { try { From 3494102cd2685332f37403c9e1b5fc28d70b88fc Mon Sep 17 00:00:00 2001 From: TymianekPL Date: Mon, 17 Feb 2025 20:33:42 +0100 Subject: [PATCH 6/6] static_cast --- Lib/Src/Utilities.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib/Src/Utilities.cpp b/Lib/Src/Utilities.cpp index 6a2048b..be944e9 100644 --- a/Lib/Src/Utilities.cpp +++ b/Lib/Src/Utilities.cpp @@ -26,7 +26,7 @@ std::filesystem::path Tkge::Utilities::GetCurrentExecutablePath() char buffer[PATH_MAX]{}; ssize_t length = readlink("/proc/self/exe", buffer, PATH_MAX); if (length == -1) return {}; // Error case - CachePath = std::filesystem::path{std::string(buffer, length)}.parent_path(); + CachePath = std::filesystem::path{std::string(buffer, static_cast(length))}.parent_path(); #else static_assert(false, "Unsupported platform"); #endif