VEX Engine
Retro looking game engine made in vulkan mostly because i wanted to understand it better.
Loading...
Searching...
No Matches
PathUtils.cpp
1#include "components/PathUtils.hpp"
2#include "components/ErrorUtils.hpp"
3
4#ifdef _WIN32
5#include <windows.h>
6#elif defined(__APPLE__)
7#include <mach-o/dyld.h>
8#include <sys/param.h>
9#else
10#include <unistd.h>
11#endif
12
13namespace vex {
14
15static std::string gAssetRootOverride = "";
16static std::string gUserDataDir = "";
17
18void SetAssetRoot(const std::string& projectPath) {
19 gAssetRootOverride = projectPath;
20}
21
22void SetUserDataDir(const std::string& userDataDir) {
23 if (!std::filesystem::exists(userDataDir)) {
24 std::filesystem::create_directories(userDataDir);
25 }
26 gUserDataDir = userDataDir;
27}
28
29std::string GetAssetDir() {
30 return gAssetRootOverride;
31}
32
33std::string GetUserDataDir() {
34 return gUserDataDir;
35}
36
37std::filesystem::path GetExecutableDir() {
38 std::filesystem::path exePath;
39
40#ifdef _WIN32
41 wchar_t wPath[MAX_PATH];
42 DWORD len = GetModuleFileNameW(NULL, wPath, MAX_PATH);
43 if (len == 0 || len == MAX_PATH) {
44 vex::throw_error("Failed to get executable path on Windows");
45 }
46 int utf8Size = WideCharToMultiByte(CP_UTF8, 0, wPath, -1, nullptr, 0, nullptr, nullptr);
47 if (utf8Size == 0) {
48 vex::throw_error("Failed to convert executable path to UTF-8");
49 }
50 std::string utf8Path(utf8Size, '\0');
51 WideCharToMultiByte(CP_UTF8, 0, wPath, -1, utf8Path.data(), utf8Size, nullptr, nullptr);
52 exePath = std::filesystem::path(utf8Path);
53#elif defined(__APPLE__)
54 char pathBuf[MAXPATHLEN];
55 uint32_t size = sizeof(pathBuf);
56 if (_NSGetExecutablePath(pathBuf, &size) != 0) {
57 vex::throw_error("Failed to get executable path on macOS");
58 }
59 exePath = std::filesystem::canonical(pathBuf);
60#else
61 char pathBuf[1024];
62 ssize_t len = ::readlink("/proc/self/exe", pathBuf, sizeof(pathBuf) - 1);
63 if (len == -1) {
64 vex::throw_error("Failed to read /proc/self/exe on Linux");
65 }
66 pathBuf[len] = '\0';
67 exePath = std::filesystem::path(pathBuf);
68#endif
69
70 return exePath.parent_path();
71}
72
73std::string GetAssetPath(const std::string& relativePath) {
74
75 const std::string& overridePath = gAssetRootOverride;
76 if (!overridePath.empty()) {
77 if(relativePath.contains(overridePath)){
78 return relativePath;
79 }
80 return (std::filesystem::path(overridePath) / relativePath).string();
81 }
82
83 #if DEBUG
84 static std::filesystem::path exeDir = GetExecutableDir();
85 return (exeDir / relativePath).string();
86 #else
87 return relativePath;
88 #endif
89}
90
91std::string GetUserDataPath(const std::string& relativePath) {
92 return (std::filesystem::path(gUserDataDir) / relativePath).string();
93}
94
95std::filesystem::path GetLogDir() {
96 std::filesystem::path logDir;
97 try {
98 logDir = GetUserDataPath("logs");
99 if (!std::filesystem::exists(logDir)) {
100 std::filesystem::create_directories(logDir);
101 }
102 return logDir;
103 } catch (...) {
104 return std::filesystem::current_path();
105 }
106}
107
108} // namespace vex
std::string VEX_EXPORT GetAssetDir()
Gets the current asset directory override.
Definition PathUtils.cpp:29
std::filesystem::path VEX_EXPORT GetExecutableDir()
Retrieves the directory containing the current executable.
Definition PathUtils.cpp:37
void VEX_EXPORT SetAssetRoot(const std::string &projectPath)
Overrides the default asset root directory.
Definition PathUtils.cpp:18
std::filesystem::path VEX_EXPORT GetLogDir()
Gets the directory where logs and crash dumps are stored.
Definition PathUtils.cpp:95
void VEX_EXPORT throw_error(const std::string &msg)
Throws an error or terminates the application.
std::string VEX_EXPORT GetAssetPath(const std::string &relativePath)
Resolves the absolute path of an asset based on the current build configuration.
Definition PathUtils.cpp:73
std::string VEX_EXPORT GetUserDataDir()
Gets the current user data directory.
Definition PathUtils.cpp:33
void VEX_EXPORT SetUserDataDir(const std::string &userDataDir)
Sets the current user data directory.
Definition PathUtils.cpp:22
std::string VEX_EXPORT GetUserDataPath(const std::string &relativePath)
Resolves the absolute path of a user data file based on the current user data directory.
Definition PathUtils.cpp:91