Fixing Save level As template path (#4054)

* Fixing Save level As template path

Signed-off-by: Mikhail Naumov <mnaumov@amazon.com>

* CR Feedback

Signed-off-by: Mikhail Naumov <mnaumov@amazon.com>

* CR feedback

Signed-off-by: Mikhail Naumov <mnaumov@amazon.com>

* PR feedback

Signed-off-by: Mikhail Naumov <mnaumov@amazon.com>

* remove emplace

Signed-off-by: Mikhail Naumov <mnaumov@amazon.com>

* more PR feedback

Signed-off-by: Mikhail Naumov <mnaumov@amazon.com>
This commit is contained in:
Mikhail Naumov
2021-09-15 19:19:01 -05:00
committed by GitHub
parent 0f5fc1de4d
commit a8908a987a
6 changed files with 57 additions and 1 deletions
@@ -221,7 +221,8 @@ namespace AzToolsFramework
AZ::IO::Path relativePath = m_loaderInterface->GenerateRelativePath(filename);
m_rootInstance->SetTemplateSourcePath(relativePath);
m_prefabSystemComponent->UpdateTemplateFilePath(m_rootInstance->GetTemplateId(), relativePath);
AZStd::string out;
if (!m_loaderInterface->SaveTemplateToString(m_rootInstance->GetTemplateId(), out))
{
@@ -424,6 +424,48 @@ namespace AzToolsFramework
return newTemplateId;
}
void PrefabSystemComponent::UpdateTemplateFilePath(TemplateId templateId, const AZ::IO::PathView& filePath)
{
auto findTemplateResult = FindTemplate(templateId);
if (!findTemplateResult.has_value())
{
AZ_Error(
"Prefab", false,
"Template associated by given Id '%llu' doesn't exist in PrefabSystemComponent.",
templateId);
return;
}
if (!filePath.IsRelative())
{
AZ_Error("Prefab", false, "Provided filePath '%.*s' must be relative.", AZ_STRING_ARG(filePath.Native()));
return;
}
Template& templateToChange = findTemplateResult->get();
if (templateToChange.GetFilePath() == filePath)
{
return;
}
m_templateFilePathToIdMap.erase(templateToChange.GetFilePath());
if (!m_templateFilePathToIdMap.try_emplace(filePath, templateId).second)
{
AZ_Error("Prefab", false, "Provided filePath '%.*s' already exists.", AZ_STRING_ARG(filePath.Native()));
return;
}
PrefabDom& prefabDom = templateToChange.GetPrefabDom();
PrefabDomValueReference pathReference = Prefab::PrefabDomUtils::FindPrefabDomValue(prefabDom, "Source");
if (pathReference)
{
const AZStd::string_view pathStr = filePath.Native();
pathReference->get().SetString(pathStr.data(), aznumeric_caster(pathStr.length()), prefabDom.GetAllocator());
}
templateToChange.SetFilePath(filePath);
}
void PrefabSystemComponent::RemoveTemplate(const TemplateId& templateId)
{
auto findTemplateResult = FindTemplate(templateId);
@@ -101,6 +101,13 @@ namespace AzToolsFramework
*/
TemplateId AddTemplate(const AZ::IO::Path& filePath, PrefabDom prefabDom) override;
/**
* Updates relative filepath location of a prefab (in case of SaveAs operation).
* @param templateId An id of a Template to change filepath of.
* @param filePath new relative path of the Template.
*/
void UpdateTemplateFilePath(TemplateId templateId, const AZ::IO::PathView& filePath) override;
/**
* Remove the Template associated with the given id from Prefab System Component.
* @param templateId A unique id of a Template.
@@ -33,6 +33,7 @@ namespace AzToolsFramework
virtual LinkReference FindLink(const LinkId& id) = 0;
virtual TemplateId AddTemplate(const AZ::IO::Path& filePath, PrefabDom prefabDom) = 0;
virtual void UpdateTemplateFilePath(TemplateId templateId, const AZ::IO::PathView& filePath) = 0;
virtual void RemoveTemplate(const TemplateId& templateId) = 0;
virtual void RemoveAllTemplates() = 0;
@@ -180,5 +180,9 @@ namespace AzToolsFramework
return m_filePath;
}
void Template::SetFilePath(const AZ::IO::PathView& path)
{
m_filePath = path;
}
} // namespace Prefab
} // namespace AzToolsFramework
@@ -63,6 +63,7 @@ namespace AzToolsFramework
PrefabDomValueConstReference GetInstancesValue() const;
const AZ::IO::Path& GetFilePath() const;
void SetFilePath(const AZ::IO::PathView& path);
private:
// Container for keeping links representing the Template's nested instances.