2
0

IHandlerBase.h 4.5 KB

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