From 6a943333a16a8bf55c793cc9156e4ef9a57db956 Mon Sep 17 00:00:00 2001 From: nsubiron Date: Tue, 19 Feb 2019 18:39:56 +0100 Subject: [PATCH] Refactor map related methods --- .../Carla/Source/Carla/Game/CarlaEpisode.cpp | 57 ++++++++++++++++++- .../Carla/Source/Carla/Game/CarlaEpisode.h | 57 ++++--------------- .../Carla/Source/Carla/Game/CarlaStatics.cpp | 21 +++++++ .../Carla/Source/Carla/Game/CarlaStatics.h | 16 +----- 4 files changed, 91 insertions(+), 60 deletions(-) create mode 100644 Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Game/CarlaStatics.cpp diff --git a/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Game/CarlaEpisode.cpp b/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Game/CarlaEpisode.cpp index 6e7ae03af..67558dc99 100644 --- a/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Game/CarlaEpisode.cpp +++ b/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Game/CarlaEpisode.cpp @@ -15,6 +15,7 @@ #include "EngineUtils.h" #include "Engine/StaticMeshActor.h" #include "GameFramework/SpectatorPawn.h" +#include "Kismet/GameplayStatics.h" static FString UCarlaEpisode_GetTrafficSignId(ETrafficSignState State) { @@ -40,10 +41,64 @@ static FString UCarlaEpisode_GetTrafficSignId(ETrafficSignState State) UCarlaEpisode::UCarlaEpisode(const FObjectInitializer &ObjectInitializer) : Super(ObjectInitializer), - Id(URandomEngine::GenerateRandomId()) { + Id(URandomEngine::GenerateRandomId()) +{ ActorDispatcher = CreateDefaultSubobject(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 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) { FCarlaStaticDelegates::OnEpisodeSettingsChange.Broadcast(Settings); diff --git a/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Game/CarlaEpisode.h b/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Game/CarlaEpisode.h index 2ba5d16b5..2e208a60e 100644 --- a/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Game/CarlaEpisode.h +++ b/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Game/CarlaEpisode.h @@ -14,7 +14,6 @@ #include "Carla/Weather/Weather.h" #include "GameFramework/Pawn.h" -#include "Kismet/GameplayStatics.h" #include #include @@ -41,6 +40,18 @@ public: 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 ------------------------------------------------------- // =========================================================================== @@ -221,50 +232,6 @@ public: 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 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 ---------------------------------------------------------- // =========================================================================== diff --git a/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Game/CarlaStatics.cpp b/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Game/CarlaStatics.cpp new file mode 100644 index 000000000..04fa30ae5 --- /dev/null +++ b/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Game/CarlaStatics.cpp @@ -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 . + +#include "Carla.h" +#include "Carla/Game/CarlaStatics.h" + +TArray UCarlaStatics::GetAllMapNames() +{ + TArray 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; +} diff --git a/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Game/CarlaStatics.h b/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Game/CarlaStatics.h index 44c791610..cbd0a46ef 100644 --- a/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Game/CarlaStatics.h +++ b/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Game/CarlaStatics.h @@ -39,19 +39,8 @@ public: UFUNCTION(BlueprintPure, Category="CARLA", meta=(WorldContext="WorldContextObject")) static UCarlaSettings *GetCarlaSettings(const UObject *WorldContextObject); - UFUNCTION(BlueprintPure, Category="CARLA", meta=(WorldContext="WorldContextObject")) - static FORCEINLINE TArray GetAllMapNames() - { - TArray 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; - } + UFUNCTION(BlueprintPure, Category="CARLA") + static TArray GetAllMapNames(); }; // ============================================================================= @@ -79,4 +68,3 @@ inline UCarlaSettings *UCarlaStatics::GetCarlaSettings(const UObject *WorldConte auto GameInstance = GetGameInstance(WorldContext); return GameInstance != nullptr ? GameInstance->GetCARLASettings() : nullptr; } -