diff --git a/FlingEngine/Resources/src/FlingPaths.cpp b/FlingEngine/Resources/src/FlingPaths.cpp index 694aa223..787bde70 100644 --- a/FlingEngine/Resources/src/FlingPaths.cpp +++ b/FlingEngine/Resources/src/FlingPaths.cpp @@ -34,11 +34,13 @@ namespace Fling void FlingPaths::GetCurrentWorkingDir(char* t_OutBuf, size_t t_BufSize) { + // Normalize CWD to the directory that contains this executable. + // Shipping builds use relative paths (Config/, Assets/, Logs/), so + // loading must not depend on whichever directory the process was + // launched from (IDE vs shell vs CI). #if FLING_WINDOWS { - // Get the real, full path to this executable, end the string before - // the filename itself and then set that as the current directory - GetModuleFileName(0, t_OutBuf, t_BufSize); + GetModuleFileName(0, t_OutBuf, static_cast(t_BufSize)); char* lastSlash = strrchr(t_OutBuf, '\\'); if (lastSlash) { @@ -48,19 +50,27 @@ namespace Fling } #elif FLING_LINUX { - if (getcwd(t_OutBuf, t_BufSize) != nullptr) + const ssize_t Len = readlink("/proc/self/exe", t_OutBuf, t_BufSize - 1); + if (Len == -1) { - F_LOG_TRACE("Current working dir: {}\n", t_OutBuf); + F_LOG_FATAL("readlink(/proc/self/exe) error"); + return; } - else + + t_OutBuf[Len] = '\0'; + char* lastSlash = strrchr(t_OutBuf, '/'); + if (lastSlash) { - F_LOG_FATAL("getcwd() error"); + *lastSlash = 0; } - if (chdir(t_OutBuf) == -1) + if (chdir(t_OutBuf) == -1) { - F_LOG_FATAL("chdir() error :"); + F_LOG_FATAL("chdir() error"); + return; } + + F_LOG_TRACE("Current working dir: {}", t_OutBuf); } #endif // FLING_LINUX } diff --git a/FlingTests/CMakeLists.txt b/FlingTests/CMakeLists.txt index a88c5f51..f4c27229 100644 --- a/FlingTests/CMakeLists.txt +++ b/FlingTests/CMakeLists.txt @@ -60,4 +60,14 @@ endif() add_executable( ${PROJECT_NAME} ${_source_list} ) # Link Catch2 and everything else -target_link_libraries( ${PROJECT_NAME} LINK_PUBLIC ${LINK_LIBS} ) \ No newline at end of file +target_link_libraries( ${PROJECT_NAME} LINK_PUBLIC ${LINK_LIBS} ) + +# Shipping builds resolve EngineConfigDir() to a relative "Config" path, and +# ResourceManager::Init() chdirs to the executable directory. Keep TestConf.ini +# next to the binary so FlingTests pass regardless of launch CWD. +add_custom_command( TARGET ${PROJECT_NAME} POST_BUILD + COMMAND ${CMAKE_COMMAND} -E copy_directory + "${FLING_ROOT_DIR}/Config" + "$/Config" + COMMENT "Copying Config next to ${PROJECT_NAME}" +) \ No newline at end of file diff --git a/FlingTests/src/ResourceTests.cpp b/FlingTests/src/ResourceTests.cpp index 9602c54e..1d7e6161 100644 --- a/FlingTests/src/ResourceTests.cpp +++ b/FlingTests/src/ResourceTests.cpp @@ -14,15 +14,13 @@ TEST_CASE("Engine Config File", "[resource]") using namespace Fling; // Logger HAS to be initalized first Logger::Get().Init(); + // Sets CWD to the executable directory so relative shipping paths resolve ResourceManager::Get().Init(); FlingConfig::Get().Init(); - SECTION("Valid Config") - { - // Load a test config - bool ConfigLoaded = FlingConfig::Get().LoadConfigFile(FlingPaths::EngineConfigDir() + "/TestConf.ini"); - REQUIRE(ConfigLoaded); - } + // Load once for all sections (Catch re-runs this block per SECTION) + bool ConfigLoaded = FlingConfig::Get().LoadConfigFile(FlingPaths::EngineConfigDir() + "/TestConf.ini"); + REQUIRE(ConfigLoaded); SECTION("Read False Bool") { @@ -58,4 +56,4 @@ TEST_CASE("Engine Config File", "[resource]") ResourceManager::Get().Shutdown(); Logger::Get().Shutdown(); FlingConfig::Get().Shutdown(); -} \ No newline at end of file +}