| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546 |
- //
- // universe.tpp
- // engine_base
- //
- // Created by Sam Jaffe on 3/15/23.
- //
- #pragma once
- #include <filesystem>
- #include <fstream>
- #include <json/json.h>
- #include <engine/config.h>
- #include <engine/universe.h>
- #include <serializer/jsonizer.tpp>
- namespace engine {
- template <typename T>
- void Universe::load(std::string const & name, std::string const & fallback) {
- namespace fs = std::filesystem;
- fs::path location = config().property(name, fallback);
- if (!fs::exists(location)) { return; }
- auto import = [this](fs::path const & file) {
- Json::Value json;
- std::ifstream in(file.string());
- in >> json;
- std::shared_ptr<T const> tmp;
- if (json.isArray()) {
- for (Json::Value const & elm : json) {
- jsonizer().from_json(tmp, elm);
- }
- } else {
- jsonizer().from_json(tmp, json);
- }
- };
- if (fs::is_directory(location)) {
- std::for_each(fs::directory_iterator(location), {}, import);
- } else {
- import(location);
- }
- }
- }
|