Improve the WingTypes registry

This commit is contained in:
2026-03-18 23:10:09 -04:00
parent a18cff3fc9
commit 336b80df39
3 changed files with 111 additions and 68 deletions

View File

@@ -3,7 +3,10 @@
#include "Editor.h"
#include "EdGraphSchema_K2.h"
#include "Engine/Blueprint.h"
#include "StructUtils/UserDefinedStruct.h"
#include "Engine/UserDefinedEnum.h"
#include "UObject/UObjectIterator.h"
#include "AssetRegistry/AssetRegistryModule.h"
// ---------------------------------------------------------------------------
// Choose Short Name
@@ -29,42 +32,51 @@ void UWingTypes::ReserveShortName(FName Name)
ShortToPath.Add(NameStr.ToLower(), FString(TEXT("PRIMITIVE")));
}
FString UWingTypes::ChooseShortName(const UObject* Obj)
FString UWingTypes::ChooseShortName(const FString &Proposal, const FString &FullObjectPath)
{
if (!Cast<UScriptStruct>(Obj) && !Cast<UClass>(Obj) && !Cast<UEnum>(Obj))
return FString();
// You must call this with an object path, not an asset path.
check(FullObjectPath.Contains(TEXT(".")));
FString Path = Obj->GetPathName();
FString *OldShort = PathToShort.Find(Path);
// Look to see if we've already assigned a short name to this path.
FString *OldShort = PathToShort.Find(FullObjectPath);
if (OldShort != nullptr) return *OldShort;
FString Name = GetNameWithoutUnderscoreC(Obj);
FString Lower = Name.ToLower();
// Check if the proposed name is available.
FString Lower = Proposal.ToLower();
if (!ShortToPath.Contains(Lower))
{
ShortToPath.Add(Lower, Path);
PathToShort.Add(Path, Name);
return Name;
ShortToPath.Add(Lower, FullObjectPath);
PathToShort.Add(FullObjectPath, Proposal);
return Proposal;
}
// The proposed name is not available. Add numbers until we
// find a name that is available.
for (int32 i = 2; ; ++i)
{
FString NumberedLower = FString::Printf(TEXT("%s%d"), *Lower, i);
if (!ShortToPath.Contains(NumberedLower))
{
FString NumberedName = FString::Printf(TEXT("%s_%d"), *Name, i);
ShortToPath.Add(NumberedLower, Path);
PathToShort.Add(Path, NumberedName);
return NumberedName;
FString NumberedProposal = FString::Printf(TEXT("%s_%d"), *Proposal, i);
ShortToPath.Add(NumberedLower, FullObjectPath);
PathToShort.Add(FullObjectPath, NumberedProposal);
return NumberedProposal;
}
}
}
FString UWingTypes::ChooseShortName(const UObject* Obj)
{
if (!Cast<UScriptStruct>(Obj) && !Cast<UClass>(Obj) && !Cast<UEnum>(Obj))
return FString();
FString ProposedName = GetNameWithoutUnderscoreC(Obj);
return ChooseShortName(ProposedName, Obj->GetPathName());
}
void UWingTypes::ChooseShortNames(UPackage* Package)
{
if (Package == nullptr) return;
ForEachObjectWithPackage(Package, [&](UObject* Obj)
{
ChooseShortName(Obj);
@@ -72,7 +84,25 @@ void UWingTypes::ChooseShortNames(UPackage* Package)
}, false);
}
void UWingTypes::ChooseShortName(const FAssetData &Data)
{
FString AssetName = Data.AssetName.ToString();
FString PackageName = Data.PackageName.ToString();
FTopLevelAssetPath ClassPath = Data.AssetClassPath;
if (ClassPath == UBlueprint::StaticClass()->GetClassPathName())
{
// Blueprint: the generated class is AssetName_C
FString ObjectPath = FString::Printf(TEXT("%s.%s_C"), *PackageName, *AssetName);
ChooseShortName(AssetName, ObjectPath);
}
else if (ClassPath == UUserDefinedStruct::StaticClass()->GetClassPathName() ||
ClassPath == UUserDefinedEnum::StaticClass()->GetClassPathName())
{
FString ObjectPath = FString::Printf(TEXT("%s.%s"), *PackageName, *AssetName);
ChooseShortName(AssetName, ObjectPath);
}
}
// ---------------------------------------------------------------------------
// TypeToText
@@ -212,11 +242,26 @@ void UWingTypes::Initialize(FSubsystemCollectionBase& Collection)
for (UPackage* Pkg : Packages)
ChooseShortNames(Pkg);
// Scan the asset registry for unloaded assets.
IAssetRegistry& AssetRegistry = FModuleManager::LoadModuleChecked<FAssetRegistryModule>(TEXT("AssetRegistry")).Get();
TArray<FAssetData> AllAssets;
AssetRegistry.GetAllAssets(AllAssets, true);
for (const FAssetData& Data : AllAssets)
ChooseShortName(Data);
// Register for future asset discoveries.
AssetRegistry.OnAssetAdded().AddUObject(this, &UWingTypes::ChooseShortName);
UE_LOG(LogTemp, Display, TEXT("WingTypes: Registered %d types"), ShortToPath.Num());
}
void UWingTypes::Deinitialize()
{
if (FModuleManager::Get().IsModuleLoaded(TEXT("AssetRegistry")))
{
IAssetRegistry& AssetRegistry = FModuleManager::GetModuleChecked<FAssetRegistryModule>(TEXT("AssetRegistry")).Get();
AssetRegistry.OnAssetAdded().RemoveAll(this);
}
Super::Deinitialize();
}
@@ -259,30 +304,43 @@ void UWingTypes::Tokenize(const FString& Input)
}
// ---------------------------------------------------------------------------
// Path to Object Conversion
// Short Name Resolution
// ---------------------------------------------------------------------------
bool UWingTypes::ResolvePath(const FString &Name, const FString &Path, FEdGraphPinType &OutType)
bool UWingTypes::ResolveShortName(const FString &Name, FEdGraphPinType &OutType)
{
// Load the object.
UObject* Obj = LoadObject<UObject>(nullptr, *Path);
if (!Obj)
FString *Path = ShortToPath.Find(Name.ToLower());
if (!Path)
{
Error = FString::Printf(TEXT("Failed to load type '%s' at path '%s'"), *Name, *Path);
Error = FString::Printf(TEXT("Unrecognized type '%s'"), *Name);
return false;
}
// If it's a blueprint, use its generated class.
if (UBlueprint* BP = Cast<UBlueprint>(Obj))
// Primitives (int, float, etc.) are registered with the path "PRIMITIVE".
if (*Path == TEXT("PRIMITIVE"))
{
Obj = BP->GeneratedClass;
if (!Obj)
OutType.PinCategory = FName(*Name);
if ((OutType.PinCategory == UEdGraphSchema_K2::PC_Double) ||
(OutType.PinCategory == UEdGraphSchema_K2::PC_Float))
{
Error = FString::Printf(TEXT("Blueprint '%s' has no generated class"), *Name);
return false;
OutType.PinSubCategory = OutType.PinCategory;
OutType.PinCategory = UEdGraphSchema_K2::PC_Real;
}
return true;
}
// Load the object.
UObject* Obj = LoadObject<UObject>(nullptr, **Path);
if (!Obj)
{
Error = FString::Printf(TEXT("Failed to load type '%s' at path '%s'"), *Name, **Path);
return false;
}
// The short name registry only contains UClass, UScriptStruct, and UEnum.
checkf(Cast<UClass>(Obj) || Cast<UScriptStruct>(Obj) || Cast<UEnum>(Obj),
TEXT("Short name '%s' resolved to unexpected type '%s'"), *Name, *Obj->GetClass()->GetName());
// Determine the category from the object type.
if (Cast<UScriptStruct>(Obj))
{
@@ -295,15 +353,9 @@ bool UWingTypes::ResolvePath(const FString &Name, const FString &Path, FEdGraphP
else
OutType.PinCategory = UEdGraphSchema_K2::PC_Object;
}
else if (Cast<UEnum>(Obj))
{
OutType.PinCategory = UEdGraphSchema_K2::PC_Byte;
}
else
{
// This really shouldn't happen.
Error = FString::Printf(TEXT("'%s' is not a struct, class, enum, or interface"), *Name);
return false;
OutType.PinCategory = UEdGraphSchema_K2::PC_Byte;
}
OutType.PinSubCategoryObject = Obj;
@@ -370,28 +422,7 @@ bool UWingTypes::ParsePlainIdentifier(FEdGraphPinType& OutType)
return false;
}
FString Name = Tokens[Cursor++];
FString *Path = ShortToPath.Find(Name.ToLower());
if (Path == nullptr)
{
Error = TEXT("Unrecognized Type");
return false;
}
if (*Path == TEXT("PRIMITIVE"))
{
OutType.PinCategory = FName(*Name);
if ((OutType.PinCategory == UEdGraphSchema_K2::PC_Double) ||
(OutType.PinCategory == UEdGraphSchema_K2::PC_Float))
{
OutType.PinSubCategory = OutType.PinCategory;
OutType.PinCategory = UEdGraphSchema_K2::PC_Real;
}
return true;
}
else
{
return ResolvePath(Name, *Path, OutType);
}
return ResolveShortName(Name, OutType);
}
bool UWingTypes::ParseWrapped(FName Wrapper, FEdGraphPinType& OutType)