Files
UnrealEngineUWP/Engine/Plugins/Runtime/Metasound/Source/MetasoundEngine/Private/MetasoundUObjectRegistry.cpp
rob gay ff8b5954ac MetaSounds Interface Checkpoint
- Archetype to Interface rename & support for multiple interface versions stored on MetaSoundBase
- Added ability to register interfaces
- Added spatialization/attenuation interfaces
- Added UX to add/remove Interfaces
- Fix ensure when deleting UMetaSound asset
#rb phil.popp
#jira UE-135000
#jira UE-120656
#rnx
#preflight 619bd9e33a7219926732337c

#ROBOMERGE-AUTHOR: rob.gay
#ROBOMERGE-SOURCE: CL 18262648 in //UE5/Release-5.0/... via CL 18262703
#ROBOMERGE-BOT: STARSHIP (Release-Engine-Staging -> Release-Engine-Test) (v895-18170469)

[CL 18262725 by rob gay in ue5-release-engine-test branch]
2021-11-22 15:55:50 -05:00

216 lines
6.6 KiB
C++

// Copyright Epic Games, Inc. All Rights Reserved.
#include "MetasoundUObjectRegistry.h"
#include "Algo/Copy.h"
#include "AssetData.h"
#include "AssetRegistry/AssetRegistryModule.h"
#include "Async/Async.h"
#include "Async/TaskGraphInterfaces.h"
#include "CoreMinimal.h"
#include "Engine/AssetManager.h"
#include "Metasound.h"
#include "MetasoundAssetBase.h"
#include "MetasoundFrontendArchetypeRegistry.h"
#include "MetasoundFrontendRegistries.h"
#include "MetasoundFrontendTransform.h"
#include "MetasoundSettings.h"
#include "MetasoundSource.h"
#include "MetasoundTrace.h"
#include "UObject/Object.h"
namespace Metasound
{
class FMetasoundUObjectRegistry : public IMetasoundUObjectRegistry
{
public:
FMetasoundUObjectRegistry()
{
// Set default version to use base UMetaSound class implementation
FMetasoundFrontendVersion DefaultVersion;
using FRegistryEntryType = TMetasoundUObjectRegistryEntry<UMetaSound>;
RegisterUClassInterface(MakeUnique<FRegistryEntryType>(DefaultVersion));
}
void RegisterUClassInterface(TUniquePtr<IMetasoundUObjectRegistryEntry>&& InEntry) override
{
if (InEntry.IsValid())
{
Frontend::FInterfaceRegistryKey Key = Frontend::GetInterfaceRegistryKey(InEntry->GetInterfaceVersion());
EntriesByInterface.Add(Key, InEntry.Get());
EntriesByName.Add(InEntry->GetInterfaceVersion().Name, InEntry.Get());
Entries.Add(InEntry.Get());
Storage.Add(MoveTemp(InEntry));
}
}
TArray<const IMetasoundUObjectRegistryEntry*> FindInterfaceEntriesByName(FName InName) const override
{
TArray<const IMetasoundUObjectRegistryEntry*> EntriesWithName;
EntriesByName.MultiFind(InName, EntriesWithName);
return EntriesWithName;
}
TArray<UClass*> FindSupportedInterfaceClasses(const FMetasoundFrontendVersion& InInterfaceVersion) const override
{
TArray<UClass*> Classes;
TArray<const IMetasoundUObjectRegistryEntry*> EntriesForInterface;
EntriesByInterface.MultiFind(Frontend::GetInterfaceRegistryKey(InInterfaceVersion), EntriesForInterface);
for (const IMetasoundUObjectRegistryEntry* Entry : EntriesForInterface)
{
if (nullptr != Entry)
{
if (UClass* Class = Entry->GetUClass())
{
Classes.Add(Class);
}
}
}
return Classes;
}
UObject* NewObject(UClass* InClass, const FMetasoundFrontendDocument& InDocument, const FString& InPath) const override
{
TArray<const IMetasoundUObjectRegistryEntry*> AllInterfaceEntries;
for (const FMetasoundFrontendVersion& InterfaceVersion : InDocument.InterfaceVersions)
{
TArray<const IMetasoundUObjectRegistryEntry*> EntriesForInterface;
EntriesByInterface.MultiFind(Frontend::GetInterfaceRegistryKey(InterfaceVersion), EntriesForInterface);
AllInterfaceEntries.Append(MoveTemp(EntriesForInterface));
}
auto IsChildClassOfRegisteredClass = [&](const IMetasoundUObjectRegistryEntry* Entry)
{
return Entry->IsChildClass(InClass);
};
if (const IMetasoundUObjectRegistryEntry* const* EntryForClass = AllInterfaceEntries.FindByPredicate(IsChildClassOfRegisteredClass))
{
return NewObject(**EntryForClass, InDocument, InPath);
}
return nullptr;
}
bool IsRegisteredClass(UObject* InObject) const override
{
return (nullptr != GetEntryByUObject(InObject));
}
FMetasoundAssetBase* GetObjectAsAssetBase(UObject* InObject) const override
{
if (const IMetasoundUObjectRegistryEntry* Entry = GetEntryByUObject(InObject))
{
return Entry->Cast(InObject);
}
return nullptr;
}
const FMetasoundAssetBase* GetObjectAsAssetBase(const UObject* InObject) const override
{
if (const IMetasoundUObjectRegistryEntry* Entry = GetEntryByUObject(InObject))
{
return Entry->Cast(InObject);
}
return nullptr;
}
private:
UObject* NewObject(const IMetasoundUObjectRegistryEntry& InEntry, const FMetasoundFrontendDocument& InDocument, const FString& InPath) const
{
UPackage* PackageToSaveTo = nullptr;
if (GIsEditor)
{
FText InvalidPathReason;
bool const bValidPackageName = FPackageName::IsValidLongPackageName(InPath, false, &InvalidPathReason);
if (!ensureAlwaysMsgf(bValidPackageName, TEXT("Tried to generate a Metasound UObject with an invalid package path/name Falling back to transient package, which means we won't be able to save this asset.")))
{
PackageToSaveTo = GetTransientPackage();
}
else
{
PackageToSaveTo = CreatePackage(*InPath);
}
}
else
{
PackageToSaveTo = GetTransientPackage();
}
UObject* NewMetasoundObject = InEntry.NewObject(PackageToSaveTo, *InDocument.RootGraph.Metadata.GetClassName().GetFullName().ToString());
FMetasoundAssetBase* NewAssetBase = InEntry.Cast(NewMetasoundObject);
if (ensure(nullptr != NewAssetBase))
{
NewAssetBase->SetDocument(InDocument);
}
#if WITH_EDITOR
AsyncTask(ENamedThreads::GameThread, [NewMetasoundObject]()
{
FAssetRegistryModule::AssetCreated(NewMetasoundObject);
NewMetasoundObject->MarkPackageDirty();
// todo: how do you get the package for a uobject and save it? I forget
});
#endif
return NewMetasoundObject;
}
const IMetasoundUObjectRegistryEntry* FindEntryByPredicate(TFunction<bool (const IMetasoundUObjectRegistryEntry*)> InPredicate) const
{
const IMetasoundUObjectRegistryEntry* const* Entry = Entries.FindByPredicate(InPredicate);
if (nullptr == Entry)
{
return nullptr;
}
return *Entry;
}
TArray<const IMetasoundUObjectRegistryEntry*> FindEntriesByPredicate(TFunction<bool (const IMetasoundUObjectRegistryEntry*)> InPredicate) const
{
TArray<const IMetasoundUObjectRegistryEntry*> FoundEntries;
Algo::CopyIf(Entries, FoundEntries, InPredicate);
return FoundEntries;
}
const IMetasoundUObjectRegistryEntry* GetEntryByUObject(const UObject* InObject) const
{
auto IsChildClassOfRegisteredClass = [&](const IMetasoundUObjectRegistryEntry* Entry)
{
if (nullptr == Entry)
{
return false;
}
return Entry->IsChildClass(InObject);
};
return FindEntryByPredicate(IsChildClassOfRegisteredClass);
}
TArray<TUniquePtr<IMetasoundUObjectRegistryEntry>> Storage;
TMultiMap<Frontend::FInterfaceRegistryKey, const IMetasoundUObjectRegistryEntry*> EntriesByInterface;
TMultiMap<FName, const IMetasoundUObjectRegistryEntry*> EntriesByName;
TArray<const IMetasoundUObjectRegistryEntry*> Entries;
};
IMetasoundUObjectRegistry& IMetasoundUObjectRegistry::Get()
{
static FMetasoundUObjectRegistry Registry;
return Registry;
}
}