UE4 Pak package hot update

Posted by scnjl on Sun, 02 Jan 2022 22:52:05 +0100

pak file, also known as pak package, is a file format used by UE4 to update resources (including hot update). UE4 combines multiple files into one pak file and updates resources through pak files. pak files can not only load UE4 resource files, such as uasset, umap, etc., but also load non resource files, such as xml, json, txt, etc, In addition to the file, the pak file can also contain some additional information, such as the encryption of the pak file, the version of the pak, and so on.

1, Cook resource (baking resource)

1. Why bake resources

Because the program runs on a variety of platforms, and different platforms have their own resource formats, you must bake the resources of the corresponding platform before creating the Pak file. UE4 provides ue4editor cmd Exe tool to provide resource baking, ue4editor cmd Exe can be directly in cmd command.

<Engine path>\Engine\Binaries\Win64\UE4Editor-Cmd.exe <Project path>\RobotEngine.uproject -run=Cook  -TargetPlatform=<Platform type> -fileopenlog -unversioned -abslog=<Log output path> -stdout -CrashForUAT -unattended -NoLogTimes  -UTF8Output


The baked resources will be stored in the < project folder > / saved / cooked / < corresponding platform name > / < project name > / content / < corresponding Directory > parameters, and the specific meaning will be studied later.

2. Platform types supported by baking

UE4's resources are self-supporting. At present, most mainstream platforms:

Of course, if we only need to bake the resources of the Windows platform, UE4 directly provides the bake button

 

2, Pack Pak files

UE4 provides a tool for creating Pak files - unrealpak Exe is for us to use. We can run unrealpak directly from the cmd command line Exe to create a Pak package for the specified file.

<Engine path>\Engine\Binaries\Win64\UnrealPak.exe <pak File path> -Create=<Project path>\RobotEngine\Saved\Cooked\Android_ASTC\RobotEngine\Content\Comps\<cook The folder where the resource file is located> -compress


Here are a few points to note. The pak file path is the path where we want to store the created pak file, such as D: \ pak \ mypak pak, the folder where the cook resource file is located, that is, the directory where the uasset file after the cook is located. Remember that it is not a file path, because the directory can contain multiple resource files, including Android_ ASTC is a platform type. You need to select the corresponding folder according to the actual platform of the cook.

-compress means that the file is compressed when it is packaged pak.

Little knowledge

When we are packing, check Edit\Project Setting\Packaging\Use Pak File

 

The packaged resources will be encapsulated in an independent Pak package. If it is not checked, the resource path is the same as that during editing.

3, Pak file mounting and loading

First, create a class as a carrier for mounting code

//.h
#pragma once

#include "IPlatformFilePak.h"
#include "GenericPlatform/GenericPlatformFile.h"
#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "OperatActor.generated.h"

UCLASS()
class MYPROJECT_API AOperatActor : public AActor
{
	GENERATED_BODY()
	
public:	
	AOperatActor();
	TSharedPtr<FPakPlatformFile> PakPlatform;
	class IPlatformFile* OldPlatform;

protected:
	virtual void BeginPlay() override;

public:	
	virtual void Tick(float DeltaTime) override;
	
