CObjectClassesHandler.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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 "../mapObjects/CompoundMapObjectID.h"
  12. #include "../IHandlerBase.h"
  13. #include "../json/JsonNode.h"
  14. VCMI_LIB_NAMESPACE_BEGIN
  15. class AObjectTypeHandler;
  16. class ObjectTemplate;
  17. struct SObjectSounds;
  18. class CGObjectInstance;
  19. using TObjectTypeHandler = std::shared_ptr<AObjectTypeHandler>;
  20. /// Class responsible for creation of adventure map objects of specific type
  21. class DLL_LINKAGE ObjectClass : boost::noncopyable
  22. {
  23. public:
  24. std::string modScope;
  25. std::string identifier;
  26. si32 id;
  27. std::string handlerName; // ID of handler that controls this object, should be determined using handlerConstructor map
  28. JsonNode base;
  29. std::vector<TObjectTypeHandler> objectTypeHandlers;
  30. ObjectClass();
  31. ~ObjectClass();
  32. std::string getJsonKey() const;
  33. std::string getNameTextID() const;
  34. std::string getNameTranslated() const;
  35. };
  36. /// Main class responsible for creation of all adventure map objects
  37. class DLL_LINKAGE CObjectClassesHandler : public IHandlerBase
  38. {
  39. /// list of object handlers, each of them handles only one type
  40. std::vector< std::unique_ptr<ObjectClass> > mapObjectTypes;
  41. /// map that is filled during construction with all known handlers. Not serializeable due to usage of std::function
  42. std::map<std::string, std::function<TObjectTypeHandler()> > handlerConstructors;
  43. std::vector<std::pair<CompoundMapObjectID, std::function<void(CompoundMapObjectID)>>> objectIdHandlers;
  44. /// container with H3 templates, used only during loading, no need to serialize it
  45. using TTemplatesContainer = std::multimap<std::pair<MapObjectID, MapObjectSubID>, std::shared_ptr<const ObjectTemplate>>;
  46. TTemplatesContainer legacyTemplates;
  47. TObjectTypeHandler loadSubObjectFromJson(const std::string & scope, const std::string & identifier, const JsonNode & entry, ObjectClass * obj, size_t index);
  48. void loadSubObject(const std::string & scope, const std::string & identifier, const JsonNode & entry, ObjectClass * obj);
  49. void loadSubObject(const std::string & scope, const std::string & identifier, const JsonNode & entry, ObjectClass * obj, size_t index);
  50. std::unique_ptr<ObjectClass> loadFromJson(const std::string & scope, const JsonNode & json, const std::string & name, size_t index);
  51. void generateExtraMonolithsForRMG(ObjectClass * container);
  52. public:
  53. CObjectClassesHandler();
  54. ~CObjectClassesHandler();
  55. std::vector<JsonNode> loadLegacyData() override;
  56. void loadObject(std::string scope, std::string name, const JsonNode & data) override;
  57. void loadObject(std::string scope, std::string name, const JsonNode & data, size_t index) override;
  58. void loadSubObject(const std::string & identifier, JsonNode config, MapObjectID ID, MapObjectSubID subID);
  59. void removeSubObject(MapObjectID ID, MapObjectSubID subID);
  60. void beforeValidate(JsonNode & object) override;
  61. void afterLoadFinalization() override;
  62. /// Queries to detect loaded objects
  63. std::set<MapObjectID> knownObjects() const;
  64. std::set<MapObjectSubID> knownSubObjects(MapObjectID primaryID) const;
  65. /// returns handler for specified object (ID-based). ObjectHandler keeps ownership
  66. TObjectTypeHandler getHandlerFor(MapObjectID type, MapObjectSubID subtype) const;
  67. TObjectTypeHandler getHandlerFor(const std::string & scope, const std::string & type, const std::string & subtype) const;
  68. TObjectTypeHandler getHandlerFor(CompoundMapObjectID compoundIdentifier) const;
  69. CompoundMapObjectID getCompoundIdentifier(const std::string & scope, const std::string & type, const std::string & subtype) const;
  70. CompoundMapObjectID getCompoundIdentifier(const std::string & objectName) const;
  71. std::string getObjectName(MapObjectID type, MapObjectSubID subtype) const;
  72. SObjectSounds getObjectSounds(MapObjectID type, MapObjectSubID subtype) const;
  73. void resolveObjectCompoundId(const std::string & id, std::function<void(CompoundMapObjectID)> callback);
  74. /// Returns handler string describing the handler (for use in client)
  75. std::string getObjectHandlerName(MapObjectID type) const;
  76. std::string getJsonKey(MapObjectID type) const;
  77. };
  78. VCMI_LIB_NAMESPACE_END