2022-07-23 18:55:27 -04:00
|
|
|
/* /\ +------------------------------------------------------------+
|
|
|
|
____/ \____ /| zlib/MIT/Unlicenced game framework licensed to freely use, |
|
|
|
|
\ / / | copy, modify and sell without restriction |
|
|
|
|
+--\ ^__^ /--+ | |
|
|
|
|
| ~/ \~ | | Learn more about [SPACE BOX] at [shampoo.ooo] |
|
|
|
|
| ~~~~~~~~~~~~ | +------------------------------------------------------------+
|
2021-11-09 23:30:27 -05:00
|
|
|
| SPACE ~~~~~ | /
|
|
|
|
| ~~~~~~~ BOX |/
|
|
|
|
+-------------*/
|
2020-09-04 23:10:39 -04:00
|
|
|
|
2019-05-02 06:45:41 -04:00
|
|
|
#include "Configuration.hpp"
|
|
|
|
|
2021-08-31 23:55:38 -04:00
|
|
|
/* Initialize a Configuration object. The path argument is the location where the config file is stored.
|
|
|
|
* If there is no file located at the path, it will be created if the write method is called. System level
|
|
|
|
* default assignments defined in this file can be added to and overwritten by user supplied JSON file at
|
|
|
|
* the specified path or at a path passed to the load function. */
|
2019-05-02 06:45:41 -04:00
|
|
|
Configuration::Configuration(Node *parent, fs::path path) : Node(parent)
|
|
|
|
{
|
|
|
|
config_path = path;
|
|
|
|
set_defaults();
|
|
|
|
load();
|
2021-08-31 23:55:38 -04:00
|
|
|
auto_refresher.set_frame_length(config["configuration"]["auto-refresh-interval"].get<float>());
|
|
|
|
auto_refresh(config["configuration"]["auto-refresh"]);
|
2019-05-02 06:45:41 -04:00
|
|
|
}
|
|
|
|
|
2021-08-31 23:55:38 -04:00
|
|
|
/* Fill the system level config JSON dict with default values set by the framework */
|
2019-05-02 06:45:41 -04:00
|
|
|
void Configuration::set_defaults()
|
|
|
|
{
|
|
|
|
sys_config["keys"] = {
|
2022-07-26 17:28:56 -04:00
|
|
|
{"record", {"CTRL", "SHIFT", "i"}},
|
2019-05-31 02:54:31 -04:00
|
|
|
{"save-current-stash", {"CTRL", "SHIFT", "v"}},
|
2022-07-26 17:28:56 -04:00
|
|
|
{"screenshot", {"CTRL", "i"}},
|
2019-05-16 03:51:36 -04:00
|
|
|
{"action", "space"},
|
2019-05-04 03:25:35 -04:00
|
|
|
{"up", "up"},
|
|
|
|
{"right", "right"},
|
|
|
|
{"down", "down"},
|
2019-05-16 03:51:36 -04:00
|
|
|
{"left", "left"},
|
|
|
|
{"pause", "enter"},
|
2019-06-16 03:05:02 -04:00
|
|
|
{"fullscreen", {"ALT", "enter"}},
|
2020-07-22 00:53:40 -04:00
|
|
|
{"toggle-framerate", {"CTRL", "f"}},
|
|
|
|
{"reset", {"CTRL", "r"}}
|
2019-05-02 06:45:41 -04:00
|
|
|
};
|
2020-07-21 01:09:34 -04:00
|
|
|
sys_config["input"] = {
|
|
|
|
{"suppress-any-key-on-mods", true},
|
2021-08-02 20:18:56 -04:00
|
|
|
{"system-any-key-ignore-commands", {"fullscreen", "screenshot", "toggle-framerate", "record", "quit"}},
|
2021-08-31 23:55:38 -04:00
|
|
|
{"any-key-ignore-commands", nlohmann::json::array()},
|
2020-08-19 03:29:46 -04:00
|
|
|
{"default-unsuppress-delay", 700},
|
|
|
|
{"ignore-repeat-keypress", true}
|
2020-07-21 01:09:34 -04:00
|
|
|
};
|
2019-05-07 03:33:54 -04:00
|
|
|
sys_config["display"] = {
|
2022-11-08 17:16:05 -05:00
|
|
|
{"dimensions", {960, 540}},
|
2020-07-19 15:43:47 -04:00
|
|
|
{"framerate", 60},
|
2022-11-08 17:16:05 -05:00
|
|
|
{"title", "[SPACEBOX]"},
|
2020-08-21 02:21:34 -04:00
|
|
|
{"debug", false},
|
|
|
|
{"show-cursor", false},
|
2020-08-28 22:17:01 -04:00
|
|
|
{"render-test-spacing", 2},
|
|
|
|
{"render driver", "opengl"}
|
2019-05-22 04:27:15 -04:00
|
|
|
};
|
2020-09-11 18:01:27 -04:00
|
|
|
sys_config["audio"] = {
|
|
|
|
{"default-sfx-root", "resource/sfx"},
|
|
|
|
{"default-bgm-root", "resource/bgm"}
|
|
|
|
};
|
2022-10-07 18:29:25 -04:00
|
|
|
sys_config["gl"] = {
|
|
|
|
{"depth-size", 16},
|
|
|
|
{"red-size", 8},
|
|
|
|
{"green-size", 8},
|
|
|
|
{"blue-size", 8},
|
|
|
|
{"share-with-current-context", true},
|
|
|
|
{"double-buffer", true},
|
|
|
|
{"major-version", 3},
|
|
|
|
{"minor-version", 2}
|
|
|
|
},
|
2019-05-22 04:27:15 -04:00
|
|
|
sys_config["recording"] = {
|
2019-07-21 03:13:19 -04:00
|
|
|
{"enabled", false},
|
2019-05-07 03:33:54 -04:00
|
|
|
{"screenshot-prefix", "screenshot-"},
|
|
|
|
{"screenshot-extension", ".png"},
|
2019-05-16 03:51:36 -04:00
|
|
|
{"screenshot-zfill", 5},
|
2021-05-08 20:01:53 -04:00
|
|
|
{"screenshot-directory", "."},
|
2019-05-22 04:27:15 -04:00
|
|
|
{"gif-frame-length", 100},
|
2021-05-08 20:01:53 -04:00
|
|
|
{"video-directory", "."},
|
2019-05-25 07:28:05 -04:00
|
|
|
{"write-mp4", false},
|
|
|
|
{"max-stash-length", 5000},
|
|
|
|
{"max-in-game-stashes", 3},
|
2019-05-31 02:54:31 -04:00
|
|
|
{"max-video-stashes", 40},
|
|
|
|
{"max-video-memory", 1000},
|
|
|
|
{"mp4-pixel-format", "yuv444p"}
|
2019-05-07 03:33:54 -04:00
|
|
|
};
|
2019-06-16 03:05:02 -04:00
|
|
|
sys_config["fps-indicator"] = {
|
|
|
|
{"width", .05},
|
|
|
|
{"height", .04},
|
|
|
|
{"background", {255, 255, 255}},
|
|
|
|
{"foreground", {0, 0, 0}}
|
|
|
|
};
|
2020-07-17 22:58:23 -04:00
|
|
|
sys_config["animation"] = {
|
|
|
|
{"all-frames-frameset-name", "all"}
|
|
|
|
};
|
2021-06-24 17:40:30 -04:00
|
|
|
sys_config["log"] = {
|
2021-05-08 20:01:53 -04:00
|
|
|
{"enabled", false},
|
2021-06-24 17:40:30 -04:00
|
|
|
{"debug-to-stdout", false},
|
|
|
|
{"debug-to-file", false},
|
2022-11-08 17:16:05 -05:00
|
|
|
{"output-directory", "."},
|
2022-05-06 23:39:32 -04:00
|
|
|
{"info-file-name", "space_box_log.txt"},
|
2022-11-08 17:16:05 -05:00
|
|
|
{"debug-file-name", "space_box_debug_log.txt"},
|
|
|
|
{"short-name", "spacebox"}
|
2021-05-08 20:01:53 -04:00
|
|
|
};
|
2021-08-31 23:55:38 -04:00
|
|
|
sys_config["configuration"] = {
|
|
|
|
{"auto-refresh", false},
|
|
|
|
{"auto-refresh-interval", 1000}
|
|
|
|
};
|
|
|
|
config = sys_config;
|
2019-05-02 06:45:41 -04:00
|
|
|
}
|
|
|
|
|
2021-08-31 23:55:38 -04:00
|
|
|
/* Load the configuration file at path */
|
2019-05-02 06:45:41 -04:00
|
|
|
void Configuration::load(fs::path path)
|
|
|
|
{
|
2021-08-31 23:55:38 -04:00
|
|
|
/* read contents of path into the game level config JSON dict */
|
2022-11-08 17:16:05 -05:00
|
|
|
user_config = nlohmann::json::parse(sb::file_to_string(path));
|
|
|
|
|
|
|
|
/* merge into the full config JSON dict */
|
|
|
|
merge();
|
2019-05-04 03:25:35 -04:00
|
|
|
}
|
|
|
|
|
2021-08-31 23:55:38 -04:00
|
|
|
/* Load the configuration file at Configuration::config_path */
|
|
|
|
void Configuration::load()
|
|
|
|
{
|
|
|
|
load(config_path);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Merge the system level config JSON dict (hard-coded in this file) with the user level config JSON
|
|
|
|
* dict (loaded from disk by the load function) */
|
2019-05-04 03:25:35 -04:00
|
|
|
void Configuration::merge()
|
|
|
|
{
|
2022-11-08 17:16:05 -05:00
|
|
|
if (!user_config.empty())
|
2019-05-04 03:25:35 -04:00
|
|
|
{
|
2021-08-31 23:55:38 -04:00
|
|
|
/* loop over first level key/value pairs */
|
|
|
|
for (auto& item: user_config.items())
|
2019-05-07 03:33:54 -04:00
|
|
|
{
|
2021-08-31 23:55:38 -04:00
|
|
|
/* if the value is an object (dict), merge it into the config, overwriting keys already in the config */
|
|
|
|
if (item.value().is_object())
|
|
|
|
{
|
|
|
|
config[item.key()].update(item.value());
|
|
|
|
}
|
|
|
|
/* otherwise just assign config key to this value */
|
|
|
|
else
|
|
|
|
{
|
|
|
|
config[item.key()] = item.value();
|
|
|
|
}
|
2019-05-07 03:33:54 -04:00
|
|
|
}
|
2019-05-04 03:25:35 -04:00
|
|
|
}
|
2019-05-02 06:45:41 -04:00
|
|
|
}
|
|
|
|
|
2021-08-31 23:55:38 -04:00
|
|
|
/* Set auto refresh to on or off */
|
|
|
|
void Configuration::auto_refresh(bool on)
|
2019-05-02 06:45:41 -04:00
|
|
|
{
|
2021-08-31 23:55:38 -04:00
|
|
|
on ? auto_refresher.play() : auto_refresher.pause();
|
2019-05-02 06:45:41 -04:00
|
|
|
}
|
|
|
|
|
2021-08-31 23:55:38 -04:00
|
|
|
/* Refresh the config contents by calling the default load function */
|
|
|
|
void Configuration::refresh()
|
|
|
|
{
|
|
|
|
if (fs::exists(config_path) && fs::last_write_time(config_path) > config_file_modification_time)
|
|
|
|
{
|
|
|
|
std::ostringstream message;
|
|
|
|
message << "config file modified, reloading " << config_path;
|
2021-10-02 19:21:07 -04:00
|
|
|
sb::Log::log(message, sb::Log::DEBUG);
|
2021-08-31 23:55:38 -04:00
|
|
|
load();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Write configuration to specified path in JSON format */
|
2019-05-02 06:45:41 -04:00
|
|
|
void Configuration::write(fs::path path)
|
|
|
|
{
|
|
|
|
std::ofstream output(path);
|
2021-08-31 23:55:38 -04:00
|
|
|
output << std::setw(tab_width) << user_config << std::endl;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Write configuration to config_path (set at initialization) */
|
|
|
|
void Configuration::write()
|
|
|
|
{
|
|
|
|
write(config_path);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Updates the auto refresher */
|
|
|
|
void Configuration::update()
|
|
|
|
{
|
|
|
|
auto_refresher.update();
|
2019-05-02 06:45:41 -04:00
|
|
|
}
|