Skip to content

Commit

Permalink
Allow using plugin file names and environment variables compatible wi…
Browse files Browse the repository at this point in the history
…th Garden and later (gazebosim#2275)

Allows using gz-* in plugin filenames (eg. <plugin filename="gz-sim-physics-system" name="gz::sim::systems::Physics"/>) and using GZ_* environment variables in Fortress. This is meant to help users (especially ROS users) maintain SDF files and launch files that work in both Fortress and Garden/Harmonic.

---------

Signed-off-by: Addisu Z. Taddese <addisu@openrobotics.org>
  • Loading branch information
azeey authored Jan 12, 2024
1 parent 5d34efe commit 45a01a8
Show file tree
Hide file tree
Showing 17 changed files with 376 additions and 135 deletions.
18 changes: 15 additions & 3 deletions include/gz/sim/Util.hh
Original file line number Diff line number Diff line change
Expand Up @@ -287,19 +287,31 @@ namespace ignition
std::optional<math::Vector3d> IGNITION_GAZEBO_VISIBLE sphericalCoordinates(
Entity _entity, const EntityComponentManager &_ecm);

/// \brief Environment variable holding resource paths.
/// \brief Environment variable holding resource paths. Prefer using
/// GZ_SIM_RESOURCE_PATH for compatibility with newer versions of Gazebo.
const std::string kResourcePathEnv{"IGN_GAZEBO_RESOURCE_PATH"};
/// \brief Environment variable holding resource paths.
const std::string kResourcePathEnvGzSim{"GZ_SIM_RESOURCE_PATH"};

/// \brief Environment variable used by SDFormat to find URIs inside
/// `<include>`
const std::string kSdfPathEnv{"SDF_PATH"};

/// \brief Environment variable holding server config paths.
/// \brief Environment variable holding server config paths. Prefer using
/// GZ_SIM_SERVER_CONFIG_PATH for compatibility with newer versions of
/// Gazebo.
const std::string kServerConfigPathEnv{"IGN_GAZEBO_SERVER_CONFIG_PATH"};
/// \brief Environment variable holding server config paths.
const std::string kServerConfigPathEnvGzSim{"GZ_SIM_SERVER_CONFIG_PATH"};

/// \brief Environment variable holding paths to custom rendering engine
/// plugins.
/// plugins. Prefer using GZ_SIM_RENDER_ENGINE_PATH for compatibility with
/// newer versions of Gazebo
const std::string kRenderPluginPathEnv{"IGN_GAZEBO_RENDER_ENGINE_PATH"};
/// \brief Environment variable holding paths to custom rendering engine
/// plugins.
const std::string kRenderPluginPathEnvGzSim{"GZ_SIM_RENDER_ENGINE_PATH"};

}
}
}
Expand Down
5 changes: 2 additions & 3 deletions src/Conversions.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1713,9 +1713,8 @@ msgs::ParticleEmitter ignition::gazebo::convert(const sdf::ParticleEmitter &_in)
{
std::string path = asFullPath(_in.ColorRangeImage(), _in.FilePath());

common::SystemPaths systemPaths;
systemPaths.SetFilePathEnv(kResourcePathEnv);
std::string absolutePath = systemPaths.FindFile(path);
const std::string absolutePath =
common::SystemPaths::LocateLocalFile(path, gazebo::resourcePaths());

if (!absolutePath.empty())
out.mutable_color_range_image()->set_data(absolutePath);
Expand Down
54 changes: 35 additions & 19 deletions src/ServerConfig.cc
Original file line number Diff line number Diff line change
Expand Up @@ -922,30 +922,26 @@ ignition::gazebo::parsePluginsFromString(const std::string &_str)
std::list<ServerConfig::PluginInfo>
ignition::gazebo::loadPluginInfo(bool _isPlayback)
{
std::list<ServerConfig::PluginInfo> ret;

// 1. Check contents of environment variable
std::string envConfig;
bool configSet = common::env(kServerConfigPathEnv,
envConfig,
true);

if (configSet)
auto parsePlugins = [](const std::string &_serverConfigPathEnv,
const std::string &_envConfig)
{
if (common::exists(envConfig))
std::list<ServerConfig::PluginInfo> ret;
if (common::exists(_envConfig))
{
// Parse configuration stored in environment variable
ret = gz::sim::parsePluginsFromFile(envConfig);
ret = gz::sim::parsePluginsFromFile(_envConfig);
if (ret.empty())
{
// This may be desired behavior, but warn just in case.
// Some users may want to defer all loading until later
// during runtime.
ignwarn << kServerConfigPathEnv
<< " set but no plugins found\n";
ignwarn << _serverConfigPathEnv
<< " set but no plugins found\n";
}
igndbg << "Loaded (" << ret.size() << ") plugins from file " <<
"[" << envConfig << "]\n";
"[" << _envConfig << "]\n";

return ret;
}
Expand All @@ -954,11 +950,31 @@ ignition::gazebo::loadPluginInfo(bool _isPlayback)
// This may be desired behavior, but warn just in case.
// Some users may want to defer all loading until late
// during runtime.
ignwarn << kServerConfigPathEnv
<< " set but no file found,"
<< " no plugins loaded\n";
ignwarn << _serverConfigPathEnv
<< " set but no file found,"
<< " no plugins loaded\n";
return ret;
}
};

{
std::string envConfig;
bool configSet = common::env(kServerConfigPathEnv, envConfig, true);
if (configSet)
{
return parsePlugins(kServerConfigPathEnv, envConfig);
}
}

// Process the gz-sim environment variable the same way. If the IGN variable
// is set, it will have precedence.
{
std::string envConfig;
bool configSet = common::env(kServerConfigPathEnvGzSim, envConfig, true);
if (configSet)
{
return parsePlugins(kServerConfigPathEnvGzSim, envConfig);
}
}

std::string configFilename;
Expand Down Expand Up @@ -989,7 +1005,7 @@ ignition::gazebo::loadPluginInfo(bool _isPlayback)
{
ignerr << "Failed to create directory [" << defaultConfigDir
<< "]." << std::endl;
return ret;
return {};
}

if (!common::exists(installedConfig))
Expand All @@ -998,14 +1014,14 @@ ignition::gazebo::loadPluginInfo(bool _isPlayback)
<< "] to default config [" << defaultConfig << "]."
<< "(file " << installedConfig << " doesn't exist)"
<< std::endl;
return ret;
return {};
}
else if (!common::copyFile(installedConfig, defaultConfig))
{
ignerr << "Failed to copy installed config [" << installedConfig
<< "] to default config [" << defaultConfig << "]."
<< std::endl;
return ret;
return {};
}
else
{
Expand All @@ -1015,7 +1031,7 @@ ignition::gazebo::loadPluginInfo(bool _isPlayback)
}
}

