|
8 | 8 | #include "Carla.h" |
9 | 9 |
|
10 | 10 | #include <util/ue-header-guard-begin.h> |
| 11 | +#include "Interfaces/IPluginManager.h" |
| 12 | +#include "Misc/Paths.h" |
| 13 | +#include "Modules/ModuleManager.h" |
11 | 14 | #include "HAL/FileManagerGeneric.h" |
12 | 15 | #include <util/ue-header-guard-end.h> |
13 | 16 |
|
| 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 | + |
14 | 39 | TArray<FString> UCarlaStatics::GetAllMapNames() |
15 | 40 | { |
16 | 41 | 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 | + |
21 | 59 | 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]); |
26 | 61 | } |
27 | 62 | return MapNameList; |
28 | 63 | } |
| 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 | +} |
0 commit comments