From 9247b8df8a9f685cb0e92a6dc800227244ff6083 Mon Sep 17 00:00:00 2001 From: freezarite Date: Tue, 3 Dec 2024 18:17:31 +0100 Subject: [PATCH] started work on config-system added nlohmann_json library as submodule and to CMakeLists.txt created basic config.json file in root folder created ConfigLoader.h and added basic functionalities to it --- .gitmodules | 3 +++ CMakeLists.txt | 6 +++++- config.json | 3 +++ extern/nlohmann_json | 1 + include/ConfigLoader.h | 24 ++++++++++++++++++++++++ src/ConfigLoader.cpp | 41 +++++++++++++++++++++++++++++++++++++++++ 6 files changed, 77 insertions(+), 1 deletion(-) create mode 100644 config.json create mode 160000 extern/nlohmann_json create mode 100644 include/ConfigLoader.h create mode 100644 src/ConfigLoader.cpp diff --git a/.gitmodules b/.gitmodules index 6dd82fa..9f871ba 100644 --- a/.gitmodules +++ b/.gitmodules @@ -17,3 +17,6 @@ path = docs/doxygen-awesome-css url = https://github.com/jothepro/doxygen-awesome-css.git +[submodule "extern/nlohmann_json"] + path = extern/nlohmann_json + url = https://github.com/nlohmann/json.git diff --git a/CMakeLists.txt b/CMakeLists.txt index d38fa27..12d886d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -24,10 +24,13 @@ add_subdirectory(extern/SDL_image EXCLUDE_FROM_ALL) add_subdirectory(extern/SDL_mixer EXCLUDE_FROM_ALL) add_subdirectory(extern/SDL_ttf EXCLUDE_FROM_ALL) add_subdirectory(extern/tmxlite/tmxlite EXCLUDE_FROM_ALL) +add_subdirectory(extern/nlohmann_json EXCLUDE_FROM_ALL) file(GLOB_RECURSE SOURCES ${ENGINE_SOURCE_DIR}/src/*.cpp) -add_library(${PROJECT_NAME} ${SOURCES}) +add_library(${PROJECT_NAME} ${SOURCES} + include/ConfigLoader.h + src/ConfigLoader.cpp) target_include_directories(${PROJECT_NAME} PUBLIC ${ENGINE_INCLUDE_DIR}) @@ -37,6 +40,7 @@ target_link_libraries(${PROJECT_NAME} PUBLIC # should be private when all SDL fu SDL3_mixer::SDL3_mixer-static SDL3_ttf::SDL3_ttf-static tmxlite + nlohmann_json::nlohmann_json ) if(CMAKE_BUILD_TYPE MATCHES "Debug") diff --git a/config.json b/config.json new file mode 100644 index 0000000..2aff41b --- /dev/null +++ b/config.json @@ -0,0 +1,3 @@ +{ + "fullscreen": false +} \ No newline at end of file diff --git a/extern/nlohmann_json b/extern/nlohmann_json new file mode 160000 index 0000000..a006a7a --- /dev/null +++ b/extern/nlohmann_json @@ -0,0 +1 @@ +Subproject commit a006a7a48bb30a247f0344b788c62c2806edd90b diff --git a/include/ConfigLoader.h b/include/ConfigLoader.h new file mode 100644 index 0000000..d0115a4 --- /dev/null +++ b/include/ConfigLoader.h @@ -0,0 +1,24 @@ +#pragma once + +#include + +using json = nlohmann::json; + +class ConfigLoader { + +public: + ConfigLoader(); + ~ConfigLoader(); + + void init(); + + void setCustomConfig(std::string path); + +private: + json baseConfig; + std::optional customConfig; + json finalConfig; + + json loadConfigFromJSON(const std::string& path); + json mergeConfigs(json baseConfig, json customConfig); // + +ConfigLoader::ConfigLoader() { + baseConfig = loadConfigFromJSON("config.json"); +} + +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(std::string path) { + customConfig.emplace(loadConfigFromJSON(path)); +} + +json ConfigLoader::mergeConfigs(json baseConfig, json customConfig) { + for (auto& entry : customConfig.items()) { + baseConfig[entry.key()] = entry.value(); + } + return baseConfig; +} + +