IHandlerBase.h 4.0 KB

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