Skip to content

Commit a6a1189

Browse files
authored
Added functionality to load maps from anywhere across different content folders, project or plugin (#8862)
1 parent db1b4fd commit a6a1189

4 files changed

Lines changed: 81 additions & 39 deletions

File tree

Unreal/CarlaUnreal/Plugins/Carla/Source/Carla/Carla.Build.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,8 @@ void AddDynamicLibrary(string library)
128128
"RHI",
129129
"Renderer",
130130
"ProceduralMeshComponent",
131-
"MeshDescription"
131+
"MeshDescription",
132+
"Projects"
132133
});
133134

134135
if (EnableCarSim)

Unreal/CarlaUnreal/Plugins/Carla/Source/Carla/Game/CarlaEpisode.cpp

Lines changed: 4 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -94,38 +94,12 @@ bool UCarlaEpisode::LoadNewEpisode(const FString &MapString, bool ResetSettings)
9494
{
9595
bool bIsFileFound = false;
9696

97-
FString FinalPath = MapString.IsEmpty() ? GetMapName() : MapString;
98-
FinalPath += !MapString.EndsWith(".umap") ? ".umap" : "";
97+
FString FinalPath = UCarlaStatics::FindMapPath(MapString);
9998

100-
if (MapString.StartsWith("/Game"))
99+
if(FPaths::FileExists(FinalPath))
101100
{
102-
// Some conversions...
103-
FinalPath.RemoveFromStart(TEXT("/Game/"));
104-
FinalPath = FPaths::ProjectContentDir() + FinalPath;
105-
FinalPath = IFileManager::Get().ConvertToAbsolutePathForExternalAppForRead(*FinalPath);
106-
107-
if (FPaths::FileExists(FinalPath)) {
108-
bIsFileFound = true;
109-
FinalPath = MapString;
110-
}
111-
}
112-
else
113-
{
114-
if (MapString.Contains("/")) return false;
115-
116-
// Find the full path under Carla
117-
TArray<FString> TempStrArray, PathList;
118-
IFileManager::Get().FindFilesRecursive(PathList, *FPaths::ProjectContentDir(), *FinalPath, true, false, false);
119-
if (PathList.Num() > 0)
120-
{
121-
FinalPath = PathList[0];
122-
FinalPath.ParseIntoArray(TempStrArray, TEXT("Content/"), true);
123-
FinalPath = TempStrArray[1];
124-
FinalPath.ParseIntoArray(TempStrArray, TEXT("."), true);
125-
FinalPath = "/Game/" + TempStrArray[0];
126-
127-
return LoadNewEpisode(FinalPath, ResetSettings);
128-
}
101+
bIsFileFound = true;
102+
FinalPath = MapString;
129103
}
130104

131105
if (bIsFileFound)

Unreal/CarlaUnreal/Plugins/Carla/Source/Carla/Game/CarlaStatics.cpp

Lines changed: 69 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,21 +8,82 @@
88
#include "Carla.h"
99

1010
#include <util/ue-header-guard-begin.h>
11+
#include "Interfaces/IPluginManager.h"
12+
#include "Misc/Paths.h"
13+
#include "Modules/ModuleManager.h"
1114
#include "HAL/FileManagerGeneric.h"
1215
#include <util/ue-header-guard-end.h>
1316

17+
18+
TArray<FString> UCarlaStatics::GetAllPluginContentPaths()
19+
{
20+
TArray<FString> OutContentDirs;
21+
const TArray<TSharedRef<IPlugin>> Plugins = IPluginManager::Get().GetDiscoveredPlugins();
22+
for (const TSharedRef<IPlugin>& Plugin : Plugins)
23+
{
24+
if (Plugin->GetLoadedFrom() == EPluginLoadedFrom::Engine)
25+
{
26+
continue;
27+
}
28+
29+
FString ContentDir = Plugin->GetContentDir();
30+
if (FPaths::DirectoryExists(ContentDir))
31+
{
32+
OutContentDirs.Add(ContentDir);
33+
}
34+
}
35+
return OutContentDirs;
36+
}
37+
38+
1439
TArray<FString> UCarlaStatics::GetAllMapNames()
1540
{
1641
TArray<FString> TmpStrList, MapNameList;
17-
IFileManager::Get().FindFilesRecursive(
18-
MapNameList, *FPaths::ProjectContentDir(), TEXT("*.umap"), true, false, false);
19-
MapNameList.RemoveAll( [](const FString &Name) { return Name.Contains("TestMaps");});
20-
MapNameList.RemoveAll( [](const FString &Name) { return Name.Contains("OpenDriveMap");});
42+
TArray<FString> PathList;
43+
44+
PathList.Add(FPaths::ProjectContentDir());
45+
PathList.Append(GetAllPluginContentPaths());
46+
47+
for(const FString &Path : PathList) {
48+
if (FPaths::DirectoryExists(Path)) {
49+
UE_LOG(LogCarla, Log, TEXT("Path: %s"), *Path);
50+
IFileManager::Get().FindFilesRecursive(MapNameList, *Path, TEXT("*.umap"), true, false, false);
51+
}
52+
}
53+
54+
// Filter out undesired maps
55+
MapNameList.RemoveAll([](const FString& Name) {
56+
return Name.Contains("TestMaps") || Name.Contains("OpenDriveMap") || Name.Contains("Sublevels");
57+
});
58+
2159
for (int i = 0; i < MapNameList.Num(); i++) {
22-
MapNameList[i].ParseIntoArray(TmpStrList, TEXT("Content/"), true);
23-
MapNameList[i] = TmpStrList[1];
24-
MapNameList[i] = MapNameList[i].Replace(TEXT(".umap"), TEXT(""));
25-
MapNameList[i] = "/Game/" + MapNameList[i];
60+
MapNameList[i] = FPaths::GetBaseFilename(*MapNameList[i]);
2661
}
2762
return MapNameList;
2863
}
64+
65+
FString UCarlaStatics::FindMapPath(const FString &MapName)
66+
{
67+
TArray<FString> ContentPaths;
68+
69+
ContentPaths.Add(FPaths::ProjectContentDir());
70+
ContentPaths.Append(GetAllPluginContentPaths());
71+
72+
// Look for matching map files
73+
for (const FString& Path : ContentPaths)
74+
{
75+
TArray<FString> FoundFiles;
76+
IFileManager::Get().FindFilesRecursive(FoundFiles, *Path, TEXT("*.umap"), true, false);
77+
78+
for (const FString& FilePath : FoundFiles)
79+
{
80+
FString FileName = FPaths::GetBaseFilename(FilePath); // just "MyMap", no path, no extension
81+
if (FileName.Equals(MapName, ESearchCase::IgnoreCase))
82+
{
83+
return FilePath; // Return the full path of the first matching map. Only one map is expected.
84+
}
85+
}
86+
}
87+
88+
return FString();
89+
}

Unreal/CarlaUnreal/Plugins/Carla/Source/Carla/Game/CarlaStatics.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,15 @@ class CARLA_API UCarlaStatics : public UBlueprintFunctionLibrary
4141
UFUNCTION(BlueprintPure, Category="CARLA", meta=(WorldContext="WorldContextObject"))
4242
static UCarlaSettings *GetCarlaSettings(const UObject *WorldContextObject);
4343

44+
UFUNCTION(BlueprintPure, Category="CARLA")
45+
static TArray<FString> GetAllPluginContentPaths();
46+
4447
UFUNCTION(BlueprintPure, Category="CARLA")
4548
static TArray<FString> GetAllMapNames();
4649

50+
UFUNCTION(BlueprintPure, Category="CARLA")
51+
static FString FindMapPath(const FString &MapName);
52+
4753
UFUNCTION(BlueprintPure, Category="CARLA", meta=(WorldContext="WorldContextObject"))
4854
static ACarlaRecorder* GetRecorder(const UObject *WorldContextObject);
4955

0 commit comments

Comments
 (0)