diff --git a/App/Src/Main.cpp b/App/Src/Main.cpp index deeb249..becaf7b 100644 --- a/App/Src/Main.cpp +++ b/App/Src/Main.cpp @@ -2,6 +2,7 @@ #include #include #include +#include #include #include #include @@ -107,12 +108,12 @@ namespace } } // namespace -int main([[maybe_unused]] int argc, char** argv) +int main([[maybe_unused]] int argc, [[maybe_unused]] 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..9d951f6 --- /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 + [[nodiscard]] static 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..be944e9 --- /dev/null +++ b/Lib/Src/Utilities.cpp @@ -0,0 +1,35 @@ +#include + +#ifdef _WIN32 +#ifndef WIN32_MEAN_AND_LEAN +#define WIN32_MEAN_AND_LEAN +#define _AMD64_ +#endif +#include +#include +#else // defined(_WIN32) +#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, static_cast(length))}.parent_path(); +#else + static_assert(false, "Unsupported platform"); +#endif + + return CachePath; +}