watch multiple config files for changes
This commit is contained in:
parent
27d2128e1f
commit
1d527898aa
|
@ -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<float>());
|
||||
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)
|
||||
|
|
|
@ -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<fs::path> 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.
|
||||
|
|
|
@ -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<float>());
|
||||
_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,
|
||||
|
|
|
@ -66,9 +66,9 @@ private:
|
|||
SDL_Window* _window;
|
||||
std::shared_ptr<TTF_Font> _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
|
||||
|
|
Loading…
Reference in New Issue