mirror of
https://github.com/swift-project/pilotclient.git
synced 2026-04-06 01:45:38 +08:00
refs #392 Added BlackGui::IPluginConfig
* BlackGui::IPluginConfig is an interface for plugin configurations * Added plugin_xplane_config target * Adapted IPluginManager * Adapted CPluginManagerSimulator * Adaptem CSettingsSimulatorComponent
This commit is contained in:
committed by
Mathew Sutcliffe
parent
584616de7a
commit
a0b4d47736
@@ -39,26 +39,15 @@ namespace BlackCore
|
||||
QDirIterator it(pluginDir, QDirIterator::FollowSymlinks);
|
||||
while (it.hasNext())
|
||||
{
|
||||
if (!QLibrary::isLibrary(it.next()))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
CLogMessage(this).debug() << "Loading plugin: " << it.filePath();
|
||||
QPluginLoader loader(it.filePath());
|
||||
QJsonObject json = loader.metaData();
|
||||
if (!isValid(json))
|
||||
{
|
||||
CLogMessage(this).warning("Plugin %1 invalid, not loading it") << it.filePath();
|
||||
continue;
|
||||
}
|
||||
|
||||
QString identifier = pluginIdentifier(json);
|
||||
m_paths.insert(identifier, it.filePath());
|
||||
m_metadatas.push_back(json);
|
||||
tryLoad(it.next());
|
||||
}
|
||||
}
|
||||
|
||||
QString IPluginManager::getPluginConfigId(const QString &identifier)
|
||||
{
|
||||
return m_configs.contains(identifier) ? m_configs.value(identifier) : QString();
|
||||
}
|
||||
|
||||
QString IPluginManager::pluginDirectory() const
|
||||
{
|
||||
return qApp->applicationDirPath() % QStringLiteral("/plugins");
|
||||
@@ -100,6 +89,35 @@ namespace BlackCore
|
||||
return m_instanceIds.value(instance, QString());
|
||||
}
|
||||
|
||||
bool IPluginManager::tryLoad(QString path)
|
||||
{
|
||||
if (!QLibrary::isLibrary(path))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
CLogMessage(this).debug() << "Loading plugin: " << path;
|
||||
QPluginLoader loader(path);
|
||||
QJsonObject json = loader.metaData();
|
||||
if (!isValid(json))
|
||||
{
|
||||
CLogMessage(this).warning("Plugin %1 invalid, not loading it") << path;
|
||||
return false;
|
||||
}
|
||||
|
||||
QString identifier = pluginIdentifier(json);
|
||||
m_paths.insert(identifier, path);
|
||||
m_metadatas.push_back(json);
|
||||
|
||||
if (json.value("MetaData").toObject().contains("config")) {
|
||||
QString configId = json.value("MetaData").toObject().value("config").toString();
|
||||
if (!configId.isEmpty())
|
||||
m_configs.insert(identifier, configId);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
QObject *IPluginManager::getPluginByIdImpl(const QString &identifier)
|
||||
{
|
||||
if (m_instances.contains(identifier))
|
||||
|
||||
@@ -36,6 +36,19 @@ namespace BlackCore
|
||||
//! Looks for all available plugins
|
||||
virtual void collectPlugins();
|
||||
|
||||
//! If the plugin specifies its config plugin, its identifier can be
|
||||
//! obtained using this method. You can get the plugin config instance
|
||||
//! later, using `getPluginById()`.
|
||||
QString getPluginConfigId(const QString &identifier);
|
||||
|
||||
//! Loads the given plugin (if necessary), casts it to the desired
|
||||
//! type and returns its instance. Returns `nullptr` on failure.
|
||||
template <class T>
|
||||
T *getPluginById(const QString &identifier)
|
||||
{
|
||||
return qobject_cast<T *>(getPluginByIdImpl(identifier));
|
||||
}
|
||||
|
||||
protected:
|
||||
//! Returns the list of valid IIDs for the implementation
|
||||
virtual BlackMisc::CSequence<QString> acceptedIids() const = 0;
|
||||
@@ -57,14 +70,6 @@ namespace BlackCore
|
||||
//! Gets plugin identifier by its instance
|
||||
QString getIdByPlugin(const QObject *instance) const;
|
||||
|
||||
//! Loads the given plugin (if necessary), casts it to the desired
|
||||
//! type and returns its instance. Returns `nullptr` on failure.
|
||||
template <class T>
|
||||
T *getPluginById(const QString &identifier)
|
||||
{
|
||||
return qobject_cast<T *>(getPluginByIdImpl(identifier));
|
||||
}
|
||||
|
||||
//! Gets direct access to all plugins' metadata
|
||||
const BlackMisc::CSequence<QJsonObject> &getPlugins()
|
||||
{
|
||||
@@ -72,6 +77,8 @@ namespace BlackCore
|
||||
}
|
||||
|
||||
private:
|
||||
//! Tries to load the given plugin.
|
||||
bool tryLoad(QString path);
|
||||
|
||||
//! Loads the given plugin (if necessary) and returns its instance.
|
||||
//! Returns `nullptr` on failure.
|
||||
@@ -80,6 +87,7 @@ namespace BlackCore
|
||||
BlackMisc::CSequence<QJsonObject> m_metadatas;
|
||||
QMap<QString, QString> m_paths; //!< identifier <-> file path pairs
|
||||
QMap<QString, QObject *> m_instances; //!< identifier <-> instance pairs
|
||||
QMap<QString, QString> m_configs; //!< identifier <-> identifier pairs
|
||||
QMap<const QObject *, QString> m_instanceIds; //!< instance <-> identifier pairs
|
||||
|
||||
};
|
||||
|
||||
@@ -96,14 +96,20 @@ namespace BlackCore
|
||||
const CSequence<QJsonObject> &plugins = getPlugins();
|
||||
for (const QJsonObject &json : plugins)
|
||||
{
|
||||
auto it = m_plugins.insert(pluginIdentifier(json), {});
|
||||
it->info.convertFromJson(json);
|
||||
QString iid = json["IID"].toString();
|
||||
if (iid == QStringLiteral("org.swift-project.blackcore.simulatorinterface")) {
|
||||
auto it = m_plugins.insert(pluginIdentifier(json), {});
|
||||
it->info.convertFromJson(json);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
BlackMisc::CSequence<QString> CPluginManagerSimulator::acceptedIids() const
|
||||
{
|
||||
return { QStringLiteral("org.swift-project.blackcore.simulatorinterface") };
|
||||
return {
|
||||
QStringLiteral("org.swift-project.blackcore.simulatorinterface"),
|
||||
QStringLiteral("org.swift-project.blackgui.pluginconfiginterface")
|
||||
};
|
||||
}
|
||||
|
||||
QString CPluginManagerSimulator::pluginDirectory() const
|
||||
|
||||
@@ -292,6 +292,5 @@ namespace BlackCore
|
||||
|
||||
Q_DECLARE_INTERFACE(BlackCore::ISimulatorFactory, "org.swift-project.blackcore.simulatorinterface")
|
||||
Q_DECLARE_OPERATORS_FOR_FLAGS(BlackCore::ISimulator::SimulatorStatus)
|
||||
Q_DECLARE_INTERFACE(BlackCore::ISimulatorListener, "org.swift-project.blackcore.simulatorlistener")
|
||||
|
||||
#endif // guard
|
||||
|
||||
Reference in New Issue
Block a user