CObjectClassesHandler.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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. };
  52. /// Main class responsible for creation of all adventure map objects
  53. class DLL_LINKAGE CObjectClassesHandler : public IHandlerBase
  54. {
  55. /// list of object handlers, each of them handles only one type
  56. std::vector<ObjectClass * > objects;
  57. /// map that is filled during contruction with all known handlers. Not serializeable due to usage of std::function
  58. std::map<std::string, std::function<TObjectTypeHandler()> > handlerConstructors;
  59. /// container with H3 templates, used only during loading, no need to serialize it
  60. using TTemplatesContainer = std::multimap<std::pair<si32, si32>, std::shared_ptr<const ObjectTemplate>>;
  61. TTemplatesContainer legacyTemplates;
  62. TObjectTypeHandler loadSubObjectFromJson(const std::string & scope, const std::string & identifier, const JsonNode & entry, ObjectClass * obj, size_t index);
  63. void loadSubObject(const std::string & scope, const std::string & identifier, const JsonNode & entry, ObjectClass * obj);
  64. void loadSubObject(const std::string & scope, const std::string & identifier, const JsonNode & entry, ObjectClass * obj, size_t index);
  65. ObjectClass * loadFromJson(const std::string & scope, const JsonNode & json, const std::string & name, size_t index);
  66. void generateExtraMonolithsForRMG();
  67. public:
  68. CObjectClassesHandler();
  69. ~CObjectClassesHandler();
  70. std::vector<JsonNode> loadLegacyData() override;
  71. void loadObject(std::string scope, std::string name, const JsonNode & data) override;
  72. void loadObject(std::string scope, std::string name, const JsonNode & data, size_t index) override;
  73. void loadSubObject(const std::string & identifier, JsonNode config, MapObjectID ID, MapObjectSubID subID);
  74. void removeSubObject(MapObjectID ID, MapObjectSubID subID);
  75. void beforeValidate(JsonNode & object) override;
  76. void afterLoadFinalization() override;
  77. std::vector<bool> getDefaultAllowed() const override;
  78. /// Queries to detect loaded objects
  79. std::set<MapObjectID> knownObjects() const;
  80. std::set<MapObjectSubID> knownSubObjects(MapObjectID primaryID) const;
  81. /// returns handler for specified object (ID-based). ObjectHandler keeps ownership
  82. TObjectTypeHandler getHandlerFor(MapObjectID type, MapObjectSubID subtype) const;
  83. TObjectTypeHandler getHandlerFor(const std::string & scope, const std::string & type, const std::string & subtype) const;
  84. TObjectTypeHandler getHandlerFor(CompoundMapObjectID compoundIdentifier) const;
  85. std::string getObjectName(MapObjectID type, MapObjectSubID subtype) const;
  86. SObjectSounds getObjectSounds(MapObjectID type, MapObjectSubID subtype) const;
  87. /// Returns handler string describing the handler (for use in client)
  88. std::string getObjectHandlerName(MapObjectID type) const;
  89. std::string getJsonKey(MapObjectID type) const;
  90. };
  91. VCMI_LIB_NAMESPACE_END