	void Mount();
};
//.cpp
void AOperatActor::BeginPlay()
{
	Super::BeginPlay();
	Mount();
}
void AOperatActor::Mount()
{
    //The path of the pak package. If it is a hot update, first download the pak package locally and then mount it
	FString PakFileFullPath = TEXT("D:/Goulandis/Windows/PakTest_1_P.pak");
	//Gets the file operation chain header of the current platform
    OldPlatform = &FPlatformFileManager::Get().GetPlatformFile();
	if (!PakPlatform)//Judge whether the smart pointer is valid
	{
		PakPlatform = MakeShareable<FPakPlatformFile>(new FPakPlatformFile());
	}
    //Initialize the interface,
	PakPlatform->Initialize(&FPlatformFileManager::Get().GetPlatformFile(), TEXT(""));
    //Set initialized configuration
	FPlatformFileManager::Get().SetPlatformFile(*PakPlatform.Get());
	if (FPlatformFileManager::Get().GetPlatformFile().FileExists(*PakFileFullPath))//Determine whether the file exists
	{
         //Gets a reference to the pak file from the file system
		TSharedPtr<FPakFile> TmpPak = MakeShareable<FPakFile>(new FPakFile(PakPlatform.Get(),*PakFileFullPath,false));
		if (TmpPak)
		{
             //Obtain the mounted Pak file to avoid repeated mounting
             TArray<FString> ExistPaks;
			PakPlatformFile->GetMountedPakFilenames(ExistPaks);
			if (ExistPaks.Find(PakName) >= 0) 
             {
				return;
		    }
             //Splice new mount points
			FString PakMountPoint = TmpPak->GetMountPoint();
			int32 Pos = PakMountPoint.Find("Content/");
			FString NewMountPoint = PakMountPoint.RightChop(Pos);
			NewMountPoint = FPaths::ProjectDir() + NewMountPoint;
             //Set up a new mount point
			TmpPak->SetMountPoint(*NewMountPoint);
			//Mount the pak file. If the mount is successful, return true; otherwise, return false
			if (PakPlatform->Mount(*PakFileFullPath, 1, *NewMountPoint))
			{
                 //After the mount is successful, you can load the mount content in the specified mount directory. Here you steal the lazy and directly use the completion path
				UClass* uclass = LoadClass<AActor>(NULL, TEXT("Blueprint'/Game/Pak/PakTest.PakTest_C'"));
                 //After the blueprint class is loaded successfully, the Actor can be instantiated directly into the World
				if (uclass)
				{
					AActor* actor = GetWorld()->SpawnActor<AActor>(uclass, FVector(0), FRotator(0));
				}
			}
		}
	}
	FPlatformFileManager::Get().SetPlatformFile(*OldPlatform);
}

Here are some classes and functions to be explained:

  • IPlatformFile: This is the most basic class for UE4 to operate files and defines the relevant methods for operating files. UE4 encapsulates a file operation class instead of directly operating files with C + +. UE4 establishes such a higher-level I/O interface in order to provide platform portability;

  • FPakPlatformFile: this class is an encapsulated class of UE4's operation on pak files, derived from IPlatformFile class;

  • FPlatformFileManager: this class is the encapsulation used by UE4 to obtain physical files of different platforms. The Get() method is to obtain the instance of the class itself. The GetPlatformFile() method is to obtain the chain head of the file operation chain. If it is not found, it returns nullptr;

  • Initialize(): initialize the interface. The first parameter is the next file class object pointed to by the FPakPlatformFile class. We need to point to the current platform here. The second parameter is a command line. Some file classes will parse some parameters from here. We have no command here, so we use an empty string;

  • SetPlatformFile(): it can be understood as the configuration of setting Initialize() for UE4 file system;

  • GetMountedPakFilenames(): get the mounted pak file. The parameter is an FString array;

  • GetMountPoint(): get the mount point of the pak file, which is directly stored in the pak file;

  • RightChop(): returns the string to the right of the specified position of the string;

  • SetMountPoint(): set the mount point. There must be a mount point for mounting the pak file, otherwise UE4 cannot find the pak file. The mount point is the parent directory of the pak file in UE4;

  • Mount(): Mount pak files;

Here, the mounting of a pak file is completed after loading. The general process of hot update of the game is to download update resources from the server to the local - > mount local resources - > Load local resources.

4, Pak packet encryption and decryption

4, Resource update in the form of DLC

The difference between the form of DLC and the form of pak package lies in the generation method of pak package. pak package is baked and packaged directly using the command line, while DLC uses the ProjectLauncher. The final resources are downloaded locally in the form of pak package, but the form of pak requires us to manually write C + + code for mounting, The pak file can be automatically mounted and loaded by placing it in the specified folder in the form of DLC.

In the form of DLC, two projectlaunchers need to be configured, one is the packaged local projectlaunchers, and the other is the packaged DLC projectlaunchers

 

Topics: C++ UE4