CRmgTemplateStorage.cpp 2.0 KB

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