IHandlerBase.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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. if(index < 0 || index >= objects.size())
  41. {
  42. logMod->error("%s id %d is invalid", getTypeNames()[0], index);
  43. throw std::runtime_error("Attempt to access invalid index " + std::to_string(index) + " of type " + getTypeNames().front());
  44. }
  45. return objects[index].get();
  46. }
  47. public:
  48. using ObjectPtr = std::shared_ptr<_Object>;
  49. const Entity * getBaseByIndex(const int32_t index) const override
  50. {
  51. return getObjectImpl(index);
  52. }
  53. const _ObjectBase * getById(const _ObjectID & id) const override
  54. {
  55. return getObjectImpl(id.getNum());
  56. }
  57. const _ObjectBase * getByIndex(const int32_t index) const override
  58. {
  59. return getObjectImpl(index);
  60. }
  61. void forEachBase(const std::function<void(const Entity * entity, bool & stop)> & cb) const override
  62. {
  63. forEachT(cb);
  64. }
  65. void forEach(const std::function<void(const _ObjectBase * entity, bool & stop)> & cb) const override
  66. {
  67. forEachT(cb);
  68. }
  69. void loadObject(std::string scope, std::string name, const JsonNode & data) override
  70. {
  71. objects.push_back(loadFromJson(scope, data, name, objects.size()));
  72. for(const auto & type_name : getTypeNames())
  73. registerObject(scope, type_name, name, objects.back()->getIndex());
  74. }
  75. void loadObject(std::string scope, std::string name, const JsonNode & data, size_t index) override
  76. {
  77. assert(objects[index] == nullptr); // ensure that this id was not loaded before
  78. objects[index] = loadFromJson(scope, data, name, index);
  79. for(const auto & type_name : getTypeNames())
  80. registerObject(scope, type_name, name, objects[index]->getIndex());
  81. }
  82. const _Object * operator[] (const _ObjectID id) const
  83. {
  84. return getObjectImpl(id.getNum());
  85. }
  86. const _Object * operator[] (int32_t index) const
  87. {
  88. return getObjectImpl(index);
  89. }
  90. size_t size() const
  91. {
  92. return objects.size();
  93. }
  94. protected:
  95. virtual ObjectPtr loadFromJson(const std::string & scope, const JsonNode & json, const std::string & identifier, size_t index) = 0;
  96. virtual const std::vector<std::string> & getTypeNames() const = 0;
  97. template<typename ItemType>
  98. void forEachT(const std::function<void(const ItemType *, bool &)> & cb) const
  99. {
  100. bool stop = false;
  101. for(auto & object : objects)
  102. {
  103. cb(object.get(), stop);
  104. if(stop)
  105. break;
  106. }
  107. }
  108. public: //todo: make private
  109. std::vector<ObjectPtr> objects;
  110. };
  111. VCMI_LIB_NAMESPACE_END