2013-04-21 15:49:26 +03:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 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
|
|
|
|
*
|
|
|
|
*/
|
2014-03-07 16:21:09 +03:00
|
|
|
#include "../lib/ConstTransitivePtr.h"
|
|
|
|
#include "VCMI_Lib.h"
|
|
|
|
#include "CModHandler.h"
|
2013-04-21 15:49:26 +03:00
|
|
|
|
|
|
|
class JsonNode;
|
|
|
|
|
|
|
|
/// base class for all handlers that can be accessed from mod system
|
|
|
|
class DLL_LINKAGE IHandlerBase
|
|
|
|
{
|
|
|
|
// there also should be private member with such signature:
|
|
|
|
// Object * loadFromJson(const JsonNode & json);
|
|
|
|
// where Object is type of data loaded by handler
|
|
|
|
// primary used in loadObject methods
|
|
|
|
|
|
|
|
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(size_t dataSize) = 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;
|
|
|
|
|
2014-05-18 17:47:18 +03:00
|
|
|
/// allows handlers to alter object configuration before validation and actual load
|
|
|
|
virtual void beforeValidate(JsonNode & object){};
|
|
|
|
|
2013-07-21 17:19:29 +03:00
|
|
|
/// allows handler to do post-loading step for validation or integration of loaded data
|
|
|
|
virtual void afterLoadFinalization(){};
|
|
|
|
|
2013-04-21 15:49:26 +03:00
|
|
|
/**
|
|
|
|
* Gets a list of objects that are allowed by default on maps
|
|
|
|
*
|
|
|
|
* @return a list of allowed objects, the index is the object id
|
|
|
|
*/
|
|
|
|
virtual std::vector<bool> getDefaultAllowed() const = 0;
|
|
|
|
|
|
|
|
virtual ~IHandlerBase(){}
|
2014-03-07 16:21:09 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
template <class _ObjectID, class _Object> class CHandlerBase: public IHandlerBase
|
|
|
|
{
|
|
|
|
public:
|
2014-03-08 22:02:47 +03:00
|
|
|
virtual ~CHandlerBase()
|
|
|
|
{
|
|
|
|
for(auto & o : objects)
|
|
|
|
{
|
|
|
|
o.dellNull();
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2014-03-07 16:21:09 +03:00
|
|
|
void loadObject(std::string scope, std::string name, const JsonNode & data) override
|
|
|
|
{
|
2014-03-08 22:02:47 +03:00
|
|
|
auto type_name = getTypeName();
|
|
|
|
auto object = loadFromJson(data);
|
|
|
|
object->id = _ObjectID(objects.size());
|
2014-03-07 16:21:09 +03:00
|
|
|
|
2014-03-08 22:02:47 +03:00
|
|
|
objects.push_back(object);
|
2014-03-07 16:21:09 +03:00
|
|
|
|
2014-03-08 22:02:47 +03:00
|
|
|
VLC->modh->identifiers.registerObject(scope, type_name, name, object->id);
|
2014-03-07 16:21:09 +03:00
|
|
|
}
|
|
|
|
void loadObject(std::string scope, std::string name, const JsonNode & data, size_t index) override
|
|
|
|
{
|
2014-03-08 22:02:47 +03:00
|
|
|
auto type_name = getTypeName();
|
|
|
|
auto object = loadFromJson(data);
|
|
|
|
object->id = _ObjectID(index);
|
2014-03-07 16:21:09 +03:00
|
|
|
|
|
|
|
|
2014-03-08 22:02:47 +03:00
|
|
|
assert(objects[index] == nullptr); // ensure that this id was not loaded before
|
|
|
|
objects[index] = object;
|
2014-03-07 16:21:09 +03:00
|
|
|
|
2014-03-08 22:02:47 +03:00
|
|
|
VLC->modh->identifiers.registerObject(scope,type_name, name, object->id);
|
2014-03-07 16:21:09 +03:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
ConstTransitivePtr<_Object> operator[] (const _ObjectID id) const
|
2014-03-08 22:02:47 +03:00
|
|
|
{
|
|
|
|
const auto raw_id = id.toEnum();
|
|
|
|
|
|
|
|
if (raw_id < 0 || raw_id >= objects.size())
|
|
|
|
{
|
|
|
|
logGlobal->errorStream() << getTypeName() << " id " << static_cast<si64>(raw_id) << "is invalid";
|
|
|
|
throw std::runtime_error ("internal error");
|
|
|
|
}
|
|
|
|
|
|
|
|
return objects[raw_id];
|
2014-03-07 16:21:09 +03:00
|
|
|
}
|
|
|
|
protected:
|
2014-03-08 22:02:47 +03:00
|
|
|
virtual _Object * loadFromJson(const JsonNode & json) = 0;
|
|
|
|
virtual const std::string getTypeName() const = 0;
|
2014-03-07 16:21:09 +03:00
|
|
|
public: //todo: make private
|
2014-03-08 22:02:47 +03:00
|
|
|
std::vector<ConstTransitivePtr<_Object>> objects;
|
2014-03-07 16:21:09 +03:00
|
|
|
};
|