Files
UnrealEngineUWP/Engine/Source/Editor/UnrealEd/Private/Commandlets/CompileAllBlueprintsCommandlet.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

360 lines
12 KiB
C++

// Copyright Epic Games, Inc. All Rights Reserved.
#include "Commandlets/CompileAllBlueprintsCommandlet.h"
#include "AssetRegistry/AssetRegistryModule.h"
#include "Kismet2/CompilerResultsLog.h"
#include "EngineUtils.h"
#include "ISourceControlModule.h"
#include "Misc/FileHelper.h"
#include "Misc/Paths.h"
#include "Engine/Engine.h"
#include "Kismet2/KismetEditorUtilities.h"
DEFINE_LOG_CATEGORY_STATIC(LogCompileAllBlueprintsCommandlet, Log, All);
#define KISMET_COMPILER_MODULENAME "KismetCompiler"
UCompileAllBlueprintsCommandlet::UCompileAllBlueprintsCommandlet(const FObjectInitializer& ObjectInitializer)
: Super(ObjectInitializer)
{
bResultsOnly = false;
bCookedOnly = false;
bDirtyOnly = false;
bSimpleAssetList = false;
BlueprintBaseClassName = UBlueprint::StaticClass()->GetClassPathName();
TotalNumFailedLoads = 0;
TotalNumFatalIssues = 0;
TotalNumWarnings = 0;
}
int32 UCompileAllBlueprintsCommandlet::Main(const FString& Params)
{
InitCommandLine(Params);
InitKismetBlueprintCompiler();
BuildBlueprintAssetList();
BuildBlueprints();
LogResults();
return (TotalNumFatalIssues + TotalNumFailedLoads);
}
void UCompileAllBlueprintsCommandlet::InitCommandLine(const FString& Params)
{
TArray<FString> Tokens;
TArray<FString> Switches;
TMap<FString, FString> SwitchParams;
ParseCommandLine(*Params, Tokens, Switches, SwitchParams);
bResultsOnly = Switches.Contains(TEXT("ShowResultsOnly"));
bDirtyOnly = Switches.Contains(TEXT("DirtyOnly"));
bCookedOnly = Switches.Contains(TEXT("CookedOnly"));
bSimpleAssetList = Switches.Contains(TEXT("SimpleAssetList"));
RequireAssetTags.Empty();
if (SwitchParams.Contains(TEXT("RequireTags")))
{
const FString& FullTagInfo = SwitchParams[TEXT("RequireTags")];
ParseTagPairs(FullTagInfo, RequireAssetTags);
}
ExcludeAssetTags.Empty();
if (SwitchParams.Contains(TEXT("ExcludeTags")))
{
const FString& FullTagInfo = SwitchParams[TEXT("ExcludeTags")];
ParseTagPairs(FullTagInfo, ExcludeAssetTags);
}
IgnoreFolders.Empty();
if (SwitchParams.Contains(TEXT("IgnoreFolder")))
{
const FString& AllIgnoreFolders = SwitchParams[TEXT("IgnoreFolder")];
ParseIgnoreFolders(AllIgnoreFolders);
}
FileAllowList.Empty();
if (SwitchParams.Contains(TEXT("AllowListFile")))
{
const FString& ListFullPath = SwitchParams[TEXT("AllowListFile")];
ParseAllowList(ListFullPath);
}
if (SwitchParams.Contains(TEXT("BlueprintBaseClass")))
{
FString BlueprintBaseClassParam = *SwitchParams[TEXT("BlueprintBaseClass")];
BlueprintBaseClassName = UClass::TryConvertShortTypeNameToPathName<UClass>(BlueprintBaseClassParam, ELogVerbosity::Warning, TEXT("UCompileAllBlueprintsCommandlet::InitCommandLine"));
UE_CLOG(BlueprintBaseClassName.IsNull(), LogCompileAllBlueprintsCommandlet, Error, TEXT("Failed to convert short class name -BlueprintBaseClass=\"%s\" to path name"), *BlueprintBaseClassParam);
}
}
void UCompileAllBlueprintsCommandlet::ParseTagPairs(const FString& FullTagString, TArray<TPair<FString, TArray<FString>>>& OutputAssetTags)
{
TArray<FString> AllTagPairs;
FullTagString.ParseIntoArray(AllTagPairs, TEXT(";"));
//Break All Tag Pairs into individual tags and values
for (const FString& StringTagPair : AllTagPairs)
{
TArray<FString> ParsedTagPairs;
StringTagPair.ParseIntoArray(ParsedTagPairs, TEXT(","));
if (ParsedTagPairs.Num() > 0)
{
TArray<FString> TagValues;
//Start AssetTagIndex at 1, as the first one is the key. We will be using index 0 in all adds as the key
for (int AssetTagIndex = 1; AssetTagIndex < ParsedTagPairs.Num(); ++AssetTagIndex)
{
TagValues.Add(ParsedTagPairs[AssetTagIndex]);
}
TPair<FString, TArray<FString>> NewPair(ParsedTagPairs[0], TagValues);
OutputAssetTags.Add(NewPair);
}
}
}
void UCompileAllBlueprintsCommandlet::ParseIgnoreFolders(const FString& FullIgnoreFolderString)
{
TArray<FString> ParsedIgnoreFolders;
FullIgnoreFolderString.ParseIntoArray(ParsedIgnoreFolders, TEXT(","));
for (const FString& IgnoreFolder : ParsedIgnoreFolders)
{
IgnoreFolders.Add(IgnoreFolder.TrimQuotes());
}
}
void UCompileAllBlueprintsCommandlet::ParseAllowList(const FString& ListFilePath)
{
const FString FilePath = FPaths::ProjectDir() + ListFilePath;
if (!FFileHelper::LoadANSITextFileToStrings(*FilePath, &IFileManager::Get(), FileAllowList))
{
UE_LOG(LogCompileAllBlueprintsCommandlet, Error, TEXT("Failed to Load AllowList File! : %s"), *FilePath);
}
}
void UCompileAllBlueprintsCommandlet::BuildBlueprints()
{
double LastGCTime = FPlatformTime::Seconds();
for (FAssetData const& Asset : BlueprintAssetList)
{
if (ShouldBuildAsset(Asset))
{
FString const AssetPath = Asset.ObjectPath.ToString();
UE_LOG(LogCompileAllBlueprintsCommandlet, Display, TEXT("Loading and Compiling: '%s'..."), *AssetPath);
//Load with LOAD_NoWarn and LOAD_DisableCompileOnLoad as we are covering those explicitly with CompileBlueprint errors.
UBlueprint* LoadedBlueprint = Cast<UBlueprint>(StaticLoadObject(Asset.GetClass(), /*Outer =*/nullptr, *AssetPath,nullptr, LOAD_NoWarn | LOAD_DisableCompileOnLoad));
if (LoadedBlueprint == nullptr)
{
++TotalNumFailedLoads;
UE_LOG(LogCompileAllBlueprintsCommandlet, Error, TEXT("Failed to Load : '%s'."), *AssetPath);
continue;
}
else
{
CompileBlueprint(LoadedBlueprint);
}
const double TimeNow = FPlatformTime::Seconds();
if (TimeNow - LastGCTime >= 10.0)
{
GEngine->TrimMemory();
LastGCTime = TimeNow;
}
}
}
}
void UCompileAllBlueprintsCommandlet::BuildBlueprintAssetList()
{
BlueprintAssetList.Empty();
UE_LOG(LogCompileAllBlueprintsCommandlet, Display, TEXT("Loading Asset Registry..."));
FAssetRegistryModule& AssetRegistryModule = FModuleManager::LoadModuleChecked<FAssetRegistryModule>(AssetRegistryConstants::ModuleName);
AssetRegistryModule.Get().SearchAllAssets(/*bSynchronousSearch =*/true);
UE_LOG(LogCompileAllBlueprintsCommandlet, Display, TEXT("Finished Loading Asset Registry."));
UE_LOG(LogCompileAllBlueprintsCommandlet, Display, TEXT("Gathering All Blueprints From Asset Registry..."));
AssetRegistryModule.Get().GetAssetsByClass(BlueprintBaseClassName, BlueprintAssetList, true);
}
bool UCompileAllBlueprintsCommandlet::ShouldBuildAsset(FAssetData const& Asset) const
{
bool bShouldBuild = true;
if (bCookedOnly && Asset.GetClass() && !Asset.GetClass()->bCooked)
{
FString const AssetPath = Asset.ObjectPath.ToString();
UE_LOG(LogCompileAllBlueprintsCommandlet, Verbose, TEXT("Skipping Building %s: As is not cooked"), *AssetPath);
bShouldBuild = false;
}
if (IgnoreFolders.Num() > 0)
{
for (const FString& IgnoreFolder : IgnoreFolders)
{
if (Asset.ObjectPath.ToString().StartsWith(IgnoreFolder))
{
FString const AssetPath = Asset.ObjectPath.ToString();
UE_LOG(LogCompileAllBlueprintsCommandlet, Verbose, TEXT("Skipping Building %s: As Object is in an Ignored Folder"), *AssetPath);
bShouldBuild = false;
}
}
}
if ((ExcludeAssetTags.Num() > 0) && (CheckHasTagInList(Asset, ExcludeAssetTags)))
{
FString const AssetPath = Asset.ObjectPath.ToString();
UE_LOG(LogCompileAllBlueprintsCommandlet, Verbose, TEXT("Skipping Building %s: As has an excluded tag"), *AssetPath);
bShouldBuild = false;
}
if ((RequireAssetTags.Num() > 0) && (!CheckHasTagInList(Asset, RequireAssetTags)))
{
FString const AssetPath = Asset.ObjectPath.ToString();
UE_LOG(LogCompileAllBlueprintsCommandlet, Verbose, TEXT("Skipping Building %s: As the asset is missing a required tag"), *AssetPath);
bShouldBuild = false;
}
if ((FileAllowList.Num() > 0) && (!IsAssetAllowed(Asset)))
{
FString const AssetPath = Asset.ObjectPath.ToString();
UE_LOG(LogCompileAllBlueprintsCommandlet, Verbose, TEXT("Skipping Building %s: As the asset is not part of the allow list"), *AssetPath);
bShouldBuild = false;
}
if (bDirtyOnly)
{
const UPackage* AssetPackage = Asset.GetPackage();
if ((AssetPackage == nullptr) || !AssetPackage->IsDirty())
{
FString const AssetPath = Asset.ObjectPath.ToString();
UE_LOG(LogCompileAllBlueprintsCommandlet, Verbose, TEXT("Skipping Building %s: As Package is not dirty"), *AssetPath);
bShouldBuild = false;
}
}
return bShouldBuild;
}
bool UCompileAllBlueprintsCommandlet::CheckHasTagInList(FAssetData const& Asset, const TArray<TPair<FString, TArray<FString>>>& TagCollectionToCheck) const
{
bool bContainedTag = false;
for (const TPair<FString, TArray<FString>>& SingleTagAndValues : TagCollectionToCheck)
{
if (Asset.TagsAndValues.Contains(FName(*SingleTagAndValues.Key)))
{
const TArray<FString>& TagValuesToCheck = SingleTagAndValues.Value;
if (TagValuesToCheck.Num() > 0)
{
for (const FString& IndividualValueToCheck : TagValuesToCheck)
{
if (Asset.TagsAndValues.ContainsKeyValue(FName(*SingleTagAndValues.Key), IndividualValueToCheck))
{
bContainedTag = true;
break;
}
}
}
//if we don't have any values to check, just return true as the tag was included
else
{
bContainedTag = true;
break;
}
}
}
return bContainedTag;
}
bool UCompileAllBlueprintsCommandlet::IsAssetAllowed(FAssetData const& Asset) const
{
bool bIsInList = false;
const FString& AssetFilePath = Asset.ObjectPath.ToString();
for (const FString& Entry : FileAllowList)
{
if (AssetFilePath == Entry)
{
bIsInList = true;
break;
}
}
return bIsInList;
}
void UCompileAllBlueprintsCommandlet::CompileBlueprint(UBlueprint* Blueprint)
{
if (KismetBlueprintCompilerModule && Blueprint)
{
//Have to create a new MessageLog for each asset as the warning / error counts are cumulative
FCompilerResultsLog MessageLog;
//Need to prevent the Compiler Results Log from automatically outputting results if verbosity is too low
if (bResultsOnly)
{
MessageLog.bSilentMode = true;
}
else
{
MessageLog.bAnnotateMentionedNodes = true;
}
MessageLog.SetSourcePath(Blueprint->GetPathName());
MessageLog.BeginEvent(TEXT("Compile"));
FKismetEditorUtilities::CompileBlueprint(Blueprint, EBlueprintCompileOptions::SkipGarbageCollection, &MessageLog);
MessageLog.EndEvent();
if ((MessageLog.NumErrors + MessageLog.NumWarnings) > 0)
{
AssetsWithErrorsOrWarnings.Add(Blueprint->GetPathName());
TotalNumFatalIssues += MessageLog.NumErrors;
TotalNumWarnings += MessageLog.NumWarnings;
}
for (TSharedRef<class FTokenizedMessage>& Message : MessageLog.Messages)
{
UE_LOG(LogCompileAllBlueprintsCommandlet, Display, TEXT("%s"), *Message->ToText().ToString());
}
}
}
void UCompileAllBlueprintsCommandlet::InitKismetBlueprintCompiler()
{
UE_LOG(LogCompileAllBlueprintsCommandlet, Display, TEXT("Loading Kismit Blueprint Compiler..."));
//Get Kismet Compiler Setup. Static so that the expensive stuff only happens once per run.
KismetBlueprintCompilerModule = &FModuleManager::LoadModuleChecked<IKismetCompilerInterface>(TEXT(KISMET_COMPILER_MODULENAME));
UE_LOG(LogCompileAllBlueprintsCommandlet, Display, TEXT("Finished Loading Kismit Blueprint Compiler..."));
}
void UCompileAllBlueprintsCommandlet::LogResults()
{
//results output
UE_LOG(LogCompileAllBlueprintsCommandlet, Display, TEXT("\n\n\n===================================================================================\nCompiling Completed with %d errors and %d warnings and %d blueprints that failed to load.\n===================================================================================\n\n\n"), TotalNumFatalIssues, TotalNumWarnings, TotalNumFailedLoads);
//Assets with problems listing
if (bSimpleAssetList && (AssetsWithErrorsOrWarnings.Num() > 0))
{
UE_LOG(LogCompileAllBlueprintsCommandlet, Warning, TEXT("\n===================================================================================\nAssets With Errors or Warnings:\n===================================================================================\n"));
for (const FString& Asset : AssetsWithErrorsOrWarnings)
{
UE_LOG(LogCompileAllBlueprintsCommandlet, Warning, TEXT("%s"), *Asset);
}
UE_LOG(LogCompileAllBlueprintsCommandlet, Warning, TEXT("\n===================================================================================\nEnd of Asset List\n===================================================================================\n"));
}
}