bool FPackageReader::ReadAssetRegistryDataIfCookedPackage(TArray<FAssetData*>& AssetDataList, TArray<FString>& CookedPackageNamesWithoutAssetData)
{
	if (!!(GetPackageFlags() & PKG_FilterEditorOnly))
	{
		const FString PackageName = FPackageName::FilenameToLongPackageName(PackageFilename);
		
		bool bFoundAtLeastOneAsset = false;

		// If the packaged is saved with the right version we have the information
		// which of the objects in the export map as the asset.
		// Otherwise we need to store a temp minimal data and then force load the asset
		// to re-generate its registry data
		if (UE4Ver() >= VER_UE4_COOKED_ASSETS_IN_EDITOR_SUPPORT)
		{
			const FString PackagePath = FPackageName::GetLongPackagePath(PackageName);

			TArray<FObjectImport> ImportMap;
			TArray<FObjectExport> ExportMap;
			SerializeNameMap();
			SerializeImportMap(ImportMap);
			SerializeExportMap(ExportMap);
			for (FObjectExport& Export : ExportMap)
			{
				if (Export.bIsAsset)
				{
					FString GroupNames; // Not used for anything
					TMap<FName, FString> Tags; // Not used for anything
					TArray<int32> ChunkIDs; // Not used for anything

					// We need to get the class name from the import/export maps
					FName ObjectClassName;
					if (Export.ClassIndex.IsNull())
					{
						ObjectClassName = UClass::StaticClass()->GetFName();
					}
					else if (Export.ClassIndex.IsExport())
					{
						const FObjectExport& ClassExport = ExportMap[Export.ClassIndex.ToExport()];
						ObjectClassName = ClassExport.ObjectName;
					}
					else if (Export.ClassIndex.IsImport())
					{
						const FObjectImport& ClassImport = ImportMap[Export.ClassIndex.ToImport()];
						ObjectClassName = ClassImport.ObjectName;
					}

					AssetDataList.Add(new FAssetData(FName(*PackageName), FName(*PackagePath), FName(*GroupNames), Export.ObjectName, ObjectClassName, Tags, ChunkIDs, GetPackageFlags()));
					bFoundAtLeastOneAsset = true;
				}
			}
		}
		if (!bFoundAtLeastOneAsset)
		{
			CookedPackageNamesWithoutAssetData.Add(PackageName);
		}
		return true;
	}

	return false;
}
bool FPackageReader::ReadDependencyData (FPackageDependencyData& OutDependencyData)
{
	OutDependencyData.PackageName = FName(*FPackageName::FilenameToLongPackageName(PackageFilename));

	SerializeNameMap();
	SerializeImportMap(OutDependencyData.ImportMap);
	SerializeStringAssetReferencesMap(OutDependencyData.StringAssetReferencesMap);

	return true;
}
FNameTableArchiveWriter::~FNameTableArchiveWriter()
{
	if (FileAr)
	{
		int64 ActualNameOffset = Tell();
		SerializeNameMap();
		Seek(NameOffsetLoc);
		*this << ActualNameOffset;

		delete FileAr;
		FileAr = nullptr;

		IFileManager::Get().Move(*FinalFilename, *TempFilename);
	}
}
bool FNameTableArchiveWriter::SaveToFile(const TCHAR* Filename)
{
    int64 ActualNameOffset = Tell();
    SerializeNameMap();
    Seek(NameOffsetLoc);
    *this << ActualNameOffset;

    // Save to a temp file first, then move to the destination to avoid corruption
    const FString TempFile = FString(Filename) + TEXT(".tmp");
    if ( FFileHelper::SaveArrayToFile(Writer, *TempFile) )
    {
        if ( IFileManager::Get().Move(Filename, *TempFile) )
        {
            return true;
        }
    }

    return false;
}
bool FNameTableArchiveReader::LoadFile(const TCHAR* Filename, int32 SerializationVersion)
{
    if ( FFileHelper::LoadFileToArray(Reader, Filename, FILEREAD_Silent) )
    {
        int32 MagicNumber = 0;
        *this << MagicNumber;

        if ( MagicNumber == PACKAGE_FILE_TAG )
        {
            int32 VersionNumber = 0;
            *this << VersionNumber;

            if (VersionNumber == SerializationVersion)
            {
                return SerializeNameMap();
            }
        }
    }

    return false;
}
bool FNameTableArchiveReader::LoadFile(const TCHAR* Filename, int32 SerializationVersion)
{
	FileAr = IFileManager::Get().CreateFileReader(Filename, FILEREAD_Silent);
	if (FileAr && !FileAr->IsError() && FileAr->TotalSize() > 0)
	{
		int32 MagicNumber = 0;
		*this << MagicNumber;

		if (!IsError() && MagicNumber == PACKAGE_FILE_TAG)
		{
			int32 VersionNumber = 0;
			*this << VersionNumber;

			if (!IsError() && VersionNumber == SerializationVersion)
			{
				return SerializeNameMap();
			}
		}
	}

	return false;
}