CreateSliceCommand.cpp 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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. #include "CreateSliceCommand.h"
  9. #include <AzCore/Asset/AssetManager.h>
  10. #include <AzCore/Serialization/Utils.h>
  11. #include <AzToolsFramework/Slice/SliceUtilities.h>
  12. #include <AzToolsFramework/API/EditorAssetSystemAPI.h>
  13. #include <AzToolsFramework/Entity/SliceEditorEntityOwnershipServiceBus.h>
  14. namespace AzToolsFramework
  15. {
  16. CreateSliceCommand::CreateSliceCommand(const AZStd::string& friendlyName)
  17. : BaseSliceCommand(friendlyName)
  18. {
  19. }
  20. void CreateSliceCommand::Capture(const AZ::Data::Asset<AZ::SliceAsset>& tempSliceAsset,
  21. const AZStd::string& fullSliceAssetPath,
  22. const AZ::SliceComponent::EntityIdToEntityIdMap& liveToAssetMap)
  23. {
  24. if (!tempSliceAsset.Get())
  25. {
  26. AZ_Error("CreateSliceCommand::Capture",
  27. false,
  28. "Invalid SliceAsset passed in. Unable to capture undo/redo state");
  29. return;
  30. }
  31. if (liveToAssetMap.empty())
  32. {
  33. AZ_Warning("CreateSliceCommand::Capture",
  34. false,
  35. "Empty liveToAsset EntityId map passed in. Nothing to undo/redo");
  36. return;
  37. }
  38. m_fullSliceAssetPath = fullSliceAssetPath;
  39. m_liveToAssetMap = liveToAssetMap;
  40. // Get the sourceUUID of our newly created slice so that we can build its asset ID without having to wait for it to be processed
  41. AZ::Data::AssetInfo assetInfo;
  42. AZStd::string watchFolder;
  43. bool foundSourceInfo = false;
  44. AzToolsFramework::AssetSystemRequestBus::BroadcastResult(foundSourceInfo,
  45. &AzToolsFramework::AssetSystemRequestBus::Events::GetSourceInfoBySourcePath,
  46. m_fullSliceAssetPath.c_str(), assetInfo, watchFolder);
  47. if (!foundSourceInfo)
  48. {
  49. AZ_Error("CreateSliceCommand::Capture",
  50. false,
  51. "Failed to acquire asset source info for asset located at local path: %s. Unable to capture undo/redo state",
  52. m_fullSliceAssetPath.c_str());
  53. m_liveToAssetMap.clear();
  54. return;
  55. }
  56. // Convert retrieved assetId to have the appropriate subID for a static (non-dynamic) slice asset
  57. m_sliceAssetId = AZ::Data::AssetId(assetInfo.m_assetId.m_guid, AZ::SliceAsset::GetAssetSubId());
  58. // Save a copy of our tempSliceAsset
  59. AZ::SliceAsset* tempSliceAssetData = tempSliceAsset.Get();
  60. AZ::Entity* tempSliceAssetEntity = tempSliceAssetData->GetEntity();
  61. AZ::IO::ByteContainerStream<AZStd::vector<AZ::u8>> assetStream(&m_sliceAssetBuffer);
  62. bool saveSuccess = AZ::Utils::SaveObjectToStream(assetStream, AZ::DataStream::ST_BINARY, tempSliceAssetEntity);
  63. if (!saveSuccess)
  64. {
  65. AZ_Error("CreateSliceCommand::Capture",
  66. false,
  67. "Failed to cache provided slice asset specified at local path: %s. Unable to capture undo/redo state",
  68. m_fullSliceAssetPath.c_str());
  69. m_liveToAssetMap.clear();
  70. return;
  71. }
  72. // Capture restore info for all the live entities in our map
  73. for (const AZStd::pair<AZ::EntityId, AZ::EntityId>& liveToAssetIdPair : m_liveToAssetMap)
  74. {
  75. const AZ::EntityId& liveEntityId = liveToAssetIdPair.first;
  76. bool restoreCaptured = BaseSliceCommand::CaptureRestoreInfoForUndo(liveEntityId);
  77. if(!restoreCaptured)
  78. {
  79. m_liveToAssetMap.clear();
  80. m_entityUndoRestoreInfoArray.clear();
  81. return;
  82. }
  83. }
  84. // Remove the dirty flag on these entities
  85. // Otherwise an additional entity restore will be scheduled on these entities
  86. for (const AZStd::pair<AZ::EntityId, AZ::EntityId>& liveToAssetId : m_liveToAssetMap)
  87. {
  88. AzToolsFramework::ToolsApplicationRequestBus::Broadcast(&AzToolsFramework::ToolsApplicationRequestBus::Events::RemoveDirtyEntity, liveToAssetId.first);
  89. }
  90. }
  91. void CreateSliceCommand::Undo()
  92. {
  93. BaseSliceCommand::RestoreEntities(m_entityUndoRestoreInfoArray);
  94. }
  95. void CreateSliceCommand::Redo()
  96. {
  97. // Load our slice asset in memory
  98. AZ::Data::Asset<AZ::SliceAsset> sliceAsset = PreloadSliceAsset();
  99. // Move all entities marked in m_liveToAssetMap into a new instance of our sliceAsset
  100. AZ::SliceComponent::SliceInstanceAddress sliceInstanceResult;
  101. AzToolsFramework::SliceEditorEntityOwnershipServiceRequestBus::BroadcastResult(sliceInstanceResult,
  102. &AzToolsFramework::SliceEditorEntityOwnershipServiceRequestBus::Events::PromoteEditorEntitiesIntoSlice,
  103. sliceAsset, m_liveToAssetMap);
  104. }
  105. AZ::Data::Asset<AZ::SliceAsset> CreateSliceCommand::PreloadSliceAsset()
  106. {
  107. AZ::SerializeContext* serializeContext = nullptr;
  108. AZ::ComponentApplicationBus::BroadcastResult(serializeContext, &AZ::ComponentApplicationBus::Events::GetSerializeContext);
  109. if (!serializeContext)
  110. {
  111. AZ_Error("CreateSliceCommand::PreloadSliceAsset",
  112. false,
  113. "Failed to retrieve serialize context. Unable to proceed with loading cached slice asset for redo");
  114. return AZ::Data::Asset<AZ::SliceAsset>();
  115. }
  116. // Load our sliceAsset back in
  117. AZ::IO::ByteContainerStream<AZStd::vector<AZ::u8>> sliceAssetStream(&m_sliceAssetBuffer);
  118. // Load our cached Asset Entity from stream and store it in a unique_ptr. If we early out due to an error it will clean up the loose entity
  119. AZStd::unique_ptr<AZ::Entity> sliceAssetEntity(AZ::Utils::LoadObjectFromStream<AZ::Entity>(sliceAssetStream, serializeContext));
  120. if (!sliceAssetEntity)
  121. {
  122. AZ_Assert(false,
  123. "CreateSliceCommand::PreloadSliceAsset could not load cached slice asset from stream. Unable to proceed with loading cached asset for redo");
  124. return AZ::Data::Asset<AZ::SliceAsset>();
  125. }
  126. AZ::Data::Asset<AZ::SliceAsset> sliceAsset = AZ::Data::AssetManager::Instance().FindOrCreateAsset<AZ::SliceAsset>(m_sliceAssetId, AZ::Data::AssetLoadBehavior::Default);
  127. // No Asset should exist under this AssetID. If one does we cannot continue
  128. if (sliceAsset.GetStatus() != AZ::Data::AssetData::AssetStatus::NotLoaded)
  129. {
  130. AZ_Error("CreateSliceCommand::PreloadSliceAsset",
  131. false,
  132. "Asset already registered with AssetID: %s. Unable to proceed with loading cached asset for redo",
  133. m_sliceAssetId.ToString<AZStd::string>().c_str());
  134. return AZ::Data::Asset<AZ::SliceAsset>();
  135. }
  136. AZ::SliceAsset* sliceAssetData = sliceAsset.Get();
  137. // Confirm GetAsset was successful
  138. if (!sliceAssetData)
  139. {
  140. AZ_Error("CreateSliceCommand::PreloadSliceAsset",
  141. false,
  142. "Failed to get slice asset from Asset ID: %s via the AssetManager. Unable to generate initial slice instance during Create Slice",
  143. m_sliceAssetId.ToString<AZStd::string>().c_str());
  144. return AZ::Data::Asset<AZ::SliceAsset>();
  145. }
  146. // Set the new asset's data to be the data we cached in m_sliceAssetBuffer
  147. sliceAssetData->SetData(sliceAssetEntity.get(), sliceAssetEntity->FindComponent<AZ::SliceComponent>());
  148. // Validate that the entity and component of sliceAssetData were successfully set
  149. if (sliceAssetData->GetStatus() != AZ::Data::AssetData::AssetStatus::Ready)
  150. {
  151. AZ_Error("CreateSliceCommand::PreloadSliceAsset",
  152. false,
  153. "Failed to load valid slice data during initial creation of slice asset with AssetID: %s. Unable to generate initial slice instance during Create Slice",
  154. m_sliceAssetId.ToString<AZStd::string>().c_str());
  155. return AZ::Data::Asset<AZ::SliceAsset>();
  156. }
  157. // Safe to release our Entity to the asset
  158. sliceAssetEntity.release();
  159. // Finalize configuring the slice asset
  160. sliceAssetData->GetComponent()->SetMyAsset(sliceAssetData);
  161. sliceAssetData->GetComponent()->ListenForAssetChanges();
  162. // Update the asset's hint to be its relative asset path
  163. bool relativePathFound = false;
  164. AZStd::string assetPathRelative;
  165. AssetSystemRequestBus::BroadcastResult(relativePathFound, &AssetSystemRequestBus::Events::GetRelativeProductPathFromFullSourceOrProductPath, m_fullSliceAssetPath, assetPathRelative);
  166. AZStd::to_lower(assetPathRelative.begin(), assetPathRelative.end());
  167. sliceAsset.SetHint(assetPathRelative);
  168. // We've finalized loading this asset from memory
  169. // Ignore the following reload from AssetCatalog since it will attempt to overwrite our asset with what's on disk
  170. // Which is an unnecessary operation as SliceTransaction serialized the contents of transactionAsset to disk
  171. sliceAssetData->SetIgnoreNextAutoReload(true);
  172. return sliceAsset;
  173. }
  174. }