ResourceTypeHandler.cpp 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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 Resource::getNameTextID() const
  18. {
  19. if(id.getNum() < GameConstants::RESOURCE_QUANTITY) // OH3 resources
  20. return TextIdentifier("core.restypes", id).get();
  21. return TextIdentifier( "resources", modScope, identifier, "name" ).get();
  22. }
  23. std::string Resource::getNameTranslated() const
  24. {
  25. return LIBRARY->generaltexth->translate(getNameTextID());
  26. }
  27. void Resource::registerIcons(const IconRegistar & cb) const
  28. {
  29. cb(getIconIndex(), 0, "SMALRES", iconSmall);
  30. cb(getIconIndex(), 0, "RESOURCE", iconMedium);
  31. cb(getIconIndex(), 0, "RESOUR82", iconLarge);
  32. }
  33. std::vector<JsonNode> ResourceTypeHandler::loadLegacyData()
  34. {
  35. objects.resize(GameConstants::RESOURCE_QUANTITY);
  36. return std::vector<JsonNode>(GameConstants::RESOURCE_QUANTITY, JsonNode(JsonMap()));
  37. }
  38. std::shared_ptr<Resource> ResourceTypeHandler::loadFromJson(const std::string & scope, const JsonNode & json, const std::string & identifier, size_t index)
  39. {
  40. auto ret = std::make_shared<Resource>();
  41. ret->id = GameResID(index);
  42. ret->modScope = scope;
  43. ret->identifier = identifier;
  44. ret->price = json["price"].Integer();
  45. ret->iconSmall = json["images"]["small"].String();
  46. ret->iconMedium = json["images"]["medium"].String();
  47. ret->iconLarge = json["images"]["large"].String();
  48. if(ret->id.getNum() >= GameConstants::RESOURCE_QUANTITY) // not OH3 resources
  49. LIBRARY->generaltexth->registerString(scope, ret->getNameTextID(), json["name"]);
  50. return ret;
  51. }
  52. const std::vector<std::string> & ResourceTypeHandler::getTypeNames() const
  53. {
  54. static const std::vector<std::string> types = { "resource" };
  55. return types;
  56. }
  57. std::vector<GameResID> ResourceTypeHandler::getAllObjects() const
  58. {
  59. std::vector<GameResID> result;
  60. for (const auto & resource : objects)
  61. result.push_back(resource->getId());
  62. return result;
  63. }
  64. VCMI_LIB_NAMESPACE_END