2020-07-17 16:43:42 -04:00
|
|
|
// Copyright Epic Games, Inc. All Rights Reserved.
|
|
|
|
|
|
|
|
|
|
#include "MetasoundAssetBase.h"
|
|
|
|
|
|
2020-07-23 20:32:26 -04:00
|
|
|
#include "MetasoundJsonBackend.h"
|
2020-07-21 14:34:07 -04:00
|
|
|
#include "HAL/FileManager.h"
|
2020-07-17 16:43:42 -04:00
|
|
|
#include "IStructSerializerBackend.h"
|
|
|
|
|
#include "StructSerializer.h"
|
|
|
|
|
|
|
|
|
|
|
2020-07-23 16:39:56 -04:00
|
|
|
const FString FMetasoundAssetBase::FileExtension(TEXT(".metasound"));
|
|
|
|
|
|
|
|
|
|
FMetasoundAssetBase::FMetasoundAssetBase()
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
FMetasoundAssetBase::FMetasoundAssetBase(FMetasoundDocument& InDocument)
|
|
|
|
|
: AccessPoint(MakeShared<Metasound::Frontend::FDescriptionAccessPoint>(InDocument))
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2020-07-17 16:43:42 -04:00
|
|
|
void FMetasoundAssetBase::SetMetadata(FMetasoundClassMetadata& InMetadata)
|
|
|
|
|
{
|
|
|
|
|
InMetadata.NodeType = EMetasoundClassType::MetasoundGraph;
|
|
|
|
|
GetDocument().RootClass.Metadata = InMetadata;
|
|
|
|
|
}
|
|
|
|
|
|
2020-07-23 20:32:26 -04:00
|
|
|
void FMetasoundAssetBase::SetDocument(const FMetasoundDocument& InDocument)
|
|
|
|
|
{
|
|
|
|
|
FMetasoundDocument& Document = GetDocument();
|
|
|
|
|
Document = InDocument;
|
|
|
|
|
|
|
|
|
|
Document.Archetype = GetArchetype();
|
|
|
|
|
GetRootGraphHandle().FixDocumentToMatchArchetype();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void FMetasoundAssetBase::ConformDocumentToArchetype()
|
|
|
|
|
{
|
|
|
|
|
FMetasoundDocument& Document = GetDocument();
|
|
|
|
|
Document.Archetype = GetArchetype();
|
|
|
|
|
GetRootGraphHandle().FixDocumentToMatchArchetype();
|
|
|
|
|
}
|
|
|
|
|
|
2020-07-17 16:43:42 -04:00
|
|
|
FMetasoundClassMetadata FMetasoundAssetBase::GetMetadata()
|
|
|
|
|
{
|
|
|
|
|
return GetDocument().RootClass.Metadata;
|
|
|
|
|
}
|
|
|
|
|
|
2020-07-23 16:39:56 -04:00
|
|
|
TWeakPtr<Metasound::Frontend::FDescriptionAccessPoint> FMetasoundAssetBase::GetGraphAccessPoint() const
|
2020-07-17 16:43:42 -04:00
|
|
|
{
|
|
|
|
|
return TWeakPtr<Metasound::Frontend::FDescriptionAccessPoint>(AccessPoint);
|
|
|
|
|
}
|
|
|
|
|
|
2020-07-23 16:39:56 -04:00
|
|
|
Metasound::Frontend::FGraphHandle FMetasoundAssetBase::GetRootGraphHandle() const
|
2020-07-17 16:43:42 -04:00
|
|
|
{
|
|
|
|
|
using FDescPath = Metasound::Frontend::FDescPath;
|
|
|
|
|
using FGraphHandle = Metasound::Frontend::FGraphHandle;
|
|
|
|
|
using FHandleInitParams = Metasound::Frontend::FHandleInitParams;
|
|
|
|
|
using EFromDocument = Metasound::Frontend::Path::EFromDocument;
|
|
|
|
|
using EFromClass = Metasound::Frontend::Path::EFromClass;
|
|
|
|
|
|
|
|
|
|
FDescPath PathToGraph = FDescPath()[EFromDocument::ToRootClass][EFromClass::ToGraph];
|
|
|
|
|
FHandleInitParams InitParams = { GetGraphAccessPoint(), PathToGraph, GetDocument().RootClass.Metadata.NodeName, MakeWeakObjectPtr(GetOwningAsset()) };
|
|
|
|
|
return FGraphHandle(GetPrivateToken(), InitParams);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TArray<Metasound::Frontend::FGraphHandle> FMetasoundAssetBase::GetAllSubgraphHandles()
|
|
|
|
|
{
|
|
|
|
|
using FDescPath = Metasound::Frontend::FDescPath;
|
|
|
|
|
using FGraphHandle = Metasound::Frontend::FGraphHandle;
|
|
|
|
|
using FHandleInitParams = Metasound::Frontend::FHandleInitParams;
|
|
|
|
|
using EFromDocument = Metasound::Frontend::Path::EFromDocument;
|
|
|
|
|
using EFromClass = Metasound::Frontend::Path::EFromClass;
|
|
|
|
|
|
|
|
|
|
TArray<FGraphHandle> OutArray;
|
|
|
|
|
|
|
|
|
|
FDescPath RootPathForDependencyGraphs = FDescPath()[EFromDocument::ToDependencies];
|
|
|
|
|
|
|
|
|
|
TArray<FMetasoundClassDescription>& DependenciesList = GetDocument().Dependencies;
|
|
|
|
|
for (FMetasoundClassDescription& Dependency : DependenciesList)
|
|
|
|
|
{
|
|
|
|
|
const bool bIsSubgraph = Dependency.Metadata.NodeType == EMetasoundClassType::MetasoundGraph && Dependency.Graph.Nodes.Num() > 0;
|
|
|
|
|
if (bIsSubgraph)
|
|
|
|
|
{
|
|
|
|
|
FDescPath SubgraphPath = RootPathForDependencyGraphs[Dependency.UniqueID][EFromClass::ToGraph];
|
|
|
|
|
FHandleInitParams InitParams = { GetGraphAccessPoint(), SubgraphPath, Dependency.Metadata.NodeName, MakeWeakObjectPtr(GetOwningAsset()) };
|
|
|
|
|
OutArray.Emplace(GetPrivateToken(), InitParams);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return OutArray;
|
|
|
|
|
}
|
|
|
|
|
|
2020-07-23 16:39:56 -04:00
|
|
|
bool FMetasoundAssetBase::ImportFromJSON(const FString& InAbsolutePath)
|
|
|
|
|
{
|
|
|
|
|
return Metasound::Frontend::ImportJSONToMetasound(InAbsolutePath, GetDocument());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool FMetasoundAssetBase::ExportToJSON(const FString& InAbsolutePath) const
|
2020-07-17 16:43:42 -04:00
|
|
|
{
|
|
|
|
|
if (TUniquePtr<FArchive> FileWriter = TUniquePtr<FArchive>(IFileManager::Get().CreateFileWriter(*InAbsolutePath)))
|
|
|
|
|
{
|
2020-07-23 20:32:26 -04:00
|
|
|
Metasound::TJsonStructSerializerBackend<Metasound::DefaultCharType> Backend(*FileWriter, EStructSerializerBackendFlags::Default);
|
2020-07-17 16:43:42 -04:00
|
|
|
FStructSerializer::Serialize<FMetasoundDocument>(GetDocument(), Backend);
|
|
|
|
|
FileWriter->Close();
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ensureAlwaysMsgf(false, TEXT("Failed to create a filewriter with the given path."));
|
|
|
|
|
return false;
|
|
|
|
|
}
|