Commit 720b4285 by Kai Westerkamp

Download rework

parent 9e1d7880
......@@ -9,7 +9,8 @@
"Type": "Runtime",
"LoadingPhase": "Default",
"AdditionalDependencies": [
"Engine"
"Engine",
"CoreUObject"
]
}
]
......
// Fill out your copyright notice in the Description page of Project Settings.
#include "MasterTestProject.h"
#include "HttpDownloader.h"
void UHttpDownloader::DownloadFile(const FString &URL)
{
TSharedRef< IHttpRequest > HttpRequest = FHttpModule::Get().CreateRequest();
HttpRequest->SetVerb("GET");
HttpRequest->SetURL(URL);
HttpRequest->OnProcessRequestComplete().BindUObject(this, &UHttpDownloader::OnResponseReceived);
// Execute the request
UE_LOG(TILES, Warning, TEXT("Downlaoding: %s "), *HttpRequest->GetURL());
HttpRequest->ProcessRequest();
}
void UHttpDownloader::OnResponseReceived(FHttpRequestPtr Request, FHttpResponsePtr Response, bool bWasSuccessful)
{
if (Response.IsValid()) {
UE_LOG(TILES, Warning, TEXT("Default Downlaod Finished: %s Scucess: %s \"%s\""), *Request->GetURL(), bWasSuccessful ? TEXT("True") : TEXT("False"), *Response->GetContentAsString());
}
else {
UE_LOG(TILES, Warning, TEXT("Downlaod Finished: %s Scucess: %s not Valid"), *Request->GetURL(), bWasSuccessful ? TEXT("True") : TEXT("False"));
}
}
FString UHttpDownloader::ResolveRealativeURL(const FString &BaseUrl, const FString &RelativeUrl) {
if (RelativeUrl.StartsWith("file") || RelativeUrl.StartsWith("http")) {
//absolute Url
UE_LOG(TILES, Log, TEXT("Absolute URL %s "), *RelativeUrl);
return RelativeUrl;
}
FString BaseUrlnormalized, RelativeUrlnormalized;
BaseUrlnormalized = BaseUrl.Replace(TEXT("\\"), TEXT("/"));
RelativeUrlnormalized = RelativeUrl.Replace(TEXT("\\"), TEXT("/"));
FString basePath, right;
if (!BaseUrlnormalized.Split(TEXT("/"), &basePath, &right, ESearchCase::IgnoreCase, ESearchDir::FromEnd)) {
basePath = BaseUrlnormalized;
}
//UE_LOG(GLTF, Warning, TEXT("Diffuse Texture:\"%s\" \"%s\""), *dirPath, *fileName);
FString url = basePath + "/" + RelativeUrlnormalized;
UE_LOG(TILES, Log, TEXT("Relative URl: Base:%s, Relative %s, -> %s "), *BaseUrlnormalized, *RelativeUrlnormalized, *url);
return url;
}
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "UObject/NoExportTypes.h"
#include "Runtime/Online/HTTP/Public/Http.h"
#include "HttpDownloader.generated.h"
/**
*
*/
UCLASS()
class MASTERTESTPROJECT_API UHttpDownloader : public UObject
{
GENERATED_BODY()
public:
UFUNCTION()
FString ResolveRealativeURL(const FString &BaseUrl, const FString &RelativeUrl);
UFUNCTION()
void DownloadFile(const FString &URL);
/*Assign this function to call when the GET request processes sucessfully*/
virtual void OnResponseReceived(FHttpRequestPtr Request, FHttpResponsePtr Response, bool bWasSuccessful);
};
// Fill out your copyright notice in the Description page of Project Settings.
#include "MasterTestProject.h"
#include "JsonReader.h"
#include "JsonObjectConverter.h"
#include "TileDownloader.h"
#include "MasterTestProjectGameModeBase.h"
......@@ -10,122 +9,14 @@ void AMasterTestProjectGameModeBase::InitGame(const FString & MapName, const FSt
Super::InitGame(MapName, Options, ErrorMessage);
//FString file = "D:\\Dropbox\\Studium\\MasterArbeit\\3d-tiles\\examples\\tileset.json";
//FString base = "D:\\Dropbox\\Studium\\MasterArbeit\\";
FString base = "file:/H:\\Repositories\\MasterArbeit\\";
FString file = base+"3d-tiles-samples\\tilesets\\..\\tilesets\\TilesetWithRequestVolume\\tileset.json";
FString base = "file://D:\\Dropbox\\Studium\\MasterArbeit\\";
//FString base = "file://H:\\Repositories\\MasterArbeit\\";
FString file = "3d-tiles-samples\\tilesets\\TilesetWithRequestVolume\\tileset.json";
rootTileset = nullptr;
rootTileset = new FTileContent();
rootTileset->url = file;
DownloadFile(file);
FString JsonRaw = "";
// FFileHelper::LoadFileToString(JsonRaw, *file);
// UE_LOG(TILES, Log, TEXT("Loading file %s"), *file);
//UE_LOG(GLTF, Warning, TEXT("LangFile: \n %s"), *JsonRaw);
//FTileset* tileset = parseTileset(JsonRaw);
//DownloadFile(file, tileset->root.children[0].content.url);
}
FTileset* AMasterTestProjectGameModeBase::parseTileset(FString JsonString)
{
//TODO error Handling
TSharedPtr<FJsonObject> JsonParsed;
TSharedRef<TJsonReader<TCHAR>> JsonReader = TJsonReaderFactory<TCHAR>::Create(JsonString);
if (FJsonSerializer::Deserialize(JsonReader, JsonParsed))
{
FTileset *tileset = new FTileset();
bool success = FJsonObjectConverter::JsonObjectToUStruct<FTileset>(JsonParsed.ToSharedRef(), tileset, 0, 0);
TSharedPtr<FJsonObject> root = JsonParsed->GetObjectField("root");
parseTile(root, &tileset->root);
//UE_LOG(TILES, Warning, TEXT("Parsed: Success:%s Version:\"%s\" geometricError:%f"), success ? TEXT("True") : TEXT("False"), *tileset.asset.version, tileset.geometricError);
return tileset;
}
return nullptr;
}
void AMasterTestProjectGameModeBase::parseTile(TSharedPtr<FJsonObject> json, FTile *targetTile) {
bool success = FJsonObjectConverter::JsonObjectToUStruct<FTile>(json.ToSharedRef(), targetTile, 0, 0);
if (json->HasField("children")) {
const TArray<TSharedPtr<FJsonValue>> children = json->GetArrayField("children");
UE_LOG(TILES, Warning, TEXT("\nTile: Success:%s Version:\"%s\" Children: "), success ? TEXT("True") : TEXT("False"), *targetTile->content.url);
for (TSharedPtr<FJsonValue> child : children)
{
FTile newTile;
parseTile(child->AsObject(), &newTile);
targetTile->children.Add(newTile);
}
}
else {
UE_LOG(TILES, Warning, TEXT("Tile: Success:%s Version:\"%s\" no Childs "), success ? TEXT("True") : TEXT("False"), *targetTile->content.url);
}
if (!targetTile->content.url.IsEmpty()) {
TSharedRef<IHttpRequest> request = DownloadFile(/*base url*/, targetTile->content.url);
}
}
TSharedRef<IHttpRequest> AMasterTestProjectGameModeBase::DownloadFile(const FString & BaseUrl, const FString & RelativeUrl)
{
FString Path, Filename, Extension;
FPaths::Split(BaseUrl, Path, Filename, Extension);
FString url = Path + "/" + RelativeUrl;
UE_LOG(TILES, Warning, TEXT("Downlaoding: BAse:%s, Relative %s "), *BaseUrl, *RelativeUrl);
return DownloadFile(url);
//DownloadFile(TEXT("http://ipv4.download.thinkbroadband.com/20MB.zip"));
}
TSharedRef<IHttpRequest> AMasterTestProjectGameModeBase::DownloadFile(const FString & Url)
{
TSharedRef< IHttpRequest > HttpRequest = FHttpModule::Get().CreateRequest();
HttpRequest->SetVerb("GET");
HttpRequest->SetURL(Url);
HttpRequest->OnProcessRequestComplete().BindUObject(this, &AMasterTestProjectGameModeBase::OnReady);
HttpRequest->OnRequestProgress().BindUObject(this, &AMasterTestProjectGameModeBase::OnProgress_Internal);
// Execute the request
UE_LOG(TILES, Warning, TEXT("Downlaoding: %s "), *HttpRequest->GetURL());
HttpRequest->ProcessRequest();
return HttpRequest;
}
void AMasterTestProjectGameModeBase::OnReady(FHttpRequestPtr Request, FHttpResponsePtr Response, bool bWasSuccessful)
{
if (Response.IsValid()) {
// UE_LOG(TILES, Warning, TEXT("Downlaod Finished: %s Scucess: %s \"%s\""), *Request->GetURL(), bWasSuccessful ? TEXT("True") : TEXT("False"), *Response->GetContentAsString());
if (Request->GetURL().EndsWith(TEXT(".json"))) {
FTileset* tileset = parseTileset(Response->GetContentAsString());
if (rootTileset == nullptr) {
rootTileset = tileset;
}
Response->GetContent();
}
}
else {
UE_LOG(TILES, Warning, TEXT("Downlaod Finished: %s Scucess: %s not Valid"), *Request->GetURL(), bWasSuccessful ? TEXT("True") : TEXT("False"));
}
}
void AMasterTestProjectGameModeBase::OnProgress_Internal(FHttpRequestPtr Request, int32 BytesSent, int32 BytesReceived)
{
UE_LOG(TILES, Warning, TEXT("Downlaod Progress: %s Sent:%d, Received:%d "), *Request->GetURL(), BytesSent, BytesReceived);
UTileDownloader *downloader = NewObject<UTileDownloader>(UTileDownloader::StaticClass());
downloader->GetTileContent(rootTileset, base);
}
......@@ -3,156 +3,11 @@
#pragma once
#include "GameFramework/GameModeBase.h"
#include "Runtime/Online/HTTP/Public/Http.h"
#include "Tileset.h"
#include "MasterTestProjectGameModeBase.generated.h"
USTRUCT()
struct FAsset
{
GENERATED_USTRUCT_BODY()
UPROPERTY()
FString version;
UPROPERTY()
FString tilesetVersion;
FAsset() {
version = "";
tilesetVersion = "";
}
};
USTRUCT()
struct FProperties
{
GENERATED_USTRUCT_BODY()
UPROPERTY()
float maximum;
UPROPERTY()
float minimum;
};
USTRUCT()
struct FPropertieDict
{
GENERATED_USTRUCT_BODY()
//FIXME should be a map or so, found no Unreal doc, values are from an example
/*"patternProperties" : {
".*": {
"$ref": "properties.schema.json"
}
}*/
UPROPERTY()
FProperties Longitude;
UPROPERTY()
FProperties Latitude;
UPROPERTY()
FProperties Height;
};
USTRUCT()
struct FBoundingVolume
{
GENERATED_USTRUCT_BODY()
//TODO only one in Unreal
UPROPERTY()
TArray<float> box; //12 Elemete
UPROPERTY()
TArray<float> region; //6 Elemete
UPROPERTY()
TArray<float> sphere; //4 Elemete
};
USTRUCT()
struct FTileContent
{
GENERATED_USTRUCT_BODY()
UPROPERTY()
FBoundingVolume boundingVolume;
UPROPERTY()
FString url;
TArray<uint8> content;
// FTileset tileset;
};
USTRUCT()
struct FTile
{
GENERATED_USTRUCT_BODY()
UPROPERTY()
FBoundingVolume boundingVolume;
UPROPERTY()
FBoundingVolume viewerRequestVolume;
UPROPERTY()
float geometricError;
UPROPERTY()
FString refine; //TODO: "enum" : ["add", "replace"]
UPROPERTY()
TArray<float> transform; //16 Elemete
//TODO "default" : [ 1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0 ]
UPROPERTY()
FTileContent content;
//UPROPERTY()
//struct FTile children;
TArray<struct FTile> children;
FTile() {
float transformDefault[] = { 1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0 };
transform.Empty();
transform.Append(transformDefault, 16);
}
};
USTRUCT()
struct FTileset
{
GENERATED_USTRUCT_BODY()
UPROPERTY()
FAsset asset;
UPROPERTY()
FPropertieDict properties;
UPROPERTY()
float geometricError;
UPROPERTY()
FTile root;
FTileset() {
geometricError = -1;
}
};
/**
*
*/
......@@ -164,20 +19,6 @@ class MASTERTESTPROJECT_API AMasterTestProjectGameModeBase : public AGameModeBas
public:
virtual void InitGame(const FString & MapName, const FString & Options, FString & ErrorMessage);
FTileset* parseTileset(FString JsonString);
TSharedRef<IHttpRequest> DownloadFile(const FString &BaseUrl, const FString &RelativeUrl);
TSharedRef<IHttpRequest> DownloadFile(const FString &Url);
FTileset *rootTileset;
TMap<FHttpRequestPtr, FTileContent> activeRequests;
private:
void parseTile(TSharedPtr<FJsonObject> json, FTile *targetTile);
FTileContent *rootTileset;
void OnReady(FHttpRequestPtr Request, FHttpResponsePtr Response, bool bWasSuccessful);
void OnProgress_Internal(FHttpRequestPtr Request, int32 BytesSent, int32 BytesReceived);
};
// Fill out your copyright notice in the Description page of Project Settings.
#include "MasterTestProject.h"
#include "TileDownloader.h"
void UTileDownloader::GetTileContent(FTileContent *Content, FString &baseUrl)
{
parent = Content;
DownloadFile(ResolveRealativeURL(baseUrl, *Content->url));
}
void UTileDownloader::OnResponseReceived(FHttpRequestPtr Request, FHttpResponsePtr Response, bool bWasSuccessful)
{
if (Response.IsValid()) {
if (Request->GetURL().EndsWith(TEXT(".json"))) {
parent->tileset = UTilesetUtil::parseTileset(Response->GetContentAsString(), Request->GetURL());
UE_LOG(TILES, Warning, TEXT("Parsed Tilset: %s Scucess: %s "), *Request->GetURL(), bWasSuccessful ? TEXT("True") : TEXT("False"));
}
else {
//Response->GetContent
UE_LOG(TILES, Error, TEXT("Parsed Tile: %s Scucess: %s "), *Request->GetURL(), bWasSuccessful ? TEXT("True") : TEXT("False"));
}
}
else {
UE_LOG(TILES, Warning, TEXT("Downlaod Finished: %s Scucess: %s not Valid"), *Request->GetURL(), bWasSuccessful ? TEXT("True") : TEXT("False"));
}
}
\ No newline at end of file
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "HttpDownloader.h"
#include "Tileset.h"
#include "TileDownloader.generated.h"
/**
*
*/
UCLASS()
class MASTERTESTPROJECT_API UTileDownloader : public UHttpDownloader
{
GENERATED_BODY()
private:
FTileContent *parent;
public:
void GetTileContent(FTileContent *Content, FString &baseUrl);
virtual void OnResponseReceived(FHttpRequestPtr Request, FHttpResponsePtr Response, bool bWasSuccessful);
};
// Fill out your copyright notice in the Description page of Project Settings.
#include "MasterTestProject.h"
#include "JsonReader.h"
#include "JsonObjectConverter.h"
#include "TileDownloader.h"
#include "Tileset.h"
FTileset* UTilesetUtil::parseTileset(FString JsonString, FString BaseURL)
{
//TODO error Handling
TSharedPtr<FJsonObject> JsonParsed;
TSharedRef<TJsonReader<TCHAR>> JsonReader = TJsonReaderFactory<TCHAR>::Create(JsonString);
if (FJsonSerializer::Deserialize(JsonReader, JsonParsed))
{
FTileset *tileset = new FTileset();
bool success = FJsonObjectConverter::JsonObjectToUStruct<FTileset>(JsonParsed.ToSharedRef(), tileset, 0, 0);
tileset->absoluteURL = BaseURL;
TSharedPtr<FJsonObject> root = JsonParsed->GetObjectField("root");
UTilesetUtil::parseTile(root, &tileset->root, tileset);
//UE_LOG(TILES, Warning, TEXT("Parsed: Success:%s Version:\"%s\" geometricError:%f"), success ? TEXT("True") : TEXT("False"), *tileset.asset.version, tileset.geometricError);
return tileset;
}
return nullptr;
}
void UTilesetUtil::parseTile(TSharedPtr<FJsonObject> json, FTile *targetTile, FTileset* parent) {
bool success = FJsonObjectConverter::JsonObjectToUStruct<FTile>(json.ToSharedRef(), targetTile, 0, 0);
targetTile->parentReference = parent;
if (json->HasField("children")) {
const TArray<TSharedPtr<FJsonValue>> children = json->GetArrayField("children");
UE_LOG(TILES, Warning, TEXT("\nDownload Tile: Success:%s Version:\"%s\" Children: "), success ? TEXT("True") : TEXT("False"), *targetTile->content.url);
for (TSharedPtr<FJsonValue> child : children)
{
FTile newTile;
UTilesetUtil::parseTile(child->AsObject(), &newTile, parent);
targetTile->children.Add(newTile);
}
}
else {
UE_LOG(TILES, Warning, TEXT("Download Tile: Success:%s Version:\"%s\" no Childs "), success ? TEXT("True") : TEXT("False"), *targetTile->content.url);
}
if (!targetTile->content.url.IsEmpty()) {
UTileDownloader *downloader = NewObject<UTileDownloader>(UTileDownloader::StaticClass());
downloader->GetTileContent(&targetTile->content, parent->absoluteURL);
}
}
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "UObject/NoExportTypes.h"
#include "Tileset.generated.h"
USTRUCT()
struct FAsset
{
GENERATED_USTRUCT_BODY()
UPROPERTY()
FString version;
UPROPERTY()
FString tilesetVersion;
FAsset() {
version = "";
tilesetVersion = "";
}
};
USTRUCT()
struct FProperties
{
GENERATED_USTRUCT_BODY()
UPROPERTY()
float maximum;
UPROPERTY()
float minimum;
};
USTRUCT()
struct FPropertieDict
{
GENERATED_USTRUCT_BODY()
//FIXME should be a map or so, found no Unreal doc, values are from an example
/*"patternProperties" : {
".*": {
"$ref": "properties.schema.json"
}
}*/
UPROPERTY()
FProperties Longitude;
UPROPERTY()
FProperties Latitude;
UPROPERTY()
FProperties Height;
};
USTRUCT()
struct FBoundingVolume
{
GENERATED_USTRUCT_BODY()
//TODO only one in Unreal
UPROPERTY()
TArray<float> box; //12 Elemete
UPROPERTY()
TArray<float> region; //6 Elemete
UPROPERTY()
TArray<float> sphere; //4 Elemete
};
USTRUCT()
struct FTileContent
{
GENERATED_USTRUCT_BODY()
UPROPERTY()
FBoundingVolume boundingVolume;
UPROPERTY()
FString url;
TArray<uint8> content;
struct FTileset *tileset;
FTileContent() {
tileset = nullptr;
}
};
USTRUCT()
struct FTile
{
GENERATED_USTRUCT_BODY()
UPROPERTY()
FBoundingVolume boundingVolume;
UPROPERTY()
FBoundingVolume viewerRequestVolume;
UPROPERTY()
float geometricError;
UPROPERTY()
FString refine; //TODO: "enum" : ["add", "replace"]
UPROPERTY()
TArray<float> transform; //16 Elemete
//TODO "default" : [ 1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0 ]
UPROPERTY()
FTileContent content;
//UPROPERTY()
//struct FTile children;
TArray<struct FTile> children;
struct FTileset *parentReference;
FTile() {
float transformDefault[] = { 1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0 };
transform.Empty();
transform.Append(transformDefault, 16);
}
};
USTRUCT()
struct FTileset
{
GENERATED_USTRUCT_BODY()
UPROPERTY()
FAsset asset;
UPROPERTY()
FPropertieDict properties;
UPROPERTY()
float geometricError;
UPROPERTY()
FTile root;
FString absoluteURL;
FTileset() {
geometricError = -1;
}
};
//struct TileHeader {
// uchar[4] magic;
// unit32 version;
// unit32 byteLength;
//};
/**
*
*/
UCLASS()
class MASTERTESTPROJECT_API UTilesetUtil : public UObject
{
GENERATED_BODY()
public:
static FTileset* parseTileset(FString JsonString, FString BaseURL);
static void parseTile(TArray<uint8> data, FString BaseURL);
private:
static void parseTile(TSharedPtr<FJsonObject> json, FTile *targetTile, FTileset* parent);
};
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment