ok, I have some progress.
I deployed the local server using xampp (and place in the htdocs my chunks - which I set through asset labels).
first I download by http get file, then mount it and read the data inside it.
header
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "Runtime/Online/HTTP/Public/Http.h"
#include "LoadAssets.generated.h"
UCLASS(config=TestConfigFile)
class TESTCPP_API ALoadAssets : public AActor
{
GENERATED_BODY()
public:
// Sets default values for this actor's properties
ALoadAssets();
protected:
// Called when the game starts or when spawned
virtual void BeginPlay() override;
public:
FHttpModule* Http;
FString PakGamePath;
FString ChunkName;
FString FileSavePath;
UPROPERTY(Config, BlueprintReadOnly) //temporary set in the TestConfigFile Name ChunkToDownload.
FString ChunkToDownload;
UFUNCTION(BlueprintCallable, Category = "LoadAssets") //runs from blueprint http://joxi.ru/Vrwo64MU75BqRr
void DownloadAsset(const FString& AssetURL);
void OnResponseReceived(FHttpRequestPtr Request, FHttpResponsePtr Response, bool bWasSuccessful);
UFUNCTION()
bool MountChunk();
};
cpp file
// Fill out your copyright notice in the Description page of Project Settings.
#include "LoadAssets.h"
#include "Engine/Engine.h"
#include "Misc/Paths.h"
#include "HAL/PlatformFilemanager.h"
#include "Misc/Paths.h"
#include "GenericPlatform/GenericPlatformFile.h"
#include "IPlatformFilePak.h"
// Sets default values
ALoadAssets::ALoadAssets()
{
Http = &FHttpModule::Get();
}
// Called when the game starts or when spawned
void ALoadAssets::BeginPlay()
{
Super::BeginPlay();
PakGamePath = FPaths::ProjectContentDir() + TEXT("Paks/"); //UE_LOG(LogTemp, Warning, TEXT("ProjectContentDir() -- %s"), *PakGamePath);
}
void ALoadAssets::DownloadAsset(const FString& AssetURL)
{
TSharedRef<IHttpRequest> HttpRequest = Http->CreateRequest();
HttpRequest->SetVerb("GET");
HttpRequest->SetURL(AssetURL);
HttpRequest->OnProcessRequestComplete().BindUObject(this, &ALoadAssets::OnResponseReceived);
HttpRequest->ProcessRequest();
}
void ALoadAssets::OnResponseReceived(FHttpRequestPtr Request, FHttpResponsePtr Response, bool bWasSuccessful)
{
if (Response.IsValid() && EHttpResponseCodes::IsOk(Response->GetResponseCode()))
{
ChunkName = FPaths::GetCleanFilename(Response->GetURL());
FileSavePath = PakGamePath + ChunkName; //UE_LOG(LogTemp, Warning, TEXT("FileSavePath -- %s"), *FileSavePath);
IPlatformFile& PlatformFile = FPlatformFileManager::Get().GetPlatformFile();
IFileHandle* FileHandle = PlatformFile.OpenWrite(*FileSavePath);
if (FileHandle)
{
// Write the file
FileHandle->Write(Response->GetContent().GetData(), Response->GetContentLength());
// Close the file
delete FileHandle;
UE_LOG(LogTemp, Warning, TEXT("FileHandle exist, MountChunk()"));
MountChunk();
}
else
{
UE_LOG(LogTemp, Warning, TEXT("FileHandle NOT exist, possibly chunk with the same name already is, or Name of chunk is missing"));
}
}
else
{
UE_LOG(LogTemp, Warning, TEXT("Request failed"));
}
}
bool ALoadAssets::MountChunk()
{
IPlatformFile& PlatformFile = FPlatformFileManager::Get().GetPlatformFile();
FPakFile PakFile(&PlatformFile, *FileSavePath, false);
if (!PakFile.IsValid())
{
UE_LOG(LogTemp, Warning, TEXT("PakFile -- Not Valid"));
return false;
}
FString MountPoint = PakGamePath; // + ChunkName //UE_LOG(LogTemp, Warning, TEXT("ChunkName: %s"), *ChunkName);
PakFile.SetMountPoint(*MountPoint);
UE_LOG(LogTemp, Warning, TEXT("PakFile.GetMountPoint(): %s"), *PakFile.GetMountPoint());
TArray<FString> FileList;
PakFile.FindFilesAtPath(FileList, *PakFile.GetMountPoint(), true, false, true);
for (int32 it = 0; it < FileList.Num(); it++)
{
FString file = FileList[it];
UE_LOG(LogTemp, Warning, TEXT("[UPackageDownloader::MountPackage] Content %s: %s"), *ChunkName, *file);
}
return true;
}
after all you can get path of the assets inside package and for ex. spawn it.
just for example code (not bounding with above):
PakFile.FindFilesAtPath(FileList, *MountPoint, true, false, true);
for (int32 it = 0; it < FileList.Num(); it++)
{
FString AssetName = FileList[it];
FString GamePathName, TestExtension;
FPackageName::TryConvertFilenameToLongPackageName(AssetName, GamePathName);
FString AssetShortName = FPackageName::GetShortName(AssetName);
FString FileName, FileExt;
AssetShortName.Split(TEXT("."), &FileName, &FileExt);
if (FileExt == "uexp") continue;
else
{
FString NewAssetName = GamePathName + TEXT(".") + FileName;
TestSpawnFromPath(NewAssetName);
//UE_LOG(LogTemp, Warning, TEXT("AssetName -- %s, TestFileName -- %s, NewAssetName -- %s, AssetShortName -- %s"), *AssetName, *GamePathName, *NewAssetName, *AssetShortName);
}
}
}
}
void ATestActor::TestSpawnFromPath(FString Path)
{
FSoftObjectPath StrNewAssetRef;
if (true) ///blueprints
{
StrNewAssetRef = "Blueprint'" + Path + "_C'";
}
else
{
StrNewAssetRef = Path;
}
//UE_LOG(LogTemp, Warning, TEXT("StrNewAssetRef -- %s"), *StrNewAssetRef.ToString());
UClass* SpawnClass = StaticLoadClass(UObject::StaticClass(), NULL, *StrNewAssetRef.ToString(), NULL, LOAD_None, NULL);
if (SpawnClass != nullptr)
{
//UE_LOG(LogTemp, Warning, TEXT("NewLoadedObject -- %s"), *SpawnClass->GetName());
Transform.SetLocation(FVector(Transform.GetLocation().X, Transform.GetLocation().Y + 300.f, Transform.GetLocation().Z));
GetWorld()->SpawnActor<AActor>(SpawnClass, Transform);
}
}