SliceConverter.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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 NeedAssetProcessor() const;
  57. bool ConvertSliceFile(AZ::SerializeContext* serializeContext, const AZStd::string& slicePath, bool isDryRun);
  58. bool ConvertSliceToPrefab(
  59. AZ::SerializeContext* serializeContext, AZ::IO::PathView outputPath, bool isDryRun, AZ::Entity* rootEntity);
  60. void FixPrefabEntities(AZ::Entity& containerEntity, SliceComponent::EntityList& sliceEntities);
  61. bool ConvertNestedSlices(
  62. SliceComponent* sliceComponent, AzToolsFramework::Prefab::Instance* sourceInstance,
  63. AZ::SerializeContext* serializeContext, bool isDryRun);
  64. bool ConvertSliceInstance(
  65. AZ::SliceComponent::SliceInstance& instance, AZ::Data::Asset<AZ::SliceAsset>& sliceAsset,
  66. AzToolsFramework::Prefab::TemplateReference nestedTemplate,
  67. AzToolsFramework::Prefab::Instance* topLevelInstance,
  68. AZ::SerializeContext* serializeContext);
  69. void UpdateCachedTransform(const AZ::Entity& entity);
  70. void SetParentEntity(const AZ::Entity& entity, const AZ::EntityId& parentId, bool onlySetIfInvalid);
  71. void PrintPrefab(AzToolsFramework::Prefab::TemplateId templateId);
  72. bool SavePrefab(AZ::IO::PathView outputPath, AzToolsFramework::Prefab::TemplateId templateId);
  73. void UpdateSliceEntityInstanceMappings(
  74. const AZ::SliceComponent::EntityIdToEntityIdMap& sliceEntityIdMap,
  75. const AZStd::string& currentInstanceAlias);
  76. AZStd::string GetInstanceAlias(const AZ::SliceComponent::SliceInstance& instance);
  77. void RemapIdReferences(
  78. const AZStd::unordered_map<AZ::EntityId, SliceEntityMappingInfo>& idMapper,
  79. AzToolsFramework::Prefab::Instance* topLevelInstance,
  80. AzToolsFramework::Prefab::Instance* nestedInstance,
  81. SliceComponent::InstantiatedContainer* instantiatedEntities,
  82. SerializeContext* context);
  83. AZ::IO::Path m_projectPath;
  84. // Track all of the entity IDs created and associate them with enough conversion information to know how to place the
  85. // entities in the correct place in the prefab hierarchy and fix up parent entity ID mappings to work with the nested
  86. // prefab schema.
  87. AZStd::unordered_map<AZ::EntityId, SliceEntityMappingInfo> m_aliasIdMapper;
  88. // When we don't use the asset processor, will store all of the discovered slices path
  89. // with their absolute path and the relative posix path used by asset hint
  90. AZStd::unordered_map<AZStd::string, AZStd::string> m_relativeToAbsoluteSlicePaths;
  91. // Track all of the created prefab template IDs on a slice conversion so that they can get removed at the end of the
  92. // conversion for that file.
  93. AZStd::unordered_set<AzToolsFramework::Prefab::TemplateId> m_createdTemplateIds;
  94. };
  95. } // namespace SerializeContextTools
  96. } // namespace AZ