This repository was archived by the owner on Mar 11, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Add Tkge::Utilities::GetCurrentExecutablePath() #25
Merged
TymianekPL
merged 6 commits into
main
from
tymii/23-utility-function-to-obtain-the-executable-path
Feb 17, 2025
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
bbe4efd
Add Tkge::Utilities::GetCurrentExecutablePath()
TymianekPL d782c5b
[[nodiscard]]
TymianekPL 45c0ed6
linux support
TymianekPL 6be397e
[[maybe_unused]]
TymianekPL 1266c17
stupid space
TymianekPL 3494102
static_cast
TymianekPL File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
#pragma once | ||
|
||
#include <filesystem> | ||
#include <string> | ||
|
||
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; | ||
|
||
/// <summary> | ||
/// Gets the current executable directory. | ||
/// </summary> | ||
/// <returns>The parent directory of the main module</returns> | ||
[[nodiscard]] static std::filesystem::path GetCurrentExecutablePath(); | ||
}; | ||
} // namespace Tkge |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,15 @@ | ||
#include <Tkge/AssetLoader.hpp> | ||
#include <Tkge/Utilities.hpp> | ||
#include <filesystem> | ||
|
||
#ifdef _WIN32 | ||
#ifndef WIN32_MEAN_AND_LEAN | ||
#define WIN32_MEAN_AND_LEAN | ||
#define _AMD64_ | ||
#endif | ||
#include <libloaderapi.h> | ||
#include <minwindef.h> | ||
#endif | ||
|
||
std::vector<std::filesystem::path> Tkge::AssetLoader::GetSearchPaths() const | ||
{ | ||
std::vector<std::filesystem::path> 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; | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
#include <Tkge/Utilities.hpp> | ||
|
||
#ifdef _WIN32 | ||
#ifndef WIN32_MEAN_AND_LEAN | ||
#define WIN32_MEAN_AND_LEAN | ||
#define _AMD64_ | ||
#endif | ||
#include <libloaderapi.h> | ||
#include <minwindef.h> | ||
#else // defined(_WIN32) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should probably use |
||
#include <limits.h> | ||
#include <unistd.h> | ||
#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<std::size_t>(length))}.parent_path(); | ||
#else | ||
static_assert(false, "Unsupported platform"); | ||
#endif | ||
|
||
return CachePath; | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.