diff --git a/Code/Framework/AzCore/AzCore/Component/ComponentApplication.cpp b/Code/Framework/AzCore/AzCore/Component/ComponentApplication.cpp index cf23d2b4d7..fb0e4b82b8 100644 --- a/Code/Framework/AzCore/AzCore/Component/ComponentApplication.cpp +++ b/Code/Framework/AzCore/AzCore/Component/ComponentApplication.cpp @@ -45,6 +45,7 @@ #include #include +#include #include #include @@ -1547,7 +1548,7 @@ namespace AZ // reflect name dictionary. Name::Reflect(context); // reflect path - IO::PathReflection::Reflect(context); + IO::PathReflect(context); // reflect the SettingsRegistryInterface, SettignsRegistryImpl and the global Settings Registry // instance (AZ::SettingsRegistry::Get()) into the Behavior Context diff --git a/Code/Framework/AzCore/AzCore/IO/Path/Path.cpp b/Code/Framework/AzCore/AzCore/IO/Path/Path.cpp index 637945db35..6e4dfdaa63 100644 --- a/Code/Framework/AzCore/AzCore/IO/Path/Path.cpp +++ b/Code/Framework/AzCore/AzCore/IO/Path/Path.cpp @@ -50,13 +50,4 @@ namespace AZ::IO const PathIterator& rhs); template bool operator!=(const PathIterator& lhs, const PathIterator& rhs); - - void PathReflection::Reflect(AZ::ReflectContext* context) - { - if (auto* serializeContext = azrtti_cast(context)) - { - serializeContext->Class() - ->Field("m_path", &AZ::IO::Path::m_path); - } - } } diff --git a/Code/Framework/AzCore/AzCore/IO/Path/Path.h b/Code/Framework/AzCore/AzCore/IO/Path/Path.h index 3b5c224957..355d9ddedb 100644 --- a/Code/Framework/AzCore/AzCore/IO/Path/Path.h +++ b/Code/Framework/AzCore/AzCore/IO/Path/Path.h @@ -321,7 +321,6 @@ namespace AZ::IO using const_iterator = const PathIterator; using iterator = const_iterator; friend PathIterator; - friend struct PathReflection; // constructors and destructor constexpr BasicPath() = default; @@ -665,6 +664,7 @@ namespace AZ::IO namespace AZ { AZ_TYPE_INFO_SPECIALIZE(AZ::IO::Path, "{88E0A40F-3085-4CAB-8B11-EF5A2659C71A}"); + AZ_TYPE_INFO_SPECIALIZE(AZ::IO::FixedMaxPath, "{FA6CA49F-376A-417C-9767-DD50744DF203}"); } namespace AZ::IO diff --git a/Code/Framework/AzCore/AzCore/IO/Path/PathReflect.cpp b/Code/Framework/AzCore/AzCore/IO/Path/PathReflect.cpp new file mode 100644 index 0000000000..55a991f19f --- /dev/null +++ b/Code/Framework/AzCore/AzCore/IO/Path/PathReflect.cpp @@ -0,0 +1,77 @@ +/* + * 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 +#include +#include + +namespace AZ::IO +{ + template + struct PathSerializer + : public SerializeContext::IDataSerializer + { + public: + /// Convert binary data to text + size_t DataToText(IO::GenericStream& in, IO::GenericStream& out, bool) override + { + PathType outPath; + outPath.Native().resize_no_construct(in.GetLength()); + in.Read(outPath.Native().size(), outPath.Native().data()); + + return static_cast(out.Write(outPath.Native().size(), outPath.Native().c_str())); + } + + size_t TextToData(const char* text, unsigned int, IO::GenericStream& stream, bool) override + { + return static_cast(stream.Write(strlen(text), reinterpret_cast(text))); + } + + size_t Save(const void* classPtr, IO::GenericStream& stream, bool) override + { + /// Save paths out using the PosixPathSeparator + PathType path(reinterpret_cast(classPtr)->Native(), AZ::IO::PosixPathSeparator); + path.MakePreferred(); + + return static_cast(stream.Write(path.Native().size(), path.c_str())); + } + + bool Load(void* classPtr, IO::GenericStream& stream, unsigned int, bool) override + { + // Normalize the path load + auto path = reinterpret_cast(classPtr); + + path->Native().resize_no_construct(stream.GetLength()); + stream.Read(path->Native().size(), path->Native().data()); + *path = path->LexicallyNormal(); + + return true; + } + + bool CompareValueData(const void* lhs, const void* rhs) override + { + return SerializeContext::EqualityCompareHelper::CompareValues(lhs, rhs); + } + }; + + void PathReflect(AZ::ReflectContext* context) + { + if (auto serializeContext = azrtti_cast(context); serializeContext != nullptr) + { + serializeContext->Class() + ->Serializer(AZ::SerializeContext::IDataSerializerPtr{ new PathSerializer{}, + AZ::SerializeContext::IDataSerializer::CreateDefaultDeleteDeleter() }) + ; + + serializeContext->Class() + ->Serializer(AZ::SerializeContext::IDataSerializerPtr{ new PathSerializer{}, + AZ::SerializeContext::IDataSerializer::CreateDefaultDeleteDeleter() }) + ; + } + } +} diff --git a/Code/Framework/AzCore/AzCore/IO/Path/PathReflect.h b/Code/Framework/AzCore/AzCore/IO/Path/PathReflect.h new file mode 100644 index 0000000000..3076895026 --- /dev/null +++ b/Code/Framework/AzCore/AzCore/IO/Path/PathReflect.h @@ -0,0 +1,19 @@ +/* + * 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 + * + */ + +#pragma once + +namespace AZ +{ + class ReflectContext; +} + +namespace AZ::IO +{ + void PathReflect(AZ::ReflectContext* context); +} diff --git a/Code/Framework/AzCore/AzCore/IO/Path/Path_fwd.h b/Code/Framework/AzCore/AzCore/IO/Path/Path_fwd.h index cb61bd5887..c4fce5e1b7 100644 --- a/Code/Framework/AzCore/AzCore/IO/Path/Path_fwd.h +++ b/Code/Framework/AzCore/AzCore/IO/Path/Path_fwd.h @@ -57,11 +57,6 @@ namespace AZ::IO // It depends on the path type template class PathIterator; - - struct PathReflection - { - static void Reflect(AZ::ReflectContext* context); - }; } namespace AZStd diff --git a/Code/Framework/AzCore/AzCore/azcore_files.cmake b/Code/Framework/AzCore/AzCore/azcore_files.cmake index 30662f37c4..0c381c9850 100644 --- a/Code/Framework/AzCore/AzCore/azcore_files.cmake +++ b/Code/Framework/AzCore/AzCore/azcore_files.cmake @@ -187,6 +187,8 @@ set(FILES IO/Path/Path.inl IO/Path/PathIterable.inl IO/Path/PathParser.inl + IO/Path/PathReflect.cpp + IO/Path/PathReflect.h IO/Path/Path_fwd.h IO/SystemFile.cpp IO/SystemFile.h diff --git a/Code/Framework/AzCore/Tests/Serialization.cpp b/Code/Framework/AzCore/Tests/Serialization.cpp index 219744a480..1666913317 100644 --- a/Code/Framework/AzCore/Tests/Serialization.cpp +++ b/Code/Framework/AzCore/Tests/Serialization.cpp @@ -59,6 +59,7 @@ #include #include #include +#include #include #include @@ -8151,5 +8152,98 @@ namespace UnitTest m_serializeContext->Class(); m_serializeContext->DisableRemoveReflection(); } + + template + class PathSerializationParamFixture + : public ScopedAllocatorSetupFixture + , public ::testing::WithParamInterface + { + public: + PathSerializationParamFixture() + : ScopedAllocatorSetupFixture( + []() { AZ::SystemAllocator::Descriptor desc; desc.m_stackRecordLevels = 30; return desc; }() + ) + {} + + // We must expose the class for serialization first. + void SetUp() override + { + m_serializeContext = AZStd::make_unique(); + AZ::IO::PathReflect(m_serializeContext.get()); + + } + + void TearDown() override + { + m_serializeContext->EnableRemoveReflection(); + AZ::IO::PathReflect(m_serializeContext.get()); + m_serializeContext->DisableRemoveReflection(); + + m_serializeContext.reset(); + } + + protected: + AZStd::unique_ptr m_serializeContext; + }; + + struct PathSerializationParams + { + const char m_preferredSeparator{}; + const char* m_testPath{}; + }; + using PathSerializationFixture = PathSerializationParamFixture; + + TEST_P(PathSerializationFixture, PathSerializer_SerializesStringBackedPath_Succeeds) + { + const auto& testParams = GetParam(); + { + // Path serialization + AZ::IO::Path testPath{ testParams.m_testPath, testParams.m_preferredSeparator }; + + AZStd::vector byteBuffer; + AZ::IO::ByteContainerStream byteStream(&byteBuffer); + auto objStream = AZ::ObjectStream::Create(&byteStream, *m_serializeContext, AZ::ObjectStream::ST_XML); + objStream->WriteClass(&testPath); + objStream->Finalize(); + + byteStream.Seek(0, AZ::IO::GenericStream::ST_SEEK_BEGIN); + + AZ::IO::Path loadPath{ testParams.m_preferredSeparator }; + EXPECT_TRUE(AZ::Utils::LoadObjectFromStreamInPlace(byteStream, loadPath, m_serializeContext.get())); + EXPECT_EQ(testPath.LexicallyNormal(), loadPath); + } + + { + // FixedMaxPath serialization + AZ::IO::FixedMaxPath testFixedMaxPath{ testParams.m_testPath, testParams.m_preferredSeparator }; + + AZStd::vector byteBuffer; + AZ::IO::ByteContainerStream byteStream(&byteBuffer); + auto objStream = AZ::ObjectStream::Create(&byteStream, *m_serializeContext, AZ::ObjectStream::ST_XML); + objStream->WriteClass(&testFixedMaxPath); + objStream->Finalize(); + + byteStream.Seek(0, AZ::IO::GenericStream::ST_SEEK_BEGIN); + + AZ::IO::FixedMaxPath loadPath{ testParams.m_preferredSeparator }; + EXPECT_TRUE(AZ::Utils::LoadObjectFromStreamInPlace(byteStream, loadPath, m_serializeContext.get())); + EXPECT_EQ(testFixedMaxPath.LexicallyNormal(), loadPath); + } + } + + INSTANTIATE_TEST_CASE_P( + PathSerialization, + PathSerializationFixture, + ::testing::Values( + PathSerializationParams{ AZ::IO::PosixPathSeparator, "" }, + PathSerializationParams{ AZ::IO::PosixPathSeparator, "test" }, + PathSerializationParams{ AZ::IO::PosixPathSeparator, "/test" }, + PathSerializationParams{ AZ::IO::WindowsPathSeparator, "test" }, + PathSerializationParams{ AZ::IO::WindowsPathSeparator, "/test" }, + PathSerializationParams{ AZ::IO::WindowsPathSeparator, "D:test" }, + PathSerializationParams{ AZ::IO::WindowsPathSeparator, "D:/test" }, + PathSerializationParams{ AZ::IO::WindowsPathSeparator, "test/foo/../bar" } + ) + ); }