File size: 5,204 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
// Copyright Epic Games, Inc. All Rights Reserved.

#include "Engine/BlueprintGeneratedClass.h"
#include "UObject/UObjectIterator.h"
#include "HAL/FileManager.h"
#include "Misc/Paths.h"
#include "Engine/World.h"

#include "LyraLogChannels.h"

//////////////////////////////////////////////////////////////////////////

#if ALLOW_DEBUG_FILES
#include "HAL/IConsoleManager.h"

// Writes a collection of the specified name containing a list of items (returning the absolute file path to the collection)
// This is manual rather than relying on the collection manager so it can be used at runtime without depending on a developer module
FString WriteCollectionFile(const FString& CollectionName, const TArray<FString>& Items)

{
	// If in the editor, create it in the directory that CST_Local would have used, otherwise write it to the profiling dir for later harvesting
	const FString OutputDir = WITH_EDITOR ? (FPaths::ProjectSavedDir() / TEXT("Collections")) : (FPaths::ProfilingDir() / TEXT("AssetSnapshots"));

	IFileManager::Get().MakeDirectory(*OutputDir, true);

	const FString LogFilename = OutputDir / (CollectionName + TEXT(".collection"));

	if (FArchive* OutputFile = IFileManager::Get().CreateDebugFileWriter(*LogFilename))
	{
		const FGuid CollectionGUID = FGuid::NewGuid();

		OutputFile->Logf(TEXT("FileVersion:2"));
		OutputFile->Logf(TEXT("Type:Static"));
		OutputFile->Logf(TEXT("Guid:%s"), *CollectionGUID.ToString(EGuidFormats::DigitsWithHyphens));
		OutputFile->Logf(TEXT(""));

		for (const FString& Item : Items)
		{
			OutputFile->Logf(TEXT("%s"), *Item);
		}

		// Flush, close and delete.
		delete OutputFile;

		const FString AbsolutePath = IFileManager::Get().ConvertToAbsolutePathForExternalAppForRead(*LogFilename);
		return AbsolutePath;
	}

	return FString();
}

FAutoConsoleCommandWithWorldAndArgs GObjListToCollectionCmd(

	TEXT("Lyra.ObjListToCollection"),

	TEXT("Spits out a collection that contains the current object list"),

	FConsoleCommandWithWorldAndArgsDelegate::CreateStatic(

		[](const TArray<FString>& Params, UWorld* World)

{

	// Get the list of loaded assets

	TArray<FString> AssetPaths;

	for (TObjectIterator<UObject> It; It; ++It)

	{

		UObject* Obj = *It;

		if (Obj->IsAsset())

		{

			AssetPaths.Add(Obj->GetPathName());

		}

		else if (UBlueprintGeneratedClass* Class = Cast<UBlueprintGeneratedClass>(Obj))

		{

			FString BlueprintName = Class->GetPathName();

			BlueprintName.RemoveFromEnd(TEXT("_C"));

			AssetPaths.Add(BlueprintName);

		}

	}

	AssetPaths.Sort();



	// Determine the filename

	FString CollectionNameSuffix;

	if (Params.Num() > 0)

	{

		CollectionNameSuffix = TEXT("_") + Params[0];

	}

	const FString CollectionName = FString::Printf(TEXT("_LoadedAssets_%s_%s%s"), *FDateTime::Now().ToString(TEXT("%H%M%S")), *GWorld->GetMapName(), *CollectionNameSuffix);



	// Write the collection out

	const FString CollectionFilePath = WriteCollectionFile(CollectionName, AssetPaths);

	UE_LOG(LogLyra, Warning, TEXT("Wrote collection of loaded assets to %s"), *CollectionFilePath);

}));

#endif

//////////////////////////////////////////////////////////////////////////

// This can be used in a command to compare assets to a parent class (BP or C++ default) to determine if any fields are actually 'fixed' and can be removed to save memory
void AnalyzeObjectListForDifferences(TArrayView<UObject*> ObjectList, UClass* CommonClass, const TSet<FName>& PropertiesToIgnore, bool bLogAllMatchedDefault=false)

{
	check(CommonClass);
	UObject* CommonClassCDO = CommonClass->GetDefaultObject();

	UE_LOG(LogLyra, Log, TEXT("  Field\tDifferentToBase\tNumValues\tValues"));

	for (TFieldIterator<FProperty> PropIt(CommonClass); PropIt; ++PropIt)
	{
		FProperty* Prop = *PropIt;
		if (PropertiesToIgnore.Contains(Prop->GetFName()))
		{
			continue;
		}

		//@TODO: Handle fixed length arrays
		ensure(Prop->ArrayDim <= 1);

		FString DefaultValueStr;
		Prop->ExportText_InContainer(0, /*out*/ DefaultValueStr, CommonClassCDO, CommonClassCDO, nullptr, 0);

		bool bAnyMatchedDefaultValue = false;
		bool bAllMatchedDefaultValue = true;

		TSet<FString> ValuesObserved;
		for (UObject* Object : ObjectList)
		{
			FString ValueStr;
			if (Prop->ExportText_InContainer(0, /*out*/ ValueStr, Object, CommonClassCDO, nullptr, 0))
			{
				ValuesObserved.Add(ValueStr);
				bAllMatchedDefaultValue = false;
			}
			else
			{
				bAnyMatchedDefaultValue = true;
			}
		}

		if (bAnyMatchedDefaultValue)
		{
			ValuesObserved.Add(DefaultValueStr);
		}

		if (!bAllMatchedDefaultValue)
		{
			const FString ValueList = FString::Join(ValuesObserved, TEXT(","));

			UE_LOG(LogLyra, Log, TEXT("  %s::%s\t%s\t%d\t%s"),
				*CommonClass->GetName(),
				*Prop->GetName(),
				(ValuesObserved.Num() == 1) ? TEXT("FixedDifferent") : TEXT("Varies"),
				ValuesObserved.Num(),
				*ValueList);
		}
		else if (bLogAllMatchedDefault)
		{
			UE_LOG(LogLyra, Log, TEXT("  %s::%s\t%s"), *CommonClass->GetName(), *Prop->GetName(), TEXT("Default"));
		}
	}
}

//////////////////////////////////////////////////////////////////////////