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 // When playing in editor the map name gets an extra prefix, here we
24 // remove it.
25 FString CorrectedMapName = MapName;
26 constexpr auto PIEPrefix = TEXT("UEDPIE_0_");
27 CorrectedMapName.RemoveFromStart(PIEPrefix);
28 MapName = CorrectedMapName;
29 }
30#endif // WITH_EDITOR
31
32 MapName += TEXT(".xodr");
33
34 const FString DefaultFilePath =
35 FPaths::ProjectContentDir() +
36 TEXT("Carla/Maps/OpenDrive/") +
37 MapName;
38
39 auto &FileManager = IFileManager::Get();
40
41 if (FileManager.FileExists(*DefaultFilePath))
42 {
43 return DefaultFilePath;
44 }
45
46 TArray<FString> FilesFound;
47 FileManager.FindFilesRecursive(
48 FilesFound,
49 *FPaths::ProjectContentDir(),
50 *MapName,
51 true,
52 false,
53 false);
54
55 return FilesFound.Num() > 0 ? FilesFound[0u] : FString{};
56}
57
58FString UOpenDrive::GetXODR(const UWorld *World)
59{
60 auto MapName = World->GetMapName();
61
62 // When playing in editor the map name gets an extra prefix, here we
63 // remove it.
64 #if WITH_EDITOR
65 {
66 FString CorrectedMapName = MapName;
67 constexpr auto PIEPrefix = TEXT("UEDPIE_0_");
68 CorrectedMapName.RemoveFromStart(PIEPrefix);
69 MapName = CorrectedMapName;
70 }
71 #endif // WITH_EDITOR
72
74
75 auto MapDir = GameMode->GetFullMapPath();
76 const auto FolderDir = MapDir + "/OpenDrive/";
77 const auto FileName = MapDir.EndsWith(MapName) ? "*" : MapName;
78
79 // Find all the xodr and bin files from the map
80 TArray<FString> Files;
81 IFileManager::Get().FindFilesRecursive(Files, *FolderDir, *FString(FileName + ".xodr"), true, false, false);
82
83 FString Content;
84
85 if (!Files.Num())
86 {
87 UE_LOG(LogTemp, Error, TEXT("Failed to find OpenDrive file for map '%s'"), *MapName);
88 }
89 else if (FFileHelper::LoadFileToString(Content, *Files[0]))
90 {
91 UE_LOG(LogTemp, Log, TEXT("Loaded OpenDrive file '%s'"), *Files[0]);
92 }
93 else
94 {
95 UE_LOG(LogTemp, Error, TEXT("Failed to load OpenDrive file '%s'"), *Files[0]);
96 }
97
98 return Content;
99}
100
101FString UOpenDrive::LoadXODR(const FString &MapName)
102{
103 const auto FilePath = FindPathToXODRFile(MapName);
104
105 FString Content;
106
107 if (FilePath.IsEmpty())
108 {
109 UE_LOG(LogTemp, Error, TEXT("Failed to find OpenDrive file for map '%s'"), *MapName);
110 }
111 else if (FFileHelper::LoadFileToString(Content, *FilePath))
112 {
113 UE_LOG(LogTemp, Log, TEXT("Loaded OpenDrive file '%s'"), *FilePath);
114 }
115 else
116 {
117 UE_LOG(LogTemp, Error, TEXT("Failed to load OpenDrive file '%s'"), *FilePath);
118 }
119
120 return Content;
121}
122
123FString UOpenDrive::GetXODRByPath(FString XODRPath, FString MapName){
124
125 // When playing in editor the map name gets an extra prefix, here we
126 // remove it.
127 #if WITH_EDITOR
128 {
129 FString CorrectedMapName = MapName;
130 constexpr auto PIEPrefix = TEXT("UEDPIE_0_");
131 CorrectedMapName.RemoveFromStart(PIEPrefix);
132 MapName = CorrectedMapName;
133 }
134 #endif // WITH_EDITOR
135
136 FString FileName = XODRPath.EndsWith(MapName) ? "*" : MapName;
137 FString FolderDir = XODRPath;
138 FolderDir.RemoveFromEnd(MapName + ".xodr");
139
140 // Find all the xodr and bin files from the map
141 TArray<FString> Files;
142 IFileManager::Get().FindFilesRecursive(Files, *FolderDir, *FString(FileName + ".xodr"), true, false, false);
143
144 FString Content;
145
146 if (!Files.Num())
147 {
148 UE_LOG(LogTemp, Error, TEXT("Failed to find OpenDrive file for map '%s'"), *MapName);
149 }
150 else if (FFileHelper::LoadFileToString(Content, *Files[0]))
151 {
152 UE_LOG(LogTemp, Log, TEXT("Loaded OpenDrive file '%s'"), *Files[0]);
153 }
154
155 return Content;
156}
157
159{
160 UOpenDriveMap *Map = nullptr;
161 auto XODRContent = LoadXODR(MapName);
162 if (!XODRContent.IsEmpty())
163 {
164 Map = NewObject<UOpenDriveMap>();
165 Map->Load(XODRContent);
166 }
167 return Map;
168}
169
171{
172 UWorld *World = GEngine->GetWorldFromContextObject(WorldContextObject, EGetWorldErrorMode::LogAndReturnNull);
173 return World != nullptr ?
174 LoadOpenDriveMap(World->GetMapName()) :
175 nullptr;
176}
Base class for the CARLA Game Mode.
const FString GetFullMapPath() const
static ACarlaGameModeBase * GetGameMode(const UObject *WorldContextObject)
将 CARLA OpenDrive API 公开给蓝图的辅助类。
bool Load(const FString &XODRContent)
Load this map with an OpenDrive (XODR) file.
static UOpenDriveMap * LoadCurrentOpenDriveMap(const UObject *WorldContextObject)
Load OpenDriveMap associated to the currently loaded map.
static FString FindPathToXODRFile(const FString &InMapName)
static FString GetXODR(const UWorld *World)
Return the OpenDrive XML associated to MapName, or empty if the file is not found.
static UOpenDriveMap * LoadOpenDriveMap(const FString &MapName)
Load OpenDriveMap associated to the given MapName.
static FString GetXODRByPath(FString XODRPath, FString MapName)
Return the OpenDrive XML associated to MapName, or empty if the file is not found.