1f9b284de2
Signed-off-by: Esteban Papp <81431996+amznestebanpapp@users.noreply.github.com> # Conflicts: # Code/Editor/Plugins/ComponentEntityEditorPlugin/ComponentEntityEditorPlugin_precompiled.h # Code/Editor/Plugins/EditorCommon/EditorCommon_precompiled.h # Code/Editor/Plugins/EditorCommon/stdafx.cpp # Code/Editor/Plugins/FFMPEGPlugin/FFMPEGPlugin_precompiled.h # Code/Editor/Plugins/PerforcePlugin/PerforcePlugin_precompiled.h # Code/Editor/Plugins/ProjectSettingsTool/ProjectSettingsTool_precompiled.h # Code/Framework/AzToolsFramework/AzToolsFramework/AzToolsFramework_precompiled.h # Code/Tools/AssetProcessor/native/precompiled.h # Code/Tools/Standalone/StandaloneTools_precompiled.h # Gems/AssetMemoryAnalyzer/Code/Source/AssetMemoryAnalyzer_precompiled.h # Gems/Atom/Asset/ImageProcessingAtom/Code/Source/ImageProcessing_precompiled.h # Gems/Atom/RHI/DX12/Code/Source/RHI/Atom_RHI_DX12_precompiled.h # Gems/Atom/RHI/Metal/Code/Include/Platform/Mac/Atom_RHI_Metal_precompiled_Platform.h # Gems/Atom/RHI/Metal/Code/Include/Platform/iOS/Atom_RHI_Metal_precompiled_Platform.h # Gems/Atom/RHI/Metal/Code/Source/Atom_RHI_Metal_precompiled.h # Gems/Atom/RHI/Metal/Code/atom_rhi_metal_common_files.cmake # Gems/Atom/RHI/Null/Code/Source/Atom_RHI_Null_precompiled.h # Gems/Atom/RHI/Null/Code/atom_rhi_null_common_files.cmake # Gems/Atom/RHI/Vulkan/Code/Include/Platform/Android/Atom_RHI_Vulkan_precompiled_Platform.h # Gems/Atom/RHI/Vulkan/Code/Include/Platform/Linux/Atom_RHI_Vulkan_precompiled_Platform.h # Gems/Atom/RHI/Vulkan/Code/Include/Platform/Mac/Atom_RHI_Vulkan_precompiled_Platform.h # Gems/Atom/RHI/Vulkan/Code/Include/Platform/Windows/Atom_RHI_Vulkan_precompiled_Platform.h # Gems/Atom/RHI/Vulkan/Code/Source/Atom_RHI_Vulkan_precompiled.h # Gems/Atom/RHI/Vulkan/Code/Source/RHI/SwapChain.cpp # Gems/Atom/RHI/Vulkan/Code/atom_rhi_vulkan_common_files.cmake # Gems/AtomLyIntegration/AtomFont/Code/Include/AtomLyIntegration/AtomFont/AtomFont_precompiled.h # Gems/Blast/Code/Source/StdAfx.cpp # Gems/Camera/Code/Source/Camera_precompiled.h # Gems/EMotionFX/Code/Source/EMotionFX_precompiled.h # Gems/FastNoise/Code/Source/FastNoise_precompiled.h # Gems/Gestures/Code/Source/Gestures_precompiled.h # Gems/GradientSignal/Code/Source/GradientSignal_precompiled.h # Gems/GraphCanvas/Code/precompiled.h # Gems/ImGui/Code/Source/ImGui_precompiled.h # Gems/InAppPurchases/Code/Source/InAppPurchases_precompiled.h # Gems/LmbrCentral/Code/Source/LmbrCentral_precompiled.h # Gems/LmbrCentral/Code/Tests/ShapeGeometryUtilTest.cpp # Gems/LyShine/Code/Editor/UiCanvasEditor_precompiled.h # Gems/LyShine/Code/Source/Animation/LyShine_precompiled.h # Gems/LyShine/Code/Source/LyShine_precompiled.h # Gems/LyShineExamples/Code/Source/LyShineExamples_precompiled.h # Gems/Maestro/Code/Source/Cinematics/Maestro_precompiled.h # Gems/Maestro/Code/Source/Maestro_precompiled.h # Gems/MessagePopup/Code/Source/MessagePopup_precompiled.h # Gems/Metastream/Code/Source/Metastream_precompiled.h # Gems/Microphone/Code/Source/Microphone_precompiled.h # Gems/Multiplayer/Code/Source/Multiplayer_precompiled.h # Gems/PhysX/Code/NumericalMethods/Source/NumericalMethods_precompiled.h # Gems/PhysX/Code/Source/PhysXUnsupported_precompiled.h # Gems/PhysX/Code/Source/PhysX_precompiled.h # Gems/PhysX/Code/physx_unsupported_files.cmake # Gems/PhysXDebug/Code/Source/PhysXDebugUnsupported_precompiled.h # Gems/PhysXDebug/Code/Source/PhysXDebug_precompiled.h # Gems/ScriptCanvas/Code/Editor/precompiled.h # Gems/ScriptCanvas/Code/Source/precompiled.h # Gems/ScriptCanvasDeveloper/Code/Source/precompiled.h # Gems/ScriptCanvasPhysics/Code/Source/ScriptCanvasPhysics_precompiled.h # Gems/ScriptEvents/Code/Source/precompiled.h # Gems/ScriptEvents/Code/Tests/Editor/EditorTests.cpp # Gems/ScriptedEntityTweener/Code/Source/ScriptedEntityTweener_precompiled.h # Gems/SliceFavorites/Code/Source/SliceFavorites_precompiled.h # Gems/StartingPointCamera/Code/Source/StartingPointCamera_precompiled.h # Gems/StartingPointInput/Code/Source/StartingPointInput_precompiled.h # Gems/StartingPointMovement/Code/Source/StartingPointMovement_precompiled.h # Gems/SurfaceData/Code/Source/SurfaceData_precompiled.h # Gems/TextureAtlas/Code/Source/TextureAtlas_precompiled.h # Gems/TickBusOrderViewer/Code/Source/TickBusOrderViewer_precompiled.h # Gems/Twitch/Code/Source/Twitch_precompiled.h # Gems/VirtualGamepad/Code/Source/VirtualGamepad_precompiled.h # Gems/WhiteBox/Code/Source/WhiteBoxUnsupported_precompiled.h # Gems/WhiteBox/Code/Source/WhiteBox_precompiled.h
219 lines
9.1 KiB
C++
219 lines
9.1 KiB
C++
/*
|
|
* Copyright (c) Contributors to the Open 3D Engine Project.
|
|
* For complete copyright and license terms please see the LICENSE at the root of this distribution.
|
|
*
|
|
* SPDX-License-Identifier: Apache-2.0 OR MIT
|
|
*
|
|
*/
|
|
#include "CreateSliceCommand.h"
|
|
|
|
#include <AzCore/Asset/AssetManager.h>
|
|
#include <AzCore/Serialization/Utils.h>
|
|
|
|
#include <AzToolsFramework/Slice/SliceUtilities.h>
|
|
#include <AzToolsFramework/API/EditorAssetSystemAPI.h>
|
|
#include <AzToolsFramework/Entity/SliceEditorEntityOwnershipServiceBus.h>
|
|
namespace AzToolsFramework
|
|
{
|
|
CreateSliceCommand::CreateSliceCommand(const AZStd::string& friendlyName)
|
|
: BaseSliceCommand(friendlyName)
|
|
{
|
|
}
|
|
|
|
void CreateSliceCommand::Capture(const AZ::Data::Asset<AZ::SliceAsset>& tempSliceAsset,
|
|
const AZStd::string& fullSliceAssetPath,
|
|
const AZ::SliceComponent::EntityIdToEntityIdMap& liveToAssetMap)
|
|
{
|
|
if (!tempSliceAsset.Get())
|
|
{
|
|
AZ_Error("CreateSliceCommand::Capture",
|
|
false,
|
|
"Invalid SliceAsset passed in. Unable to capture undo/redo state");
|
|
|
|
return;
|
|
}
|
|
|
|
if (liveToAssetMap.empty())
|
|
{
|
|
AZ_Warning("CreateSliceCommand::Capture",
|
|
false,
|
|
"Empty liveToAsset EntityId map passed in. Nothing to undo/redo");
|
|
|
|
return;
|
|
}
|
|
|
|
m_fullSliceAssetPath = fullSliceAssetPath;
|
|
m_liveToAssetMap = liveToAssetMap;
|
|
|
|
// 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
|
|
AZ::Data::AssetInfo assetInfo;
|
|
AZStd::string watchFolder;
|
|
bool foundSourceInfo = false;
|
|
|
|
AzToolsFramework::AssetSystemRequestBus::BroadcastResult(foundSourceInfo,
|
|
&AzToolsFramework::AssetSystemRequestBus::Events::GetSourceInfoBySourcePath,
|
|
m_fullSliceAssetPath.c_str(), assetInfo, watchFolder);
|
|
|
|
if (!foundSourceInfo)
|
|
{
|
|
AZ_Error("CreateSliceCommand::Capture",
|
|
false,
|
|
"Failed to acquire asset source info for asset located at local path: %s. Unable to capture undo/redo state",
|
|
m_fullSliceAssetPath.c_str());
|
|
|
|
m_liveToAssetMap.clear();
|
|
return;
|
|
}
|
|
|
|
// Convert retrieved assetId to have the appropriate subID for a static (non-dynamic) slice asset
|
|
m_sliceAssetId = AZ::Data::AssetId(assetInfo.m_assetId.m_guid, AZ::SliceAsset::GetAssetSubId());
|
|
|
|
// Save a copy of our tempSliceAsset
|
|
AZ::SliceAsset* tempSliceAssetData = tempSliceAsset.Get();
|
|
AZ::Entity* tempSliceAssetEntity = tempSliceAssetData->GetEntity();
|
|
AZ::IO::ByteContainerStream<AZStd::vector<AZ::u8>> assetStream(&m_sliceAssetBuffer);
|
|
bool saveSuccess = AZ::Utils::SaveObjectToStream(assetStream, AZ::DataStream::ST_BINARY, tempSliceAssetEntity);
|
|
|
|
if (!saveSuccess)
|
|
{
|
|
AZ_Error("CreateSliceCommand::Capture",
|
|
false,
|
|
"Failed to cache provided slice asset specified at local path: %s. Unable to capture undo/redo state",
|
|
m_fullSliceAssetPath.c_str());
|
|
|
|
m_liveToAssetMap.clear();
|
|
return;
|
|
}
|
|
|
|
// Capture restore info for all the live entities in our map
|
|
for (const AZStd::pair<AZ::EntityId, AZ::EntityId>& liveToAssetIdPair : m_liveToAssetMap)
|
|
{
|
|
const AZ::EntityId& liveEntityId = liveToAssetIdPair.first;
|
|
|
|
bool restoreCaptured = BaseSliceCommand::CaptureRestoreInfoForUndo(liveEntityId);
|
|
|
|
if(!restoreCaptured)
|
|
{
|
|
m_liveToAssetMap.clear();
|
|
m_entityUndoRestoreInfoArray.clear();
|
|
|
|
return;
|
|
}
|
|
}
|
|
|
|
// Remove the dirty flag on these entities
|
|
// Otherwise an additional entity restore will be scheduled on these entities
|
|
for (const AZStd::pair<AZ::EntityId, AZ::EntityId>& liveToAssetId : m_liveToAssetMap)
|
|
{
|
|
AzToolsFramework::ToolsApplicationRequestBus::Broadcast(&AzToolsFramework::ToolsApplicationRequestBus::Events::RemoveDirtyEntity, liveToAssetId.first);
|
|
}
|
|
}
|
|
|
|
void CreateSliceCommand::Undo()
|
|
{
|
|
BaseSliceCommand::RestoreEntities(m_entityUndoRestoreInfoArray);
|
|
}
|
|
|
|
void CreateSliceCommand::Redo()
|
|
{
|
|
// Load our slice asset in memory
|
|
AZ::Data::Asset<AZ::SliceAsset> sliceAsset = PreloadSliceAsset();
|
|
|
|
// Move all entities marked in m_liveToAssetMap into a new instance of our sliceAsset
|
|
AZ::SliceComponent::SliceInstanceAddress sliceInstanceResult;
|
|
AzToolsFramework::SliceEditorEntityOwnershipServiceRequestBus::BroadcastResult(sliceInstanceResult,
|
|
&AzToolsFramework::SliceEditorEntityOwnershipServiceRequestBus::Events::PromoteEditorEntitiesIntoSlice,
|
|
sliceAsset, m_liveToAssetMap);
|
|
}
|
|
|
|
AZ::Data::Asset<AZ::SliceAsset> CreateSliceCommand::PreloadSliceAsset()
|
|
{
|
|
AZ::SerializeContext* serializeContext = nullptr;
|
|
AZ::ComponentApplicationBus::BroadcastResult(serializeContext, &AZ::ComponentApplicationBus::Events::GetSerializeContext);
|
|
if (!serializeContext)
|
|
{
|
|
AZ_Error("CreateSliceCommand::PreloadSliceAsset",
|
|
false,
|
|
"Failed to retrieve serialize context. Unable to proceed with loading cached slice asset for redo");
|
|
|
|
return AZ::Data::Asset<AZ::SliceAsset>();
|
|
}
|
|
|
|
// Load our sliceAsset back in
|
|
AZ::IO::ByteContainerStream<AZStd::vector<AZ::u8>> sliceAssetStream(&m_sliceAssetBuffer);
|
|
|
|
// 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
|
|
AZStd::unique_ptr<AZ::Entity> sliceAssetEntity(AZ::Utils::LoadObjectFromStream<AZ::Entity>(sliceAssetStream, serializeContext));
|
|
|
|
if (!sliceAssetEntity)
|
|
{
|
|
AZ_Assert(false,
|
|
"CreateSliceCommand::PreloadSliceAsset could not load cached slice asset from stream. Unable to proceed with loading cached asset for redo");
|
|
|
|
return AZ::Data::Asset<AZ::SliceAsset>();
|
|
}
|
|
|
|
AZ::Data::Asset<AZ::SliceAsset> sliceAsset = AZ::Data::AssetManager::Instance().FindOrCreateAsset<AZ::SliceAsset>(m_sliceAssetId, AZ::Data::AssetLoadBehavior::Default);
|
|
|
|
// No Asset should exist under this AssetID. If one does we cannot continue
|
|
if (sliceAsset.GetStatus() != AZ::Data::AssetData::AssetStatus::NotLoaded)
|
|
{
|
|
AZ_Error("CreateSliceCommand::PreloadSliceAsset",
|
|
false,
|
|
"Asset already registered with AssetID: %s. Unable to proceed with loading cached asset for redo",
|
|
m_sliceAssetId.ToString<AZStd::string>().c_str());
|
|
|
|
return AZ::Data::Asset<AZ::SliceAsset>();
|
|
}
|
|
|
|
AZ::SliceAsset* sliceAssetData = sliceAsset.Get();
|
|
|
|
// Confirm GetAsset was successful
|
|
if (!sliceAssetData)
|
|
{
|
|
AZ_Error("CreateSliceCommand::PreloadSliceAsset",
|
|
false,
|
|
"Failed to get slice asset from Asset ID: %s via the AssetManager. Unable to generate initial slice instance during Create Slice",
|
|
m_sliceAssetId.ToString<AZStd::string>().c_str());
|
|
|
|
return AZ::Data::Asset<AZ::SliceAsset>();
|
|
}
|
|
|
|
// Set the new asset's data to be the data we cached in m_sliceAssetBuffer
|
|
sliceAssetData->SetData(sliceAssetEntity.get(), sliceAssetEntity->FindComponent<AZ::SliceComponent>());
|
|
|
|
// Validate that the entity and component of sliceAssetData were successfully set
|
|
if (sliceAssetData->GetStatus() != AZ::Data::AssetData::AssetStatus::Ready)
|
|
{
|
|
AZ_Error("CreateSliceCommand::PreloadSliceAsset",
|
|
false,
|
|
"Failed to load valid slice data during initial creation of slice asset with AssetID: %s. Unable to generate initial slice instance during Create Slice",
|
|
m_sliceAssetId.ToString<AZStd::string>().c_str());
|
|
|
|
return AZ::Data::Asset<AZ::SliceAsset>();
|
|
}
|
|
|
|
// Safe to release our Entity to the asset
|
|
sliceAssetEntity.release();
|
|
|
|
// Finalize configuring the slice asset
|
|
sliceAssetData->GetComponent()->SetMyAsset(sliceAssetData);
|
|
sliceAssetData->GetComponent()->ListenForAssetChanges();
|
|
|
|
// Update the asset's hint to be its relative asset path
|
|
bool relativePathFound = false;
|
|
AZStd::string assetPathRelative;
|
|
AssetSystemRequestBus::BroadcastResult(relativePathFound, &AssetSystemRequestBus::Events::GetRelativeProductPathFromFullSourceOrProductPath, m_fullSliceAssetPath, assetPathRelative);
|
|
AZStd::to_lower(assetPathRelative.begin(), assetPathRelative.end());
|
|
|
|
sliceAsset.SetHint(assetPathRelative);
|
|
|
|
// We've finalized loading this asset from memory
|
|
// Ignore the following reload from AssetCatalog since it will attempt to overwrite our asset with what's on disk
|
|
// Which is an unnecessary operation as SliceTransaction serialized the contents of transactionAsset to disk
|
|
sliceAssetData->SetIgnoreNextAutoReload(true);
|
|
|
|
return sliceAsset;
|
|
}
|
|
}
|