1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143
|
/*
* IHandlerBase.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
VCMI_LIB_NAMESPACE_BEGIN
class JsonNode;
class Entity;
/// base class for all handlers that can be accessed from mod system
class DLL_LINKAGE IHandlerBase
{
protected:
static std::string getScopeBuiltin();
/// Calls modhandler. Mostly needed to avoid large number of includes in headers
static void registerObject(const std::string & scope, const std::string & type_name, const std::string & name, si32 index);
public:
/// loads all original game data in vector of json nodes
/// dataSize - is number of items that must be loaded (normally - constant from GameConstants)
virtual std::vector<JsonNode> loadLegacyData() = 0;
/// loads single object into game. Scope is namespace of this object, same as name of source mod
virtual void loadObject(std::string scope, std::string name, const JsonNode & data) = 0;
virtual void loadObject(std::string scope, std::string name, const JsonNode & data, size_t index) = 0;
/// allows handlers to alter object configuration before validation and actual load
virtual void beforeValidate(JsonNode & object){};
/// allows handler to load some custom internal data before identifier finalization
virtual void loadCustom(){};
/// allows handler to do post-loading step for validation or integration of loaded data
virtual void afterLoadFinalization(){};
virtual ~IHandlerBase() = default;
};
template <class _ObjectID, class _ObjectBase, class _Object, class _ServiceBase> class CHandlerBase : public _ServiceBase, public IHandlerBase
{
const _Object * getObjectImpl(const int32_t index) const
{
try
{
return objects.at(index).get();
}
catch (const std::out_of_range&)
{
logMod->error("%s id %d is invalid", getTypeNames()[0], index);
throw std::runtime_error("Attempt to access invalid index " + std::to_string(index) + " of type " + getTypeNames().front());
}
}
public:
using ObjectPtr = std::shared_ptr<_Object>;
const Entity * getBaseByIndex(const int32_t index) const override
{
return getObjectImpl(index);
}
const _ObjectBase * getById(const _ObjectID & id) const override
{
return getObjectImpl(id.getNum());
}
const _ObjectBase * getByIndex(const int32_t index) const override
{
return getObjectImpl(index);
}
void forEachBase(const std::function<void(const Entity * entity, bool & stop)> & cb) const override
{
forEachT(cb);
}
void forEach(const std::function<void(const _ObjectBase * entity, bool & stop)> & cb) const override
{
forEachT(cb);
}
void loadObject(std::string scope, std::string name, const JsonNode & data) override
{
objects.push_back(loadFromJson(scope, data, name, objects.size()));
for(const auto & type_name : getTypeNames())
registerObject(scope, type_name, name, objects.back()->getIndex());
}
void loadObject(std::string scope, std::string name, const JsonNode & data, size_t index) override
{
assert(objects[index] == nullptr); // ensure that this id was not loaded before
objects[index] = loadFromJson(scope, data, name, index);
for(const auto & type_name : getTypeNames())
registerObject(scope, type_name, name, objects[index]->getIndex());
}
const _Object * operator[] (const _ObjectID id) const
{
return getObjectImpl(id.getNum());
}
const _Object * operator[] (int32_t index) const
{
return getObjectImpl(index);
}
size_t size() const
{
return objects.size();
}
protected:
virtual ObjectPtr loadFromJson(const std::string & scope, const JsonNode & json, const std::string & identifier, size_t index) = 0;
virtual const std::vector<std::string> & getTypeNames() const = 0;
template<typename ItemType>
void forEachT(const std::function<void(const ItemType *, bool &)> & cb) const
{
bool stop = false;
for(auto & object : objects)
{
cb(object.get(), stop);
if(stop)
break;
}
}
public: //todo: make private
std::vector<ObjectPtr> objects;
};
VCMI_LIB_NAMESPACE_END
|