{lyn7065} adding ProcPrefab Prefab::Tempate flag method (#4765)
* {lyn7065} adding ProcPrefab Prefab::Tempate flag method
Signed-off-by: jackalbe <23512001+jackalbe@users.noreply.github.com>
* updated based on comments
Signed-off-by: jackalbe <23512001+jackalbe@users.noreply.github.com>
* moved validation logic to IsValid()
Signed-off-by: jackalbe <23512001+jackalbe@users.noreply.github.com>
* added more guards around the source string
Signed-off-by: jackalbe <23512001+jackalbe@users.noreply.github.com>
This commit is contained in:
@@ -66,7 +66,16 @@ namespace AzToolsFramework
|
||||
|
||||
bool Template::IsValid() const
|
||||
{
|
||||
return !m_prefabDom.IsNull() && !m_filePath.empty();
|
||||
if (m_prefabDom.IsNull() || m_filePath.empty())
|
||||
{
|
||||
return false;
|
||||
}
|
||||
else if (!m_prefabDom.IsObject())
|
||||
{
|
||||
return false;
|
||||
}
|
||||
auto source = m_prefabDom.FindMember(PrefabDomUtils::SourceName);
|
||||
return (source != m_prefabDom.MemberEnd());
|
||||
}
|
||||
|
||||
bool Template::IsLoadedWithErrors() const
|
||||
@@ -175,6 +184,26 @@ namespace AzToolsFramework
|
||||
return findInstancesResult->get();
|
||||
}
|
||||
|
||||
bool Template::IsProcedural() const
|
||||
{
|
||||
if (m_isProcedural.has_value())
|
||||
{
|
||||
return m_isProcedural.value();
|
||||
}
|
||||
else if (!IsValid())
|
||||
{
|
||||
return false;
|
||||
}
|
||||
auto source = m_prefabDom.FindMember(PrefabDomUtils::SourceName);
|
||||
if (!source->value.IsString())
|
||||
{
|
||||
return false;
|
||||
}
|
||||
AZ::IO::PathView path(source->value.GetString());
|
||||
m_isProcedural = AZStd::make_optional(path.Extension().Match(".procprefab"));
|
||||
return m_isProcedural.value();
|
||||
}
|
||||
|
||||
const AZ::IO::Path& Template::GetFilePath() const
|
||||
{
|
||||
return m_filePath;
|
||||
|
||||
@@ -65,6 +65,9 @@ namespace AzToolsFramework
|
||||
const AZ::IO::Path& GetFilePath() const;
|
||||
void SetFilePath(const AZ::IO::PathView& path);
|
||||
|
||||
// To tell if this Template was created from an product asset
|
||||
bool IsProcedural() const;
|
||||
|
||||
private:
|
||||
// Container for keeping links representing the Template's nested instances.
|
||||
Links m_links;
|
||||
@@ -80,6 +83,9 @@ namespace AzToolsFramework
|
||||
|
||||
// Flag to tell if this Template has changes that have yet to be saved to file.
|
||||
bool m_isDirty = false;
|
||||
|
||||
// Flag to tell if this Template was generated outside the Editor
|
||||
mutable AZStd::optional<bool> m_isProcedural;
|
||||
};
|
||||
} // namespace Prefab
|
||||
} // namespace AzToolsFramework
|
||||
|
||||
@@ -14,6 +14,7 @@
|
||||
#include <Prefab/PrefabTestFixture.h>
|
||||
#include <Prefab/Procedural/ProceduralPrefabAsset.h>
|
||||
#include <AzCore/Serialization/Json/RegistrationContext.h>
|
||||
#include <AzToolsFramework/Prefab/Template/Template.h>
|
||||
|
||||
namespace UnitTest
|
||||
{
|
||||
@@ -132,4 +133,32 @@ namespace UnitTest
|
||||
EXPECT_TRUE(outputValue.HasMember("member"));
|
||||
EXPECT_STREQ(outputValue.FindMember("member")->value.GetString(), "value");
|
||||
}
|
||||
|
||||
TEST_F(ProceduralPrefabAssetTest, Template_IsProcPrefab_DefaultsToNotProcPrefab)
|
||||
{
|
||||
AzToolsFramework::Prefab::PrefabDom dom;
|
||||
dom.SetObject();
|
||||
dom.AddMember("Source", "foo.prefab", dom.GetAllocator());
|
||||
AzToolsFramework::Prefab::Template fooTemplate("foo", AZStd::move(dom));
|
||||
EXPECT_FALSE(fooTemplate.IsProcedural());
|
||||
}
|
||||
|
||||
TEST_F(ProceduralPrefabAssetTest, Template_IsProcPrefab_DomDrivesFlagToTrue)
|
||||
{
|
||||
AzToolsFramework::Prefab::PrefabDom dom;
|
||||
dom.SetObject();
|
||||
dom.AddMember("Source", "foo.procprefab", dom.GetAllocator());
|
||||
AzToolsFramework::Prefab::Template fooTemplate("foo", AZStd::move(dom));
|
||||
EXPECT_TRUE(fooTemplate.IsProcedural());
|
||||
// the second time should use the cached version of the flag
|
||||
EXPECT_TRUE(fooTemplate.IsProcedural());
|
||||
}
|
||||
|
||||
TEST_F(ProceduralPrefabAssetTest, Template_IsProcPrefab_FailsWithNoSource)
|
||||
{
|
||||
AzToolsFramework::Prefab::PrefabDom dom;
|
||||
dom.SetObject();
|
||||
AzToolsFramework::Prefab::Template fooTemplate("foo", AZStd::move(dom));
|
||||
EXPECT_FALSE(fooTemplate.IsProcedural());
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user