SliceConverter.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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/Component/ComponentApplicationBus.h>
  10. #include <AzCore/IO/Path/Path_fwd.h>
  11. #include <AzCore/JSON/document.h>
  12. #include <AzCore/JSON/stringbuffer.h>
  13. #include <AzCore/Serialization/Json/JsonSerialization.h>
  14. #include <AzCore/std/utils.h>
  15. #include <AzCore/std/containers/vector.h>
  16. #include <AzCore/std/string/string.h>
  17. #include <AzToolsFramework/Prefab/PrefabSystemComponent.h>
  18. #include <Converter.h>
  19. namespace AZ
  20. {
  21. class CommandLine;
  22. class Entity;
  23. class ModuleEntity;
  24. class SerializeContext;
  25. struct Uuid;
  26. namespace SerializeContextTools
  27. {
  28. class Application;
  29. class SliceConverter : public Converter
  30. {
  31. public:
  32. bool ConvertSliceFiles(Application& application);
  33. private:
  34. // When converting slice entities, especially for nested slices, we need to keep track of the original
  35. // entity ID, the entity alias it uses in the prefab, and which template and nested instance path it maps to.
  36. // As we encounter each instanced entity ID, we can look it up in this structure and use this to determine how to properly
  37. // add it to the correct place in the hierarchy.
  38. struct SliceEntityMappingInfo
  39. {
  40. SliceEntityMappingInfo(
  41. AzToolsFramework::Prefab::TemplateId templateId,
  42. AzToolsFramework::Prefab::EntityAlias entityAlias,
  43. bool isMetadataEntity = false)
  44. : m_templateId(templateId)
  45. , m_entityAlias(entityAlias)
  46. , m_isMetadataEntity(isMetadataEntity)
  47. {
  48. }
  49. AzToolsFramework::Prefab::TemplateId m_templateId;
  50. AzToolsFramework::Prefab::EntityAlias m_entityAlias;
  51. AZStd::vector<AzToolsFramework::Prefab::InstanceAlias> m_nestedInstanceAliases;
  52. bool m_isMetadataEntity{ false };
  53. };
  54. bool ConnectToAssetProcessor();
  55. void DisconnectFromAssetProcessor();
  56. bool ConvertSliceFile(AZ::SerializeContext* serializeContext, const AZStd::string& slicePath, bool isDryRun);
  57. bool ConvertSliceToPrefab(
  58. AZ::SerializeContext* serializeContext, AZ::IO::PathView outputPath, bool isDryRun, AZ::Entity* rootEntity);
  59. void FixPrefabEntities(AZ::Entity& containerEntity, SliceComponent::EntityList& sliceEntities);
  60. bool ConvertNestedSlices(
  61. SliceComponent* sliceComponent, AzToolsFramework::Prefab::Instance* sourceInstance,
  62. AZ::SerializeContext* serializeContext, bool isDryRun);
  63. bool ConvertSliceInstance(
  64. AZ::SliceComponent::SliceInstance& instance, AZ::Data::Asset<AZ::SliceAsset>& sliceAsset,
  65. AzToolsFramework::Prefab::TemplateReference nestedTemplate, AzToolsFramework::Prefab::Instance* topLevelInstance);
  66. void UpdateCachedTransform(const AZ::Entity& entity);
  67. void SetParentEntity(const AZ::Entity& entity, const AZ::EntityId& parentId, bool onlySetIfInvalid);
  68. void PrintPrefab(AzToolsFramework::Prefab::TemplateId templateId);
  69. bool SavePrefab(AZ::IO::PathView outputPath, AzToolsFramework::Prefab::TemplateId templateId);
  70. void UpdateSliceEntityInstanceMappings(
  71. const AZ::SliceComponent::EntityIdToEntityIdMap& sliceEntityIdMap,
  72. const AZStd::string& currentInstanceAlias);
  73. AZStd::string GetInstanceAlias(const AZ::SliceComponent::SliceInstance& instance);
  74. void RemapIdReferences(
  75. const AZStd::unordered_map<AZ::EntityId, SliceEntityMappingInfo>& idMapper,
  76. AzToolsFramework::Prefab::Instance* topLevelInstance,
  77. AzToolsFramework::Prefab::Instance* nestedInstance,
  78. SliceComponent::InstantiatedContainer* instantiatedEntities,
  79. SerializeContext* context);
  80. // Track all of the entity IDs created and associate them with enough conversion information to know how to place the
  81. // entities in the correct place in the prefab hierarchy and fix up parent entity ID mappings to work with the nested
  82. // prefab schema.
  83. AZStd::unordered_map<AZ::EntityId, SliceEntityMappingInfo> m_aliasIdMapper;
  84. // Track all of the created prefab template IDs on a slice conversion so that they can get removed at the end of the
  85. // conversion for that file.
  86. AZStd::unordered_set<AzToolsFramework::Prefab::TemplateId> m_createdTemplateIds;
  87. };
  88. } // namespace SerializeContextTools
  89. } // namespace AZ