Refactor map related methods

This commit is contained in:
nsubiron 2019-02-19 18:39:56 +01:00
parent 7f5a39bbb5
commit 6a943333a1
4 changed files with 91 additions and 60 deletions

View File

@ -15,6 +15,7 @@
#include "EngineUtils.h" #include "EngineUtils.h"
#include "Engine/StaticMeshActor.h" #include "Engine/StaticMeshActor.h"
#include "GameFramework/SpectatorPawn.h" #include "GameFramework/SpectatorPawn.h"
#include "Kismet/GameplayStatics.h"
static FString UCarlaEpisode_GetTrafficSignId(ETrafficSignState State) static FString UCarlaEpisode_GetTrafficSignId(ETrafficSignState State)
{ {
@ -40,10 +41,64 @@ static FString UCarlaEpisode_GetTrafficSignId(ETrafficSignState State)
UCarlaEpisode::UCarlaEpisode(const FObjectInitializer &ObjectInitializer) UCarlaEpisode::UCarlaEpisode(const FObjectInitializer &ObjectInitializer)
: Super(ObjectInitializer), : Super(ObjectInitializer),
Id(URandomEngine::GenerateRandomId()) { Id(URandomEngine::GenerateRandomId())
{
ActorDispatcher = CreateDefaultSubobject<UActorDispatcher>(TEXT("ActorDispatcher")); ActorDispatcher = CreateDefaultSubobject<UActorDispatcher>(TEXT("ActorDispatcher"));
} }
bool UCarlaEpisode::LoadNewEpisode(const FString &MapString)
{
FString FinalPath = MapString.IsEmpty() ? GetMapName() : MapString;
bool bIsFileFound = false;
if (MapString.StartsWith("/Game"))
{
// Full path
if (!MapString.EndsWith(".umap"))
{
FinalPath += ".umap";
}
// Some conversions...
FinalPath = FinalPath.Replace(TEXT("/Game/"), *FPaths::ProjectContentDir());
if (FPaths::FileExists(IFileManager::Get().ConvertToAbsolutePathForExternalAppForRead(*FinalPath)))
{
bIsFileFound = true;
FinalPath = MapString;
}
}
else
{
if (MapString.Contains("/"))
{
bIsFileFound = false;
}
else
{
// Find the full path under Carla
TArray<FString> TempStrArray, PathList;
if (!MapString.EndsWith(".umap"))
{
FinalPath += ".umap";
}
IFileManager::Get().FindFilesRecursive(PathList, *FPaths::ProjectContentDir(), *FinalPath, true, false, false);
if (PathList.Num() > 0)
{
FinalPath = PathList[0];
FinalPath.ParseIntoArray(TempStrArray, TEXT("Content/"), true);
FinalPath = TempStrArray[1];
FinalPath.ParseIntoArray(TempStrArray, TEXT("."), true);
FinalPath = "/Game/" + TempStrArray[0];
bIsFileFound = true;
}
}
}
if (bIsFileFound)
{
UE_LOG(LogCarla, Warning, TEXT("Loading a new episode: %s"), *FinalPath);
UGameplayStatics::OpenLevel(GetWorld(), *FinalPath, true);
}
return bIsFileFound;
}
void UCarlaEpisode::ApplySettings(const FEpisodeSettings &Settings) void UCarlaEpisode::ApplySettings(const FEpisodeSettings &Settings)
{ {
FCarlaStaticDelegates::OnEpisodeSettingsChange.Broadcast(Settings); FCarlaStaticDelegates::OnEpisodeSettingsChange.Broadcast(Settings);

View File

@ -14,7 +14,6 @@
#include "Carla/Weather/Weather.h" #include "Carla/Weather/Weather.h"
#include "GameFramework/Pawn.h" #include "GameFramework/Pawn.h"
#include "Kismet/GameplayStatics.h"
#include <compiler/disable-ue4-macros.h> #include <compiler/disable-ue4-macros.h>
#include <carla/geom/BoundingBox.h> #include <carla/geom/BoundingBox.h>
@ -41,6 +40,18 @@ public:
UCarlaEpisode(const FObjectInitializer &ObjectInitializer); UCarlaEpisode(const FObjectInitializer &ObjectInitializer);
// ===========================================================================
// -- Load a new episode -----------------------------------------------------
// ===========================================================================
public:
/// Load a new map and start a new episode.
///
/// If @a MapString is empty, the current map is reloaded.
UFUNCTION(BlueprintCallable)
bool LoadNewEpisode(const FString &MapString);
// =========================================================================== // ===========================================================================
// -- Episode settings ------------------------------------------------------- // -- Episode settings -------------------------------------------------------
// =========================================================================== // ===========================================================================
@ -221,50 +232,6 @@ public:
return ActorDispatcher->DestroyActor(Actor); return ActorDispatcher->DestroyActor(Actor);
} }
// ===========================================================================
// -- World handling methods -------------------------------------------------
// ===========================================================================
UFUNCTION(BlueprintCallable)
bool LoadMap(const FString& MapString, const UObject* WorldContext) {
bool bIsFileFound = false;
FString FinalPath = MapString;
if (MapString.StartsWith("/Game")) {
//Full path
if(!MapString.EndsWith(".umap")) {
FinalPath += ".umap";
}
//Some conversions...
FinalPath = FinalPath.Replace(TEXT("/Game/"), *FPaths::ProjectContentDir());
if(FPaths::FileExists(IFileManager::Get().ConvertToAbsolutePathForExternalAppForRead(*FinalPath))) {
bIsFileFound = true;
FinalPath = MapString;
}
} else {
if(MapString.Contains("/")) bIsFileFound = false;
else {
//Find the full path under Carla
TArray<FString> TempStrArray, PathList;
if(!MapString.EndsWith(".umap")) {
FinalPath += ".umap";
}
IFileManager::Get().FindFilesRecursive(PathList, *FPaths::ProjectContentDir(), *FinalPath, true, false, false);
if(PathList.Num()>0) {
FinalPath = PathList[0];
FinalPath.ParseIntoArray(TempStrArray, TEXT("Content/"), true);
FinalPath = TempStrArray[1];
FinalPath.ParseIntoArray(TempStrArray, TEXT("."), true);
FinalPath = "/Game/" + TempStrArray[0];
bIsFileFound = true;
}
}
}
if(bIsFileFound) UGameplayStatics::OpenLevel(WorldContext, *FinalPath, true);
return bIsFileFound;
}
// =========================================================================== // ===========================================================================
// -- Other methods ---------------------------------------------------------- // -- Other methods ----------------------------------------------------------
// =========================================================================== // ===========================================================================

View File

@ -0,0 +1,21 @@
// Copyright (c) 2017 Computer Vision Center (CVC) at the Universitat Autonoma
// de Barcelona (UAB).
//
// This work is licensed under the terms of the MIT license.
// For a copy, see <https://opensource.org/licenses/MIT>.
#include "Carla.h"
#include "Carla/Game/CarlaStatics.h"
TArray<FString> UCarlaStatics::GetAllMapNames()
{
TArray<FString> TmpStrList, MapNameList;
IFileManager::Get().FindFilesRecursive(MapNameList, *FPaths::ProjectContentDir(), TEXT("*.umap"), true, false, false);
for (int i = 0; i < MapNameList.Num(); i++) {
MapNameList[i].ParseIntoArray(TmpStrList, TEXT("Content/"), true);
MapNameList[i] = TmpStrList[1];
MapNameList[i] = MapNameList[i].Replace(TEXT(".umap"), TEXT(""));
MapNameList[i] = "/Game/" + MapNameList[i];
}
return MapNameList;
}

View File

@ -39,19 +39,8 @@ public:
UFUNCTION(BlueprintPure, Category="CARLA", meta=(WorldContext="WorldContextObject")) UFUNCTION(BlueprintPure, Category="CARLA", meta=(WorldContext="WorldContextObject"))
static UCarlaSettings *GetCarlaSettings(const UObject *WorldContextObject); static UCarlaSettings *GetCarlaSettings(const UObject *WorldContextObject);
UFUNCTION(BlueprintPure, Category="CARLA", meta=(WorldContext="WorldContextObject")) UFUNCTION(BlueprintPure, Category="CARLA")
static FORCEINLINE TArray<FString> GetAllMapNames() static TArray<FString> GetAllMapNames();
{
TArray<FString> TmpStrList, MapNameList;
IFileManager::Get().FindFilesRecursive(MapNameList, *FPaths::ProjectContentDir(), TEXT("*.umap"), true, false, false);
for (int i = 0; i < MapNameList.Num(); i++) {
MapNameList[i].ParseIntoArray(TmpStrList, TEXT("Content/"), true);
MapNameList[i] = TmpStrList[1];
MapNameList[i] = MapNameList[i].Replace(TEXT(".umap"), TEXT(""));
MapNameList[i] = "/Game/" + MapNameList[i];
}
return MapNameList;
}
}; };
// ============================================================================= // =============================================================================
@ -79,4 +68,3 @@ inline UCarlaSettings *UCarlaStatics::GetCarlaSettings(const UObject *WorldConte
auto GameInstance = GetGameInstance(WorldContext); auto GameInstance = GetGameInstance(WorldContext);
return GameInstance != nullptr ? GameInstance->GetCARLASettings() : nullptr; return GameInstance != nullptr ? GameInstance->GetCARLASettings() : nullptr;
} }