Files
UnrealEngineUWP/Engine/Source/Editor/Kismet/Private/BlueprintNamespaceHelper.cpp
robert manuszewski d1443992e1 Deprecating ANY_PACKAGE.
This change consists of multiple changes:

Core:
- Deprecation of ANY_PACKAGE macro. Added ANY_PACKAGE_DEPRECATED macro which can still be used for backwards compatibility purposes (only used in CoreUObject)
- Deprecation of StaticFindObjectFast* functions that take bAnyPackage parameter
- Added UStruct::GetStructPathName function that returns FTopLevelAssetPath representing the path name (package + object FName, super quick compared to UObject::GetPathName) + wrapper UClass::GetClassPathName to make it look better when used with UClasses
- Added (Static)FindFirstObject* functions that find a first object given its Name (no Outer). These functions are used in places I consider valid to do global UObject (UClass) lookups like parsing command line parameters / checking for unique object names
- Added static UClass::TryFindType function which serves a similar purpose as FindFirstObject however it's going to throw a warning (with a callstack / maybe ensure in the future?) if short class name is provided. This function is used  in places that used to use short class names but now should have been converted to use path names to catch any potential regressions and or edge cases I missed.
- Added static UClass::TryConvertShortNameToPathName utility function
- Added static UClass::TryFixShortClassNameExportPath utility function
- Object text export paths will now also include class path (Texture2D'/Game/Textures/Grass.Grass' -> /Script/Engine.Texture2D'/Game/Textures/Grass.Grass')
- All places that manually generated object export paths for objects will now use FObjectPropertyBase::GetExportPath
- Added a new startup test that checks for short type names in UClass/FProperty MetaData values

AssetRegistry:
- Deprecated any member variables (FAssetData / FARFilter) or functions that use FNames to represent class names and replaced them with FTopLevelAssetPath
- Added new member variables and new function overloads that use FTopLevelAssetPath to represent class names
- This also applies to a few other modules' APIs to match AssetRegistry changes

Everything else:
- Updated code that used ANY_PACKAGE (depending on the use case) to use FindObject(nullptr, PathToObject), UClass::TryFindType (used when path name is expected, warns if it's a short name) or FindFirstObject (usually for finding types based on user input but there's been a few legitimate use cases not related to user input)
- Updated code that used AssetRegistry API to use FTopLevelAssetPaths and USomeClass::StaticClass()->GetClassPathName() instead of GetFName()
- Updated meta data and hardcoded FindObject(ANY_PACKAGE, "EEnumNameOrClassName") calls to use path names

#jira UE-99463
#rb many.people
[FYI] Marcus.Wassmer
#preflight 629248ec2256738f75de9b32

#codereviewnumbers 20320742, 20320791, 20320799, 20320756, 20320809, 20320830, 20320840, 20320846, 20320851, 20320863, 20320780, 20320765, 20320876, 20320786

#ROBOMERGE-OWNER: robert.manuszewski
#ROBOMERGE-AUTHOR: robert.manuszewski
#ROBOMERGE-SOURCE: CL 20430220 via CL 20433854 via CL 20435474 via CL 20435484
#ROBOMERGE-BOT: UE5 (Release-Engine-Staging -> Main) (v949-20362246)

[CL 20448496 by robert manuszewski in ue5-main branch]
2022-06-01 03:46:59 -04:00

277 lines
8.8 KiB
C++

