From dfce4d0398a8bafbb7ad7a31345af181c0269c09 Mon Sep 17 00:00:00 2001 From: Your Name Date: Thu, 15 Apr 2021 15:23:23 -0400 Subject: Added spells --- src/utils.h | 31 +++++++++++++++++++------------ 1 file changed, 19 insertions(+), 12 deletions(-) (limited to 'src/utils.h') diff --git a/src/utils.h b/src/utils.h index f7366dc..9f4cf5c 100644 --- a/src/utils.h +++ b/src/utils.h @@ -1,16 +1,34 @@ #pragma once #include "json.hpp" +#include "settings.h" #include #include #include #include - +#include +#include namespace utils { nlohmann::json loadJson(const std::string& path); + + // Recursively loads all .json files under directory + // If called multiple times with same directory, returns a cached result. + std::vector loadAllJson(const std::string& directory); + + // looks up directory in settings. Returns element matching name. + nlohmann::json loadJson(const std::string& type, const std::string& name); void saveJson(const nlohmann::json& data, const std::string& path); + template std::shared_ptr loadDFromJson(const nlohmann::json& data) { + try { + return std::shared_ptr(new D(loadJson(data["type"], data["name"]), data)); + } catch(std::invalid_argument& e) { + // Fall back on the data passed in + return std::shared_ptr(new D(data, data)); + } + } + template std::string join(Container parts, std::string joiner) { std::stringstream out; bool isFirst = true; @@ -40,15 +58,4 @@ namespace utils { std::vector> copper2coins(int coppers); std::string getCostString(int coppers); - - // Recursively loads all .json files under directory - /*std::vector loadAll(std::string directory) { - std::vector items; - for(auto path : std::filesystem::recursive_directory_iterator(directory)) { - if(path.path().extension() == ".json") { - items.push_back(Item::instance(loadJson(path.path()))); - } - } - return items; - }*/ } -- cgit v1.2.3