Skip to content

Commit fea08fb

Browse files
committed
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
1 parent b35a5d2 commit fea08fb

31 files changed

+215
-98
lines changed

CMakeLists.txt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
cmake_minimum_required(VERSION 3.25.2 FATAL_ERROR)
22

33
# ---- Project ----
4-
project(RAYX VERSION 0.21.27)
4+
project(RAYX VERSION 0.21.41)
55
set(CMAKE_CXX_STANDARD 23)
66
set(CMAKE_CXX_STANDARD_REQUIRED ON)
77
set(CMAKE_CUDA_STANDARD 20)
@@ -38,8 +38,8 @@ elseif(UNIX AND NOT APPLE)
3838
set(INSTALL_DATA_DIR "share/${PROJECT_NAME}")
3939
set(INSTALL_FONTS_DIR "share/fonts/${PROJECT_NAME}")
4040
elseif(WIN32)
41-
set(INSTALL_DATA_DIR "Data")
42-
set(INSTALL_FONTS_DIR "Fonts")
41+
set(INSTALL_DATA_DIR ".")
42+
set(INSTALL_FONTS_DIR ".")
4343
endif()
4444
# ------------------
4545

Intern/rayx-core/CMakeLists.txt

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,10 @@ endif()
134134

135135
target_compile_definitions(${PROJECT_NAME}
136136
PRIVATE RAYX_BUILD_DLL
137+
RAYX_INSTALL_PREFIX="${CMAKE_INSTALL_PREFIX}"
138+
RAYX_PACKAGE_INSTALL_DIR="${CPACK_PACKAGE_INSTALL_DIRECTORY}"
139+
RAYX_DATA_DIR="${INSTALL_DATA_DIR}"
140+
RAYX_FONTS_DIR="${INSTALL_FONTS_DIR}"
137141
PUBLIC ${COMPILE_PLATFORM}
138142
RAYX_PROJECT_DIR="${CMAKE_SOURCE_DIR}"
139143
$<$<CONFIG:Debug>:RAYX_DEBUG_MODE>
@@ -208,17 +212,25 @@ add_custom_command(
208212
# -----------------
209213

210214
# ---- CPack ----
211-
install(TARGETS ${PROJECT_NAME}
212-
RUNTIME DESTINATION bin
213-
LIBRARY DESTINATION lib
214-
ARCHIVE DESTINATION lib
215-
)
216-
install(DIRECTORY ${CMAKE_SOURCE_DIR}/Data
217-
DESTINATION ${INSTALL_DATA_DIR}
218-
FILES_MATCHING PATTERN "*")
215+
if(APPLE OR UNIX)
216+
install(TARGETS ${PROJECT_NAME}
217+
RUNTIME DESTINATION bin
218+
LIBRARY DESTINATION lib
219+
ARCHIVE DESTINATION lib
220+
)
221+
elseif(WIN32)
222+
install(TARGETS ${PROJECT_NAME}
223+
RUNTIME DESTINATION .
224+
LIBRARY DESTINATION .
225+
ARCHIVE DESTINATION .
226+
)
227+
endif()
228+
install(DIRECTORY ${CMAKE_SOURCE_DIR}/Data/nff
229+
DESTINATION ${INSTALL_DATA_DIR}/Data)
230+
install(DIRECTORY ${CMAKE_SOURCE_DIR}/Data/PALIK
231+
DESTINATION ${INSTALL_DATA_DIR}/Data)
219232
install(DIRECTORY ${CMAKE_SOURCE_DIR}/Scripts
220-
DESTINATION ${INSTALL_DATA_DIR}
221-
FILES_MATCHING PATTERN "*")
233+
DESTINATION ${INSTALL_DATA_DIR})
222234
include(InstallRequiredSystemLibraries)
223235
include(CPack)
224236
# -----------------

Intern/rayx-core/src/CanonicalizePath.cpp

Lines changed: 0 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -4,50 +4,9 @@
44
#include <stdexcept>
55

66
#include "Debug/Debug.h"
7-
#if defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(__NT__)
8-
#include <windows.h>
9-
#elif defined(__linux__) || defined(__unix__) || defined(_POSIX_VERSION)
10-
#include <limits.h>
11-
#include <unistd.h>
12-
#elif defined(__APPLE__)
13-
#include <mach-o/dyld.h>
14-
#endif
157

168
namespace RAYX {
179

18-
std::filesystem::path getExecutablePath() {
19-
char buffer[1024];
20-
uint32_t length = static_cast<uint32_t>(sizeof(buffer));
21-
memset(buffer, 0, length);
22-
23-
#if defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(__NT__)
24-
// Windows implementation
25-
DWORD ret = GetModuleFileNameA(NULL, buffer, length);
26-
if (ret == 0 || ret == length) {
27-
// Handle error
28-
throw std::runtime_error("Failed to get executable path.");
29-
}
30-
#elif defined(__linux__) || defined(__unix__) || defined(_POSIX_VERSION)
31-
// Linux and Unix implementation
32-
ssize_t ret = readlink("/proc/self/exe", buffer, length - 1);
33-
if (ret == -1) {
34-
// Handle error
35-
throw std::runtime_error("Failed to get executable path.");
36-
}
37-
buffer[ret] = '\0'; // Ensure null-terminated string
38-
#elif defined(__APPLE__)
39-
// macOS implementation
40-
if (_NSGetExecutablePath(buffer, &length) != 0) {
41-
// Handle error
42-
throw std::runtime_error("Buffer too small; should not happen.");
43-
}
44-
#else
45-
#error "Platform not supported."
46-
#endif
47-
48-
return std::filesystem::path(buffer).parent_path();
49-
}
50-
5110
/// this function assumes that `base` is already an absolute path
5211
std::filesystem::path canonicalize(const std::filesystem::path& relPath, const std::filesystem::path& base) {
5312
if (!base.is_absolute()) {

Intern/rayx-core/src/CanonicalizePath.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,6 @@
88

99
namespace RAYX {
1010

11-
/// Returns the path to the directory containing the executable.
12-
std::filesystem::path RAYX_API getExecutablePath();
13-
1411
/// `relPath` is a path relative to the root of the RAY-X git repository (i.e.
1512
/// where .git lies). canonicalizeRepositoryPath(relPath) yields an absolute
1613
/// path representing the same path. Examples:
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
#include "Locate.h"
2+
#include <iostream>
3+
#include <filesystem>
4+
#include <vector>
5+
6+
#if defined(_WIN32)
7+
#include <windows.h>
8+
#elif defined(__APPLE__)
9+
#include <cassert>
10+
#else
11+
#include <unistd.h>
12+
#include <limits.h>
13+
#endif
14+
15+
namespace RAYX {
16+
17+
// Check if a file exists
18+
bool ResourceHandler::fileExists(const std::string& path) { return std::filesystem::exists(path); }
19+
bool ResourceHandler::fileExists(const std::filesystem::path& path) { return std::filesystem::exists(path); }
20+
21+
std::filesystem::path ResourceHandler::getExecutablePath() {
22+
#if defined(_WIN32)
23+
// Start with a reasonable buffer size
24+
std::vector<char> buffer(MAX_PATH);
25+
while (true) {
26+
// Try to get the module filename
27+
DWORD size = GetModuleFileNameA(NULL, buffer.data(), buffer.size());
28+
if (size == 0) {
29+
return std::filesystem::path();
30+
}
31+
// If the buffer was large enough, we're done
32+
if (size < buffer.size()) {
33+
break;
34+
}
35+
// Otherwise, increase buffer size and try again
36+
buffer.resize(buffer.size() * 2);
37+
}
38+
#elif defined(__linux__)
39+
// Start with a reasonable buffer size, then grow dynamically if needed
40+
std::vector<char> buffer(PATH_MAX);
41+
ssize_t count;
42+
while (true) {
43+
count = readlink("/proc/self/exe", buffer.data(), buffer.size());
44+
if (count == -1) {
45+
return std::filesystem::path();
46+
}
47+
if (count < static_cast<ssize_t>(buffer.size())) {
48+
buffer[count] = '\0'; // Null-terminate
49+
break;
50+
}
51+
// If the buffer was too small, increase size and retry
52+
buffer.resize(buffer.size() * 2);
53+
}
54+
55+
#else
56+
throw std::runtime_error("getExecutablePath is not implemented for this platform");
57+
#endif
58+
return std::filesystem::path(buffer.data());
59+
}
60+
61+
// General method to get the full path based on the base directory (e.g., data or font directory)
62+
std::filesystem::path ResourceHandler::getFullPath(const std::string& baseDir, const std::string& relativePath) {
63+
#if defined(__linux__)
64+
// First, check in /usr (package install)
65+
std::string path = std::string("/usr/") + baseDir + "/" + relativePath;
66+
if (fileExists(path)) return path;
67+
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;
72+
if (fileExists(path)) return path;
73+
74+
// Lastly, check in /usr/local (make install)
75+
path = std::string("/usr/local/") + baseDir + "/" + relativePath;
76+
if (fileExists(path)) return path;
77+
#elif defined(_WIN32)
78+
// 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);
82+
if (fileExists(path)) return path;
83+
#elif defined(__APPLE__)
84+
static_assert(false, "macOS support is not implemented yet");
85+
86+
#endif
87+
// Not found -> empty path
88+
return std::filesystem::path();
89+
}
90+
91+
// Retrieve the full path of a resource based on the platform
92+
std::filesystem::path ResourceHandler::getResourcePath(const std::string& relativePath) { return getFullPath(RAYX_DATA_DIR, relativePath); }
93+
94+
// Retrieve the full path of a font based on the platform
95+
std::filesystem::path ResourceHandler::getFontPath(const std::string& relativePath) { return getFullPath(RAYX_FONTS_DIR, relativePath); }
96+
97+
} // namespace RAYX

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

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#pragma once
2+
3+
#include <filesystem>
4+
#include <string>
5+
#include <set>
6+
7+
#include "Core.h"
8+
9+
namespace RAYX {
10+
11+
class RAYX_API ResourceHandler {
12+
public:
13+
static ResourceHandler& getInstance() {
14+
thread_local ResourceHandler instance;
15+
return instance;
16+
}
17+
18+
// Platform-specific implementations for resource path search
19+
std::filesystem::path getExecutablePath();
20+
21+
// Retrieves a resource file's full path based on the relative path
22+
std::filesystem::path getResourcePath(const std::string& relativePath);
23+
24+
// Retrieves a font file's full path based on the relative path
25+
std::filesystem::path getFontPath(const std::string& relativePath);
26+
27+
private:
28+
ResourceHandler() = default;
29+
bool fileExists(const std::string& path);
30+
bool fileExists(const std::filesystem::path& path);
31+
std::filesystem::path getFullPath(const std::string& baseDir, const std::string& relativePath);
32+
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+
};
39+
};
40+
41+
} // namespace RAYX

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,11 +180,18 @@ bool paramPositionNoGroup(const rapidxml::xml_node<>* node, glm::dvec4* out) {
180180

181181
std::filesystem::path Parser::parseEnergyDistributionFile() const {
182182
std::filesystem::path datpath = Parser::parseStr("photonEnergyDistributionFile");
183+
// Check if the path is empty
184+
if (datpath.empty()) {
185+
// Since the photon energy distribution file is optional, return an empty path
186+
RAYX_VERB << "No photon energy distribution file specified.";
187+
return std::filesystem::path(); // Or handle as per your application's logic
188+
}
189+
183190
std::filesystem::path combinedPath = rmlFile.parent_path() / datpath;
184191
try {
185192
combinedPath = std::filesystem::canonical(combinedPath);
186193
} catch (const std::exception& e) {
187-
RAYX_EXIT << "Failed to canonicalize datfile path: " << e.what();
194+
RAYX_EXIT << "Failed to canonicalize datfile path: " << combinedPath.string() << " -- Error: " << e.what();
188195
}
189196

190197
RAYX_VERB << "Combined datfile path: " << combinedPath;

Intern/rayx-core/src/Material/NffTable.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
#include <algorithm>
55
#include <fstream>
66

7-
#include "CanonicalizePath.h"
7+
#include "Data/Locate.h"
88
#include "Debug/Debug.h"
99

1010
namespace RAYX {
@@ -14,7 +14,7 @@ bool NffTable::load(const char* element, NffTable* out) {
1414

1515
std::transform(elementString.begin(), elementString.end(), elementString.begin(), [](unsigned char c) { return std::tolower(c); });
1616

17-
std::string f = getExecutablePath().string() + "/Data/nff/" + elementString + ".nff";
17+
std::filesystem::path f = ResourceHandler::getInstance().getResourcePath("Data/nff/" + elementString + ".nff");
1818
RAYX_VERB << "Loading NffTable from " << f;
1919
std::ifstream s(f);
2020

@@ -39,7 +39,7 @@ bool NffTable::load(const char* element, NffTable* out) {
3939
#else
4040
if (sscanf(line.c_str(), "%le %le %le", &e.m_energy, &e.m_f1, &e.m_f2) != 3) {
4141
#endif
42-
RAYX_EXIT << "Failed to parse NffTable\"" << element << "\", at line " << lineidx << ": \"" << line << "\"";
42+
RAYX_WARN << "Failed to parse NffTable\"" << element << "\", at line " << lineidx << ": \"" << line << "\"";
4343
return false;
4444
}
4545
out->m_Lines.push_back(e);

Intern/rayx-core/src/Material/PalikTable.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
#include <algorithm>
44
#include <fstream>
55

6-
#include "CanonicalizePath.h"
6+
#include "Data/Locate.h"
77
#include "Debug/Debug.h"
88

99
namespace RAYX {
@@ -12,7 +12,7 @@ bool PalikTable::load(const char* element, PalikTable* out) {
1212
std::string elementString = element;
1313
std::transform(elementString.begin(), elementString.end(), elementString.begin(), [](unsigned char c) { return std::toupper(c); });
1414

15-
std::string f = getExecutablePath().string() + "/Data/PALIK/" + elementString + ".NKP";
15+
std::filesystem::path f = ResourceHandler::getInstance().getResourcePath("Data/PALIK/" + elementString + ".NKP");
1616
RAYX_VERB << "Loading PalikTable from " << f;
1717
std::ifstream s(f);
1818

@@ -39,7 +39,7 @@ bool PalikTable::load(const char* element, PalikTable* out) {
3939
#else
4040
if (sscanf(line.c_str(), "%le %le %le", &e.m_energy, &e.m_n, &e.m_k) != 3) {
4141
#endif
42-
RAYX_EXIT << "Failed to parse PalikTable \"" << element << "\", at line " << lineidx << ": \"" << line << "\"";
42+
RAYX_WARN << "Failed to parse PalikTable \"" << element << "\", at line " << lineidx << ": \"" << line << "\"";
4343
return false;
4444
}
4545
out->m_Lines.push_back(e);

Intern/rayx-ui/CMakeLists.txt

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,9 @@ if(Vulkan_FOUND)
4141
GLM_FORCE_DEPTH_ZERO_TO_ONE)
4242

4343
if(CMAKE_BUILD_TYPE STREQUAL "Debug")
44-
set(RESOURCE_DIR ${CMAKE_RUNTIME_OUTPUT_DIRECTORY_DEBUG}/Assets)
44+
set(RESOURCE_DIR ${CMAKE_RUNTIME_OUTPUT_DIRECTORY_DEBUG})
4545
else()
46-
set(RESOURCE_DIR ${CMAKE_RUNTIME_OUTPUT_DIRECTORY_RELEASE}/Assets)
46+
set(RESOURCE_DIR ${CMAKE_RUNTIME_OUTPUT_DIRECTORY_RELEASE})
4747
endif()
4848

4949
message(STATUS "Resource directory: ${RESOURCE_DIR}")
@@ -56,26 +56,25 @@ if(Vulkan_FOUND)
5656

5757

5858
# ---- CPack ----
59-
install(TARGETS ${PROJECT_NAME} RUNTIME DESTINATION bin)
59+
if(APPLE OR UNIX)
60+
install(TARGETS ${PROJECT_NAME} RUNTIME DESTINATION bin)
61+
elseif(WIN32)
62+
install(TARGETS ${PROJECT_NAME} RUNTIME DESTINATION .)
63+
endif()
6064

6165
if(CMAKE_BUILD_TYPE STREQUAL "Debug")
6266
install(DIRECTORY ${CMAKE_RUNTIME_OUTPUT_DIRECTORY_DEBUG}/Shaders
63-
DESTINATION ${INSTALL_DATA_DIR}
64-
FILES_MATCHING PATTERN "*_*.spv")
65-
install(DIRECTORY ${CMAKE_RUNTIME_OUTPUT_DIRECTORY_DEBUG}/Assets
66-
DESTINATION ${INSTALL_DATA_DIR}
67-
PATTERN "fonts" EXCLUDE)
68-
install(DIRECTORY ${CMAKE_RUNTIME_OUTPUT_DIRECTORY_DEBUG}/Assets/fonts
69-
DESTINATION ${INSTALL_FONTS_DIR}
70-
FILES_MATCHING PATTERN "*")
67+
DESTINATION ${INSTALL_DATA_DIR})
68+
install(DIRECTORY ${CMAKE_RUNTIME_OUTPUT_DIRECTORY_DEBUG}/Textures
69+
DESTINATION ${INSTALL_DATA_DIR})
70+
install(DIRECTORY ${CMAKE_RUNTIME_OUTPUT_DIRECTORY_DEBUG}/Fonts
71+
DESTINATION ${INSTALL_FONTS_DIR})
7172
else()
7273
install(DIRECTORY ${CMAKE_RUNTIME_OUTPUT_DIRECTORY_RELEASE}/Shaders
73-
DESTINATION ${INSTALL_DATA_DIR}
74-
FILES_MATCHING PATTERN "*_*.spv")
75-
install(DIRECTORY ${CMAKE_RUNTIME_OUTPUT_DIRECTORY_RELEASE}/Assets
76-
DESTINATION ${INSTALL_DATA_DIR}
77-
PATTERN "fonts" EXCLUDE)
78-
install(DIRECTORY ${CMAKE_RUNTIME_OUTPUT_DIRECTORY_RELEASE}/Assets/fonts
74+
DESTINATION ${INSTALL_DATA_DIR})
75+
install(DIRECTORY ${CMAKE_RUNTIME_OUTPUT_DIRECTORY_RELEASE}/Textures
76+
DESTINATION ${INSTALL_DATA_DIR})
77+
install(DIRECTORY ${CMAKE_RUNTIME_OUTPUT_DIRECTORY_RELEASE}/Fonts
7978
DESTINATION ${INSTALL_FONTS_DIR})
8079
endif()
8180

0 commit comments

Comments
 (0)