ExtPackageReader.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. // Copyright 2017-2021 marynate. All Rights Reserved.
  2. #pragma once
  3. #include "CoreMinimal.h"
  4. #include "Serialization/ArchiveUObject.h"
  5. #include "UObject/ObjectResource.h"
  6. #include "UObject/PackageFileSummary.h"
  7. #include "UObject/Linker.h"
  8. #include "AssetRegistry/AssetData.h"
  9. struct FExtAssetData;
  10. class FExtPackageDependencyData;
  11. class FObjectThumbnail;
  12. /**
  13. * Class for read and parse package file
  14. */
  15. class FExtPackageReader : public FArchiveUObject
  16. {
  17. public:
  18. FExtPackageReader();
  19. ~FExtPackageReader();
  20. enum class EOpenPackageResult : uint8
  21. {
  22. Success,
  23. NoLoader,
  24. MalformedTag,
  25. VersionTooOld,
  26. VersionTooNew,
  27. CustomVersionMissing,
  28. CustomVersionInvalid,
  29. Unversioned,
  30. FailedToLoad
  31. };
  32. /** Creates a loader for the filename */
  33. bool OpenPackageFile(const FString& PackageFilename, EOpenPackageResult* OutErrorCode = nullptr);
  34. bool OpenPackageFile(FArchive* Loader, EOpenPackageResult* OutErrorCode = nullptr);
  35. bool OpenPackageFile(EOpenPackageResult* OutErrorCode = nullptr);
  36. /** Reads information from the asset registry data table and converts it to FAssetData */
  37. bool ReadAssetRegistryData(TArray<FAssetData*>& AssetDataList);
  38. /** Attempts to get the class name of an object from the thumbnail cache for packages older than VER_UE4_ASSET_REGISTRY_TAGS */
  39. bool ReadAssetDataFromThumbnailCache(TArray<FAssetData*>& AssetDataList);
  40. /** Creates asset data reconstructing all the required data from cooked package info */
  41. bool ReadAssetRegistryDataIfCookedPackage(TArray<FAssetData*>& AssetDataList, TArray<FString>& CookedPackageNamesWithoutAssetData);
  42. /** Reads information used by the dependency graph */
  43. bool ReadDependencyData(FExtPackageDependencyData& OutDependencyData);
  44. /** Serializers for different package maps */
  45. bool SerializeNameMap();
  46. bool SerializeImportMap();
  47. bool SerializeExportMap();
  48. bool SerializeImportedClasses(const TArray<FObjectImport>& InImportMap, TArray<FName>& OutClassNames);
  49. bool SerializeSoftPackageReferenceList(TArray<FName>& OutSoftPackageReferenceList);
  50. bool SerializeSearchableNamesMap(FExtPackageDependencyData& OutDependencyData);
  51. bool SerializeAssetRegistryDependencyData(FExtPackageDependencyData& DependencyData);
  52. bool SerializePackageTrailer(FAssetPackageData& PackageData);
  53. /** Returns flags the asset package was saved with */
  54. uint32 GetPackageFlags() const;
  55. // Farchive implementation to redirect requests to the Loader
  56. void Serialize( void* V, int64 Length );
  57. bool Precache( int64 PrecacheOffset, int64 PrecacheSize );
  58. void Seek( int64 InPos );
  59. int64 Tell();
  60. int64 TotalSize();
  61. FArchive& operator<<( FName& Name );
  62. virtual FString GetArchiveName() const override
  63. {
  64. return PackageFilename;
  65. }
  66. //////////////////////////////////
  67. // For FExtAssetData
  68. const FPackageFileSummary& GetPackageFileSummary() const;
  69. int32 GetSoftPackageReferencesCount() const;
  70. /** Reads information from the asset registry data table and converts it to FAssetData */
  71. bool ReadAssetRegistryData(FExtAssetData& OutAssetData);
  72. /** Attempts to get the class name of an object from the thumbnail cache for packages older than VER_UE4_ASSET_REGISTRY_TAGS */
  73. bool ReadAssetDataFromThumbnailCache(FExtAssetData& OutAssetData);
  74. /** Reads information used by the dependency graph */
  75. bool ReadDependencyData(FExtAssetData& OutAssetData);
  76. bool ReadThumbnail(FObjectThumbnail& OutThumbnail);
  77. private:
  78. bool StartSerializeSection(int64 Offset);
  79. FString PackageFilename;
  80. FArchive* Loader;
  81. FPackageFileSummary PackageFileSummary;
  82. TArray<FName> NameMap;
  83. TArray<FObjectImport> ImportMap;
  84. TArray<FObjectExport> ExportMap;
  85. int64 PackageFileSize;
  86. int64 AssetRegistryDependencyDataOffset;
  87. bool bLoaderOwner;
  88. };
  89. /**
  90. * Support class for gathering package dependency data
  91. */
  92. class FExtPackageDependencyData : public FLinkerTables
  93. {
  94. public:
  95. /** The name of the package that dependency data is gathered from */
  96. FName PackageName;
  97. /** Asset Package data, gathered at the same time as dependency data */
  98. FAssetPackageData PackageData;
  99. TBitArray<> ImportUsedInGame;
  100. TBitArray<> SoftPackageUsedInGame;
  101. // Transient Flags indicating which types of data have been gathered
  102. bool bHasPackageData = false;
  103. bool bHasDependencyData = false;
  104. /**
  105. * Return the package name of the UObject represented by the specified import.
  106. *
  107. * @param PackageIndex package index for the resource to get the name for
  108. *
  109. * @return the path name of the UObject represented by the resource at PackageIndex, or the empty string if this isn't an import
  110. */
  111. FName GetImportPackageName(int32 ImportIndex);
  112. /**
  113. * Serialize as part of the registry cache. This is not meant to be serialized as part of a package so it does not handle versions normally
  114. * To version this data change FAssetRegistryVersion or CacheSerializationVersion
  115. */
  116. void SerializeForCache(FArchive& Ar)
  117. {
  118. Ar << PackageName;
  119. Ar << SoftPackageReferenceList;
  120. Ar << SearchableNamesMap;
  121. PackageData.SerializeForCache(Ar);
  122. }
  123. };