From 75af9b5dc19a85513a100e3d6708bc305b10e986 Mon Sep 17 00:00:00 2001 From: silverweed Date: Fri, 6 Sep 2024 09:03:12 +0200 Subject: [PATCH] use std::filesystem::create_directories in lif::createDirIfNotExisting This automatically handles the creation of the whole directory tree instead of failing if the parent directories don't exist, and it works on all platforms the same way. The downside is that we now need C++17 to build the game, but at this point it's probably safe to assume it's supported on all the platforms we care about. --- CMakeLists.txt | 2 +- src/core/utils.cpp | 11 +++-------- src/core/utils.hpp | 3 ++- src/main.cpp | 6 ++++-- 4 files changed, 10 insertions(+), 12 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 164f1292..c9de4223 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -108,7 +108,7 @@ endif() add_executable(${PROJECT_NAME} ${LIFISH_SRC}) set_target_properties(${PROJECT_NAME} PROPERTIES - CXX_STANDARD 14 + CXX_STANDARD 17 CXX_STANDARD_REQUIRED YES CXX_EXTENSIONS NO LINKER_LANGUAGE CXX diff --git a/src/core/utils.cpp b/src/core/utils.cpp index f0927087..4642dcc0 100644 --- a/src/core/utils.cpp +++ b/src/core/utils.cpp @@ -72,12 +72,7 @@ sf::View lif::keepRatio(const sf::Vector2f& size, const sf::Vector2u& designedsi return view; } -bool lif::createDirIfNotExisting(const std::string& path) { -#if defined(_WIN32) || defined(__MINGW32__) - bool ok = !!CreateDirectory(path.c_str(), NULL); - return ok || GetLastError() == ERROR_ALREADY_EXISTS; -#else - bool ok = mkdir(path.c_str(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH) == 0; - return ok || errno == EEXIST; -#endif +bool lif::createDirIfNotExisting(const std::filesystem::path& path) { + bool ok = std::filesystem::create_directories(path); + return ok; } diff --git a/src/core/utils.hpp b/src/core/utils.hpp index 855c9350..84d1c2f7 100644 --- a/src/core/utils.hpp +++ b/src/core/utils.hpp @@ -8,6 +8,7 @@ #include #include #include +#include // Enable automatic sf::Time / json conversion: // https://github.com/nlohmann/json#basic-usage @@ -226,6 +227,6 @@ void testMusic(); sf::View keepRatio(const sf::Vector2f& size, const sf::Vector2u& designedsize); /** Note: this will NOT create a directory recursively */ -bool createDirIfNotExisting(const std::string& path); +bool createDirIfNotExisting(const std::filesystem::path& path); } // end namespace lif diff --git a/src/main.cpp b/src/main.cpp index 54b194ae..ccfddd24 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -309,8 +309,10 @@ int main(int argc, char **argv) { if (ui.mustSaveGame()) { const auto saveName = ui.getSaveName() + ".lifish"; - lif::SaveManager::saveGame(saveName, game->getLM()); - std::cerr << "Saved game in " << saveName << "." << std::endl; + if (lif::SaveManager::saveGame(saveName, game->getLM())) + std::cerr << "Saved game in " << saveName << "." << std::endl; + else + std::cerr << "Failed to save game in " << saveName << "." << std::endl; } ///// LOGIC LOOP /////