// Copyright Epic Games, Inc. All Rights Reserved. #include "MetasoundFrontendQuerySteps.h" #include "Algo/MaxElement.h" #include "MetasoundFrontend.h" #include "MetasoundFrontendDocument.h" #include "MetasoundFrontendNodeClassRegistry.h" #include "MetasoundFrontendNodeClassRegistryPrivate.h" #include "MetasoundFrontendQuery.h" #include "MetasoundFrontendRegistryTransaction.h" #include "MetasoundLog.h" namespace Metasound { class FNodeClassRegistrationEventsPimpl : public IFrontendQueryStreamStep { public: FNodeClassRegistrationEventsPimpl() { TransactionStream = Frontend::FNodeClassRegistry::Get().CreateTransactionStream(); } virtual void Stream(TArray& OutValues) override { using namespace Frontend; auto AddEntry = [&OutValues](const FNodeRegistryTransaction& InTransaction) { OutValues.Emplace(TInPlaceType(), InTransaction); }; if (TransactionStream.IsValid()) { TransactionStream->Stream(AddEntry); } } private: TUniquePtr TransactionStream; }; FNodeClassRegistrationEvents::FNodeClassRegistrationEvents() : Pimpl(MakePimpl()) { } void FNodeClassRegistrationEvents::Stream(TArray& OutValues) { Pimpl->Stream(OutValues); } FFrontendQueryKey FMapRegistrationEventsToNodeRegistryKeys::Map(const FFrontendQueryEntry& InEntry) const { using namespace Frontend; FNodeRegistryKey RegistryKey; if (ensure(InEntry.Value.IsType())) { RegistryKey = InEntry.Value.Get().GetNodeRegistryKey(); } return FFrontendQueryKey(RegistryKey.ToString()); } void FReduceRegistrationEventsToCurrentStatus::Reduce(const FFrontendQueryKey& InKey, FFrontendQueryPartition& InOutEntries) const { using namespace Frontend; InOutEntries.Sort([](const FFrontendQueryEntry& A, const FFrontendQueryEntry& B) { return GetTransactionTimestamp(A) < GetTransactionTimestamp(B); }); // Registration - Unregistration pairs result in no net change, // so reduce to most recent transaction that is not a pair int32 MostRecentNonPairedIndex = INDEX_NONE; int32 Index = 0; while (Index < InOutEntries.Num()) { // If last entry, cannot be a pair if (Index == InOutEntries.Num() - 1) { MostRecentNonPairedIndex = Index; break; } // Skip if valid pair if (InOutEntries[Index].Value.Get().GetTransactionType() == FNodeRegistryTransaction::ETransactionType::NodeRegistration && InOutEntries[Index + 1].Value.Get().GetTransactionType() == FNodeRegistryTransaction::ETransactionType::NodeUnregistration) { Index += 2; } // If not valid pair, this is the current most recent non paired index else { MostRecentNonPairedIndex = Index; Index++; } } // Empty or entries were all pairs if (MostRecentNonPairedIndex == INDEX_NONE) { InOutEntries.Reset(); } else { const FFrontendQueryEntry Entry = InOutEntries[MostRecentNonPairedIndex]; InOutEntries.Reset(); InOutEntries.Add(Entry); } } FReduceRegistrationEventsToCurrentStatus::FTimeType FReduceRegistrationEventsToCurrentStatus::GetTransactionTimestamp(const FFrontendQueryEntry& InEntry) { using namespace Frontend; if (ensure(InEntry.Value.IsType())) { return InEntry.Value.Get().GetTimestamp(); } return 0; } bool FReduceRegistrationEventsToCurrentStatus::IsValidTransactionOfType(Frontend::FNodeRegistryTransaction::ETransactionType InType, const FFrontendQueryEntry* InEntry) { using namespace Frontend; if (nullptr != InEntry) { if (InEntry->Value.IsType()) { return InEntry->Value.Get().GetTransactionType() == InType; } } return false; } void FTransformRegistrationEventsToClasses::Transform(FFrontendQueryEntry::FValue& InValue) const { using namespace Frontend; FMetasoundFrontendClass FrontendClass; if (ensure(InValue.IsType())) { const FNodeRegistryTransaction& Transaction = InValue.Get(); if (Transaction.GetTransactionType() == Frontend::FNodeRegistryTransaction::ETransactionType::NodeRegistration) { // It's possible that the node is no longer registered (we're processing removals) // but that's okay because the returned default FrontendClass will be processed out later FMetasoundFrontendRegistryContainer::Get()->FindFrontendClassFromRegistered(Transaction.GetNodeRegistryKey(), FrontendClass); } } InValue.Set(MoveTemp(FrontendClass)); } FFilterClassesByInputVertexDataType::FFilterClassesByInputVertexDataType(const FName& InTypeName) : InputVertexTypeName(InTypeName) { } bool FFilterClassesByInputVertexDataType::Filter(const FFrontendQueryEntry& InEntry) const { check(InEntry.Value.IsType()); return InEntry.Value.Get().GetDefaultInterface().Inputs.ContainsByPredicate( [this](const FMetasoundFrontendClassInput& InDesc) { return InDesc.TypeName == InputVertexTypeName; } ); } FFilterClassesByOutputVertexDataType::FFilterClassesByOutputVertexDataType(const FName& InTypeName) : OutputVertexTypeName(InTypeName) { } bool FFilterClassesByOutputVertexDataType::Filter(const FFrontendQueryEntry& InEntry) const { return InEntry.Value.Get().GetDefaultInterface().Outputs.ContainsByPredicate( [this](const FMetasoundFrontendClassOutput& InDesc) { return InDesc.TypeName == OutputVertexTypeName; } ); } FFrontendQueryKey FMapClassesToClassName::Map(const FFrontendQueryEntry& InEntry) const { return FFrontendQueryKey(InEntry.Value.Get().Metadata.GetClassName().GetFullName()); } FFilterClassesByClassID::FFilterClassesByClassID(const FGuid InClassID) : ClassID(InClassID) { } bool FFilterClassesByClassID::Filter(const FFrontendQueryEntry& InEntry) const { return InEntry.Value.Get().ID == ClassID; } FFrontendQueryKey FMapToFullClassName::Map(const FFrontendQueryEntry& InEntry) const { const FMetasoundFrontendClass& FrontendClass = InEntry.Value.Get(); return FFrontendQueryKey(FrontendClass.Metadata.GetClassName().GetFullName()); } void FReduceClassesToHighestVersion::Reduce(const FFrontendQueryKey& InKey, FFrontendQueryPartition& InOutEntries) const { FFrontendQueryEntry* HighestVersionEntry = nullptr; FMetasoundFrontendVersionNumber HighestVersion; for (FFrontendQueryEntry& Entry : InOutEntries) { const FMetasoundFrontendVersionNumber& Version = Entry.Value.Get().Metadata.GetVersion(); if (!HighestVersionEntry || HighestVersion < Version) { HighestVersionEntry = &Entry; HighestVersion = Version; } } if (HighestVersionEntry) { FFrontendQueryEntry Entry = *HighestVersionEntry; InOutEntries.Reset(); InOutEntries.Add(Entry); } } bool FSortClassesByVersion::Sort(const FFrontendQueryEntry& InEntryLHS, const FFrontendQueryEntry& InEntryRHS) const { const FMetasoundFrontendVersionNumber& VersionLHS = InEntryLHS.Value.Get().Metadata.GetVersion(); const FMetasoundFrontendVersionNumber& VersionRHS = InEntryRHS.Value.Get().Metadata.GetVersion(); return VersionLHS > VersionRHS; } }