22#include < iostream>
33#include < filesystem>
44#include < vector>
5+ #include < algorithm>
56
67#if defined(_WIN32)
78#include < windows.h>
1415
1516namespace 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
1829bool ResourceHandler::fileExists (const std::string& path) { return std::filesystem::exists (path); }
1930bool 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)
6273std::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
95114std::filesystem::path ResourceHandler::getFontPath (const std::string& relativePath) { return getFullPath (RAYX_FONTS_DIR, relativePath); }
96115
97- } // namespace RAYX
116+ } // namespace RAYX
0 commit comments