IHandlerBase.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. #pragma once
  2. /*
  3. * IHandlerBase.h, part of VCMI engine
  4. *
  5. * Authors: listed in file AUTHORS in main folder
  6. *
  7. * License: GNU General Public License v2.0 or later
  8. * Full text of license available in license.txt file, in main folder
  9. *
  10. */
  11. #include "../lib/ConstTransitivePtr.h"
  12. #include "VCMI_Lib.h"
  13. //#include "CModHandler.h"
  14. class JsonNode;
  15. /// base class for all handlers that can be accessed from mod system
  16. class DLL_LINKAGE IHandlerBase
  17. {
  18. // there also should be private member with such signature:
  19. // Object * loadFromJson(const JsonNode & json);
  20. // where Object is type of data loaded by handler
  21. // primary used in loadObject methods
  22. protected:
  23. /// Calls modhandler. Mostly needed to avoid large number of includes in headers
  24. void registerObject(std::string scope, std::string type_name, std::string name, si32 index);
  25. std::string normalizeIdentifier(const std::string & scope, const std::string & remoteScope, const std::string & identifier) const;
  26. public:
  27. /// loads all original game data in vector of json nodes
  28. /// dataSize - is number of items that must be loaded (normally - constant from GameConstants)
  29. virtual std::vector<JsonNode> loadLegacyData(size_t dataSize) = 0;
  30. /// loads single object into game. Scope is namespace of this object, same as name of source mod
  31. virtual void loadObject(std::string scope, std::string name, const JsonNode & data) = 0;
  32. virtual void loadObject(std::string scope, std::string name, const JsonNode & data, size_t index) = 0;
  33. /// allows handlers to alter object configuration before validation and actual load
  34. virtual void beforeValidate(JsonNode & object){};
  35. /// allows handler to do post-loading step for validation or integration of loaded data
  36. virtual void afterLoadFinalization(){};
  37. /**
  38. * Gets a list of objects that are allowed by default on maps
  39. *
  40. * @return a list of allowed objects, the index is the object id
  41. */
  42. virtual std::vector<bool> getDefaultAllowed() const = 0;
  43. virtual ~IHandlerBase(){}
  44. };
  45. template <class _ObjectID, class _Object> class CHandlerBase: public IHandlerBase
  46. {
  47. public:
  48. virtual ~CHandlerBase()
  49. {
  50. for(auto & o : objects)
  51. {
  52. o.dellNull();
  53. }
  54. }
  55. void loadObject(std::string scope, std::string name, const JsonNode & data) override
  56. {
  57. auto type_name = getTypeName();
  58. auto object = loadFromJson(data, normalizeIdentifier(scope, "core", name));
  59. object->id = _ObjectID(objects.size());
  60. objects.push_back(object);
  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 type_name = getTypeName();
  66. auto object = loadFromJson(data, normalizeIdentifier(scope, "core", name));
  67. object->id = _ObjectID(index);
  68. assert(objects[index] == nullptr); // ensure that this id was not loaded before
  69. objects[index] = object;
  70. registerObject(scope,type_name, name, object->id);
  71. }
  72. ConstTransitivePtr<_Object> operator[] (const _ObjectID id) const
  73. {
  74. const auto raw_id = id.toEnum();
  75. if (raw_id < 0 || raw_id >= objects.size())
  76. {
  77. logGlobal->errorStream() << getTypeName() << " id " << static_cast<si64>(raw_id) << "is invalid";
  78. throw std::runtime_error ("internal error");
  79. }
  80. return objects[raw_id];
  81. }
  82. protected:
  83. virtual _Object * loadFromJson(const JsonNode & json, const std::string & identifier) = 0;
  84. virtual const std::string getTypeName() const = 0;
  85. public: //todo: make private
  86. std::vector<ConstTransitivePtr<_Object>> objects;
  87. };