Files
UnrealEngineUWP/Engine/Plugins/Runtime/Metasound/Source/MetasoundFrontend/Public/MetasoundFrontendDocumentIdGenerator.h
helen yang 3ddcc821aa Enable MetaSound deterministic id generation at runtime
#rb phil.popp
#jira UE-189686
#rnx

[CL 26547630 by helen yang in ue5-main branch]
2023-07-24 12:45:51 -04:00

94 lines
2.7 KiB
C++

// Copyright Epic Games, Inc. All Rights Reserved.
#pragma once
#include "MetasoundFrontendDocument.h"
#include "HAL/IConsoleManager.h"
#include "Misc/Guid.h"
namespace Metasound
{
namespace Frontend
{
extern METASOUNDFRONTEND_API int32 MetaSoundEnableDeterministicIDGenerationInEditorCVar;
extern METASOUNDFRONTEND_API int32 MetaSoundEnableRuntimeDeterministicIDGeneration;
/***
* For generating IDs using a given document.
* USAGE:
*
* If you want everything within the calling scope to be deterministic, use
* the scope determinism lock like you would a `FScopeLock`
*
* {
* constexpr bool bIsDeterministic = true;
* FDocumentIDGenerator::FScopeDeterminism DeterminismScope(bIsDeterministic);
*
* // Anything called in this scope will use a deterministic ID generator.
* // Once the `DeterminismScope` variable is destroyed, it will return to
* // whatever the prior behavior was
* MetaSoundAsset->UpdateOrWhatever();
* }
*
* void FMetaSoundAsset::UpdateOrWhatever()
* {
* // Anytime you call IDGen::Create*ID(..), it will obey whatever the outside scope set it to be.
* AddNewNodeOrWhatever(...)
* }
*
* void FMetaSoundAsset::AddNodeOrWhatever(...)
* {
* FDocumentIDGenerator& IDGen = FDocumentIDGenerator::Get();
*
* FGuid NewNodeID = IDGen::CreateNewNodeID();
* ...
* }
*
*/
class FDocumentIDGenerator
{
public:
class METASOUNDFRONTEND_API FScopeDeterminism final
{
public:
FScopeDeterminism(bool bInIsDeterministic);
bool GetDeterminism() const;
~FScopeDeterminism();
private:
bool bOriginalValue = false;
};
static FDocumentIDGenerator& Get();
FGuid CreateNodeID(const FMetasoundFrontendDocument& InDocument) const;
FGuid CreateVertexID(const FMetasoundFrontendDocument& InDocument) const;
FGuid CreateClassID(const FMetasoundFrontendDocument& InDocument) const;
FGuid CreateIDFromDocument(const FMetasoundFrontendDocument& InDocument) const;
private:
FDocumentIDGenerator() = default;
friend class FScopeDeterminism;
void SetDeterminism(bool bInIsDeterministic);
bool GetDeterminism() const;
bool bIsDeterministic = false;
};
// For generating IDs that are not derived from a given document.
class FClassIDGenerator
{
public:
static FClassIDGenerator& Get();
FGuid CreateInputID(const FMetasoundFrontendClassInput& Input) const;
FGuid CreateInputID(const Audio::FParameterInterface::FInput& Input) const;
FGuid CreateOutputID(const FMetasoundFrontendClassOutput& Output) const;
FGuid CreateOutputID(const Audio::FParameterInterface::FOutput& Output) const;
FGuid CreateNamespacedIDFromString(const FGuid NamespaceGuid, FString StringToHash) const;
};
}
}