CARLA
 
载入中...
搜索中...
未找到
CarlaBlueprintRegistry.cpp
浏览该文件的文档.
1// Copyright (c) 2019 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
7#include "Carla.h" // 包含CARLA的主头文件
8#include "Carla/Actor/CarlaBlueprintRegistry.h" // 包含CARLA蓝图注册表的头文件
9#include "Carla/Game/CarlaStatics.h" // 包含CARLA静态数据的头文件
10#include "Dom/JsonObject.h" // 包含JSON对象的头文件
11#include "Misc/FileHelper.h" // 包含文件帮助函数的头文件
12#include "Serialization/JsonReader.h" // 包含JSON读取器的头文件
13#include "Serialization/JsonSerializer.h" // 包含JSON序列化的头文件
14
16 // 定义一些通用属性路径
17 static const FString PATH = FPaths::ProjectContentDir(); // 项目内容目录的路径
18 static const FString DEFAULT = TEXT("/Carla/Config/Default"); // 默认配置文件的相对路径
19 static const FString DEFINITIONS = TEXT("props"); // 定义属性数组的JSON字段名
20}
21
22namespace PropAttributes {
23 // 定义一些属性相关的JSON字段名
24 static const FString REGISTRY_FORMAT = TEXT(".Package.json"); // 属性注册表文件的扩展名
25 static const FString NAME = TEXT("name"); // 属性名称的JSON字段名
26 static const FString MESH_PATH = TEXT("path"); // 网格路径的JSON字段名
27 static const FString SIZE = TEXT("size"); // 属性大小的JSON字段名
28}
29
30// 将属性大小枚举转换为字符串
31static FString PropSizeTypeToString(EPropSize PropSizeType)
32{
33 const UEnum *ptr = FindObject<UEnum>(ANY_PACKAGE, TEXT("EPropSize"), true); // 查找EPropSize枚举
34 if (!ptr) // 如果枚举未找到
35 {
36 return FString("unknown"); // 返回未知字符串
37 }
38 return ptr->GetNameStringByIndex(static_cast<int32>(PropSizeType)); // 返回枚举项的名称字符串
39}
40
41// 将字符串转换为属性大小枚举
42static EPropSize StringToPropSizeType(FString PropSize)
43{
44 const UEnum *EnumPtr = FindObject<UEnum>(ANY_PACKAGE, TEXT("EPropSize"), true); // 查找EPropSize枚举
45 if (EnumPtr) // 如果枚举找到
46 {
47 return (EPropSize)EnumPtr->GetIndexByName(FName(*PropSize)); // 返回枚举项的索引
48 }
49 return EPropSize::INVALID; // 如果枚举未找到,返回无效值
50}
51
52// 将属性添加到CARLA蓝图注册表
53void UCarlaBlueprintRegistry::AddToCarlaBlueprintRegistry(const TArray<FPropParameters> &PropParametersArray)
54{
55 TArray<TSharedPtr<FJsonValue>> ResultPropJsonArray; // 存储属性JSON值的数组
56 // 加载默认属性文件
57 FString DefaultPropFilePath = CommonAttributes::PATH + CommonAttributes::DEFAULT +
58 PropAttributes::REGISTRY_FORMAT; // 构造默认属性文件的完整路径
59 FString JsonString; // 存储JSON字符串
60 FFileHelper::LoadFileToString(JsonString, *DefaultPropFilePath); // 从文件加载JSON字符串
61 TSharedPtr<FJsonObject> JsonObject = MakeShareable(new FJsonObject()); // 创建JSON对象
62 TSharedRef<TJsonReader<>> JsonReader = TJsonReaderFactory<>::Create(JsonString); // 创建JSON读取器
63 // 填充属性数组并将每个属性的索引保存到PropIndexes地图中
64 TMap<FString, int> PropIndexes; // 存储属性名称和索引的映射
65 if (FJsonSerializer::Deserialize(JsonReader, JsonObject) && JsonObject.IsValid()) // 如果JSON反序列化成功且对象有效
66 {
67 ResultPropJsonArray = JsonObject->GetArrayField(CommonAttributes::DEFINITIONS); // 获取属性数组字段
68 }
69}
70
71 for (int32 i = 0; i < ResultPropJsonArray.Num(); ++i)
72 {
73 TSharedPtr<FJsonObject> PropJsonObject = ResultPropJsonArray[i]->AsObject();
74 FString Name = PropJsonObject->GetStringField(PropAttributes::NAME);
75 PropIndexes.Add(Name, i);
76 }
77 }
78 // 添加输入属性或更新它们(如果已存在)
79 for (auto &PropParameter : PropParametersArray)
80 {
81 TSharedPtr<FJsonObject> PropJsonObject;
82 // 创建对象或更新现有对象
83 int *PropIndex = PropIndexes.Find(PropParameter.Name);
84 if (PropIndex)
85 {
86 PropJsonObject = ResultPropJsonArray[*PropIndex]->AsObject();
87 }
88 else
89 {
90 PropJsonObject = MakeShareable(new FJsonObject);
91 }
92 // 填充属性 JSON
93 PropJsonObject->SetStringField(PropAttributes::NAME, PropParameter.Name);
94 PropJsonObject->SetStringField(PropAttributes::MESH_PATH, PropParameter.Mesh->GetPathName());
95 PropJsonObject->SetStringField(PropAttributes::SIZE, PropSizeTypeToString(PropParameter.Size));
96 //添加或更新
97 TSharedRef<FJsonValue> PropJsonValue = MakeShareable(new FJsonValueObject(PropJsonObject));
98 if (PropIndex)
99 {
100 ResultPropJsonArray[*PropIndex] = PropJsonValue;
101 }
102 else
103 {
104 ResultPropJsonArray.Add(PropJsonValue);
105 PropIndexes.Add(PropParameter.Name, ResultPropJsonArray.Num() - 1);
106 }
107 }
108 // 更新Json对象
109 JsonObject->SetArrayField(CommonAttributes::DEFINITIONS, ResultPropJsonArray);
110 // 序列化文件
112 TSharedRef<TJsonWriter<>> Writer = TJsonWriterFactory<>::Create(&OutputString);
113 FJsonSerializer::Serialize(JsonObject.ToSharedRef(), Writer);
114 // 保存文件
115 FFileHelper::SaveStringToFile(OutputString, *DefaultPropFilePath);
116}
117void UCarlaBlueprintRegistry::LoadPropDefinitions(TArray<FPropParameters> &PropParametersArray)
118{
119 // 在Unreal Content文件夹中查找所有Package.json文件
120 const FString WildCard = FString("*").Append(PropAttributes::REGISTRY_FORMAT);
121 TArray<FString> PropFileNames;
122 IFileManager::Get().FindFilesRecursive(PropFileNames,
124 *WildCard,
125 true,
126 false,
127 false);
128 // 如果存在,则先排序并放置默认文件
129 PropFileNames.Sort();
130 FString DefaultFileName;
131 for (int32 i = 0; i < PropFileNames.Num() && DefaultFileName.IsEmpty(); ++i)
132 {
133 if (PropFileNames[i].Contains(CommonAttributes::DEFAULT))
134 {
135 DefaultFileName = PropFileNames[i];
136 PropFileNames.RemoveAt(i);
137 }
138 }
139 if (!DefaultFileName.IsEmpty())
140 {
141 PropFileNames.Insert(DefaultFileName, 0);
142 }
143 // 读取所有注册表文件并用用户值覆盖默认注册表值
144 // 注册表文件
145 TMap<FString, int> PropIndexes;
146 for (int32 i = 0; i < PropFileNames.Num(); ++i)
147 {
148 FString FileJsonContent;
149 if (FFileHelper::LoadFileToString(FileJsonContent, *PropFileNames[i]))
150 {
151 TSharedPtr<FJsonObject> JsonParsed;
152 TSharedRef<TJsonReader<TCHAR>> JsonReader = TJsonReaderFactory<TCHAR>::Create(FileJsonContent);
153 if (FJsonSerializer::Deserialize(JsonReader, JsonParsed))
154 {
155 auto PropJsonArray = JsonParsed->GetArrayField(CommonAttributes::DEFINITIONS);
156 for (auto &PropJsonValue : PropJsonArray)
157 {
158 // 读取属性JSON
159 TSharedPtr<FJsonObject> PropJsonObject = PropJsonValue->AsObject();
160 FString PropName = PropJsonObject->GetStringField(PropAttributes::NAME);
161 FString PropMeshPath = PropJsonObject->GetStringField(PropAttributes::MESH_PATH);
162 FString PropSize = PropJsonObject->GetStringField(PropAttributes::SIZE);
163 // 构建属性参数
164 UStaticMesh *PropMesh = LoadObject<UStaticMesh>(nullptr, *PropMeshPath);
165 EPropSize PropSizeType = StringToPropSizeType(PropSize);
166 FPropParameters Params {PropName, PropMesh, PropSizeType};
167 // 添加或更新
168 if (PropIndexes.Contains(PropName))
169 {
170 PropParametersArray[PropIndexes[PropName]] = Params;
171 }
172 else
173 {
174 PropParametersArray.Add(Params);
175 PropIndexes.Add(Params.Name, PropParametersArray.Num() - 1);
176 }
177 }
178 }
179 }
180 }
181}
static EPropSize StringToPropSizeType(FString PropSize)
TSharedRef< TJsonWriter<> > Writer
static FString PropSizeTypeToString(EPropSize PropSizeType)
FString OutputString
EPropSize
static void AddToCarlaBlueprintRegistry(const TArray< FPropParameters > &PropParametersArray)
static void LoadPropDefinitions(TArray< FPropParameters > &PropParametersArray)
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