CRmgTemplateStorage.cpp 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. /*
  2. * CRmgTemplateStorage.cpp, part of VCMI engine
  3. *
  4. * Authors: listed in file AUTHORS in main folder
  5. *
  6. * License: GNU General Public License v2.0 or later
  7. * Full text of license available in license.txt file, in main folder
  8. *
  9. */
  10. #include "StdInc.h"
  11. #include "CRmgTemplateStorage.h"
  12. #include "CRmgTemplate.h"
  13. #include "../serializer/JsonDeserializer.h"
  14. VCMI_LIB_NAMESPACE_BEGIN
  15. using namespace rmg;
  16. void CRmgTemplateStorage::loadObject(std::string scope, std::string name, const JsonNode & data, size_t index)
  17. {
  18. //unused
  19. loadObject(scope, name, data);
  20. }
  21. void CRmgTemplateStorage::afterLoadFinalization()
  22. {
  23. for (auto& temp : templates)
  24. {
  25. temp.second->afterLoad();
  26. }
  27. }
  28. void CRmgTemplateStorage::loadObject(std::string scope, std::string name, const JsonNode & data)
  29. {
  30. try
  31. {
  32. JsonDeserializer handler(nullptr, data);
  33. auto fullKey = scope + ":" + name; //actually it's not used
  34. templates[fullKey]->setId(fullKey);
  35. templates[fullKey]->serializeJson(handler);
  36. templates[fullKey]->setName(name);
  37. templates[fullKey]->validate();
  38. }
  39. catch(const std::exception & e)
  40. {
  41. logGlobal->error("Template %s has errors. Message: %s.", name, std::string(e.what()));
  42. }
  43. }
  44. std::vector<bool> CRmgTemplateStorage::getDefaultAllowed() const
  45. {
  46. //all templates are allowed
  47. return std::vector<bool>();
  48. }
  49. std::vector<JsonNode> CRmgTemplateStorage::loadLegacyData()
  50. {
  51. return std::vector<JsonNode>();
  52. //it would be cool to load old rmg.txt files
  53. }
  54. const CRmgTemplate * CRmgTemplateStorage::getTemplate(const std::string & templateName) const
  55. {
  56. auto iter = templates.find(templateName);
  57. if(iter==templates.end())
  58. return nullptr;
  59. return iter->second.get();
  60. }
  61. std::vector<const CRmgTemplate *> CRmgTemplateStorage::getTemplates() const
  62. {
  63. std::vector<const CRmgTemplate *> result;
  64. result.reserve(templates.size());
  65. for(const auto & i : templates)
  66. {
  67. result.push_back(i.second.get());
  68. }
  69. return result;
  70. }
  71. VCMI_LIB_NAMESPACE_END