CARLA
CarlaSettings.cpp
Go to the documentation of this file.
1 // Copyright (c) 2017 Computer Vision Center (CVC) at the Universitat Autonoma
2 // de Barcelona (UAB).
3 //
4 // This work is licensed under the terms of the MIT license.
5 // For a copy, see <https://opensource.org/licenses/MIT>.
6 
7 #include "Carla.h"
9 
10 #include "Carla/Util/IniFile.h"
11 
12 #include "CommandLine.h"
13 #include "Engine/DirectionalLight.h"
14 #include "Engine/Engine.h"
15 #include "Engine/PointLight.h"
16 #include "Engine/PostProcessVolume.h"
17 #include "Engine/StaticMesh.h"
18 #include "Kismet/GameplayStatics.h"
19 #include "Materials/MaterialInstance.h"
20 #include "Package.h"
21 #include "UnrealMathUtility.h"
22 
23 // INI file sections.
24 #define S_CARLA_SERVER TEXT("CARLA/Server")
25 #define S_CARLA_QUALITYSETTINGS TEXT("CARLA/QualitySettings")
26 
27 // =============================================================================
28 // -- Static variables & constants ---------------------------------------------
29 // =============================================================================
30 
31 const FName UCarlaSettings::CARLA_ROAD_TAG = FName("CARLA_ROAD");
32 const FName UCarlaSettings::CARLA_SKY_TAG = FName("CARLA_SKY");
33 
34 // =============================================================================
35 // -- Static methods -----------------------------------------------------------
36 // =============================================================================
37 
39  const FString &SQualitySettingsLevel,
40  const EQualityLevel Default = EQualityLevel::INVALID)
41 {
42  if (SQualitySettingsLevel.Equals("Low"))
43  {
44  return EQualityLevel::Low;
45  }
46  if (SQualitySettingsLevel.Equals("Epic"))
47  {
48  return EQualityLevel::Epic;
49  }
50  return Default;
51 }
52 
53 FString QualityLevelToString(EQualityLevel QualitySettingsLevel)
54 {
55  const UEnum *ptr = FindObject<UEnum>(ANY_PACKAGE, TEXT("EQualityLevel"), true);
56  if (!ptr)
57  {
58  return FString("Invalid");
59  }
60  return ptr->GetNameStringByIndex(static_cast<int32>(QualitySettingsLevel));
61 }
62 
64  const FIniFile &ConfigFile,
65  UCarlaSettings &Settings,
66  const bool bLoadCarlaServerSection)
67 {
68  // CarlaServer.
69  if (bLoadCarlaServerSection)
70  {
71  ConfigFile.GetInt(S_CARLA_SERVER, TEXT("WorldPort"), Settings.RPCPort);
72  ConfigFile.GetInt(S_CARLA_SERVER, TEXT("RPCPort"), Settings.RPCPort);
73  }
74  ConfigFile.GetBool(S_CARLA_SERVER, TEXT("SynchronousMode"), Settings.bSynchronousMode);
75  ConfigFile.GetBool(S_CARLA_SERVER, TEXT("DisableRendering"), Settings.bDisableRendering);
76  // QualitySettings.
77  FString sQualityLevel;
78  ConfigFile.GetString(S_CARLA_QUALITYSETTINGS, TEXT("QualityLevel"), sQualityLevel);
79  Settings.SetQualityLevel(QualityLevelFromString(sQualityLevel));
80 }
81 
82 static bool GetSettingsFilePathFromCommandLine(FString &Value)
83 {
84  if (FParse::Value(FCommandLine::Get(), TEXT("-carla-settings="), Value))
85  {
86  if (FPaths::IsRelative(Value))
87  {
88  Value = FPaths::ConvertRelativePathToFull(FPaths::LaunchDir(), Value);
89  }
90  return true;
91  }
92  return false;
93 }
94 
95 // =============================================================================
96 // -- UCarlaSettings -----------------------------------------------------------
97 // =============================================================================
98 
100 {
101  CurrentFileName = TEXT("");
102  // Load settings from project Config folder if present.
103  LoadSettingsFromFile(FPaths::Combine(FPaths::ProjectConfigDir(), TEXT("CarlaSettings.ini")), false);
104  // Load settings given by command-line arg if provided.
105  {
106  FString FilePath;
108  {
109  LoadSettingsFromFile(FilePath, true);
110  }
111  }
112  // Override settings from command-line.
113  {
114  uint32 Value;
115  if (FParse::Value(FCommandLine::Get(), TEXT("-world-port="), Value) ||
116  FParse::Value(FCommandLine::Get(), TEXT("-carla-port="), Value) ||
117  FParse::Value(FCommandLine::Get(), TEXT("-carla-rpc-port="), Value) ||
118  FParse::Value(FCommandLine::Get(), TEXT("-carla-world-port="), Value))
119  {
120  RPCPort = Value;
121  }
122  if (FParse::Value(FCommandLine::Get(), TEXT("-carla-streaming-port="), Value))
123  {
124  StreamingPort = Value;
125  }
126  FString StringQualityLevel;
127  if (FParse::Value(FCommandLine::Get(), TEXT("-quality-level="), StringQualityLevel))
128  {
130  }
131  if (FParse::Param(FCommandLine::Get(), TEXT("-no-rendering")))
132  {
133  bDisableRendering = true;
134  }
135  }
136 }
137 
138 void UCarlaSettings::LoadSettingsFromString(const FString &INIFileContents)
139 {
140  UE_LOG(LogCarla, Log, TEXT("Loading CARLA settings from string"));
141  FIniFile ConfigFile;
142  ConfigFile.ProcessInputFileContents(INIFileContents);
143  constexpr bool bLoadCarlaServerSection = false;
144  LoadSettingsFromConfig(ConfigFile, *this, bLoadCarlaServerSection);
145  CurrentFileName = TEXT("<string-provided-by-client>");
146 }
147 
149 {
150  auto EnabledDisabled = [](bool bValue) { return (bValue ? TEXT("Enabled") : TEXT("Disabled")); };
151  UE_LOG(LogCarla, Log,
152  TEXT("== CARLA Settings =============================================================="));
153  UE_LOG(LogCarla, Log, TEXT("Last settings file loaded: %s"), *CurrentFileName);
154  UE_LOG(LogCarla, Log, TEXT("[%s]"), S_CARLA_SERVER);
155  UE_LOG(LogCarla, Log, TEXT("RPC Port = %d"), RPCPort);
156  UE_LOG(LogCarla, Log, TEXT("Streaming Port = %d"), StreamingPort.Get(RPCPort + 1u));
157  UE_LOG(LogCarla, Log, TEXT("Synchronous Mode = %s"), EnabledDisabled(bSynchronousMode));
158  UE_LOG(LogCarla, Log, TEXT("Rendering = %s"), EnabledDisabled(!bDisableRendering));
159  UE_LOG(LogCarla, Log, TEXT("[%s]"), S_CARLA_QUALITYSETTINGS);
160  UE_LOG(LogCarla, Log, TEXT("Quality Level = %s"), *QualityLevelToString(QualityLevel));
161  UE_LOG(LogCarla, Log,
162  TEXT("================================================================================"));
163 }
164 
165 #undef S_CARLA_QUALITYSETTINGS
166 #undef S_CARLA_SERVER
167 
168 void UCarlaSettings::LoadSettingsFromFile(const FString &FilePath, const bool bLogOnFailure)
169 {
170  if (FPaths::FileExists(FilePath))
171  {
172  UE_LOG(LogCarla, Log, TEXT("Loading CARLA settings from \"%s\""), *FilePath);
173  const FIniFile ConfigFile(FilePath);
174  constexpr bool bLoadCarlaServerSection = true;
175  LoadSettingsFromConfig(ConfigFile, *this, bLoadCarlaServerSection);
176  CurrentFileName = FilePath;
177  }
178  else if (bLogOnFailure)
179  {
180  UE_LOG(LogCarla, Error, TEXT("Unable to find settings file \"%s\""), *FilePath);
181  }
182 }
EQualityLevel
bool bSynchronousMode
In synchronous mode, CARLA waits every tick until the control from the client is received.
Definition: CarlaSettings.h:91
EQualityLevel QualityLevel
Quality Settings level.
void GetString(const TCHAR *Section, const TCHAR *Key, FString &Target) const
Definition: IniFile.h:99
void GetInt(const TCHAR *Section, const TCHAR *Key, T &Target) const
Definition: IniFile.h:91
static bool GetSettingsFilePathFromCommandLine(FString &Value)
void LoadSettingsFromFile(const FString &FilePath, bool bLogOnFailure)
void LoadSettings()
Load the settings based on the command-line arguments and the INI file if provided.
void SetQualityLevel(EQualityLevel InQualityLevel)
Sets the new quality settings level and make changes in the game related to it.
Definition: CarlaSettings.h:34
#define S_CARLA_QUALITYSETTINGS
static T Get(carla::rpc::Response< T > &response)
void LoadSettingsFromString(const FString &INIFileContents)
Load the settings from the given string (formatted as INI).
static void LoadSettingsFromConfig(const FIniFile &ConfigFile, UCarlaSettings &Settings, const bool bLoadCarlaServerSection)
TOptional< uint32 > StreamingPort
Optional setting for the secondary port.
Definition: CarlaSettings.h:86
static EQualityLevel QualityLevelFromString(const FString &SQualitySettingsLevel, const EQualityLevel Default=EQualityLevel::INVALID)
void ProcessInputFileContents(const FString &INIFileContents)
Definition: IniFile.h:55
Global settings for CARLA.
Definition: CarlaSettings.h:21
void GetBool(const TCHAR *Section, const TCHAR *Key, bool &Target) const
Definition: IniFile.h:107
FString QualityLevelToString(EQualityLevel QualitySettingsLevel)
uint32 RPCPort
World port to listen for client connections.
Definition: CarlaSettings.h:83
static const FName CARLA_ROAD_TAG
CARLA_ROAD name to tag road mesh actors.
Definition: CarlaSettings.h:60
void LogSettings() const
Log settings values.
FString CurrentFileName
File name of the settings file used to load this settings.
Definition: CarlaSettings.h:72
bool bDisableRendering
Enable or disable the viewport rendering of the world.
Definition: CarlaSettings.h:96
static const FName CARLA_SKY_TAG
CARLA_SKY name to tag the sky sphere (BPS) actors in the scenes.
Definition: CarlaSettings.h:63
Wrapper around Unreal&#39;s INI file.
Definition: IniFile.h:15
#define S_CARLA_SERVER