universe.tpp 932 B

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. //
  2. // universe.tpp
  3. // engine_base
  4. //
  5. // Created by Sam Jaffe on 3/15/23.
  6. //
  7. #pragma once
  8. #include <filesystem>
  9. #include <fstream>
  10. #include <json/json.h>
  11. #include <engine/universe.h>
  12. namespace engine {
  13. template <typename T>
  14. void Universe::load(std::string const & name, std::string const & fallback) {
  15. std::filesystem::path location = config().property(name, fallback);
  16. if (!std::filesystem::exists(location)) { return; }
  17. auto import = [this](auto & file) {
  18. Json::Value json;
  19. std::ifstream in(file.string());
  20. in >> json;
  21. std::shared_ptr<T const> tmp;
  22. if (json.isArray()) {
  23. for (Json::Value const & elm : json) {
  24. jsonizer().from_json(tmp, elm);
  25. }
  26. } else {
  27. jsonizer().from_json(tmp, json);
  28. }
  29. };
  30. if (std::filesystem::is_directory(location)) {
  31. std::for_each(std::filesystem::directory_iterator(location), {}, import);
  32. } else {
  33. import(location);
  34. }
  35. }
  36. }