Files
UnrealEngineUWP/Engine/Plugins/Runtime/Metasound/Source/MetasoundFrontend/Public/MetasoundFrontendDocumentBuilder.h
helen yang cc09fda972 Expand and consolidate MetaSound builder preset asset functionality
- Added document builder and BP functions for SetGraphInputDefault, ConvertFromPreset, ConvertToPreset, CreatePatch/SourcePresetBuilder, IsPreset
- Refactor MetaSound asset and preset initialization to use builder architecture more

#jira UE-185391
#rb rob.gay
#preflight 64710f423b73ebeef4c295bb

[CL 25683338 by helen yang in ue5-main branch]
2023-05-30 14:39:48 -04:00

246 lines
11 KiB
C++

// Copyright Epic Games, Inc. All Rights Reserved.
#pragma once
#include "Containers/Map.h"
#include "Interfaces/MetasoundFrontendInterfaceRegistry.h"
#include "MetasoundFrontendDocumentCacheInterface.h"
#include "MetasoundDocumentInterface.h"
#include "MetasoundFrontendDocument.h"
#include "MetasoundFrontendRegistries.h"
#include "MetasoundFrontendTransform.h"
#include "MetasoundVertex.h"
#include "Templates/Function.h"
#include "UObject/ScriptInterface.h"
#include "MetasoundFrontendDocumentBuilder.generated.h"
// Forward Declarations
class FMetasoundAssetBase;
namespace Metasound::Frontend
{
struct METASOUNDFRONTEND_API FNamedEdge
{
const FGuid OutputNodeID;
const FName OutputName;
const FGuid InputNodeID;
const FName InputName;
friend bool operator==(const FNamedEdge& InLHS, const FNamedEdge& InRHS)
{
return InLHS.OutputNodeID == InRHS.OutputNodeID
&& InLHS.OutputName == InRHS.OutputName
&& InLHS.InputNodeID == InRHS.InputNodeID
&& InLHS.InputName == InRHS.InputName;
}
friend bool operator!=(const FNamedEdge& InLHS, const FNamedEdge& InRHS)
{
return !(InLHS == InRHS);
}
friend FORCEINLINE uint32 GetTypeHash(const FNamedEdge& InBinding)
{
const int32 NameHash = HashCombineFast(GetTypeHash(InBinding.OutputName), GetTypeHash(InBinding.InputName));
const int32 GuidHash = HashCombineFast(GetTypeHash(InBinding.OutputNodeID), GetTypeHash(InBinding.InputNodeID));
return HashCombineFast(NameHash, GuidHash);
}
};
struct METASOUNDFRONTEND_API FModifyInterfaceOptions
{
FModifyInterfaceOptions(const TArray<FMetasoundFrontendInterface>& InInterfacesToRemove, const TArray<FMetasoundFrontendInterface>& InInterfacesToAdd);
FModifyInterfaceOptions(const TArray<FMetasoundFrontendVersion>& InInterfaceVersionsToRemove, const TArray<FMetasoundFrontendVersion>& InInterfaceVersionsToAdd);
TArray<FMetasoundFrontendInterface> InterfacesToRemove;
TArray<FMetasoundFrontendInterface> InterfacesToAdd;
// Function used to determine if an old of a removed interface
// and new member of an added interface are considered equal and
// to be swapped, retaining preexisting connections (and locations
// if in editor and 'SetDefaultNodeLocations' option is set)
TFunction<bool(FName, FName)> NamePairingFunction;
#if WITH_EDITORONLY_DATA
bool bSetDefaultNodeLocations = true;
#endif // WITH_EDITORONLY_DATA
};
} // namespace Metasound::Frontend
UCLASS()
class METASOUNDFRONTEND_API UMetaSoundBuilderDocument : public UObject, public IMetaSoundDocumentInterface
{
GENERATED_BODY()
public:
// Returns the document
virtual const FMetasoundFrontendDocument& GetDocument() const override
{
return Document;
}
// Base MetaSoundClass that document is published to.
virtual const UClass& GetBaseMetaSoundUClass() const final override
{
checkf(MetaSoundUClass, TEXT("BaseMetaSoundUClass must be set upon creation of UMetaSoundBuilderDocument instance"));
return *MetaSoundUClass;
}
private:
// Sets the base MetaSound UClass (to be called immediately following UObject construction)
void SetBaseMetaSoundUClass(const UClass& InMetaSoundClass)
{
MetaSoundUClass = &InMetaSoundClass;
}
virtual FMetasoundFrontendDocument& GetDocument() override
{
return Document;
}
UPROPERTY(Transient)
FMetasoundFrontendDocument Document;
UPROPERTY(Transient)
TObjectPtr<const UClass> MetaSoundUClass = nullptr;
friend struct FMetaSoundFrontendDocumentBuilder;
friend class UMetaSoundBuilderBase;
};
// Builder used to support dynamically generating MetaSound documents at runtime. Builder contains caches that speed up
// common search and modification operations on a given document, which may result in slower performance on construction,
// but faster manipulation of its managed document. The builder's managed copy of a document is expected to not be modified
// by any external system to avoid cache to not become stale.
USTRUCT()
struct METASOUNDFRONTEND_API FMetaSoundFrontendDocumentBuilder
{
GENERATED_BODY()
public:
// Exists only to make UObject reflection happy. Should never be
// used directly as builder class should be specified on construction.
FMetaSoundFrontendDocumentBuilder() = default;
FMetaSoundFrontendDocumentBuilder(FMetaSoundFrontendDocumentBuilder&& InBuilder);
FMetaSoundFrontendDocumentBuilder(TScriptInterface<IMetaSoundDocumentInterface> InDocumentInterface);
// Copying actively used builders is highly discouraged (as only one builder should be acting on a document at any given time
// to avoid internal cache state to become out-of-date) but must contractually be implemented in this case as a USTRUCT (builders
// are USTRUCTS to keep document dependencies from being GC'ed). To protect against this kind of "back door" building from another
// builder, copying builders also creates a new document & builder owner.
FMetaSoundFrontendDocumentBuilder(const FMetaSoundFrontendDocumentBuilder& InBuilder);
FMetaSoundFrontendDocumentBuilder& operator=(const FMetaSoundFrontendDocumentBuilder& InRHS);
FMetaSoundFrontendDocumentBuilder& operator=(FMetaSoundFrontendDocumentBuilder&& InRHS);
const FMetasoundFrontendClass* AddGraphDependency(const FMetasoundFrontendGraphClass& InClass);
const FMetasoundFrontendClass* AddNativeDependency(const FMetasoundFrontendClassMetadata& InClassMetadata);
const FMetasoundFrontendEdge* AddEdge(FMetasoundFrontendEdge&& InNewEdge);
bool AddNamedEdges(const TSet<Metasound::Frontend::FNamedEdge>& ConnectionsToMake, TArray<const FMetasoundFrontendEdge*>* OutNewEdges = nullptr);
bool AddEdgesByNodeClassInterfaceBindings(const FGuid& InFromNodeID, const FGuid& InToNodeID);
bool AddEdgesFromMatchingInterfaceNodeOutputsToGraphOutputs(const FGuid& InNodeID, TArray<const FMetasoundFrontendEdge*>& OutEdgesCreated);
bool AddEdgesFromMatchingInterfaceNodeInputsToGraphInputs(const FGuid& InNodeID, TArray<const FMetasoundFrontendEdge*>& OutEdgesCreated);
const FMetasoundFrontendNode* AddGraphInput(const FMetasoundFrontendClassInput& InClassInput);
const FMetasoundFrontendNode* AddGraphOutput(const FMetasoundFrontendClassOutput& InClassOutput);
bool AddInterface(FName InterfaceName);
const FMetasoundFrontendNode* AddGraphNode(const FMetasoundFrontendGraphClass& InGraphClass, FGuid InNodeID = FGuid::NewGuid());
bool CanAddEdge(const FMetasoundFrontendEdge& InEdge) const;
void ClearGraph();
bool ContainsEdge(const FMetasoundFrontendEdge& InEdge) const;
bool ContainsNode(const FGuid& InNodeID) const;
bool ConvertFromPreset();
bool ConvertToPreset(const FMetasoundFrontendDocument& InReferencedDocument);
static bool FindDeclaredInterfaces(const FMetasoundFrontendDocument& InDocument, TArray<const Metasound::Frontend::IInterfaceRegistryEntry*>& OutInterfaces);
bool FindDeclaredInterfaces(TArray<const Metasound::Frontend::IInterfaceRegistryEntry*>& OutInterfaces) const;
const FMetasoundFrontendClass* FindDependency(const FGuid& InClassID) const;
const FMetasoundFrontendClass* FindDependency(const FMetasoundFrontendClassMetadata& InMetadata) const;
bool FindInterfaceInputNodes(FName InterfaceName, TArray<const FMetasoundFrontendNode*>& OutInputs) const;
bool FindInterfaceOutputNodes(FName InterfaceName, TArray<const FMetasoundFrontendNode*>& OutOutputs) const;
const FMetasoundFrontendClassInput* FindGraphInput(FName InputName) const;
const FMetasoundFrontendNode* FindGraphInputNode(FName InputName) const;
const FMetasoundFrontendClassOutput* FindGraphOutput(FName OutputName) const;
const FMetasoundFrontendNode* FindGraphOutputNode(FName OutputName) const;
const FMetasoundFrontendVertex* FindNodeInput(const FGuid& InNodeID, const FGuid& InVertexID) const;
const FMetasoundFrontendVertex* FindNodeInput(const FGuid& InNodeID, FName InVertexName) const;
const FMetasoundFrontendVertex* FindNodeOutput(const FGuid& InNodeID, const FGuid& InVertexID) const;
const FMetasoundFrontendVertex* FindNodeOutput(const FGuid& InNodeID, FName InVertexName) const;
const FMetasoundFrontendNode* FindNode(const FGuid& InNodeID) const;
TArray<const FMetasoundFrontendVertex*> FindNodeInputs(const FGuid& InNodeID, FName TypeName = FName()) const;
TArray<const FMetasoundFrontendVertex*> FindNodeOutputs(const FGuid& InNodeID, FName TypeName = FName()) const;
const FMetasoundFrontendDocument& GetDocument() const;
const IMetaSoundDocumentInterface& GetDocumentInterface() const;
static void InitGraphClassMetadata(FMetasoundFrontendClassMetadata& InOutMetadata, bool bResetVersion = false);
void InitDocument();
void InitNodeLocations();
bool IsDependencyReferenced(const FGuid& InClassID) const;
bool IsNodeInputConnected(const FGuid& InNodeID, const FGuid& InVertexID) const;
bool IsNodeOutputConnected(const FGuid& InNodeID, const FGuid& InVertexID) const;
bool IsInterfaceDeclared(FName InInterfaceName) const;
bool IsInterfaceDeclared(const FMetasoundFrontendVersion& InInterfaceVersion) const;
bool IsPreset() const;
bool ModifyInterfaces(Metasound::Frontend::FModifyInterfaceOptions&& InOptions);
bool RemoveDependency(const FGuid& InClassID);
bool RemoveDependency(EMetasoundFrontendClassType ClassType, const FMetasoundFrontendClassName& InClassName, const FMetasoundFrontendVersionNumber& InClassVersionNumber);
bool RemoveEdge(const FMetasoundFrontendEdge& EdgeToRemove);
bool RemoveEdgesByNodeClassInterfaceBindings(const FGuid& InOutputNodeID, const FGuid& InInputNodeID);
bool RemoveEdgesFromNodeOutput(const FGuid& InNodeID, const FGuid& InVertexID);
bool RemoveEdgeToNodeInput(const FGuid& InNodeID, const FGuid& InVertexID);
bool RemoveGraphInput(FName InInputName);
bool RemoveGraphOutput(FName InOutputName);
bool RemoveInterface(FName InName);
bool RemoveNamedEdges(const TSet<Metasound::Frontend::FNamedEdge>& InNamedEdgesToRemove, TArray<FMetasoundFrontendEdge>* OutRemovedEdges = nullptr);
bool RemoveNode(const FGuid& InNodeID);
bool RemoveUnusedDependencies();
#if WITH_EDITOR
void SetAuthor(const FString& InAuthor);
#endif // WITH_EDITOR
bool SetGraphInputDefault(FName InputName, const FMetasoundFrontendLiteral& InDefaultLiteral);
bool SetNodeInputDefault(const FGuid& InNodeID, const FGuid& InVertexID, const FMetasoundFrontendLiteral& InLiteral);
bool SwapGraphInput(const FMetasoundFrontendClassVertex& InExistingInputVertex, const FMetasoundFrontendClassVertex& NewInputVertex);
bool SwapGraphOutput(const FMetasoundFrontendClassVertex& InExistingOutputVertex, const FMetasoundFrontendClassVertex& NewOutputVertex);
private:
using FFinalizeNodeFunctionRef = TFunctionRef<void(FMetasoundFrontendNode&, const Metasound::Frontend::FNodeRegistryKey&)>;
UPROPERTY(Transient)
TScriptInterface<IMetaSoundDocumentInterface> DocumentInterface;
TUniquePtr<Metasound::Frontend::IDocumentCache> DocumentCache;
const FTopLevelAssetPath GetBuilderClassPath() const;
FMetasoundFrontendDocument& GetDocument();
FMetasoundFrontendNode* AddNodeInternal(const FMetasoundFrontendClassMetadata& InClassMetadata, FFinalizeNodeFunctionRef FinalizeNode, FGuid InNodeID = FGuid::NewGuid());
const FMetasoundFrontendDocument* FindOrLoadNodeClassDocument(const FGuid& InNodeID) const;
const FMetasoundFrontendClassInput* FindNodeInputClassInput(const FGuid& InNodeID, const FGuid& InVertexID) const;
const FMetasoundFrontendClassOutput* FindNodeOutputClassOutput(const FGuid& InNodeID, const FGuid& InVertexID) const;
void ReloadCache();
bool SetInputInheritsDefault(FName InName, bool bInputInheritsDefault);
};