CBonusTypeHandler.h 2.2 KB

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