1
0
Fork 0

Add-ons: add method resourcePath() to addons.Addon ghosts

This method takes a string such as "this/is/a/relative/path" and returns
"[addon=ADDON_ID]this/is/a/relative/path", substituting the add-on
identifier for ADDON_ID.

This way, add-on authors don't even have to know the special syntax
'[addon=ADDON_ID]relative/path' used for add-on-specific resource paths,
and don't need to hardcode their add-on identifier inside each such path
either.
This commit is contained in:
Florent Rougon 2018-01-10 01:00:34 +01:00
parent 717fdfec39
commit 5f50e25535
4 changed files with 25 additions and 0 deletions

View file

@ -24,6 +24,7 @@
#include <vector>
#include <simgear/misc/sg_path.hxx>
#include <simgear/misc/strutils.hxx>
#include <simgear/nasal/cppbind/Ghost.hxx>
#include <simgear/nasal/cppbind/NasalHash.hxx>
#include <simgear/nasal/naref.h>
@ -36,8 +37,11 @@
#include "Addon.hxx"
#include "AddonMetadataParser.hxx"
#include "AddonVersion.hxx"
#include "exceptions.hxx"
#include "pointer_traits.hxx"
namespace strutils = simgear::strutils;
using std::string;
using std::vector;
@ -180,6 +184,17 @@ SGPath Addon::getBasePath() const
void Addon::setBasePath(const SGPath& addonBasePath)
{ _basePath = addonBasePath; }
std::string Addon::resourcePath(const std::string& relativePath) const
{
if (strutils::starts_with(relativePath, "/")) {
throw errors::invalid_resource_path(
"addon-specific resource path '" + relativePath + "' shouldn't start "
"with a '/'");
}
return "[addon=" + getId() + "]" + relativePath;
}
std::string Addon::getMinFGVersionRequired() const
{ return _minFGVersionRequired; }
@ -315,6 +330,7 @@ void Addon::setupGhost(nasal::Hash& addonsModule)
.member("licenseUrl", &Addon::getLicenseUrl)
.member("tags", &Addon::getTags)
.member("basePath", &Addon::getBasePath)
.method("resourcePath", &Addon::resourcePath)
.member("minFGVersionRequired", &Addon::getMinFGVersionRequired)
.member("maxFGVersionRequired", &Addon::getMaxFGVersionRequired)
.member("homePage", &Addon::getHomePage)

View file

@ -127,6 +127,11 @@ public:
SGPath getBasePath() const;
void setBasePath(const SGPath& addonBasePath);
// Return a resource path suitable for use with the simgear::ResourceManager.
// 'relativePath' is relative to the add-on base path, and should not start
// with a '/'.
std::string resourcePath(const std::string& relativePath) const;
// Should be valid for use with simgear::strutils::compare_versions()
std::string getMinFGVersionRequired() const;
void setMinFGVersionRequired(const std::string& minFGVersionRequired);

View file

@ -59,6 +59,7 @@ class error_loading_metadata_file;
class duplicate_registration_attempt;
class fg_version_too_old;
class fg_version_too_recent;
class invalid_resource_path;
} // of namespace errors

View file

@ -59,6 +59,9 @@ class fg_version_too_old : public error
class fg_version_too_recent : public error
{ using error::error; };
class invalid_resource_path : public error
{ using error::error; };
} // of namespace errors
} // of namespace addons