Skip to content

Commit

Permalink
Add interface to add lookup paths
Browse files Browse the repository at this point in the history
  • Loading branch information
Atraxus committed Oct 8, 2024
1 parent f4626cf commit 68c3324
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 21 deletions.
45 changes: 32 additions & 13 deletions Intern/rayx-core/src/Data/Locate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include <iostream>
#include <filesystem>
#include <vector>
#include <algorithm>

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

namespace RAYX {

// Adds a new lookup path where the handler will search for resources
void ResourceHandler::addLookUpPath(const std::filesystem::path& path) {
// Check if the path is already in the lookUpPaths
auto it = std::find(lookUpPaths.begin(), lookUpPaths.end(), path);
if (it == lookUpPaths.end()) {
// Insert at the beginning to prioritize newly added paths
lookUpPaths.insert(lookUpPaths.begin(), path);
}
}

// Check if a file exists
bool ResourceHandler::fileExists(const std::string& path) { return std::filesystem::exists(path); }
bool ResourceHandler::fileExists(const std::filesystem::path& path) { return std::filesystem::exists(path); }
Expand Down Expand Up @@ -53,33 +64,41 @@ std::filesystem::path ResourceHandler::getExecutablePath() {
}

#else
throw std::runtime_error("getExecutablePath is not implemented for this platform");
static_assert(false, "macOS support is not implemented yet");
#endif
return std::filesystem::path(buffer.data());
}

// General method to get the full path based on the base directory (e.g., data or font directory)
std::filesystem::path ResourceHandler::getFullPath(const std::string& baseDir, const std::string& relativePath) {
// First, check in user-defined lookup paths
for (const auto& lookupPath : lookUpPaths) {
std::filesystem::path path = lookupPath / baseDir / relativePath;
if (fileExists(path)) {
return path;
}
}

#if defined(__linux__)
// First, check in /usr (package install)
std::string path = std::string("/usr/") + baseDir + "/" + relativePath;
// Check in /usr (package install)
std::filesystem::path path = std::filesystem::path("/usr") / baseDir / relativePath;
if (fileExists(path)) return path;

// Next, check next to the executable (built from source)
std::string execDir = getExecutablePath().string();
execDir = execDir.substr(0, execDir.find_last_of("/\\"));
path = execDir + "/" + relativePath;
// Check next to the executable (built from source)
std::filesystem::path execDir = getExecutablePath().parent_path();
path = execDir / relativePath;
if (fileExists(path)) return path;

// Lastly, check in /usr/local (make install)
path = std::string("/usr/local/") + baseDir + "/" + relativePath;
// Check in /usr/local (make install)
path = std::filesystem::path("/usr/local") / baseDir / relativePath;
if (fileExists(path)) return path;

#elif defined(_WIN32)
// On Windows, only look next to the executable
std::string execDir = getExecutablePath().string();
execDir = execDir.substr(0, execDir.find_last_of("/\\"));
std::filesystem::path path = std::filesystem::path(execDir + "\\" + relativePath);
std::filesystem::path execDir = getExecutablePath().parent_path();
std::filesystem::path path = execDir / relativePath;
if (fileExists(path)) return path;

#elif defined(__APPLE__)
static_assert(false, "macOS support is not implemented yet");

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

} // namespace RAYX
} // namespace RAYX
14 changes: 6 additions & 8 deletions Intern/rayx-core/src/Data/Locate.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

#include <filesystem>
#include <string>
#include <set>
#include <vector>

#include "Core.h"

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

// Adds a new lookup path where the handler will search for resources
void addLookUpPath(const std::filesystem::path& path);

private:
ResourceHandler() = default;
bool fileExists(const std::string& path);
bool fileExists(const std::filesystem::path& path);
std::filesystem::path getFullPath(const std::string& baseDir, const std::string& relativePath);

std::set<std::filesystem::path> lookUpPaths = {
#if defined(__linux__)
std::filesystem::path("/usr"), //
std::filesystem::path("/usr/local"), //
#endif
};
std::vector<std::filesystem::path> lookUpPaths; // Maintains insertion order
};

} // namespace RAYX
} // namespace RAYX

0 comments on commit 68c3324

Please sign in to comment.