CBonusTypeHandler.h 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. /*
  2. * CBonusTypeHandler.h, 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. #pragma once
  11. #include "IBonusTypeHandler.h"
  12. #include "IHandlerBase.h"
  13. #include "HeroBonus.h"
  14. VCMI_LIB_NAMESPACE_BEGIN
  15. class JsonNode;
  16. typedef Bonus::BonusType BonusTypeID;
  17. class MacroString
  18. {
  19. struct Item
  20. {
  21. enum ItemType
  22. {
  23. STRING, MACRO
  24. };
  25. Item(ItemType _type, std::string _value): type(_type), value(_value){};
  26. ItemType type;
  27. std::string value; //constant string or macro name
  28. };
  29. std::vector<Item> items;
  30. public:
  31. typedef std::function<std::string(const std::string &)> GetValue;
  32. MacroString() = default;
  33. ~MacroString() = default;
  34. explicit MacroString(const std::string & format);
  35. std::string build(const GetValue & getValue) const;
  36. };
  37. class DLL_LINKAGE CBonusType
  38. {
  39. public:
  40. CBonusType();
  41. ~CBonusType();
  42. template <typename Handler> void serialize(Handler & h, const int version)
  43. {
  44. h & icon;
  45. h & nameTemplate;
  46. h & descriptionTemplate;
  47. h & hidden;
  48. if (!h.saving)
  49. buildMacros();
  50. }
  51. private:
  52. void buildMacros();
  53. MacroString name, description;
  54. friend class CBonusTypeHandler;
  55. std::string icon;
  56. std::string nameTemplate, descriptionTemplate;
  57. bool hidden;
  58. };
  59. class DLL_LINKAGE CBonusTypeHandler : public IBonusTypeHandler
  60. {
  61. public:
  62. CBonusTypeHandler();
  63. virtual ~CBonusTypeHandler();
  64. std::string bonusToString(const std::shared_ptr<Bonus> & bonus, const IBonusBearer * bearer, bool description) const override;
  65. std::string bonusToGraphics(const std::shared_ptr<Bonus> & bonus) const override;
  66. template <typename Handler> void serialize(Handler & h, const int version)
  67. {
  68. //for now always use up to date configuration
  69. //once modded bonus type will be implemented, serialize only them
  70. std::vector<CBonusType> ignore;
  71. h & ignore;
  72. }
  73. private:
  74. void load();
  75. void load(const JsonNode & config);
  76. void loadItem(const JsonNode & source, CBonusType & dest);
  77. std::vector<CBonusType> bonusTypes; //index = BonusTypeID
  78. };
  79. VCMI_LIB_NAMESPACE_END
  80. #ifndef INSTANTIATE_CBonusTypeHandler_HERE
  81. extern template class std::vector<VCMI_LIB_WRAP_NAMESPACE(CBonusType)>;
  82. #endif