Skip to content
This repository was archived by the owner on Mar 11, 2025. It is now read-only.

Commit 4640372

Browse files
authored
Add Tkge::Utilities::GetCurrentExecutablePath() (#25)
* Add Tkge::Utilities::GetCurrentExecutablePath() * [[nodiscard]] * linux support * [[maybe_unused]] * stupid space * static_cast
1 parent 4023a39 commit 4640372

File tree

4 files changed

+64
-18
lines changed

4 files changed

+64
-18
lines changed

App/Src/Main.cpp

+3-2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
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>
@@ -107,12 +108,12 @@ namespace
107108
}
108109
} // namespace
109110

110-
int main([[maybe_unused]] int argc, char** argv)
111+
int main([[maybe_unused]] int argc, [[maybe_unused]] char** argv)
111112
{
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)

Lib/Include/Tkge/Utilities.hpp

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
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+
[[nodiscard]] static std::filesystem::path GetCurrentExecutablePath();
23+
};
24+
} // namespace Tkge

Lib/Src/AssetLoader.cpp

+2-16
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,15 @@
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-
135
std::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
}

Lib/Src/Utilities.cpp

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
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+
#else // defined(_WIN32)
11+
#include <limits.h>
12+
#include <unistd.h>
13+
#endif
14+
15+
std::filesystem::path Tkge::Utilities::GetCurrentExecutablePath()
16+
{
17+
static std::filesystem::path CachePath{}; // can never change throughout the process existance
18+
if (!CachePath.empty()) return CachePath;
19+
20+
#ifdef _WIN32
21+
char buffer[MAX_PATH]{};
22+
DWORD length = GetModuleFileNameA(nullptr, buffer, MAX_PATH);
23+
if (length == 0) return {}; // Error case
24+
CachePath = std::filesystem::path{std::string(buffer, length)}.parent_path();
25+
#elif defined(__linux__)
26+
char buffer[PATH_MAX]{};
27+
ssize_t length = readlink("/proc/self/exe", buffer, PATH_MAX);
28+
if (length == -1) return {}; // Error case
29+
CachePath = std::filesystem::path{std::string(buffer, static_cast<std::size_t>(length))}.parent_path();
30+
#else
31+
static_assert(false, "Unsupported platform");
32+
#endif
33+
34+
return CachePath;
35+
}

0 commit comments

Comments
 (0)