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