|
|
|
|
| #include "AssetRegistry/IAssetRegistry.h"
|
| #include "CollectionManagerModule.h"
|
| #include "HAL/IConsoleManager.h"
|
| #include "ICollectionContainer.h"
|
| #include "ICollectionManager.h"
|
| #include "Templates/Greater.h"
|
| #include "UObject/SoftObjectPath.h"
|
|
|
| class UWorld;
|
|
|
|
|
|
|
| FAutoConsoleCommandWithWorldArgsAndOutputDevice GDiffCollectionReferenceSupport(
|
| TEXT("Lyra.DiffCollectionReferenceSupport"),
|
| TEXT("Usage:\n")
|
| TEXT(" Lyra.DiffCollectionReferenceSupport OldCollectionName NewCollectionName [Deduplicate]\n")
|
| TEXT("\n")
|
| TEXT("It will list the assets in Old that 'support' assets introduced in New (are referencers directly/indirectly)\n")
|
| TEXT("as well as any loose unsupported assets.\n")
|
| TEXT("\n")
|
| TEXT("The optional third argument controls whether or not multi-supported assets will be de-duplicated (true) or not (false)"),
|
| FConsoleCommandWithWorldArgsAndOutputDeviceDelegate::CreateStatic(
|
| [](const TArray<FString>& Params, UWorld* World, FOutputDevice& Ar)
|
| {
|
| IAssetRegistry& AssetRegistry = IAssetRegistry::GetChecked();;
|
| const TSharedRef<ICollectionContainer>& CollectionContainer = FCollectionManagerModule::GetModule().Get().GetProjectCollectionContainer();
|
|
|
| if (Params.Num() < 2)
|
| {
|
| Ar.Log(TEXT("Expected two parameters"));
|
| return;
|
| }
|
|
|
| if (AssetRegistry.IsLoadingAssets())
|
| {
|
| Ar.Log(TEXT("Asset registry is still scanning, try again later"));
|
| return;
|
| }
|
|
|
| const bool bExcludeSecondInstanceOfMultiSupported = (Params.Num() >= 3) ? Params[2].ToBool() : true;
|
|
|
| TArray<FSoftObjectPath> OldPaths;
|
| if (!CollectionContainer->GetAssetsInCollection(FName(*Params[0]), ECollectionShareType::CST_All, OldPaths))
|
| {
|
| Ar.Log(FString::Printf(TEXT("Failed to find collection %s"), *Params[0]));
|
| return;
|
| }
|
|
|
| TArray<FSoftObjectPath> NewPaths;
|
| if (!CollectionContainer->GetAssetsInCollection(FName(*Params[1]), ECollectionShareType::CST_All, NewPaths))
|
| {
|
| Ar.Log(FString::Printf(TEXT("Failed to find collection %s"), *Params[1]));
|
| return;
|
| }
|
|
|
| TSet<FName> OldPathSet;
|
| Algo::Transform(OldPaths, OldPathSet, &FSoftObjectPath::GetLongPackageFName);
|
|
|
| TSet<FName> NewPathSet;
|
| Algo::Transform(NewPaths, NewPathSet, &FSoftObjectPath::GetLongPackageFName);
|
|
|
| TSet<FName> IntroducedAssetSet = NewPathSet.Difference(OldPathSet);
|
|
|
|
|
| TMap<FName, TSet<FName>> SupportMap;
|
| TSet<FName> VisitedAssets;
|
|
|
|
|
| TMap<FName, int32> SupporterCountMap;
|
| TMap<FName, TSet<FName>> SupporterToAddedMap;
|
|
|
| TFunction<void(const FName)> RecursivelyBuildSupport = [&](const FName IntroducedAssetPath)
|
| {
|
|
|
| if (!VisitedAssets.Contains(IntroducedAssetPath))
|
| {
|
| VisitedAssets.Add(IntroducedAssetPath);
|
|
|
| TArray<FName> Referencers;
|
| AssetRegistry.GetReferencers(IntroducedAssetPath, Referencers);
|
|
|
| for (const FName& Referencer : Referencers)
|
| {
|
| if (OldPathSet.Contains(Referencer))
|
| {
|
|
|
| SupportMap.FindOrAdd(IntroducedAssetPath).Add(Referencer);
|
| }
|
| else
|
| {
|
|
|
| RecursivelyBuildSupport(Referencer);
|
|
|
|
|
| TSet<FName>& MySupports = SupportMap.FindOrAdd(IntroducedAssetPath);
|
| if (TSet<FName>* pRecuriveReferencers = SupportMap.Find(Referencer))
|
| {
|
| MySupports.Append(*pRecuriveReferencers);
|
| }
|
| }
|
| }
|
| }
|
| };
|
|
|
|
|
| for (const FName& IntroducedAssetPath : IntroducedAssetSet)
|
| {
|
| RecursivelyBuildSupport(IntroducedAssetPath);
|
| }
|
|
|
|
|
| for (const auto& KVP : SupportMap)
|
| {
|
| const FName SupportedAsset = KVP.Key;
|
| for (const FName& Supporter : KVP.Value)
|
| {
|
| SupporterToAddedMap.FindOrAdd(Supporter).Add(SupportedAsset);
|
| }
|
| }
|
|
|
| TSet<FName> AlreadyPrintedOut;
|
|
|
| Ar.Log(TEXT("List of supporters, sorted by count of newly added assets being supported"));
|
| SupporterCountMap.ValueSort(TGreater<int32>());
|
| for (const auto& KVP : SupporterToAddedMap)
|
| {
|
| const FName Supporter = KVP.Key;
|
|
|
|
|
| TArray<FName> AddedAssetsBeingSupported(KVP.Value.Array());
|
| AddedAssetsBeingSupported = AddedAssetsBeingSupported.FilterByPredicate([&](FName Test) { return IntroducedAssetSet.Contains(Test); });
|
|
|
| int32 IncludingMultisupportCount = AddedAssetsBeingSupported.Num();
|
| if (bExcludeSecondInstanceOfMultiSupported)
|
| {
|
| AddedAssetsBeingSupported = AddedAssetsBeingSupported.FilterByPredicate([&](FName Test) { return !AlreadyPrintedOut.Contains(Test); });
|
| }
|
|
|
| Algo::Sort(AddedAssetsBeingSupported, [](const FName LHS, const FName RHS) { return LHS.LexicalLess(RHS); });
|
|
|
|
|
| Ar.Log(FString::Printf(TEXT("%s supports %d new assets:"), *Supporter.ToString(), IncludingMultisupportCount));
|
| for (const FName& AddedAsset : AddedAssetsBeingSupported)
|
| {
|
| const int32 AddedAssetSupportCount = SupportMap.FindChecked(AddedAsset).Num();
|
| Ar.Log(FString::Printf(TEXT("\t%s%s"), *AddedAsset.ToString(), (AddedAssetSupportCount > 1) ? TEXT(" [multi-supported]") : TEXT("")));
|
| AlreadyPrintedOut.Add(AddedAsset);
|
| }
|
|
|
| const int32 NumExcludedDueToMultiSupport = IncludingMultisupportCount - AddedAssetsBeingSupported.Num();
|
| if (NumExcludedDueToMultiSupport > 0)
|
| {
|
| Ar.Log(FString::Printf(TEXT("\tAnd %d more that were also supported by a previous supporter"), NumExcludedDueToMultiSupport));
|
| }
|
| }
|
|
|
| Ar.Log(TEXT("\n"));
|
| Ar.Log(TEXT("List of unsupported assets:"));
|
| for (const FName& AssetName : IntroducedAssetSet)
|
| {
|
| if (!SupportMap.Contains(AssetName) || (SupportMap.FindRef(AssetName).Num() == 0))
|
| {
|
| Ar.Log(FString::Printf(TEXT("\t%s"), *AssetName.ToString()));
|
|
|
| }
|
| }
|
| }));
|
|
|