1
0
Fork 0

Add-ons: add a simgear::ResourceProvider for add-on-specific lookup

A resource can be specified with the syntax:

  [addon=ADDON_ID]relative/path

Such a resource corresponds to the file $addon_base_path/relative/path
for the specific add-on whose identifier is ADDON_ID.

If the particular add-on isn't registered, looking up such a resource
throws sg_exception.
This commit is contained in:
Florent Rougon 2018-01-10 00:17:22 +01:00
parent a137aed541
commit 8c82fca416
5 changed files with 131 additions and 0 deletions

View file

@ -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 <string>
#include <simgear/misc/ResourceManager.hxx>
#include <simgear/misc/sg_path.hxx>
#include <simgear/misc/strutils.hxx>
#include <Main/util.hxx>
#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

View file

@ -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 <string>
#include <simgear/misc/ResourceManager.hxx>
#include <simgear/misc/sg_path.hxx>
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

View file

@ -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

View file

@ -33,6 +33,7 @@ class Addon;
class AddonManager;
class AddonVersion;
class AddonVersionSuffix;
class ResourceProvider;
enum class UrlType;
class QualifiedUrl;

View file

@ -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