CRmgTemplateStorage.cpp 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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. #include "../CModHandler.h"
  15. VCMI_LIB_NAMESPACE_BEGIN
  16. using namespace rmg;
  17. void CRmgTemplateStorage::loadObject(std::string scope, std::string name, const JsonNode & data, size_t index)
  18. {
  19. //unused
  20. loadObject(scope, name, data);
  21. }
  22. void CRmgTemplateStorage::afterLoadFinalization()
  23. {
  24. for (auto& temp : templates)
  25. {
  26. temp.second.afterLoad();
  27. }
  28. }
  29. void CRmgTemplateStorage::loadObject(std::string scope, std::string name, const JsonNode & data)
  30. {
  31. try
  32. {
  33. JsonDeserializer handler(nullptr, data);
  34. auto fullKey = scope + ":" + name; //actually it's not used
  35. templates[fullKey].setId(fullKey);
  36. templates[fullKey].serializeJson(handler);
  37. templates[fullKey].setName(name);
  38. templates[fullKey].validate();
  39. }
  40. catch(const std::exception & e)
  41. {
  42. logGlobal->error("Template %s has errors. Message: %s.", name, std::string(e.what()));
  43. }
  44. }
  45. std::vector<bool> CRmgTemplateStorage::getDefaultAllowed() const
  46. {
  47. //all templates are allowed
  48. return std::vector<bool>();
  49. }
  50. std::vector<JsonNode> CRmgTemplateStorage::loadLegacyData()
  51. {
  52. return std::vector<JsonNode>();
  53. //it would be cool to load old rmg.txt files
  54. }
  55. const CRmgTemplate * CRmgTemplateStorage::getTemplate(const std::string & templateName) const
  56. {
  57. auto iter = templates.find(templateName);
  58. if(iter==templates.end())
  59. return nullptr;
  60. return &iter->second;
  61. }
  62. std::vector<const CRmgTemplate *> CRmgTemplateStorage::getTemplates() const
  63. {
  64. std::vector<const CRmgTemplate *> result;
  65. result.reserve(templates.size());
  66. for(const auto & i : templates)
  67. {
  68. result.push_back(&i.second);
  69. }
  70. return result;
  71. }
  72. VCMI_LIB_NAMESPACE_END