diff --git a/src/Add-ons/AddonResourceProvider.cxx b/src/Add-ons/AddonResourceProvider.cxx new file mode 100644 index 000000000..0acdc0f20 --- /dev/null +++ b/src/Add-ons/AddonResourceProvider.cxx @@ -0,0 +1,80 @@ +// -*- coding: utf-8 -*- +// +// AddonResourceProvider.cxx --- ResourceProvider subclass for add-on files +// Copyright (C) 2018 Florent Rougon +// +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License along +// with this program; if not, write to the Free Software Foundation, Inc., +// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + +#include + +#include +#include +#include + +#include
+ +#include "AddonManager.hxx" +#include "AddonResourceProvider.hxx" + +namespace strutils = simgear::strutils; + +using std::string; + +namespace flightgear +{ + +namespace addons +{ + +ResourceProvider::ResourceProvider() + : simgear::ResourceProvider(simgear::ResourceManager::PRIORITY_NORMAL) +{ } + +SGPath +ResourceProvider::resolve(const string& resource, SGPath& context) const +{ + if (!strutils::starts_with(resource, "[addon=")) { + return SGPath(); + } + + string rest = resource.substr(7); // what follows '[addon=' + auto endOfAddonId = rest.find(']'); + + if (endOfAddonId == string::npos) { + return SGPath(); + } + + string addonId = rest.substr(0, endOfAddonId); + // Extract what follows '[addon=ADDON_ID]' + string relPath = rest.substr(endOfAddonId + 1); + + if (relPath.empty()) { + return SGPath(); + } + + const auto& addonMgr = AddonManager::instance(); + SGPath addonDir = addonMgr->addonBasePath(addonId); + SGPath candidate = addonDir / relPath; + + if (!candidate.isFile()) { + return SGPath(); + } + + return fgValidatePath(candidate, /* write */ false); +} + +} // of namespace addons + +} // of namespace flightgear diff --git a/src/Add-ons/AddonResourceProvider.hxx b/src/Add-ons/AddonResourceProvider.hxx new file mode 100644 index 000000000..bcc178755 --- /dev/null +++ b/src/Add-ons/AddonResourceProvider.hxx @@ -0,0 +1,47 @@ +// -*- coding: utf-8 -*- +// +// AddonResourceProvider.hxx --- ResourceProvider subclass for add-on files +// Copyright (C) 2018 Florent Rougon +// +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License along +// with this program; if not, write to the Free Software Foundation, Inc., +// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + +#ifndef FG_ADDON_RESOURCE_PROVIDER_HXX +#define FG_ADDON_RESOURCE_PROVIDER_HXX + +#include + +#include +#include + +namespace flightgear +{ + +namespace addons +{ + +class ResourceProvider : public simgear::ResourceProvider +{ +public: + ResourceProvider(); + + virtual SGPath resolve(const std::string& resource, SGPath& context) const + override; +}; + +} // of namespace addons + +} // of namespace flightgear + +#endif // of FG_ADDON_RESOURCE_PROVIDER_HXX diff --git a/src/Add-ons/CMakeLists.txt b/src/Add-ons/CMakeLists.txt index 0bd80a34c..7dfc01717 100644 --- a/src/Add-ons/CMakeLists.txt +++ b/src/Add-ons/CMakeLists.txt @@ -3,6 +3,7 @@ include(FlightGearComponent) set(SOURCES Addon.cxx AddonManager.cxx AddonMetadataParser.cxx + AddonResourceProvider.cxx AddonVersion.cxx contacts.cxx exceptions.cxx @@ -12,6 +13,7 @@ set(HEADERS addon_fwd.hxx Addon.hxx AddonManager.hxx AddonMetadataParser.hxx + AddonResourceProvider.hxx AddonVersion.hxx contacts.hxx exceptions.hxx diff --git a/src/Add-ons/addon_fwd.hxx b/src/Add-ons/addon_fwd.hxx index 50ce27d57..195eec482 100644 --- a/src/Add-ons/addon_fwd.hxx +++ b/src/Add-ons/addon_fwd.hxx @@ -33,6 +33,7 @@ class Addon; class AddonManager; class AddonVersion; class AddonVersionSuffix; +class ResourceProvider; enum class UrlType; class QualifiedUrl; diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index b8b403a4c..43e65728d 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -10,6 +10,7 @@ set(sources Add-ons/Addon.cxx Add-ons/AddonManager.cxx Add-ons/AddonMetadataParser.cxx + Add-ons/AddonResourceProvider.cxx Add-ons/AddonVersion.cxx Add-ons/contacts.cxx Add-ons/exceptions.cxx