Unmounting PAK does not close file

My game mounts pak files at runtime containing custom maps. Occasionally, these pak files may need to be re-downloaded while they are mounted. The issue I’m running into is that my steam workshop download fails to complete because the pak file is locked. I’ve tried simply unmounting the pak before download, but that does not seem to unlock the file; the download still fails and i cannot modify the file in file explorer until I close my game.

I’ve managed to work around this by copying pak files to a PakCache folder before mounting so that there’s never any conflicts. This works, but has the potential to be pretty heavy on the hard drive.

My guess is that IPlatformFilePak is leaving a FileHandle open on mount and there’s no way to close it. So far I’ve found this one:

bool FPakPlatformFile::Mount(const TCHAR* InPakFilename, uint32 PakOrder, const TCHAR* InPath /*= NULL*/)
{
	UE_LOG(LogPakFile, Verbose, TEXT("Mounting Pak: %s"), InPakFilename);

	bool bSuccess = false;
	TSharedPtr<IFileHandle> PakHandle = MakeShareable(LowerLevel->OpenRead(InPakFilename));
	if (PakHandle.IsValid())
	{
		...
	}
	else
	{
		UE_LOG(LogPakFile, Warning, TEXT("Pak \"%s\" does not exist!"), InPakFilename);
	}
	return bSuccess;
}

I do not see PakHandle being closed, but I tried doing something like delete PakHandle.Get() and it caused a crash at runtime.

Actually I am able to delete unmounted paks from my PakCache which indicates to me that this was a user error. There is an issue however with proper pak unmounting when paks are mounted the way most resources say to do it. This is the recommended solution for mounting pak files at runtime:

	IPlatformFile& PlatformFile = FPlatformFileManager::Get().GetPlatformFile();
	FPakPlatformFile* PakPlatform = new FPakPlatformFile();
	PakPlatform->Initialize(&PlatformFile, TEXT(""));
	PakPlatform->InitializeNewAsyncIO();
	FPlatformFileManager::Get().SetPlatformFile(*PakPlatform);

	if (PakPlatform->Mount(*PakFileName, 0, *MountPoint)) {
		UE_LOG(ProxyWarGameInstance, Log, TEXT("Successfully mounted %s at %s"), *PakFileName, *MountPoint);
	}

This works, however, when you go to unmount the pak it will not be registered because there’s a new PakPlatform every time. It will be removed from the pak precacher but the file handle will never be deleted. In order for this to happen, I had to initialize the PakPlatform file in my GameInstance’s Init function and keep a pointer to it in the game instance. Then I just use that pointer to mount/unmount everything.

void UMyGameInstance::Init() {
	...
	IPlatformFile& PlatformFile = FPlatformFileManager::Get().GetPlatformFile();
	PakPlatform = new FPakPlatformFile();
	PakPlatform->Initialize(&PlatformFile, TEXT(""));
	PakPlatform->InitializeNewAsyncIO();
	FPlatformFileManager::Get().SetPlatformFile(*PakPlatform);
	...
}

void UMyGameInstance::MountPak(FString PakFileName, FString MountPoint) {
	if (PakPlatform->Mount(*PakFileName, 0, *MountPoint)) {
		UE_LOG(LogTemp, Log, TEXT("Successfully mounted %s at %s"), *PakFileName, *MountPoint);

	}
}

void UMyGameInstance::UnmountPak(FString PakFileName) {
	if (PakPlatform->Unmount(*PakFileName)) {
		UE_LOG(LogTemp, Log, TEXT("Successfully unmounted pak at %s"), *PakFileName);
	}
	else {
		UE_LOG(LogTemp, Warning, TEXT("failed to unmount pak at %s"), *PakFileName);
	}
}