From 8febd6673948f329e75063f6690660a068260756 Mon Sep 17 00:00:00 2001 From: nsubiron Date: Tue, 21 May 2019 15:13:32 +0200 Subject: [PATCH] Find XODR files anywhere in content --- CHANGELOG.md | 1 + .../Carla/Source/Carla/Game/CarlaStatics.cpp | 2 + .../Carla/Source/Carla/Util/OpenDrive.cpp | 48 ++++++++++++++++--- .../Carla/Source/Carla/Util/OpenDrive.h | 6 +-- 4 files changed, 47 insertions(+), 10 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e6805bfd3..7dd99146a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -34,6 +34,7 @@ * Corrected Latitude in WGS84 reprojection code such that Latitudes increase as one move north in Carla worlds * Register user props in fbx format, make them available in Carla Blueprint Library and spawnable. * Exposed 'is_invincible' for pedestrians + * Fixed XODR files can be found now anywhere in content * Fixed bug related with Pygame error of surface too large, added sidewalks and improved lane markings in `no_rendering_mode.py` * Physics: - Added Friction Trigger Boxes for simulating, for example, slippery surfaces in any region of the map defined by users. diff --git a/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Game/CarlaStatics.cpp b/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Game/CarlaStatics.cpp index 27151576e..cbb5a49dd 100644 --- a/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Game/CarlaStatics.cpp +++ b/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Game/CarlaStatics.cpp @@ -7,6 +7,8 @@ #include "Carla.h" #include "Carla/Game/CarlaStatics.h" +#include "Runtime/Core/Public/HAL/FileManagerGeneric.h" + TArray UCarlaStatics::GetAllMapNames() { TArray TmpStrList, MapNameList; diff --git a/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Util/OpenDrive.cpp b/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Util/OpenDrive.cpp index 4591ae03d..8e68e9de1 100644 --- a/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Util/OpenDrive.cpp +++ b/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Util/OpenDrive.cpp @@ -7,8 +7,11 @@ #include "Carla.h" #include "Carla/Util/OpenDrive.h" -FString FOpenDrive::Load(FString MapName) +#include "Runtime/Core/Public/HAL/FileManagerGeneric.h" + +static FString FOpenDrive_FindPathToXODRFile(const FString &InMapName) { + FString MapName = InMapName; #if WITH_EDITOR { // When playing in editor the map name gets an extra prefix, here we @@ -16,21 +19,52 @@ FString FOpenDrive::Load(FString MapName) FString CorrectedMapName = MapName; constexpr auto PIEPrefix = TEXT("UEDPIE_0_"); CorrectedMapName.RemoveFromStart(PIEPrefix); - UE_LOG(LogCarla, Log, TEXT("FOpenDrive: Corrected map name from %s to %s"), *MapName, *CorrectedMapName); + UE_LOG(LogCarla, Log, TEXT("UOpenDrive: Corrected map name from %s to %s"), *MapName, *CorrectedMapName); MapName = CorrectedMapName; } #endif // WITH_EDITOR - const FString FilePath = + MapName += TEXT(".xodr"); + + const FString DefaultFilePath = FPaths::ProjectContentDir() + TEXT("Carla/Maps/OpenDrive/") + - MapName + - TEXT(".xodr"); + MapName; + + auto &FileManager = IFileManager::Get(); + + if (FileManager.FileExists(*DefaultFilePath)) + { + return DefaultFilePath; + } + + TArray FilesFound; + FileManager.FindFilesRecursive( + FilesFound, + *FPaths::ProjectContentDir(), + *MapName, + true, + false, + false); + + return FilesFound.Num() > 0 ? FilesFound[0u] : FString{}; +} + +FString FOpenDrive::Load(const FString &MapName) +{ + const auto FilePath = FOpenDrive_FindPathToXODRFile(MapName); FString Content; - UE_LOG(LogCarla, Log, TEXT("Loading OpenDrive file '%s'"), *FilePath); - if (!FFileHelper::LoadFileToString(Content, *FilePath)) + if (FilePath.IsEmpty()) + { + UE_LOG(LogTemp, Error, TEXT("Failed to find OpenDrive file for map '%s'"), *MapName); + } + else if (FFileHelper::LoadFileToString(Content, *FilePath)) + { + UE_LOG(LogTemp, Log, TEXT("Loaded OpenDrive file '%s'"), *FilePath); + } + else { UE_LOG(LogTemp, Error, TEXT("Failed to load OpenDrive file '%s'"), *FilePath); } diff --git a/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Util/OpenDrive.h b/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Util/OpenDrive.h index 4e4fc6df3..3a5add290 100644 --- a/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Util/OpenDrive.h +++ b/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Util/OpenDrive.h @@ -10,7 +10,7 @@ class FOpenDrive { public: - /// Return the OpenDrive XML associated to @a MapName, or empty if the such - /// file wasn't serialized. - static FString Load(FString MapName); + /// Return the OpenDrive XML associated to @a MapName, or empty if the file + /// is not found. + static FString Load(const FString &MapName); };