CARLA
 
载入中...
搜索中...
未找到
Unreal/CarlaUE4/Plugins/Carla/Source/Carla/OpenDrive/OpenDrive.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"
10#include "Misc/FileHelper.h"
12#include "GenericPlatform/GenericPlatformProcess.h"
13
14#include "Runtime/Core/Public/HAL/FileManagerGeneric.h"
15#include "Misc/FileHelper.h"
16
17FString UOpenDrive::FindPathToXODRFile(const FString &InMapName){
18
19 FString MapName = InMapName;
20
21#if WITH_EDITOR
22 {
23 // 在编辑器中游玩时,地图名称会多出一个前缀,这里我们将其移除。
24 FString CorrectedMapName = MapName;
25 constexpr auto PIEPrefix = TEXT("UEDPIE_0_");
26 CorrectedMapName.RemoveFromStart(PIEPrefix);
27 MapName = CorrectedMapName;
28 }
29#endif // WITH_EDITOR
30
31 // 向地图名称添加.xodr扩展名
32 MapName += TEXT(".xodr");
33
34 // 定义默认文件路径,位于项目的Content目录下的Carla/Maps/OpenDrive子目录中
35 const FString DefaultFilePath =
36 FPaths::ProjectContentDir() +
37 TEXT("Carla/Maps/OpenDrive/") +
38 MapName;
39
40 // 获取文件管理器的引用
41 auto &FileManager = IFileManager::Get();
42
43 // 检查文件管理器是否存在指定的默认文件路径
44 if (FileManager.FileExists(*DefaultFilePath))
45 {
46 // 如果文件存在,则返回该路径
47 return DefaultFilePath;
48 }
49
50 // 创建一个字符串数组,用于存储查找到的文件
51 TArray<FString> FilesFound;
52 // 递归查找项目内容目录下所有匹配MapName的文件
53 FileManager.FindFilesRecursive(
54 FilesFound,
55 *FPaths::ProjectContentDir(),
56 *MapName,
57 true,// 搜索子目录
58 false,// 不递归查找子目录中的子目录
59 false);// 只查找文件,不查找目录
60
61 // 返回找到的第一个文件的路径,如果没有找到任何文件,则返回空字符串
62 return FilesFound.Num() > 0 ? FilesFound[0u] : FString{};
63}
64
65// UOpenDrive类的成员函数,用于获取当前世界对应的OpenDrive文件路径
66FString UOpenDrive::GetXODR(const UWorld *World)
67{
68 // 获取世界对象的地图名称
69 auto MapName = World->GetMapName();
70
71 // 在编辑器中游玩时,地图名称会多出一个前缀,这里我们将其移除。
72 #if WITH_EDITOR
73 {
74 FString CorrectedMapName = MapName;
75 constexpr auto PIEPrefix = TEXT("UEDPIE_0_");
76 CorrectedMapName.RemoveFromStart(PIEPrefix);
77 MapName = CorrectedMapName;
78 }
79 #endif // WITH_EDITOR
80
82
83 auto MapDir = GameMode->GetFullMapPath();
84 const auto FolderDir = MapDir + "/OpenDrive/";
85 const auto FileName = MapDir.EndsWith(MapName) ? "*" : MapName;
86
87 // 查找地图中所有.xodr和.bin文件。
88 TArray<FString> Files;
89 // 递归查找指定文件夹下所有.xodr文件
90 IFileManager::Get().FindFilesRecursive(Files, *FolderDir, *FString(FileName + ".xodr"), true, false, false);
91
92 FString Content;
93
94 // 如果没有找到文件
95 if (!Files.Num())
96 {
97 // 记录错误日志,表示没有找到OpenDrive文件
98 UE_LOG(LogTemp, Error, TEXT("Failed to find OpenDrive file for map '%s'"), *MapName);
99 }
100 // 如果成功加载了文件
101 else if (FFileHelper::LoadFileToString(Content, *Files[0]))
102 {
103 // 记录日志,表示成功加载了OpenDrive文件
104 UE_LOG(LogTemp, Log, TEXT("Loaded OpenDrive file '%s'"), *Files[0]);
105 }
106 // 如果加载文件失败
107 else
108 {
109 // 记录错误日志,表示加载OpenDrive文件失败
110 UE_LOG(LogTemp, Error, TEXT("Failed to load OpenDrive file '%s'"), *Files[0]);
111 }
112
113 // 返回文件内容
114 return Content;
115}
116
117// UOpenDrive类的成员函数,用于加载指定地图的OpenDrive文件
118FString UOpenDrive::LoadXODR(const FString &MapName)
119{
120 // 调用FindPathToXODRFile函数查找OpenDrive文件的路径
121 const auto FilePath = FindPathToXODRFile(MapName);
122
123 FString Content;
124
125 // 如果文件路径为空
126 if (FilePath.IsEmpty())
127 {
128 // 记录错误日志,表示没有找到OpenDrive文件
129 UE_LOG(LogTemp, Error, TEXT("Failed to find OpenDrive file for map '%s'"), *MapName);
130 }
131 // 如果成功加载了文件
132 else if (FFileHelper::LoadFileToString(Content, *FilePath))
133 {
134 // 记录日志,表示成功加载了OpenDrive文件
135 UE_LOG(LogTemp, Log, TEXT("Loaded OpenDrive file '%s'"), *FilePath);
136 }
137 // 如果加载文件失败
138 else
139 {
140 // 记录错误日志,表示加载OpenDrive文件失败
141 UE_LOG(LogTemp, Error, TEXT("Failed to load OpenDrive file '%s'"), *FilePath);
142 }
143
144 // 返回文件内容
145 return Content;
146}
147
148// UOpenDrive类的成员函数,用于根据给定的路径和地图名称获取OpenDrive文件内容
149FString UOpenDrive::GetXODRByPath(FString XODRPath, FString MapName){
150
151 // 在编辑器中游玩时,地图名称会多出一个前缀,这里我们将其移除。
152 #if WITH_EDITOR
153 {
154 FString CorrectedMapName = MapName;
155 constexpr auto PIEPrefix = TEXT("UEDPIE_0_");
156 CorrectedMapName.RemoveFromStart(PIEPrefix);
157 MapName = CorrectedMapName;
158 }
159 #endif // WITH_EDITOR
160
161 FString FileName = XODRPath.EndsWith(MapName) ? "*" : MapName;
162 FString FolderDir = XODRPath;
163 FolderDir.RemoveFromEnd(MapName + ".xodr");
164
165 // 查找地图中所有.xodr和.bin文件。
166 TArray<FString> Files;
167 IFileManager::Get().FindFilesRecursive(Files, *FolderDir, *FString(FileName + ".xodr"), true, false, false);
168
169 FString Content;
170
171 if (!Files.Num())
172 {
173 UE_LOG(LogTemp, Error, TEXT("Failed to find OpenDrive file for map '%s'"), *MapName);
174 }
175 else if (FFileHelper::LoadFileToString(Content, *Files[0]))
176 {
177 UE_LOG(LogTemp, Log, TEXT("Loaded OpenDrive file '%s'"), *Files[0]);
178 }
179
180 return Content;
181}
182
184{
185 UOpenDriveMap *Map = nullptr;
186 auto XODRContent = LoadXODR(MapName);
187 if (!XODRContent.IsEmpty())
188 {
189 Map = NewObject<UOpenDriveMap>();
190 Map->Load(XODRContent);
191 }
192 return Map;
193}
194
196{
197 UWorld *World = GEngine->GetWorldFromContextObject(WorldContextObject, EGetWorldErrorMode::LogAndReturnNull);
198 return World != nullptr ?
199 LoadOpenDriveMap(World->GetMapName()) :
200 nullptr;
201}
UE_LOG(LogCarla, Log, TEXT("UActorDispatcher::Destroying actor: '%s' %x"), *Id, Actor)
TSharedPtr< const FActorInfo > carla::rpc::ActorState UWorld * World
CARLA Game Mode 的基类。
const FString GetFullMapPath() const
地图类的前向声明,用于在LaneInvasionSensor类中可能的引用。
static ACarlaGameModeBase * GetGameMode(const UObject *WorldContextObject)
将 CARLA OpenDrive API 公开给蓝图的辅助类。
static UOpenDriveMap * LoadCurrentOpenDriveMap(const UObject *WorldContextObject)
뵱ǰصӳOpenDriveMapҲ뵱ǰӳƥXODR򷵻nullptr
static FString FindPathToXODRFile(const FString &InMapName)
static FString GetXODR(const UWorld *World)
MapNameOpenDrive XMLҲļ򷵻ؿա
static UOpenDriveMap * LoadOpenDriveMap(const FString &MapName)
MapNameOpenDriveMapҲͬMapNameODR򷵻nullptr
static FString GetXODRByPath(FString XODRPath, FString MapName)
MapNameOpenDrive XMLҲļ򷵻ؿա