0
0
mirror of https://github.com/Nimac0/SDL_Minigame synced 2026-01-12 13:43:41 +00:00
SDL_Minigame/src/ConfigLoader.cpp
2025-01-14 19:27:56 +01:00

52 lines
1.2 KiB
C++

#include "ConfigLoader.h"
#include <fstream>
ConfigLoader::ConfigLoader() {}
ConfigLoader::~ConfigLoader() {}
void ConfigLoader::init() {
//TODO: look into adaptive paths for better handling as this requires the implemented game
// to have ./engine in the root folder (very low prio)
const json baseConfig = loadConfigFromJSON("./engine/config.json");
if (!customConfigPath.has_value()) {
finalConfig = baseConfig;
return;
}
finalConfig = mergeConfigs(baseConfig, loadConfigFromJSON(customConfigPath.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) {
customConfigPath = path;
}
json ConfigLoader::mergeConfigs(json baseConfig, json customConfig) {
for (auto& entry : customConfig.items()) {
baseConfig[entry.key()] = entry.value();
}
return baseConfig;
}
json ConfigLoader::getFinalConfig() {
return finalConfig;
}