From 2c54b48a9e726906aa6b39780a1ea68af1dcc2ac Mon Sep 17 00:00:00 2001 From: Chris Galvan Date: Thu, 3 Feb 2022 16:20:55 -0600 Subject: [PATCH] Modified image gradient config json serializer to be able to detect version difference earlier Signed-off-by: Chris Galvan --- .../Components/ImageGradientComponent.cpp | 116 +++++++++--------- 1 file changed, 58 insertions(+), 58 deletions(-) diff --git a/Gems/GradientSignal/Code/Source/Components/ImageGradientComponent.cpp b/Gems/GradientSignal/Code/Source/Components/ImageGradientComponent.cpp index c06792c1f1..468b96e5ad 100644 --- a/Gems/GradientSignal/Code/Source/Components/ImageGradientComponent.cpp +++ b/Gems/GradientSignal/Code/Source/Components/ImageGradientComponent.cpp @@ -23,6 +23,16 @@ namespace GradientSignal void* outputValue, [[maybe_unused]] const AZ::Uuid& outputValueTypeId, const rapidjson::Value& inputValue, AZ::JsonDeserializerContext& context) { + // We can distinguish between version 1 and 2 by the presence of the "ImageAsset" field, + // which is only in version 1. + // For version 2, we don't need to do any special processing, so just let the base class + // load the JSON if we don't find the "ImageAsset" field. + rapidjson::Value::ConstMemberIterator itr = inputValue.FindMember("ImageAsset"); + if (itr == inputValue.MemberEnd()) + { + return AZ::BaseJsonSerializer::Load(outputValue, outputValueTypeId, inputValue, context); + } + namespace JSR = AZ::JsonSerializationResult; auto configInstance = reinterpret_cast(outputValue); @@ -30,69 +40,59 @@ namespace GradientSignal JSR::ResultCode result(JSR::Tasks::ReadField); - rapidjson::Value::ConstMemberIterator itr = inputValue.FindMember("ImageAsset"); - if (itr != inputValue.MemberEnd()) - { - // Version 1 stored a custom GradientSignal::ImageAsset as the image asset. - // In Version 2, we changed the image asset to use the generic AZ::RPI::StreamingImageAsset, - // so they are both AZ::Data::Asset but reference different types. - // Using the assetHint, which will be something like "my_test_image.gradimage", - // we need to find the valid streaming image asset product from the same source, - // which will be something like "my_test_image.png.streamingimage" - AZStd::string assetHint; - AZ::Data::AssetId fixedAssetId; - auto it = itr->value.FindMember("assetHint"); - if (it != itr->value.MemberEnd()) - { - AZ::ScopedContextPath subPath(context, "assetHint"); - result.Combine(ContinueLoading(&assetHint, azrtti_typeid(), it->value, context)); - - if (assetHint.ends_with(".gradimage")) - { - // We don't know what image format the original source was, so we need to loop through - // all the supported image extensions to check if they have a valid corresponding - // streaming image asset - for (int i = 0; i < ImageProcessingAtom::s_TotalSupportedImageExtensions; i++) - { - AZStd::string imageExtension(ImageProcessingAtom::s_SupportedImageExtensions[i]); - - // The image extensions are stored with a wildcard (e.g. *.png) so we need to strip that off first - AZ::StringFunc::Replace(imageExtension, "*", ""); - - // Form potential streaming image path (e.g. my_test_image.png.streamingimage) - AZStd::string potentialStreamingImagePath(assetHint); - AZ::StringFunc::Replace(potentialStreamingImagePath, ".gradimage", ""); - potentialStreamingImagePath += imageExtension + ".streamingimage"; - - // Check if there is a valid streaming image asset for this path - AZ::Data::AssetCatalogRequestBus::BroadcastResult(fixedAssetId, &AZ::Data::AssetCatalogRequestBus::Events::GetAssetIdByPath, potentialStreamingImagePath.c_str(), azrtti_typeid>(), false); - if (fixedAssetId.IsValid()) - { - break; - } - } - } - } - - // Replace the old gradimage with new AssetId for streaming image asset (if we needed to replace) - if (fixedAssetId.IsValid()) - { - configInstance->m_imageAsset = AZ::Data::AssetManager::Instance().GetAsset(fixedAssetId, AZ::Data::AssetLoadBehavior::QueueLoad); - } - // Otherwise, this was already a streaming image asset, so just load it like normal - else - { - result.Combine(ContinueLoadingFromJsonObjectField( - &configInstance->m_imageAsset, azrtti_typeidm_imageAsset)>(), inputValue, "ImageAsset", context)); - } - } - result.Combine(ContinueLoadingFromJsonObjectField( &configInstance->m_tilingX, azrtti_typeidm_tilingX)>(), inputValue, "TilingX", context)); result.Combine(ContinueLoadingFromJsonObjectField( &configInstance->m_tilingY, azrtti_typeidm_tilingY)>(), inputValue, "TilingY", context)); + // Version 1 stored a custom GradientSignal::ImageAsset as the image asset. + // In Version 2, we changed the image asset to use the generic AZ::RPI::StreamingImageAsset, + // so they are both AZ::Data::Asset but reference different types. + // Using the assetHint, which will be something like "my_test_image.gradimage", + // we need to find the valid streaming image asset product from the same source, + // which will be something like "my_test_image.png.streamingimage" + AZStd::string assetHint; + AZ::Data::AssetId fixedAssetId; + auto it = itr->value.FindMember("assetHint"); + if (it != itr->value.MemberEnd()) + { + AZ::ScopedContextPath subPath(context, "assetHint"); + result.Combine(ContinueLoading(&assetHint, azrtti_typeid(), it->value, context)); + + if (assetHint.ends_with(".gradimage")) + { + // We don't know what image format the original source was, so we need to loop through + // all the supported image extensions to check if they have a valid corresponding + // streaming image asset + for (auto& supportedImageExtension : ImageProcessingAtom::s_SupportedImageExtensions) + { + AZStd::string imageExtension(supportedImageExtension); + + // The image extensions are stored with a wildcard (e.g. *.png) so we need to strip that off first + AZ::StringFunc::Replace(imageExtension, "*", ""); + + // Form potential streaming image path (e.g. my_test_image.png.streamingimage) + AZStd::string potentialStreamingImagePath(assetHint); + AZ::StringFunc::Replace(potentialStreamingImagePath, ".gradimage", ""); + potentialStreamingImagePath += imageExtension + ".streamingimage"; + + // Check if there is a valid streaming image asset for this path + AZ::Data::AssetCatalogRequestBus::BroadcastResult(fixedAssetId, &AZ::Data::AssetCatalogRequestBus::Events::GetAssetIdByPath, potentialStreamingImagePath.c_str(), azrtti_typeid>(), false); + if (fixedAssetId.IsValid()) + { + break; + } + } + } + } + + // Replace the old gradimage with new AssetId for streaming image asset + if (fixedAssetId.IsValid()) + { + configInstance->m_imageAsset = AZ::Data::AssetManager::Instance().GetAsset(fixedAssetId, AZ::Data::AssetLoadBehavior::QueueLoad); + } + return context.Report(result, result.GetProcessing() != JSR::Processing::Halted ? "Successfully loaded ImageGradientConfig information." : @@ -113,9 +113,9 @@ namespace GradientSignal { serialize->Class() ->Version(2) - ->Field("ImageAsset", &ImageGradientConfig::m_imageAsset) ->Field("TilingX", &ImageGradientConfig::m_tilingX) ->Field("TilingY", &ImageGradientConfig::m_tilingY) + ->Field("StreamingImageAsset", &ImageGradientConfig::m_imageAsset) ; AZ::EditContext* edit = serialize->GetEditContext();