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

Add Tkge::Utilities::GetCurrentExecutablePath() #25

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions App/Src/Main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include <Tkge/Engine.hpp>
#include <Tkge/Graphics/Drawable.hpp>
#include <Tkge/Graphics/Shader.hpp>
#include <Tkge/Utilities.hpp>
#include <klib/assert.hpp>
#include <kvf/time.hpp>
#include <cmath>
Expand Down Expand Up @@ -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)
Expand Down
24 changes: 24 additions & 0 deletions Lib/Include/Tkge/Utilities.hpp
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
18 changes: 2 additions & 16 deletions Lib/Src/AssetLoader.cpp
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;
}
35 changes: 35 additions & 0 deletions Lib/Src/Utilities.cpp
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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should probably use #elif defined(__linux__) here. Then the only error will be the static assertion instead of "can't find includes" etc.

#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;
}