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  Settings.StreamingPort = Settings.RPCPort + 1u;
74  Settings.SecondaryPort = Settings.RPCPort + 2u;
75  ConfigFile.GetInt(S_CARLA_SERVER, TEXT("StreamingPort"), Settings.StreamingPort);
76  ConfigFile.GetInt(S_CARLA_SERVER, TEXT("SecondaryPort"), Settings.SecondaryPort);
77  FString Tmp;
78  ConfigFile.GetString(S_CARLA_SERVER, TEXT("PrimaryIP"), Tmp);
79  Settings.PrimaryIP = TCHAR_TO_UTF8(*Tmp);
80  ConfigFile.GetInt(S_CARLA_SERVER, TEXT("PrimaryPort"), Settings.PrimaryPort);
81  }
82  ConfigFile.GetBool(S_CARLA_SERVER, TEXT("SynchronousMode"), Settings.bSynchronousMode);
83  ConfigFile.GetBool(S_CARLA_SERVER, TEXT("DisableRendering"), Settings.bDisableRendering);
84  // QualitySettings.
85  FString sQualityLevel;
86  ConfigFile.GetString(S_CARLA_QUALITYSETTINGS, TEXT("QualityLevel"), sQualityLevel);
87  Settings.SetQualityLevel(QualityLevelFromString(sQualityLevel));
88 }
89 
90 static bool GetSettingsFilePathFromCommandLine(FString &Value)
91 {
92  if (FParse::Value(FCommandLine::Get(), TEXT("-carla-settings="), Value))
93  {
94  if (FPaths::IsRelative(Value))
95  {
96  Value = FPaths::ConvertRelativePathToFull(FPaths::LaunchDir(), Value);
97  }
98  return true;
99  }
100  return false;
101 }
102 
103 // =============================================================================
104 // -- UCarlaSettings -----------------------------------------------------------
105 // =============================================================================
106 
108 {
109  CurrentFileName = TEXT("");
110  // Load settings from project Config folder if present.
111  LoadSettingsFromFile(FPaths::Combine(FPaths::ProjectConfigDir(), TEXT("CarlaSettings.ini")), false);
112  // Load settings given by command-line arg if provided.
113  {
114  FString FilePath;
116  {
117  LoadSettingsFromFile(FilePath, true);
118  }
119  }
120  // Override settings from command-line.
121  {
122  uint32 Value;
123  if (FParse::Value(FCommandLine::Get(), TEXT("-world-port="), Value) ||
124  FParse::Value(FCommandLine::Get(), TEXT("-carla-port="), Value) ||
125  FParse::Value(FCommandLine::Get(), TEXT("-carla-rpc-port="), Value) ||
126  FParse::Value(FCommandLine::Get(), TEXT("-carla-world-port="), Value))
127  {
128  RPCPort = Value;
129  StreamingPort = Value + 1u;
130  SecondaryPort = Value + 2u;
131  }
132  if (FParse::Value(FCommandLine::Get(), TEXT("-carla-streaming-port="), Value))
133  {
134  StreamingPort = Value;
135  }
136  if (FParse::Value(FCommandLine::Get(), TEXT("-carla-secondary-port="), Value))
137  {
138  SecondaryPort = Value;
139  }
140  FString Tmp;
141  if (FParse::Value(FCommandLine::Get(), TEXT("-carla-primary-host="), Tmp))
142  {
143  PrimaryIP = TCHAR_TO_UTF8(*Tmp);
144  }
145  if (FParse::Value(FCommandLine::Get(), TEXT("-carla-primary-port="), Value))
146  {
147  PrimaryPort = Value;
148  }
149  FString StringQualityLevel;
150  if (FParse::Value(FCommandLine::Get(), TEXT("-quality-level="), StringQualityLevel))
151  {
153  }
154  if (FParse::Param(FCommandLine::Get(), TEXT("-no-rendering")))
155  {
156  bDisableRendering = true;
157  }
158  if (FParse::Param(FCommandLine::Get(), TEXT("-ros2")))
159  {
160  ROS2 = true;
161  }
162  }
163 }
164 
165 void UCarlaSettings::LoadSettingsFromString(const FString &INIFileContents)
166 {
167  UE_LOG(LogCarla, Log, TEXT("Loading CARLA settings from string"));
168  FIniFile ConfigFile;
169  ConfigFile.ProcessInputFileContents(INIFileContents);
170  constexpr bool bLoadCarlaServerSection = false;
171  LoadSettingsFromConfig(ConfigFile, *this, bLoadCarlaServerSection);
172  CurrentFileName = TEXT("<string-provided-by-client>");
173 }
174 
176 {
177  auto EnabledDisabled = [](bool bValue) { return (bValue ? TEXT("Enabled") : TEXT("Disabled")); };
178  UE_LOG(LogCarla, Log,
179  TEXT("== CARLA Settings =============================================================="));
180  UE_LOG(LogCarla, Log, TEXT("Last settings file loaded: %s"), *CurrentFileName);
181  UE_LOG(LogCarla, Log, TEXT("[%s]"), S_CARLA_SERVER);
182  UE_LOG(LogCarla, Log, TEXT("RPC Port = %d"), RPCPort);
183  UE_LOG(LogCarla, Log, TEXT("Streaming Port = %d"), StreamingPort);
184  UE_LOG(LogCarla, Log, TEXT("Secondary Port = %d"), SecondaryPort);
185  UE_LOG(LogCarla, Log, TEXT("Synchronous Mode = %s"), EnabledDisabled(bSynchronousMode));
186  UE_LOG(LogCarla, Log, TEXT("Rendering = %s"), EnabledDisabled(!bDisableRendering));
187  UE_LOG(LogCarla, Log, TEXT("[%s]"), S_CARLA_QUALITYSETTINGS);
188  UE_LOG(LogCarla, Log, TEXT("Quality Level = %s"), *QualityLevelToString(QualityLevel));
189  UE_LOG(LogCarla, Log,
190  TEXT("================================================================================"));
191 }
192 
193 #undef S_CARLA_QUALITYSETTINGS
194 #undef S_CARLA_SERVER
195 
196 void UCarlaSettings::LoadSettingsFromFile(const FString &FilePath, const bool bLogOnFailure)
197 {
198  if (FPaths::FileExists(FilePath))
199  {
200  UE_LOG(LogCarla, Log, TEXT("Loading CARLA settings from \"%s\""), *FilePath);
201  const FIniFile ConfigFile(FilePath);
202  constexpr bool bLoadCarlaServerSection = true;
203  LoadSettingsFromConfig(ConfigFile, *this, bLoadCarlaServerSection);
204  CurrentFileName = FilePath;
205  }
206  else if (bLogOnFailure)
207  {
208  UE_LOG(LogCarla, Error, TEXT("Unable to find settings file \"%s\""), *FilePath);
209  }
210 }
EQualityLevel
bool bSynchronousMode
In synchronous mode, CARLA waits every tick until the control from the client is received.
Definition: CarlaSettings.h:98
uint32 SecondaryPort
setting for the secondary servers port.
Definition: CarlaSettings.h:89
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)
uint32 StreamingPort
setting for the streaming port.
Definition: CarlaSettings.h:86
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)
static EQualityLevel QualityLevelFromString(const FString &SQualitySettingsLevel, const EQualityLevel Default=EQualityLevel::INVALID)
void ProcessInputFileContents(const FString &INIFileContents)
Definition: IniFile.h:55
std::string PrimaryIP
setting for the IP and Port of the primary server to connect.
Definition: CarlaSettings.h:92
Global settings for CARLA.
Definition: CarlaSettings.h:21
uint32 PrimaryPort
Definition: CarlaSettings.h:93
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.
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