spacebox/src/Configuration.cpp
Cocktail Frank efa08e7c93 Use SDL_GetKeyFromName to read key names in config
Log a warning if SDL does not understand the key name.
2025-02-04 13:28:42 -05:00

223 lines
6.4 KiB
C++

/* +=======================================================+
____/ \____ /: Open source game framework licensed to freely use, :
\ / / : copy, and modify - created for dank.game :
+==\ ^__^ /==+ : :
: ~/ \~ : : Download at https://open.shampoo.ooo/shampoo/spacebox :
: ~~~~~~~~~~~~ : +=======================================================+
: SPACE ~~~~~ : /
: ~~~~~~~ BOX :/
+=============*/
#include "Configuration.hpp"
using namespace sb;
Configuration::Configuration()
{
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"},
{"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.7f},
{"ignore repeat keypress", true}
};
config["display"] = {
{"dimensions", {1280, 720}},
{"max framerate", -1},
{"vsync", false},
{"sdl delay", 6},
{"title", "play your next game at dank.game 🪐💫💫💫📦"},
{"debug", false},
{"show cursor", false},
{"render-test-spacing", 2},
{"render driver", "opengl"},
{"fluid resize", false},
{"default font path", "BPmono.ttf"},
{"default font size", 16},
{"use play button", false},
{"fullscreen", false},
{"fullscreen enabled", true},
{"asyncify frequency", 0.1f}
};
config["audio"] = {
{"frequency", 48000},
{"chunk size", 2048}
};
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},
{"video frame length", 1.0f / 60.0f},
{"screenshot prefix", "screenshot-"},
{"screenshot extension", ".png"},
{"screenshot zfill", 5},
{"screenshot directory", "."},
{"gif frame length", 0.1f},
{"video directory", "."},
{"write mp4", false},
{"max stash length", 5.0f},
{"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"] = {
{"stdout enabled", false},
{"file enabled", false},
{"debug to stdout", false},
{"verbose to stdout", false},
{"debug to file", false},
{"output directory", "."},
{"info file name", "space_box_log.txt"},
{"debug file name", "space_box_debug_log.txt"},
{"short name", "spacebox"},
{"gl error", false}
};
config["configuration"] = {
{"auto refresh", false},
{"auto refresh interval", 5.0},
{"android config path", "config_android.json"},
{"wasm config path", "config_wasm.json"},
{"search path", {"config.json", "config"}}
};
config["storage"] = {
{"preferences file", "storage/preferences.json"}
};
}
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& patch)
{
if (!patch.empty())
{
config.merge_patch(patch);
}
else
{
sb::Log::log("Attempted to merge empty JSON into configuration", sb::Log::WARN);
}
}
void Configuration::merge(const fs::path& path)
{
merge(json_from_file(path));
}
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)
{
#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(message, sb::Log::WARN);
}
#endif
files_to_refresh.push_back(file_to_refresh);
auto_refresher.frame_length(config.at("configuration").at("auto refresh interval").get<float>());
auto_refresher.play();
}
void Configuration::disable_auto_refresh()
{
auto_refresher.pause();
}
void Configuration::refresh()
{
#if !defined(__ANDROID__)
for (const fs::path& path : files_to_refresh)
{
if (fs::exists(path) && fs::last_write_time(path) > config_modification_time)
{
std::ostringstream message;
message << "Config file modified, reloading " << path;
sb::Log::log(message, sb::Log::DEBUG);
/* If there is an error while merging, just ignore with a warning, so the game won't crash. */
try
{
merge(path);
}
catch (const std::exception& error)
{
std::ostringstream message;
message << "Couldn't merge changes made to " << path << ": " << error.what();
sb::Log::log(message, sb::Log::WARN);
}
/* Save the modification time and post a reconfig event to the event queue. */
config_modification_time = fs::file_time_type::clock::now();
sb::Delegate::post("reconfig");
}
}
#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)
{
auto_refresher.update(timestamp);
}
std::ostream& std::operator<<(std::ostream& out, const Configuration& configuration)
{
out << configuration();
return out;
}