CARLA
Loading...
Searching...
No Matches
CarlaBlueprintRegistry.cpp
Go to the documentation of this file.
1// Copyright (c) 2026 Computer Vision Center (CVC) at the Universitat Autonoma
2// de Barcelona (UAB).
3//
4// This work is licensed under the terms of the MIT license.
5// For a copy, see <https://opensource.org/licenses/MIT>.
6
8#include "Carla.h"
11
12#include <util/ue-header-guard-begin.h>
13#include "Dom/JsonObject.h"
14#include "Misc/FileHelper.h"
15#include "Serialization/JsonReader.h"
16#include "Serialization/JsonSerializer.h"
17#include <util/ue-header-guard-end.h>
18
20 static const FString PATH = FPaths::ProjectContentDir();
21 static const FString DEFAULT = TEXT("/Carla/Config/Default");
22 static const FString DEFINITIONS = TEXT("props");
23}
24
25namespace PropAttributes {
26 static const FString REGISTRY_FORMAT = TEXT(".Package.json");
27 static const FString NAME = TEXT("name");
28 static const FString MESH_PATH = TEXT("path");
29 static const FString SIZE = TEXT("size");
30}
31
32static FString PropSizeTypeToString(EPropSize PropSizeType)
33{
34 const UEnum *ptr = FindObject<UEnum>(nullptr, TEXT("/Script/Carla.EPropSize"), true);
35 if (!ptr)
36 {
37 return FString("unknown");
38 }
39 return ptr->GetNameStringByIndex(static_cast<int32>(PropSizeType));
40}
41
42static EPropSize StringToPropSizeType(FString PropSize)
43{
44 const UEnum *EnumPtr = FindObject<UEnum>(nullptr, TEXT("/Script/Carla.EPropSize"), true);
45 if (EnumPtr)
46 {
47 return (EPropSize) EnumPtr->GetIndexByName(FName(*PropSize));
48 }
49 return EPropSize::INVALID;
50}
51
52void UCarlaBlueprintRegistry::AddToCarlaBlueprintRegistry(const TArray<FPropParameters> &PropParametersArray)
53{
54 TArray<TSharedPtr<FJsonValue>> ResultPropJsonArray;
55
56 // Load default props file
57 FString DefaultPropFilePath = CommonAttributes::PATH + CommonAttributes::DEFAULT +
59 FString JsonString;
60 FFileHelper::LoadFileToString(JsonString, *DefaultPropFilePath);
61
62 TSharedPtr<FJsonObject> JsonObject = MakeShareable(new FJsonObject());
63 TSharedRef<TJsonReader<>> JsonReader = TJsonReaderFactory<>::Create(JsonString);
64
65 // Fill Array of Props and save indexes of each prop in PropIndexes Map
66 TMap<FString, int> PropIndexes;
67 if (FJsonSerializer::Deserialize(JsonReader, JsonObject) && JsonObject.IsValid())
68 {
69 ResultPropJsonArray = JsonObject->GetArrayField(CommonAttributes::DEFINITIONS);
70 for (int32 i = 0; i < ResultPropJsonArray.Num(); ++i)
71 {
72 TSharedPtr<FJsonObject> PropJsonObject = ResultPropJsonArray[i]->AsObject();
73
74 FString Name = PropJsonObject->GetStringField(PropAttributes::NAME);
75
76 PropIndexes.Add(Name, i);
77 }
78 }
79
80 // Add Input Props or Update them if already exists
81 for (auto &PropParameter : PropParametersArray)
82 {
83 TSharedPtr<FJsonObject> PropJsonObject;
84
85 // Create object or update existing one
86 int *PropIndex = PropIndexes.Find(PropParameter.Name);
87 if (PropIndex)
88 {
89 PropJsonObject = ResultPropJsonArray[*PropIndex]->AsObject();
90 }
91 else
92 {
93 PropJsonObject = MakeShareable(new FJsonObject);
94 }
95
96 // Fill Prop Json
97 PropJsonObject->SetStringField(PropAttributes::NAME, PropParameter.Name);
98 PropJsonObject->SetStringField(PropAttributes::MESH_PATH, PropParameter.Mesh.ToSoftObjectPath().ToString());
99 PropJsonObject->SetStringField(PropAttributes::SIZE, PropSizeTypeToString(PropParameter.Size));
100
101 // Add or Update
102 TSharedRef<FJsonValue> PropJsonValue = MakeShareable(new FJsonValueObject(PropJsonObject));
103 if (PropIndex)
104 {
105 ResultPropJsonArray[*PropIndex] = PropJsonValue;
106 }
107 else
108 {
109 ResultPropJsonArray.Add(PropJsonValue);
110
111 PropIndexes.Add(PropParameter.Name, ResultPropJsonArray.Num() - 1);
112 }
113 }
114
115 // Update Json Object
116 JsonObject->SetArrayField(CommonAttributes::DEFINITIONS, ResultPropJsonArray);
117
118 // Serialize file
119 FString OutputString;
120 TSharedRef<TJsonWriter<>> Writer = TJsonWriterFactory<>::Create(&OutputString);
121 FJsonSerializer::Serialize(JsonObject.ToSharedRef(), Writer);
122
123 // Save file
124 FFileHelper::SaveStringToFile(OutputString, *DefaultPropFilePath);
125
126}
127
128void UCarlaBlueprintRegistry::LoadPropDefinitions(TArray<FPropParameters> &PropParametersArray)
129{
130
131 // Find all Package.json files inside Unreal Content folder
132 const FString WildCard = FString("*").Append(PropAttributes::REGISTRY_FORMAT);
133
134 TArray<FString> PropFileNames;
135 IFileManager::Get().FindFilesRecursive(PropFileNames,
137 *WildCard,
138 true,
139 false,
140 false);
141
142 // Sort and place Default File First if it exists
143 PropFileNames.Sort();
144 FString DefaultFileName;
145 for (int32 i = 0; i < PropFileNames.Num() && DefaultFileName.IsEmpty(); ++i)
146 {
147 if (PropFileNames[i].Contains(CommonAttributes::DEFAULT))
148 {
149 DefaultFileName = PropFileNames[i];
150 PropFileNames.RemoveAt(i);
151 }
152 }
153 if (!DefaultFileName.IsEmpty())
154 {
155 PropFileNames.Insert(DefaultFileName, 0);
156 }
157
158 // Content packs: every <Pack>/Content/Config/**/*.Package.json, appended
159 // after the project files so pack entries override by name.
160 PropFileNames.Append(FindContentPackPropFiles());
161
162 LoadPropDefinitionsFromFiles(PropFileNames, PropParametersArray);
163}
164
166{
167 TArray<FString> PropFileNames;
168 const FString WildCard = FString("*").Append(PropAttributes::REGISTRY_FORMAT);
170 {
171 for (const FString &PackContentDir : ContentPacks->GetPackContentDirs())
172 {
173 TArray<FString> PackPropFileNames;
174 IFileManager::Get().FindFilesRecursive(PackPropFileNames,
175 *(PackContentDir / TEXT("Config")),
176 *WildCard,
177 true,
178 false,
179 false);
180 PackPropFileNames.Sort();
181 PropFileNames.Append(PackPropFileNames);
182 }
183 }
184 return PropFileNames;
185}
186
188 const TArray<FString> &PropFileNames,
189 TArray<FPropParameters> &PropParametersArray)
190{
191 // Read all registry files and overwrite default registry values with user
192 // registry files
193 TMap<FString, int> PropIndexes;
194 for (int32 i = 0; i < PropParametersArray.Num(); ++i)
195 {
196 PropIndexes.Add(PropParametersArray[i].Name, i);
197 }
198
199 for (int32 i = 0; i < PropFileNames.Num(); ++i)
200 {
201 FString FileJsonContent;
202 if (FFileHelper::LoadFileToString(FileJsonContent, *PropFileNames[i]))
203 {
204 TSharedPtr<FJsonObject> JsonParsed;
205 TSharedRef<TJsonReader<TCHAR>> JsonReader = TJsonReaderFactory<TCHAR>::Create(FileJsonContent);
206 if (FJsonSerializer::Deserialize(JsonReader, JsonParsed))
207 {
208 auto PropJsonArray = JsonParsed->GetArrayField(CommonAttributes::DEFINITIONS);
209
210 for (auto &PropJsonValue : PropJsonArray)
211 {
212 // Read Prop Json
213 TSharedPtr<FJsonObject> PropJsonObject = PropJsonValue->AsObject();
214
215 FString PropName = PropJsonObject->GetStringField(PropAttributes::NAME);
216 FString PropMeshPath = PropJsonObject->GetStringField(PropAttributes::MESH_PATH);
217 FString PropSize = PropJsonObject->GetStringField(PropAttributes::SIZE);
218
219 // Build Prop Parameter; the mesh stays a soft reference so the
220 // registry walk does not force-load every asset from disk.
221 EPropSize PropSizeType = StringToPropSizeType(PropSize);
222 FPropParameters Params{
223 PropName,
224 TSoftObjectPtr<UStaticMesh>(FSoftObjectPath(PropMeshPath)),
225 PropSizeType};
226
227 // Add or Update
228 if (PropIndexes.Contains(PropName))
229 {
230 PropParametersArray[PropIndexes[PropName]] = Params;
231 }
232 else
233 {
234 PropParametersArray.Add(Params);
235 PropIndexes.Add(Params.Name, PropParametersArray.Num() - 1);
236 }
237 }
238 }
239 }
240 }
241}
static EPropSize StringToPropSizeType(FString PropSize)
static FString PropSizeTypeToString(EPropSize PropSizeType)
EPropSize
static void AddToCarlaBlueprintRegistry(const TArray< FPropParameters > &PropParametersArray)
static void LoadPropDefinitions(TArray< FPropParameters > &PropParametersArray)
static TArray< FString > FindContentPackPropFiles()
Every <Content>/Config/‍**/*.Package.json of every mounted content pack.
static void LoadPropDefinitionsFromFiles(const TArray< FString > &PropFileNames, TArray< FPropParameters > &PropParametersArray)
Parse the given *.Package.json files in order (later files override earlier entries with the same nam...
Mounts CARLA content packs (content-only Unreal plugins cooked as DLC against a base release,...
static UCarlaContentPackManager * Get()
static const FString DEFINITIONS
static const FString DEFAULT
static const FString PATH
static const FString SIZE
static const FString MESH_PATH
static const FString NAME
static const FString REGISTRY_FORMAT