ResourceTypeHandler.cpp 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. /*
  2. * ResourceTypeHandler.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 "ResourceTypeHandler.h"
  12. #include "../GameLibrary.h"
  13. #include "../json/JsonNode.h"
  14. #include "../texts/CGeneralTextHandler.h"
  15. #include "../texts/TextIdentifier.h"
  16. VCMI_LIB_NAMESPACE_BEGIN
  17. std::string resources::ResourceType::getNameTextID() const
  18. {
  19. return TextIdentifier( "resources", modScope, identifier, "name" ).get();
  20. }
  21. std::string resources::ResourceType::getNameTranslated() const
  22. {
  23. return LIBRARY->generaltexth->translate(getNameTextID());
  24. }
  25. std::vector<JsonNode> ResourceTypeHandler::loadLegacyData()
  26. {
  27. objects.resize(8);
  28. return std::vector<JsonNode>(8, JsonNode(JsonMap()));
  29. }
  30. std::shared_ptr<resources::ResourceType> ResourceTypeHandler::loadObjectImpl(std::string scope, std::string name, const JsonNode & data, size_t index)
  31. {
  32. auto ret = std::make_shared<resources::ResourceType>();
  33. ret->id = GameResID(index);
  34. ret->modScope = scope;
  35. ret->identifier = name;
  36. ret->price = data["price"].Integer();
  37. LIBRARY->generaltexth->registerString(scope, ret->getNameTextID(), data["name"]);
  38. return ret;
  39. }
  40. /// loads single object into game. Scope is namespace of this object, same as name of source mod
  41. void ResourceTypeHandler::loadObject(std::string scope, std::string name, const JsonNode & data)
  42. {
  43. objects.push_back(loadObjectImpl(scope, name, data, objects.size()));
  44. registerObject(scope, "resources", name, objects.back()->getIndex());
  45. }
  46. void ResourceTypeHandler::loadObject(std::string scope, std::string name, const JsonNode & data, size_t index)
  47. {
  48. assert(objects[index] == nullptr); // ensure that this id was not loaded before
  49. objects[index] = loadObjectImpl(scope, name, data, index);
  50. registerObject(scope, "resources", name, objects[index]->getIndex());
  51. }
  52. std::vector<GameResID> ResourceTypeHandler::getAllObjects() const
  53. {
  54. std::vector<GameResID> result;
  55. for (const auto & resource : objects)
  56. result.push_back(resource->id);
  57. return result;
  58. }
  59. VCMI_LIB_NAMESPACE_END