From 70e897e875362e4e6924674d5699ed81fcdae55a Mon Sep 17 00:00:00 2001 From: Ian Chen Date: Wed, 28 Feb 2024 21:49:17 +0000 Subject: [PATCH] add search paths arg to resolveURI Signed-off-by: Ian Chen --- include/sdf/ParserConfig.hh | 8 -------- src/Heightmap.cc | 16 +++++++++------- src/Material.cc | 5 ++--- src/Mesh.cc | 4 +--- src/ParserConfig.cc | 12 ------------ src/SDF.cc | 9 --------- src/Sky.cc | 4 +--- src/Utils.cc | 16 +++++++++++++++- src/Utils.hh | 6 +++++- 9 files changed, 33 insertions(+), 47 deletions(-) diff --git a/include/sdf/ParserConfig.hh b/include/sdf/ParserConfig.hh index a8de279d0..b1a97ad0d 100644 --- a/include/sdf/ParserConfig.hh +++ b/include/sdf/ParserConfig.hh @@ -199,14 +199,6 @@ class SDFORMAT_VISIBLE ParserConfig /// store them. False to preserve original URIs public: bool StoreResolvedURIs() const; - /// \brief Set the path to search for URIs with relative path. - /// \param[in] _path Path to search. - public: void SetRelativeURISearchPath(const std::string &_path); - - /// \brief Get the path to search for URIs with relative path. - /// \return Path to search - public: const std::string &RelativeURISearchPath() const; - /// \brief Private data pointer. GZ_UTILS_IMPL_PTR(dataPtr) }; diff --git a/src/Heightmap.cc b/src/Heightmap.cc index 6c00d73bf..3dced1650 100644 --- a/src/Heightmap.cc +++ b/src/Heightmap.cc @@ -134,9 +134,11 @@ Errors HeightmapTexture::Load(ElementPtr _sdf, const ParserConfig &_config) if (_sdf->HasElement("diffuse")) { + auto path = std::filesystem::path( + this->dataPtr->sdf->FilePath()).parent_path(); this->dataPtr->diffuse = resolveURI( _sdf->Get(errors, "diffuse", this->dataPtr->diffuse).first, - _config, errors); + _config, errors, {path.string()}); } else { @@ -146,9 +148,11 @@ Errors HeightmapTexture::Load(ElementPtr _sdf, const ParserConfig &_config) if (_sdf->HasElement("normal")) { + auto path = std::filesystem::path( + this->dataPtr->sdf->FilePath()).parent_path(); this->dataPtr->normal = resolveURI( _sdf->Get(errors, "normal", this->dataPtr->normal).first, - _config, errors); + _config, errors, {path.string()}); } else { @@ -325,14 +329,12 @@ Errors Heightmap::Load(ElementPtr _sdf, const ParserConfig &_config) return errors; } - auto config = _config; - auto path = std::filesystem::path(this->dataPtr->filePath).parent_path(); - config.SetRelativeURISearchPath(path.string()); if (_sdf->HasElement("uri")) { + auto path = std::filesystem::path(this->dataPtr->filePath).parent_path(); this->dataPtr->uri = resolveURI( _sdf->Get(errors, "uri", "").first, - config, errors); + _config, errors, {path.string()}); } else { @@ -353,7 +355,7 @@ Errors Heightmap::Load(ElementPtr _sdf, const ParserConfig &_config) this->dataPtr->sampling).first; Errors textureLoadErrors = loadRepeated(_sdf, - "texture", this->dataPtr->textures, config); + "texture", this->dataPtr->textures, _config); errors.insert(errors.end(), textureLoadErrors.begin(), textureLoadErrors.end()); diff --git a/src/Material.cc b/src/Material.cc index d6cb19403..8335fbda0 100644 --- a/src/Material.cc +++ b/src/Material.cc @@ -124,10 +124,9 @@ Errors Material::Load(sdf::ElementPtr _sdf, const sdf::ParserConfig &_config) " element is empty."}); } - auto config = _config; auto path = std::filesystem::path(this->dataPtr->filePath).parent_path(); - config.SetRelativeURISearchPath(path.string()); - this->dataPtr->scriptUri = resolveURI(uriPair.first, config, errors); + this->dataPtr->scriptUri = resolveURI(uriPair.first, _config, errors, + {path.string()}); std::pair namePair = elem->Get(errors, "name", ""); diff --git a/src/Mesh.cc b/src/Mesh.cc index ebd7269d3..068102f77 100644 --- a/src/Mesh.cc +++ b/src/Mesh.cc @@ -84,12 +84,10 @@ Errors Mesh::Load(ElementPtr _sdf, const ParserConfig &_config) if (_sdf->HasElement("uri")) { - auto config = _config; auto path = std::filesystem::path(this->dataPtr->filePath).parent_path(); - config.SetRelativeURISearchPath(path.string()); this->dataPtr->uri = resolveURI( _sdf->Get(errors, "uri", "").first, - config, errors); + _config, errors, {path.string()}); } else { diff --git a/src/ParserConfig.cc b/src/ParserConfig.cc index 732a6fae2..0661b352c 100644 --- a/src/ParserConfig.cc +++ b/src/ParserConfig.cc @@ -201,15 +201,3 @@ bool ParserConfig::StoreResolvedURIs() const { return this->dataPtr->storeResolvedURIs; } - -///////////////////////////////////////////////// -void ParserConfig::SetRelativeURISearchPath(const std::string &_path) -{ - this->dataPtr->relativeUriSearchPath = _path; -} - -///////////////////////////////////////////////// -const std::string &ParserConfig::RelativeURISearchPath() const -{ - return this->dataPtr->relativeUriSearchPath; -} diff --git a/src/SDF.cc b/src/SDF.cc index c4c1e6302..7a43d0f9a 100644 --- a/src/SDF.cc +++ b/src/SDF.cc @@ -115,15 +115,6 @@ std::string findFile(const std::string &_filename, bool _searchLocalPath, { return path; } - else if (!_config.RelativeURISearchPath().empty()) - { - path = sdf::filesystem::append(_config.RelativeURISearchPath(), - filename); - if (sdf::filesystem::exists(path)) - { - return path; - } - } } // Next check the install path. diff --git a/src/Sky.cc b/src/Sky.cc index 023e811d2..d54e6daf6 100644 --- a/src/Sky.cc +++ b/src/Sky.cc @@ -203,12 +203,10 @@ Errors Sky::Load(ElementPtr _sdf, const ParserConfig &_config) if (_sdf->HasElement("cubemap_uri")) { - auto config = _config; auto path = std::filesystem::path(_sdf->FilePath()).parent_path(); - config.SetRelativeURISearchPath(path.string()); this->dataPtr->cubemapUri = resolveURI( _sdf->Get(errors, "cubemap_uri", "").first, - config, errors); + _config, errors, {path.string()}); } if ( _sdf->HasElement("clouds")) diff --git a/src/Utils.cc b/src/Utils.cc index 77ea4d35d..5e1c9f245 100644 --- a/src/Utils.cc +++ b/src/Utils.cc @@ -18,6 +18,7 @@ #include #include #include "sdf/Assert.hh" +#include "sdf/Filesystem.hh" #include "sdf/SDFImpl.hh" #include "Utils.hh" @@ -382,11 +383,24 @@ void copyChildren(ElementPtr _sdf, tinyxml2::XMLElement *_xml, ///////////////////////////////////////////////// std::string resolveURI(const std::string &_inputURI, - const sdf::ParserConfig &_config, sdf::Errors &_errors) + const sdf::ParserConfig &_config, sdf::Errors &_errors, + const std::unordered_set &_searchPaths) { std::string resolvedURI = _inputURI; if (_config.StoreResolvedURIs()) { + std::string sep("://"); + if (!_searchPaths.empty() && _inputURI.find(sep) == std::string::npos) + { + for (const auto &sp : _searchPaths) + { + std::string fullPath = sdf::filesystem::append(sp, _inputURI); + resolvedURI = sdf::findFile(fullPath, true, false); + if (!resolvedURI.empty()) + return resolvedURI; + } + } + resolvedURI = sdf::findFile(_inputURI, true, true, _config); if (resolvedURI.empty()) { diff --git a/src/Utils.hh b/src/Utils.hh index 18898eadd..b02ff91a0 100644 --- a/src/Utils.hh +++ b/src/Utils.hh @@ -23,6 +23,7 @@ #include #include #include +#include #include "sdf/Error.hh" #include "sdf/Element.hh" #include "sdf/InterfaceElements.hh" @@ -262,10 +263,13 @@ namespace sdf /// \param[in] _inputURI URI from parsed SDF file to resolve /// \param[in] _config Parser configuration to use to resolve /// \param[in, out] _errors Error vector to append to if resolution fails + /// \param[in] _searchPaths Optional additional search paths (directory) + /// when resolving URI /// \return Resolved URI or Original URI, depending on parser configuration std::string resolveURI(const std::string &_inputURI, const sdf::ParserConfig &_config, - sdf::Errors &_errors); + sdf::Errors &_errors, + const std::unordered_set &_searchPaths = {}); } } #endif