CARLA
Loading...
Searching...
No Matches
CarlaSettings.cpp
Go to the documentation of this file.
1// Copyright (c) 2026 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
8#include "Carla.h"
10
11#include <util/ue-header-guard-begin.h>
12#include "Misc/ConfigCacheIni.h"
13#include "Misc/CommandLine.h"
14#include "Engine/DirectionalLight.h"
15#include "Engine/Engine.h"
16#include "Engine/PointLight.h"
17#include "Engine/PostProcessVolume.h"
18#include "Engine/StaticMesh.h"
19#include "Kismet/GameplayStatics.h"
20#include "Materials/MaterialInstance.h"
21#include "UObject/Package.h"
22#include "Math/UnrealMathUtility.h"
23#include <util/ue-header-guard-end.h>
24
25
26// INI file sections.
27#define S_CARLA_SERVER TEXT("CARLA/Server")
28#define S_CARLA_QUALITYSETTINGS TEXT("CARLA/QualitySettings")
29
30// =============================================================================
31// -- Static variables & constants ---------------------------------------------
32// =============================================================================
33
34const FName UCarlaSettings::CARLA_ROAD_TAG = FName("CARLA_ROAD");
35const FName UCarlaSettings::CARLA_SKY_TAG = FName("CARLA_SKY");
36
37// =============================================================================
38// -- Static methods -----------------------------------------------------------
39// =============================================================================
40
42 const FString &SQualitySettingsLevel,
43 const EQualityLevel Default = EQualityLevel::INVALID)
44{
45 if (SQualitySettingsLevel.Equals("Low"))
46 {
47 return EQualityLevel::Low;
48 }
49 if (SQualitySettingsLevel.Equals("Medium"))
50 {
52 }
53 if (SQualitySettingsLevel.Equals("High"))
54 {
56 }
57 if (SQualitySettingsLevel.Equals("Epic"))
58 {
60 }
61 return Default;
62}
63
64FString QualityLevelToString(EQualityLevel QualitySettingsLevel)
65{
66 static_assert(TIsEnumClass<EQualityLevel>::Value);
67 return StaticEnum<EQualityLevel>()->GetNameStringByValue((int64)QualitySettingsLevel);
68}
69
71 const FIniFile &ConfigFile,
72 UCarlaSettings &Settings,
73 const bool bLoadCarlaServerSection)
74{
75 // CarlaServer.
76 if (bLoadCarlaServerSection)
77 {
78 ConfigFile.GetInt(S_CARLA_SERVER, TEXT("WorldPort"), Settings.RPCPort);
79 ConfigFile.GetInt(S_CARLA_SERVER, TEXT("RPCPort"), Settings.RPCPort);
80 Settings.StreamingPort = Settings.RPCPort + 1u;
81 Settings.SecondaryPort = Settings.RPCPort + 2u;
82 ConfigFile.GetInt(S_CARLA_SERVER, TEXT("StreamingPort"), Settings.StreamingPort);
83 ConfigFile.GetInt(S_CARLA_SERVER, TEXT("SecondaryPort"), Settings.SecondaryPort);
84 FString Tmp;
85 ConfigFile.GetString(S_CARLA_SERVER, TEXT("PrimaryIP"), Tmp);
86 Settings.PrimaryIP = TCHAR_TO_UTF8(*Tmp);
87 ConfigFile.GetInt(S_CARLA_SERVER, TEXT("PrimaryPort"), Settings.PrimaryPort);
88 }
89 ConfigFile.GetBool(S_CARLA_SERVER, TEXT("SynchronousMode"), Settings.bSynchronousMode);
90 ConfigFile.GetBool(S_CARLA_SERVER, TEXT("DisableRendering"), Settings.bDisableRendering);
91 // QualitySettings.
92 FString sQualityLevel;
93 ConfigFile.GetString(S_CARLA_QUALITYSETTINGS, TEXT("QualityLevel"), sQualityLevel);
94 Settings.SetQualityLevel(QualityLevelFromString(sQualityLevel));
95}
96
97static bool GetSettingsFilePathFromCommandLine(FString &Value)
98{
99 if (FParse::Value(FCommandLine::Get(), TEXT("-carla-settings="), Value))
100 {
101 if (FPaths::IsRelative(Value))
102 {
103 Value = FPaths::ConvertRelativePathToFull(FPaths::LaunchDir(), Value);
104 }
105 return true;
106 }
107 return false;
108}
109
110// =============================================================================
111// -- UCarlaSettings -----------------------------------------------------------
112// =============================================================================
113
115{
116 CurrentFileName = TEXT("");
117 // Load settings from project Config folder if present.
118 LoadSettingsFromFile(FPaths::Combine(FPaths::ProjectConfigDir(), TEXT("CarlaSettings.ini")), false);
119 // Load settings given by command-line arg if provided.
120 {
121 FString FilePath;
123 {
124 LoadSettingsFromFile(FilePath, true);
125 }
126 }
127 // Override settings from command-line.
128 {
129 uint32 Value;
130 if (FParse::Value(FCommandLine::Get(), TEXT("-world-port="), Value) ||
131 FParse::Value(FCommandLine::Get(), TEXT("-carla-port="), Value) ||
132 FParse::Value(FCommandLine::Get(), TEXT("-carla-rpc-port="), Value) ||
133 FParse::Value(FCommandLine::Get(), TEXT("-carla-world-port="), Value))
134 {
135 RPCPort = Value;
136 StreamingPort = Value + 1u;
137 SecondaryPort = Value + 2u;
138 }
139 if (FParse::Value(FCommandLine::Get(), TEXT("-carla-streaming-port="), Value))
140 {
141 StreamingPort = Value;
142 }
143 if (FParse::Value(FCommandLine::Get(), TEXT("-carla-secondary-port="), Value))
144 {
145 SecondaryPort = Value;
146 }
147 FString Tmp;
148 if (FParse::Value(FCommandLine::Get(), TEXT("-carla-primary-host="), Tmp))
149 {
150 PrimaryIP = TCHAR_TO_UTF8(*Tmp);
151 }
152 if (FParse::Value(FCommandLine::Get(), TEXT("-carla-primary-port="), Value))
153 {
154 PrimaryPort = Value;
155 }
156 FString StringQualityLevel;
157 if (FParse::Value(FCommandLine::Get(), TEXT("-quality-level="), StringQualityLevel))
158 {
160 }
161 if (FParse::Param(FCommandLine::Get(), TEXT("-no-rendering")))
162 {
163 bDisableRendering = true;
164 }
165 // FParse::Param only matches single-dash switches; also accept the
166 // historically documented "--ros2" spelling so it does not silently no-op.
167 if (FParse::Param(FCommandLine::Get(), TEXT("-ros2")) ||
168 FCString::Strifind(FCommandLine::Get(), TEXT("--ros2")) != nullptr)
169 {
170 ROS2 = true;
171 }
172 FString RmwValue;
173 if (FParse::Value(FCommandLine::Get(), TEXT("-rmw="), RmwValue))
174 {
175 RmwName = RmwValue.ToLower();
176 }
177 int32 DomainIdValue;
178 if (FParse::Value(FCommandLine::Get(), TEXT("-ros-domain-id="), DomainIdValue))
179 {
180 ROS2DomainId = DomainIdValue;
181 }
182 }
183}
184
185void UCarlaSettings::LoadSettingsFromString(const FString &INIFileContents)
186{
187 UE_LOG(LogCarla, Log, TEXT("Loading CARLA settings from string"));
188 FIniFile ConfigFile;
189 CurrentFileName = TEXT("<string-provided-by-client>");
190 ConfigFile.ProcessInputFileContents(INIFileContents, CurrentFileName);
191 constexpr bool bLoadCarlaServerSection = false;
192 LoadSettingsFromConfig(ConfigFile, *this, bLoadCarlaServerSection);
193}
194
196{
197 auto EnabledDisabled = [](bool bValue) { return (bValue ? TEXT("Enabled") : TEXT("Disabled")); };
198 UE_LOG(LogCarla, Log,
199 TEXT("== CARLA Settings =============================================================="));
200 UE_LOG(LogCarla, Log, TEXT("Last settings file loaded: %s"), *CurrentFileName);
201 UE_LOG(LogCarla, Log, TEXT("[%s]"), S_CARLA_SERVER);
202 UE_LOG(LogCarla, Log, TEXT("RPC Port = %d"), RPCPort);
203 UE_LOG(LogCarla, Log, TEXT("Streaming Port = %d"), StreamingPort);
204 UE_LOG(LogCarla, Log, TEXT("Secondary Port = %d"), SecondaryPort);
205 UE_LOG(LogCarla, Log, TEXT("Synchronous Mode = %s"), EnabledDisabled(bSynchronousMode));
206 UE_LOG(LogCarla, Log, TEXT("Rendering = %s"), EnabledDisabled(!bDisableRendering));
207 UE_LOG(LogCarla, Log, TEXT("[%s]"), S_CARLA_QUALITYSETTINGS);
208 UE_LOG(LogCarla, Log, TEXT("Quality Level = %s"), *QualityLevelToString(QualityLevel));
209 UE_LOG(LogCarla, Log,
210 TEXT("================================================================================"));
211}
212
213#undef S_CARLA_QUALITYSETTINGS
214#undef S_CARLA_SERVER
215
216void UCarlaSettings::LoadSettingsFromFile(const FString &FilePath, const bool bLogOnFailure)
217{
218 if (FPaths::FileExists(FilePath))
219 {
220 UE_LOG(LogCarla, Log, TEXT("Loading CARLA settings from \"%s\""), *FilePath);
221 const FIniFile ConfigFile(FilePath);
222 constexpr bool bLoadCarlaServerSection = true;
223 LoadSettingsFromConfig(ConfigFile, *this, bLoadCarlaServerSection);
224 CurrentFileName = FilePath;
225 }
226 else if (bLogOnFailure)
227 {
228 UE_LOG(LogCarla, Error, TEXT("Unable to find settings file \"%s\""), *FilePath);
229 }
230}
static bool GetSettingsFilePathFromCommandLine(FString &Value)
#define S_CARLA_QUALITYSETTINGS
#define S_CARLA_SERVER
static void LoadSettingsFromConfig(const FIniFile &ConfigFile, UCarlaSettings &Settings, const bool bLoadCarlaServerSection)
static EQualityLevel QualityLevelFromString(const FString &SQualitySettingsLevel, const EQualityLevel Default=EQualityLevel::INVALID)
FString QualityLevelToString(EQualityLevel QualitySettingsLevel)
EQualityLevel
Wrapper around Unreal's INI file.
Definition IniFile.h:18
void GetBool(const TCHAR *Section, const TCHAR *Key, bool &Target) const
Definition IniFile.h:109
void GetString(const TCHAR *Section, const TCHAR *Key, FString &Target) const
Definition IniFile.h:101
void ProcessInputFileContents(const FString &INIFileContents, const FString &Hint)
Definition IniFile.h:57
void GetInt(const TCHAR *Section, const TCHAR *Key, T &Target) const
Definition IniFile.h:93
Global settings for CARLA.
uint32 StreamingPort
setting for the streaming port.
void LoadSettings()
Load the settings based on the command-line arguments and the INI file if provided.
std::string PrimaryIP
setting for the IP and Port of the primary server to connect.
bool bDisableRendering
Enable or disable the viewport rendering of the world.
uint32 RPCPort
World port to listen for client connections.
static const FName CARLA_SKY_TAG
CARLA_SKY name to tag the sky sphere (BPS) actors in the scenes.
FString CurrentFileName
File name of the settings file used to load this settings.
void SetQualityLevel(EQualityLevel InQualityLevel)
Sets the new quality settings level and make changes in the game related to it.
void LoadSettingsFromFile(const FString &FilePath, bool bLogOnFailure)
bool bSynchronousMode
In synchronous mode, CARLA waits every tick until the control from the client is received.
void LogSettings() const
Log settings values.
static const FName CARLA_ROAD_TAG
CARLA_ROAD name to tag road mesh actors.
EQualityLevel QualityLevel
Quality Settings level.
uint32 SecondaryPort
setting for the secondary servers port.
void LoadSettingsFromString(const FString &INIFileContents)
Load the settings from the given string (formatted as INI).