PrefabConversionUtils.h 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. /*
  2. * Copyright (c) Contributors to the Open 3D Engine Project.
  3. * For complete copyright and license terms please see the LICENSE at the root of this distribution.
  4. *
  5. * SPDX-License-Identifier: Apache-2.0 OR MIT
  6. *
  7. */
  8. #pragma once
  9. #include <AzCore/RTTI/RTTI.h>
  10. #include <AzCore/std/containers/unordered_map.h>
  11. #include <AzCore/Component/EntityIdSerializer.h>
  12. #include <AzToolsFramework/Prefab/PrefabDomUtils.h>
  13. #include <AzToolsFramework/Prefab/PrefabLoaderInterface.h>
  14. #include <AzToolsFramework/Prefab/PrefabSystemComponentInterface.h>
  15. namespace PhysX::Utils
  16. {
  17. struct PrefabInfo
  18. {
  19. AzToolsFramework::Prefab::TemplateId m_templateId;
  20. AzToolsFramework::Prefab::Template* m_template = nullptr;
  21. AZStd::string m_prefabFullPath;
  22. };
  23. AZStd::vector<PrefabInfo> CollectPrefabs();
  24. void SavePrefab(PrefabInfo& prefabInfo);
  25. AZStd::vector<AzToolsFramework::Prefab::PrefabDomValue*> GetPrefabEntities(AzToolsFramework::Prefab::PrefabDom& prefab);
  26. AZStd::vector<AzToolsFramework::Prefab::PrefabDomValue*> GetEntityComponents(AzToolsFramework::Prefab::PrefabDomValue& entity);
  27. AZ::TypeId GetComponentTypeId(const AzToolsFramework::Prefab::PrefabDomValue& component);
  28. // Mapper to ensure the entity ids remain the same when loading and storing entities from a prefab.
  29. class PrefabEntityIdMapper final
  30. : public AZ::JsonEntityIdSerializer::JsonEntityIdMapper
  31. {
  32. public:
  33. AZ_RTTI(PrefabEntityIdMapper, "{CAA0D7E0-00B0-4B84-8480-A3475CE25043}", AZ::JsonEntityIdSerializer::JsonEntityIdMapper);
  34. PrefabEntityIdMapper() = default;
  35. AZ::JsonSerializationResult::Result MapJsonToId(
  36. AZ::EntityId& outputValue, const rapidjson::Value& inputValue, AZ::JsonDeserializerContext& context) override;
  37. AZ::JsonSerializationResult::Result MapIdToJson(
  38. rapidjson::Value& outputValue, [[maybe_unused]] const AZ::EntityId& inputValue, AZ::JsonSerializerContext& context) override;
  39. private:
  40. AZStd::unordered_map<AZ::EntityId, AZStd::string> m_entityIdMap;
  41. };
  42. bool LoadPrefabEntity(
  43. PrefabEntityIdMapper& prefabEntityIdMapper, const AzToolsFramework::Prefab::PrefabDomValue& prefabEntity, AZ::Entity& entity);
  44. bool StorePrefabEntity(
  45. const PrefabEntityIdMapper& prefabEntityIdMapper,
  46. AzToolsFramework::Prefab::PrefabDom& prefabDom,
  47. AzToolsFramework::Prefab::PrefabDomValue& prefabEntity,
  48. const AZ::Entity& entity);
  49. } // namespace PhysX::Utils