File size: 6,290 Bytes
7fd553e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
// Copyright Epic Games, Inc. All Rights Reserved.

#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, /*out*/ 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, /*out*/ 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);



	// Map from added asset to list of assets in old paths that supports it (if any)

	TMap<FName, TSet<FName>> SupportMap;

	TSet<FName> VisitedAssets;



	// Map of count of newly added assets directly/indirectly supported by each supporter asset in the old paths

	TMap<FName, int32> SupporterCountMap;

	TMap<FName, TSet<FName>> SupporterToAddedMap;



	TFunction<void(const FName)> RecursivelyBuildSupport = [&](const FName IntroducedAssetPath)

	{

		// Someone else may have already processed me as part of their dependencies

		if (!VisitedAssets.Contains(IntroducedAssetPath))

		{

			VisitedAssets.Add(IntroducedAssetPath);



			TArray<FName> Referencers;

			AssetRegistry.GetReferencers(IntroducedAssetPath, /*out*/ Referencers);



			for (const FName& Referencer : Referencers)

			{

				if (OldPathSet.Contains(Referencer))

				{

					// Direct support

					SupportMap.FindOrAdd(IntroducedAssetPath).Add(Referencer);

				}

				else

				{

					// Indirect, need to process recursively

					RecursivelyBuildSupport(Referencer);



					// Can now use the supports it indicated to build into our own

					TSet<FName>& MySupports = SupportMap.FindOrAdd(IntroducedAssetPath);

					if (TSet<FName>* pRecuriveReferencers = SupportMap.Find(Referencer))

					{

						MySupports.Append(*pRecuriveReferencers);

					}

				}

			}

		}

	};



	// Find the supporters

	for (const FName& IntroducedAssetPath : IntroducedAssetSet)

	{

		RecursivelyBuildSupport(IntroducedAssetPath);

	}



	// Count the strongest supporters

	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;



		// Filter to added assets (and exclude ones already printed if we were asked to)

		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); });



		// Print out the list

		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()));

			//@TODO: Check to see if this is instead supported by a primary asset maybe?

		}

	}

}));