universe.tpp 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  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. if (file.extension() != "json") { return; }
  22. Json::Value json;
  23. std::ifstream in(file.string());
  24. in >> json;
  25. std::shared_ptr<T const> tmp;
  26. if (json.isArray()) {
  27. for (Json::Value const & elm : json) {
  28. jsonizer().from_json(tmp, elm);
  29. }
  30. } else {
  31. jsonizer().from_json(tmp, json);
  32. }
  33. };
  34. if (fs::is_directory(location)) {
  35. std::for_each(fs::directory_iterator(location), {}, import);
  36. } else {
  37. import(location);
  38. }
  39. }
  40. }