1
0
Fork 0

Add-ons: move all code to the flightgear::addons namespace

This commit is contained in:
Florent Rougon 2017-12-15 23:53:46 +01:00
parent c3b1442546
commit f5ab26bd71
16 changed files with 90 additions and 34 deletions

View file

@ -48,6 +48,9 @@ using std::vector;
namespace flightgear
{
namespace addons
{
Addon::Addon(std::string id, AddonVersion version, SGPath basePath,
std::string minFGVersionRequired, std::string maxFGVersionRequired,
SGPropertyNode* addonNode)
@ -239,14 +242,14 @@ Addon Addon::fromAddonDir(const SGPath& addonPath)
SGPropertyNode addonRoot;
if (!metadataFile.exists()) {
throw addon_errors::no_metadata_file_found(
throw errors::no_metadata_file_found(
"unable to find add-on metadata file '" + metadataFile.utf8Str() + "'");
}
try {
readProperties(metadataFile, &addonRoot);
} catch (const sg_exception &e) {
throw addon_errors::error_loading_metadata_file(
throw errors::error_loading_metadata_file(
"unable to load add-on metadata file '" + metadataFile.utf8Str() + "': " +
e.getFormattedMessage());
}
@ -254,7 +257,7 @@ Addon Addon::fromAddonDir(const SGPath& addonPath)
// Check the 'meta' section
SGPropertyNode *metaNode = addonRoot.getChild("meta");
if (metaNode == nullptr) {
throw addon_errors::error_loading_metadata_file(
throw errors::error_loading_metadata_file(
"no /meta node found in add-on metadata file '" +
metadataFile.utf8Str() + "'");
}
@ -262,14 +265,14 @@ Addon Addon::fromAddonDir(const SGPath& addonPath)
// Check the file type
SGPropertyNode *fileTypeNode = metaNode->getChild("file-type");
if (fileTypeNode == nullptr) {
throw addon_errors::error_loading_metadata_file(
throw errors::error_loading_metadata_file(
"no /meta/file-type node found in add-on metadata file '" +
metadataFile.utf8Str() + "'");
}
string fileType = fileTypeNode->getStringValue();
if (fileType != "FlightGear add-on metadata") {
throw addon_errors::error_loading_metadata_file(
throw errors::error_loading_metadata_file(
"Invalid /meta/file-type value for add-on metadata file '" +
metadataFile.utf8Str() + "': '" + fileType + "' "
"(expected 'FlightGear add-on metadata')");
@ -278,14 +281,14 @@ Addon Addon::fromAddonDir(const SGPath& addonPath)
// Check the format version
SGPropertyNode *fmtVersionNode = metaNode->getChild("format-version");
if (fmtVersionNode == nullptr) {
throw addon_errors::error_loading_metadata_file(
throw errors::error_loading_metadata_file(
"no /meta/format-version node found in add-on metadata file '" +
metadataFile.utf8Str() + "'");
}
int formatVersion = fmtVersionNode->getIntValue();
if (formatVersion != 1) {
throw addon_errors::error_loading_metadata_file(
throw errors::error_loading_metadata_file(
"unknown format version in add-on metadata file '" +
metadataFile.utf8Str() + "': " + std::to_string(formatVersion));
}
@ -293,14 +296,14 @@ Addon Addon::fromAddonDir(const SGPath& addonPath)
// Now the data we are really interested in
SGPropertyNode *addonNode = addonRoot.getChild("addon");
if (addonNode == nullptr) {
throw addon_errors::error_loading_metadata_file(
throw errors::error_loading_metadata_file(
"no /addon node found in add-on metadata file '" +
metadataFile.utf8Str() + "'");
}
SGPropertyNode *idNode = addonNode->getChild("identifier");
if (idNode == nullptr) {
throw addon_errors::error_loading_metadata_file(
throw errors::error_loading_metadata_file(
"no /addon/identifier node found in add-on metadata file '" +
metadataFile.utf8Str() + "'");
}
@ -308,7 +311,7 @@ Addon Addon::fromAddonDir(const SGPath& addonPath)
// Require a non-empty identifier for the add-on
if (addonId.empty()) {
throw addon_errors::error_loading_metadata_file(
throw errors::error_loading_metadata_file(
"empty or whitespace-only value for the /addon/identifier node in "
"add-on metadata file '" + metadataFile.utf8Str() + "'");
} else if (addonId.find('.') == string::npos) {
@ -320,7 +323,7 @@ Addon Addon::fromAddonDir(const SGPath& addonPath)
SGPropertyNode *nameNode = addonNode->getChild("name");
if (nameNode == nullptr) {
throw addon_errors::error_loading_metadata_file(
throw errors::error_loading_metadata_file(
"no /addon/name node found in add-on metadata file '" +
metadataFile.utf8Str() + "'");
}
@ -328,14 +331,14 @@ Addon Addon::fromAddonDir(const SGPath& addonPath)
// Require a non-empty name for the add-on
if (addonName.empty()) {
throw addon_errors::error_loading_metadata_file(
throw errors::error_loading_metadata_file(
"empty or whitespace-only value for the /addon/name node in add-on "
"metadata file '" + metadataFile.utf8Str() + "'");
}
SGPropertyNode *versionNode = addonNode->getChild("version");
if (versionNode == nullptr) {
throw addon_errors::error_loading_metadata_file(
throw errors::error_loading_metadata_file(
"no /addon/version node found in add-on metadata file '" +
metadataFile.utf8Str() + "'");
}
@ -535,4 +538,6 @@ std::ostream& operator<<(std::ostream& os, const Addon& addon)
return os << addon.str();
}
} // of namespace addons
} // of namespace flightgear

View file

@ -37,6 +37,9 @@
namespace flightgear
{
namespace addons
{
class Addon : public SGReferenced {
public:
// Default constructor. 'minFGVersionRequired' is initialized to "2017.4.0"
@ -178,6 +181,8 @@ private:
std::ostream& operator<<(std::ostream& os, const Addon& addonMetaData);
} // of namespace addons
} // of namespace flightgear
#endif // of FG_ADDON_HXX

View file

@ -53,6 +53,9 @@ using std::unique_ptr;
namespace flightgear
{
namespace addons
{
static unique_ptr<AddonManager> staticInstance;
// ***************************************************************************
@ -95,7 +98,7 @@ AddonManager::loadConfigFileIfExists(const SGPath& configFile)
try {
readProperties(configFile, globals->get_props());
} catch (const sg_exception &e) {
throw addon_errors::error_loading_config_file(
throw errors::error_loading_config_file(
"unable to load add-on config file '" + configFile.utf8Str() + "': " +
e.getFormattedMessage());
}
@ -120,7 +123,7 @@ AddonManager::registerAddonMetadata(const SGPath& addonPath)
// Check that the FlightGear version satisfies the add-on requirements
std::string minFGversion = addon->getMinFGVersionRequired();
if (strutils::compare_versions(FLIGHTGEAR_VERSION, minFGversion) < 0) {
throw addon_errors::fg_version_too_old(
throw errors::fg_version_too_old(
"add-on '" + addonId + "' requires FlightGear " + minFGversion +
" or later, however this is FlightGear " + FLIGHTGEAR_VERSION);
}
@ -128,7 +131,7 @@ AddonManager::registerAddonMetadata(const SGPath& addonPath)
std::string maxFGversion = addon->getMaxFGVersionRequired();
if (maxFGversion != "none" &&
strutils::compare_versions(FLIGHTGEAR_VERSION, maxFGversion) > 0) {
throw addon_errors::fg_version_too_recent(
throw errors::fg_version_too_recent(
"add-on '" + addonId + "' requires FlightGear " + maxFGversion +
" or earlier, however this is FlightGear " + FLIGHTGEAR_VERSION);
}
@ -140,7 +143,7 @@ AddonManager::registerAddonMetadata(const SGPath& addonPath)
if (!emplaceRetval.second) {
auto existingElt = _idToAddonMap.find(addonId);
assert(existingElt != _idToAddonMap.end());
throw addon_errors::duplicate_registration_attempt(
throw errors::duplicate_registration_attempt(
"attempt to register add-on '" + addonId + "' with base path '"
+ addonPath.utf8Str() + "', however it is already registered with base "
"path '" + existingElt->second->getBasePath().utf8Str() + "'");
@ -254,4 +257,6 @@ SGPropertyNode_ptr AddonManager::addonNode(const string& addonId) const
return getAddon(addonId)->getAddonNode();
}
} // of namespace addons
} // of namespace flightgear

View file

@ -35,6 +35,9 @@
namespace flightgear
{
namespace addons
{
class AddonManager
{
public:
@ -100,6 +103,8 @@ private:
int _loadSequenceNumber = 0;
};
} // of namespace addons
} // of namespace flightgear
#endif // of FG_ADDONMANAGER_HXX

View file

@ -47,6 +47,9 @@ namespace strutils = simgear::strutils;
namespace flightgear
{
namespace addons
{
// ***************************************************************************
// * AddonVersionSuffix *
// ***************************************************************************
@ -399,4 +402,6 @@ void AddonVersion::setupGhost(nasal::Hash& addonsModule)
.method("greaterThanOrEqual", &AddonVersion::greaterThanOrEqual);
}
} // of namespace addons
} // of namespace flightgear

View file

@ -34,6 +34,9 @@
namespace flightgear
{
namespace addons
{
// Order matters for the sorting/comparison functions
enum class AddonVersionSuffixPrereleaseType {
alpha = 0,
@ -170,6 +173,8 @@ bool operator>=(const AddonVersion& lhs, const AddonVersion& rhs);
std::ostream& operator<<(std::ostream&, const AddonVersion&);
} // of namespace addons
} // of namespace flightgear
#endif // of FG_ADDONVERSION_HXX

View file

@ -26,6 +26,9 @@
namespace flightgear
{
namespace addons
{
class Addon;
class AddonManager;
class AddonVersion;
@ -34,7 +37,7 @@ class AddonVersionSuffix;
using AddonRef = SGSharedPtr<Addon>;
using AddonVersionRef = SGSharedPtr<AddonVersion>;
namespace addon_errors
namespace errors
{
class error;
@ -45,7 +48,9 @@ class duplicate_registration_attempt;
class fg_version_too_old;
class fg_version_too_recent;
} // of namespace addon_errors
} // of namespace errors
} // of namespace addons
} // of namespace flightgear

View file

@ -28,10 +28,14 @@ using std::string;
namespace flightgear
{
namespace addon_errors
namespace addons
{
namespace errors
{
// ***************************************************************************
// * Base class for custom exceptions *
// * Base class for add-on exceptions *
// ***************************************************************************
// Prepending a prefix such as "Add-on error: " would be redundant given the
@ -44,6 +48,8 @@ error::error(const char* message, const char* origin)
: error(string(message), string(origin))
{ }
} // of namespace addon_errors
} // of namespace errors
} // of namespace addons
} // of namespace flightgear

View file

@ -27,7 +27,10 @@
namespace flightgear
{
namespace addon_errors
namespace addons
{
namespace errors
{
class error : public sg_exception
@ -56,7 +59,9 @@ class fg_version_too_old : public error
class fg_version_too_recent : public error
{ using error::error; };
} // of namespace addon_errors
} // of namespace errors
} // of namespace addons
} // of namespace flightgear

View file

@ -38,12 +38,14 @@
using std::string;
using std::vector;
using namespace flightgear;
using flightgear::addons::Addon;
using flightgear::addons::AddonVersion;
using flightgear::addons::AddonVersionSuffix;
void testAddonVersionSuffix()
{
using AddonRelType = AddonVersionSuffixPrereleaseType;
using AddonRelType = flightgear::addons::AddonVersionSuffixPrereleaseType;
fgtest::initTestGlobals("AddonVersion");
@ -140,7 +142,7 @@ void testAddonVersionSuffix()
void testAddonVersion()
{
using AddonRelType = AddonVersionSuffixPrereleaseType;
using AddonRelType = flightgear::addons::AddonVersionSuffixPrereleaseType;
fgtest::initTestGlobals("AddonVersion");

View file

@ -1138,8 +1138,8 @@ void fgStartNewReset()
// otherwise channels are duplicated
globals->get_channel_options_list()->clear();
flightgear::AddonManager::reset();
flightgear::AddonManager::createInstance();
flightgear::addons::AddonManager::reset();
flightgear::addons::AddonManager::createInstance();
fgInitConfig(0, NULL, true);
fgInitGeneral(); // all of this?

View file

@ -535,7 +535,7 @@ int fgMainInit( int argc, char **argv )
return EXIT_SUCCESS;
}
AddonManager::createInstance();
addons::AddonManager::createInstance();
configResult = flightgear::Options::sharedInstance()->processOptions();
if (configResult == flightgear::FG_OPTIONS_ERROR) {

View file

@ -707,7 +707,7 @@ static int
fgOptAddon(const char *arg)
{
const SGPath addonPath = SGPath::fromLocal8Bit(arg);
const auto& addonManager = AddonManager::instance();
const auto& addonManager = addons::AddonManager::instance();
try {
addonManager->registerAddon(addonPath);

View file

@ -36,6 +36,9 @@
namespace flightgear
{
namespace addons
{
// ***************************************************************************
// * AddonManager *
// ***************************************************************************
@ -194,4 +197,6 @@ void initAddonClassesForNasal(naRef globals, naContext c)
addonsModule.createHash("AddonVersion").set("new", &f_createAddonVersion);
}
} // of namespace addons
} // of namespace flightgear

View file

@ -25,8 +25,13 @@
namespace flightgear
{
namespace addons
{
void initAddonClassesForNasal(naRef globals, naContext c);
} // of namespace addons
} // of namespace flightgear
#endif // of FG_ADDON_NASAL_INTERFACE_HXX

View file

@ -857,8 +857,6 @@ void FGNasalSys::setCmdArg(SGPropertyNode* aNode)
void FGNasalSys::init()
{
using namespace flightgear;
if (_inited) {
SG_LOG(SG_GENERAL, SG_ALERT, "duplicate init of Nasal");
}
@ -914,7 +912,7 @@ void FGNasalSys::init()
.member("simulatedTime", &TimerObj::isSimTime, &f_timerObj_setSimTime)
.member("isRunning", &TimerObj::isRunning);
initAddonClassesForNasal(_globals, _context);
flightgear::addons::initAddonClassesForNasal(_globals, _context);
// Now load the various source files in the Nasal directory
simgear::Dir nasalDir(SGPath(globals->get_fg_root(), "Nasal"));