// Copyright Epic Games, Inc. All Rights Reserved.
#include "BlueprintNamespaceHelper.h"
#include "Engine/Blueprint.h"
#include "BlueprintEditor.h"
#include "BlueprintEditorSettings.h"
#include "Toolkits/ToolkitManager.h"
#include "EdGraph/EdGraphPin.h"
#include "EdGraphSchema_K2.h"
#include "SPinTypeSelector.h"
#include "Widgets/Input/SCheckBox.h"
#include "Widgets/Text/STextBlock.h"
#include "ClassViewerFilter.h"
#include "BlueprintNamespacePathTree.h"
#include "BlueprintNamespaceUtilities.h"
#define LOCTEXT_NAMESPACE "BlueprintNamespaceHelper"
// ---
// @todo_namespaces - Remove CVar flags/sink below after converting to editable 'config' properties
// ---
static TAutoConsoleVariable<bool> CVarBPEnableNamespaceFilteringFeatures(
TEXT("BP.EnableNamespaceFilteringFeatures"),
false,
TEXT("Enables namespace filtering features in the Blueprint editor (experimental).")
);
static TAutoConsoleVariable<bool> CVarBPEnableNamespaceImportingFeatures(
TEXT("BP.EnableNamespaceImportingFeatures"),
false,
TEXT("Enables namespace importing features in the Blueprint editor (experimental)."));
static TAutoConsoleVariable<bool> CVarBPImportParentClassNamespaces(
TEXT("BP.ImportParentClassNamespaces"),
false,
TEXT("Enables import of parent class namespaces when opening a Blueprint for editing."));
// ---
class FClassViewerNamespaceFilter : public IClassViewerFilter
{
public:
FClassViewerNamespaceFilter(const FBlueprintNamespaceHelper* InNamespaceHelper)
: CachedNamespaceHelper(InNamespaceHelper)
{
}
virtual bool IsClassAllowed(const FClassViewerInitializationOptions& InInitOptions, const UClass* InClass, TSharedRef<FClassViewerFilterFuncs> InFilterFuncs) override
{
if (!CachedNamespaceHelper)
{
return true;
}
return CachedNamespaceHelper->IsImportedObject(InClass);
}
virtual bool IsUnloadedClassAllowed(const FClassViewerInitializationOptions& InInitOptions, const TSharedRef<const IUnloadedBlueprintData> InBlueprint, TSharedRef<FClassViewerFilterFuncs> InFilterFuncs) override
{
if (!CachedNamespaceHelper)
{
return true;
}
FSoftObjectPath ClassPath(InBlueprint->GetClassPathName().ToString());
return CachedNamespaceHelper->IsImportedObject(ClassPath);
}
private:
/** Associated namespace helper object. */
const FBlueprintNamespaceHelper* CachedNamespaceHelper;
};
// ---
class FPinTypeSelectorNamespaceFilter : public IPinTypeSelectorFilter
{
DECLARE_MULTICAST_DELEGATE(FOnFilterChanged);
public:
FPinTypeSelectorNamespaceFilter(const FBlueprintNamespaceHelper* InNamespaceHelper)
: CachedNamespaceHelper(InNamespaceHelper)
{
}
virtual bool ShouldShowPinTypeTreeItem(FPinTypeTreeItem InItem) const override
{
if (!CachedNamespaceHelper)
{
return true;
}
const bool bForceLoadSubCategoryObject = false;
const FEdGraphPinType& PinType = InItem->GetPinType(bForceLoadSubCategoryObject);
if (PinType.PinSubCategoryObject.IsValid() && !CachedNamespaceHelper->IsImportedObject(PinType.PinSubCategoryObject.Get()))
{
// A pin type whose underlying object is loaded, but not imported.
return false;
}
else
{
const FSoftObjectPath& AssetRef = InItem->GetSubCategoryObjectAsset();
if (AssetRef.IsValid() && !CachedNamespaceHelper->IsImportedObject(AssetRef))
{
// A pin type whose underlying asset may be either loaded or unloaded, but is not imported.
return false;
}
}
return true;
}
private:
/** Associated namespace helper object. */
const FBlueprintNamespaceHelper* CachedNamespaceHelper;
};
// ---
FBlueprintNamespaceHelper::FBlueprintNamespaceHelper()
{
// Instance the path tree used to store/retrieve namespaces.
NamespacePathTree = MakeUnique<FBlueprintNamespacePathTree>();
// Add global namespace paths implicitly imported by every Blueprint.
TSet<FString> GlobalImports;
FBlueprintNamespaceUtilities::GetSharedGlobalImports(GlobalImports);
AddNamespaces(GlobalImports);
// Instance the filters that can be used with type pickers, etc.
ClassViewerFilter = MakeShared<FClassViewerNamespaceFilter>(this);
PinTypeSelectorFilter = MakeShared<FPinTypeSelectorNamespaceFilter>(this);
}
FBlueprintNamespaceHelper::~FBlueprintNamespaceHelper()
{
}
void FBlueprintNamespaceHelper::AddBlueprint(const UBlueprint* InBlueprint)
{
if (!InBlueprint)
{
return;
}
// Add the default import set for the given Blueprint.
TSet<FString> DefaultImports;
FBlueprintNamespaceUtilities::GetDefaultImportsForBlueprint(InBlueprint, DefaultImports);
AddNamespaces(DefaultImports);
// Additional namespaces that are explicitly imported by this Blueprint.
AddNamespaces(InBlueprint->ImportedNamespaces);
}
void FBlueprintNamespaceHelper::AddNamespace(const FString& Namespace)
{
if (!Namespace.IsEmpty())
{
// Add the path corresponding to the given namespace identifier.
NamespacePathTree->AddPath(Namespace);
}
}
void FBlueprintNamespaceHelper::RemoveNamespace(const FString& Namespace)
{
if (!Namespace.IsEmpty())
{
// Remove the path corresponding to the given namespace identifier.
NamespacePathTree->RemovePath(Namespace);
}
}
bool FBlueprintNamespaceHelper::IsIncludedInNamespaceList(const FString& TestNamespace) const
{
// Empty namespace == global namespace
if (TestNamespace.IsEmpty())
{
return true;
}
// Check to see if X is added, followed by X.Y (which contains X.Y.Z), and so on until we run out of path segments
const bool bMatchFirstInclusivePath = true;
TSharedPtr<FBlueprintNamespacePathTree::FNode> PathNode = NamespacePathTree->FindPathNode(TestNamespace, bMatchFirstInclusivePath);
// Return true if this is a valid path that was explicitly added
return PathNode.IsValid();
}
bool FBlueprintNamespaceHelper::IsImportedObject(const UObject* InObject) const
{
// Determine the object's namespace identifier.
FString Namespace = FBlueprintNamespaceUtilities::GetObjectNamespace(InObject);
// Return whether or not the namespace was added, explicitly or otherwise.
return IsIncludedInNamespaceList(Namespace);
}
bool FBlueprintNamespaceHelper::IsImportedObject(const FSoftObjectPath& InObjectPath) const
{
// Determine the object's namespace identifier.
FString Namespace = FBlueprintNamespaceUtilities::GetObjectNamespace(InObjectPath);
// Return whether or not the namespace was added, explicitly or otherwise.
return IsIncludedInNamespaceList(Namespace);
}
bool FBlueprintNamespaceHelper::IsImportedAsset(const FAssetData& InAssetData) const
{
// Determine the asset's namespace identifier.
FString Namespace = FBlueprintNamespaceUtilities::GetAssetNamespace(InAssetData);
// Return whether or not the namespace was added, explicitly or otherwise.
return IsIncludedInNamespaceList(Namespace);
}
namespace UE::Editor::Kismet::Private
{
static void OnUpdateNamespaceEditorFeatureConsoleFlag(IConsoleVariable* InCVar, bool* InValuePtr)
{
check(InCVar);
// Skip if not set by console command; in that case we're updating the flag directly.
if ((InCVar->GetFlags() & ECVF_SetByMask) != ECVF_SetByConsole)
{
return;
}
// Update the editor setting (referenced) to match the console variable's new setting.
check(InValuePtr);
*InValuePtr = InCVar->GetBool();
// Refresh the Blueprint editor UI environment in response to the console variable change.
FBlueprintNamespaceUtilities::RefreshBlueprintEditorFeatures();
}
}
void FBlueprintNamespaceHelper::RefreshEditorFeatureConsoleFlags()
{
UBlueprintEditorSettings* BlueprintEditorSettings = GetMutableDefault<UBlueprintEditorSettings>();
// Register callbacks to respond to flag changes via console.
static bool bIsInitialized = false;
if (!bIsInitialized)
{
auto InitCVarFlag = [](IConsoleVariable* InCVar, bool& InValueRef)
{
using namespace UE::Editor::Kismet::Private;
InCVar->OnChangedDelegate().AddStatic(&OnUpdateNamespaceEditorFeatureConsoleFlag, &InValueRef);
};
InitCVarFlag(CVarBPEnableNamespaceFilteringFeatures.AsVariable(), BlueprintEditorSettings->bEnableNamespaceFilteringFeatures);
InitCVarFlag(CVarBPEnableNamespaceImportingFeatures.AsVariable(), BlueprintEditorSettings->bEnableNamespaceImportingFeatures);
InitCVarFlag(CVarBPImportParentClassNamespaces.AsVariable(), BlueprintEditorSettings->bInheritImportedNamespacesFromParentBP);
bIsInitialized = true;
}
// Update console variables to match current Blueprint editor settings.
static bool bIsUpdating = false;
if (!bIsUpdating)
{
TGuardValue<bool> ScopeGuard(bIsUpdating, true);
auto SetCVarFlag = [](IConsoleVariable* InCVar, bool& InValueRef)
{
InCVar->Set(InValueRef);
};
SetCVarFlag(CVarBPEnableNamespaceFilteringFeatures.AsVariable(), BlueprintEditorSettings->bEnableNamespaceFilteringFeatures);
SetCVarFlag(CVarBPEnableNamespaceImportingFeatures.AsVariable(), BlueprintEditorSettings->bEnableNamespaceImportingFeatures);
SetCVarFlag(CVarBPImportParentClassNamespaces.AsVariable(), BlueprintEditorSettings->bInheritImportedNamespacesFromParentBP);
}
}
#undef LOCTEXT_NAMESPACE