1
0
mirror of https://github.com/vcmi/vcmi.git synced 2024-11-28 08:48:48 +02:00
vcmi/lib/CBonusTypeHandler.h

104 lines
2.3 KiB
C
Raw Normal View History

/*
* CBonusTypeHandler.h, part of VCMI engine
*
* Authors: listed in file AUTHORS in main folder
*
* License: GNU General Public License v2.0 or later
* Full text of license available in license.txt file, in main folder
*
*/
#pragma once
#include "IBonusTypeHandler.h"
#include "IHandlerBase.h"
#include "HeroBonus.h"
VCMI_LIB_NAMESPACE_BEGIN
class JsonNode;
typedef Bonus::BonusType BonusTypeID;
class MacroString
{
struct Item
{
enum ItemType
{
STRING, MACRO
};
Item(ItemType _type, std::string _value): type(_type), value(_value){};
ItemType type;
std::string value; //constant string or macro name
};
std::vector<Item> items;
public:
2017-08-08 16:53:01 +02:00
typedef std::function<std::string(const std::string &)> GetValue;
2017-08-08 16:53:01 +02:00
MacroString() = default;
~MacroString() = default;
explicit MacroString(const std::string & format);
2017-08-08 16:53:01 +02:00
std::string build(const GetValue & getValue) const;
};
class DLL_LINKAGE CBonusType
{
public:
CBonusType();
~CBonusType();
2017-08-08 16:53:01 +02:00
template <typename Handler> void serialize(Handler & h, const int version)
{
h & icon;
h & nameTemplate;
h & descriptionTemplate;
h & hidden;
if (!h.saving)
buildMacros();
}
private:
void buildMacros();
MacroString name, description;
friend class CBonusTypeHandler;
std::string icon;
std::string nameTemplate, descriptionTemplate;
bool hidden;
};
class DLL_LINKAGE CBonusTypeHandler : public IBonusTypeHandler
{
public:
CBonusTypeHandler();
virtual ~CBonusTypeHandler();
2017-08-08 16:53:01 +02:00
std::string bonusToString(const std::shared_ptr<Bonus> & bonus, const IBonusBearer * bearer, bool description) const override;
std::string bonusToGraphics(const std::shared_ptr<Bonus> & bonus) const override;
2017-08-08 16:53:01 +02:00
template <typename Handler> void serialize(Handler & h, const int version)
{
2017-08-08 16:53:01 +02:00
//for now always use up to date configuration
//once modded bonus type will be implemented, serialize only them
std::vector<CBonusType> ignore;
h & ignore;
}
private:
2017-08-08 16:53:01 +02:00
void load();
void load(const JsonNode & config);
void loadItem(const JsonNode & source, CBonusType & dest);
std::vector<CBonusType> bonusTypes; //index = BonusTypeID
};
2017-08-08 16:53:01 +02:00
VCMI_LIB_NAMESPACE_END
2017-08-08 16:53:01 +02:00
#ifndef INSTANTIATE_CBonusTypeHandler_HERE
extern template class std::vector<VCMI_LIB_WRAP_NAMESPACE(CBonusType)>;
2017-08-08 16:53:01 +02:00
#endif