Skip to content

Commit 5f8f392

Browse files
committed
Add interface to add lookup paths
1 parent 013593f commit 5f8f392

File tree

2 files changed

+38
-21
lines changed

2 files changed

+38
-21
lines changed

Intern/rayx-core/src/Data/Locate.cpp

Lines changed: 32 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#include <iostream>
33
#include <filesystem>
44
#include <vector>
5+
#include <algorithm>
56

67
#if defined(_WIN32)
78
#include <windows.h>
@@ -14,6 +15,16 @@
1415

1516
namespace RAYX {
1617

18+
// Adds a new lookup path where the handler will search for resources
19+
void ResourceHandler::addLookUpPath(const std::filesystem::path& path) {
20+
// Check if the path is already in the lookUpPaths
21+
auto it = std::find(lookUpPaths.begin(), lookUpPaths.end(), path);
22+
if (it == lookUpPaths.end()) {
23+
// Insert at the beginning to prioritize newly added paths
24+
lookUpPaths.insert(lookUpPaths.begin(), path);
25+
}
26+
}
27+
1728
// Check if a file exists
1829
bool ResourceHandler::fileExists(const std::string& path) { return std::filesystem::exists(path); }
1930
bool ResourceHandler::fileExists(const std::filesystem::path& path) { return std::filesystem::exists(path); }
@@ -53,33 +64,41 @@ std::filesystem::path ResourceHandler::getExecutablePath() {
5364
}
5465

5566
#else
56-
throw std::runtime_error("getExecutablePath is not implemented for this platform");
67+
static_assert(false, "macOS support is not implemented yet");
5768
#endif
5869
return std::filesystem::path(buffer.data());
5970
}
6071

6172
// General method to get the full path based on the base directory (e.g., data or font directory)
6273
std::filesystem::path ResourceHandler::getFullPath(const std::string& baseDir, const std::string& relativePath) {
74+
// First, check in user-defined lookup paths
75+
for (const auto& lookupPath : lookUpPaths) {
76+
std::filesystem::path path = lookupPath / baseDir / relativePath;
77+
if (fileExists(path)) {
78+
return path;
79+
}
80+
}
81+
6382
#if defined(__linux__)
64-
// First, check in /usr (package install)
65-
std::string path = std::string("/usr/") + baseDir + "/" + relativePath;
83+
// Check in /usr (package install)
84+
std::filesystem::path path = std::filesystem::path("/usr") / baseDir / relativePath;
6685
if (fileExists(path)) return path;
6786

68-
// Next, check next to the executable (built from source)
69-
std::string execDir = getExecutablePath().string();
70-
execDir = execDir.substr(0, execDir.find_last_of("/\\"));
71-
path = execDir + "/" + relativePath;
87+
// Check next to the executable (built from source)
88+
std::filesystem::path execDir = getExecutablePath().parent_path();
89+
path = execDir / relativePath;
7290
if (fileExists(path)) return path;
7391

74-
// Lastly, check in /usr/local (make install)
75-
path = std::string("/usr/local/") + baseDir + "/" + relativePath;
92+
// Check in /usr/local (make install)
93+
path = std::filesystem::path("/usr/local") / baseDir / relativePath;
7694
if (fileExists(path)) return path;
95+
7796
#elif defined(_WIN32)
7897
// On Windows, only look next to the executable
79-
std::string execDir = getExecutablePath().string();
80-
execDir = execDir.substr(0, execDir.find_last_of("/\\"));
81-
std::filesystem::path path = std::filesystem::path(execDir + "\\" + relativePath);
98+
std::filesystem::path execDir = getExecutablePath().parent_path();
99+
std::filesystem::path path = execDir / relativePath;
82100
if (fileExists(path)) return path;
101+
83102
#elif defined(__APPLE__)
84103
static_assert(false, "macOS support is not implemented yet");
85104

@@ -94,4 +113,4 @@ std::filesystem::path ResourceHandler::getResourcePath(const std::string& relati
94113
// Retrieve the full path of a font based on the platform
95114
std::filesystem::path ResourceHandler::getFontPath(const std::string& relativePath) { return getFullPath(RAYX_FONTS_DIR, relativePath); }
96115

97-
} // namespace RAYX
116+
} // namespace RAYX

Intern/rayx-core/src/Data/Locate.h

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
#include <filesystem>
44
#include <string>
5-
#include <set>
5+
#include <vector>
66

77
#include "Core.h"
88

@@ -24,18 +24,16 @@ class RAYX_API ResourceHandler {
2424
// Retrieves a font file's full path based on the relative path
2525
std::filesystem::path getFontPath(const std::string& relativePath);
2626

27+
// Adds a new lookup path where the handler will search for resources
28+
void addLookUpPath(const std::filesystem::path& path);
29+
2730
private:
2831
ResourceHandler() = default;
2932
bool fileExists(const std::string& path);
3033
bool fileExists(const std::filesystem::path& path);
3134
std::filesystem::path getFullPath(const std::string& baseDir, const std::string& relativePath);
3235

33-
std::set<std::filesystem::path> lookUpPaths = {
34-
#if defined(__linux__)
35-
std::filesystem::path("/usr"), //
36-
std::filesystem::path("/usr/local"), //
37-
#endif
38-
};
36+
std::vector<std::filesystem::path> lookUpPaths; // Maintains insertion order
3937
};
4038

41-
} // namespace RAYX
39+
} // namespace RAYX

0 commit comments

Comments
 (0)