|
| 1 | +/* |
| 2 | + * Copyright (c) Contributors to the Open 3D Engine Project. |
| 3 | + * For complete copyright and license terms please see the LICENSE at the root of this distribution. |
| 4 | + * |
| 5 | + * SPDX-License-Identifier: Apache-2.0 OR MIT |
| 6 | + * |
| 7 | + */ |
| 8 | + |
| 9 | +#include "AssetPlatformComponentRemover.h" |
| 10 | + |
| 11 | +#include <AzCore/Component/ComponentApplicationBus.h> |
| 12 | +#include <AzCore/RTTI/ReflectContext.h> |
| 13 | +#include <AzCore/Serialization/SerializeContext.h> |
| 14 | +#include <AzToolsFramework/Prefab/Spawnable/PrefabDocument.h> |
| 15 | + |
| 16 | + |
| 17 | +namespace AzToolsFramework::Prefab::PrefabConversionUtils |
| 18 | +{ |
| 19 | + void AssetPlatformComponentRemover::Reflect(AZ::ReflectContext* context) |
| 20 | + { |
| 21 | + if (auto* serializeContext = azrtti_cast<AZ::SerializeContext*>(context)) |
| 22 | + { |
| 23 | + serializeContext->Class<AssetPlatformComponentRemover, PrefabProcessor>() |
| 24 | + ->Version(1) |
| 25 | + ->Field("PlatformExcludedComponents", &AssetPlatformComponentRemover::m_platformExcludedComponents) |
| 26 | + ; |
| 27 | + } |
| 28 | + } |
| 29 | + |
| 30 | + void AssetPlatformComponentRemover::Process(PrefabProcessorContext& prefabProcessorContext) |
| 31 | + { |
| 32 | + AZ::PlatformTagSet platformTags = prefabProcessorContext.GetPlatformTags(); |
| 33 | + AZStd::set<AZ::Uuid> excludedComponents; |
| 34 | + for (const auto& platforms : m_platformExcludedComponents) |
| 35 | + { |
| 36 | + if (platformTags.contains(AZ::Crc32(platforms.first))) |
| 37 | + { |
| 38 | + excludedComponents.insert_range(platforms.second); |
| 39 | + } |
| 40 | + } |
| 41 | + |
| 42 | + if (excludedComponents.empty()) |
| 43 | + { |
| 44 | + // No need to remove any components. |
| 45 | + return; |
| 46 | + } |
| 47 | + |
| 48 | + // Iterate over every entity, in every prefab |
| 49 | + prefabProcessorContext.ListPrefabs( |
| 50 | + [&prefabProcessorContext, &excludedComponents](PrefabDocument& prefab) -> void |
| 51 | + { |
| 52 | + prefab.GetInstance().GetAllEntitiesInHierarchy( |
| 53 | + [&prefab, &prefabProcessorContext, &excludedComponents](AZStd::unique_ptr<AZ::Entity>& entity) -> bool |
| 54 | + { |
| 55 | + // Loop over an entity's components backwards and pop-off components that shouldn't exist. |
| 56 | + AZStd::vector<AZ::Component*> components = entity->GetComponents(); |
| 57 | + const int oldComponentCount = components.size(); |
| 58 | + for (int i = oldComponentCount - 1; i >= 0; --i) |
| 59 | + { |
| 60 | + AZ::Component* component = components[i]; |
| 61 | + if (excludedComponents.contains(component->GetUnderlyingComponentType())) |
| 62 | + { |
| 63 | + entity->RemoveComponent(component); |
| 64 | + delete component; |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + // Make sure we didn't remove any components that another component dependends on |
| 69 | + if (oldComponentCount != entity->GetComponents().size()) |
| 70 | + { |
| 71 | + if (entity->EvaluateDependencies() == AZ::Entity::DependencySortResult::MissingRequiredService) |
| 72 | + { |
| 73 | + AZ_Error( "AssetPlatformComponentRemover", false, |
| 74 | + "Processing prefab '%s' failed! Removing components on entity '%s' has broken component " |
| 75 | + "dependency. Make sure you also remove any dependent components. If dependent component is actually required, " |
| 76 | + "then keep the provider. Please update Amazon/Tools/Prefab/Processing/PlatformExcludedComponents settings registry (.setreg).", |
| 77 | + prefab.GetName().c_str(), |
| 78 | + entity->GetName().c_str() |
| 79 | + ); |
| 80 | + |
| 81 | + prefabProcessorContext.ErrorEncountered(); |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + // continue iterating over entities... |
| 86 | + return true; |
| 87 | + }); |
| 88 | + }); |
| 89 | + } |
| 90 | +} // namespace AzToolsFramework::Prefab::PrefabConversionUtils |
0 commit comments