ret = gz::sim::parsePluginsFromFile(defaultConfig);
const auto ret = gz::sim::parsePluginsFromFile(defaultConfig);

if (ret.empty())
{
Expand Down
35 changes: 28 additions & 7 deletions src/ServerConfig_TEST.cc
Original file line number Diff line number Diff line change
Expand Up @@ -198,14 +198,8 @@ TEST(LoadPluginInfo, FromEmptyEnv)
EXPECT_TRUE(common::unsetenv(kServerConfigPathEnv));
}

//////////////////////////////////////////////////
TEST(LoadPluginInfo, FromValidEnv)
void testFromValidEnvPlugins()
{
auto validPath = common::joinPaths(PROJECT_SOURCE_PATH,
"test", "worlds", "server_valid2.config");

ASSERT_TRUE(common::setenv(kServerConfigPathEnv, validPath));

auto plugins = loadPluginInfo();
ASSERT_EQ(2u, plugins.size());

Expand All @@ -226,9 +220,36 @@ TEST(LoadPluginInfo, FromValidEnv)
EXPECT_EQ("gz::sim::TestModelSystem", plugin->Name());
EXPECT_EQ("gz::sim::TestModelSystem", plugin->Plugin().Name());

}
//////////////////////////////////////////////////
TEST(LoadPluginInfo, FromValidEnv)
{
auto validPath = common::joinPaths(PROJECT_SOURCE_PATH,
"test", "worlds", "server_valid2.config");

ASSERT_TRUE(common::setenv(kServerConfigPathEnv, validPath));

SCOPED_TRACE("FromValidEnv");
testFromValidEnvPlugins();

EXPECT_TRUE(common::unsetenv(kServerConfigPathEnv));
}

//////////////////////////////////////////////////
TEST(LoadPluginInfo, FromValidEnvGzSimCompatibility)
{
auto validPath = common::joinPaths(PROJECT_SOURCE_PATH,
"test", "worlds", "server_valid2.config");

ASSERT_TRUE(common::unsetenv(kServerConfigPathEnv));
ASSERT_TRUE(common::setenv(kServerConfigPathEnvGzSim, validPath));

SCOPED_TRACE("FromValidEnvGzSimCompatibility");
testFromValidEnvPlugins();

EXPECT_TRUE(common::unsetenv(kServerConfigPathEnvGzSim));
}

//////////////////////////////////////////////////
TEST(ServerConfig, GenerateRecordPlugin)
{
Expand Down
6 changes: 3 additions & 3 deletions src/ServerPrivate.hh
Original file line number Diff line number Diff line change
Expand Up @@ -122,9 +122,9 @@ namespace ignition
/// string and return value of false will be used if the resource could
/// not be found.
///
/// Fuel will be checked and then the GZ_GAZEBO_RESOURCE_PATH environment
/// variable paths. This service will not check for files relative to
/// working directory of the Gazebo server.
/// Fuel will be checked and then paths from IGN_GAZEBO_RESOURCE_PATH and
/// GZ_SIM_RESOURCE_PATH environment variables. This service will not
/// check for files relative to working directory of the Gazebo server.
///
/// \param[in] _req Request filled with a resource URI to resolve.
/// Example values could be:
Expand Down
Loading

0 comments on commit 45a01a8

Please sign in to comment.