IHandlerBase.h 4.6 KB

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