14#include <util/ue-header-guard-begin.h>
16#include "JsonUtilities.h"
17#include "Misc/FileHelper.h"
18#include "Misc/Paths.h"
19#include <util/ue-header-guard-end.h>
31 TArray<FBlueprintParameters> PackBlueprintsParams;
42 const FTransform &SpawnAtTransform,
48 UE_LOG(LogCarla, Error, TEXT(
"Actor Description Class is null."));
49 SpawnResult.
Status = EActorSpawnResultStatus::InvalidDescription;
53 AActor* SpawnedActor = GetWorld()->SpawnActor<
AActor>(ActorDescription.
Class, SpawnAtTransform);
54 SpawnResult.
Actor = SpawnedActor;
56 if(SpawnedActor ==
nullptr)
58 SpawnResult.
Status = EActorSpawnResultStatus::Collision;
64 SpawnResult.
Status = EActorSpawnResultStatus::Success;
68 SpawnResult.
Status = EActorSpawnResultStatus::UnknownError;
80 TSharedPtr<FJsonObject> JsonObject = MakeShareable(
new FJsonObject);
81 JsonObject->SetStringField(TEXT(
"Name"), BlueprintParams.
Name);
82 JsonObject->SetStringField(TEXT(
"Path"), BlueprintParams.
Path);
89 TArray<TSharedPtr<FJsonValue>> JsonArray;
95 JsonArray.Add(MakeShareable(
new FJsonValueObject(JsonObject)));
99 TSharedRef<FJsonObject> RootObject = MakeShareable(
new FJsonObject);
100 RootObject->SetArrayField(TEXT(
"Blueprints"), JsonArray);
103 FString OutputString;
104 TSharedRef<TJsonWriter<>> Writer = TJsonWriterFactory<>::Create(&OutputString);
105 FJsonSerializer::Serialize(RootObject, Writer);
112 FString FilePath = FPaths::ProjectContentDir() + TEXT(
"Carla/Config/") + FileName;
117 if (FFileHelper::SaveStringToFile(JsonContent, *FilePath))
119 UE_LOG(LogCarla, Log, TEXT(
"JSON file successfully saved at: %s"), *FilePath);
123 UE_LOG(LogCarla, Error, TEXT(
"Failed to save JSON file at: %s"), *FilePath);
129 if (!JsonObject.IsValid())
return false;
131 JsonObject->TryGetStringField(TEXT(
"Name"), OutBlueprintParams.
Name);
132 JsonObject->TryGetStringField(TEXT(
"Path"), OutBlueprintParams.
Path);
139 TSharedPtr<FJsonObject> RootObject;
140 TSharedRef<TJsonReader<>> Reader = TJsonReaderFactory<>::Create(JsonString);
142 if (!FJsonSerializer::Deserialize(Reader, RootObject) || !RootObject.IsValid())
144 UE_LOG(LogCarla, Error, TEXT(
"Failed to parse JSON."));
149 const TArray<TSharedPtr<FJsonValue>>* BlueprintsArray;
150 if (RootObject->TryGetArrayField(TEXT(
"Blueprints"), BlueprintsArray))
152 OutBlueprintParamsArray.Empty();
153 for (
const TSharedPtr<FJsonValue>& BlueprintValue : *BlueprintsArray)
155 TSharedPtr<FJsonObject> BlueprintObject = BlueprintValue->AsObject();
156 if (BlueprintObject.IsValid())
161 OutBlueprintParamsArray.Add(BlueprintParams);
175 FString FilePath = FPaths::IsRelative(FileName)
176 ? FPaths::ProjectContentDir() + TEXT(
"Carla/Config/") + FileName
181 if (FFileHelper::LoadFileToString(JsonContent, *FilePath))
186 UE_LOG(LogCarla, Log, TEXT(
"Blueprint parameters loaded successfully from %s"), *FilePath);
190 UE_LOG(LogCarla, Error, TEXT(
"Failed to parse Blueprint parameters from %s"), *FilePath);
195 UE_LOG(LogCarla, Error, TEXT(
"Failed to load file: %s"), *FilePath);
static bool IsValid(const ACarlaWheeledVehicle *Vehicle)
TArray< FBlueprintParameters > BlueprintsParams
virtual TArray< FActorDefinition > GetDefinitions() override
Retrieve the definitions of the static mesh actor.
static FString FBlueprintParametersArrayToJson(const TArray< FBlueprintParameters > &BlueprintParamsArray)
TArray< FActorDefinition > Definitions
static void LoadBlueprintParametersArrayFromFile(const FString &FileName, TArray< FBlueprintParameters > &OutBlueprintParamsArray)
virtual FActorSpawnResult SpawnActor(const FTransform &SpawnAtTransform, const FActorDescription &ActorDescription) override
Spawn an actor based on ActorDescription and Transform.
static bool JsonToFBlueprintParametersArray(const FString &JsonString, TArray< FBlueprintParameters > &OutBlueprintParamsArray)
static TSharedPtr< FJsonObject > FBlueprintParametersToJsonObject(const FBlueprintParameters &BlueprintParams)
static bool JsonToFBlueprintParameters(const TSharedPtr< FJsonObject > JsonObject, FBlueprintParameters &OutBlueprintParams)
virtual void ReleaseContentPack(const FString &MountPoint) override
Drop every cached definition, parameter and loaded asset that refers to content under MountPoint ("/<...
bool PostProcessBlueprint(AActor *SpawnedActor, const FActorDescription &BlueprintParams)
static void SaveBlueprintParametersArrayToFile(const TArray< FBlueprintParameters > &BlueprintParamsArray, const FString &FileName)
static bool DefinitionReferencesPath(const FActorDefinition &Definition, const FString &MountPoint)
True when the definition's class or any attribute/variation value points at content under MountPoint.
static void MakeBlueprintDefinitions(const TArray< FBlueprintParameters > &ParameterArray, TArray< FActorDefinition > &Definitions)
static void MergeCatalog(TArray< T > &Into, const TArray< T > &From, KeyFn Key)
Merge catalog entries: an entry whose key already exists replaces it in place (later files override b...
static TArray< FString > FindPackCatalogFiles(const FString &RelativeName)
<ContentDir>/Config/<RelativeName> of every mounted pack where the file exists, in mount order (later...
A definition of a Carla Actor with all the variation and attributes.
A description of a Carla Actor with all its variation.
TSubclassOf< AActor > Class
Class of the actor to be spawned.
Result of an actor spawn function.
EActorSpawnResultStatus Status