This repository was archived by the owner on Mar 11, 2025. It is now read-only.
File tree Expand file tree Collapse file tree 4 files changed +60
-17
lines changed Expand file tree Collapse file tree 4 files changed +60
-17
lines changed Original file line number Diff line number Diff line change 22#include < Tkge/Engine.hpp>
33#include < Tkge/Graphics/Drawable.hpp>
44#include < Tkge/Graphics/Shader.hpp>
5+ #include < Tkge/Utilities.hpp>
56#include < klib/assert.hpp>
67#include < kvf/time.hpp>
78#include < cmath>
@@ -112,7 +113,7 @@ int main([[maybe_unused]] int argc, char** argv)
112113 try
113114 {
114115 KLIB_ASSERT (argc > 0 );
115- const auto assets_path = Upfind (*argv , " Assets" );
116+ const auto assets_path = Upfind (Tkge::Utilities::GetCurrentExecutablePath () , " Assets" );
116117 Run (assets_path);
117118 }
118119 catch (const std::exception& e)
Original file line number Diff line number Diff line change 1+ #pragma once
2+
3+ #include < filesystem>
4+ #include < string>
5+
6+ namespace Tkge
7+ {
8+ class Utilities
9+ {
10+ public:
11+ ~Utilities () = delete ;
12+ Utilities () = delete ;
13+ Utilities (const Utilities&) = delete ;
14+ Utilities (Utilities&) = delete ;
15+ Utilities& operator =(const Utilities&) = delete ;
16+ Utilities& operator =(Utilities&) = delete ;
17+
18+ // / <summary>
19+ // / Gets the current executable directory.
20+ // / </summary>
21+ // / <returns>The parent directory of the main module</returns>
22+ static [[nodiscard]] std::filesystem::path GetCurrentExecutablePath ();
23+ };
24+ } // namespace Tkge
Original file line number Diff line number Diff line change 11#include < Tkge/AssetLoader.hpp>
2+ #include < Tkge/Utilities.hpp>
23#include < filesystem>
34
4- #ifdef _WIN32
5- #ifndef WIN32_MEAN_AND_LEAN
6- #define WIN32_MEAN_AND_LEAN
7- #define _AMD64_
8- #endif
9- #include < libloaderapi.h>
10- #include < minwindef.h>
11- #endif
12-
135std::vector<std::filesystem::path> Tkge::AssetLoader::GetSearchPaths () const
146{
157 std::vector<std::filesystem::path> paths{};
168
179 paths.emplace_back (" ." );
1810
19- #ifdef _WIN32
20- char buffer[MAX_PATH]{};
21- GetModuleFileNameA (nullptr , buffer, MAX_PATH);
22-
23- paths.push_back (std::filesystem::path{buffer}.parent_path ());
11+ paths.push_back (Tkge::Utilities::GetCurrentExecutablePath ());
2412 paths.push_back (paths.back () / " Assets" );
25- #else
26- #endif
2713
2814 return paths;
2915}
Original file line number Diff line number Diff line change 1+ #include < Tkge/Utilities.hpp>
2+
3+ #ifdef _WIN32
4+ #ifndef WIN32_MEAN_AND_LEAN
5+ #define WIN32_MEAN_AND_LEAN
6+ #define _AMD64_
7+ #endif
8+ #include < libloaderapi.h>
9+ #include < minwindef.h>
10+ #endif
11+
12+ std::filesystem::path Tkge::Utilities::GetCurrentExecutablePath ()
13+ {
14+ static std::filesystem::path CachePath{}; // can never change throughout the process existance
15+ if (!CachePath.empty ()) return CachePath;
16+
17+ #ifdef _WIN32
18+ char buffer[MAX_PATH]{};
19+ DWORD length = GetModuleFileNameA (nullptr , buffer, MAX_PATH);
20+ if (length == 0 ) return {}; // Error case
21+ CachePath = std::filesystem::path{std::string (buffer, length)}.parent_path ();
22+ #elif defined(__linux__)
23+ char buffer[PATH_MAX]{};
24+ ssize_t length = readlink (" /proc/self/exe" , buffer, PATH_MAX);
25+ if (length == -1 ) return {}; // Error case
26+ CachePath = std::filesystem::path{std::string (buffer, length)}.parent_path ();
27+ #else
28+ static_assert (false , " Unsupported platform" );
29+ #endif
30+
31+ return CachePath;
32+ }
You can’t perform that action at this time.
0 commit comments