222 lines
6.1 KiB
C++
222 lines
6.1 KiB
C++
/* +------------------------------------------------------+
|
|
____/ \____ /| - Open source game framework licensed to freely use, |
|
|
\ / / | copy, modify and sell without restriction |
|
|
+--\ ^__^ /--+ | |
|
|
| ~/ \~ | | - created for <https://foam.shampoo.ooo> |
|
|
| ~~~~~~~~~~~~ | +------------------------------------------------------+
|
|
| SPACE ~~~~~ | /
|
|
| ~~~~~~~ BOX |/
|
|
+-------------*/
|
|
|
|
#include "Configuration.hpp"
|
|
|
|
Configuration::Configuration(Node* parent) : Node(parent)
|
|
{
|
|
set_defaults();
|
|
}
|
|
|
|
void Configuration::set_defaults()
|
|
{
|
|
config["keys"] = {
|
|
{"record", {"CTRL", "SHIFT", "i"}},
|
|
{"save-current-stash", {"CTRL", "SHIFT", "v"}},
|
|
{"screenshot", {"CTRL", "i"}},
|
|
{"action", "space"},
|
|
{"up", "up"},
|
|
{"right", "right"},
|
|
{"down", "down"},
|
|
{"left", "left"},
|
|
{"pause", "enter"},
|
|
{"fullscreen", {"ALT", "enter"}},
|
|
{"reset", {"CTRL", "r"}}
|
|
};
|
|
config["input"] = {
|
|
{"suppress-any-key-on-mods", true},
|
|
{"system-any-key-ignore-commands", {"fullscreen", "screenshot", "record", "quit"}},
|
|
{"any-key-ignore-commands", nlohmann::json::array()},
|
|
{"default-unsuppress-delay", 0.7},
|
|
{"ignore-repeat-keypress", true}
|
|
};
|
|
config["display"] = {
|
|
{"dimensions", {960, 540}},
|
|
{"framerate", 60},
|
|
{"title", "[SPACEBOX]"},
|
|
{"debug", false},
|
|
{"show-cursor", false},
|
|
{"render-test-spacing", 2},
|
|
{"render driver", "opengl"},
|
|
{"fluid resize", false},
|
|
{"default font path", "BPmono.ttf"},
|
|
{"default font size", 16}
|
|
};
|
|
config["audio"] = {
|
|
{"default-sfx-root", "resource/sfx"},
|
|
{"default-bgm-root", "resource/bgm"}
|
|
};
|
|
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}
|
|
},
|
|
config["recording"] = {
|
|
{"enabled", false},
|
|
{"screenshot-prefix", "screenshot-"},
|
|
{"screenshot-extension", ".png"},
|
|
{"screenshot-zfill", 5},
|
|
{"screenshot-directory", "."},
|
|
{"gif-frame-length", 0.1},
|
|
{"video-directory", "."},
|
|
{"write-mp4", false},
|
|
{"max-stash-length", 5.0},
|
|
{"max-in-game-stashes", 3},
|
|
{"max-video-stashes", 40},
|
|
{"max-video-memory", 1000},
|
|
{"mp4-pixel-format", "yuv444p"}
|
|
};
|
|
config["animation"] = {
|
|
{"all frames frameset name", "all"}
|
|
};
|
|
config["log"] = {
|
|
{"enabled", false},
|
|
{"debug-to-stdout", true},
|
|
{"debug-to-file", false},
|
|
{"output-directory", "."},
|
|
{"info-file-name", "space_box_log.txt"},
|
|
{"debug-file-name", "space_box_debug_log.txt"},
|
|
{"short-name", "spacebox"}
|
|
};
|
|
config["configuration"] = {
|
|
{"auto-refresh", false},
|
|
{"auto-refresh-interval", 1.0}
|
|
};
|
|
}
|
|
|
|
nlohmann::json& Configuration::operator[](const std::string& key)
|
|
{
|
|
return config[key];
|
|
}
|
|
|
|
const nlohmann::json& Configuration::operator[](const std::string& key) const
|
|
{
|
|
return config[key];
|
|
}
|
|
|
|
const nlohmann::json& Configuration::operator()() const
|
|
{
|
|
return config;
|
|
}
|
|
|
|
void Configuration::merge(const nlohmann::json& incoming)
|
|
{
|
|
if (!incoming.empty())
|
|
{
|
|
/* loop over first level key/value pairs */
|
|
for (auto& item: incoming.items())
|
|
{
|
|
/* 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();
|
|
}
|
|
}
|
|
}
|
|
else
|
|
{
|
|
sb::Log::log("Attempted to merge empty JSON into configuration", sb::Log::WARN);
|
|
}
|
|
}
|
|
|
|
void Configuration::merge(const fs::path& path)
|
|
{
|
|
#ifndef __ANDROID__
|
|
/* Can't check for file existence in an Android APK */
|
|
if (fs::exists(path))
|
|
{
|
|
#endif
|
|
/* Load JSON to a string and check for validity. */
|
|
std::string contents = sb::file_to_string(path);
|
|
if (nlohmann::json::accept(contents))
|
|
{
|
|
merge(nlohmann::json::parse(contents));
|
|
}
|
|
else
|
|
{
|
|
std::ostringstream message;
|
|
message << "Invalid JSON at " << path;
|
|
sb::Log::log(message, sb::Log::WARN);
|
|
}
|
|
#ifndef __ANDROID__
|
|
config_file_modification_time = fs::last_write_time(path);
|
|
}
|
|
else
|
|
{
|
|
std::ostringstream message;
|
|
message << "File not found: " << path;
|
|
sb::Log::log(message, sb::Log::WARN);
|
|
}
|
|
#endif
|
|
}
|
|
|
|
void Configuration::merge(const std::string& path)
|
|
{
|
|
merge(fs::path(path));
|
|
}
|
|
|
|
void Configuration::merge(const char* path)
|
|
{
|
|
merge(fs::path(path));
|
|
}
|
|
|
|
void Configuration::enable_auto_refresh(const fs::path& file_to_refresh, float interval)
|
|
{
|
|
#ifndef __ANDROID__
|
|
/* Warn user if the file does not exist */
|
|
if (!fs::exists(file_to_refresh))
|
|
{
|
|
std::ostringstream message;
|
|
message << "File to auto-refresh does not exist: " << file_to_refresh;
|
|
sb::Log::log(file_to_refresh, sb::Log::WARN);
|
|
}
|
|
#endif
|
|
this->file_to_refresh = file_to_refresh;
|
|
auto_refresher.set_frame_length(interval);
|
|
auto_refresher.play();
|
|
}
|
|
|
|
void Configuration::disable_auto_refresh()
|
|
{
|
|
auto_refresher.pause();
|
|
}
|
|
|
|
void Configuration::refresh()
|
|
{
|
|
if (fs::exists(file_to_refresh) && fs::last_write_time(file_to_refresh) > config_file_modification_time)
|
|
{
|
|
std::ostringstream message;
|
|
message << "config file modified, reloading " << file_to_refresh;
|
|
sb::Log::log(message, sb::Log::DEBUG);
|
|
merge(file_to_refresh);
|
|
}
|
|
}
|
|
|
|
void Configuration::update(float timestamp)
|
|
{
|
|
auto_refresher.update(timestamp);
|
|
}
|
|
|
|
std::ostream& std::operator<<(std::ostream& out, const Configuration& configuration)
|
|
{
|
|
out << configuration();
|
|
return out;
|
|
}
|