IHandlerBase.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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. class JsonNode;
  14. /// base class for all handlers that can be accessed from mod system
  15. class DLL_LINKAGE IHandlerBase
  16. {
  17. // there also should be private member with such signature:
  18. // Object * loadFromJson(const JsonNode & json);
  19. // where Object is type of data loaded by handler
  20. // primary used in loadObject methods
  21. protected:
  22. /// Calls modhandler. Mostly needed to avoid large number of includes in headers
  23. void registerObject(std::string scope, std::string type_name, std::string name, si32 index);
  24. std::string normalizeIdentifier(const std::string & scope, const std::string & remoteScope, const std::string & identifier) const;
  25. public:
  26. /// loads all original game data in vector of json nodes
  27. /// dataSize - is number of items that must be loaded (normally - constant from GameConstants)
  28. virtual std::vector<JsonNode> loadLegacyData(size_t dataSize) = 0;
  29. /// loads single object into game. Scope is namespace of this object, same as name of source mod
  30. virtual void loadObject(std::string scope, std::string name, const JsonNode & data) = 0;
  31. virtual void loadObject(std::string scope, std::string name, const JsonNode & data, size_t index) = 0;
  32. /// allows handlers to alter object configuration before validation and actual load
  33. virtual void beforeValidate(JsonNode & object){};
  34. /// allows handler to load some custom internal data before identifier finalization
  35. virtual void loadCustom(){};
  36. /// allows handler to do post-loading step for validation or integration of loaded data
  37. virtual void afterLoadFinalization(){};
  38. /**
  39. * Gets a list of objects that are allowed by default on maps
  40. *
  41. * @return a list of allowed objects, the index is the object id
  42. */
  43. virtual std::vector<bool> getDefaultAllowed() const = 0;
  44. virtual ~IHandlerBase(){}
  45. };
  46. template <class _ObjectID, class _Object> class CHandlerBase: public IHandlerBase
  47. {
  48. public:
  49. virtual ~CHandlerBase()
  50. {
  51. for(auto & o : objects)
  52. {
  53. o.dellNull();
  54. }
  55. }
  56. void loadObject(std::string scope, std::string name, const JsonNode & data) override
  57. {
  58. auto object = loadFromJson(data, normalizeIdentifier(scope, "core", name), objects.size());
  59. objects.push_back(object);
  60. for(auto type_name : getTypeNames())
  61. registerObject(scope, type_name, name, object->id);
  62. }
  63. void loadObject(std::string scope, std::string name, const JsonNode & data, size_t index) override
  64. {
  65. auto object = loadFromJson(data, normalizeIdentifier(scope, "core", name), index);
  66. assert(objects[index] == nullptr); // ensure that this id was not loaded before
  67. objects[index] = object;
  68. for(auto type_name : getTypeNames())
  69. registerObject(scope, type_name, name, object->id);
  70. }
  71. ConstTransitivePtr<_Object> operator[] (const _ObjectID id) const
  72. {
  73. const auto raw_id = id.toEnum();
  74. if (raw_id < 0 || raw_id >= objects.size())
  75. {
  76. logMod->error("%s id %d is invalid", getTypeNames()[0], static_cast<si64>(raw_id));
  77. throw std::runtime_error("internal error");
  78. }
  79. return objects[raw_id];
  80. }
  81. size_t size() const
  82. {
  83. return objects.size();
  84. }
  85. protected:
  86. virtual _Object * loadFromJson(const JsonNode & json, const std::string & identifier, size_t index) = 0;
  87. virtual const std::vector<std::string> & getTypeNames() const = 0;
  88. public: //todo: make private
  89. std::vector<ConstTransitivePtr<_Object>> objects;
  90. };