Unrealzilla/Source/Unrealzilla/Private/BugMarkerLoader.cpp

151 lines
4.7 KiB
C++

// ©2022 Batty Bovine Productions, LLC. All Rights Reserved.
#include "BugMarkerLoader.h"
#include "BugMarkerActor.h"
#include "HttpModule.h"
#include "JsonObjectConverter.h"
#include "UnrealzillaGlobalSettings.h"
#include "Kismet/GameplayStatics.h"
void ABugMarkerLoader::BeginPlay()
{
Super::BeginPlay();
const FString FullURL = GetDefault<UUnrealzillaGlobalSettings>()->SubmissionServer + "/rest.cgi";
TArray<FString> StatusQueries;
if (GetDefault<UUnrealzillaGlobalSettings>()->bShowUnresolvedBugs)
{
for (const FString Unresolved : GetDefault<UUnrealzillaGlobalSettings>()->UnresolvedStatuses)
{
StatusQueries.Add("status=" + Unresolved);
}
}
if (GetDefault<UUnrealzillaGlobalSettings>()->bShowInProgressBugs)
{
for (const FString InProgress : GetDefault<UUnrealzillaGlobalSettings>()->InProgressStatuses)
{
StatusQueries.Add("status=" + InProgress);
}
}
if (GetDefault<UUnrealzillaGlobalSettings>()->bShowResolvedBugs)
{
for (const FString Resolved : GetDefault<UUnrealzillaGlobalSettings>()->ResolvedStatuses)
{
StatusQueries.Add("status=" + Resolved);
}
}
StatusQueries.Add("cf_mapname=" + this->GetWorld()->GetMapName().RightChop(this->GetWorld()->StreamingLevelsPrefix.Len()));
StatusQueries.Add("api_key=" + GetDefault<UUnrealzillaGlobalSettings>()->APIKey);
const FString QueryString = FString::Join(StatusQueries, TEXT("&"));
FHttpModule &HttpModule = FHttpModule::Get();
TSharedRef<IHttpRequest, ESPMode::ThreadSafe> SeverityRequest = HttpModule.CreateRequest();
SeverityRequest->SetVerb(TEXT("GET"));
SeverityRequest->SetHeader(TEXT("Content-Type"), TEXT("application/json"));
SeverityRequest->SetURL(FullURL + "/bug" + "?" + QueryString);
SeverityRequest->OnProcessRequestComplete().BindUObject(this, &ABugMarkerLoader::ServerBugResponse);
SeverityRequest->ProcessRequest();
}
void ABugMarkerLoader::EndPlay(const EEndPlayReason::Type EndPlayReason)
{
this->GetWorldTimerManager().ClearTimer(this->NewBatchTimerHandle);
this->GetWorldTimerManager().ClearTimer(this->UnloadAllTimerHandle);
Super::EndPlay(EndPlayReason);
}
void ABugMarkerLoader::ServerBugResponse(FHttpRequestPtr Request, FHttpResponsePtr Response, bool Success)
{
if (Success)
{
FJSONBugResponse ResponseData;
FString JSONResponse = Response->GetContentAsString();
FJsonObjectConverter::JsonObjectStringToUStruct(JSONResponse, &ResponseData);
if (ResponseData.error)
{
this->Destroy();
return;
}
else
{
this->BugBatch = ResponseData.bugs;
this->GetWorldTimerManager().SetTimer(this->NewBatchTimerHandle, this, &ABugMarkerLoader::LoadNewBatch, 1.0f/*this->GetWorld()->GetDeltaSeconds()*/);
}
}
else
{
this->Destroy();
return;
}
}
void ABugMarkerLoader::LoadNewBatch()
{
while (!this->BugBatch.IsEmpty())
{
const FJSONBugData BugData = this->BugBatch[0];
FString LocationString, UpVectorString;
BugData.cf_location.Split(":", &LocationString, &UpVectorString);
FString LocationX, LocationY, LocationZ;
LocationString.Split(",", &LocationX, &LocationY);
LocationY.Split(",", &LocationY, &LocationZ);
FString UpVectorX, UpVectorY, UpVectorZ;
UpVectorString.Split(",", &UpVectorX, &UpVectorY);
UpVectorY.Split(",", &UpVectorY, &UpVectorZ);
const FVector Location(FCString::Atof(*LocationX),FCString::Atof(*LocationY),FCString::Atof(*LocationZ));
const FVector UpVector(FCString::Atof(*UpVectorX),FCString::Atof(*UpVectorY),FCString::Atof(*UpVectorZ));
TSubclassOf<ABugMarkerActor> Class = StaticLoadClass(ABugMarkerActor::StaticClass(), this, BUG_MARKER_ACTOR_BP);
FActorSpawnParameters SpawnParams;
SpawnParams.Owner = this;
const FTransform Transform = FTransform(FRotationMatrix::MakeFromZ(UpVector).Rotator(), Location, FVector::OneVector);
ABugMarkerActor *Marker = this->GetWorld()->SpawnActor<ABugMarkerActor>(Class, Transform, SpawnParams);
Marker->SetBugData(BugData);
this->Markers.Add(Marker);
this->BugBatch.RemoveAt(0);
if (!(this->BugBatch.Num() % GetDefault<UUnrealzillaGlobalSettings>()->LoadMarkersBatch))
{
break;
}
}
if (!this->BugBatch.IsEmpty())
{
this->GetWorldTimerManager().SetTimer(this->NewBatchTimerHandle, this, &ABugMarkerLoader::LoadNewBatch, this->GetWorld()->GetDeltaSeconds());
}
}
void ABugMarkerLoader::UnloadAll()
{
this->GetWorldTimerManager().ClearTimer(this->NewBatchTimerHandle);
while (!this->Markers.IsEmpty())
{
AActor *Marker = this->Markers[0];
this->Markers.RemoveAt(0);
Marker->Destroy();
if (!(this->Markers.Num() % GetDefault<UUnrealzillaGlobalSettings>()->UnloadMarkersBatch))
{
break;
}
}
if (!this->Markers.IsEmpty())
{
this->GetWorldTimerManager().SetTimer(this->UnloadAllTimerHandle, this, &ABugMarkerLoader::UnloadAll, this->GetWorld()->GetDeltaSeconds());
}
else
{
this->Destroy();
}
}