spacebox/src/Configuration.hpp

92 lines
2.3 KiB
C++

/* +--------------------------------------------------------------+
____/ \____ /| - zlib/MIT/Unlicenced game framework licensed to freely use, |
\ / / | copy, modify and sell without restriction |
+--\ ^__^ /--+ | |
| ~/ \~ | | - originally created at [http://nugget.fun] |
| ~~~~~~~~~~~~ | +--------------------------------------------------------------+
| SPACE ~~~~~ | /
| ~~~~~~~ BOX |/
+-------------*/
#ifndef SB_CONFIGURATION_H_
#define SB_CONFIGURATION_H_
#include <fstream>
#include <ostream>
#include <iomanip>
#include "json/json.hpp"
#include "filesystem.hpp"
#include "Node.hpp"
#include "Animation.hpp"
#include "Log.hpp"
#include "extension.hpp"
class Configuration : public Node
{
private:
nlohmann::json sys_config, user_config;
fs::path config_path;
int tab_width = 4;
Animation auto_refresher = Animation(&Configuration::refresh, this);
fs::file_time_type config_file_modification_time;
void set_defaults();
void merge();
public:
nlohmann::json config;
Configuration(Node*, fs::path = "config.json");
void load(fs::path path);
void load();
void auto_refresh(bool);
void refresh();
void write(fs::path path);
void write();
void update();
virtual std::string class_name() const { return "Configuration"; }
};
/* Extend GLM so nlohmann::json can read and write glm::vec2 */
namespace glm
{
template <typename T>
void to_json(nlohmann::json& j, const vec<2, T, defaultp>& v)
{
j = nlohmann::json{v.x, v.y};
}
template <typename T>
void from_json(const nlohmann::json& j, vec<2, T, defaultp>& v)
{
j.at(0).get_to(v.x);
j.at(1).get_to(v.y);
}
}
/* Extend std::filesystem so nlohmann::json can read and write std::filesystem::path */
#if defined(__MINGW32__)
namespace std::experimental::filesystem
#else
namespace std::filesystem
#endif
{
template <typename T>
void to_json(nlohmann::json& j, const path& p)
{
j = nlohmann::json{p};
}
template <typename T>
void from_json(const nlohmann::json& j, path& p)
{
j.at(0).get_to(p);
}
}
#endif