0
0
mirror of https://github.com/Nimac0/SDL_Minigame synced 2026-01-12 21:23:41 +00:00
SDL_Minigame/src/ConfigLoader.cpp
freezarite acdbf29896 Fixed a bug where the custom json was not loaded correctly
Changed base config location to ./engine/config.json to make implementation better for the game-dev
2024-12-16 05:47:28 +01:00

53 lines
1.2 KiB
C++

#include "ConfigLoader.h"
#include <fstream>
ConfigLoader::ConfigLoader() {
//TODO: look into adaptive paths for better handling as this requires the implemented game
// to have ./engine in the root folder
baseConfig = loadConfigFromJSON("./engine/config.json");
}
ConfigLoader::~ConfigLoader() {}
void ConfigLoader::init() {
if (!customConfig.has_value()) {
finalConfig = baseConfig;
return;
}
finalConfig = mergeConfigs(baseConfig, customConfig.value());
}
json ConfigLoader::loadConfigFromJSON(const std::string& path) {
std::ifstream config_file(path);
json config;
if (!config_file.is_open()) {
throw std::runtime_error(std::string("Could not load config file at: " + path));
}
config_file >> config;
return config;
}
void ConfigLoader::setCustomConfig(const std::optional<std::string>& path) {
if (path.has_value()) {
customConfig = loadConfigFromJSON(path.value());
}
}
json ConfigLoader::mergeConfigs(json baseConfig, json customConfig) {
for (auto& entry : customConfig.items()) {
baseConfig[entry.key()] = entry.value();
}
return baseConfig;
}
json ConfigLoader::getFinalConfig() {
return finalConfig;
}