IHandlerBase.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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. static std::string getScopeBuiltin();
  21. /// Calls modhandler. Mostly needed to avoid large number of includes in headers
  22. static void registerObject(const std::string & scope, const std::string & type_name, const std::string & name, si32 index);
  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() = 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. virtual ~IHandlerBase() = default;
  37. };
  38. template <class _ObjectID, class _ObjectBase, class _Object, class _ServiceBase> class CHandlerBase : public _ServiceBase, public IHandlerBase
  39. {
  40. const _Object * getObjectImpl(const int32_t index) const
  41. {
  42. if(index < 0 || index >= objects.size())
  43. {
  44. logMod->error("%s id %d is invalid", getTypeNames()[0], index);
  45. throw std::runtime_error("Attempt to access invalid index " + std::to_string(index) + " of type " + getTypeNames().front());
  46. }
  47. return objects[index].get();
  48. }
  49. public:
  50. virtual ~CHandlerBase()
  51. {
  52. for(auto & o : objects)
  53. {
  54. o.dellNull();
  55. }
  56. }
  57. _Object * getObjectWriteable(const int32_t index)
  58. {
  59. if(index < 0 || index >= objects.size())
  60. {
  61. logMod->error("%s id %d is invalid", getTypeNames()[0], index);
  62. throw std::runtime_error("Attempt to access invalid index " + std::to_string(index) + " of type " + getTypeNames().front());
  63. }
  64. return objects[index].get();
  65. }
  66. const Entity * getBaseByIndex(const int32_t index) const override
  67. {
  68. return getObjectImpl(index);
  69. }
  70. const _ObjectBase * getById(const _ObjectID & id) const override
  71. {
  72. return getObjectImpl(id.getNum());
  73. }
  74. const _ObjectBase * getByIndex(const int32_t index) const override
  75. {
  76. return getObjectImpl(index);
  77. }
  78. void forEachBase(const std::function<void(const Entity * entity, bool & stop)> & cb) const override
  79. {
  80. forEachT(cb);
  81. }
  82. void forEach(const std::function<void(const _ObjectBase * entity, bool & stop)> & cb) const override
  83. {
  84. forEachT(cb);
  85. }
  86. void loadObject(std::string scope, std::string name, const JsonNode & data) override
  87. {
  88. auto object = loadFromJson(scope, data, name, objects.size());
  89. objects.push_back(object);
  90. for(const auto & type_name : getTypeNames())
  91. registerObject(scope, type_name, name, object->getIndex());
  92. }
  93. void loadObject(std::string scope, std::string name, const JsonNode & data, size_t index) override
  94. {
  95. auto object = loadFromJson(scope, data, name, index);
  96. assert(objects[index] == nullptr); // ensure that this id was not loaded before
  97. objects[index] = object;
  98. for(const auto & type_name : getTypeNames())
  99. registerObject(scope, type_name, name, object->getIndex());
  100. }
  101. const _Object * operator[] (const _ObjectID id) const
  102. {
  103. return getObjectImpl(id.getNum());
  104. }
  105. const _Object * operator[] (int32_t index) const
  106. {
  107. return getObjectImpl(index);
  108. }
  109. void updateEntity(int32_t index, const JsonNode & data)
  110. {
  111. if(index < 0 || index >= objects.size())
  112. {
  113. logMod->error("%s id %d is invalid", getTypeNames()[0], index);
  114. }
  115. else
  116. {
  117. objects.at(index)->updateFrom(data);
  118. }
  119. }
  120. size_t size() const
  121. {
  122. return objects.size();
  123. }
  124. protected:
  125. virtual _Object * loadFromJson(const std::string & scope, const JsonNode & json, const std::string & identifier, size_t index) = 0;
  126. virtual const std::vector<std::string> & getTypeNames() const = 0;
  127. template<typename ItemType>
  128. void forEachT(const std::function<void(const ItemType *, bool &)> & cb) const
  129. {
  130. bool stop = false;
  131. for(auto & object : objects)
  132. {
  133. cb(object.get(), stop);
  134. if(stop)
  135. break;
  136. }
  137. }
  138. public: //todo: make private
  139. std::vector<ConstTransitivePtr<_Object>> objects;
  140. };
  141. VCMI_LIB_NAMESPACE_END