CRmgTemplateStorage.cpp 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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::loadObject(std::string scope, std::string name, const JsonNode & data)
  23. {
  24. try
  25. {
  26. JsonDeserializer handler(nullptr, data);
  27. auto fullKey = normalizeIdentifier(scope, CModHandler::scopeBuiltin(), name); //actually it's not used
  28. templates[fullKey].setId(name);
  29. templates[fullKey].serializeJson(handler);
  30. templates[fullKey].validate();
  31. }
  32. catch(const std::exception & e)
  33. {
  34. logGlobal->error("Template %s has errors. Message: %s.", name, std::string(e.what()));
  35. }
  36. }
  37. std::vector<bool> CRmgTemplateStorage::getDefaultAllowed() const
  38. {
  39. //all templates are allowed
  40. return std::vector<bool>();
  41. }
  42. std::vector<JsonNode> CRmgTemplateStorage::loadLegacyData(size_t dataSize)
  43. {
  44. return std::vector<JsonNode>();
  45. //it would be cool to load old rmg.txt files
  46. }
  47. const CRmgTemplate * CRmgTemplateStorage::getTemplate(const std::string & templateName) const
  48. {
  49. auto iter = templates.find(templateName);
  50. if(iter==templates.end())
  51. return nullptr;
  52. return &iter->second;
  53. }
  54. std::vector<const CRmgTemplate *> CRmgTemplateStorage::getTemplates() const
  55. {
  56. std::vector<const CRmgTemplate *> result;
  57. for(auto i=templates.cbegin(); i!=templates.cend(); ++i)
  58. {
  59. result.push_back(&i->second);
  60. }
  61. return result;
  62. }
  63. VCMI_LIB_NAMESPACE_END