2
0

CObjectClassesHandler.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /*
  2. * CObjectClassesHandler.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 "../IHandlerBase.h"
  12. #include "../JsonNode.h"
  13. VCMI_LIB_NAMESPACE_BEGIN
  14. class CRandomGenerator;
  15. class AObjectTypeHandler;
  16. class ObjectTemplate;
  17. struct SObjectSounds;
  18. struct DLL_LINKAGE CompoundMapObjectID
  19. {
  20. si32 primaryID;
  21. si32 secondaryID;
  22. CompoundMapObjectID(si32 primID, si32 secID) : primaryID(primID), secondaryID(secID) {};
  23. bool operator<(const CompoundMapObjectID& other) const
  24. {
  25. if(this->primaryID != other.primaryID)
  26. return this->primaryID < other.primaryID;
  27. else
  28. return this->secondaryID < other.secondaryID;
  29. }
  30. bool operator==(const CompoundMapObjectID& other) const
  31. {
  32. return (this->primaryID == other.primaryID) && (this->secondaryID == other.secondaryID);
  33. }
  34. };
  35. class CGObjectInstance;
  36. using TObjectTypeHandler = std::shared_ptr<AObjectTypeHandler>;
  37. /// Class responsible for creation of adventure map objects of specific type
  38. class DLL_LINKAGE ObjectClass
  39. {
  40. public:
  41. std::string modScope;
  42. std::string identifier;
  43. si32 id;
  44. std::string handlerName; // ID of handler that controls this object, should be determined using handlerConstructor map
  45. JsonNode base;
  46. std::vector<TObjectTypeHandler> objects;
  47. ObjectClass() = default;
  48. std::string getJsonKey() const;
  49. std::string getNameTextID() const;
  50. std::string getNameTranslated() const;
  51. template <typename Handler> void serialize(Handler &h, const int version)
  52. {
  53. h & id;
  54. h & handlerName;
  55. h & base;
  56. h & objects;
  57. h & identifier;
  58. h & modScope;
  59. }
  60. };
  61. /// Main class responsible for creation of all adventure map objects
  62. class DLL_LINKAGE CObjectClassesHandler : public IHandlerBase
  63. {
  64. /// list of object handlers, each of them handles only one type
  65. std::vector<ObjectClass * > objects;
  66. /// map that is filled during contruction with all known handlers. Not serializeable due to usage of std::function
  67. std::map<std::string, std::function<TObjectTypeHandler()> > handlerConstructors;
  68. /// container with H3 templates, used only during loading, no need to serialize it
  69. using TTemplatesContainer = std::multimap<std::pair<si32, si32>, std::shared_ptr<const ObjectTemplate>>;
  70. TTemplatesContainer legacyTemplates;
  71. TObjectTypeHandler loadSubObjectFromJson(const std::string & scope, const std::string & identifier, const JsonNode & entry, ObjectClass * obj, size_t index);
  72. void loadSubObject(const std::string & scope, const std::string & identifier, const JsonNode & entry, ObjectClass * obj);
  73. void loadSubObject(const std::string & scope, const std::string & identifier, const JsonNode & entry, ObjectClass * obj, size_t index);
  74. ObjectClass * loadFromJson(const std::string & scope, const JsonNode & json, const std::string & name, size_t index);
  75. void generateExtraMonolithsForRMG();
  76. public:
  77. CObjectClassesHandler();
  78. ~CObjectClassesHandler();
  79. std::vector<JsonNode> loadLegacyData() override;
  80. void loadObject(std::string scope, std::string name, const JsonNode & data) override;
  81. void loadObject(std::string scope, std::string name, const JsonNode & data, size_t index) override;
  82. void loadSubObject(const std::string & identifier, JsonNode config, MapObjectID ID, MapObjectSubID subID);
  83. void removeSubObject(MapObjectID ID, MapObjectSubID subID);
  84. void beforeValidate(JsonNode & object) override;
  85. void afterLoadFinalization() override;
  86. std::vector<bool> getDefaultAllowed() const override;
  87. /// Queries to detect loaded objects
  88. std::set<MapObjectID> knownObjects() const;
  89. std::set<MapObjectSubID> knownSubObjects(MapObjectID primaryID) const;
  90. /// returns handler for specified object (ID-based). ObjectHandler keeps ownership
  91. TObjectTypeHandler getHandlerFor(MapObjectID type, MapObjectSubID subtype) const;
  92. TObjectTypeHandler getHandlerFor(const std::string & scope, const std::string & type, const std::string & subtype) const;
  93. TObjectTypeHandler getHandlerFor(CompoundMapObjectID compoundIdentifier) const;
  94. std::string getObjectName(MapObjectID type, MapObjectSubID subtype) const;
  95. SObjectSounds getObjectSounds(MapObjectID type, MapObjectSubID subtype) const;
  96. /// Returns handler string describing the handler (for use in client)
  97. std::string getObjectHandlerName(MapObjectID type) const;
  98. std::string getJsonKey(MapObjectID type) const;
  99. template <typename Handler> void serialize(Handler &h, const int version)
  100. {
  101. h & objects;
  102. }
  103. };
  104. VCMI_LIB_NAMESPACE_END