From 4d90b7cfb60a206149c31bd6f875debb9fadfd20 Mon Sep 17 00:00:00 2001 From: srikappa Date: Fri, 18 Jun 2021 11:19:59 -0700 Subject: [PATCH] Fix bug with detachPrefab incorrectly replacing old entity aliases in patches --- .../Prefab/Instance/Instance.cpp | 10 +++++++ .../Prefab/Instance/Instance.h | 1 + .../Prefab/PrefabPublicHandler.cpp | 30 +++++++++---------- 3 files changed, 25 insertions(+), 16 deletions(-) diff --git a/Code/Framework/AzToolsFramework/AzToolsFramework/Prefab/Instance/Instance.cpp b/Code/Framework/AzToolsFramework/AzToolsFramework/Prefab/Instance/Instance.cpp index e5179f4229..123b31ff61 100644 --- a/Code/Framework/AzToolsFramework/AzToolsFramework/Prefab/Instance/Instance.cpp +++ b/Code/Framework/AzToolsFramework/AzToolsFramework/Prefab/Instance/Instance.cpp @@ -326,6 +326,16 @@ namespace AzToolsFramework return *(m_nestedInstances[newInstanceAlias] = std::move(instance)); } + void Instance::DetachNestedInstances(const AZStd::function)>& callback) + { + for (auto&& [instanceAlias, instance] : m_nestedInstances) + { + instance->m_parent = nullptr; + callback(AZStd::move(instance)); + } + m_nestedInstances.clear(); + } + AZStd::unique_ptr Instance::DetachNestedInstance(const InstanceAlias& instanceAlias) { AZStd::unique_ptr removedNestedInstance; diff --git a/Code/Framework/AzToolsFramework/AzToolsFramework/Prefab/Instance/Instance.h b/Code/Framework/AzToolsFramework/AzToolsFramework/Prefab/Instance/Instance.h index 9d3ae31796..9fba839e1e 100644 --- a/Code/Framework/AzToolsFramework/AzToolsFramework/Prefab/Instance/Instance.h +++ b/Code/Framework/AzToolsFramework/AzToolsFramework/Prefab/Instance/Instance.h @@ -103,6 +103,7 @@ namespace AzToolsFramework Instance& AddInstance(AZStd::unique_ptr instance); Instance& AddInstance(AZStd::unique_ptr instance, InstanceAlias instanceAlias); AZStd::unique_ptr DetachNestedInstance(const InstanceAlias& instanceAlias); + void DetachNestedInstances(const AZStd::function)>& callback); /** * Gets the aliases for the entities in the Instance DOM. diff --git a/Code/Framework/AzToolsFramework/AzToolsFramework/Prefab/PrefabPublicHandler.cpp b/Code/Framework/AzToolsFramework/AzToolsFramework/Prefab/PrefabPublicHandler.cpp index 690d408007..3d02e797cc 100644 --- a/Code/Framework/AzToolsFramework/AzToolsFramework/Prefab/PrefabPublicHandler.cpp +++ b/Code/Framework/AzToolsFramework/AzToolsFramework/Prefab/PrefabPublicHandler.cpp @@ -1211,25 +1211,23 @@ namespace AzToolsFramework const auto instanceTemplateId = instancePtr->GetTemplateId(); auto parentContainerEntityId = parentInstance.GetContainerEntityId(); - instancePtr->GetNestedInstances( - [&](AZStd::unique_ptr& nestedInstancePtr) + + instancePtr->DetachNestedInstances( + [&](AZStd::unique_ptr detachedNestedInstance) { - //get previous link patch - auto linkRef = m_prefabSystemComponentInterface->FindLink(nestedInstancePtr->GetLinkId()); - PrefabDomValueReference linkPatches = linkRef->get().GetLinkPatches(); - AZ_Assert( - linkPatches.has_value(), "Unable to get patches on link with id '%llu' during prefab creation.", - nestedInstancePtr->GetLinkId()); + PrefabDom& nestedInstanceTemplateDom = + m_prefabSystemComponentInterface->FindTemplateDom(detachedNestedInstance->GetTemplateId()); - PrefabDom linkPatchesCopy; - linkPatchesCopy.CopyFrom(linkPatches->get(), linkPatchesCopy.GetAllocator()); - - RemoveLink(nestedInstancePtr, instanceTemplateId, undoBatch.GetUndoBatch()); - - UpdateLinkPatchesWithNewEntityAliases(linkPatchesCopy, oldEntityAliases, parentInstance); + Instance& nestedInstanceUnderNewParent = parentInstance.AddInstance(AZStd::move(detachedNestedInstance)); - CreateLink(*nestedInstancePtr, parentTemplateId, undoBatch.GetUndoBatch(), - AZStd::move(linkPatchesCopy), true); + PrefabDom nestedInstanceDomUnderNewParent; + m_instanceToTemplateInterface->GenerateDomForInstance( + nestedInstanceDomUnderNewParent, nestedInstanceUnderNewParent); + PrefabDom reparentPatch; + m_instanceToTemplateInterface->GeneratePatch( + reparentPatch, nestedInstanceTemplateDom, nestedInstanceDomUnderNewParent); + + CreateLink(nestedInstanceUnderNewParent, parentTemplateId, undoBatch.GetUndoBatch(), AZStd::move(reparentPatch), true); }); }