universe.tpp 986 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  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/config.h>
  12. #include <engine/universe.h>
  13. #include <serializer/jsonizer.tpp>
  14. namespace engine {
  15. template <typename T>
  16. void Universe::load(std::string const & name, std::string const & fallback) {
  17. namespace fs = std::filesystem;
  18. fs::path location = config().property(name, fallback);
  19. if (!fs::exists(location)) { return; }
  20. auto import = [this](fs::path const & file) {
  21. Json::Value json;
  22. std::ifstream in(file.string());
  23. in >> json;
  24. std::shared_ptr<T const> tmp;
  25. if (json.isArray()) {
  26. for (Json::Value const & elm : json) {
  27. jsonizer().from_json(tmp, elm);
  28. }
  29. } else {
  30. jsonizer().from_json(tmp, json);
  31. }
  32. };
  33. if (fs::is_directory(location)) {
  34. std::for_each(fs::directory_iterator(location), {}, import);
  35. } else {
  36. import(location);
  37. }
  38. }
  39. }