/* * 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 #include #include #include #include #include #include #include #include #include #include namespace AZ { namespace RPI { AZ_CLASS_ALLOCATOR_IMPL(JsonMaterialPropertyValueSerializer, SystemAllocator, 0); template JsonSerializationResult::ResultCode JsonMaterialPropertyValueSerializer::LoadVariant( MaterialPropertyValue& intoValue, const T& defaultValue, const rapidjson::Value& inputValue, JsonDeserializerContext& context) { T value = defaultValue; JsonSerializationResult::ResultCode result = ContinueLoading(&value, azrtti_typeid(), inputValue, context); intoValue = value; return result; } JsonSerializationResult::Result JsonMaterialPropertyValueSerializer::Load(void* outputValue, const Uuid& outputValueTypeId, const rapidjson::Value& inputValue, JsonDeserializerContext& context) { namespace JSR = JsonSerializationResult; AZ_Assert(azrtti_typeid() == outputValueTypeId, "Unable to deserialize material property value to json because the provided type is %s", outputValueTypeId.ToString().c_str()); AZ_UNUSED(outputValueTypeId); MaterialSourceData::Property* property = reinterpret_cast(outputValue); AZ_Assert(property, "Output value for JsonMaterialPropertyValueSerializer can't be null."); const MaterialTypeSourceData* materialType = context.GetMetadata().Find(); if (!materialType) { AZ_Assert(false, "Material type reference not found"); return context.Report(JsonSerializationResult::Tasks::ReadField, JsonSerializationResult::Outcomes::Catastrophic, "Material type reference not found."); } // Construct the full property name (groupName.propertyName) by parsing it from the JSON path string. size_t startPropertyName = context.GetPath().Get().rfind('/'); size_t startGroupName = context.GetPath().Get().rfind('/', startPropertyName-1); AZStd::string_view groupName = context.GetPath().Get().substr(startGroupName + 1, startPropertyName - startGroupName - 1); AZStd::string_view propertyName = context.GetPath().Get().substr(startPropertyName + 1); JSR::ResultCode result(JSR::Tasks::ReadField); auto propertyDefinition = materialType->FindProperty(groupName, propertyName); if (!propertyDefinition) { AZStd::string message = AZStd::string::format("Property '%.*s.%.*s' not found in material type.", AZ_STRING_ARG(groupName), AZ_STRING_ARG(propertyName)); return context.Report(JsonSerializationResult::Tasks::ReadField, JsonSerializationResult::Outcomes::Unsupported, message); } else { switch (propertyDefinition->m_dataType) { case MaterialPropertyDataType::Bool: result.Combine(LoadVariant(property->m_value, false, inputValue, context)); break; case MaterialPropertyDataType::Int: result.Combine(LoadVariant(property->m_value, 0, inputValue, context)); break; case MaterialPropertyDataType::UInt: result.Combine(LoadVariant(property->m_value, 0u, inputValue, context)); break; case MaterialPropertyDataType::Float: result.Combine(LoadVariant(property->m_value, 0.0f, inputValue, context)); break; case MaterialPropertyDataType::Vector2: result.Combine(LoadVariant(property->m_value, Vector2{0.0f, 0.0f}, inputValue, context)); break; case MaterialPropertyDataType::Vector3: result.Combine(LoadVariant(property->m_value, Vector3{0.0f, 0.0f, 0.0f}, inputValue, context)); break; case MaterialPropertyDataType::Vector4: result.Combine(LoadVariant(property->m_value, Vector4{0.0f, 0.0f, 0.0f, 0.0f}, inputValue, context)); break; case MaterialPropertyDataType::Color: result.Combine(LoadVariant(property->m_value, AZ::Colors::White, inputValue, context)); break; case MaterialPropertyDataType::Image: case MaterialPropertyDataType::Enum: result.Combine(LoadVariant(property->m_value, AZStd::string{}, inputValue, context)); break; default: return context.Report(JsonSerializationResult::Tasks::ReadField, JsonSerializationResult::Outcomes::Unsupported, "Unknown data type"); } } if (result.GetProcessing() == JsonSerializationResult::Processing::Completed) { return context.Report(result, "Successfully loaded property value."); } else { return context.Report(result, "Partially loaded property value."); } } JsonSerializationResult::Result JsonMaterialPropertyValueSerializer::Store(rapidjson::Value& outputValue, const void* inputValue, [[maybe_unused]] const void* defaultValue, const Uuid& valueTypeId, JsonSerializerContext& context) { namespace JSR = JsonSerializationResult; AZ_Assert(azrtti_typeid() == valueTypeId, "Unable to serialize material property value to json because the provided type is %s", valueTypeId.ToString().c_str()); AZ_UNUSED(valueTypeId); const MaterialSourceData::Property* property = reinterpret_cast(inputValue); AZ_Assert(property, "Input value for JsonMaterialPropertyValueSerializer can't be null."); JSR::ResultCode result(JSR::Tasks::WriteValue); if (property->m_value.Is()) { result.Combine(ContinueStoring(outputValue, &property->m_value.GetValue(), nullptr, azrtti_typeid(), context)); } else if (property->m_value.Is()) { result.Combine(ContinueStoring(outputValue, &property->m_value.GetValue(), nullptr, azrtti_typeid(), context)); } else if (property->m_value.Is()) { result.Combine(ContinueStoring(outputValue, &property->m_value.GetValue(), nullptr, azrtti_typeid(), context)); } else if (property->m_value.Is()) { result.Combine(ContinueStoring(outputValue, &property->m_value.GetValue(), nullptr, azrtti_typeid(), context)); } else if (property->m_value.Is()) { result.Combine(ContinueStoring(outputValue, &property->m_value.GetValue(), nullptr, azrtti_typeid(), context)); } else if (property->m_value.Is()) { result.Combine(ContinueStoring(outputValue, &property->m_value.GetValue(), nullptr, azrtti_typeid(), context)); } else if (property->m_value.Is()) { result.Combine(ContinueStoring(outputValue, &property->m_value.GetValue(), nullptr, azrtti_typeid(), context)); } else if (property->m_value.Is()) { result.Combine(ContinueStoring(outputValue, &property->m_value.GetValue(), nullptr, azrtti_typeid(), context)); } else if (property->m_value.Is()) { result.Combine(ContinueStoring(outputValue, &property->m_value.GetValue(), nullptr, azrtti_typeid(), context)); } if (result.GetProcessing() == JsonSerializationResult::Processing::Completed) { return context.Report(result, "Successfully stored property value."); } else { return context.Report(result, "Partially stored property value."); } } } // namespace RPI } // namespace AZ