-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Correct paths for data files and interface to find them
Update version to v0.21.28 Update version to v0.21.29 Update version to v0.21.30 Update version to v0.21.31 Update version to v0.21.32 Update version to v0.21.33 Update version to v0.21.34 Update version to v0.21.35 Update version to v0.21.36 Update version to v0.21.37 Update version to v0.21.38 Update version to v0.21.39 Update version to v0.21.40 Update version to v0.21.41
- Loading branch information
Showing
31 changed files
with
215 additions
and
98 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
#include "Locate.h" | ||
#include <iostream> | ||
#include <filesystem> | ||
#include <vector> | ||
|
||
#if defined(_WIN32) | ||
#include <windows.h> | ||
#elif defined(__APPLE__) | ||
#include <cassert> | ||
#else | ||
#include <unistd.h> | ||
#include <limits.h> | ||
#endif | ||
|
||
namespace RAYX { | ||
|
||
// 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); } | ||
|
||
std::filesystem::path ResourceHandler::getExecutablePath() { | ||
#if defined(_WIN32) | ||
// Start with a reasonable buffer size | ||
std::vector<char> buffer(MAX_PATH); | ||
while (true) { | ||
// Try to get the module filename | ||
DWORD size = GetModuleFileNameA(NULL, buffer.data(), buffer.size()); | ||
if (size == 0) { | ||
return std::filesystem::path(); | ||
} | ||
// If the buffer was large enough, we're done | ||
if (size < buffer.size()) { | ||
break; | ||
} | ||
// Otherwise, increase buffer size and try again | ||
buffer.resize(buffer.size() * 2); | ||
} | ||
#elif defined(__linux__) | ||
// Start with a reasonable buffer size, then grow dynamically if needed | ||
std::vector<char> buffer(PATH_MAX); | ||
ssize_t count; | ||
while (true) { | ||
count = readlink("/proc/self/exe", buffer.data(), buffer.size()); | ||
if (count == -1) { | ||
return std::filesystem::path(); | ||
} | ||
if (count < static_cast<ssize_t>(buffer.size())) { | ||
buffer[count] = '\0'; // Null-terminate | ||
break; | ||
} | ||
// If the buffer was too small, increase size and retry | ||
buffer.resize(buffer.size() * 2); | ||
} | ||
|
||
#else | ||
throw std::runtime_error("getExecutablePath is not implemented for this platform"); | ||
#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) { | ||
#if defined(__linux__) | ||
// First, check in /usr (package install) | ||
std::string path = std::string("/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; | ||
if (fileExists(path)) return path; | ||
|
||
// Lastly, check in /usr/local (make install) | ||
path = std::string("/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); | ||
if (fileExists(path)) return path; | ||
#elif defined(__APPLE__) | ||
static_assert(false, "macOS support is not implemented yet"); | ||
|
||
#endif | ||
// Not found -> empty path | ||
return std::filesystem::path(); | ||
} | ||
|
||
// Retrieve the full path of a resource based on the platform | ||
std::filesystem::path ResourceHandler::getResourcePath(const std::string& relativePath) { return getFullPath(RAYX_DATA_DIR, relativePath); } | ||
|
||
// 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
#pragma once | ||
|
||
#include <filesystem> | ||
#include <string> | ||
#include <set> | ||
|
||
#include "Core.h" | ||
|
||
namespace RAYX { | ||
|
||
class RAYX_API ResourceHandler { | ||
public: | ||
static ResourceHandler& getInstance() { | ||
thread_local ResourceHandler instance; | ||
return instance; | ||
} | ||
|
||
// Platform-specific implementations for resource path search | ||
std::filesystem::path getExecutablePath(); | ||
|
||
// Retrieves a resource file's full path based on the relative path | ||
std::filesystem::path getResourcePath(const std::string& relativePath); | ||
|
||
// Retrieves a font file's full path based on the relative path | ||
std::filesystem::path getFontPath(const std::string& relativePath); | ||
|
||
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 | ||
}; | ||
}; | ||
|
||
} // namespace RAYX |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes
Oops, something went wrong.