ExtPackageUtils.cpp 48 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543
  1. // Copyright 2017-2021 marynate. All Rights Reserved.
  2. #include "ExtPackageUtils.h"
  3. #include "ExtContentBrowser.h"
  4. #include "ExtAssetData.h"
  5. #include "ExtPackageReader.h"
  6. #include "UObject/ObjectMacros.h"
  7. #include "UObject/Class.h"
  8. #include "UObject/UObjectIterator.h"
  9. #include "UObject/Package.h"
  10. #include "UObject/ObjectResource.h"
  11. #include "UObject/LinkerLoad.h"
  12. #include "UObject/CoreRedirects.h"
  13. #include "Engine/EngineTypes.h"
  14. #include "GameFramework/Actor.h"
  15. #include "Engine/World.h"
  16. #include "Commandlets/Commandlet.h"
  17. #include "Engine/SkeletalMesh.h"
  18. #include "Animation/AnimSequence.h"
  19. #include "Serialization/ArchiveCountMem.h"
  20. #include "Serialization/ArchiveReplaceObjectRef.h"
  21. #include "GameFramework/WorldSettings.h"
  22. #include "Editor.h"
  23. #include "FileHelpers.h"
  24. #include "GenericPlatform/GenericPlatformFile.h"
  25. #include "AssetRegistry/ARFilter.h"
  26. #include "AssetRegistry/AssetRegistryState.h"
  27. #include "PackageHelperFunctions.h"
  28. #include "PackageUtilityWorkers.h"
  29. #include "HAL/FileManager.h"
  30. #include "Misc/CommandLine.h"
  31. #include "Misc/FileHelper.h"
  32. #include "Misc/Paths.h"
  33. #include "Misc/ObjectThumbnail.h"
  34. #include "Misc/PackageName.h"
  35. #include "Misc/ConfigCacheIni.h"
  36. #include "CollectionManagerTypes.h"
  37. #include "ICollectionManager.h"
  38. #include "CollectionManagerModule.h"
  39. #include "EngineUtils.h"
  40. #include "Materials/Material.h"
  41. #include "Serialization/ArchiveStackTrace.h"
  42. #include "Misc/OutputDeviceHelper.h"
  43. #include "Misc/OutputDeviceFile.h"
  44. #include "UObject/UObjectThreadContext.h"
  45. #include "UObject/MetaData.h"
  46. struct FExportInfo
  47. {
  48. FObjectExport Export;
  49. int32 ExportIndex;
  50. FString PathName;
  51. FString OuterPathName;
  52. FExportInfo(FLinkerLoad* Linker, int32 InIndex)
  53. : Export(Linker->ExportMap[InIndex]), ExportIndex(InIndex)
  54. , OuterPathName(TEXT("NULL"))
  55. {
  56. PathName = Linker->GetExportPathName(ExportIndex);
  57. SetOuterPathName(Linker);
  58. }
  59. void SetOuterPathName(FLinkerLoad* Linker)
  60. {
  61. if (!Export.OuterIndex.IsNull())
  62. {
  63. OuterPathName = Linker->GetPathName(Export.OuterIndex);
  64. }
  65. }
  66. };
  67. namespace
  68. {
  69. enum EExportSortType
  70. {
  71. EXPORTSORT_ExportSize,
  72. EXPORTSORT_ExportIndex,
  73. EXPORTSORT_ObjectPathname,
  74. EXPORTSORT_OuterPathname,
  75. EXPORTSORT_MAX
  76. };
  77. struct FObjectExport_Sorter
  78. {
  79. static EExportSortType SortPriority[EXPORTSORT_MAX];
  80. // Comparison method
  81. bool operator()(const FExportInfo& A, const FExportInfo& B) const
  82. {
  83. int32 Result = 0;
  84. for (int32 PriorityType = 0; PriorityType < EXPORTSORT_MAX; PriorityType++)
  85. {
  86. switch (SortPriority[PriorityType])
  87. {
  88. case EXPORTSORT_ExportSize:
  89. Result = B.Export.SerialSize - A.Export.SerialSize;
  90. break;
  91. case EXPORTSORT_ExportIndex:
  92. Result = A.ExportIndex - B.ExportIndex;
  93. break;
  94. case EXPORTSORT_ObjectPathname:
  95. Result = A.PathName.Len() - B.PathName.Len();
  96. if (Result == 0)
  97. {
  98. Result = FCString::Stricmp(*A.PathName, *B.PathName);
  99. }
  100. break;
  101. case EXPORTSORT_OuterPathname:
  102. Result = A.OuterPathName.Len() - B.OuterPathName.Len();
  103. if (Result == 0)
  104. {
  105. Result = FCString::Stricmp(*A.OuterPathName, *B.OuterPathName);
  106. }
  107. break;
  108. case EXPORTSORT_MAX:
  109. return !!Result;
  110. }
  111. if (Result != 0)
  112. {
  113. break;
  114. }
  115. }
  116. return Result < 0;
  117. }
  118. };
  119. EExportSortType FObjectExport_Sorter::SortPriority[EXPORTSORT_MAX] =
  120. { EXPORTSORT_ExportIndex, EXPORTSORT_ExportSize, EXPORTSORT_OuterPathname, EXPORTSORT_ObjectPathname };
  121. }
  122. /** Given a package filename, creates a linker and a temporary package. The file name does not need to point to a package under the current project content folder */
  123. FLinkerLoad* CreateLinker(FUObjectSerializeContext* LoadContext, const FString& InFileName, const FString& InPackageName)
  124. {
  125. UPackage* Package = FindObjectFast<UPackage>(nullptr, *InPackageName);
  126. if (!Package)
  127. {
  128. Package = CreatePackage(*InPackageName);
  129. }
  130. uint32 LoadFlags = LOAD_NoVerify;// | LOAD_NoWarn | LOAD_Quiet;
  131. FLinkerLoad* Linker = FLinkerLoad::CreateLinker(LoadContext, Package, FPackagePath::FromLocalPath(InFileName), LOAD_NoVerify);
  132. if (Linker && Package)
  133. {
  134. Package->SetPackageFlags(PKG_ForDiffing);
  135. }
  136. return Linker;
  137. }
  138. /** Resets linkers on packages after they have finished loading */
  139. void ResetLoaders(const FString& InPackageName)
  140. {
  141. if (IsGarbageCollectingAndLockingUObjectHashTables())
  142. return;
  143. UPackage* ExistingPackage = FindObject<UPackage>(nullptr, *InPackageName, /*bool ExactClass*/true);
  144. if (ExistingPackage != nullptr)
  145. {
  146. ResetLoaders(ExistingPackage);
  147. ExistingPackage->ClearFlags(RF_WasLoaded);
  148. ExistingPackage->bHasBeenFullyLoaded = false;
  149. ExistingPackage->GetMetaData()->RemoveMetaDataOutsidePackage();
  150. }
  151. }
  152. /////////////////////////////////////////////////////////////
  153. // FExtPackageUtilities implementation
  154. //
  155. #if ECB_LEGACY
  156. bool FExtPackageUtils::ParsePackage(struct FExtAssetData& OutExtAssetData)
  157. {
  158. FString FileName = OutExtAssetData.PackageFilePath.ToString();
  159. FString PackageName = OutExtAssetData.PackageName.ToString();
  160. // Try reset the loaders for the packages we want to load so that we don't find the wrong version of the file
  161. ResetLoaders(PackageName);
  162. FLinkerLoad* Linker = nullptr;
  163. UPackage* Package = nullptr;
  164. FArchiveStackTraceReader* Reader = nullptr;
  165. {
  166. TGuardValue<bool> GuardAllowUnversionedContentInEditor(GAllowUnversionedContentInEditor, true);
  167. TGuardValue<int32> GuardAllowCookedContentInEditor(GAllowCookedDataInEditorBuilds, 1);
  168. TRefCountPtr<FUObjectSerializeContext> LoadContext(FUObjectThreadContext::Get().GetSerializeContext());
  169. BeginLoad(LoadContext);
  170. Linker = CreateLinker(LoadContext, FileName, PackageName);
  171. EndLoad(Linker ? Linker->GetSerializeContext() : LoadContext.GetReference());
  172. }
  173. if (!Linker)
  174. {
  175. OutExtAssetData.InvalidReason = FExtAssetData::EInvalidReason::FailedToLoadPackage;
  176. return false;
  177. }
  178. // Text format is not supported for now
  179. if (Linker->IsTextFormat())
  180. {
  181. OutExtAssetData.InvalidReason = FExtAssetData::EInvalidReason::TextForamtNotSupported;
  182. return false;
  183. }
  184. #if ECB_DISABLE//ECB_DEBUG
  185. // Generate package report for debugging
  186. {
  187. uint32 InfoFlags = PKGINFO_None;
  188. // names
  189. //InfoFlags |= PKGINFO_Names;
  190. // imports
  191. //InfoFlags |= PKGINFO_Imports;
  192. // exports
  193. //InfoFlags |= PKGINFO_Exports;
  194. // simple
  195. //InfoFlags |= PKGINFO_Compact;
  196. // depends
  197. InfoFlags |= PKGINFO_Depends;
  198. // paths
  199. //InfoFlags |= PKGINFO_Paths;
  200. // thumbnails
  201. InfoFlags |= PKGINFO_Thumbs;
  202. // lazy
  203. //InfoFlags |= PKGINFO_Lazy;
  204. // asset registry
  205. InfoFlags |= PKGINFO_AssetRegistry;
  206. const bool bHideOffsets = false; // Determines whether FObjectExport::SerialOffset will be included in the output.
  207. GeneratePackageReport(Linker, InfoFlags, bHideOffsets);
  208. }
  209. #endif
  210. // Parsing package
  211. {
  212. // Version Info
  213. {
  214. OutExtAssetData.FileVersionUE4 = Linker->UE4Ver();
  215. OutExtAssetData.SavedByEngineVersion = Linker->Summary.SavedByEngineVersion;
  216. OutExtAssetData.CompatibleWithEngineVersion = Linker->Summary.CompatibleWithEngineVersion;
  217. }
  218. int64 PackageFileSize = Linker->GetLoader_Unsafe()->TotalSize();
  219. // AssetRegistry
  220. if (Linker->Summary.AssetRegistryDataOffset > 0 && Linker->Summary.AssetRegistryDataOffset < PackageFileSize)
  221. {
  222. {
  223. const int32 NextOffset = Linker->Summary.WorldTileInfoDataOffset ? Linker->Summary.WorldTileInfoDataOffset : Linker->Summary.TotalHeaderSize;
  224. const int32 AssetRegistrySize = NextOffset - Linker->Summary.AssetRegistryDataOffset;
  225. //ECB_LOG(Display, TEXT("Asset Registry Size: %10i"), AssetRegistrySize);
  226. }
  227. // Seek to the AssetRegistry table of contents
  228. Linker->GetLoader_Unsafe()->Seek(Linker->Summary.AssetRegistryDataOffset);
  229. // Load the number of assets in the tag map
  230. int32 AssetCount = 0;
  231. *Linker << AssetCount;
  232. //ECB_LOG(Display, TEXT("Number of assets with Asset Registry data: %d"), AssetCount);
  233. // Only care about first asset
  234. // for (int32 AssetIdx = 0; AssetIdx < AssetCount; ++AssetIdx)
  235. if (AssetCount > 0)
  236. {
  237. // Display the asset class and path
  238. FString ObjectPath;
  239. FString ObjectClassName;
  240. int32 TagCount = 0;
  241. *Linker << ObjectPath;
  242. *Linker << ObjectClassName;
  243. *Linker << TagCount;
  244. //ECB_LOG(Display, TEXT("\t\t%s'%s' (%d Tags)"), *ObjectClassName, *ObjectPath, TagCount); // Output : StaticMesh'SM_Test_LOD0' (14 Tags)
  245. OutExtAssetData.AssetName = FName(*ObjectPath);
  246. OutExtAssetData.AssetClass = FName(*ObjectClassName);
  247. // Get all tags on this asset
  248. FAssetDataTagMap NewTagsAndValues;
  249. for (int32 TagIdx = 0; TagIdx < TagCount; ++TagIdx)
  250. {
  251. FString Key;
  252. FString Value;
  253. *Linker << Key;
  254. *Linker << Value;
  255. //ECB_LOG(Display, TEXT("\t\t\t\"%s\": \"%s\""), *Key, *Value);
  256. NewTagsAndValues.Add(FName(*Key), Value);
  257. }
  258. OutExtAssetData.TagsAndValues = FAssetDataTagMapSharedView(MoveTemp(NewTagsAndValues));
  259. }
  260. }
  261. // Thumbnail Data
  262. if (Linker->Summary.ThumbnailTableOffset > 0 && Linker->Summary.ThumbnailTableOffset < PackageFileSize)
  263. {
  264. Linker->GetLoader_Unsafe()->Seek(Linker->Summary.ThumbnailTableOffset);
  265. // Load the thumbnail count
  266. int32 ThumbnailCount = 0;
  267. *Linker << ThumbnailCount;
  268. // Load the names and file offsets for the thumbnails in this package
  269. //for (int32 CurThumbnailIndex = 0; CurThumbnailIndex < ThumbnailCount; ++CurThumbnailIndex)
  270. if (ThumbnailCount > 0)
  271. {
  272. int32 CurThumbnailIndex = 0;
  273. FString ObjectClassName;
  274. *Linker << ObjectClassName;
  275. // Object path
  276. FString ObjectPath;
  277. *Linker << ObjectPath;
  278. //ECB_LOG(Display, TEXT("\t\t%d) %s'%s' (%d thumbs)"), CurThumbnailIndex, *ObjectClassName, *ObjectPath, ThumbnailCount);
  279. bool bHaveValidClassName = !ObjectClassName.IsEmpty() && ObjectClassName != TEXT("???");
  280. if (OutExtAssetData.AssetClass == NAME_None && bHaveValidClassName)
  281. {
  282. OutExtAssetData.AssetClass = FName(*ObjectClassName);
  283. }
  284. const bool bHaveValidAssetName = !ObjectPath.IsEmpty();
  285. if (OutExtAssetData.AssetName == NAME_None && bHaveValidAssetName)
  286. {
  287. OutExtAssetData.AssetName = FName(*ObjectPath);
  288. }
  289. // File offset to image data
  290. int32 FileOffset = 0;
  291. *Linker << FileOffset;
  292. if (FileOffset > 0 && FileOffset < PackageFileSize)
  293. {
  294. OutExtAssetData.bHasThumbnail = true;
  295. }
  296. }
  297. }
  298. // Dependencies
  299. {
  300. FName LinkerName = Linker->LinkerRoot->GetFName();
  301. TSet<FName>& DependentPackages = OutExtAssetData.HardDependentPackages;
  302. for (int32 i = 0; i < Linker->ImportMap.Num(); ++i)
  303. {
  304. FObjectImport& import = Linker->ImportMap[i];
  305. FName DependentPackageName = NAME_None;
  306. if (!import.OuterIndex.IsNull())
  307. {
  308. // Find the package which contains this import. import.SourceLinker is cleared in EndLoad, so we'll need to do this manually now.
  309. FPackageIndex OutermostLinkerIndex = import.OuterIndex;
  310. for (FPackageIndex LinkerIndex = import.OuterIndex; !LinkerIndex.IsNull(); )
  311. {
  312. OutermostLinkerIndex = LinkerIndex;
  313. LinkerIndex = Linker->ImpExp(LinkerIndex).OuterIndex;
  314. }
  315. check(!OutermostLinkerIndex.IsNull());
  316. DependentPackageName = Linker->ImpExp(OutermostLinkerIndex).ObjectName;
  317. }
  318. if (DependentPackageName == NAME_None && import.ClassName == NAME_Package)
  319. {
  320. DependentPackageName = import.ObjectName;
  321. }
  322. if (DependentPackageName != NAME_None && DependentPackageName != LinkerName)
  323. {
  324. DependentPackages.Add(DependentPackageName);
  325. }
  326. if (import.ClassPackage != NAME_None && import.ClassPackage != LinkerName)
  327. {
  328. DependentPackages.Add(import.ClassPackage);
  329. }
  330. }
  331. if (DependentPackages.Num())
  332. {
  333. ECB_LOG(Display, TEXT("-----------FExtPackageUtilities::ParsePackage----------------"));
  334. ECB_LOG(Display, TEXT("\tPackages referenced by %s:"), *LinkerName.ToString());
  335. int32 i = 0;
  336. for (const FName& Dependent : DependentPackages)
  337. {
  338. ECB_LOG(Display, TEXT("\t\t%i) %s"), i++, *Dependent.ToString());
  339. }
  340. }
  341. }
  342. // Soft References
  343. if (Linker->Summary.SoftPackageReferencesCount > 0)
  344. {
  345. OutExtAssetData.SoftPackageReferencesCount = Linker->Summary.SoftPackageReferencesCount;
  346. }
  347. }
  348. // Reset linker to free memory
  349. ResetLoaders(PackageName);
  350. return true;
  351. }
  352. bool FExtPackageUtils::LoadThumbnail(const FExtAssetData& InExtAssetData, FObjectThumbnail& InOutThumbnail)
  353. {
  354. bool bThumbnailLoaded = false;
  355. FString FileName = InExtAssetData.PackageFilePath.ToString();
  356. FString PackageName = InExtAssetData.PackageName.ToString();
  357. // Try reset the loaders for the packages we want to load so that we don't find the wrong version of the file
  358. ResetLoaders(PackageName);
  359. FLinkerLoad* Linker = nullptr;
  360. {
  361. TGuardValue<bool> GuardAllowUnversionedContentInEditor(GAllowUnversionedContentInEditor, true);
  362. TGuardValue<int32> GuardAllowCookedContentInEditor(GAllowCookedDataInEditorBuilds, 1);
  363. TRefCountPtr<FUObjectSerializeContext> LoadContext(FUObjectThreadContext::Get().GetSerializeContext());
  364. BeginLoad(LoadContext);
  365. Linker = CreateLinker(LoadContext, FileName, PackageName);
  366. EndLoad(Linker ? Linker->GetSerializeContext() : LoadContext.GetReference());
  367. }
  368. if (Linker)
  369. {
  370. if (Linker->SerializeThumbnails(true) && Linker->LinkerRoot->HasThumbnailMap())
  371. {
  372. FThumbnailMap& LinkerThumbnails = Linker->LinkerRoot->AccessThumbnailMap();
  373. for (TMap<FName, FObjectThumbnail>::TIterator It(LinkerThumbnails); It; ++It)
  374. {
  375. FName& ObjectFullName = It.Key();
  376. InOutThumbnail = It.Value();
  377. //ECB_LOG(Display, TEXT("[LoadThumbnail] %s: %ix%i\t\tImage Data:%i bytes"), *ObjectFullName.ToString(), InOutThumbnail.GetImageWidth(), InOutThumbnail.GetImageHeight(), InOutThumbnail.GetCompressedDataSize());
  378. bThumbnailLoaded = true;
  379. }
  380. }
  381. ResetLoaders(PackageName);
  382. }
  383. return bThumbnailLoaded;
  384. }
  385. void FExtPackageUtils::LoadSoftReferences(const FExtAssetData& InExtAssetData, TArray<FName>& OutSoftPackageReferenceList)
  386. {
  387. ECB_LOG(Display, TEXT("[LoadSoftReferences] Soft Package References Count: %10i"), InExtAssetData.SoftPackageReferencesCount);
  388. if (InExtAssetData.SoftPackageReferencesCount <= 0)
  389. {
  390. return;
  391. }
  392. FString FileName = InExtAssetData.PackageFilePath.ToString();
  393. {
  394. FPackageReader Reader;
  395. //Reader.OpenPackageFile(*Linker->GetLoader_Unsafe());
  396. if (Reader.OpenPackageFile(FileName))//, EOpenPackageResult * OutErrorCode)
  397. {
  398. Reader.SerializeNameMap();
  399. Reader.SerializeSoftPackageReferenceList(OutSoftPackageReferenceList);
  400. for (int32 SoftIdx = 0; SoftIdx < OutSoftPackageReferenceList.Num(); ++SoftIdx)
  401. {
  402. FName SoftPackageName = OutSoftPackageReferenceList[SoftIdx];
  403. ECB_LOG(Display, TEXT("\t\t%d) %s"), SoftIdx, *SoftPackageName.ToString());
  404. }
  405. }
  406. }
  407. }
  408. #endif
  409. bool FExtPackageUtils::ParsePackageWithPackageReader(struct FExtAssetData& OutAssetData)
  410. {
  411. FString PackageFilePath = OutAssetData.PackageFilePath.ToString();
  412. FString PackageName = OutAssetData.PackageName.ToString();
  413. // Try reset the loaders for the packages we want to load so that we don't find the wrong version of the file
  414. ResetLoaders(PackageName);
  415. FExtPackageReader PackageReader;
  416. FExtPackageReader::EOpenPackageResult ErrorCode;
  417. if (!PackageReader.OpenPackageFile(PackageFilePath, &ErrorCode))
  418. {
  419. FExtAssetData::EInvalidReason InvalidReason = FExtAssetData::EInvalidReason::Unknown;
  420. switch (ErrorCode)
  421. {
  422. case FExtPackageReader::EOpenPackageResult::FailedToLoad :
  423. ECB_LOG(Display, TEXT("Failed to load %s, VersionTooOld!"), *PackageFilePath);
  424. InvalidReason = FExtAssetData::EInvalidReason::PackageFileFailedToLoad;
  425. break;
  426. case FExtPackageReader::EOpenPackageResult::VersionTooOld:
  427. ECB_LOG(Display, TEXT("Failed to open %s, VersionTooOld!"), *PackageFilePath);
  428. InvalidReason = FExtAssetData::EInvalidReason::PackageFileVersionEmptyOrTooOld;
  429. break;
  430. case FExtPackageReader::EOpenPackageResult::VersionTooNew:
  431. ECB_LOG(Display, TEXT("Failed to open %s, VersionTooNew!"), *PackageFilePath);
  432. InvalidReason = FExtAssetData::EInvalidReason::PackageFileOrCustomVersionTooNew;
  433. break;
  434. case FExtPackageReader::EOpenPackageResult::NoLoader:
  435. ECB_LOG(Display, TEXT("Failed to open %s, NoLoader!"), *PackageFilePath);
  436. break;
  437. case FExtPackageReader::EOpenPackageResult::MalformedTag:
  438. ECB_LOG(Display, TEXT("Failed to open %s, MalformedTag!"), *PackageFilePath);
  439. InvalidReason = FExtAssetData::EInvalidReason::PackageFileMalformed;
  440. break;
  441. case FExtPackageReader::EOpenPackageResult::CustomVersionMissing:
  442. ECB_LOG(Display, TEXT("Failed to open %s, CustomVersionMissing!"), *PackageFilePath);
  443. InvalidReason = FExtAssetData::EInvalidReason::PackageFileCustomVersionMissing;
  444. break;
  445. default:
  446. InvalidReason = FExtAssetData::EInvalidReason::Unknown;
  447. ECB_LOG(Display, TEXT("Failed to open %s!"), *PackageFilePath);
  448. }
  449. OutAssetData.InvalidReason = InvalidReason;
  450. OutAssetData.FileSize = PackageReader.TotalSize();
  451. ResetLoaders(PackageName);
  452. return false;
  453. }
  454. const bool bCooked = !!(PackageReader.GetPackageFlags() & PKG_FilterEditorOnly);
  455. if (bCooked)
  456. {
  457. ECB_LOG(Display, TEXT("Cooked asset is not supported: %s!"), *PackageFilePath);
  458. OutAssetData.InvalidReason = FExtAssetData::EInvalidReason::CookedPackageNotSupported;
  459. ResetLoaders(PackageName);
  460. return false;
  461. }
  462. // Version Info
  463. {
  464. OutAssetData.FileVersionUE4 = PackageReader.UEVer().ToValue();
  465. OutAssetData.SavedByEngineVersion = PackageReader.GetPackageFileSummary().SavedByEngineVersion;
  466. OutAssetData.CompatibleWithEngineVersion = PackageReader.GetPackageFileSummary().CompatibleWithEngineVersion;
  467. }
  468. // AssetRegistry
  469. {
  470. PackageReader.ReadAssetRegistryData(OutAssetData);
  471. }
  472. // Thumbnail Data
  473. {
  474. PackageReader.ReadAssetDataFromThumbnailCache(OutAssetData);
  475. }
  476. // Dependencies and soft references
  477. {
  478. PackageReader.ReadDependencyData(OutAssetData);
  479. }
  480. {
  481. OutAssetData.FileSize = PackageReader.TotalSize();
  482. }
  483. // Reset linker to free memory
  484. ResetLoaders(PackageName);
  485. return true;
  486. }
  487. bool FExtPackageUtils::LoadThumbnailWithPackageReader(const FExtAssetData& InAssetData, FObjectThumbnail& InOutThumbnail)
  488. {
  489. FString PackageFilePath = InAssetData.PackageFilePath.ToString();
  490. FString PackageName = InAssetData.PackageName.ToString();
  491. // Try reset the loaders for the packages we want to load so that we don't find the wrong version of the file
  492. ResetLoaders(PackageName);
  493. FExtPackageReader PackageReader;
  494. if (PackageReader.OpenPackageFile(PackageFilePath))
  495. {
  496. bool bResult = PackageReader.ReadThumbnail(InOutThumbnail);
  497. // Reset linker to free memory
  498. ResetLoaders(PackageName);
  499. return bResult;
  500. }
  501. // Reset linker to free memory
  502. ResetLoaders(PackageName);
  503. return false;
  504. }
  505. void FExtPackageUtils::UnloadPackage(const TArray<FString>& InFilePaths, TArray<UPackage*>* OutUnloadPackage)
  506. {
  507. for (const FString& FilePath : InFilePaths)
  508. {
  509. FString PackageName;
  510. if (FPackageName::TryConvertFilenameToLongPackageName(FilePath, PackageName))
  511. {
  512. if (UPackage* Package = FindPackage(nullptr, *PackageName))
  513. {
  514. if (OutUnloadPackage)
  515. {
  516. OutUnloadPackage->Add(Package);
  517. }
  518. // Detach the linker if loaded
  519. #if ECB_DISABLE
  520. if (!Package->IsFullyLoaded())
  521. {
  522. FlushAsyncLoading();
  523. Package->FullyLoad();
  524. }
  525. #endif
  526. ResetLoaders(PackageName);
  527. }
  528. }
  529. }
  530. }
  531. void FExtPackageUtils::GetDirectories(const FString& InBaseDirectory, TArray<FString>& OutPathList)
  532. {
  533. struct FFolderVisitor : public IPlatformFile::FDirectoryVisitor
  534. {
  535. virtual bool Visit(const TCHAR* FilenameOrDirectory, bool bIsDirectory) override
  536. {
  537. if (bIsDirectory)
  538. {
  539. Directories.Add(FilenameOrDirectory);
  540. }
  541. return true;
  542. }
  543. TArray<FString> Directories;
  544. };
  545. class FFolderStatVisitor : public IPlatformFile::FDirectoryStatVisitor
  546. {
  547. public:
  548. virtual bool Visit(const TCHAR* FilenameOrDirectory, const FFileStatData& StatData) override
  549. {
  550. if (StatData.bIsDirectory)
  551. {
  552. Directories.Add(FilenameOrDirectory);
  553. }
  554. return true;
  555. }
  556. TArray<FString> Directories;
  557. };
  558. if (IFileManager::Get().DirectoryExists(*InBaseDirectory))
  559. {
  560. FFolderVisitor FolderVisitor;
  561. //FFolderStatVisitor FolderVisitor;
  562. //IFileManager::Get().IterateDirectoryStat(*InBaseDirectory, FolderVisitor);
  563. IFileManager::Get().IterateDirectory(*InBaseDirectory, FolderVisitor);
  564. OutPathList.Append(FolderVisitor.Directories);
  565. }
  566. }
  567. void FExtPackageUtils::GetDirectoriesRecursively(const FString& InBaseDirectory, TArray<FString>& OutPathList)
  568. {
  569. struct Local
  570. {
  571. static void GetChildDirectories(const TArray<FString>& InBaseDirs, TArray<FString>& OutPathList)
  572. {
  573. for (const FString& BaseDir : InBaseDirs)
  574. {
  575. TArray<FString> ChildDirs;
  576. FExtPackageUtils::GetDirectories(BaseDir, ChildDirs);
  577. OutPathList.Append(ChildDirs);
  578. GetChildDirectories(ChildDirs, OutPathList);
  579. }
  580. }
  581. };
  582. double GetAllDirectoriesStartTime = FPlatformTime::Seconds();
  583. if (IFileManager::Get().DirectoryExists(*InBaseDirectory))
  584. {
  585. Local::GetChildDirectories({ InBaseDirectory }, OutPathList);
  586. }
  587. ECB_LOG(Display, TEXT("GetDirectoriesRecursively completed in %0.4f seconds"), FPlatformTime::Seconds() - GetAllDirectoriesStartTime);
  588. }
  589. #if ECB_LEGACY
  590. void FExtPackageUtils::GetDirectories(const FString& InBaseDirectory, TArray<FString>& OutPathList, bool bResursively)
  591. {
  592. double GetAllDirectoriesStartTime = FPlatformTime::Seconds();
  593. struct FFolderVisitor : public IPlatformFile::FDirectoryVisitor
  594. {
  595. virtual bool Visit(const TCHAR* FilenameOrDirectory, bool bIsDirectory) override
  596. {
  597. if (bIsDirectory)
  598. {
  599. Directories.Add(FilenameOrDirectory);
  600. }
  601. return true;
  602. }
  603. TArray<FString> Directories;
  604. };
  605. class FFolderStatVisitor : public IPlatformFile::FDirectoryStatVisitor
  606. {
  607. public:
  608. virtual bool Visit(const TCHAR* FilenameOrDirectory, const FFileStatData& StatData) override
  609. {
  610. if (StatData.bIsDirectory)
  611. {
  612. Directories.Add(FilenameOrDirectory);
  613. }
  614. return true;
  615. }
  616. TArray<FString> Directories;
  617. };
  618. if (IFileManager::Get().DirectoryExists(*InBaseDirectory))
  619. {
  620. //FFolderVisitor FolderVisitor;
  621. FFolderStatVisitor FolderVisitor;
  622. if (bResursively)
  623. {
  624. //IFileManager::Get().IterateDirectoryRecursively(*InBaseDirectory, FolderVisitor);
  625. IFileManager::Get().IterateDirectoryStatRecursively(*InBaseDirectory, FolderVisitor);
  626. }
  627. else
  628. {
  629. //IFileManager::Get().IterateDirectory(*InBaseDirectory, FolderVisitor);
  630. IFileManager::Get().IterateDirectoryStat(*InBaseDirectory, FolderVisitor);
  631. }
  632. OutPathList.Append(FolderVisitor.Directories);
  633. }
  634. ECB_LOG(Display, TEXT("GetAllDirectories completed in %0.4f seconds"), FPlatformTime::Seconds() - GetAllDirectoriesStartTime);
  635. }
  636. #endif
  637. void FExtPackageUtils::GetAllPackages(const FString& InRootDirectory, TArray<FString>& OutFileList, bool bResursively)
  638. {
  639. struct FPackageVisitor : public IPlatformFile::FDirectoryVisitor
  640. {
  641. virtual bool Visit(const TCHAR* FilenameOrDirectory, bool bIsDirectory) override
  642. {
  643. if (!bIsDirectory && FExtAssetSupport::IsSupportedPackageFilename(FilenameOrDirectory))
  644. {
  645. PackageFiles.Add(FilenameOrDirectory);
  646. }
  647. return true;
  648. }
  649. TArray<FString> PackageFiles;
  650. };
  651. if (IFileManager::Get().DirectoryExists(*InRootDirectory))
  652. {
  653. FPackageVisitor PackageVisitor;
  654. if (bResursively)
  655. {
  656. IFileManager::Get().IterateDirectoryRecursively(*InRootDirectory, PackageVisitor);
  657. }
  658. else
  659. {
  660. IFileManager::Get().IterateDirectory(*InRootDirectory, PackageVisitor);
  661. }
  662. OutFileList = PackageVisitor.PackageFiles;
  663. }
  664. }
  665. void FExtPackageUtils::GetAllPackages(const FName& InRootDirectory, TArray<FName>& OutFileList)
  666. {
  667. struct FPackageVisitor : public IPlatformFile::FDirectoryVisitor
  668. {
  669. virtual bool Visit(const TCHAR* FilenameOrDirectory, bool bIsDirectory) override
  670. {
  671. if (!bIsDirectory && FExtAssetSupport::IsSupportedPackageFilename(FilenameOrDirectory))
  672. {
  673. PackageFiles.Add(FilenameOrDirectory);
  674. }
  675. return true;
  676. }
  677. TArray<FName> PackageFiles;
  678. };
  679. FString BaseDir = InRootDirectory.ToString();
  680. if (IFileManager::Get().DirectoryExists(*BaseDir))
  681. {
  682. FPackageVisitor PackageVisitor;
  683. IFileManager::Get().IterateDirectory(*BaseDir, PackageVisitor);
  684. OutFileList = PackageVisitor.PackageFiles;
  685. }
  686. }
  687. bool FExtPackageUtils::HasPackages(const FString& InDirectory, bool bResursively)
  688. {
  689. struct FPackageVisitor : public IPlatformFile::FDirectoryVisitor
  690. {
  691. virtual bool Visit(const TCHAR* FilenameOrDirectory, bool bIsDirectory) override
  692. {
  693. if (!bIsDirectory && FExtAssetSupport::IsSupportedPackageFilename(FilenameOrDirectory))
  694. {
  695. bFoundPackage = true;
  696. return false;
  697. }
  698. return true;
  699. }
  700. bool bFoundPackage = false;
  701. };
  702. if (IFileManager::Get().DirectoryExists(*InDirectory))
  703. {
  704. FPackageVisitor PackageVisitor;
  705. if (bResursively)
  706. {
  707. IFileManager::Get().IterateDirectoryRecursively(*InDirectory, PackageVisitor);
  708. }
  709. else
  710. {
  711. IFileManager::Get().IterateDirectory(*InDirectory, PackageVisitor);
  712. }
  713. return PackageVisitor.bFoundPackage;
  714. }
  715. return false;
  716. }
  717. bool FExtPackageUtils::DoesPackageExist(const FString PackageName)
  718. {
  719. bool bPackageExist = false;
  720. // Script package should be loaded
  721. // todo: or skip if it's soft referenced ?
  722. if (FPackageName::IsScriptPackage(PackageName))
  723. {
  724. bPackageExist = NULL != FindPackage(NULL, *PackageName);
  725. if (!bPackageExist) // Not found, double check if redirected
  726. {
  727. const FCoreRedirect* ValueRedirect = nullptr;
  728. FCoreRedirectObjectName OldPackageName(PackageName);
  729. FCoreRedirectObjectName NewPackageName;
  730. bPackageExist = FCoreRedirects::RedirectNameAndValues(ECoreRedirectFlags::Type_Package, OldPackageName, NewPackageName, &ValueRedirect);
  731. }
  732. }
  733. else
  734. {
  735. // assume it's long package name
  736. bPackageExist = FPackageName::DoesPackageExist(PackageName);
  737. }
  738. return bPackageExist;
  739. }
  740. // PackageUtilities.cpp
  741. void FExtPackageUtils::GeneratePackageReport(FLinkerLoad* Linker, uint32 InfoFlags, bool bHideOffsets)
  742. {
  743. check(Linker);
  744. if (Linker->IsTextFormat())
  745. {
  746. ECB_LOG(Warning, TEXT("\tPackageReports are not currently supported for text based assets"));
  747. return;
  748. }
  749. // Display information about the package.
  750. FName LinkerName = Linker->LinkerRoot->GetFName();
  751. // Display summary info.
  752. ECB_LOG(Display, TEXT("********************************************"));
  753. ECB_LOG(Display, TEXT("Package '%s' Summary"), *LinkerName.ToString());
  754. ECB_LOG(Display, TEXT("--------------------------------------------"));
  755. ECB_LOG(Display, TEXT("\t Filename: %s"), *Linker->GetPackagePath().GetLocalFullPath());
  756. ECB_LOG(Display, TEXT("\t File Version: %i"), Linker->UEVer().ToValue());
  757. ECB_LOG(Display, TEXT("\t Engine Version: %s"), *Linker->Summary.SavedByEngineVersion.ToString());
  758. ECB_LOG(Display, TEXT("\t Compat Version: %s"), *Linker->Summary.CompatibleWithEngineVersion.ToString());
  759. ECB_LOG(Display, TEXT("\t PackageFlags: %X"), Linker->Summary.GetPackageFlags());
  760. ECB_LOG(Display, TEXT("\t NameCount: %d"), Linker->Summary.NameCount);
  761. ECB_LOG(Display, TEXT("\t NameOffset: %d"), Linker->Summary.NameOffset);
  762. ECB_LOG(Display, TEXT("\t ImportCount: %d"), Linker->Summary.ImportCount);
  763. ECB_LOG(Display, TEXT("\t ImportOffset: %d"), Linker->Summary.ImportOffset);
  764. ECB_LOG(Display, TEXT("\t ExportCount: %d"), Linker->Summary.ExportCount);
  765. ECB_LOG(Display, TEXT("\t ExportOffset: %d"), Linker->Summary.ExportOffset);
  766. ECB_LOG(Display, TEXT("\tCompression Flags: %X"), Linker->Summary.CompressionFlags);
  767. ECB_LOG(Display, TEXT("\t Custom Versions:\n%s"), *Linker->Summary.GetCustomVersionContainer().ToString("\t\t"));
  768. {
  769. PRAGMA_DISABLE_DEPRECATION_WARNINGS
  770. ECB_LOG(Display, TEXT("\t Guid: %s"), *Linker->Summary.Guid.ToString());
  771. PRAGMA_ENABLE_DEPRECATION_WARNINGS
  772. }
  773. ECB_LOG(Display, TEXT("\t PersistentGuid: %s"), *Linker->Summary.PersistentGuid.ToString());
  774. ECB_LOG(Display, TEXT("\t Generations:"));
  775. for (int32 i = 0; i < Linker->Summary.Generations.Num(); ++i)
  776. {
  777. const FGenerationInfo& generationInfo = Linker->Summary.Generations[i];
  778. ECB_LOG(Display, TEXT("\t\t\t%d) ExportCount=%d, NameCount=%d "), i, generationInfo.ExportCount, generationInfo.NameCount);
  779. }
  780. if ((InfoFlags & PKGINFO_Names) != 0)
  781. {
  782. ECB_LOG(Display, TEXT("--------------------------------------------"));
  783. ECB_LOG(Display, TEXT("Name Map"));
  784. ECB_LOG(Display, TEXT("========"));
  785. for (int32 i = 0; i < Linker->NameMap.Num(); ++i)
  786. {
  787. FName name = FName::CreateFromDisplayId(Linker->NameMap[i], 0);
  788. ECB_LOG(Display, TEXT("\t%d: Name '%s' [Internal: %s, %d]"), i, *name.ToString(), *name.GetPlainNameString(), name.GetNumber());
  789. //ECB_LOG(Display, TEXT("\t%d: Name '%s' Comparison Index %d Display Index %d [Internal: %s, %d]"), i, *name.ToString(), name.GetComparisonIndex().ToUnstableInt(), name.GetDisplayIndex().ToUnstableInt(), *name.GetPlainNameString(), name.GetNumber());
  790. }
  791. }
  792. // if we _only_ want name info, skip this part completely
  793. if (InfoFlags != PKGINFO_Names)
  794. {
  795. if ((InfoFlags & PKGINFO_Imports) != 0)
  796. {
  797. ECB_LOG(Display, TEXT("--------------------------------------------"));
  798. ECB_LOG(Display, TEXT("Import Map"));
  799. ECB_LOG(Display, TEXT("=========="));
  800. }
  801. TArray<FName> DependentPackages;
  802. for (int32 i = 0; i < Linker->ImportMap.Num(); ++i)
  803. {
  804. FObjectImport& import = Linker->ImportMap[i];
  805. FName PackageName = NAME_None;
  806. FName OuterName = NAME_None;
  807. if (!import.OuterIndex.IsNull())
  808. {
  809. if ((InfoFlags & PKGINFO_Paths) != 0)
  810. {
  811. OuterName = *Linker->GetPathName(import.OuterIndex);
  812. }
  813. else
  814. {
  815. OuterName = Linker->ImpExp(import.OuterIndex).ObjectName;
  816. }
  817. // Find the package which contains this import. import.SourceLinker is cleared in EndLoad, so we'll need to do this manually now.
  818. FPackageIndex OutermostLinkerIndex = import.OuterIndex;
  819. for (FPackageIndex LinkerIndex = import.OuterIndex; !LinkerIndex.IsNull(); )
  820. {
  821. OutermostLinkerIndex = LinkerIndex;
  822. LinkerIndex = Linker->ImpExp(LinkerIndex).OuterIndex;
  823. }
  824. check(!OutermostLinkerIndex.IsNull());
  825. PackageName = Linker->ImpExp(OutermostLinkerIndex).ObjectName;
  826. }
  827. if ((InfoFlags & PKGINFO_Imports) != 0)
  828. {
  829. ECB_LOG(Display, TEXT("\t*************************"));
  830. ECB_LOG(Display, TEXT("\tImport %d: '%s'"), i, *import.ObjectName.ToString());
  831. ECB_LOG(Display, TEXT("\t\t Outer: '%s' (%d)"), *OuterName.ToString(), import.OuterIndex.ForDebugging());
  832. ECB_LOG(Display, TEXT("\t\t Package: '%s'"), *PackageName.ToString());
  833. ECB_LOG(Display, TEXT("\t\t Class: '%s'"), *import.ClassName.ToString());
  834. ECB_LOG(Display, TEXT("\t\tClassPackage: '%s'"), *import.ClassPackage.ToString());
  835. ECB_LOG(Display, TEXT("\t\t XObject: %s"), import.XObject ? TEXT("VALID") : TEXT("NULL"));
  836. ECB_LOG(Display, TEXT("\t\t SourceIndex: %d"), import.SourceIndex);
  837. // dump depends info
  838. if ((InfoFlags & PKGINFO_Depends) != 0)
  839. {
  840. ECB_LOG(Display, TEXT("\t\t All Depends:"));
  841. TSet<FDependencyRef> AllDepends;
  842. Linker->GatherImportDependencies(i, AllDepends);
  843. int32 DependsIndex = 0;
  844. for (TSet<FDependencyRef>::TConstIterator It(AllDepends); It; ++It)
  845. {
  846. const FDependencyRef& Ref = *It;
  847. if (Ref.Linker)
  848. {
  849. ECB_LOG(Display, TEXT("\t\t\t%i) %s"), DependsIndex++, *Ref.Linker->GetExportFullName(Ref.ExportIndex));
  850. }
  851. else
  852. {
  853. ECB_LOG(Display, TEXT("\t\t\t%i) NULL"), DependsIndex++);
  854. }
  855. }
  856. }
  857. }
  858. if (PackageName == NAME_None && import.ClassName == NAME_Package)
  859. {
  860. PackageName = import.ObjectName;
  861. }
  862. if (PackageName != NAME_None && PackageName != LinkerName)
  863. {
  864. DependentPackages.AddUnique(PackageName);
  865. }
  866. if (import.ClassPackage != NAME_None && import.ClassPackage != LinkerName)
  867. {
  868. DependentPackages.AddUnique(import.ClassPackage);
  869. }
  870. }
  871. if (DependentPackages.Num())
  872. {
  873. ECB_LOG(Display, TEXT("--------------------------------------------"));
  874. ECB_LOG(Display, TEXT("\tPackages referenced by %s:"), *LinkerName.ToString());
  875. for (int32 i = 0; i < DependentPackages.Num(); i++)
  876. {
  877. ECB_LOG(Display, TEXT("\t\t%i) %s"), i, *DependentPackages[i].ToString());
  878. }
  879. }
  880. }
  881. if ((InfoFlags & PKGINFO_Exports) != 0)
  882. {
  883. ECB_LOG(Display, TEXT("--------------------------------------------"));
  884. ECB_LOG(Display, TEXT("Export Map"));
  885. ECB_LOG(Display, TEXT("=========="));
  886. TArray<FExportInfo> SortedExportMap;
  887. SortedExportMap.Empty(Linker->ExportMap.Num());
  888. for (int32 i = 0; i < Linker->ExportMap.Num(); ++i)
  889. {
  890. new(SortedExportMap) FExportInfo(Linker, i);
  891. }
  892. FString SortingParms;
  893. if (FParse::Value(FCommandLine::Get(), TEXT("SORT="), SortingParms))
  894. {
  895. TArray<FString> SortValues;
  896. SortingParms.ParseIntoArray(SortValues, TEXT(","), true);
  897. for (int32 i = 0; i < EXPORTSORT_MAX; i++)
  898. {
  899. if (i < SortValues.Num())
  900. {
  901. const FString Value = SortValues[i];
  902. if (Value == TEXT("index"))
  903. {
  904. FObjectExport_Sorter::SortPriority[i] = EXPORTSORT_ExportIndex;
  905. }
  906. else if (Value == TEXT("size"))
  907. {
  908. FObjectExport_Sorter::SortPriority[i] = EXPORTSORT_ExportSize;
  909. }
  910. else if (Value == TEXT("name"))
  911. {
  912. FObjectExport_Sorter::SortPriority[i] = EXPORTSORT_ObjectPathname;
  913. }
  914. else if (Value == TEXT("outer"))
  915. {
  916. FObjectExport_Sorter::SortPriority[i] = EXPORTSORT_OuterPathname;
  917. }
  918. }
  919. else
  920. {
  921. FObjectExport_Sorter::SortPriority[i] = EXPORTSORT_MAX;
  922. }
  923. }
  924. }
  925. SortedExportMap.Sort(FObjectExport_Sorter());
  926. if ((InfoFlags & PKGINFO_Compact) == 0)
  927. {
  928. for (const auto& ExportInfo : SortedExportMap)
  929. {
  930. ECB_LOG(Display, TEXT("\t*************************"));
  931. const FObjectExport& Export = ExportInfo.Export;
  932. ECB_LOG(Display, TEXT("\tExport %d: '%s'"), ExportInfo.ExportIndex, *Export.ObjectName.ToString());
  933. // find the name of this object's class
  934. FPackageIndex ClassIndex = Export.ClassIndex;
  935. FName ClassName = ClassIndex.IsNull() ? FName(NAME_Class) : Linker->ImpExp(ClassIndex).ObjectName;
  936. // find the name of this object's parent...for UClasses, this will be the parent class
  937. // for UFunctions, this will be the SuperFunction, if it exists, etc.
  938. FString ParentName;
  939. if (!Export.SuperIndex.IsNull())
  940. {
  941. if ((InfoFlags & PKGINFO_Paths) != 0)
  942. {
  943. ParentName = *Linker->GetPathName(Export.SuperIndex);
  944. }
  945. else
  946. {
  947. ParentName = Linker->ImpExp(Export.SuperIndex).ObjectName.ToString();
  948. }
  949. }
  950. // find the name of this object's parent...for UClasses, this will be the parent class
  951. // for UFunctions, this will be the SuperFunction, if it exists, etc.
  952. FString TemplateName;
  953. if (!Export.TemplateIndex.IsNull())
  954. {
  955. if ((InfoFlags & PKGINFO_Paths) != 0)
  956. {
  957. TemplateName = *Linker->GetPathName(Export.TemplateIndex);
  958. }
  959. else
  960. {
  961. TemplateName = Linker->ImpExp(Export.TemplateIndex).ObjectName.ToString();
  962. }
  963. }
  964. // find the name of this object's Outer. For UClasses, this will generally be the
  965. // top-level package itself. For properties, a UClass, etc.
  966. FString OuterName;
  967. if (!Export.OuterIndex.IsNull())
  968. {
  969. if ((InfoFlags & PKGINFO_Paths) != 0)
  970. {
  971. OuterName = *Linker->GetPathName(Export.OuterIndex);
  972. }
  973. else
  974. {
  975. OuterName = Linker->ImpExp(Export.OuterIndex).ObjectName.ToString();
  976. }
  977. }
  978. ECB_LOG(Display, TEXT("\t\t Class: '%s' (%i)"), *ClassName.ToString(), ClassIndex.ForDebugging());
  979. ECB_LOG(Display, TEXT("\t\t Parent: '%s' (%d)"), *ParentName, Export.SuperIndex.ForDebugging());
  980. ECB_LOG(Display, TEXT("\t\t Template: '%s' (%d)"), *TemplateName, Export.TemplateIndex.ForDebugging());
  981. ECB_LOG(Display, TEXT("\t\t Outer: '%s' (%d)"), *OuterName, Export.OuterIndex.ForDebugging());
  982. ECB_LOG(Display, TEXT("\t\t ObjectFlags: 0x%08X"), (uint32)Export.ObjectFlags);
  983. ECB_LOG(Display, TEXT("\t\t Size: %d"), Export.SerialSize);
  984. if (!bHideOffsets)
  985. {
  986. ECB_LOG(Display, TEXT("\t\t Offset: %d"), Export.SerialOffset);
  987. }
  988. ECB_LOG(Display, TEXT("\t\t Object: %s"), Export.Object ? TEXT("VALID") : TEXT("NULL"));
  989. if (!bHideOffsets)
  990. {
  991. ECB_LOG(Display, TEXT("\t\t HashNext: %d"), Export.HashNext);
  992. }
  993. ECB_LOG(Display, TEXT("\t\t bNotForClient: %d"), Export.bNotForClient);
  994. ECB_LOG(Display, TEXT("\t\t bNotForServer: %d"), Export.bNotForServer);
  995. // dump depends info
  996. if (InfoFlags & PKGINFO_Depends)
  997. {
  998. if (ExportInfo.ExportIndex < Linker->DependsMap.Num())
  999. {
  1000. TArray<FPackageIndex>& Depends = Linker->DependsMap[ExportInfo.ExportIndex];
  1001. ECB_LOG(Display, TEXT("\t\t DependsMap:"));
  1002. for (int32 DependsIndex = 0; DependsIndex < Depends.Num(); DependsIndex++)
  1003. {
  1004. ECB_LOG(Display, TEXT("\t\t\t%i) %s (%i)"),
  1005. DependsIndex,
  1006. *Linker->GetFullImpExpName(Depends[DependsIndex]),
  1007. Depends[DependsIndex].ForDebugging()
  1008. );
  1009. }
  1010. TSet<FDependencyRef> AllDepends;
  1011. Linker->GatherExportDependencies(ExportInfo.ExportIndex, AllDepends);
  1012. ECB_LOG(Display, TEXT("\t\t All Depends:"));
  1013. int32 DependsIndex = 0;
  1014. for (TSet<FDependencyRef>::TConstIterator It(AllDepends); It; ++It)
  1015. {
  1016. const FDependencyRef& Ref = *It;
  1017. if (Ref.Linker)
  1018. {
  1019. ECB_LOG(Display, TEXT("\t\t\t%i) %s (%i)"),
  1020. DependsIndex++,
  1021. *Ref.Linker->GetExportFullName(Ref.ExportIndex),
  1022. Ref.ExportIndex);
  1023. }
  1024. else
  1025. {
  1026. ECB_LOG(Display, TEXT("\t\t\t%i) NULL (%i)"),
  1027. DependsIndex++,
  1028. Ref.ExportIndex);
  1029. }
  1030. }
  1031. }
  1032. }
  1033. }
  1034. }
  1035. else
  1036. {
  1037. for (const auto& ExportInfo : SortedExportMap)
  1038. {
  1039. const FObjectExport& Export = ExportInfo.Export;
  1040. ECB_LOG(Display, TEXT(" %8i %10i %32s %s"), ExportInfo.ExportIndex, Export.SerialSize,
  1041. *(Linker->GetExportClassName(ExportInfo.ExportIndex).ToString()),
  1042. (InfoFlags & PKGINFO_Paths) != 0 ? *Linker->GetExportPathName(ExportInfo.ExportIndex) : *Export.ObjectName.ToString());
  1043. }
  1044. }
  1045. }
  1046. if ((InfoFlags & PKGINFO_Text) != 0)
  1047. {
  1048. ECB_LOG(Display, TEXT("--------------------------------------------"));
  1049. ECB_LOG(Display, TEXT("Gatherable Text Data Map"));
  1050. ECB_LOG(Display, TEXT("=========="));
  1051. if (Linker->SerializeGatherableTextDataMap(true))
  1052. {
  1053. ECB_LOG(Display, TEXT("Number of Text Data Entries: %d"), Linker->GatherableTextDataMap.Num());
  1054. for (int32 i = 0; i < Linker->GatherableTextDataMap.Num(); ++i)
  1055. {
  1056. const FGatherableTextData& GatherableTextData = Linker->GatherableTextDataMap[i];
  1057. ECB_LOG(Display, TEXT("Entry %d:"), 1 + i);
  1058. ECB_LOG(Display, TEXT("\t String: %s"), *GatherableTextData.SourceData.SourceString.ReplaceCharWithEscapedChar());
  1059. ECB_LOG(Display, TEXT("\tNamespace: %s"), *GatherableTextData.NamespaceName);
  1060. ECB_LOG(Display, TEXT("\t Key(s): %d"), GatherableTextData.SourceSiteContexts.Num());
  1061. for (const FTextSourceSiteContext& TextSourceSiteContext : GatherableTextData.SourceSiteContexts)
  1062. {
  1063. ECB_LOG(Display, TEXT("\t\t%s from %s"), *TextSourceSiteContext.KeyName, *TextSourceSiteContext.SiteDescription);
  1064. }
  1065. }
  1066. }
  1067. else
  1068. {
  1069. if (Linker->Summary.GatherableTextDataOffset > 0)
  1070. {
  1071. ECB_LOG(Warning, TEXT("Failed to load gatherable text data for package %s!"), *LinkerName.ToString());
  1072. }
  1073. }
  1074. }
  1075. if ((InfoFlags & PKGINFO_Thumbs) != 0)
  1076. {
  1077. ECB_LOG(Display, TEXT("--------------------------------------------"));
  1078. ECB_LOG(Display, TEXT("Thumbnail Data"));
  1079. ECB_LOG(Display, TEXT("=========="));
  1080. if (Linker->SerializeThumbnails(true))
  1081. {
  1082. if (Linker->LinkerRoot->HasThumbnailMap())
  1083. {
  1084. FThumbnailMap& LinkerThumbnails = Linker->LinkerRoot->AccessThumbnailMap();
  1085. int32 MaxObjectNameSize = 0;
  1086. for (TMap<FName, FObjectThumbnail>::TIterator It(LinkerThumbnails); It; ++It)
  1087. {
  1088. FName& ObjectPathName = It.Key();
  1089. MaxObjectNameSize = FMath::Max(MaxObjectNameSize, ObjectPathName.ToString().Len());
  1090. }
  1091. int32 ThumbIdx = 0;
  1092. for (TMap<FName, FObjectThumbnail>::TIterator It(LinkerThumbnails); It; ++It)
  1093. {
  1094. FName& ObjectFullName = It.Key();
  1095. FObjectThumbnail& Thumb = It.Value();
  1096. ECB_LOG(Display, TEXT("\t\t%i) %*s: %ix%i\t\tImage Data:%i bytes"), ThumbIdx++, MaxObjectNameSize, *ObjectFullName.ToString(), Thumb.GetImageWidth(), Thumb.GetImageHeight(), Thumb.GetCompressedDataSize());
  1097. }
  1098. }
  1099. else
  1100. {
  1101. ECB_LOG(Warning, TEXT("%s has no thumbnail map!"), *LinkerName.ToString());
  1102. }
  1103. }
  1104. else
  1105. {
  1106. if (Linker->Summary.ThumbnailTableOffset > 0)
  1107. {
  1108. ECB_LOG(Warning, TEXT("Failed to load thumbnails for package %s!"), *LinkerName.ToString());
  1109. }
  1110. }
  1111. }
  1112. if ((InfoFlags & PKGINFO_Lazy) != 0)
  1113. {
  1114. ECB_LOG(Display, TEXT("--------------------------------------------"));
  1115. ECB_LOG(Display, TEXT("Lazy Pointer Data"));
  1116. ECB_LOG(Display, TEXT("==============="));
  1117. }
  1118. if ((InfoFlags & PKGINFO_AssetRegistry) != 0)
  1119. {
  1120. ECB_LOG(Display, TEXT("--------------------------------------------"));
  1121. {
  1122. const int32 NextOffset = Linker->Summary.WorldTileInfoDataOffset ? Linker->Summary.WorldTileInfoDataOffset : Linker->Summary.TotalHeaderSize;
  1123. const int32 AssetRegistrySize = NextOffset - Linker->Summary.AssetRegistryDataOffset;
  1124. ECB_LOG(Display, TEXT("Asset Registry Size: %10i"), AssetRegistrySize);
  1125. }
  1126. ECB_LOG(Display, TEXT("Asset Registry Data"));
  1127. ECB_LOG(Display, TEXT("=========="));
  1128. if (Linker->Summary.AssetRegistryDataOffset > 0)
  1129. {
  1130. // Seek to the AssetRegistry table of contents
  1131. Linker->GetLoader_Unsafe()->Seek(Linker->Summary.AssetRegistryDataOffset);
  1132. TArray<FAssetData*> AssetDatas;
  1133. UE::AssetRegistry::EReadPackageDataMainErrorCode ErrorCode;
  1134. int64 DependencyDataOffset;
  1135. UE::AssetRegistry::ReadPackageDataMain(*Linker->GetLoader_Unsafe(), LinkerName.ToString(), Linker->Summary, DependencyDataOffset, AssetDatas, ErrorCode);
  1136. ECB_LOG(Display, TEXT("Number of assets with Asset Registry data: %d"), AssetDatas.Num());
  1137. // If there are any Asset Registry tags, print them
  1138. int AssetIdx = 0;
  1139. for (FAssetData* AssetData : AssetDatas)
  1140. {
  1141. // Display the asset class and path
  1142. ECB_LOG(Display, TEXT("\t\t%d) %s'%s' (%d Tags)"), AssetIdx++, *AssetData->AssetClassPath.ToString(), *AssetData->GetSoftObjectPath().ToString(), AssetData->TagsAndValues.Num());
  1143. // Display all tags on the asset
  1144. for (const TPair<FName, FAssetTagValueRef>& Pair : AssetData->TagsAndValues)
  1145. {
  1146. ECB_LOG(Display, TEXT("\t\t\t\"%s\": \"%s\""), *Pair.Key.ToString(), *Pair.Value.AsString());
  1147. }
  1148. delete AssetData;
  1149. }
  1150. }
  1151. }
  1152. }
  1153. #if ECB_LEGACY
  1154. bool FExtPackageUtilities::LoadThumbnailsFromPackage(const FString& InPackageFileName, const TSet< FName >& InObjectFullNames, FThumbnailMap& InOutThumbnails)
  1155. {
  1156. // Create a file reader to load the file
  1157. TUniquePtr< FArchive > FileReader(IFileManager::Get().CreateFileReader(*InPackageFileName));
  1158. if (FileReader == nullptr)
  1159. {
  1160. // Couldn't open the file
  1161. return false;
  1162. }
  1163. // Read package file summary from the file
  1164. FPackageFileSummary FileSummary;
  1165. (*FileReader) << FileSummary;
  1166. // Make sure this is indeed a package
  1167. if (FileSummary.Tag != PACKAGE_FILE_TAG || FileReader->IsError())
  1168. {
  1169. // Unrecognized or malformed package file
  1170. return false;
  1171. }
  1172. // Does the package contains a thumbnail table?
  1173. if (FileSummary.ThumbnailTableOffset == 0)
  1174. {
  1175. // No thumbnails to be loaded
  1176. return false;
  1177. }
  1178. // Seek the the part of the file where the thumbnail table lives
  1179. FileReader->Seek(FileSummary.ThumbnailTableOffset);
  1180. int32 LastFileOffset = -1;
  1181. // Load the thumbnail table of contents
  1182. TMap< FName, int32 > ObjectNameToFileOffsetMap;
  1183. {
  1184. // Load the thumbnail count
  1185. int32 ThumbnailCount = 0;
  1186. *FileReader << ThumbnailCount;
  1187. // Load the names and file offsets for the thumbnails in this package
  1188. for (int32 CurThumbnailIndex = 0; CurThumbnailIndex < ThumbnailCount; ++CurThumbnailIndex)
  1189. {
  1190. bool bHaveValidClassName = false;
  1191. FString ObjectClassName;
  1192. *FileReader << ObjectClassName;
  1193. // Object path
  1194. FString ObjectPathWithoutPackageName;
  1195. *FileReader << ObjectPathWithoutPackageName;
  1196. FString ObjectPath;
  1197. // handle UPackage thumbnails differently from usual assets
  1198. if (ObjectClassName == UPackage::StaticClass()->GetName())
  1199. {
  1200. ObjectPath = ObjectPathWithoutPackageName;
  1201. }
  1202. else
  1203. {
  1204. // ObjectPath = (FPackageName::FilenameToLongPackageName(InPackageFileName) + TEXT(".") + ObjectPathWithoutPackageName);
  1205. ObjectPath = FPaths::Combine(TEXT("/Temp"), *FPaths::GetPath(InPackageFileName.Mid(InPackageFileName.Find(TEXT(":"), ESearchCase::CaseSensitive) + 1)), *FPaths::GetBaseFilename(InPackageFileName));
  1206. }
  1207. // If the thumbnail was stored with a missing class name ("???") when we'll catch that here
  1208. if (ObjectClassName.Len() > 0 && ObjectClassName != TEXT("???"))
  1209. {
  1210. bHaveValidClassName = true;
  1211. }
  1212. else
  1213. {
  1214. // Class name isn't valid. Probably legacy data. We'll try to fix it up below.
  1215. }
  1216. if (!bHaveValidClassName)
  1217. {
  1218. // Try to figure out a class name based on input assets. This should really only be needed
  1219. // for packages saved by older versions of the editor (VER_CONTENT_BROWSER_FULL_NAMES)
  1220. for (TSet<FName>::TConstIterator It(InObjectFullNames); It; ++It)
  1221. {
  1222. const FName& CurObjectFullNameFName = *It;
  1223. FString CurObjectFullName;
  1224. CurObjectFullNameFName.ToString(CurObjectFullName);
  1225. if (CurObjectFullName.EndsWith(ObjectPath))
  1226. {
  1227. // Great, we found a path that matches -- we just need to add that class name
  1228. const int32 FirstSpaceIndex = CurObjectFullName.Find(TEXT(" "));
  1229. check(FirstSpaceIndex != -1);
  1230. ObjectClassName = CurObjectFullName.Left(FirstSpaceIndex);
  1231. // We have a useful class name now!
  1232. bHaveValidClassName = true;
  1233. break;
  1234. }
  1235. }
  1236. }
  1237. // File offset to image data
  1238. int32 FileOffset = 0;
  1239. *FileReader << FileOffset;
  1240. if (FileOffset != -1 && FileOffset < LastFileOffset)
  1241. {
  1242. ECB_LOG(Warning, TEXT("Loaded thumbnail '%s' out of order!: FileOffset:%i LastFileOffset:%i"), *ObjectPath, FileOffset, LastFileOffset);
  1243. }
  1244. if (bHaveValidClassName)
  1245. {
  1246. // Create a full name string with the object's class and fully qualified path
  1247. const FString ObjectFullName(ObjectClassName + TEXT(" ") + ObjectPath);
  1248. // Add to our map
  1249. ObjectNameToFileOffsetMap.Add(FName(*ObjectFullName), FileOffset);
  1250. }
  1251. else
  1252. {
  1253. // Oh well, we weren't able to fix the class name up. We won't bother making this
  1254. // thumbnail available to load
  1255. }
  1256. }
  1257. }
  1258. // @todo CB: Should sort the thumbnails to load by file offset to reduce seeks [reviewed; pre-qa release]
  1259. for (TSet<FName>::TConstIterator It(InObjectFullNames); It; ++It)
  1260. {
  1261. const FName& CurObjectFullName = *It;
  1262. // Do we have this thumbnail in the file?
  1263. // @todo thumbnails: Backwards compat
  1264. const int32* pFileOffset = ObjectNameToFileOffsetMap.Find(CurObjectFullName);
  1265. if (pFileOffset != NULL)
  1266. {
  1267. // Seek to the location in the file with the image data
  1268. FileReader->Seek(*pFileOffset);
  1269. // Load the image data
  1270. FObjectThumbnail LoadedThumbnail;
  1271. LoadedThumbnail.Serialize(*FileReader);
  1272. // Store the data!
  1273. InOutThumbnails.Add(CurObjectFullName, LoadedThumbnail);
  1274. }
  1275. else
  1276. {
  1277. // Couldn't find the requested thumbnail in the file!
  1278. }
  1279. }
  1280. return true;
  1281. }
  1282. bool FExtPackageUtilities::ConditionallyLoadThumbnailsFromPackage(const FString& InPackageFileName, const TSet< FName >& InObjectFullNames, FThumbnailMap& InOutThumbnails)
  1283. {
  1284. // First check to see if any of the requested thumbnails are already in memory
  1285. TSet< FName > ObjectFullNamesToLoad;
  1286. ObjectFullNamesToLoad.Empty(InObjectFullNames.Num());
  1287. #if ECB_LEGACY
  1288. for (TSet<FName>::TConstIterator It(InObjectFullNames); It; ++It)
  1289. {
  1290. const FName& CurObjectFullName = *It;
  1291. // Do we have this thumbnail in our cache already?
  1292. // @todo thumbnails: Backwards compat
  1293. const FObjectThumbnail* FoundThumbnail = FindCachedThumbnailInPackage(InPackageFileName, CurObjectFullName);
  1294. if (FoundThumbnail != NULL)
  1295. {
  1296. // Great, we already have this thumbnail in memory! Copy it to our output map.
  1297. InOutThumbnails.Add(CurObjectFullName, *FoundThumbnail);
  1298. }
  1299. else
  1300. {
  1301. ObjectFullNamesToLoad.Add(CurObjectFullName);
  1302. }
  1303. }
  1304. // Did we find all of the requested thumbnails in our cache?
  1305. if (ObjectFullNamesToLoad.Num() == 0)
  1306. {
  1307. // Done!
  1308. return true;
  1309. }
  1310. #endif
  1311. // OK, go ahead and load the remaining thumbnails!
  1312. return LoadThumbnailsFromPackage(InPackageFileName, ObjectFullNamesToLoad, InOutThumbnails);
  1313. }
  1314. #endif