Added validation to net prefab processing to error if there are hierarchical entities without Net hierarchy components

Signed-off-by: Sergey Pereslavtsev <pereslav@amazon.com>
This commit is contained in:
Sergey Pereslavtsev
2021-10-26 16:10:35 +01:00
parent 9ce6f43a22
commit dc336ffaba
@@ -17,6 +17,8 @@
#include <AzToolsFramework/Prefab/Instance/Instance.h>
#include <AzToolsFramework/Prefab/PrefabDomUtils.h>
#include <Prefab/Spawnable/SpawnableUtils.h>
#include <Multiplayer/Components/NetworkHierarchyChildComponent.h>
#include <Multiplayer/Components/NetworkHierarchyRootComponent.h>
namespace Multiplayer
{
@@ -93,6 +95,41 @@ namespace Multiplayer
});
}
static bool HasNetHierarchyComponents(const AZ::Entity* entity)
{
return (entity->FindComponent<NetworkHierarchyRootComponent>() != nullptr)
|| (entity->FindComponent<NetworkHierarchyChildComponent>() != nullptr);
}
static void ValidateNetHierarchies(const AZStd::vector<AZ::Entity*>& prefabNetEntities)
{
AZStd::unordered_map<AZ::EntityId, AZ::Entity*> prefabEntitesMap;
for (auto* entity : prefabNetEntities)
{
auto* entityTransform = entity->FindComponent<AzFramework::TransformComponent>();
AZ_Assert(entityTransform, "Net entities have to have Transform Component");
AZ::EntityId parentId = entityTransform->GetParentId();
// The input entities array is sorted in the parent to children order,
// so we only need to check against already iterated entities
if (parentId.IsValid() && prefabEntitesMap.contains(parentId))
{
AZ::Entity* parentEntity = prefabEntitesMap[parentId];
bool parentValid = HasNetHierarchyComponents(parentEntity);
AZ_Error("NetworkPrefabProcessor", parentValid, "Parent Entity %s must have a Network Hierarchy component assigned",
parentEntity->GetName().c_str());
bool childValid = HasNetHierarchyComponents(entity);
AZ_Error("NetworkPrefabProcessor", childValid, "Child Entity %s must have a Network Hierarchy component assigned",
entity->GetName().c_str());
}
prefabEntitesMap[entity->GetId()] = entity;
}
}
void NetworkPrefabProcessor::ProcessPrefab(PrefabProcessorContext& context, AZStd::string_view prefabName, PrefabDom& prefab)
{
using namespace AzToolsFramework::Prefab;
@@ -131,6 +168,9 @@ namespace Multiplayer
// Sort the entities prior to processing. The entities will end up in the net spawnable in this order.
SpawnableUtils::SortEntitiesByTransformHierarchy(prefabNetEntities);
// Here we validate the hierarchical net entities have one of Network Hierarchy components.
ValidateNetHierarchies(prefabNetEntities);
// Create an asset for our future network spawnable: this allows us to put references to the asset in the components
AZ::Data::Asset<AzFramework::Spawnable> networkSpawnableAsset;
networkSpawnableAsset.Create(networkSpawnable->GetId());