I’m trying to add content dynamically to my game during runtime and am running into some strange behavior. I’ve bundled up all the assets for a particular map into a separate pak file which then gets mounted using the following code:
FPakPlatformFile* PakPlatform = new FPakPlatformFile();
PakPlatform->Initialize(&PlatformFile, TEXT(""));
FPlatformFileManager::Get().SetPlatformFile(*PakPlatform);
UE_LOG(LogTemp, Log, TEXT("Mounting pak %s"), *PakPath);
if (!PakPlatform->Mount(*PakPath, 0, TEXT("../../../"))) {
UE_LOG(LogTemp, Error, TEXT("Failed to mount %s"), *PakPath);
}
else {
UE_LOG(LogTemp, Log, TEXT("Serializing asset data for %s"), *PakFilename);
FArrayReader SerializedAssetData;
int32 DashPosition = PakFilename.Find(TEXT("-"), ESearchCase::CaseSensitive, ESearchDir::FromEnd);
if (DashPosition != -1)
{
PakFilename = PakFilename.Left(DashPosition);
FString AssetRegistryName = PakFilename + TEXT("-AssetRegistry.bin");
UE_LOG(LogTemp, Log, TEXT("AssetRegistryName %s"), *AssetRegistryName);
if (FFileHelper::LoadFileToArray(SerializedAssetData, *(FPaths::GameDir() / AssetRegistryName)))
{
// serialize the data with the memory reader (will convert FStrings to FNames, etc)
AssetRegistryModule.Get().Serialize(SerializedAssetData);
}
else
{
UE_LOG(LogTemp, Warning, TEXT("%s could not be found"), *AssetRegistryName);
}
}
}
This code seems to work when my pak file is already present when the game is started. The problem I’m having is when the pak file is added to the game while it’s running (whether by downloading it from a server or manually copying the file). The map still loads but the textures are all messed up and there is an infinite loop of log messages which state:
The file '.../.../.../Game/Content/Maps/NextMap.umap' contains unrecognizable data, check that is is of the expected type.
The level I’m trying to load has a few sublevels, which are the ones listed in the warnings. I know there is nothing wrong with the pak file since the map will load just fine if I restart the game leaving the pak file there when it starts up.
So my question:
What am I missing? What is Unreal doing under-the-hood upon startup in regards to pak files that I’m not replicating properly when I try to manually load the pak file?