diff --git a/io.github.openbrickprotocolfoundation.oopetris.yml b/io.github.openbrickprotocolfoundation.oopetris.yml index 25239e42..6e318b8e 100644 --- a/io.github.openbrickprotocolfoundation.oopetris.yml +++ b/io.github.openbrickprotocolfoundation.oopetris.yml @@ -1,6 +1,6 @@ app-id: io.github.openbrickprotocolfoundation.oopetris runtime: org.freedesktop.Platform -runtime-version: "24.08" +runtime-version: '24.08' sdk: org.freedesktop.Sdk command: oopetris modules: @@ -24,13 +24,14 @@ modules: - -Dbuild_installer=true - --libdir=lib - -Dtests=true + - --force-fallback-for=fmt # note, the freedesktop sdk has this installed, but it is not copiable into the runtime, so we need to build it ourself, to be able to install it correctly builddir: true build-options: build-args: - --share=network sources: - type: dir - path: "." + path: '.' skip: - .github/ - .vscode/ diff --git a/src/executables/game/main.cpp b/src/executables/game/main.cpp index 46acced5..75bf9949 100644 --- a/src/executables/game/main.cpp +++ b/src/executables/game/main.cpp @@ -34,8 +34,11 @@ namespace { void initialize_spdlog() { const auto logs_path = utils::get_root_folder() / "logs"; - if (not std::filesystem::exists(logs_path)) { - std::filesystem::create_directory(logs_path); + + auto created_log_dir = utils::create_directory(logs_path, true); + if (created_log_dir.has_value()) { + std::cerr << "warning: couldn't create logs directory '" << logs_path.string() + << "': disabled file logger\n"; } std::vector sinks; @@ -46,9 +49,13 @@ namespace { #else sinks.push_back(std::make_shared()); #endif - sinks.push_back(std::make_shared( - fmt::format("{}/oopetris.log", logs_path.string()), 1024 * 1024 * 10, 5, true - )); + + if (not created_log_dir.has_value()) { + sinks.push_back(std::make_shared( + fmt::format("{}/oopetris.log", logs_path.string()), 1024 * 1024 * 10, 5, true + )); + } + auto combined_logger = std::make_shared("combined_logger", begin(sinks), end(sinks)); spdlog::set_default_logger(combined_logger); diff --git a/src/helper/graphic_utils.cpp b/src/helper/graphic_utils.cpp index d25bb41e..b57a015c 100644 --- a/src/helper/graphic_utils.cpp +++ b/src/helper/graphic_utils.cpp @@ -147,6 +147,32 @@ utils::ExitException::ExitException(int status_code) noexcept : m_status_code{ s return "An exit exception occurred"; } +std::optional utils::create_directory(const std::filesystem::path& folder, bool recursive) { + + if (std::filesystem::exists(folder)) { + return std::nullopt; + } + + try { + if (recursive) { + auto result = std::filesystem::create_directories(folder); + if (not result) { + return "an unknown error occurred"; + } + return std::nullopt; + } + + + auto result = std::filesystem::create_directory(folder); + if (not result) { + return "an unknown error occurred"; + } + return std::nullopt; + } catch (const std::exception& error) { + return error.what(); + } +} + void utils::exit(int status_code) { #if defined(__ANDROID__) // calling exit() in android doesn't do the correct job, it completely avoids resource cleanup by the underlying SDLActivity.java diff --git a/src/helper/graphic_utils.hpp b/src/helper/graphic_utils.hpp index 0a32b835..0c525a98 100644 --- a/src/helper/graphic_utils.hpp +++ b/src/helper/graphic_utils.hpp @@ -7,6 +7,7 @@ #include "helper/constants.hpp" #include +#include #include namespace utils { @@ -34,6 +35,9 @@ namespace utils { }; + OOPETRIS_GRAPHICS_EXPORTED [[nodiscard]] std::optional + create_directory(const std::filesystem::path& folder, bool recursive); + // this needs some special handling, so the macro is defined here #if defined(_MSC_VER) #if defined(OOPETRIS_LIBRARY_GRAPHICS_TYPE) && OOPETRIS_LIBRARY_GRAPHICS_TYPE == 0 diff --git a/src/input/input_creator.cpp b/src/input/input_creator.cpp index ac1b5697..93a3e8f4 100644 --- a/src/input/input_creator.cpp +++ b/src/input/input_creator.cpp @@ -13,6 +13,7 @@ #include "input_creator.hpp" #include +#include #include namespace { @@ -123,32 +124,34 @@ input::get_game_parameters_for_replay( const auto recording_directory_path = utils::get_root_folder() / constants::recordings_directory; - if (not std::filesystem::exists(recording_directory_path)) { - std::filesystem::create_directory(recording_directory_path); - } - const auto date_time_str = date.to_string(); + auto dir_result = utils::create_directory(recording_directory_path, true); + if (not dir_result.has_value()) { - if (not date_time_str.has_value()) { - throw std::runtime_error{ fmt::format("Erro in date to string conversion: {}", date_time_str.error()) }; - } + const auto date_time_str = date.to_string(); - const auto filename = fmt::format("{}.{}", date_time_str.value(), constants::recording::extension); - const auto file_path = recording_directory_path / filename; + if (not date_time_str.has_value()) { + throw std::runtime_error{ fmt::format("Erro in date to string conversion: {}", date_time_str.error()) }; + } + const auto filename = fmt::format("{}.{}", date_time_str.value(), constants::recording::extension); + const auto file_path = recording_directory_path / filename; - auto recording_writer_create_result = - recorder::RecordingWriter::get_writer(file_path, std::move(tetrion_headers), std::move(information)); - if (not recording_writer_create_result.has_value()) { - throw std::runtime_error(recording_writer_create_result.error()); - } - const auto recording_writer = - std::make_shared(std::move(recording_writer_create_result.value())); + auto recording_writer_create_result = + recorder::RecordingWriter::get_writer(file_path, std::move(tetrion_headers), std::move(information)); + if (not recording_writer_create_result.has_value()) { + throw std::runtime_error(recording_writer_create_result.error()); + } + const auto recording_writer = + std::make_shared(std::move(recording_writer_create_result.value())); - std::get<1>(result).recording_writer = recording_writer; + std::get<1>(result).recording_writer = recording_writer; + } else { + spdlog::warn("Couldn't create recordings folder {}: skipping creation of a recording", dir_result.value()); + } return result; } diff --git a/src/manager/settings_manager.cpp b/src/manager/settings_manager.cpp index ff1d99ad..883655e1 100644 --- a/src/manager/settings_manager.cpp +++ b/src/manager/settings_manager.cpp @@ -14,12 +14,14 @@ SettingsManager::SettingsManager(ServiceProvider* service_provider) : m_service_ if (result.has_value()) { m_settings = result.value(); } else { - spdlog::error("unable to load settings from \"{}\": {}", detail::settings_filename, result.error()); + spdlog::warn("unable to load settings from \"{}\": {}", detail::settings_filename, result.error()); spdlog::warn("applying default settings"); m_settings = { - detail::Settings{ {}, std::nullopt, 1.0, false } + detail::Settings{ .controls = {}, .selected = std::nullopt, .volume = 1.0, .discord = false } }; + + //TODO(Totto): save the file, if it doesn't exist, if it has an error, just leave it there } }