Skip to content

Commit

Permalink
ECS: Change json functions
Browse files Browse the repository at this point in the history
  • Loading branch information
Saverio976 committed Oct 31, 2023
1 parent 2bbfc15 commit 42d0012
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 39 deletions.
1 change: 1 addition & 0 deletions deps/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
cmake_minimum_required(VERSION 3.20)

add_subdirectory(date)
add_subdirectory(json)
19 changes: 19 additions & 0 deletions deps/json/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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
$<BUILD_INTERFACE:${json_SOURCE_DIR}/include>
$<INSTALL_INTERFACE:include>
)
55 changes: 16 additions & 39 deletions include/B-luga/Json.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,52 +7,30 @@

#pragma once

#include <nlohmann/json.hpp>
#include <string>
#include <unordered_map>
#include <fstream>
#include <iostream>
#include <sstream>
#include <utility>
#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<enemy_type_e, JsonType> messageTypes = {
{CLASSIC_ENEMY, JsonType::DEFAULT_ENEMY},
{TERMINATOR, JsonType::TERMINATOR }
};

const std::unordered_map<JsonType, std::string> 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];

Expand All @@ -67,7 +45,7 @@ class Json {
}

template <typename T>
T getDataByJsonType(const std::string &index, JsonType dataType)
T getDataByJsonType(const std::string &dataType, const std::string &index)
{
nlohmann::json finalData(_jsonDatas[dataType]);

Expand All @@ -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]);

Expand Down Expand Up @@ -161,7 +139,7 @@ class Json {
}

std::vector<nlohmann::json>
getDatasByJsonType(const std::vector<std::string> &indexes, JsonType dataType)
getDatasByJsonType(const std::string &dataType, const std::vector<std::string> &indexes)
{
nlohmann::json finalData = getDataByJsonType(dataType);
std::vector<nlohmann::json> datas;
Expand All @@ -182,7 +160,6 @@ class Json {
return datas;
}


template <typename T>
T getDataFromJson(nlohmann::json jsonData, const std::string &index)
{
Expand All @@ -194,7 +171,7 @@ class Json {
}

template <typename T>
T getDataByVector(const std::vector<std::string> &indexes, JsonType dataType)
T getDataByVector(const std::string &dataType, const std::vector<std::string> &indexes)
{
auto datas = getDataByJsonType(dataType);
auto begin = indexes.begin();
Expand All @@ -213,7 +190,7 @@ class Json {
return getDataFromJson<T>(datas, *(indexes.end() - 1));
}

nlohmann::json getDataByVector(const std::vector<std::string> &indexes, JsonType dataType)
nlohmann::json getDataByVector(const std::string &dataType, const std::vector<std::string> &indexes)
{
return getDataByVector<nlohmann::json>(indexes, dataType);
}
Expand All @@ -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 = {};

Expand All @@ -250,5 +227,5 @@ class Json {
return jsonData;
}

std::unordered_map<JsonType, nlohmann::json> _jsonDatas;
std::unordered_map<std::string, nlohmann::json> _jsonDatas;
};

0 comments on commit 42d0012

Please sign in to comment.