IHandlerBase.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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. const Entity * getBaseByIndex(const int32_t index) const override
  58. {
  59. return getObjectImpl(index);
  60. }
  61. const _ObjectBase * getById(const _ObjectID & id) const override
  62. {
  63. return getObjectImpl(id.getNum());
  64. }
  65. const _ObjectBase * getByIndex(const int32_t index) const override
  66. {
  67. return getObjectImpl(index);
  68. }
  69. void forEachBase(const std::function<void(const Entity * entity, bool & stop)> & cb) const override
  70. {
  71. forEachT(cb);
  72. }
  73. void forEach(const std::function<void(const _ObjectBase * entity, bool & stop)> & cb) const override
  74. {
  75. forEachT(cb);
  76. }
  77. void loadObject(std::string scope, std::string name, const JsonNode & data) override
  78. {
  79. auto object = loadFromJson(scope, data, name, objects.size());
  80. objects.push_back(object);
  81. for(const auto & type_name : getTypeNames())
  82. registerObject(scope, type_name, name, object->getIndex());
  83. }
  84. void loadObject(std::string scope, std::string name, const JsonNode & data, size_t index) override
  85. {
  86. auto object = loadFromJson(scope, data, name, index);
  87. assert(objects[index] == nullptr); // ensure that this id was not loaded before
  88. objects[index] = object;
  89. for(const auto & type_name : getTypeNames())
  90. registerObject(scope, type_name, name, object->getIndex());
  91. }
  92. const _Object * operator[] (const _ObjectID id) const
  93. {
  94. return getObjectImpl(id.getNum());
  95. }
  96. const _Object * operator[] (int32_t index) const
  97. {
  98. return getObjectImpl(index);
  99. }
  100. size_t size() const
  101. {
  102. return objects.size();
  103. }
  104. protected:
  105. virtual _Object * loadFromJson(const std::string & scope, const JsonNode & json, const std::string & identifier, size_t index) = 0;
  106. virtual const std::vector<std::string> & getTypeNames() const = 0;
  107. template<typename ItemType>
  108. void forEachT(const std::function<void(const ItemType *, bool &)> & cb) const
  109. {
  110. bool stop = false;
  111. for(auto & object : objects)
  112. {
  113. cb(object.get(), stop);
  114. if(stop)
  115. break;
  116. }
  117. }
  118. public: //todo: make private
  119. std::vector<ConstTransitivePtr<_Object>> objects;
  120. };
  121. VCMI_LIB_NAMESPACE_END