Debian Codegraph @

src/Add-ons/AddonResourceProvider.cxx

raw symbols SCIP
1 // -*- coding: utf-8 -*-
2 //
3 // AddonResourceProvider.cxx --- ResourceProvider subclass for add-on files
4 // Copyright (C) 2018 Florent Rougon
5 //
6 // This program is free software; you can redistribute it and/or modify
7 // it under the terms of the GNU General Public License as published by
8 // the Free Software Foundation; either version 2 of the License, or
9 // (at your option) any later version.
10 //
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
15 //
16 // You should have received a copy of the GNU General Public License along
17 // with this program; if not, write to the Free Software Foundation, Inc.,
18 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19
20 #include <string>
21
22 #include <simgear/misc/ResourceManager.hxx>
23 #include <simgear/misc/sg_path.hxx>
24 #include <simgear/misc/strutils.hxx>
25
26 #include "AddonManager.hxx"
27 #include "AddonResourceProvider.hxx"
28
29 namespace strutils = simgear::strutils;
30
31 using std::string;
32
33 namespace flightgear
34 {
35
36 namespace addons
37 {
38
39 ResourceProvider::ResourceProvider()
40 : simgear::ResourceProvider(simgear::ResourceManager::PRIORITY_DEFAULT)
41 { }
42
43 SGPath
44 ResourceProvider::resolve(const string& resource, SGPath& context) const
45 {
46 if (!strutils::starts_with(resource, "[addon=")) {
47 return SGPath();
48 }
49
50 string rest = resource.substr(7); // what follows '[addon='
51 auto endOfAddonId = rest.find(']');
52
53 if (endOfAddonId == string::npos) {
54 return SGPath();
55 }
56
57 string addonId = rest.substr(0, endOfAddonId);
58 // Extract what follows '[addon=ADDON_ID]'
59 string relPath = rest.substr(endOfAddonId + 1);
60
61 if (relPath.empty()) {
62 return SGPath();
63 }
64
65 const auto& addonMgr = AddonManager::instance();
66 SGPath addonDir = addonMgr->addonBasePath(addonId);
67 SGPath candidate = addonDir / relPath;
68
69 if (!candidate.isFile()) {
70 return SGPath();
71 }
72
73 return SGPath(candidate).validate(/* write */ false);
74 }
75
76 } // of namespace addons
77
78 } // of namespace flightgear