From e83a3f640f90b96430edde7e20d6127531250c5e Mon Sep 17 00:00:00 2001 From: Jacob Jensen Date: Mon, 16 Feb 2015 20:52:43 +0100 Subject: [PATCH] Game now creates the necessary folders for saving creations which fixes previous crashes. --- src/engine/engine.cpp | 42 +++++++++++++++++++++++++----------------- 1 file changed, 25 insertions(+), 17 deletions(-) diff --git a/src/engine/engine.cpp b/src/engine/engine.cpp index 26165cb038..8e0b36cb8a 100644 --- a/src/engine/engine.cpp +++ b/src/engine/engine.cpp @@ -732,25 +732,33 @@ Engine::saveCreation( ) const { namespace fs = boost::filesystem; StorageContainer creation = entityManager.storeEntity(entityId); - creation.set("thriveversion", this->thriveVersion()); - std::ofstream stream( - (fs::path("creations") / fs::path(type) / fs::path(name + "." + type)).string(), - std::ofstream::trunc | std::ofstream::binary - ); - stream.exceptions(std::ofstream::failbit | std::ofstream::badbit); - if (stream) { - try { - stream << creation; - stream.flush(); - stream.close(); - } - catch (const std::ofstream::failure& e) { - std::cerr << "Error saving file: " << e.what() << std::endl; - throw; - } + fs::path pth = (fs::path("creations") / fs::path(type)); + boost::system::error_code returnedError; + boost::filesystem::create_directories( pth, returnedError ); + if (returnedError) { + std::perror("Could not create necessary directories for saving."); } else { - std::perror("Could not open file for saving"); + creation.set("thriveversion", this->thriveVersion()); + std::ofstream stream( + (pth / fs::path(name + "." + type)).string(), + std::ofstream::trunc | std::ofstream::binary + ); + stream.exceptions(std::ofstream::failbit | std::ofstream::badbit); + if (stream) { + try { + stream << creation; + stream.flush(); + stream.close(); + } + catch (const std::ofstream::failure& e) { + std::cerr << "Error saving file: " << e.what() << std::endl; + throw; + } + } + else { + std::perror("Could not open file for saving"); + } } }