From 42d0012ed52b661de8a83344d49bf64f1bab286a Mon Sep 17 00:00:00 2001 From: Xavier Mitault Date: Tue, 31 Oct 2023 12:40:34 +0000 Subject: [PATCH] ECS: Change json functions --- deps/CMakeLists.txt | 1 + deps/json/CMakeLists.txt | 19 ++++++++++++++ include/B-luga/Json.hpp | 55 ++++++++++++---------------------------- 3 files changed, 36 insertions(+), 39 deletions(-) create mode 100644 deps/json/CMakeLists.txt diff --git a/deps/CMakeLists.txt b/deps/CMakeLists.txt index 2e986c0..e903f97 100644 --- a/deps/CMakeLists.txt +++ b/deps/CMakeLists.txt @@ -1,3 +1,4 @@ cmake_minimum_required(VERSION 3.20) add_subdirectory(date) +add_subdirectory(json) diff --git a/deps/json/CMakeLists.txt b/deps/json/CMakeLists.txt new file mode 100644 index 0000000..9cf605a --- /dev/null +++ b/deps/json/CMakeLists.txt @@ -0,0 +1,19 @@ +cmake_minimum_required(VERSION 3.20) + +include(FetchContent) + +FetchContent_Declare( + json + CMAKE_ARGS "-DCMAKE_BUILD_TYPE=Release" "-DCMAKE_CONFIGURATION_TYPES=\"Release;Release\"" "-DCONFIG=Release" + URL https://github.com/nlohmann/json/releases/download/v3.11.2/json.tar.xz + DOWNLOAD_EXTRACT_TIMESTAMP TRUE + DOWNLOAD_NO_PROGRESS FALSE +) +FetchContent_MakeAvailable(json) + +target_include_directories( + ${PROJECT_NAME} + INTERFACE + $ + $ +) diff --git a/include/B-luga/Json.hpp b/include/B-luga/Json.hpp index 398eaa7..3f4f426 100644 --- a/include/B-luga/Json.hpp +++ b/include/B-luga/Json.hpp @@ -7,52 +7,30 @@ #pragma once -#include #include #include #include #include #include #include -#include "Logger.hpp" -#include "ResourcesManager.hpp" - -extern "C" -{ -#include "MessageTypes.h" -} - -enum class JsonType { DEFAULT_ENEMY, DEFAULT_PLAYER, DEFAULT_PARALLAX, TERMINATOR, WAVE, BULLETS }; - -const std::unordered_map messageTypes = { - {CLASSIC_ENEMY, JsonType::DEFAULT_ENEMY}, - {TERMINATOR, JsonType::TERMINATOR } -}; - -const std::unordered_map pathToJson = { - {JsonType::DEFAULT_ENEMY, "assets/Json/enemyData.json" }, - {JsonType::DEFAULT_PLAYER, "assets/Json/playerData.json" }, - {JsonType::DEFAULT_PARALLAX, "assets/Json/parallaxData.json"}, - {JsonType::TERMINATOR, "assets/Json/terminator.json" }, - {JsonType::WAVE, "assets/Json/wave.json" }, - {JsonType::BULLETS, "assets/Json/bullets.json" } -}; +#include "nlohmann/json.hpp" +#include "B-luga/Logger.hpp" class Json { public: static Json &getInstance() { static auto instance = Json(); - if (instance._jsonDatas.empty()) { - for (const auto &it : pathToJson) { - instance._jsonDatas.insert({it.first, instance.loadJsonData(it.second)}); - } - } return instance; } + void registerJsonFile(const std::string &path) + { + _jsonDatas.insert({path, loadJsonData(path)}); + } + nlohmann::json - getJsonObjectById(JsonType type, const std::string &id, const std::string &arrayName) + getJsonObjectById(const std::string &type, const std::string &id, const std::string &arrayName) { auto objectList = getDataByJsonType(type)[arrayName]; @@ -67,7 +45,7 @@ class Json { } template - T getDataByJsonType(const std::string &index, JsonType dataType) + T getDataByJsonType(const std::string &dataType, const std::string &index) { nlohmann::json finalData(_jsonDatas[dataType]); @@ -85,14 +63,14 @@ class Json { return (finalData); } - nlohmann::json getDataByJsonType(JsonType dataType) + nlohmann::json getDataByJsonType(const std::string &dataType) { nlohmann::json data(_jsonDatas[dataType]); return (data); } - nlohmann::basic_json<> getDataByJsonType(const std::string &index, JsonType dataType) + nlohmann::basic_json<> getDataByJsonType(const std::string &dataType, const std::string &index) { nlohmann::basic_json<> finalData(_jsonDatas[dataType]); @@ -161,7 +139,7 @@ class Json { } std::vector - getDatasByJsonType(const std::vector &indexes, JsonType dataType) + getDatasByJsonType(const std::string &dataType, const std::vector &indexes) { nlohmann::json finalData = getDataByJsonType(dataType); std::vector datas; @@ -182,7 +160,6 @@ class Json { return datas; } - template T getDataFromJson(nlohmann::json jsonData, const std::string &index) { @@ -194,7 +171,7 @@ class Json { } template - T getDataByVector(const std::vector &indexes, JsonType dataType) + T getDataByVector(const std::string &dataType, const std::vector &indexes) { auto datas = getDataByJsonType(dataType); auto begin = indexes.begin(); @@ -213,7 +190,7 @@ class Json { return getDataFromJson(datas, *(indexes.end() - 1)); } - nlohmann::json getDataByVector(const std::vector &indexes, JsonType dataType) + nlohmann::json getDataByVector(const std::string &dataType, const std::vector &indexes) { return getDataByVector(indexes, dataType); } @@ -236,7 +213,7 @@ class Json { static nlohmann::json loadJsonData(const std::string &path) { - std::ifstream fileData(ECS::ResourcesManager::convertPath(path)); + std::ifstream fileData(path); std::ostringstream input; nlohmann::json jsonData = {}; @@ -250,5 +227,5 @@ class Json { return jsonData; } - std::unordered_map _jsonDatas; + std::unordered_map _jsonDatas; };