From 1d527898aa05aa84450c12edb30057c49b1a2539 Mon Sep 17 00:00:00 2001 From: frank Date: Fri, 21 Jul 2023 00:51:19 -0400 Subject: [PATCH] watch multiple config files for changes --- src/Configuration.cpp | 26 +++++++++++++++++--------- src/Configuration.hpp | 9 ++++----- src/Game.cpp | 8 ++++---- src/Game.hpp | 6 +++--- 4 files changed, 28 insertions(+), 21 deletions(-) diff --git a/src/Configuration.cpp b/src/Configuration.cpp index 8682763..2ca4053 100644 --- a/src/Configuration.cpp +++ b/src/Configuration.cpp @@ -93,7 +93,7 @@ void Configuration::set_defaults() }; config["configuration"] = { {"auto-refresh", false}, - {"auto-refresh-interval", 1.0} + {"auto-refresh-interval", 5.0} }; } @@ -178,7 +178,7 @@ void Configuration::merge(const char* path) merge(fs::path(path)); } -void Configuration::enable_auto_refresh(const fs::path& file_to_refresh, float interval) +void Configuration::enable_auto_refresh(const fs::path& file_to_refresh) { #ifndef __ANDROID__ /* Warn user if the file does not exist */ @@ -189,8 +189,8 @@ void Configuration::enable_auto_refresh(const fs::path& file_to_refresh, float i sb::Log::log(file_to_refresh, sb::Log::WARN); } #endif - this->file_to_refresh = file_to_refresh; - auto_refresher.frame_length(interval); + files_to_refresh.push_back(file_to_refresh); + auto_refresher.frame_length(config["configuration"]["auto-refresh-interval"].get()); auto_refresher.play(); } @@ -201,13 +201,21 @@ void Configuration::disable_auto_refresh() void Configuration::refresh() { - if (fs::exists(file_to_refresh) && fs::last_write_time(file_to_refresh) > config_file_modification_time) +#if !defined(__ANDROID__) + for (const fs::path& path : files_to_refresh) { - std::ostringstream message; - message << "config file modified, reloading " << file_to_refresh; - sb::Log::log(message, sb::Log::DEBUG); - merge(file_to_refresh); + if (fs::exists(path) && fs::last_write_time(path) > config_file_modification_time) + { + std::ostringstream message; + message << "config file modified, reloading " << path; + sb::Log::log(message, sb::Log::DEBUG); + merge(path); + } } +#else + /* Warn user file modification check doesn't work on Android */ + sb::Log::log("File modification time can't be checked on Android, so file cannot be reloaded automatically", sb::Log::WARN); +#endif } void Configuration::update(float timestamp) diff --git a/src/Configuration.hpp b/src/Configuration.hpp index 7fa737d..2673185 100644 --- a/src/Configuration.hpp +++ b/src/Configuration.hpp @@ -29,7 +29,7 @@ private: Animation auto_refresher = Animation(std::bind(&Configuration::refresh, this)); fs::file_time_type config_file_modification_time; nlohmann::json config; - fs::path file_to_refresh; + std::vector files_to_refresh; /*! * Fill the config JSON with default values set by the framework. @@ -159,13 +159,12 @@ public: void merge(const char* path); /*! - * Enable auto refresh. Auto refresh watches the file at the given path for changes and loads them automatically every interval given - * in seconds. + * Enable auto refresh. Auto refresh watches the file at the given path for changes and loads them automatically at every interval + * specified in the configuration under "configuration" -> "auto-refresh-interval". * * @param file_to_refresh path to a configuration JSON - * @param interval amount of seconds between each refresh */ - void enable_auto_refresh(const fs::path& file_to_refresh, float interval); + void enable_auto_refresh(const fs::path& file_to_refresh); /*! * Disable auto refresh. The file previously set with Configuration::enable_auto_refresh will no longer be watched for changes. diff --git a/src/Game.cpp b/src/Game.cpp index 0d4e9a3..80fb3a8 100644 --- a/src/Game.cpp +++ b/src/Game.cpp @@ -18,15 +18,15 @@ Game::Game() SDL_LogSetPriority(sb::Log::DEFAULT_CATEGORY, SDL_LOG_PRIORITY_DEBUG); /* Merge user configuration into the existing configuration and turn on auto refresh if it is enabled by the configuration. */ - _configuration.merge(USER_CONFIG_PATH); + _configuration.merge(user_config_path); #ifdef __ANDROID__ - _configuration.merge(ANDROID_CONFIG_PATH); + _configuration.merge(android_config_path); #elif defined(__EMSCRIPTEN__) - _configuration.merge(WASM_CONFIG_PATH); + _configuration.merge(wasm_config_path); #endif if (configuration()["configuration"]["auto-refresh"]) { - _configuration.enable_auto_refresh(USER_CONFIG_PATH, configuration()["configuration"]["auto-refresh-interval"].get()); + _configuration.enable_auto_refresh(user_config_path); } /* Set the appropriate priority level for the default log category. Change it to VERBOSE if it is requested. Otherwise, diff --git a/src/Game.hpp b/src/Game.hpp index c2a9ef2..d713f64 100644 --- a/src/Game.hpp +++ b/src/Game.hpp @@ -66,9 +66,9 @@ private: SDL_Window* _window; std::shared_ptr _font; - inline static const std::string USER_CONFIG_PATH = "config.json"; - inline static const std::string ANDROID_CONFIG_PATH = "config_android.json"; - inline static const std::string WASM_CONFIG_PATH = "config_wasm.json"; + inline static const std::string user_config_path = "config.json"; + inline static const std::string android_config_path = "config_android.json"; + inline static const std::string wasm_config_path = "config_wasm.json"; /*! * Overrides SDL's default log function to log a message to stdout/stderr and, if log is enabled in the