IHandlerBase.h 4.7 KB

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