|
33 | 33 | using namespace ignition; |
34 | 34 | using namespace gazebo; |
35 | 35 |
|
36 | | -////////////////////////////////////////////////// |
37 | | -// Getting the first .sdf file in the path |
38 | | -std::string findFuelResourceSdf(const std::string &_path) |
39 | | -{ |
40 | | - if (!common::exists(_path)) |
41 | | - return ""; |
42 | | - |
43 | | - for (common::DirIter file(_path); file != common::DirIter(); ++file) |
44 | | - { |
45 | | - std::string current(*file); |
46 | | - if (!common::isFile(current)) |
47 | | - continue; |
48 | | - |
49 | | - auto fileName = common::basename(current); |
50 | | - auto fileExtensionIndex = fileName.rfind("."); |
51 | | - auto fileExtension = fileName.substr(fileExtensionIndex + 1); |
52 | | - |
53 | | - if (fileExtension == "sdf") |
54 | | - { |
55 | | - return current; |
56 | | - } |
57 | | - } |
58 | | - return ""; |
59 | | -} |
60 | | - |
61 | 36 | /// \brief This struct provides access to the default world. |
62 | 37 | struct DefaultWorld |
63 | 38 | { |
@@ -98,83 +73,64 @@ Server::Server(const ServerConfig &_config) |
98 | 73 |
|
99 | 74 | sdf::Errors errors; |
100 | 75 |
|
101 | | - // Load a world if specified. Check SDF string first, then SDF file |
102 | | - if (!_config.SdfString().empty()) |
| 76 | + switch (_config.Source()) |
103 | 77 | { |
104 | | - std::string msg = "Loading SDF string. "; |
105 | | - if (_config.SdfFile().empty()) |
| 78 | + // Load a world if specified. Check SDF string first, then SDF file |
| 79 | + case ServerConfig::SourceType::kSdfRoot: |
106 | 80 | { |
107 | | - msg += "File path not available.\n"; |
| 81 | + this->dataPtr->sdfRoot = _config.SdfRoot()->Clone(); |
| 82 | + ignmsg << "Loading SDF world from SDF DOM.\n"; |
| 83 | + break; |
108 | 84 | } |
109 | | - else |
110 | | - { |
111 | | - msg += "File path [" + _config.SdfFile() + "].\n"; |
112 | | - } |
113 | | - ignmsg << msg; |
114 | | - errors = this->dataPtr->sdfRoot.LoadSdfString(_config.SdfString()); |
115 | | - } |
116 | | - else if (!_config.SdfFile().empty()) |
117 | | - { |
118 | | - std::string filePath; |
119 | 85 |
|
120 | | - // Check Fuel if it's a URL |
121 | | - auto sdfUri = common::URI(_config.SdfFile()); |
122 | | - if (sdfUri.Scheme() == "http" || sdfUri.Scheme() == "https") |
| 86 | + case ServerConfig::SourceType::kSdfString: |
123 | 87 | { |
124 | | - std::string fuelCachePath; |
125 | | - if (this->dataPtr->fuelClient->CachedWorld(common::URI(_config.SdfFile()), |
126 | | - fuelCachePath)) |
| 88 | + std::string msg = "Loading SDF string. "; |
| 89 | + if (_config.SdfFile().empty()) |
127 | 90 | { |
128 | | - filePath = findFuelResourceSdf(fuelCachePath); |
129 | | - } |
130 | | - else if (auto result = this->dataPtr->fuelClient->DownloadWorld( |
131 | | - common::URI(_config.SdfFile()), fuelCachePath)) |
132 | | - { |
133 | | - filePath = findFuelResourceSdf(fuelCachePath); |
| 91 | + msg += "File path not available.\n"; |
134 | 92 | } |
135 | 93 | else |
136 | 94 | { |
137 | | - ignwarn << "Fuel couldn't download URL [" << _config.SdfFile() |
138 | | - << "], error: [" << result.ReadableResult() << "]" |
139 | | - << std::endl; |
| 95 | + msg += "File path [" + _config.SdfFile() + "].\n"; |
140 | 96 | } |
| 97 | + ignmsg << msg; |
| 98 | + errors = this->dataPtr->sdfRoot.LoadSdfString(_config.SdfString()); |
| 99 | + break; |
141 | 100 | } |
142 | 101 |
|
143 | | - if (filePath.empty()) |
| 102 | + case ServerConfig::SourceType::kSdfFile: |
144 | 103 | { |
145 | | - common::SystemPaths systemPaths; |
| 104 | + std::string filePath = resolveSdfWorldFile(_config.SdfFile(), |
| 105 | + _config.ResourceCache()); |
146 | 106 |
|
147 | | - // Worlds from environment variable |
148 | | - systemPaths.SetFilePathEnv(kResourcePathEnv); |
| 107 | + if (filePath.empty()) |
| 108 | + { |
| 109 | + ignerr << "Failed to find world [" << _config.SdfFile() << "]" |
| 110 | + << std::endl; |
| 111 | + return; |
| 112 | + } |
149 | 113 |
|
150 | | - // Worlds installed with ign-gazebo |
151 | | - systemPaths.AddFilePaths(IGN_GAZEBO_WORLD_INSTALL_DIR); |
| 114 | + ignmsg << "Loading SDF world file[" << filePath << "].\n"; |
152 | 115 |
|
153 | | - filePath = systemPaths.FindFile(_config.SdfFile()); |
| 116 | + // \todo(nkoenig) Async resource download. |
| 117 | + // This call can block for a long period of time while |
| 118 | + // resources are downloaded. Blocking here causes the GUI to block with |
| 119 | + // a black screen (search for "Async resource download" in |
| 120 | + // 'src/gui_main.cc'. |
| 121 | + errors = this->dataPtr->sdfRoot.Load(filePath); |
| 122 | + break; |
154 | 123 | } |
155 | 124 |
|
156 | | - if (filePath.empty()) |
| 125 | + case ServerConfig::SourceType::kNone: |
| 126 | + default: |
157 | 127 | { |
158 | | - ignerr << "Failed to find world [" << _config.SdfFile() << "]" |
159 | | - << std::endl; |
160 | | - return; |
| 128 | + ignmsg << "Loading default world.\n"; |
| 129 | + // Load an empty world. |
| 130 | + /// \todo(nkoenig) Add a "AddWorld" function to sdf::Root. |
| 131 | + errors = this->dataPtr->sdfRoot.LoadSdfString(DefaultWorld::World()); |
| 132 | + break; |
161 | 133 | } |
162 | | - |
163 | | - ignmsg << "Loading SDF world file[" << filePath << "].\n"; |
164 | | - |
165 | | - // \todo(nkoenig) Async resource download. |
166 | | - // This call can block for a long period of time while |
167 | | - // resources are downloaded. Blocking here causes the GUI to block with |
168 | | - // a black screen (search for "Async resource download" in |
169 | | - // 'src/gui_main.cc'. |
170 | | - errors = this->dataPtr->sdfRoot.Load(filePath); |
171 | | - } |
172 | | - else |
173 | | - { |
174 | | - ignmsg << "Loading default world.\n"; |
175 | | - // Load an empty world. |
176 | | - /// \todo(nkoenig) Add a "AddWorld" function to sdf::Root. |
177 | | - errors = this->dataPtr->sdfRoot.LoadSdfString(DefaultWorld::World()); |
178 | 134 | } |
179 | 135 |
|
180 | 136 | if (!errors.empty()) |
|
0 commit comments