From 9dec723e33e1bd2ab498b5516868ab076697dcb4 Mon Sep 17 00:00:00 2001 From: srikappa Date: Tue, 8 Jun 2021 17:28:23 -0700 Subject: [PATCH 1/2] Remove file size limits when loading prefabs and prefab-based-levels --- Code/Framework/AzCore/AzCore/Utils/Utils.cpp | 31 +++++++++++++++++++ Code/Framework/AzCore/AzCore/Utils/Utils.h | 7 ++++- .../PrefabEditorEntityOwnershipService.cpp | 8 +---- .../AzToolsFramework/Prefab/PrefabLoader.cpp | 2 +- .../Prefab/PrefabLoaderInterface.h | 2 -- 5 files changed, 39 insertions(+), 11 deletions(-) diff --git a/Code/Framework/AzCore/AzCore/Utils/Utils.cpp b/Code/Framework/AzCore/AzCore/Utils/Utils.cpp index 7025b3977e..5521ff6131 100644 --- a/Code/Framework/AzCore/AzCore/Utils/Utils.cpp +++ b/Code/Framework/AzCore/AzCore/Utils/Utils.cpp @@ -169,6 +169,37 @@ namespace AZ::Utils template AZ::Outcome, AZStd::string> ReadFile(AZStd::string_view filePath, size_t maxFileSize); template AZ::Outcome, AZStd::string> ReadFile(AZStd::string_view filePath, size_t maxFileSize); + template + AZ::Outcome ReadFileWithNoSizeLimit(AZStd::string_view filePath) + { + IO::FileIOStream file; + if (!file.Open(filePath.data(), IO::OpenMode::ModeRead)) + { + return AZ::Failure(AZStd::string::format("Failed to open '%.*s'.", AZ_STRING_ARG(filePath))); + } + + AZ::IO::SizeType length = file.GetLength(); + + if (length == 0) + { + return AZ::Failure(AZStd::string::format("Failed to load '%.*s'. File is empty.", AZ_STRING_ARG(filePath))); + } + + Container fileContent; + fileContent.resize(length); + AZ::IO::SizeType bytesRead = file.Read(length, fileContent.data()); + file.Close(); + + // Resize again just in case bytesRead is less than length for some reason + fileContent.resize(bytesRead); + + return AZ::Success(AZStd::move(fileContent)); + } + + template AZ::Outcome ReadFileWithNoSizeLimit(AZStd::string_view filePath); + template AZ::Outcome, AZStd::string> ReadFileWithNoSizeLimit(AZStd::string_view filePath); + template AZ::Outcome, AZStd::string> ReadFileWithNoSizeLimit(AZStd::string_view filePath); + AZ::IO::FixedMaxPathString GetO3deManifestDirectory() { AZ::IO::FixedMaxPath path = GetHomeDirectory(); diff --git a/Code/Framework/AzCore/AzCore/Utils/Utils.h b/Code/Framework/AzCore/AzCore/Utils/Utils.h index 4e64ce5a39..6aaa1b868f 100644 --- a/Code/Framework/AzCore/AzCore/Utils/Utils.h +++ b/Code/Framework/AzCore/AzCore/Utils/Utils.h @@ -113,8 +113,13 @@ namespace AZ //! Save a string to a file. Otherwise returns a failure with error message. AZ::Outcome WriteFile(AZStd::string_view content, AZStd::string_view filePath); - //! Read a file into a string. Returns a failure with error message if the content could not be loaded. + //! Read a file into a string. Returns a failure with error message if the content could not be loaded or if + //! the file size is larger than the max file size provided. template AZ::Outcome ReadFile(AZStd::string_view filePath, size_t maxFileSize = DefaultMaxFileSize); + + //! Read a file into a string. Returns a failure with error message if the content could not be loaded. + template + AZ::Outcome ReadFileWithNoSizeLimit(AZStd::string_view filePath); } } diff --git a/Code/Framework/AzToolsFramework/AzToolsFramework/Entity/PrefabEditorEntityOwnershipService.cpp b/Code/Framework/AzToolsFramework/AzToolsFramework/Entity/PrefabEditorEntityOwnershipService.cpp index 7fd11ff9bf..fa7d5f6e9b 100644 --- a/Code/Framework/AzToolsFramework/AzToolsFramework/Entity/PrefabEditorEntityOwnershipService.cpp +++ b/Code/Framework/AzToolsFramework/AzToolsFramework/Entity/PrefabEditorEntityOwnershipService.cpp @@ -185,13 +185,7 @@ namespace AzToolsFramework bool PrefabEditorEntityOwnershipService::LoadFromStream(AZ::IO::GenericStream& stream, AZStd::string_view filename) { Reset(); - // Make loading from stream to behave the same in terms of filesize as regular loading of prefabs - // This may need to be revisited in the future for supporting higher sizes along with prefab loading - if (stream.GetLength() > Prefab::MaxPrefabFileSize) - { - AZ_Error("Prefab", false, "'%.*s' prefab content is bigger than the max supported size (%f MB)", AZ_STRING_ARG(filename), Prefab::MaxPrefabFileSize / (1024.f * 1024.f)); - return false; - } + const size_t bufSize = stream.GetLength(); AZStd::unique_ptr buf(new char[bufSize]); AZ::IO::SizeType bytes = stream.Read(bufSize, buf.get()); diff --git a/Code/Framework/AzToolsFramework/AzToolsFramework/Prefab/PrefabLoader.cpp b/Code/Framework/AzToolsFramework/AzToolsFramework/Prefab/PrefabLoader.cpp index d7de634c11..25a16f18a8 100644 --- a/Code/Framework/AzToolsFramework/AzToolsFramework/Prefab/PrefabLoader.cpp +++ b/Code/Framework/AzToolsFramework/AzToolsFramework/Prefab/PrefabLoader.cpp @@ -74,7 +74,7 @@ namespace AzToolsFramework return InvalidTemplateId; } - auto readResult = AZ::Utils::ReadFile(GetFullPath(filePath).Native(), MaxPrefabFileSize); + auto readResult = AZ::Utils::ReadFileWithNoSizeLimit(GetFullPath(filePath).Native()); if (!readResult.IsSuccess()) { AZ_Error( diff --git a/Code/Framework/AzToolsFramework/AzToolsFramework/Prefab/PrefabLoaderInterface.h b/Code/Framework/AzToolsFramework/AzToolsFramework/Prefab/PrefabLoaderInterface.h index 0e551cee6b..a9a1ee0607 100644 --- a/Code/Framework/AzToolsFramework/AzToolsFramework/Prefab/PrefabLoaderInterface.h +++ b/Code/Framework/AzToolsFramework/AzToolsFramework/Prefab/PrefabLoaderInterface.h @@ -21,8 +21,6 @@ namespace AzToolsFramework { namespace Prefab { - constexpr size_t MaxPrefabFileSize = 1024 * 1024; - /*! * PrefabLoaderInterface * Interface for saving/loading Prefab files. From b23c95cab3872bb619768274ec29949f71de27c4 Mon Sep 17 00:00:00 2001 From: srikappa Date: Tue, 8 Jun 2021 17:58:59 -0700 Subject: [PATCH 2/2] Removed the new added flavor of ReadFile and reused existing one --- Code/Framework/AzCore/AzCore/Utils/Utils.cpp | 31 ------------------- Code/Framework/AzCore/AzCore/Utils/Utils.h | 4 --- .../AzToolsFramework/Prefab/PrefabLoader.cpp | 2 +- 3 files changed, 1 insertion(+), 36 deletions(-) diff --git a/Code/Framework/AzCore/AzCore/Utils/Utils.cpp b/Code/Framework/AzCore/AzCore/Utils/Utils.cpp index 5521ff6131..7025b3977e 100644 --- a/Code/Framework/AzCore/AzCore/Utils/Utils.cpp +++ b/Code/Framework/AzCore/AzCore/Utils/Utils.cpp @@ -169,37 +169,6 @@ namespace AZ::Utils template AZ::Outcome, AZStd::string> ReadFile(AZStd::string_view filePath, size_t maxFileSize); template AZ::Outcome, AZStd::string> ReadFile(AZStd::string_view filePath, size_t maxFileSize); - template - AZ::Outcome ReadFileWithNoSizeLimit(AZStd::string_view filePath) - { - IO::FileIOStream file; - if (!file.Open(filePath.data(), IO::OpenMode::ModeRead)) - { - return AZ::Failure(AZStd::string::format("Failed to open '%.*s'.", AZ_STRING_ARG(filePath))); - } - - AZ::IO::SizeType length = file.GetLength(); - - if (length == 0) - { - return AZ::Failure(AZStd::string::format("Failed to load '%.*s'. File is empty.", AZ_STRING_ARG(filePath))); - } - - Container fileContent; - fileContent.resize(length); - AZ::IO::SizeType bytesRead = file.Read(length, fileContent.data()); - file.Close(); - - // Resize again just in case bytesRead is less than length for some reason - fileContent.resize(bytesRead); - - return AZ::Success(AZStd::move(fileContent)); - } - - template AZ::Outcome ReadFileWithNoSizeLimit(AZStd::string_view filePath); - template AZ::Outcome, AZStd::string> ReadFileWithNoSizeLimit(AZStd::string_view filePath); - template AZ::Outcome, AZStd::string> ReadFileWithNoSizeLimit(AZStd::string_view filePath); - AZ::IO::FixedMaxPathString GetO3deManifestDirectory() { AZ::IO::FixedMaxPath path = GetHomeDirectory(); diff --git a/Code/Framework/AzCore/AzCore/Utils/Utils.h b/Code/Framework/AzCore/AzCore/Utils/Utils.h index 6aaa1b868f..d082a3ebe0 100644 --- a/Code/Framework/AzCore/AzCore/Utils/Utils.h +++ b/Code/Framework/AzCore/AzCore/Utils/Utils.h @@ -117,9 +117,5 @@ namespace AZ //! the file size is larger than the max file size provided. template AZ::Outcome ReadFile(AZStd::string_view filePath, size_t maxFileSize = DefaultMaxFileSize); - - //! Read a file into a string. Returns a failure with error message if the content could not be loaded. - template - AZ::Outcome ReadFileWithNoSizeLimit(AZStd::string_view filePath); } } diff --git a/Code/Framework/AzToolsFramework/AzToolsFramework/Prefab/PrefabLoader.cpp b/Code/Framework/AzToolsFramework/AzToolsFramework/Prefab/PrefabLoader.cpp index 25a16f18a8..44dff93cb5 100644 --- a/Code/Framework/AzToolsFramework/AzToolsFramework/Prefab/PrefabLoader.cpp +++ b/Code/Framework/AzToolsFramework/AzToolsFramework/Prefab/PrefabLoader.cpp @@ -74,7 +74,7 @@ namespace AzToolsFramework return InvalidTemplateId; } - auto readResult = AZ::Utils::ReadFileWithNoSizeLimit(GetFullPath(filePath).Native()); + auto readResult = AZ::Utils::ReadFile(GetFullPath(filePath).Native(), AZStd::numeric_limits::max()); if (!readResult.IsSuccess()) { AZ_Error(