Files
UnrealEngineUWP/Engine/Source/Editor/ContentBrowser/Private/CollectionAssetRegistryBridge.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

169 lines
6.0 KiB
C++

// Copyright Epic Games, Inc. All Rights Reserved.
#include "CollectionAssetRegistryBridge.h"
#include "Modules/ModuleManager.h"
#include "UObject/ObjectMacros.h"
#include "UObject/Class.h"
#include "Misc/PackageName.h"
#include "UObject/ConstructorHelpers.h"
#include "AssetRegistry/AssetData.h"
#include "AssetRegistry/AssetRegistryModule.h"
#include "CollectionManagerTypes.h"
#include "ICollectionManager.h"
#include "CollectionManagerModule.h"
#include "ContentBrowserLog.h"
#define LOCTEXT_NAMESPACE "ContentBrowser"
/** The collection manager doesn't know how to follow redirectors, this class provides it with that knowledge */
class FCollectionRedirectorFollower : public ICollectionRedirectorFollower
{
public:
FCollectionRedirectorFollower()
: AssetRegistryModule(FModuleManager::LoadModuleChecked<FAssetRegistryModule>(TEXT("AssetRegistry")))
{
}
virtual bool FixupObject(const FName& InObjectPath, FName& OutNewObjectPath) override
{
OutNewObjectPath = NAME_None;
if (InObjectPath.ToString().StartsWith(TEXT("/Script/")))
{
// We can't use FindObject while we're saving
if (!GIsSavingPackage)
{
const FString ClassPathStr = InObjectPath.ToString();
check(FPackageName::IsValidObjectPath(ClassPathStr));
UClass* FoundClass = FindObject<UClass>(nullptr, *ClassPathStr);
if (!FoundClass)
{
// Use the linker to search for class name redirects (from the loaded ActiveClassRedirects)
const FString NewClassName = FLinkerLoad::FindNewPathNameForClass(ClassPathStr, false);
if (!NewClassName.IsEmpty())
{
check(FPackageName::IsValidObjectPath(NewClassName));
// Our new class name might be lacking the path, so try and find it so we can use the full path in the collection
FoundClass = FindObject<UClass>(nullptr, *NewClassName);
if (FoundClass)
{
OutNewObjectPath = *FoundClass->GetPathName();
}
}
}
}
}
else
{
// Keep track of visted redirectors in case we loop.
TSet<FName> VisitedRedirectors;
// Use the asset registry to avoid loading the object
FAssetData ObjectAssetData = AssetRegistryModule.Get().GetAssetByObjectPath(InObjectPath, true);
while (ObjectAssetData.IsValid() && ObjectAssetData.IsRedirector())
{
// Check to see if we've already seen this path before, it's possible we might have found a redirector loop.
if ( VisitedRedirectors.Contains(ObjectAssetData.ObjectPath) )
{
UE_LOG(LogContentBrowser, Error, TEXT("Redirector Loop Found!"));
for ( FName Redirector : VisitedRedirectors )
{
UE_LOG(LogContentBrowser, Error, TEXT("Redirector: %s"), *Redirector.ToString());
}
ObjectAssetData = FAssetData();
break;
}
VisitedRedirectors.Add(ObjectAssetData.ObjectPath);
// Get the destination object from the meta-data rather than load the redirector object, as
// loading a redirector will also load the object it points to, which could cause a large hitch
FString DestinationObjectPath;
if (ObjectAssetData.GetTagValue("DestinationObject", DestinationObjectPath))
{
ConstructorHelpers::StripObjectClass(DestinationObjectPath);
ObjectAssetData = AssetRegistryModule.Get().GetAssetByObjectPath(*DestinationObjectPath);
}
else
{
ObjectAssetData = FAssetData();
}
}
OutNewObjectPath = ObjectAssetData.ObjectPath;
}
return OutNewObjectPath != NAME_None && InObjectPath != OutNewObjectPath;
}
private:
FAssetRegistryModule& AssetRegistryModule;
};
FCollectionAssetRegistryBridge::FCollectionAssetRegistryBridge()
{
// Load the asset registry module to listen for updates
FAssetRegistryModule& AssetRegistryModule = FModuleManager::LoadModuleChecked<FAssetRegistryModule>(TEXT("AssetRegistry"));
AssetRegistryModule.Get().OnAssetRemoved().AddRaw(this, &FCollectionAssetRegistryBridge::OnAssetRemoved);
AssetRegistryModule.Get().OnAssetRenamed().AddRaw(this, &FCollectionAssetRegistryBridge::OnAssetRenamed);
if (AssetRegistryModule.Get().IsLoadingAssets())
{
AssetRegistryModule.Get().OnFilesLoaded().AddRaw(this, &FCollectionAssetRegistryBridge::OnAssetRegistryLoadComplete);
}
else
{
OnAssetRegistryLoadComplete();
}
}
FCollectionAssetRegistryBridge::~FCollectionAssetRegistryBridge()
{
// Load the asset registry module to unregister delegates
if (FModuleManager::Get().IsModuleLoaded("AssetRegistry"))
{
FAssetRegistryModule& AssetRegistryModule = FModuleManager::GetModuleChecked<FAssetRegistryModule>(TEXT("AssetRegistry"));
AssetRegistryModule.Get().OnAssetRemoved().RemoveAll(this);
AssetRegistryModule.Get().OnAssetRenamed().RemoveAll(this);
AssetRegistryModule.Get().OnFilesLoaded().RemoveAll(this);
}
}
void FCollectionAssetRegistryBridge::OnAssetRegistryLoadComplete()
{
FCollectionManagerModule& CollectionManagerModule = FCollectionManagerModule::GetModule();
// We've found all the assets, let the collections manager fix up its references now so that it doesn't reference any redirectors
FCollectionRedirectorFollower RedirectorFollower;
CollectionManagerModule.Get().HandleFixupRedirectors(RedirectorFollower);
}
void FCollectionAssetRegistryBridge::OnAssetRenamed(const FAssetData& AssetData, const FString& OldObjectPath)
{
FCollectionManagerModule& CollectionManagerModule = FCollectionManagerModule::GetModule();
// Notify the collections manager that an asset has been renamed
CollectionManagerModule.Get().HandleObjectRenamed(*OldObjectPath, AssetData.ObjectPath);
}
void FCollectionAssetRegistryBridge::OnAssetRemoved(const FAssetData& AssetData)
{
FCollectionManagerModule& CollectionManagerModule = FCollectionManagerModule::GetModule();
if (AssetData.IsRedirector())
{
// Notify the collections manager that a redirector has been removed
// This will attempt to re-save any collections that still have a reference to this redirector in their on-disk collection data
CollectionManagerModule.Get().HandleRedirectorDeleted(AssetData.ObjectPath);
}
else
{
// Notify the collections manager that an asset has been removed
CollectionManagerModule.Get().HandleObjectDeleted(AssetData.ObjectPath);
}
}
#undef LOCTEXT_NAMESPACE