SceneManifest.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. #pragma once
  2. /*
  3. * Copyright (c) Contributors to the Open 3D Engine Project.
  4. * For complete copyright and license terms please see the LICENSE at the root of this distribution.
  5. *
  6. * SPDX-License-Identifier: Apache-2.0 OR MIT
  7. *
  8. */
  9. #include <stdint.h>
  10. #include <AzCore/JSON/rapidjson.h>
  11. #include <AzCore/JSON/document.h>
  12. #include <AzCore/Memory/Memory.h>
  13. #include <AzCore/Outcome/Outcome.h>
  14. #include <AzCore/std/containers/vector.h>
  15. #include <AzCore/std/containers/unordered_map.h>
  16. #include <AzCore/std/smart_ptr/shared_ptr.h>
  17. #include <AzCore/std/utils.h>
  18. #include <AzCore/Serialization/SerializeContext.h>
  19. #include <SceneAPI/SceneCore/SceneCoreConfiguration.h>
  20. #include <SceneAPI/SceneCore/Containers/Views/View.h>
  21. #include <SceneAPI/SceneCore/Containers/Views/ConvertIterator.h>
  22. #include <SceneAPI/SceneCore/DataTypes/IManifestObject.h>
  23. namespace AZ
  24. {
  25. class JsonRegistrationContext;
  26. namespace SceneAPI
  27. {
  28. namespace Containers
  29. {
  30. // Scene manifests hold arbitrary meta data about a scene in a dictionary-like fashion.
  31. // This can include data such as export groups.
  32. class SCENE_CORE_API SceneManifest
  33. {
  34. friend class SceneManifestContainer;
  35. public:
  36. AZ_CLASS_ALLOCATOR_DECL
  37. AZ_RTTI(SceneManifest, "{9274AD17-3212-4651-9F3B-7DCCB080E467}");
  38. static constexpr size_t MaxSceneManifestFileSizeInBytes = AZStd::numeric_limits<size_t>::max();
  39. virtual ~SceneManifest();
  40. static AZStd::shared_ptr<const DataTypes::IManifestObject> SceneManifestConstDataConverter(
  41. const AZStd::shared_ptr<DataTypes::IManifestObject>& value);
  42. using Index = size_t;
  43. static const Index s_invalidIndex = static_cast<Index>(-1);
  44. using StorageHash = const DataTypes::IManifestObject *;
  45. using StorageLookup = AZStd::unordered_map<StorageHash, Index>;
  46. using ValueStorageType = AZStd::shared_ptr<DataTypes::IManifestObject>;
  47. using ValueStorage = AZStd::vector<ValueStorageType>;
  48. using ValueStorageData = Views::View<ValueStorage::const_iterator>;
  49. using ValueStorageConstDataIteratorWrapper = Views::ConvertIterator<ValueStorage::const_iterator,
  50. decltype(SceneManifestConstDataConverter(AZStd::shared_ptr<DataTypes::IManifestObject>()))>;
  51. using ValueStorageConstData = Views::View<ValueStorageConstDataIteratorWrapper>;
  52. void Clear();
  53. inline bool IsEmpty() const;
  54. inline bool AddEntry(const AZStd::shared_ptr<DataTypes::IManifestObject>& value);
  55. bool AddEntry(AZStd::shared_ptr<DataTypes::IManifestObject>&& value);
  56. inline bool RemoveEntry(const AZStd::shared_ptr<DataTypes::IManifestObject>& value);
  57. bool RemoveEntry(const DataTypes::IManifestObject* const value);
  58. inline size_t GetEntryCount() const;
  59. inline AZStd::shared_ptr<DataTypes::IManifestObject> GetValue(Index index);
  60. inline AZStd::shared_ptr<const DataTypes::IManifestObject> GetValue(Index index) const;
  61. // Finds the index of the given manifest object. A nullptr or invalid object will return s_invalidIndex.
  62. inline Index FindIndex(const AZStd::shared_ptr<DataTypes::IManifestObject>& value) const;
  63. // Finds the index of the given manifest object. A nullptr or invalid object will return s_invalidIndex.
  64. Index FindIndex(const DataTypes::IManifestObject* const value) const;
  65. inline ValueStorageData GetValueStorage();
  66. inline ValueStorageConstData GetValueStorage() const;
  67. bool LoadFromFile(const AZStd::string& absoluteFilePath, SerializeContext* context = nullptr);
  68. /**
  69. * Save manifest to file. Overwrites the file in case it already exists and creates a new file if not.
  70. * @param absoluteFilePath the absolute path of the file you want to save to.
  71. * @param context If no serialize context was specified, it will get the serialize context from the application component bus.
  72. * @result True in case saving went all fine, false if an error occurred.
  73. */
  74. bool SaveToFile(const AZStd::string& absoluteFilePath, SerializeContext* context = nullptr);
  75. AZ::Outcome<void, AZStd::string> LoadFromString(
  76. const AZStd::string& fileContents, SerializeContext* context = nullptr,
  77. JsonRegistrationContext* registrationContext = nullptr, bool loadXml = false);
  78. static void Reflect(ReflectContext* context);
  79. static bool VersionConverter(SerializeContext& context, SerializeContext::DataElementNode& node);
  80. //! Save manifest to string buffer.
  81. //! @param context If no serialize context was specified, it will get the context from the application component bus.
  82. //! @param registrationContext If no Json registration context was specified, it will get the context from the application component bus.
  83. //! @result True in case saving went all fine, false if an error occurred.
  84. AZ::Outcome<rapidjson::Document, AZStd::string> SaveToJsonDocument(SerializeContext* context = nullptr, JsonRegistrationContext* registrationContext = nullptr);
  85. private:
  86. void Init();
  87. AZ_PUSH_DISABLE_WARNING(4251, "-Wunknown-warning-option")
  88. StorageLookup m_storageLookup;
  89. ValueStorage m_values;
  90. AZ_POP_DISABLE_WARNING
  91. };
  92. } // Containers
  93. } // SceneAPI
  94. } // AZ
  95. #include <SceneAPI/SceneCore/Containers/SceneManifest.inl>