82 lines
1.7 KiB
C++
82 lines
1.7 KiB
C++
#include "Configuration.hpp"
|
|
|
|
Configuration::Configuration(Node *parent) : Configuration(parent, "config.json") {}
|
|
|
|
Configuration::Configuration(Node *parent, fs::path path) : Node(parent)
|
|
{
|
|
config_path = path;
|
|
set_defaults();
|
|
load();
|
|
merge();
|
|
}
|
|
|
|
void Configuration::set_defaults()
|
|
{
|
|
sys_config["keys"] = {
|
|
{"record", {"CTRL", "SHIFT", "f10"}},
|
|
{"screenshot", "f9"},
|
|
{"action", "space"},
|
|
{"up", "up"},
|
|
{"right", "right"},
|
|
{"down", "down"},
|
|
{"left", "left"},
|
|
{"pause", "enter"},
|
|
{"fullscreen", {"ALT", "enter"}}
|
|
};
|
|
sys_config["path"] = {
|
|
{"screenshots", "."},
|
|
{"video", "."}
|
|
};
|
|
sys_config["display"] = {
|
|
{"dimensions", {640, 480}}
|
|
};
|
|
sys_config["recording"] = {
|
|
{"screenshot-prefix", "screenshot-"},
|
|
{"screenshot-extension", ".png"},
|
|
{"screenshot-zfill", 5},
|
|
{"gif-frame-length", 100},
|
|
{"write-mp4", false}
|
|
};
|
|
}
|
|
|
|
void Configuration::load()
|
|
{
|
|
load(config_path);
|
|
}
|
|
|
|
void Configuration::load(fs::path path)
|
|
{
|
|
if (fs::exists(path))
|
|
{
|
|
std::ifstream contents(path);
|
|
contents >> game_config;
|
|
}
|
|
}
|
|
|
|
void Configuration::merge()
|
|
{
|
|
config = sys_config;
|
|
if (not game_config.empty())
|
|
{
|
|
for (auto& section: config.items())
|
|
{
|
|
if (game_config.contains(section.key()))
|
|
{
|
|
config[section.key()].update(game_config[section.key()]);
|
|
}
|
|
}
|
|
}
|
|
std::cout << std::setw(4) << config << std::endl;
|
|
}
|
|
|
|
void Configuration::write()
|
|
{
|
|
write(config_path);
|
|
}
|
|
|
|
void Configuration::write(fs::path path)
|
|
{
|
|
std::ofstream output(path);
|
|
output << std::setw(tab_width) << game_config << std::endl;
|
|
}
|